|
| 1 | +/** |
| 2 | + * The smallest example that proves why `ui.selection.capture()` exists. |
| 3 | + * |
| 4 | + * The problem: a comment composer cannot read the live selection at |
| 5 | + * submit time. The textarea takes focus the moment the composer |
| 6 | + * opens; the editor's live selection visually clears. By the time |
| 7 | + * the user types and clicks Post, `ui.selection.getSnapshot()` |
| 8 | + * returns null. |
| 9 | + * |
| 10 | + * The fix: `ui.selection.capture()` returns a frozen snapshot of the |
| 11 | + * selection at composer-open time. Hold it across the user's typing. |
| 12 | + * Pass it to `ui.comments.createFromCapture(capture, { text })` at |
| 13 | + * submit. The new comment anchors against the original selection |
| 14 | + * regardless of where focus has moved. |
| 15 | + * |
| 16 | + * This example shows that flow and nothing else. No threading, no |
| 17 | + * resolve / reopen / reply, no toolbar, no mode toggle. For the |
| 18 | + * full Custom UI surface, see `demos/custom-ui` (React). |
| 19 | + */ |
| 20 | + |
| 21 | +import { SuperDoc } from 'superdoc'; |
| 22 | +import { createSuperDocUI } from 'superdoc/ui'; |
| 23 | +import 'superdoc/style.css'; |
| 24 | +import './style.css'; |
| 25 | + |
| 26 | +const superdoc = new SuperDoc({ |
| 27 | + selector: '#editor', |
| 28 | + document: '/test_file.docx', |
| 29 | + documentMode: 'editing', |
| 30 | + user: { name: 'Alex', email: 'alex@example.com' }, |
| 31 | + modules: { comments: false }, // disable built-in comments UI; we render our own |
| 32 | +}); |
| 33 | + |
| 34 | +const ui = createSuperDocUI({ superdoc }); |
| 35 | +const scope = ui.createScope(); |
| 36 | + |
| 37 | +const addBtn = document.querySelector<HTMLButtonElement>('#add-comment')!; |
| 38 | +const composerMount = document.querySelector<HTMLElement>('#composer-mount')!; |
| 39 | +const list = document.querySelector<HTMLUListElement>('#comments')!; |
| 40 | + |
| 41 | +// Enable the Add button only when the editor has a positional |
| 42 | +// selection. `selection.observe` fires once with the initial state |
| 43 | +// and then on every selection change. |
| 44 | +scope.add( |
| 45 | + ui.selection.observe((sel) => { |
| 46 | + addBtn.disabled = sel.empty || sel.selectionTarget == null; |
| 47 | + }), |
| 48 | +); |
| 49 | + |
| 50 | +addBtn.addEventListener('click', () => { |
| 51 | + // Capture NOW, before the textarea steals focus. |
| 52 | + const capture = ui.selection.capture(); |
| 53 | + if (!capture) return; |
| 54 | + |
| 55 | + composerMount.innerHTML = ` |
| 56 | + <div class="composer"> |
| 57 | + <div class="quote">${capture.quotedText ? `"${esc(capture.quotedText)}"` : '<em>No text selected</em>'}</div> |
| 58 | + <textarea autofocus placeholder="Comment on the selection…"></textarea> |
| 59 | + <div class="actions"> |
| 60 | + <button data-action="cancel">Cancel</button> |
| 61 | + <button data-action="post" class="primary" disabled>Post</button> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + `; |
| 65 | + |
| 66 | + const ta = composerMount.querySelector<HTMLTextAreaElement>('textarea')!; |
| 67 | + const post = composerMount.querySelector<HTMLButtonElement>('button[data-action="post"]')!; |
| 68 | + const cancel = composerMount.querySelector<HTMLButtonElement>('button[data-action="cancel"]')!; |
| 69 | + |
| 70 | + ta.addEventListener('input', () => { |
| 71 | + post.disabled = ta.value.trim().length === 0; |
| 72 | + }); |
| 73 | + ta.focus(); |
| 74 | + |
| 75 | + cancel.addEventListener('click', close); |
| 76 | + post.addEventListener('click', () => { |
| 77 | + const text = ta.value.trim(); |
| 78 | + if (!text) return; |
| 79 | + // The captured snapshot still has the original anchor; the live |
| 80 | + // selection has been gone since the textarea took focus. |
| 81 | + const receipt = ui.comments.createFromCapture(capture, { text }); |
| 82 | + if (!receipt.success) console.error('[selection-capture] create failed', receipt); |
| 83 | + close(); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +function close(): void { |
| 88 | + composerMount.innerHTML = ''; |
| 89 | +} |
| 90 | + |
| 91 | +// Render the comments list. One subscription, plain DOM. |
| 92 | +scope.add( |
| 93 | + ui.comments.observe((snapshot) => { |
| 94 | + list.innerHTML = ''; |
| 95 | + if (snapshot.items.length === 0) { |
| 96 | + const empty = document.createElement('li'); |
| 97 | + empty.className = 'empty'; |
| 98 | + empty.textContent = 'No comments yet. Select text and click Add.'; |
| 99 | + list.appendChild(empty); |
| 100 | + return; |
| 101 | + } |
| 102 | + for (const c of snapshot.items) { |
| 103 | + const li = document.createElement('li'); |
| 104 | + li.className = 'card'; |
| 105 | + li.innerHTML = ` |
| 106 | + ${c.anchoredText ? `<div class="quote">"${esc(c.anchoredText)}"</div>` : ''} |
| 107 | + <div class="body">${esc(c.text ?? '')}</div> |
| 108 | + `; |
| 109 | + list.appendChild(li); |
| 110 | + } |
| 111 | + }), |
| 112 | +); |
| 113 | + |
| 114 | +function esc(s: string): string { |
| 115 | + return s.replace(/[&<>"]/g, (ch) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[ch]!); |
| 116 | +} |
| 117 | + |
| 118 | +const teardown = () => { |
| 119 | + ui.destroy(); |
| 120 | + superdoc.destroy(); |
| 121 | +}; |
| 122 | +window.addEventListener('beforeunload', teardown); |
| 123 | +if (import.meta.hot) import.meta.hot.dispose(teardown); |
0 commit comments