| 1 | /** Replace `value.slice(start, end)` with `insert`, then select `selectionStart`–`selectionEnd` of the result. */ |
| 2 | export type Edit |
| 3 | start: number |
| 4 | end: number |
| 5 | insert: string |
| 6 | selectionStart: number |
| 7 | selectionEnd: number |
| 8 | }; |
| 9 | |
| 10 | export const FORMATS = |
| 11 | "heading", |
| 12 | "bold", |
| 13 | "italic", |
| 14 | "quote", |
| 15 | "code", |
| 16 | "link", |
| 17 | "bulleted", |
| 18 | "numbered", |
| 19 | "task", |
| 20 | ] as const; |
| 21 | export type Format |
| 22 | |
| 23 | const WRAPS: Partial |
| 24 | bold: ["**", |
| 25 | italic: ["_", |
| 26 | code: ["`", |
| 27 | link: ["[", |
| 28 | }; |
| 29 | |
| 30 | const LINE_PREFIXES: Partial |
| 31 | heading: () => |
| 32 | quote: () => |
| 33 | bulleted: () => |
| 34 | numbered: (index) |
| 35 | task: () => |
| 36 | }; |
| 37 | |
| 38 | /** The edit a toolbar button or shortcut makes to the selection. */ |
| 39 | export function formatEdit( |
| 40 | value: string, |
| 41 | start: number, |
| 42 | end: number, |
| 43 | format: Format |
| 44 | ): Edit { |
| 45 | const selected = |
| 46 | |
| 47 | if (format === |
| 48 | const insert = |
| 49 | return { |
| 50 | start, |
| 51 | end, |
| 52 | insert, |
| 53 | selectionStart: start + |
| 54 | selectionEnd: start + |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | const wrap = |
| 59 | if (wrap) { |
| 60 | const [before, |
| 61 | const insert = |
| 62 | // A link selects its placeholder URL, ready to be typed over; anything else keeps the text selected inside its markers. |
| 63 | return format === |
| 64 | ? { |
| 65 | start, |
| 66 | end, |
| 67 | insert, |
| 68 | selectionStart: start + |
| 69 | selectionEnd: start + |
| 70 | } |
| 71 | : { |
| 72 | start, |
| 73 | end, |
| 74 | insert, |
| 75 | selectionStart: start + |
| 76 | selectionEnd: start + |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | const prefix = |
| 81 | const lineStart = |
| 82 | const nextBreak = |
| 83 | "\n", |
| 84 | end > start |
| 85 | ); |
| 86 | const lineEnd = |
| 87 | const insert = |
| 88 | .slice(lineStart, |
| 89 | .split("\n") |
| 90 | .map((line, |
| 91 | .join("\n") |
| 92 | return start === |
| 93 | ? { |
| 94 | start: lineStart, |
| 95 | end: lineEnd, |
| 96 | insert, |
| 97 | selectionStart: lineStart + |
| 98 | selectionEnd: lineStart + |
| 99 | } |
| 100 | : { |
| 101 | start: lineStart, |
| 102 | end: lineEnd, |
| 103 | insert, |
| 104 | selectionStart: lineStart, |
| 105 | selectionEnd: lineStart + |
| 106 | }; |
| 107 | } |
| 108 | |
| 109 | /** `@` or `#` and what follows it, when the caret sits at the end of a mention or reference being typed. */ |
| 110 | export type Mention |
| 111 | |
| 112 | export function mentionAt(value |
| 113 | const match = |
| 114 | if (!match) |
| 115 | return { |
| 116 | trigger: match[1] |
| 117 | query: match[2] |
| 118 | start: caret - |
| 119 | }; |
| 120 | } |
| 121 | |
| 122 | /** Swaps the mention being typed for the chosen `@username` or `#number`, followed by a space unless one is already there. */ |
| 123 | export function completeMention( |
| 124 | value: string, |
| 125 | mention: Mention |
| 126 | caret: number, |
| 127 | completion: string, |
| 128 | ): Edit { |
| 129 | const spaced = |
| 130 | const insert = |
| 131 | const after = |
| 132 | return { |
| 133 | start: mention.start |
| 134 | end: caret, |
| 135 | insert, |
| 136 | selectionStart: after, |
| 137 | selectionEnd: after, |
| 138 | }; |
| 139 | } |
| 140 | |
| 141 | const escapeLabel = ( |
| 142 | |
| 143 | /** What stands in the text while a file uploads, so it can be found and replaced once it lands. */ |
| 144 | export function uploadPlaceholder(name |
| 145 | return `[Uploading ${escapeLabel |
| 146 | } |
| 147 | |
| 148 | /** Images embed; anything else links. */ |
| 149 | export function attachmentMarkdown( |
| 150 | name: string, |
| 151 | url: string, |
| 152 | contentType: string, |
| 153 | ) { |
| 154 | const link = |
| 155 | return contentType.startsWith |
| 156 | } |