@quillmark/svelte 0.42.0 → 0.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CUSTOMIZING.md +78 -0
  2. package/README.md +96 -17
  3. package/THEMING.md +2 -0
  4. package/dist/core/codec/anchor.d.ts +0 -7
  5. package/dist/core/codec/anchor.js +0 -19
  6. package/dist/core/codec/field.js +1 -1
  7. package/dist/core/codec/index.d.ts +4 -7
  8. package/dist/core/codec/index.js +3 -4
  9. package/dist/core/codec/table-view.js +1 -1
  10. package/dist/core/index.js +1 -5
  11. package/dist/core/lifecycle.js +1 -1
  12. package/dist/core/schema.d.ts +6 -0
  13. package/dist/core/schema.js +9 -0
  14. package/dist/index.d.ts +3 -0
  15. package/dist/index.js +5 -3
  16. package/dist/preview/index.d.ts +3 -0
  17. package/dist/preview/index.js +4 -2
  18. package/dist/preview/strings.js +1 -1
  19. package/dist/visual/ArrayField.svelte +1 -6
  20. package/dist/visual/Card.svelte +2 -1
  21. package/dist/visual/MatrixField.svelte +1 -6
  22. package/dist/visual/ProseField.svelte +3 -13
  23. package/dist/visual/ProseValue.svelte +3 -13
  24. package/dist/visual/VariantField.svelte +1 -6
  25. package/dist/visual/VisualEditorInner.svelte +25 -14
  26. package/dist/visual/diagnostics.d.ts +1 -1
  27. package/dist/visual/index.js +4 -5
  28. package/dist/visual/leaves.d.ts +6 -0
  29. package/dist/visual/proposals.js +12 -8
  30. package/dist/visual/review.d.ts +0 -13
  31. package/dist/visual/review.js +1 -105
  32. package/dist/visual/review.svelte.d.ts +6 -0
  33. package/dist/visual/review.svelte.js +18 -1
  34. package/dist/visual/structure.js +1 -1
  35. package/dist/workspace/index.d.ts +5 -0
  36. package/dist/workspace/index.js +7 -0
  37. package/dist/workspace/print.d.ts +13 -0
  38. package/dist/workspace/print.js +67 -0
  39. package/dist/workspace/trap.d.ts +8 -0
  40. package/dist/workspace/trap.js +20 -0
  41. package/dist/workspace/workspace.d.ts +107 -0
  42. package/dist/workspace/workspace.js +346 -0
  43. package/package.json +3 -12
