|
| 1 | +/** |
| 2 | + * Custom toolbar (vanilla TypeScript), single file. |
| 3 | + * |
| 4 | + * Wires SuperDoc's UI controller to a hand-rolled toolbar. Three |
| 5 | + * patterns to notice: |
| 6 | + * |
| 7 | + * 1. createSuperDocUI({ superdoc }) accepts the SuperDoc instance |
| 8 | + * directly. No cast. |
| 9 | + * 2. ui.createScope() collects every subscription, custom command |
| 10 | + * registration, and DOM listener. ui.destroy() cascades into |
| 11 | + * every scope so consumers tear everything down with one call. |
| 12 | + * 3. BUILT_IN_COMMAND_IDS + ui.commands.has(id) validate a |
| 13 | + * config-driven button list at startup so a typo cannot ship |
| 14 | + * silently. ui.commands.require(id) throws on unknown ids at |
| 15 | + * trusted dispatch sites. |
| 16 | + */ |
| 17 | + |
| 18 | +import { SuperDoc } from 'superdoc'; |
| 19 | +import { |
| 20 | + BUILT_IN_COMMAND_IDS, |
| 21 | + createSuperDocUI, |
| 22 | + type PublicToolbarItemId, |
| 23 | +} from 'superdoc/ui'; |
| 24 | +import 'superdoc/style.css'; |
| 25 | +import './style.css'; |
| 26 | + |
| 27 | +// Compile-time-typed config. TypeScript verifies every id is a real |
| 28 | +// built-in. The runtime check below catches dynamic / config-driven |
| 29 | +// arrays the type system cannot see (feature flags, user settings). |
| 30 | +const BUTTONS: readonly PublicToolbarItemId[] = ['bold', 'italic', 'underline', 'undo', 'redo']; |
| 31 | + |
| 32 | +const LABELS: Partial<Record<PublicToolbarItemId, string>> = { |
| 33 | + bold: 'B', |
| 34 | + italic: 'I', |
| 35 | + underline: 'U', |
| 36 | + undo: '↶', |
| 37 | + redo: '↷', |
| 38 | +}; |
| 39 | + |
| 40 | +const superdoc = new SuperDoc({ |
| 41 | + selector: '#editor', |
| 42 | + document: '/test_file.docx', |
| 43 | + documentMode: 'editing', |
| 44 | +}); |
| 45 | + |
| 46 | +const ui = createSuperDocUI({ superdoc }); |
| 47 | +const scope = ui.createScope(); |
| 48 | + |
| 49 | +// Custom command. scope.register(...) is a passthrough to |
| 50 | +// ui.commands.register(...) that auto-unregisters when the scope (or |
| 51 | +// the controller) is destroyed. |
| 52 | +scope.register({ |
| 53 | + id: 'company.insertClause', |
| 54 | + getState: ({ state }) => ({ disabled: state.selection.selectionTarget == null }), |
| 55 | + execute: ({ editor }) => { |
| 56 | + const target = ui.selection.getSnapshot().selectionTarget; |
| 57 | + if (!target || !editor?.doc?.insert) return false; |
| 58 | + return editor.doc.insert({ target, value: ' [Standard MSA boilerplate] ', type: 'text' }).success; |
| 59 | + }, |
| 60 | +}); |
| 61 | + |
| 62 | +const toolbarEl = document.querySelector<HTMLElement>('#toolbar')!; |
| 63 | + |
| 64 | +// Built-in buttons. Each button binds to its OWN command's state via |
| 65 | +// observe(state => ...), so unrelated state changes never re-render |
| 66 | +// the button. Equivalent to React's useSuperDocCommand(id). |
| 67 | +for (const id of BUTTONS) { |
| 68 | + if (!ui.commands.has(id)) { |
| 69 | + console.warn(`[toolbar] unknown command id: ${id}`); |
| 70 | + continue; |
| 71 | + } |
| 72 | + const handle = ui.commands.require(id); |
| 73 | + const btn = document.createElement('button'); |
| 74 | + btn.className = 'tb-btn'; |
| 75 | + btn.textContent = LABELS[id] ?? id; |
| 76 | + btn.addEventListener('click', () => handle.execute()); |
| 77 | + scope.add( |
| 78 | + handle.observe((state) => { |
| 79 | + btn.classList.toggle('active', !!state.active); |
| 80 | + btn.disabled = !!state.disabled; |
| 81 | + }), |
| 82 | + ); |
| 83 | + toolbarEl.appendChild(btn); |
| 84 | +} |
| 85 | + |
| 86 | +// Custom command button. Same observe / execute shape as built-ins; |
| 87 | +// `ui.commands.require(id)` returns a typed handle for either. |
| 88 | +const customHandle = ui.commands.require('company.insertClause'); |
| 89 | +const insertBtn = document.createElement('button'); |
| 90 | +insertBtn.className = 'tb-btn tb-btn-pill'; |
| 91 | +insertBtn.textContent = 'Insert clause'; |
| 92 | +insertBtn.addEventListener('click', () => { |
| 93 | + void customHandle.execute(); |
| 94 | +}); |
| 95 | +scope.add( |
| 96 | + customHandle.observe((state) => { |
| 97 | + insertBtn.disabled = !!state.disabled; |
| 98 | + }), |
| 99 | +); |
| 100 | +toolbarEl.appendChild(insertBtn); |
| 101 | + |
| 102 | +// Quick reference for consumers reading this file: BUILT_IN_COMMAND_IDS |
| 103 | +// is the readonly list of every valid built-in. Useful for validating |
| 104 | +// configs loaded from outside the type system (feature flags, user |
| 105 | +// settings, plugin manifests). |
| 106 | +void BUILT_IN_COMMAND_IDS; |
| 107 | + |
| 108 | +// One teardown for the whole app. ui.destroy() cascades into every |
| 109 | +// scope created from this controller, so consumers do not need a |
| 110 | +// separate scope.destroy() call. |
| 111 | +const teardown = () => { |
| 112 | + ui.destroy(); |
| 113 | + superdoc.destroy(); |
| 114 | +}; |
| 115 | +window.addEventListener('beforeunload', teardown); |
| 116 | +if (import.meta.hot) import.meta.hot.dispose(teardown); |
0 commit comments