uncial 0.0.7 → 2.0.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 (93) hide show
  1. package/README.md +82 -15
  2. package/dist/core/attributes.d.ts +21 -3
  3. package/dist/core/attributes.js +86 -15
  4. package/dist/core/defineBlock.js +26 -12
  5. package/dist/core/index.d.ts +3 -1
  6. package/dist/core/index.js +2 -1
  7. package/dist/core/issues.d.ts +7 -0
  8. package/dist/core/issues.js +9 -0
  9. package/dist/core/meta.js +2 -7
  10. package/dist/core/migrations.d.ts +30 -0
  11. package/dist/core/migrations.js +50 -0
  12. package/dist/core/normalize.d.ts +12 -1
  13. package/dist/core/normalize.js +123 -22
  14. package/dist/core/registry.d.ts +7 -0
  15. package/dist/core/registry.js +35 -4
  16. package/dist/core/runtime.d.ts +7 -0
  17. package/dist/core/types.d.ts +31 -3
  18. package/dist/core/validate.js +27 -7
  19. package/dist/editor/AttributeFieldControl.svelte +155 -7
  20. package/dist/editor/AttributeFieldControl.svelte.d.ts +1 -0
  21. package/dist/editor/BlockAttributesPanel.svelte +76 -44
  22. package/dist/editor/BlockAttributesPanel.svelte.d.ts +9 -1
  23. package/dist/editor/Editor.svelte +149 -27
  24. package/dist/editor/Editor.svelte.d.ts +37 -0
  25. package/dist/editor/RichTextAttributeEditor.svelte +75 -145
  26. package/dist/editor/RichTextAttributeEditor.svelte.d.ts +2 -1
  27. package/dist/editor/Toolbar.svelte +2 -3
  28. package/dist/editor/attributeTargets.d.ts +23 -0
  29. package/dist/editor/attributeTargets.js +21 -0
  30. package/dist/editor/attributesController.d.ts +12 -0
  31. package/dist/editor/attributesController.js +142 -230
  32. package/dist/editor/bindEditor.d.ts +7 -0
  33. package/dist/editor/bindEditor.js +141 -27
  34. package/dist/editor/chooseAttribute.d.ts +26 -0
  35. package/dist/editor/chooseAttribute.js +10 -0
  36. package/dist/editor/containerChildOps.d.ts +33 -0
  37. package/dist/editor/containerChildOps.js +115 -0
  38. package/dist/editor/dropdownDismiss.d.ts +18 -0
  39. package/dist/editor/dropdownDismiss.js +46 -0
  40. package/dist/editor/dropdownFloat.d.ts +16 -0
  41. package/dist/editor/dropdownFloat.js +127 -0
  42. package/dist/editor/index.d.ts +3 -1
  43. package/dist/editor/index.js +2 -1
  44. package/dist/editor/linkController.d.ts +19 -0
  45. package/dist/editor/linkController.js +72 -0
  46. package/dist/editor/toolbarFeatures.d.ts +12 -0
  47. package/dist/editor/toolbarFeatures.js +36 -0
  48. package/dist/index.d.ts +1 -0
  49. package/dist/index.js +1 -0
  50. package/dist/render/Renderer.svelte +1 -1
  51. package/dist/render/RichContent.svelte +12 -4
  52. package/dist/render/RichContent.svelte.d.ts +2 -0
  53. package/dist/render/RichNode.svelte +80 -10
  54. package/dist/render/RichNode.svelte.d.ts +2 -0
  55. package/dist/render/Tab.svelte +27 -0
  56. package/dist/render/Tab.svelte.d.ts +10 -0
  57. package/dist/render/Tabs.svelte +57 -0
  58. package/dist/render/Tabs.svelte.d.ts +10 -0
  59. package/dist/render/index.d.ts +3 -0
  60. package/dist/render/index.js +6 -0
  61. package/dist/render/sanitize.d.ts +6 -0
  62. package/dist/render/sanitize.js +20 -1
  63. package/dist/render/tabSelection.svelte.d.ts +8 -0
  64. package/dist/render/tabSelection.svelte.js +40 -0
  65. package/dist/runtime/reactiveProps.svelte.d.ts +16 -0
  66. package/dist/runtime/reactiveProps.svelte.js +16 -0
  67. package/dist/runtime/svelte.d.ts +2 -1
  68. package/dist/runtime/svelte.js +26 -6
  69. package/dist/shared/EditorBlockFixture.svelte +24 -0
  70. package/dist/shared/EditorBlockFixture.svelte.d.ts +9 -0
  71. package/dist/shared/content.js +2 -1
  72. package/dist/shared/documentIdentity.d.ts +6 -0
  73. package/dist/shared/documentIdentity.js +64 -0
  74. package/dist/shared/guards.d.ts +13 -0
  75. package/dist/shared/guards.js +16 -0
  76. package/dist/shared/richText.js +20 -4
  77. package/dist/shared/runtimeId.d.ts +8 -0
  78. package/dist/shared/runtimeId.js +8 -0
  79. package/dist/shared/syntaxHighlight.d.ts +14 -0
  80. package/dist/shared/syntaxHighlight.js +36 -3
  81. package/dist/shared/tiptap.js +140 -27
  82. package/dist/styles/base.css +177 -0
  83. package/dist/styles/prose.css +26 -0
  84. package/dist/styles/tokens.css +53 -69
  85. package/dist/tabs.d.ts +6 -0
  86. package/dist/tabs.js +36 -0
  87. package/dist/web-components/UncialEditor.svelte +23 -7
  88. package/dist/web-components/UncialEditor.svelte.d.ts +7 -2
  89. package/dist/web-components/index.d.ts +45 -10
  90. package/dist/web-components/index.js +98 -36
  91. package/dist/web-components/reactiveProps.svelte.d.ts +10 -0
  92. package/dist/web-components/reactiveProps.svelte.js +13 -0
  93. package/package.json +134 -133
