|
| 1 | +/** |
| 2 | + * Source-mode chrome: strike checked tasks, URL tooltips, fence copy. |
| 3 | + */ |
| 4 | + |
| 5 | +import { RangeSetBuilder } from '@codemirror/state'; |
| 6 | +import { |
| 7 | + Decoration, |
| 8 | + EditorView, |
| 9 | + ViewPlugin, |
| 10 | + hoverTooltip, |
| 11 | + type DecorationSet, |
| 12 | + type ViewUpdate, |
| 13 | +} from '@codemirror/view'; |
| 14 | +import { checkedTaskMarks, fenceAt, markdownLinkAt } from '@dripnex/commands'; |
| 15 | + |
| 16 | +const taskMark = Decoration.mark({ class: 'cm-task-checked' }); |
| 17 | + |
| 18 | +function taskDecorations(doc: string): DecorationSet { |
| 19 | + const builder = new RangeSetBuilder<Decoration>(); |
| 20 | + for (const mark of checkedTaskMarks(doc)) { |
| 21 | + if (mark.to > mark.from) builder.add(mark.from, mark.to, taskMark); |
| 22 | + } |
| 23 | + return builder.finish(); |
| 24 | +} |
| 25 | + |
| 26 | +export const checkedTaskStrike = ViewPlugin.fromClass( |
| 27 | + class { |
| 28 | + decorations: DecorationSet; |
| 29 | + |
| 30 | + constructor(view: EditorView) { |
| 31 | + this.decorations = taskDecorations(view.state.doc.toString()); |
| 32 | + } |
| 33 | + |
| 34 | + update(update: ViewUpdate) { |
| 35 | + if (update.docChanged) { |
| 36 | + this.decorations = taskDecorations(update.state.doc.toString()); |
| 37 | + } |
| 38 | + } |
| 39 | + }, |
| 40 | + { decorations: plugin => plugin.decorations } |
| 41 | +); |
| 42 | + |
| 43 | +export const markdownLinkTooltip = hoverTooltip( |
| 44 | + (view, pos) => { |
| 45 | + const line = view.state.doc.lineAt(pos); |
| 46 | + const hit = markdownLinkAt(line.text, pos - line.from); |
| 47 | + if (!hit) return null; |
| 48 | + return { |
| 49 | + pos: line.from + hit.from, |
| 50 | + end: line.from + hit.to, |
| 51 | + above: true, |
| 52 | + create() { |
| 53 | + const dom = document.createElement('div'); |
| 54 | + dom.className = 'cm-md-link-tooltip'; |
| 55 | + dom.textContent = hit.url; |
| 56 | + return { dom }; |
| 57 | + }, |
| 58 | + }; |
| 59 | + }, |
| 60 | + { hoverTime: 400 } |
| 61 | +); |
| 62 | + |
| 63 | +export function fenceCopyExtension( |
| 64 | + copy: (text: string) => void | Promise<void> = text => navigator.clipboard.writeText(text) |
| 65 | +) { |
| 66 | + return ViewPlugin.fromClass( |
| 67 | + class { |
| 68 | + button: HTMLButtonElement; |
| 69 | + label = 'Copy'; |
| 70 | + |
| 71 | + constructor(readonly view: EditorView) { |
| 72 | + this.button = document.createElement('button'); |
| 73 | + this.button.type = 'button'; |
| 74 | + this.button.className = 'cm-fence-copy'; |
| 75 | + this.button.textContent = this.label; |
| 76 | + this.button.setAttribute('aria-label', 'Copy code block'); |
| 77 | + this.button.addEventListener('mousedown', event => event.preventDefault()); |
| 78 | + this.button.addEventListener('click', event => { |
| 79 | + event.preventDefault(); |
| 80 | + event.stopPropagation(); |
| 81 | + const fence = fenceAt( |
| 82 | + this.view.state.doc.toString(), |
| 83 | + this.view.state.selection.main.head |
| 84 | + ); |
| 85 | + if (!fence) return; |
| 86 | + void Promise.resolve(copy(fence.body)).then(() => { |
| 87 | + this.button.textContent = 'Copied'; |
| 88 | + window.setTimeout(() => { |
| 89 | + this.button.textContent = this.label; |
| 90 | + }, 1200); |
| 91 | + }); |
| 92 | + }); |
| 93 | + this.button.hidden = true; |
| 94 | + this.view.scrollDOM.appendChild(this.button); |
| 95 | + this.sync(); |
| 96 | + } |
| 97 | + |
| 98 | + update(update: ViewUpdate) { |
| 99 | + if ( |
| 100 | + update.docChanged || |
| 101 | + update.selectionSet || |
| 102 | + update.viewportChanged || |
| 103 | + update.geometryChanged |
| 104 | + ) { |
| 105 | + this.sync(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + sync() { |
| 110 | + const fence = fenceAt(this.view.state.doc.toString(), this.view.state.selection.main.head); |
| 111 | + if (!fence) { |
| 112 | + this.button.hidden = true; |
| 113 | + return; |
| 114 | + } |
| 115 | + const from = Math.max(fence.openFrom, this.view.viewport.from); |
| 116 | + const coords = this.view.coordsAtPos(from); |
| 117 | + const scroller = this.view.scrollDOM.getBoundingClientRect(); |
| 118 | + if (!coords || coords.top > scroller.bottom - 24) { |
| 119 | + this.button.hidden = true; |
| 120 | + return; |
| 121 | + } |
| 122 | + const top = Math.max(4, coords.top - scroller.top) + this.view.scrollDOM.scrollTop; |
| 123 | + this.button.hidden = false; |
| 124 | + this.button.style.top = `${top}px`; |
| 125 | + } |
| 126 | + |
| 127 | + destroy() { |
| 128 | + this.button.remove(); |
| 129 | + } |
| 130 | + } |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +export const editorPolishExtensions = [ |
| 135 | + checkedTaskStrike, |
| 136 | + markdownLinkTooltip, |
| 137 | + fenceCopyExtension(), |
| 138 | +]; |
0 commit comments