@@ -0,0 +1,107 @@
1
+ import { type Artifact, type ChangeSet, type Diagnostic, type Document, type Engine, type LiveSession, type OutputFormat, type Quill } from '@quillmark/wasm';
2
+ import type { Landing, Place } from '../core/address.js';
3
+ import { type EditorError } from '../core/errors.js';
4
+ import type { ReviewDrawn, ReviewProjection } from '../visual/review.js';
5
+ import type { ActiveLeaf, EditorChange } from '../visual/signals.js';
6
+ /** The editor verbs the workspace calls: a mounted `<VisualEditor>` carries them. */
7
+ export interface WorkspaceEditor {
8
+ setCaret(at: Landing): unknown;
9
+ /** Writes the open review into a copy for the preview to compile. Without it, the preview
10
+ * compiles the document whatever the review holds. */
11
+ projectReview?(target: Document): ReviewProjection;
12
+ /** Brings a review item to the reader, for a pick on what only the projection holds. */
13
+ revealReview?(id: string): unknown;
14
+ }
15
+ /** The preview verbs the workspace calls: a mounted `<Preview>` and a `PreviewController` carry
16
+ * them. */
17
+ export interface WorkspacePreview {
18
+ refresh(change: ChangeSet): void;
19
+ focusPosition(at: Place): void;
20
+ endFollow(): void;
21
+ }
22
+ export interface WorkspaceOptions {
23
+ /** The engine the session opens on. A promise is awaited by the open, so the editor need not
24
+ * wait on it. */
25
+ engine: Engine | Promise<Engine>;
26
+ /** Borrowed: the workspace frees neither the quill nor the document. */
27
+ quill: Quill;
28
+ doc: Document;
29
+ /** The mounted editor, or `undefined` while none is. */
30
+ editor: () => WorkspaceEditor | undefined;
31
+ /** The mounted preview, or `undefined` while none is: a preview mounts once a session
32
+ * stands. */
33
+ preview: () => WorkspacePreview | undefined;
34
+ /** The host's own diagnostics, merged into {@link WorkspaceState.diagnostics} at each state
35
+ * change. Re-read on the next one, so a host whose list changes outside an edit calls
36
+ * `wrote(true)`. */
37
+ diagnostics?: () => readonly Diagnostic[];
38
+ /** Called with the new state on every change. Not called at creation: read `state` then. */
39
+ onState?: (state: WorkspaceState) => void;
40
+ }
41
+ export type WorkspaceStatus =
42
+ /** No session yet: the open is in flight. */
43
+ 'opening'
44
+ /** The session stands and the last compile took the document. */
45
+ | 'ready'
46
+ /** The engine refused the last open or compile; `refused` says with what. A session that
47
+ * stands still paints its last good compile. */
48
+ | 'refused'
49
+ /** The preview could not paint a page, until the next edit. */
50
+ | 'paint-failed'
51
+ /** The render build trapped, in this workspace or another in the process: there is no session
52
+ * and nothing compiles again until the page reloads. Editing continues. */
53
+ | 'trapped';
54
+ export interface WorkspaceState {
55
+ /** The session this workspace opened, until it is disposed. Mount the preview over it. */
56
+ session: LiveSession | undefined;
57
+ status: WorkspaceStatus;
58
+ /** Every producer's diagnostics, deduplicated, for the editor's `diagnostics` prop: what was
59
+ * refused, the schema's verdict, the compile's warnings, then the host's own. */
60
+ diagnostics: Diagnostic[];
61
+ /** What the engine refused the last open or compile with; empty while it compiles. */
62
+ refused: Diagnostic[];
63
+ /** What the quill's backend emits, once the engine is in hand. */
64
+ formats: OutputFormat[];
65
+ /** What the preview shows of the editor's open review: `none` open, the projection with every
66
+ * open proposal taken (`shown`), or the document because the projection did not compile
67
+ * (`failed`). */
68
+ review: 'none' | 'shown' | 'failed';
69
+ }
70
+ export interface Workspace {
71
+ readonly state: WorkspaceState;
72
+ /** What the editor mounts over: the options' own handles. */
73
+ readonly quill: Quill;
74
+ readonly doc: Document;
75
+ /** Handlers for `<VisualEditor>`, to spread onto it or call from the host's own. */
76
+ readonly editor: {
77
+ readonly onChange: (change: EditorChange) => void;
78
+ readonly onCaretMove: (at: Place) => void;
79
+ readonly onActiveLeafChange: (active?: ActiveLeaf) => void;
80
+ readonly onReviewDrawn: (drawn: ReviewDrawn) => void;
81
+ };
82
+ /** Handlers for `<Preview>`, to spread onto it or call from the host's own. */
83
+ readonly preview: {
84
+ readonly onPick: (at: Landing) => void;
85
+ readonly onError: (err: EditorError) => void;
86
+ };
87
+ /** The host wrote to the document itself: `structural` compiles at once, as a card operation
88
+ * does, and otherwise the compile debounces as a keystroke's. */
89
+ wrote(structural?: boolean): void;
90
+ /** Run the pending compile now; with none pending, nothing. Resolves once the page is
91
+ * current, the open in flight included. */
92
+ flush(): Promise<void>;
93
+ /**
94
+ * The document in hand as a file, never an unaccepted proposal, once the pending compile has
95
+ * run and the open in flight landed. `undefined` where the document does not compile, which
96
+ * `state` says why, or the workspace is disposed or trapped. A render the engine refuses
97
+ * throws.
98
+ */
99
+ render(format: OutputFormat): Promise<Artifact | undefined>;
100
+ /**
101
+ * Cancel the pending compile, hand `onState` a state with no session, await `unmounted` so the
102
+ * host's surfaces go first, then free the session this workspace opened. Every call after is a
103
+ * no-op.
104
+ */
105
+ dispose(unmounted?: () => Promise<void>): Promise<void>;
106
+ }
107
+ export declare function createWorkspace(opts: WorkspaceOptions): Workspace;
@@ -0,0 +1,346 @@
1
+ // The glue every shell over the two surfaces writes, packaged as one opt-in policy over one
2
+ // document. Framework-free: the surfaces are reached through getters, and the host renders
3
+ // from the state it is handed.
4
+ import { isQuillmarkError } from '@quillmark/wasm';
5
+ import { errorMessage, reportError } from '../core/errors.js';
6
+ import { backendTrapped, isBackendTrap, latch } from './trap.js';
7
+ const SETTLE_MS = 120;
8
+ /** What makes two diagnostics the same one: one message at two lines of a plate is two. */
9
+ const keyOf = (d) => [
10
+ d.severity,
11
+ d.code ?? '',
12
+ d.path ?? '',
13
+ d.location ? `${d.location.file}:${d.location.line}:${d.location.column}` : '',
14
+ d.message
15
+ ].join(' ');
16
+ function merge(sources) {
17
+ const seen = new Map();
18
+ for (const diags of sources)
19
+ for (const diag of diags) {
20
+ const k = keyOf(diag);
21
+ if (!seen.has(k))
22
+ seen.set(k, diag);
23
+ }
24
+ return [...seen.values()];
25
+ }
26
+ function diagnosticsOf(err) {
27
+ if (isQuillmarkError(err) && err.diagnostics.length > 0)
28
+ return err.diagnostics;
29
+ return [{ severity: 'error', message: errorMessage(err) }];
30
+ }
31
+ function freeQuietly(session) {
32
+ try {
33
+ session?.free();
34
+ }
35
+ catch {
36
+ // A trapped build may refuse the free; the reload reclaims it.
37
+ }
38
+ }
39
+ const SAME = { ids: [], toProjection: (at) => at, toLive: (at) => at };
40
+ export function createWorkspace(opts) {
41
+ const { quill, doc } = opts;
42
+ let session;
43
+ let refused = [];
44
+ let formats = [];
45
+ let paintFailed = false;
46
+ let trapped = backendTrapped();
47
+ let disposed = false;
48
+ let timer;
49
+ let opening;
50
+ /** Whether an edit landed after the open in flight read the document. */
51
+ let behind = false;
52
+ /** The open review items as one key, so a report that changes none compiles nothing. */
53
+ let reviewing = '';
54
+ /** The map between the document's places and those of what the session last compiled. */
55
+ let projection = SAME;
56
+ let review = 'none';
57
+ const warnings = () => (session?.warnings ?? []).flatMap((d) => {
58
+ if (!d.path)
59
+ return [d];
60
+ const path = projection.toLive(d.path);
61
+ return typeof path === 'string' ? [{ ...d, path }] : [];
62
+ });
63
+ const snapshot = () => ({
64
+ session,
65
+ status: trapped
66
+ ? 'trapped'
67
+ : refused.length
68
+ ? 'refused'
69
+ : paintFailed
70
+ ? 'paint-failed'
71
+ : session
72
+ ? 'ready'
73
+ : 'opening',
74
+ diagnostics: merge([refused, quill.validate(doc), warnings(), opts.diagnostics?.() ?? []]),
75
+ refused,
76
+ formats,
77
+ review
78
+ });
79
+ let state = snapshot();
80
+ function emit() {
81
+ state = snapshot();
82
+ opts.onState?.(state);
83
+ }
84
+ function stop() {
85
+ clearTimeout(timer);
86
+ timer = undefined;
87
+ }
88
+ /** The render build is gone: drop the session for the surfaces to unmount, then free it. */
89
+ function trap(err) {
90
+ if (trapped)
91
+ return;
92
+ latch();
93
+ trapped = true;
94
+ stop();
95
+ if (err !== undefined)
96
+ console.error('[quillmark/ui] the render build trapped', err);
97
+ const lost = session;
98
+ session = undefined;
99
+ refused = [];
100
+ paintFailed = false;
101
+ projection = SAME;
102
+ review = 'none';
103
+ if (!disposed)
104
+ emit();
105
+ setTimeout(() => freeQuietly(lost), 0);
106
+ }
107
+ /** Whether this workspace is trapped, taking up a trap another one in the process latched. */
108
+ function struck() {
109
+ if (!trapped && backendTrapped())
110
+ trap();
111
+ return trapped;
112
+ }
113
+ function open() {
114
+ behind = false;
115
+ const run = (async () => {
116
+ try {
117
+ const engine = await opts.engine;
118
+ formats = await engine.supportedFormats(quill);
119
+ if (struck())
120
+ return;
121
+ const opened = await engine.open(quill, doc);
122
+ if (disposed || struck())
123
+ return freeQuietly(opened);
124
+ session = opened;
125
+ refused = [];
126
+ }
127
+ catch (err) {
128
+ if (disposed)
129
+ return;
130
+ if (isBackendTrap(err))
131
+ return trap(err);
132
+ refused = diagnosticsOf(err);
133
+ }
134
+ finally {
135
+ opening = undefined;
136
+ }
137
+ if (behind || (reviewing && opts.editor()?.projectReview))
138
+ return compile();
139
+ emit();
140
+ })();
141
+ opening = run;
142
+ return run;
143
+ }
144
+ /** Compile the open review into a copy of the document, freed in every path. */
145
+ function project(live) {
146
+ const editor = opts.editor();
147
+ if (!editor?.projectReview)
148
+ return 'none';
149
+ const copy = doc.clone();
150
+ let change;
151
+ try {
152
+ const projected = editor.projectReview(copy);
153
+ if (!projected.ids.length)
154
+ return 'none';
155
+ change = live.update(copy);
156
+ projection = projected;
157
+ }
158
+ catch (err) {
159
+ if (!isBackendTrap(err))
160
+ return 'failed';
161
+ trap(err);
162
+ return 'trapped';
163
+ }
164
+ finally {
165
+ copy.free();
166
+ }
167
+ review = 'shown';
168
+ refused = [];
169
+ opts.preview()?.refresh(change);
170
+ emit();
171
+ return 'shown';
172
+ }
173
+ function compile() {
174
+ stop();
175
+ if (disposed || struck())
176
+ return Promise.resolve();
177
+ if (!session) {
178
+ if (!opening)
179
+ return open();
180
+ behind = true;
181
+ return opening;
182
+ }
183
+ const projected = reviewing ? project(session) : 'none';
184
+ if (projected === 'shown' || projected === 'trapped')
185
+ return Promise.resolve();
186
+ projection = SAME;
187
+ review = projected;
188
+ let change;
189
+ try {
190
+ change = session.update(doc);
191
+ refused = [];
192
+ }
193
+ catch (err) {
194
+ if (isBackendTrap(err)) {
195
+ trap(err);
196
+ return Promise.resolve();
197
+ }
198
+ refused = diagnosticsOf(err);
199
+ }
200
+ if (change)
201
+ opts.preview()?.refresh(change);
202
+ emit();
203
+ return Promise.resolve();
204
+ }
205
+ function wrote(structural = false) {
206
+ if (disposed || trapped)
207
+ return;
208
+ if (paintFailed) {
209
+ paintFailed = false;
210
+ emit();
211
+ }
212
+ if (structural)
213
+ void compile();
214
+ else {
215
+ clearTimeout(timer);
216
+ timer = setTimeout(() => void compile(), SETTLE_MS);
217
+ }
218
+ }
219
+ async function flush() {
220
+ if (timer !== undefined)
221
+ await compile();
222
+ await opening;
223
+ }
224
+ function renderOf(live, format) {
225
+ let artifact;
226
+ try {
227
+ [artifact] = live.render({ format }).artifacts;
228
+ }
229
+ catch (err) {
230
+ if (!isBackendTrap(err))
231
+ throw err;
232
+ trap(err);
233
+ return undefined;
234
+ }
235
+ if (!artifact)
236
+ throw new Error(`the ${quill.backendId} backend emitted no ${format}`);
237
+ return artifact;
238
+ }
239
+ /** The session holds the projection: compile the document for the file, then the projection
240
+ * again for the preview. A document that does not compile leaves the projection painted. */
241
+ function renderDocument(live, format) {
242
+ try {
243
+ live.update(doc);
244
+ }
245
+ catch (err) {
246
+ if (isBackendTrap(err))
247
+ trap(err);
248
+ else {
249
+ refused = diagnosticsOf(err);
250
+ emit();
251
+ }
252
+ return undefined;
253
+ }
254
+ try {
255
+ return renderOf(live, format);
256
+ }
257
+ finally {
258
+ void compile();
259
+ }
260
+ }
261
+ if (!trapped)
262
+ void open();
263
+ return {
264
+ get state() {
265
+ return state;
266
+ },
267
+ quill,
268
+ doc,
269
+ editor: {
270
+ onChange(change) {
271
+ wrote(change.source === 'structure');
272
+ },
273
+ onCaretMove(at) {
274
+ if (disposed)
275
+ return;
276
+ const shown = projection.toProjection(at);
277
+ if (shown)
278
+ opts.preview()?.focusPosition(shown);
279
+ },
280
+ onActiveLeafChange() {
281
+ if (!disposed)
282
+ opts.preview()?.endFollow();
283
+ },
284
+ onReviewDrawn(drawn) {
285
+ const key = drawn.open.join('\n');
286
+ if (disposed || key === reviewing)
287
+ return;
288
+ reviewing = key;
289
+ if (opts.editor()?.projectReview || review !== 'none')
290
+ void compile();
291
+ }
292
+ },
293
+ preview: {
294
+ onPick(at) {
295
+ if (disposed)
296
+ return;
297
+ const editor = opts.editor();
298
+ const live = projection.toLive(at);
299
+ if (!editor || !live)
300
+ return;
301
+ if ('review' in live)
302
+ void editor.revealReview?.(live.review);
303
+ else
304
+ void editor.setCaret(live);
305
+ },
306
+ onError(err) {
307
+ if (err.code !== 'paint-failed')
308
+ return reportError(undefined, err);
309
+ if (disposed || trapped)
310
+ return;
311
+ if (isBackendTrap(err.cause))
312
+ return trap(err.cause);
313
+ if (paintFailed)
314
+ return;
315
+ paintFailed = true;
316
+ emit();
317
+ }
318
+ },
319
+ wrote,
320
+ flush,
321
+ async render(format) {
322
+ if (disposed)
323
+ return undefined;
324
+ await flush();
325
+ if (disposed || struck() || !session)
326
+ return undefined;
327
+ if (review === 'shown')
328
+ return renderDocument(session, format);
329
+ if (refused.length)
330
+ return undefined;
331
+ return renderOf(session, format);
332
+ },
333
+ async dispose(unmounted) {
334
+ if (disposed)
335
+ return;
336
+ disposed = true;
337
+ stop();
338
+ const opened = session;
339
+ session = undefined;
340
+ state = { ...state, session: undefined };
341
+ opts.onState?.(state);
342
+ await unmounted?.();
343
+ opened?.free();
344
+ }
345
+ };
346
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quillmark/svelte",
3
- "version": "0.42.0",
3
+ "version": "0.44.0",
4
4
  "description": "Editor, live-preview and source surfaces over a Quillmark WASM session.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -33,21 +33,11 @@
33
33
  "svelte": "./dist/index.js",
34
34
  "default": "./dist/index.js"
35
35
  },
