|
| 1 | +/** |
| 2 | + * Custom comments sidebar (vanilla TypeScript), single file. |
| 3 | + * |
| 4 | + * The load-bearing pattern is `ui.selection.capture()`: |
| 5 | + * |
| 6 | + * The user selects text, clicks Add comment, the textarea takes |
| 7 | + * focus, and the editor's live selection visually clears. A |
| 8 | + * composer that read the live selection at submit time would see |
| 9 | + * `null` and refuse the create. `capture()` returns a frozen |
| 10 | + * snapshot at the moment the composer opens, so |
| 11 | + * `comments.createFromCapture(capture, { text })` anchors the new |
| 12 | + * comment against the original selection regardless of where focus |
| 13 | + * moves afterwards. |
| 14 | + * |
| 15 | + * The other patterns: |
| 16 | + * |
| 17 | + * - `ui.comments.observe(snapshot => ...)` drives the sidebar list |
| 18 | + * from a single subscription. No event-wrapped shape. |
| 19 | + * - `ui.comments.resolve / .reopen / .reply` route through the |
| 20 | + * same Document API that powers DOCX import / export, so changes |
| 21 | + * made here round-trip through Word. |
| 22 | + * - `ui.createScope()` collects every subscription so the whole |
| 23 | + * surface tears down cleanly on `ui.destroy()`. |
| 24 | + */ |
| 25 | + |
| 26 | +import { SuperDoc } from 'superdoc'; |
| 27 | +import { createSuperDocUI, type CommentsSlice, type SelectionCapture } from 'superdoc/ui'; |
| 28 | +import 'superdoc/style.css'; |
| 29 | +import './style.css'; |
| 30 | + |
| 31 | +const superdoc = new SuperDoc({ |
| 32 | + selector: '#editor', |
| 33 | + document: '/test_file.docx', |
| 34 | + documentMode: 'editing', |
| 35 | + user: { name: 'Alex Rivera', email: 'alex@example.com' }, |
| 36 | + modules: { comments: false }, // disable built-in comments UI; we render our own |
| 37 | +}); |
| 38 | + |
| 39 | +const ui = createSuperDocUI({ superdoc }); |
| 40 | +const scope = ui.createScope(); |
| 41 | + |
| 42 | +// DOM handles the example writes into. |
| 43 | +const addBtn = document.querySelector<HTMLButtonElement>('#add-comment')!; |
| 44 | +const composerMount = document.querySelector<HTMLElement>('#composer-mount')!; |
| 45 | +const list = document.querySelector<HTMLUListElement>('#comments')!; |
| 46 | + |
| 47 | +// Add-comment button is enabled only when the editor has a real |
| 48 | +// positional selection. `ui.selection.observe` fires once |
| 49 | +// synchronously and again on every selection change. |
| 50 | +scope.add( |
| 51 | + ui.selection.observe((sel) => { |
| 52 | + addBtn.disabled = sel.empty || sel.selectionTarget == null; |
| 53 | + }), |
| 54 | +); |
| 55 | + |
| 56 | +// The composer mounts only when the user clicks Add comment, and |
| 57 | +// captures the selection at that moment. |
| 58 | +addBtn.addEventListener('click', () => openComposer()); |
| 59 | + |
| 60 | +function openComposer(): void { |
| 61 | + // Capture the selection NOW. The textarea will steal focus next. |
| 62 | + const capture = ui.selection.capture(); |
| 63 | + if (!capture) return; |
| 64 | + |
| 65 | + composerMount.innerHTML = ` |
| 66 | + <div class="composer"> |
| 67 | + <div class="quote">${capture.quotedText ? `"${escape(capture.quotedText)}"` : '<em>No text selection</em>'}</div> |
| 68 | + <textarea autofocus placeholder="Write a comment…"></textarea> |
| 69 | + <div class="actions"> |
| 70 | + <button data-action="cancel">Cancel</button> |
| 71 | + <button data-action="post" class="primary" disabled>Post</button> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + `; |
| 75 | + |
| 76 | + const ta = composerMount.querySelector<HTMLTextAreaElement>('textarea')!; |
| 77 | + const postBtn = composerMount.querySelector<HTMLButtonElement>('button[data-action="post"]')!; |
| 78 | + const cancelBtn = composerMount.querySelector<HTMLButtonElement>('button[data-action="cancel"]')!; |
| 79 | + |
| 80 | + ta.addEventListener('input', () => { |
| 81 | + postBtn.disabled = ta.value.trim().length === 0; |
| 82 | + }); |
| 83 | + ta.focus(); |
| 84 | + |
| 85 | + cancelBtn.addEventListener('click', closeComposer); |
| 86 | + postBtn.addEventListener('click', () => post(capture, ta.value)); |
| 87 | +} |
| 88 | + |
| 89 | +function closeComposer(): void { |
| 90 | + composerMount.innerHTML = ''; |
| 91 | +} |
| 92 | + |
| 93 | +function post(capture: SelectionCapture, raw: string): void { |
| 94 | + const text = raw.trim(); |
| 95 | + if (!text) return; |
| 96 | + const receipt = ui.comments.createFromCapture(capture, { text }); |
| 97 | + if (!receipt.success) { |
| 98 | + console.error('[comments] create failed', receipt); |
| 99 | + return; |
| 100 | + } |
| 101 | + closeComposer(); |
| 102 | +} |
| 103 | + |
| 104 | +// Render the sidebar from the comments slice. One subscription, the |
| 105 | +// whole list re-renders when the snapshot changes. For a real product |
| 106 | +// you'd diff DOM; this example optimises for clarity. |
| 107 | +scope.add( |
| 108 | + ui.comments.observe((snapshot) => renderComments(snapshot)), |
| 109 | +); |
| 110 | + |
| 111 | +function renderComments(snapshot: CommentsSlice): void { |
| 112 | + list.innerHTML = ''; |
| 113 | + if (snapshot.items.length === 0) { |
| 114 | + const empty = document.createElement('li'); |
| 115 | + empty.className = 'empty'; |
| 116 | + empty.textContent = 'No comments yet. Select text and click Add comment.'; |
| 117 | + list.appendChild(empty); |
| 118 | + return; |
| 119 | + } |
| 120 | + for (const c of snapshot.items) { |
| 121 | + if (c.parentCommentId) continue; // replies render under their root, below |
| 122 | + const li = document.createElement('li'); |
| 123 | + li.className = `card${c.status === 'resolved' ? ' resolved' : ''}`; |
| 124 | + li.innerHTML = ` |
| 125 | + <div class="author">${escape(c.creatorName ?? c.creatorEmail ?? 'Unknown')}</div> |
| 126 | + ${c.anchoredText ? `<div class="quote">"${escape(c.anchoredText)}"</div>` : ''} |
| 127 | + <div class="body">${escape(c.text ?? '')}</div> |
| 128 | + <div class="actions"> |
| 129 | + ${c.status === 'resolved' |
| 130 | + ? `<button data-action="reopen" class="primary">Reopen</button>` |
| 131 | + : `<button data-action="resolve">Resolve</button><button data-action="reply">Reply</button>`} |
| 132 | + </div> |
| 133 | + `; |
| 134 | + li.querySelector('[data-action="resolve"]')?.addEventListener('click', () => ui.comments.resolve(c.id)); |
| 135 | + li.querySelector('[data-action="reopen"]')?.addEventListener('click', () => ui.comments.reopen(c.id)); |
| 136 | + li.querySelector('[data-action="reply"]')?.addEventListener('click', () => { |
| 137 | + const text = window.prompt('Reply:'); |
| 138 | + if (text?.trim()) ui.comments.reply(c.id, { text: text.trim() }); |
| 139 | + }); |
| 140 | + list.appendChild(li); |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +function escape(s: string): string { |
| 145 | + return s.replace(/[&<>"]/g, (ch) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[ch]!); |
| 146 | +} |
| 147 | + |
| 148 | +// One teardown for the whole app. ui.destroy() cascades into the scope. |
| 149 | +const teardown = () => { |
| 150 | + ui.destroy(); |
| 151 | + superdoc.destroy(); |
| 152 | +}; |
| 153 | +window.addEventListener('beforeunload', teardown); |
| 154 | +if (import.meta.hot) import.meta.hot.dispose(teardown); |
0 commit comments