|
| 1 | +import { html, type TemplateResult } from "lit"; |
| 2 | +import { ref } from "lit/directives/ref.js"; |
| 3 | +import { t } from "../../../i18n/index.ts"; |
| 4 | +import type { WorkspaceWidget } from "../types.ts"; |
| 5 | +import type { BuiltinWidgetContext, BuiltinWidgetState } from "./types.ts"; |
| 6 | +import { widgetProps } from "./types.ts"; |
| 7 | + |
| 8 | +const NOTES_PERSIST_DEBOUNCE_MS = 500; |
| 9 | + |
| 10 | +function seedText(widget: WorkspaceWidget): string { |
| 11 | + const text = widgetProps(widget).text; |
| 12 | + return typeof text === "string" ? text : ""; |
| 13 | +} |
| 14 | + |
| 15 | +function isConflict(error: unknown): boolean { |
| 16 | + return error instanceof Error && /version conflict/i.test(error.message); |
| 17 | +} |
| 18 | + |
| 19 | +const editorBindings = new WeakMap<BuiltinWidgetState, (element: Element | undefined) => void>(); |
| 20 | + |
| 21 | +function bindEditor(state: BuiltinWidgetState): (element: Element | undefined) => void { |
| 22 | + const existing = editorBindings.get(state); |
| 23 | + if (existing) { |
| 24 | + return existing; |
| 25 | + } |
| 26 | + let textarea: HTMLTextAreaElement | null = null; |
| 27 | + let timer: ReturnType<typeof setTimeout> | undefined; |
| 28 | + let dirty = false; |
| 29 | + let disconnected = false; |
| 30 | + let disconnectRevision = 0; |
| 31 | + let saving = false; |
| 32 | + let queued = false; |
| 33 | + let latest = ""; |
| 34 | + let version: number | undefined; |
| 35 | + let hydrationPending = true; |
| 36 | + let hydrationStarted = false; |
| 37 | + |
| 38 | + const showStatus = (key: "saveError" | "conflict" | null): void => { |
| 39 | + const status = textarea?.parentElement?.querySelector<HTMLElement>( |
| 40 | + "[data-test-id='workspace-notes-status']", |
| 41 | + ); |
| 42 | + if (!status) { |
| 43 | + return; |
| 44 | + } |
| 45 | + status.dataset.state = key ? "error" : "idle"; |
| 46 | + status.textContent = |
| 47 | + key === "conflict" |
| 48 | + ? t("workspaces.widget.notes.conflict") |
| 49 | + : key === "saveError" |
| 50 | + ? t("workspaces.widget.notes.saveError") |
| 51 | + : ""; |
| 52 | + }; |
| 53 | + |
| 54 | + const persist = async (): Promise<void> => { |
| 55 | + timer = undefined; |
| 56 | + if (disconnected) { |
| 57 | + return; |
| 58 | + } |
| 59 | + if (saving) { |
| 60 | + queued = true; |
| 61 | + return; |
| 62 | + } |
| 63 | + if (version === undefined) { |
| 64 | + if (hydrationPending) { |
| 65 | + queued = true; |
| 66 | + return; |
| 67 | + } |
| 68 | + showStatus("saveError"); |
| 69 | + return; |
| 70 | + } |
| 71 | + saving = true; |
| 72 | + queued = false; |
| 73 | + const value = latest; |
| 74 | + try { |
| 75 | + const result = await state.set(value, version); |
| 76 | + version = result.version; |
| 77 | + if (latest === value) { |
| 78 | + dirty = false; |
| 79 | + } |
| 80 | + showStatus(null); |
| 81 | + } catch (error) { |
| 82 | + showStatus(isConflict(error) ? "conflict" : "saveError"); |
| 83 | + } finally { |
| 84 | + saving = false; |
| 85 | + if (!disconnected && queued && latest !== value) { |
| 86 | + void persist(); |
| 87 | + } |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + const onInput = (): void => { |
| 92 | + if (!textarea) { |
| 93 | + return; |
| 94 | + } |
| 95 | + dirty = true; |
| 96 | + latest = textarea.value; |
| 97 | + showStatus(null); |
| 98 | + if (timer !== undefined) { |
| 99 | + clearTimeout(timer); |
| 100 | + } |
| 101 | + timer = setTimeout(() => void persist(), NOTES_PERSIST_DEBOUNCE_MS); |
| 102 | + }; |
| 103 | + |
| 104 | + const binding = (element: Element | undefined): void => { |
| 105 | + if (!(element instanceof HTMLTextAreaElement)) { |
| 106 | + disconnected = true; |
| 107 | + const revision = ++disconnectRevision; |
| 108 | + queueMicrotask(() => { |
| 109 | + if (!disconnected || revision !== disconnectRevision) { |
| 110 | + return; |
| 111 | + } |
| 112 | + if (timer !== undefined) { |
| 113 | + clearTimeout(timer); |
| 114 | + timer = undefined; |
| 115 | + } |
| 116 | + queued ||= dirty; |
| 117 | + textarea?.removeEventListener("input", onInput); |
| 118 | + textarea = null; |
| 119 | + }); |
| 120 | + return; |
| 121 | + } |
| 122 | + disconnected = false; |
| 123 | + disconnectRevision += 1; |
| 124 | + if (textarea === element) { |
| 125 | + return; |
| 126 | + } |
| 127 | + textarea?.removeEventListener("input", onInput); |
| 128 | + textarea = element; |
| 129 | + if (dirty) { |
| 130 | + element.value = latest; |
| 131 | + } else { |
| 132 | + latest = element.value; |
| 133 | + } |
| 134 | + element.addEventListener("input", onInput); |
| 135 | + if (version !== undefined) { |
| 136 | + if (dirty && queued && timer === undefined && !saving) { |
| 137 | + timer = setTimeout(() => void persist(), NOTES_PERSIST_DEBOUNCE_MS); |
| 138 | + } |
| 139 | + return; |
| 140 | + } |
| 141 | + if (hydrationStarted) { |
| 142 | + return; |
| 143 | + } |
| 144 | + hydrationStarted = true; |
| 145 | + hydrationPending = true; |
| 146 | + void state |
| 147 | + .get() |
| 148 | + .then((result) => { |
| 149 | + hydrationPending = false; |
| 150 | + if (disconnected) { |
| 151 | + hydrationStarted = false; |
| 152 | + return; |
| 153 | + } |
| 154 | + version = result.version; |
| 155 | + if (!dirty && typeof result.state === "string" && textarea) { |
| 156 | + textarea.value = result.state; |
| 157 | + latest = result.state; |
| 158 | + } |
| 159 | + if (dirty && queued) { |
| 160 | + void persist(); |
| 161 | + } |
| 162 | + }) |
| 163 | + .catch(() => { |
| 164 | + hydrationPending = false; |
| 165 | + hydrationStarted = false; |
| 166 | + if (dirty) { |
| 167 | + showStatus("saveError"); |
| 168 | + } |
| 169 | + }); |
| 170 | + }; |
| 171 | + editorBindings.set(state, binding); |
| 172 | + return binding; |
| 173 | +} |
| 174 | + |
| 175 | +export function renderNotes( |
| 176 | + widget: WorkspaceWidget, |
| 177 | + _value: unknown, |
| 178 | + context: BuiltinWidgetContext, |
| 179 | +): TemplateResult { |
| 180 | + const seed = seedText(widget); |
| 181 | + const status = context.state ? "" : t("workspaces.widget.notes.readonlyHint"); |
| 182 | + return html` |
| 183 | + <div class="workspace-notes" data-test-id="workspace-notes"> |
| 184 | + <textarea |
| 185 | + class="workspace-notes__pad" |
| 186 | + data-test-id="workspace-notes-pad" |
| 187 | + aria-label=${widget.title ?? t("workspaces.widget.notes.placeholder")} |
| 188 | + placeholder=${t("workspaces.widget.notes.placeholder")} |
| 189 | + ?readonly=${!context.state} |
| 190 | + .value=${seed} |
| 191 | + ${context.state ? ref(bindEditor(context.state)) : null} |
| 192 | + ></textarea> |
| 193 | + <div |
| 194 | + class="workspace-notes__status" |
| 195 | + data-test-id="workspace-notes-status" |
| 196 | + data-state=${context.state ? "idle" : "readonly"} |
| 197 | + role=${context.state ? "alert" : "status"} |
| 198 | + > |
| 199 | + ${status} |
| 200 | + </div> |
| 201 | + </div> |
| 202 | + `; |
| 203 | +} |
0 commit comments