36
- "./core": {
37
- "types": "./dist/core/index.d.ts",
38
- "svelte": "./dist/core/index.js",
39
- "default": "./dist/core/index.js"
40
- },
41
36
  "./preview": {
42
37
  "types": "./dist/preview/index.d.ts",
43
38
  "svelte": "./dist/preview/index.js",
44
39
  "default": "./dist/preview/index.js"
45
40
  },
46
- "./visual": {
47
- "types": "./dist/visual/index.d.ts",
48
- "svelte": "./dist/visual/index.js",
49
- "default": "./dist/visual/index.js"
50
- },
51
41
  "./preset": {
52
42
  "types": "./dist/preset/index.d.ts",
53
43
  "default": "./dist/preset/index.css"
@@ -56,6 +46,7 @@
56
46
  "files": [
57
47
  "dist",
58
48
  "THEMING.md",
49
+ "CUSTOMIZING.md",
59
50
  "NOTICE"
60
51
  ],
61
52
  "sideEffects": true,
@@ -69,7 +60,7 @@
69
60
  "check": "svelte-check --tsconfig ./tsconfig.json --fail-on-warnings"
70
61
  },
71
62
  "peerDependencies": {
72
- "@quillmark/wasm": ">=0.140.0-0",
63
+ "@quillmark/wasm": ">=0.142.0-0",
73
64
  "svelte": "^5.33.0"
74
65
  },
75
66
  "dependencies": {