@@ -1,35 +1,39 @@
1
1
  import { get, writable } from 'svelte/store';
2
2
  import { NodeSelection } from '@tiptap/pm/state';
3
- import { getBlockDefaultAttrs } from '../shared/tiptap.js';
4
3
  import { parseBlockDraftAttributes, toAttributeDraftValues } from '../core/attributes.js';
5
- import { CODE_BLOCK_ID, codeBlockAttributeTarget } from '../shared/codeBlockAttributes.js';
6
- import { sanitizeHref } from '../render/sanitize.js';
7
- const INITIAL_STATE = {
8
- open: false,
9
- mode: null,
10
- selectedBlockId: '',
11
- draftAttrs: {},
12
- validationErrors: {},
13
- activeBlock: null,
14
- allowedBlockIds: [],
15
- containerChildren: [],
16
- link: {
4
+ import { CODE_BLOCK_ID } from '../shared/codeBlockAttributes.js';
5
+ import { createAttributeTargets } from './attributeTargets.js';
6
+ import { createLinkController, getActiveLinkAttrs } from './linkController.js';
7
+ import { createContainerChildOps } from './containerChildOps.js';
8
+ /**
9
+ * The single source of truth for a fresh controller state snapshot. Every
10
+ * consumer that needs an initial `BlockAttributesState` (the controller store,
11
+ * `bindEditor`, and the Svelte panels) imports this factory rather than
12
+ * duplicating the literal, so the shape can never drift between them.
13
+ */
14
+ export function createInitialState() {
15
+ return {
17
16
  open: false,
18
- attrs: {}
19
- }
20
- };
21
- function getActiveLinkAttrs(editor) {
22
- if (!editor?.isActive('link'))
23
- return null;
24
- return editor.getAttributes('link');
17
+ mode: null,
18
+ selectedBlockId: '',
19
+ draftAttrs: {},
20
+ validationErrors: {},
21
+ activeBlock: null,
22
+ allowedBlockIds: [],
23
+ containerChildren: [],
24
+ link: {
25
+ open: false,
26
+ attrs: {}
27
+ }
28
+ };
25
29
  }
26
- function findActiveBlock(editor, registry) {
27
- if (!editor || !registry)
30
+ function findActiveBlock(editor, targets) {
31
+ if (!editor)
28
32
  return null;
29
33
  const selection = editor.state.selection;
30
34
  const selectedNode = selection.node;
31
35
  const selectedType = selectedNode?.type?.name;
32
- if (selectedType && (registry.has(selectedType) || selectedType === CODE_BLOCK_ID)) {
36
+ if (selectedType && targets.has(selectedType)) {
33
37
  return {
34
38
  id: selectedType,
35
39
  attrs: selectedNode?.attrs ?? {},
@@ -39,7 +43,7 @@ function findActiveBlock(editor, registry) {
39
43
  for (let depth = selection.$from.depth; depth > 0; depth -= 1) {
40
44
  const node = selection.$from.node(depth);
41
45
  const nodeType = node.type.name;
42
- if (!registry.has(nodeType) && nodeType !== CODE_BLOCK_ID) {
46
+ if (!targets.has(nodeType)) {
43
47
  continue;
44
48
  }
45
49
  return {
@@ -52,7 +56,7 @@ function findActiveBlock(editor, registry) {
52
56
  return null;
53
57
  const adjacentNode = selection.$from.nodeAfter ?? selection.$from.nodeBefore ?? null;
54
58
  const adjacentType = adjacentNode?.type?.name;
55
- if (!adjacentType || (!registry.has(adjacentType) && adjacentType !== CODE_BLOCK_ID))
59
+ if (!adjacentType || !targets.has(adjacentType))
56
60
  return null;
57
61
  return {
58
62
  id: adjacentType,
@@ -60,11 +64,11 @@ function findActiveBlock(editor, registry) {
60
64
  pos: selection.from
61
65
  };
62
66
  }
63
- function findBlockAt(editor, registry, pos) {
64
- if (!editor || !registry || pos < 0 || pos > editor.state.doc.content.size)
67
+ function findBlockAt(editor, targets, pos) {
68
+ if (!editor || pos < 0 || pos > editor.state.doc.content.size)
65
69
  return null;
66
70
  const node = editor.state.doc.nodeAt(pos);
67
- if (!node || (!registry.has(node.type.name) && node.type.name !== CODE_BLOCK_ID))
71
+ if (!node || !targets.has(node.type.name))
68
72
  return null;
69
73
  return {
70
74
  id: node.type.name,
@@ -73,12 +77,42 @@ function findBlockAt(editor, registry, pos) {
73
77
  };
74
78
  }
75
79
  export function createBlockAttributesController() {
76
- const state = writable(INITIAL_STATE);
80
+ const state = writable(createInitialState());
77
81
  let editor = null;
78
82
  let registry = null;
79
83
  let schema = undefined;
80
- let explicitContainerOpenUntil = 0;
84
+ // Attribute-metadata resolver over the current registry plus the virtual code
85
+ // block; rebuilt whenever the registry changes (attach/detach).
86
+ let targets = createAttributeTargets(null);
87
+ // One-shot suppression of `bindEditor`'s auto-open behavior. The controller
88
+ // dispatches its own transactions (selecting a block, mutating a container's
89
+ // children); Tiptap fires the resulting `selectionUpdate`/`update` events
90
+ // SYNCHRONOUSLY inside `view.dispatch`, so a flag that is raised for the
91
+ // duration of the dispatch and lowered immediately after covers exactly those
92
+ // self-triggered echo events and cannot wedge (the `finally` always clears it).
93
+ let suppressAutoOpen = false;
81
94
  let applyingDraftAttrs = false;
95
+ const link = createLinkController(() => editor, state);
96
+ const containerOps = createContainerChildOps({
97
+ getEditor: () => editor,
98
+ getRegistry: () => registry,
99
+ getSchema: () => schema,
100
+ resolveActiveBlock: () => resolveActiveBlock(get(state)),
101
+ dispatch: (tr) => dispatchSuppressingAutoOpen(tr),
102
+ sync: () => syncFromSelection('update')
103
+ });
104
+ function dispatchSuppressingAutoOpen(tr) {
105
+ if (!editor)
106
+ return;
107
+ const previous = suppressAutoOpen;
108
+ suppressAutoOpen = true;
109
+ try {
110
+ editor.view.dispatch(tr);
111
+ }
112
+ finally {
113
+ suppressAutoOpen = previous;
114
+ }
115
+ }
82
116
  function allowedBlocks() {
83
117
  if (!registry)
84
118
  return [];
@@ -86,29 +120,25 @@ export function createBlockAttributesController() {
86
120
  .map((block) => block.id)
87
121
  .filter((id) => !schema || schema.allowedBlocks.has(id));
88
122
  }
89
- function getAttributeTarget(id) {
90
- if (id === CODE_BLOCK_ID)
91
- return codeBlockAttributeTarget;
92
- return registry?.get(id) ?? null;
123
+ function resolveActiveBlock(current) {
124
+ return current.activeBlock ? findBlockAt(editor, targets, current.activeBlock.pos) : null;
93
125
  }
94
126
  function resetDraftForBlock(blockId) {
95
127
  if (!registry || !blockId) {
96
128
  state.update((current) => ({ ...current, draftAttrs: {} }));
97
129
  return;
98
130
  }
99
- const block = getAttributeTarget(blockId);
131
+ const block = targets.get(blockId);
100
132
  state.update((current) => ({
101
133
  ...current,
102
- draftAttrs: block
103
- ? toAttributeDraftValues(block, blockId === CODE_BLOCK_ID ? {} : getBlockDefaultAttrs(block))
104
- : {},
134
+ draftAttrs: block ? toAttributeDraftValues(block, targets.defaultAttrs(blockId)) : {},
105
135
  validationErrors: {}
106
136
  }));
107
137
  }
108
138
  function getContainerChildrenInfo(activeOverride) {
109
139
  if (!editor || !registry)
110
140
  return [];
111
- const active = activeOverride ?? findActiveBlock(editor, registry);
141
+ const active = activeOverride ?? findActiveBlock(editor, targets);
112
142
  if (!active)
113
143
  return [];
114
144
  const block = registry.get(active.id);
@@ -139,16 +169,17 @@ export function createBlockAttributesController() {
139
169
  key: `${child.type.name}:${String(stableAttr ?? '')}:${offset}:${index}`,
140
170
  blockId: child.type.name,
141
171
  label,
142
- summary
172
+ summary,
173
+ pos: active.pos + 1 + offset
143
174
  });
144
175
  });
145
176
  return children;
146
177
  }
147
178
  function syncFromSelection(source = 'selection') {
148
- const active = findActiveBlock(editor, registry);
179
+ const active = findActiveBlock(editor, targets);
149
180
  const activeLinkAttrs = getActiveLinkAttrs(editor);
150
181
  state.update((current) => {
151
- const activeBlock = active ? getAttributeTarget(active.id) : undefined;
182
+ const activeBlock = active ? targets.get(active.id) : undefined;
152
183
  const activeIsCodeBlock = active?.id === CODE_BLOCK_ID;
153
184
  const shouldCloseOnSelection = Boolean(source === 'selection' &&
154
185
  current.open &&
@@ -165,10 +196,8 @@ export function createBlockAttributesController() {
165
196
  validationErrors: {}
166
197
  };
167
198
  }
168
- const currentActive = current.activeBlock
169
- ? findBlockAt(editor, registry, current.activeBlock.pos)
170
- : null;
171
- const currentActiveIsContainer = Boolean(currentActive && getAttributeTarget(currentActive.id)?.content);
199
+ const currentActive = resolveActiveBlock(current);
200
+ const currentActiveIsContainer = Boolean(currentActive && targets.get(currentActive.id)?.content);
172
201
  const shouldPreserveContainerOnUpdate = Boolean(source === 'update' && current.open && current.mode === 'edit' && currentActiveIsContainer);
173
202
  const shouldEditActiveAtom = Boolean((current.open || activeIsCodeBlock) &&
174
203
  active &&
@@ -178,9 +207,7 @@ export function createBlockAttributesController() {
178
207
  const explicitContainerOpen = Boolean(current.open &&
179
208
  current.mode === 'edit' &&
180
209
  currentActiveIsContainer &&
181
- (!shouldEditActiveAtom ||
182
- shouldPreserveContainerOnUpdate ||
183
- Date.now() < explicitContainerOpenUntil));
210
+ (!shouldEditActiveAtom || shouldPreserveContainerOnUpdate));
184
211
  const selectedBlockId = explicitContainerOpen
185
212
  ? currentActive.id
186
213
  : shouldEditActiveAtom
@@ -199,8 +226,8 @@ export function createBlockAttributesController() {
199
226
  ? current.draftAttrs
200
227
  : nextMode === 'edit' && active && activeBlock && active.id === selectedBlockId
201
228
  ? toAttributeDraftValues(activeBlock, active.attrs)
202
- : explicitContainerOpen && currentActive && getAttributeTarget(currentActive.id)
203
- ? toAttributeDraftValues(getAttributeTarget(currentActive.id), currentActive.attrs)
229
+ : explicitContainerOpen && currentActive && targets.get(currentActive.id)
230
+ ? toAttributeDraftValues(targets.get(currentActive.id), currentActive.attrs)
204
231
  : current.draftAttrs;
205
232
  return {
206
233
  ...current,
@@ -209,7 +236,10 @@ export function createBlockAttributesController() {
209
236
  activeBlock: nextActive,
210
237
  mode: nextMode,
211
238
  draftAttrs: nextDraft,
212
- validationErrors: nextMode === 'edit' ? {} : current.validationErrors,
239
+ // A write-through from setDraftAttr echoes back here as an 'update';
240
+ // wiping errors then would hide a still-invalid sibling field's error
241
+ // right after a valid edit to another field.
242
+ validationErrors: nextMode === 'edit' && !shouldPreserveDraftAttrs ? {} : current.validationErrors,
213
243
  containerChildren: getContainerChildrenInfo(nextActive),
214
244
  link: activeLinkAttrs ? { open: true, attrs: activeLinkAttrs } : { open: false, attrs: {} }
215
245
  };
@@ -232,7 +262,7 @@ export function createBlockAttributesController() {
232
262
  selectBlock(blockId);
233
263
  }
234
264
  const current = get(state);
235
- const active = findActiveBlock(editor, registry);
265
+ const active = findActiveBlock(editor, targets);
236
266
  let selectedBlockId = blockId ?? active?.id ?? current.selectedBlockId;
237
267
  if (!selectedBlockId && current.activeBlock?.id) {
238
268
  selectedBlockId = current.activeBlock.id;
@@ -241,7 +271,7 @@ export function createBlockAttributesController() {
241
271
  if (!selectedBlockId)
242
272
  return;
243
273
  const mode = active?.id === selectedBlockId ? 'edit' : 'insert';
244
- const activeTarget = active ? getAttributeTarget(active.id) : null;
274
+ const activeTarget = active ? targets.get(active.id) : null;
245
275
  const draftAttrs = mode === 'edit' && active && activeTarget
246
276
  ? toAttributeDraftValues(activeTarget, active.attrs)
247
277
  : get(state).draftAttrs;
@@ -255,16 +285,14 @@ export function createBlockAttributesController() {
255
285
  }));
256
286
  }
257
287
  function openAttributesAt(pos) {
258
- const active = findBlockAt(editor, registry, pos);
288
+ const active = findBlockAt(editor, targets, pos);
259
289
  if (!active || !registry)
260
290
  return;
261
- const block = getAttributeTarget(active.id);
291
+ const block = targets.get(active.id);
262
292
  if (!block)
263
293
  return;
264
- explicitContainerOpenUntil = block.content ? Date.now() + 300 : 0;
265
294
  if (editor) {
266
- const tr = editor.state.tr.setSelection(NodeSelection.create(editor.state.doc, active.pos));
267
- editor.view.dispatch(tr);
295
+ dispatchSuppressingAutoOpen(editor.state.tr.setSelection(NodeSelection.create(editor.state.doc, active.pos)));
268
296
  }
269
297
  state.update((current) => ({
270
298
  ...current,
@@ -285,62 +313,6 @@ export function createBlockAttributesController() {
285
313
  validationErrors: {}
286
314
  }));
287
315
  }
288
- function openLinkAttributes() {
289
- if (!editor)
290
- return;
291
- const attrs = getActiveLinkAttrs(editor) ?? { href: '' };
292
- state.update((current) => ({
293
- ...current,
294
- link: { open: true, attrs }
295
- }));
296
- }
297
- function closeLinkAttributes() {
298
- state.update((current) => ({
299
- ...current,
300
- link: { open: false, attrs: {} }
301
- }));
302
- }
303
- function setLinkAttr(name, value) {
304
- state.update((current) => ({
305
- ...current,
306
- link: {
307
- open: true,
308
- attrs: {
309
- ...current.link.attrs,
310
- [name]: value
311
- }
312
- }
313
- }));
314
- }
315
- function commitLinkAttributes() {
316
- if (!editor)
317
- return false;
318
- const attrs = get(state).link.attrs;
319
- const href = sanitizeHref(attrs.href);
320
- if (!href)
321
- return false;
322
- return editor
323
- .chain()
324
- .focus()
325
- .extendMarkRange('link')
326
- .setMark('link', {
327
- ...attrs,
328
- href,
329
- target: attrs.target || null,
330
- rel: attrs.rel || null,
331
- title: attrs.title?.trim() || null,
332
- class: attrs.class?.trim() || null
333
- })
334
- .run();
335
- }
336
- function removeLink() {
337
- if (!editor)
338
- return false;
339
- const ok = editor.chain().focus().extendMarkRange('link').unsetLink().run();
340
- if (ok)
341
- closeLinkAttributes();
342
- return ok;
343
- }
344
316
  function parseAndValidateDraft(block, draftAttrs) {
345
317
  const attrs = parseBlockDraftAttributes(block, draftAttrs);
346
318
  const validationErrors = Object.fromEntries(Object.entries(block.attributes).flatMap(([name, spec]) => {
@@ -357,6 +329,8 @@ export function createBlockAttributesController() {
357
329
  }
358
330
  function setDraftAttr(name, value) {
359
331
  const current = get(state);
332
+ if (current.selectedBlockId && targets.get(current.selectedBlockId)?.readOnly)
333
+ return;
360
334
  const nextDraftAttrs = {
361
335
  ...current.draftAttrs,
362
336
  [name]: value
@@ -371,23 +345,49 @@ export function createBlockAttributesController() {
371
345
  }));
372
346
  if (current.mode !== 'edit' || !current.activeBlock || !current.selectedBlockId)
373
347
  return;
374
- const block = getAttributeTarget(current.selectedBlockId);
348
+ const block = targets.get(current.selectedBlockId);
375
349
  if (!block)
376
350
  return;
377
351
  const { attrs, validationErrors } = parseAndValidateDraft(block, nextDraftAttrs);
378
- if (Object.keys(validationErrors).length > 0) {
379
- state.update((draft) => ({ ...draft, validationErrors }));
352
+ state.update((draft) => ({ ...draft, validationErrors: { [name]: '', ...validationErrors } }));
353
+ // Only the EDITED field gates its own write-through. An invalid sibling
354
+ // field (a cleared required title, an image block whose required image is
355
+ // not chosen yet) must not silently block live edits to other fields —
356
+ // that reads as "the attribute editor stopped working".
357
+ if (validationErrors[name])
380
358
  return;
381
- }
382
- const active = findBlockAt(editor, registry, current.activeBlock.pos);
359
+ const active = findBlockAt(editor, targets, current.activeBlock.pos);
383
360
  if (!editor || active?.id !== current.selectedBlockId)
384
361
  return;
385
362
  const node = editor.state.doc.nodeAt(active.pos);
386
363
  if (!node)
387
364
  return;
365
+ // Write valid fields through; fields whose draft is invalid keep the
366
+ // node's current (last valid) values.
367
+ const appliedAttrs = { ...attrs };
368
+ for (const invalidName of Object.keys(validationErrors)) {
369
+ appliedAttrs[invalidName] = node.attrs?.[invalidName];
370
+ }
371
+ // Per-key attribute steps, never `setNodeMarkup`: replacing the node's
372
+ // markup rewrites the node, which maps a NodeSelection on it away (the
373
+ // caret lands in the document, taking focus out of the field being typed
374
+ // into) and forces the node view to re-render its whole subtree. An
375
+ // attrs-only edit must leave the document's shape, the selection and the
376
+ // mounted block component exactly where they are, because the panel
377
+ // writes through on every keystroke.
378
+ const tr = editor.state.tr;
379
+ let changed = false;
380
+ for (const [attrName, value] of Object.entries(appliedAttrs)) {
381
+ if (node.attrs?.[attrName] === value)
382
+ continue;
383
+ tr.setNodeAttribute(active.pos, attrName, value);
384
+ changed = true;
385
+ }
386
+ if (!changed)
387
+ return;
388
388
  applyingDraftAttrs = true;
389
389
  try {
390
- editor.view.dispatch(editor.state.tr.setNodeMarkup(active.pos, undefined, attrs));
390
+ editor.view.dispatch(tr);
391
391
  }
392
392
  finally {
393
393
  applyingDraftAttrs = false;
@@ -395,10 +395,9 @@ export function createBlockAttributesController() {
395
395
  }
396
396
  function canEditSelectedBlock(blockId) {
397
397
  const current = get(state);
398
- const explicitActive = current.activeBlock
399
- ? findBlockAt(editor, registry, current.activeBlock.pos)
400
- : null;
401
- return (explicitActive ?? findActiveBlock(editor, registry))?.id === blockId;
398
+ const explicitActive = resolveActiveBlock(current);
399
+ return (!targets.get(blockId)?.readOnly &&
400
+ (explicitActive ?? findActiveBlock(editor, targets))?.id === blockId);
402
401
  }
403
402
  function insertBlock(blockId, attrs = {}) {
404
403
  if (!editor || !registry?.has(blockId))
@@ -424,9 +423,11 @@ export function createBlockAttributesController() {
424
423
  const current = get(state);
425
424
  if (!current.selectedBlockId)
426
425
  return false;
427
- const block = getAttributeTarget(current.selectedBlockId);
426
+ const block = targets.get(current.selectedBlockId);
428
427
  if (!block)
429
428
  return false;
429
+ if (current.mode === 'edit' && block.readOnly)
430
+ return false;
430
431
  const { attrs, validationErrors } = parseAndValidateDraft(block, current.draftAttrs);
431
432
  if (Object.keys(validationErrors).length > 0) {
432
433
  state.update((draft) => ({ ...draft, validationErrors }));
@@ -434,7 +435,7 @@ export function createBlockAttributesController() {
434
435
  }
435
436
  let ok = false;
436
437
  if (current.mode === 'edit' && current.activeBlock) {
437
- const active = findBlockAt(editor, registry, current.activeBlock.pos);
438
+ const active = findBlockAt(editor, targets, current.activeBlock.pos);
438
439
  if (editor && active?.id === current.selectedBlockId) {
439
440
  const node = editor.state.doc.nodeAt(active.pos);
440
441
  if (node) {
@@ -456,6 +457,7 @@ export function createBlockAttributesController() {
456
457
  editor = nextEditor;
457
458
  registry = nextRegistry;
458
459
  schema = nextSchema;
460
+ targets = createAttributeTargets(nextRegistry);
459
461
  state.update((current) => ({
460
462
  ...current,
461
463
  allowedBlockIds: allowedBlocks()
@@ -466,85 +468,20 @@ export function createBlockAttributesController() {
466
468
  editor = null;
467
469
  registry = null;
468
470
  schema = undefined;
469
- state.set(INITIAL_STATE);
471
+ targets = createAttributeTargets(null);
472
+ state.set(createInitialState());
470
473
  }
471
474
  function getActiveBlock() {
472
- return findActiveBlock(editor, registry);
475
+ return findActiveBlock(editor, targets);
473
476
  }
474
477
  function isSelectionAutoOpenSuppressed() {
475
- return Date.now() < explicitContainerOpenUntil;
476
- }
477
- function moveContainerChild(fromIndex, toIndex) {
478
- if (!editor || !registry || fromIndex === toIndex)
479
- return false;
480
- const current = get(state);
481
- const active = current.activeBlock
482
- ? findBlockAt(editor, registry, current.activeBlock.pos)
483
- : null;
484
- if (!active)
485
- return false;
486
- const block = registry.get(active.id);
487
- if (!block || !block.content)
488
- return false;
489
- const node = editor.state.doc.nodeAt(active.pos);
490
- if (!node)
491
- return false;
492
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
493
- const children = [];
494
- node.forEach((child) => children.push(child));
495
- if (fromIndex < 0 ||
496
- fromIndex >= children.length ||
497
- toIndex < 0 ||
498
- toIndex >= children.length) {
499
- return false;
500
- }
501
- const [moved] = children.splice(fromIndex, 1);
502
- children.splice(toIndex, 0, moved);
503
- const { tr } = editor.state;
504
- const contentStart = active.pos + 1;
505
- const contentEnd = active.pos + node.nodeSize - 1;
506
- explicitContainerOpenUntil = Date.now() + 1000;
507
- tr.replaceWith(contentStart, contentEnd, children);
508
- editor.view.dispatch(tr);
509
- syncFromSelection('update');
510
- return true;
511
- }
512
- function insertContainerChild(blockId, attrs) {
513
- if (!editor || !registry?.has(blockId))
514
- return false;
515
- if (schema && !schema.allowedBlocks.has(blockId))
516
- return false;
517
- const current = get(state);
518
- const active = current.activeBlock
519
- ? findBlockAt(editor, registry, current.activeBlock.pos)
520
- : null;
521
- if (!active)
522
- return false;
523
- const containerBlock = registry.get(active.id);
524
- if (!containerBlock?.content)
525
- return false;
526
- const containerNode = editor.state.doc.nodeAt(active.pos);
527
- if (!containerNode)
528
- return false;
529
- const childBlock = registry.get(blockId);
530
- const childNodeType = editor.schema.nodes[blockId];
531
- if (!childBlock || !childNodeType)
532
- return false;
533
- const childNode = childNodeType.createAndFill(attrs ?? getBlockDefaultAttrs(childBlock));
534
- if (!childNode)
535
- return false;
536
- explicitContainerOpenUntil = Date.now() + 1000;
537
- editor.view.dispatch(editor.state.tr.insert(active.pos + containerNode.nodeSize - 1, childNode));
538
- syncFromSelection('update');
539
- return true;
478
+ return suppressAutoOpen;
540
479
  }
541
480
  function removeActiveBlock() {
542
481
  if (!editor || !registry)
543
482
  return false;
544
483
  const current = get(state);
545
- const active = current.activeBlock
546
- ? findBlockAt(editor, registry, current.activeBlock.pos)
547
- : null;
484
+ const active = resolveActiveBlock(current);
548
485
  if (!active)
549
486
  return false;
550
487
  const node = editor.state.doc.nodeAt(active.pos);
@@ -555,31 +492,6 @@ export function createBlockAttributesController() {
555
492
  syncFromSelection('update');
556
493
  return true;
557
494
  }
558
- function removeContainerChild(index) {
559
- if (!editor || !registry)
560
- return false;
561
- const current = get(state);
562
- const active = current.activeBlock
563
- ? findBlockAt(editor, registry, current.activeBlock.pos)
564
- : null;
565
- if (!active)
566
- return false;
567
- const block = registry.get(active.id);
568
- if (!block?.content)
569
- return false;
570
- const node = editor.state.doc.nodeAt(active.pos);
571
- if (!node || node.childCount <= 1 || index < 0 || index >= node.childCount)
572
- return false;
573
- let childStart = active.pos + 1;
574
- for (let i = 0; i < index; i += 1) {
575
- childStart += node.child(i).nodeSize;
576
- }
577
- const child = node.child(index);
578
- explicitContainerOpenUntil = Date.now() + 1000;
579
- editor.view.dispatch(editor.state.tr.delete(childStart, childStart + child.nodeSize));
580
- syncFromSelection('update');
581
- return true;
582
- }
583
495
  return {
584
496
  subscribe: state.subscribe,
585
497
  attach,
@@ -587,11 +499,11 @@ export function createBlockAttributesController() {
587
499
  openAttributes,
588
500
  openAttributesAt,
589
501
  closeAttributes,
590
- openLinkAttributes,
591
- closeLinkAttributes,
592
- setLinkAttr,
593
- commitLinkAttributes,
594
- removeLink,
502
+ openLinkAttributes: link.open,
503
+ closeLinkAttributes: link.close,
504
+ setLinkAttr: link.setAttr,
505
+ commitLinkAttributes: link.commit,
506
+ removeLink: link.remove,
595
507
  selectBlock,
596
508
  setDraftAttr,
597
509
  commit,
@@ -601,9 +513,9 @@ export function createBlockAttributesController() {
601
513
  getActiveBlock,
602
514
  syncFromSelection,
603
515
  isSelectionAutoOpenSuppressed,
604
- moveContainerChild,
605
- insertContainerChild,
516
+ moveContainerChild: containerOps.move,
517
+ insertContainerChild: containerOps.insert,
606
518
  removeActiveBlock,
607
- removeContainerChild
519
+ removeContainerChild: containerOps.remove
608
520
  };
609
521
  }
@@ -9,6 +9,13 @@ export interface BindEditorOptions {
9
9
  meta?: Record<string, unknown>;
10
10
  extensions?: AnyExtension[];
11
11
  attributesController?: BlockAttributesController | null;
12
+ /**
13
+ * Whether selecting a block opens its attributes panel by itself. Right for
14
+ * a panel that holds a column of its own; wrong for one that floats over the
15
+ * document, where an unasked-for panel covers the page. Off, the block's own
16
+ * gutter label opens it.
17
+ */
18
+ autoOpenAttributes?: boolean;
12
19
  onIssue?: (issue: ValidationIssue) => void;
13
20
  onChange?: (json: JSONContent) => void;
14
21
  onMetaChange?: (meta: Record<string, unknown>) => void;