@portabletext/editor 1.44.12 → 1.44.14

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.
@@ -1,14 +1,14 @@
1
1
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
2
2
  import { c } from "react-compiler-runtime";
3
- import React, { createContext, useContext, useEffect, useState, startTransition, Component } from "react";
3
+ import React, { useEffect, createContext, useContext, useState, startTransition, Component } from "react";
4
4
  import { ReactEditor, withReact, Slate } from "slate-react";
5
5
  import { useSelector, useActorRef } from "@xstate/react";
6
6
  import debug$g from "debug";
7
7
  import isEqual from "lodash/isEqual.js";
8
- import { Element, Text, Range, Editor, Node, Point, Path, Operation, Transforms, insertText, createEditor } from "slate";
8
+ import { Element, Text, Editor, Path, Operation, Transforms, insertText, Node, Range, Point, createEditor } from "slate";
9
9
  import { setup, stateIn, fromCallback, assign, enqueueActions, emit, assertEvent, and, not, createActor } from "xstate";
10
10
  import { unset, set, setIfMissing, insert, diffMatchPatch as diffMatchPatch$1, applyAll } from "@portabletext/patches";
11
- import { defineType, defineField, isKeySegment, isPortableTextTextBlock, isPortableTextSpan as isPortableTextSpan$1, isPortableTextListBlock } from "@sanity/types";
11
+ import { isPortableTextTextBlock, isKeySegment, isPortableTextSpan as isPortableTextSpan$1, isPortableTextListBlock, defineType, defineField } from "@sanity/types";
12
12
  import flatten from "lodash/flatten.js";
13
13
  import isPlainObject from "lodash/isPlainObject.js";
14
14
  import uniq from "lodash/uniq.js";
@@ -30,120 +30,6 @@ import { defineBehavior, raise, coreBehaviors } from "./behavior.core.js";
30
30
  import { getFocusTextBlock, getPreviousBlock, getNextBlock, getFocusSpan, isSelectionCollapsed, isOverlappingSelection, getSelectedBlocks, isSelectionExpanded } from "./selector.is-overlapping-selection.js";
31
31
  import { Subject } from "rxjs";
32
32
  import { useEffectEvent } from "use-effect-event";
33
- function createEditorSchema(portableTextType) {
34
- if (!portableTextType)
35
- throw new Error("Parameter 'portabletextType' missing (required)");
36
- const blockType = portableTextType.of?.find(findBlockType);
37
- if (!blockType)
38
- throw new Error("Block type is not defined in this schema (required)");
39
- const childrenField = blockType.fields?.find((field) => field.name === "children");
40
- if (!childrenField)
41
- throw new Error("Children field for block type found in schema (required)");
42
- const ofType = childrenField.type.of;
43
- if (!ofType)
44
- throw new Error("Valid types for block children not found in schema (required)");
45
- const spanType = ofType.find((memberType) => memberType.name === "span");
46
- if (!spanType)
47
- throw new Error("Span type not found in schema (required)");
48
- const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes = portableTextType.of?.filter((field) => field.name !== blockType.name) || [];
49
- return {
50
- styles: resolveEnabledStyles(blockType),
51
- decorators: resolveEnabledDecorators(spanType),
52
- lists: resolveEnabledListItems(blockType),
53
- block: blockType,
54
- span: spanType,
55
- portableText: portableTextType,
56
- inlineObjects: inlineObjectTypes,
57
- blockObjects: blockObjectTypes,
58
- annotations: spanType.annotations
59
- };
60
- }
61
- function resolveEnabledStyles(blockType) {
62
- const styleField = blockType.fields?.find((btField) => btField.name === "style");
63
- if (!styleField)
64
- throw new Error("A field with name 'style' is not defined in the block type (required).");
65
- const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter((style) => style.value);
66
- if (!textStyles || textStyles.length === 0)
67
- throw new Error("The style fields need at least one style defined. I.e: {title: 'Normal', value: 'normal'}.");
68
- return textStyles;
69
- }
70
- function resolveEnabledDecorators(spanType) {
71
- return spanType.decorators;
72
- }
73
- function resolveEnabledListItems(blockType) {
74
- const listField = blockType.fields?.find((btField) => btField.name === "listItem");
75
- if (!listField)
76
- throw new Error("A field with name 'listItem' is not defined in the block type (required).");
77
- const listItems = listField.type.options?.list && listField.type.options.list.filter((list) => list.value);
78
- if (!listItems)
79
- throw new Error("The list field need at least to be an empty array");
80
- return listItems;
81
- }
82
- function findBlockType(type) {
83
- return type.type ? findBlockType(type.type) : type.name === "block" ? type : null;
84
- }
85
- function defineSchema(definition) {
86
- return definition;
87
- }
88
- function compileSchemaDefinition(definition) {
89
- const blockObjects = definition?.blockObjects?.map((blockObject) => defineType({
90
- type: "object",
91
- // Very naive way to work around `SanitySchema.compile` adding default
92
- // fields to objects with the name `image`
93
- name: blockObject.name === "image" ? "tmp-image" : blockObject.name,
94
- title: blockObject.name === "image" && blockObject.title === void 0 ? "Image" : blockObject.title,
95
- fields: []
96
- })) ?? [], inlineObjects = definition?.inlineObjects?.map((inlineObject) => defineType({
97
- type: "object",
98
- name: inlineObject.name,
99
- title: inlineObject.title,
100
- fields: []
101
- })) ?? [], portableTextSchema = defineField({
102
- type: "array",
103
- name: "portable-text",
104
- of: [...blockObjects.map((blockObject) => ({
105
- type: blockObject.name
106
- })), {
107
- type: "block",
108
- name: "block",
109
- of: inlineObjects.map((inlineObject) => ({
110
- type: inlineObject.name
111
- })),
112
- marks: {
113
- decorators: definition?.decorators?.map((decorator) => ({
114
- title: decorator.title ?? startCase(decorator.name),
115
- value: decorator.name
116
- })) ?? [],
117
- annotations: definition?.annotations?.map((annotation) => ({
118
- name: annotation.name,
119
- type: "object",
120
- title: annotation.title
121
- })) ?? []
122
- },
123
- lists: definition?.lists?.map((list) => ({
124
- value: list.name,
125
- title: list.title ?? startCase(list.name)
126
- })) ?? [],
127
- styles: definition?.styles?.map((style) => ({
128
- value: style.name,
129
- title: style.title ?? startCase(style.name)
130
- })) ?? []
131
- }]
132
- }), schema = Schema.compile({
133
- types: [portableTextSchema, ...blockObjects, ...inlineObjects]
134
- }).get("portable-text"), pteSchema = createEditorSchema(schema);
135
- return {
136
- ...pteSchema,
137
- blockObjects: pteSchema.blockObjects.map((blockObject) => blockObject.name === "tmp-image" ? {
138
- ...blockObject,
139
- name: "image",
140
- type: {
141
- ...blockObject.type,
142
- name: "image"
143
- }
144
- } : blockObject)
145
- };
146
- }
147
33
  const rootName = "sanity-pte:";
148
34
  debug$g(rootName);
149
35
  function debugWithName(name) {
@@ -257,319 +143,86 @@ function fromSlateValue(value, textBlockType, keyMap = {}) {
257
143
  function isEqualToEmptyEditor(children, schemaTypes) {
258
144
  return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && Element.isElement(children[0]) && children[0]._type === schemaTypes.block.name && "style" in children[0] && children[0].style === schemaTypes.styles[0].value && !("listItem" in children[0]) && Array.isArray(children[0].children) && children[0].children.length === 1 && Text.isText(children[0].children[0]) && children[0].children[0]._type === "span" && !children[0].children[0].marks?.join("") && children[0].children[0].text === "";
259
145
  }
260
- function getFocusBlock({
261
- editor
262
- }) {
263
- if (!editor.selection)
264
- return [void 0, void 0];
265
- try {
266
- return Editor.node(editor, editor.selection.focus.path.slice(0, 1)) ?? [void 0, void 0];
267
- } catch {
268
- return [void 0, void 0];
269
- }
270
- }
271
- function getPointBlock({
272
- editor,
273
- point
274
- }) {
275
- try {
276
- const [block] = Editor.node(editor, point.path.slice(0, 1)) ?? [void 0, void 0];
277
- return block ? [block, point.path] : [void 0, void 0];
278
- } catch {
279
- return [void 0, void 0];
280
- }
281
- }
282
- function getFocusChild({
283
- editor
284
- }) {
285
- const [focusBlock, focusBlockPath] = getFocusBlock({
286
- editor
287
- }), childIndex = editor.selection?.focus.path.at(1);
288
- if (!focusBlock || !focusBlockPath || childIndex === void 0)
289
- return [void 0, void 0];
290
- try {
291
- const focusChild = Node.child(focusBlock, childIndex);
292
- return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
293
- } catch {
294
- return [void 0, void 0];
295
- }
296
- }
297
- function getPointChild({
298
- editor,
299
- point
300
- }) {
301
- const [block, blockPath] = getPointBlock({
302
- editor,
303
- point
304
- }), childIndex = point.path.at(1);
305
- if (!block || !blockPath || childIndex === void 0)
306
- return [void 0, void 0];
307
- try {
308
- const pointChild = Node.child(block, childIndex);
309
- return pointChild ? [pointChild, [...blockPath, childIndex]] : [void 0, void 0];
310
- } catch {
311
- return [void 0, void 0];
312
- }
313
- }
314
- function getFirstBlock({
315
- editor
316
- }) {
317
- const firstBlockPath = Editor.start(editor, []).path.at(0);
318
- try {
319
- return firstBlockPath !== void 0 ? Editor.node(editor, [firstBlockPath]) ?? [void 0, void 0] : [void 0, void 0];
320
- } catch {
321
- return [void 0, void 0];
322
- }
323
- }
324
- function getLastBlock({
325
- editor
326
- }) {
327
- const lastBlockPath = Editor.end(editor, []).path.at(0);
328
- try {
329
- return lastBlockPath !== void 0 ? Editor.node(editor, [lastBlockPath]) ?? [void 0, void 0] : [void 0, void 0];
330
- } catch {
331
- return [void 0, void 0];
332
- }
333
- }
334
- function getNodeBlock({
335
- editor,
336
- schema,
337
- node
338
- }) {
339
- if (Editor.isEditor(node))
340
- return;
341
- if (isBlockElement({
342
- editor,
343
- schema
344
- }, node))
345
- return elementToBlock({
346
- schema,
347
- element: node
348
- });
349
- const parent = Array.from(Editor.nodes(editor, {
350
- mode: "highest",
351
- at: [],
352
- match: (n) => isBlockElement({
353
- editor,
354
- schema
355
- }, n) && n.children.some((child) => child._key === node._key)
356
- })).at(0)?.at(0);
357
- return Element.isElement(parent) ? elementToBlock({
358
- schema,
359
- element: parent
360
- }) : void 0;
361
- }
362
- function elementToBlock({
363
- schema,
364
- element
365
- }) {
366
- return fromSlateValue([element], schema.block.name)?.at(0);
367
- }
368
- function isBlockElement({
369
- editor,
370
- schema
371
- }, node) {
372
- return Element.isElement(node) && !editor.isInline(node) && (schema.block.name === node._type || schema.blockObjects.some((blockObject) => blockObject.name === node._type));
373
- }
374
- function isListItemActive({
375
- editor,
376
- listItem
377
- }) {
378
- if (!editor.selection)
379
- return !1;
380
- const selectedBlocks = [...Editor.nodes(editor, {
381
- at: editor.selection,
382
- match: (node) => editor.isTextBlock(node)
383
- })];
384
- return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => editor.isListBlock(node) && node.listItem === listItem) : !1;
385
- }
386
- function isStyleActive({
387
- editor,
388
- style
389
- }) {
390
- if (!editor.selection)
391
- return !1;
392
- const selectedBlocks = [...Editor.nodes(editor, {
393
- at: editor.selection,
394
- match: (node) => editor.isTextBlock(node)
395
- })];
396
- return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => node.style === style) : !1;
397
- }
398
- function slateRangeToSelection({
399
- schema,
400
- editor,
401
- range
402
- }) {
403
- const [anchorBlock] = getPointBlock({
404
- editor,
405
- point: range.anchor
406
- }), [focusBlock] = getPointBlock({
407
- editor,
408
- point: range.focus
409
- });
410
- if (!anchorBlock || !focusBlock)
411
- return null;
412
- const [anchorChild] = anchorBlock._type === schema.block.name ? getPointChild({
413
- editor,
414
- point: range.anchor
415
- }) : [void 0, void 0], [focusChild] = focusBlock._type === schema.block.name ? getPointChild({
416
- editor,
417
- point: range.focus
418
- }) : [void 0, void 0], selection = {
419
- anchor: {
420
- path: [{
421
- _key: anchorBlock._key
422
- }],
423
- offset: range.anchor.offset
424
- },
425
- focus: {
426
- path: [{
427
- _key: focusBlock._key
428
- }],
429
- offset: range.focus.offset
430
- },
431
- backward: Range.isBackward(range)
432
- };
433
- return anchorChild && (selection.anchor.path.push("children"), selection.anchor.path.push({
434
- _key: anchorChild._key
435
- })), focusChild && (selection.focus.path.push("children"), selection.focus.path.push({
436
- _key: focusChild._key
437
- })), selection;
438
- }
439
- function toSlatePath(path, editor) {
440
- if (!editor)
441
- return [];
442
- const [block, blockPath] = Array.from(Editor.nodes(editor, {
443
- at: [],
444
- match: (n) => isKeySegment(path[0]) && n._key === path[0]._key
445
- }))[0] || [void 0, void 0];
446
- if (!block || !Element.isElement(block))
447
- return [];
448
- if (editor.isVoid(block))
449
- return [blockPath[0], 0];
450
- const childPath = [path[2]], childIndex = block.children.findIndex((child) => isEqual([{
451
- _key: child._key
452
- }], childPath));
453
- if (childIndex >= 0 && block.children[childIndex]) {
454
- const child = block.children[childIndex];
455
- return Element.isElement(child) && editor.isVoid(child) ? blockPath.concat(childIndex).concat(0) : blockPath.concat(childIndex);
456
- }
457
- return [blockPath[0], 0];
458
- }
459
- function toSlateRange(selection, editor) {
460
- if (!selection || !editor)
461
- return null;
462
- const anchor = {
463
- path: toSlatePath(selection.anchor.path, editor),
464
- offset: selection.anchor.offset
465
- }, focus = {
466
- path: toSlatePath(selection.focus.path, editor),
467
- offset: selection.focus.offset
468
- };
469
- return focus.path.length === 0 || anchor.path.length === 0 ? null : anchor && focus ? {
470
- anchor,
471
- focus
472
- } : null;
473
- }
474
- function moveRangeByOperation(range, operation) {
475
- const anchor = Point.transform(range.anchor, operation), focus = Point.transform(range.focus, operation);
476
- return anchor === null || focus === null ? null : Point.equals(anchor, range.anchor) && Point.equals(focus, range.focus) ? range : {
477
- anchor,
478
- focus
479
- };
480
- }
481
- const IS_PROCESSING_REMOTE_CHANGES = /* @__PURE__ */ new WeakMap(), KEY_TO_SLATE_ELEMENT = /* @__PURE__ */ new WeakMap(), KEY_TO_VALUE_ELEMENT = /* @__PURE__ */ new WeakMap(), SLATE_TO_PORTABLE_TEXT_RANGE = /* @__PURE__ */ new WeakMap(), EditorActorContext = createContext({}), PortableTextEditorContext = createContext(null), usePortableTextEditor = () => {
482
- const editor = useContext(PortableTextEditorContext);
483
- if (!editor)
484
- throw new Error("The `usePortableTextEditor` hook must be used inside the <PortableTextEditor> component's context.");
485
- return editor;
486
- };
487
- function compileType(rawType) {
488
- return Schema.compile({
489
- name: "blockTypeSchema",
490
- types: [rawType]
491
- }).get(rawType.name);
492
- }
493
- const mutationMachine = setup({
494
- types: {
495
- context: {},
496
- events: {},
497
- input: {},
498
- emitted: {}
499
- },
500
- actions: {
501
- "emit has pending patches": emit({
502
- type: "has pending patches"
503
- }),
504
- "emit mutations": enqueueActions(({
505
- context,
506
- enqueue
507
- }) => {
508
- for (const bulk of context.pendingMutations)
509
- enqueue.emit({
510
- type: "mutation",
511
- patches: bulk.patches,
512
- snapshot: bulk.value
513
- });
514
- }),
515
- "clear pending mutations": assign({
516
- pendingMutations: []
517
- }),
518
- "defer patch": assign({
519
- pendingMutations: ({
520
- context,
521
- event
522
- }) => {
523
- if (assertEvent(event, "patch"), context.pendingMutations.length === 0)
524
- return [{
525
- actionId: event.actionId,
526
- value: event.value,
527
- patches: [event.patch]
528
- }];
529
- const lastBulk = context.pendingMutations.at(-1);
530
- return lastBulk && lastBulk.actionId === event.actionId ? context.pendingMutations.slice(0, -1).concat({
531
- value: event.value,
532
- actionId: lastBulk.actionId,
533
- patches: [...lastBulk.patches, event.patch]
534
- }) : context.pendingMutations.concat({
535
- value: event.value,
536
- actionId: event.actionId,
537
- patches: [event.patch]
538
- });
539
- }
540
- })
541
- },
542
- actors: {
543
- "type listener": fromCallback(({
544
- input,
545
- sendBack
546
- }) => {
547
- const originalApply = input.slateEditor.apply;
548
- return input.slateEditor.apply = (op) => {
549
- op.type === "insert_text" || op.type === "remove_text" ? sendBack({
550
- type: "typing"
551
- }) : sendBack({
552
- type: "not typing"
553
- }), originalApply(op);
554
- }, () => {
555
- input.slateEditor.apply = originalApply;
556
- };
557
- })
558
- },
559
- guards: {
560
- "is typing": stateIn({
561
- typing: "typing"
562
- }),
563
- "no pending mutations": ({
564
- context
565
- }) => context.pendingMutations.length === 0,
566
- "slate is normalizing": ({
567
- context
568
- }) => Editor.isNormalizing(context.slateEditor)
569
- },
570
- delays: {
571
- "mutation debounce": process.env.NODE_ENV === "test" ? 250 : 0,
572
- "type debounce": process.env.NODE_ENV === "test" ? 0 : 250
146
+ const IS_PROCESSING_REMOTE_CHANGES = /* @__PURE__ */ new WeakMap(), KEY_TO_SLATE_ELEMENT = /* @__PURE__ */ new WeakMap(), KEY_TO_VALUE_ELEMENT = /* @__PURE__ */ new WeakMap(), SLATE_TO_PORTABLE_TEXT_RANGE = /* @__PURE__ */ new WeakMap(), mutationMachine = setup({
147
+ types: {
148
+ context: {},
149
+ events: {},
150
+ input: {},
151
+ emitted: {}
152
+ },
153
+ actions: {
154
+ "emit has pending patches": emit({
155
+ type: "has pending patches"
156
+ }),
157
+ "emit mutations": enqueueActions(({
158
+ context,
159
+ enqueue
160
+ }) => {
161
+ for (const bulk of context.pendingMutations)
162
+ enqueue.emit({
163
+ type: "mutation",
164
+ patches: bulk.patches,
165
+ snapshot: bulk.value
166
+ });
167
+ }),
168
+ "clear pending mutations": assign({
169
+ pendingMutations: []
170
+ }),
171
+ "defer patch": assign({
172
+ pendingMutations: ({
173
+ context,
174
+ event
175
+ }) => {
176
+ if (assertEvent(event, "patch"), context.pendingMutations.length === 0)
177
+ return [{
178
+ actionId: event.actionId,
179
+ value: event.value,
180
+ patches: [event.patch]
181
+ }];
182
+ const lastBulk = context.pendingMutations.at(-1);
183
+ return lastBulk && lastBulk.actionId === event.actionId ? context.pendingMutations.slice(0, -1).concat({
184
+ value: event.value,
185
+ actionId: lastBulk.actionId,
186
+ patches: [...lastBulk.patches, event.patch]
187
+ }) : context.pendingMutations.concat({
188
+ value: event.value,
189
+ actionId: event.actionId,
190
+ patches: [event.patch]
191
+ });
192
+ }
193
+ })
194
+ },
195
+ actors: {
196
+ "type listener": fromCallback(({
197
+ input,
198
+ sendBack
199
+ }) => {
200
+ const originalApply = input.slateEditor.apply;
201
+ return input.slateEditor.apply = (op) => {
202
+ op.type === "insert_text" || op.type === "remove_text" ? sendBack({
203
+ type: "typing"
204
+ }) : sendBack({
205
+ type: "not typing"
206
+ }), originalApply(op);
207
+ }, () => {
208
+ input.slateEditor.apply = originalApply;
209
+ };
210
+ })
211
+ },
212
+ guards: {
213
+ "is typing": stateIn({
214
+ typing: "typing"
215
+ }),
216
+ "no pending mutations": ({
217
+ context
218
+ }) => context.pendingMutations.length === 0,
219
+ "slate is normalizing": ({
220
+ context
221
+ }) => Editor.isNormalizing(context.slateEditor)
222
+ },
223
+ delays: {
224
+ "mutation debounce": process.env.NODE_ENV === "test" ? 250 : 0,
225
+ "type debounce": process.env.NODE_ENV === "test" ? 0 : 250
573
226
  }
574
227
  }).createMachine({
575
228
  id: "mutation",
@@ -2676,17 +2329,26 @@ const converterJson = {
2676
2329
  snapshot,
2677
2330
  event
2678
2331
  }) => {
2679
- const blocks = htmlToBlocks(event.data, snapshot.context.schema.portableText, {
2332
+ const parsedBlocks = htmlToBlocks(event.data, snapshot.context.schema.portableText, {
2680
2333
  keyGenerator: snapshot.context.keyGenerator,
2681
2334
  unstable_whitespaceOnPasteMode: snapshot.context.schema.block.options.unstable_whitespaceOnPasteMode
2335
+ }).flatMap((block) => {
2336
+ const parsedBlock = parseBlock({
2337
+ context: snapshot.context,
2338
+ block,
2339
+ options: {
2340
+ refreshKeys: !1
2341
+ }
2342
+ });
2343
+ return parsedBlock ? [parsedBlock] : [];
2682
2344
  });
2683
- return blocks.length === 0 ? {
2345
+ return parsedBlocks.length === 0 ? {
2684
2346
  type: "deserialization.failure",
2685
2347
  mimeType: "text/html",
2686
2348
  reason: "No blocks deserialized"
2687
2349
  } : {
2688
2350
  type: "deserialization.success",
2689
- data: blocks,
2351
+ data: parsedBlocks,
2690
2352
  mimeType: "text/html"
2691
2353
  };
2692
2354
  }
@@ -2718,16 +2380,25 @@ const converterJson = {
2718
2380
  snapshot,
2719
2381
  event
2720
2382
  }) => {
2721
- const textToHtml = `<html><body>${escapeHtml(event.data).split(/\n{2,}/).map((line) => line ? `<p>${line.replace(/(?:\r\n|\r|\n)/g, "<br/>")}</p>` : "<p></p>").join("")}</body></html>`, blocks = htmlToBlocks(textToHtml, snapshot.context.schema.portableText, {
2383
+ const textToHtml = `<html><body>${escapeHtml(event.data).split(/\n{2,}/).map((line) => line ? `<p>${line.replace(/(?:\r\n|\r|\n)/g, "<br/>")}</p>` : "<p></p>").join("")}</body></html>`, parsedBlocks = htmlToBlocks(textToHtml, snapshot.context.schema.portableText, {
2722
2384
  keyGenerator: snapshot.context.keyGenerator
2385
+ }).flatMap((block) => {
2386
+ const parsedBlock = parseBlock({
2387
+ context: snapshot.context,
2388
+ block,
2389
+ options: {
2390
+ refreshKeys: !1
2391
+ }
2392
+ });
2393
+ return parsedBlock ? [parsedBlock] : [];
2723
2394
  });
2724
- return blocks.length === 0 ? {
2395
+ return parsedBlocks.length === 0 ? {
2725
2396
  type: "deserialization.failure",
2726
2397
  mimeType: "text/plain",
2727
2398
  reason: "No blocks deserialized"
2728
2399
  } : {
2729
2400
  type: "deserialization.success",
2730
- data: blocks,
2401
+ data: parsedBlocks,
2731
2402
  mimeType: "text/plain"
2732
2403
  };
2733
2404
  }
@@ -2744,7 +2415,66 @@ const converterJson = {
2744
2415
  function escapeHtml(str) {
2745
2416
  return String(str).replace(/[&<>"'`=/]/g, (s) => entityMap[s]);
2746
2417
  }
2747
- const coreConverters = [converterJson, converterPortableText, converterTextHtml, converterTextPlain], debug$c = debugWithName("operationToPatches");
2418
+ const coreConverters = [converterJson, converterPortableText, converterTextHtml, converterTextPlain];
2419
+ function compileType(rawType) {
2420
+ return Schema.compile({
2421
+ name: "blockTypeSchema",
2422
+ types: [rawType]
2423
+ }).get(rawType.name);
2424
+ }
2425
+ function createEditorSchema(portableTextType) {
2426
+ if (!portableTextType)
2427
+ throw new Error("Parameter 'portabletextType' missing (required)");
2428
+ const blockType = portableTextType.of?.find(findBlockType);
2429
+ if (!blockType)
2430
+ throw new Error("Block type is not defined in this schema (required)");
2431
+ const childrenField = blockType.fields?.find((field) => field.name === "children");
2432
+ if (!childrenField)
2433
+ throw new Error("Children field for block type found in schema (required)");
2434
+ const ofType = childrenField.type.of;
2435
+ if (!ofType)
2436
+ throw new Error("Valid types for block children not found in schema (required)");
2437
+ const spanType = ofType.find((memberType) => memberType.name === "span");
2438
+ if (!spanType)
2439
+ throw new Error("Span type not found in schema (required)");
2440
+ const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes = portableTextType.of?.filter((field) => field.name !== blockType.name) || [];
2441
+ return {
2442
+ styles: resolveEnabledStyles(blockType),
2443
+ decorators: resolveEnabledDecorators(spanType),
2444
+ lists: resolveEnabledListItems(blockType),
2445
+ block: blockType,
2446
+ span: spanType,
2447
+ portableText: portableTextType,
2448
+ inlineObjects: inlineObjectTypes,
2449
+ blockObjects: blockObjectTypes,
2450
+ annotations: spanType.annotations
2451
+ };
2452
+ }
2453
+ function resolveEnabledStyles(blockType) {
2454
+ const styleField = blockType.fields?.find((btField) => btField.name === "style");
2455
+ if (!styleField)
2456
+ throw new Error("A field with name 'style' is not defined in the block type (required).");
2457
+ const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter((style) => style.value);
2458
+ if (!textStyles || textStyles.length === 0)
2459
+ throw new Error("The style fields need at least one style defined. I.e: {title: 'Normal', value: 'normal'}.");
2460
+ return textStyles;
2461
+ }
2462
+ function resolveEnabledDecorators(spanType) {
2463
+ return spanType.decorators;
2464
+ }
2465
+ function resolveEnabledListItems(blockType) {
2466
+ const listField = blockType.fields?.find((btField) => btField.name === "listItem");
2467
+ if (!listField)
2468
+ throw new Error("A field with name 'listItem' is not defined in the block type (required).");
2469
+ const listItems = listField.type.options?.list && listField.type.options.list.filter((list) => list.value);
2470
+ if (!listItems)
2471
+ throw new Error("The list field need at least to be an empty array");
2472
+ return listItems;
2473
+ }
2474
+ function findBlockType(type) {
2475
+ return type.type ? findBlockType(type.type) : type.name === "block" ? type : null;
2476
+ }
2477
+ const debug$c = debugWithName("operationToPatches");
2748
2478
  function createOperationToPatches(types) {
2749
2479
  const textBlockName = types.block.name;
2750
2480
  function insertTextPatch(editor, operation, beforeValue) {
@@ -3507,494 +3237,169 @@ function isDecoratorActive({
3507
3237
  ...Editor.marks(editor) || {}
3508
3238
  }.marks || []).includes(decorator);
3509
3239
  }
3510
- const debug$a = debugWithName("API:editable");
3511
- function createEditableAPI(editor, editorActor) {
3512
- const types = editorActor.getSnapshot().context.schema;
3513
- return {
3514
- focus: () => {
3515
- editorActor.send({
3516
- type: "behavior event",
3517
- behaviorEvent: {
3518
- type: "focus"
3519
- },
3520
- editor
3521
- });
3522
- },
3523
- blur: () => {
3524
- editorActor.send({
3525
- type: "behavior event",
3526
- behaviorEvent: {
3527
- type: "blur"
3528
- },
3529
- editor
3530
- });
3531
- },
3532
- toggleMark: (mark) => {
3533
- editorActor.send({
3534
- type: "behavior event",
3535
- behaviorEvent: {
3536
- type: "decorator.toggle",
3537
- decorator: mark
3538
- },
3539
- editor
3540
- });
3541
- },
3542
- toggleList: (listItem) => {
3543
- editorActor.send({
3544
- type: "behavior event",
3545
- behaviorEvent: {
3546
- type: "list item.toggle",
3547
- listItem
3548
- },
3549
- editor
3240
+ const addAnnotationActionImplementation = ({
3241
+ context,
3242
+ action
3243
+ }) => {
3244
+ const editor = action.editor;
3245
+ if (!editor.selection || Range.isCollapsed(editor.selection))
3246
+ return;
3247
+ let paths, spanPath, markDefPath;
3248
+ const markDefPaths = [], selectedBlocks = Editor.nodes(editor, {
3249
+ at: editor.selection,
3250
+ match: (node) => editor.isTextBlock(node),
3251
+ reverse: Range.isBackward(editor.selection)
3252
+ });
3253
+ for (const [block, blockPath] of selectedBlocks) {
3254
+ if (block.children.length === 0 || block.children.length === 1 && block.children[0].text === "")
3255
+ continue;
3256
+ const annotationKey = context.keyGenerator(), markDefs = block.markDefs ?? [];
3257
+ markDefs.find((markDef) => markDef._type === action.annotation.name && markDef._key === annotationKey) === void 0 && (Transforms.setNodes(editor, {
3258
+ markDefs: [...markDefs, {
3259
+ _type: action.annotation.name,
3260
+ _key: annotationKey,
3261
+ ...action.annotation.value
3262
+ }]
3263
+ }, {
3264
+ at: blockPath
3265
+ }), markDefPath = [{
3266
+ _key: block._key
3267
+ }, "markDefs", {
3268
+ _key: annotationKey
3269
+ }], Range.isBackward(editor.selection) ? markDefPaths.unshift(markDefPath) : markDefPaths.push(markDefPath)), Transforms.setNodes(editor, {}, {
3270
+ match: Text.isText,
3271
+ split: !0
3272
+ });
3273
+ const children = Node.children(editor, blockPath);
3274
+ for (const [span, path] of children) {
3275
+ if (!editor.isTextSpan(span) || !Range.includes(editor.selection, path))
3276
+ continue;
3277
+ const marks = span.marks ?? [], existingSameTypeAnnotations = marks.filter((mark) => markDefs.some((markDef) => markDef._key === mark && markDef._type === action.annotation.name));
3278
+ Transforms.setNodes(editor, {
3279
+ marks: [...marks.filter((mark) => !existingSameTypeAnnotations.includes(mark)), annotationKey]
3280
+ }, {
3281
+ at: path
3282
+ }), spanPath = [{
3283
+ _key: block._key
3284
+ }, "children", {
3285
+ _key: span._key
3286
+ }];
3287
+ }
3288
+ }
3289
+ return markDefPath && spanPath && (paths = {
3290
+ markDefPath,
3291
+ markDefPaths,
3292
+ spanPath
3293
+ }), paths;
3294
+ }, removeAnnotationActionImplementation = ({
3295
+ action
3296
+ }) => {
3297
+ const editor = action.editor;
3298
+ if (editor.selection)
3299
+ if (Range.isCollapsed(editor.selection)) {
3300
+ const [block, blockPath] = Editor.node(editor, editor.selection, {
3301
+ depth: 1
3550
3302
  });
3551
- },
3552
- toggleBlockStyle: (style) => {
3553
- editorActor.send({
3554
- type: "behavior event",
3555
- behaviorEvent: {
3556
- type: "style.toggle",
3557
- style
3558
- },
3559
- editor
3303
+ if (!editor.isTextBlock(block))
3304
+ return;
3305
+ const potentialAnnotations = (block.markDefs ?? []).filter((markDef) => markDef._type === action.annotation.name), [selectedChild, selectedChildPath] = Editor.node(editor, editor.selection, {
3306
+ depth: 2
3560
3307
  });
3561
- },
3562
- isMarkActive: (mark) => {
3563
- try {
3564
- return isDecoratorActive({
3565
- editor,
3566
- decorator: mark
3308
+ if (!editor.isTextSpan(selectedChild))
3309
+ return;
3310
+ const annotationToRemove = selectedChild.marks?.find((mark) => potentialAnnotations.some((markDef) => markDef._key === mark));
3311
+ if (!annotationToRemove)
3312
+ return;
3313
+ const previousSpansWithSameAnnotation = [];
3314
+ for (const [child, childPath] of Node.children(editor, blockPath, {
3315
+ reverse: !0
3316
+ }))
3317
+ if (editor.isTextSpan(child) && Path.isBefore(childPath, selectedChildPath))
3318
+ if (child.marks?.includes(annotationToRemove))
3319
+ previousSpansWithSameAnnotation.push([child, childPath]);
3320
+ else
3321
+ break;
3322
+ const nextSpansWithSameAnnotation = [];
3323
+ for (const [child, childPath] of Node.children(editor, blockPath))
3324
+ if (editor.isTextSpan(child) && Path.isAfter(childPath, selectedChildPath))
3325
+ if (child.marks?.includes(annotationToRemove))
3326
+ nextSpansWithSameAnnotation.push([child, childPath]);
3327
+ else
3328
+ break;
3329
+ for (const [child, childPath] of [...previousSpansWithSameAnnotation, [selectedChild, selectedChildPath], ...nextSpansWithSameAnnotation])
3330
+ Transforms.setNodes(editor, {
3331
+ marks: child.marks?.filter((mark) => mark !== annotationToRemove)
3332
+ }, {
3333
+ at: childPath
3567
3334
  });
3568
- } catch (err) {
3569
- return console.warn(err), !1;
3570
- }
3571
- },
3572
- marks: () => ({
3573
- ...Editor.marks(editor) || {}
3574
- }).marks || [],
3575
- undo: () => {
3576
- editorActor.send({
3577
- type: "behavior event",
3578
- behaviorEvent: {
3579
- type: "history.undo"
3580
- },
3581
- editor
3335
+ } else {
3336
+ Transforms.setNodes(editor, {}, {
3337
+ match: (node) => editor.isTextSpan(node),
3338
+ split: !0,
3339
+ hanging: !0
3582
3340
  });
3583
- },
3584
- redo: () => {
3585
- editorActor.send({
3586
- type: "behavior event",
3587
- behaviorEvent: {
3588
- type: "history.redo"
3589
- },
3590
- editor
3341
+ const blocks = Editor.nodes(editor, {
3342
+ at: editor.selection,
3343
+ match: (node) => editor.isTextBlock(node)
3591
3344
  });
3592
- },
3593
- select: (selection) => {
3594
- const slateSelection = toSlateRange(selection, editor);
3595
- slateSelection ? Transforms.select(editor, slateSelection) : Transforms.deselect(editor), editor.onChange();
3596
- },
3597
- focusBlock: () => {
3598
- if (editor.selection) {
3599
- const block = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
3600
- if (block)
3601
- return fromSlateValue([block], types.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0];
3602
- }
3603
- },
3604
- focusChild: () => {
3605
- if (editor.selection) {
3606
- const block = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
3607
- if (block && editor.isTextBlock(block))
3608
- return fromSlateValue([block], types.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0].children[editor.selection.focus.path[1]];
3609
- }
3610
- },
3611
- insertChild: (type, value) => {
3612
- if (type.name !== types.span.name)
3613
- return editorActor.send({
3614
- type: "behavior event",
3615
- behaviorEvent: {
3616
- type: "insert.inline object",
3617
- inlineObject: {
3618
- name: type.name,
3619
- value
3620
- }
3621
- },
3622
- editor
3623
- }), editor.selection ? slateRangeToSelection({
3624
- schema: editorActor.getSnapshot().context.schema,
3625
- editor,
3626
- range: editor.selection
3627
- })?.focus.path ?? [] : [];
3628
- if (!editor.selection)
3629
- throw new Error("The editor has no selection");
3630
- const [focusBlock] = Array.from(Editor.nodes(editor, {
3631
- at: editor.selection.focus.path.slice(0, 1),
3632
- match: (n) => n._type === types.block.name
3633
- }))[0] || [void 0];
3634
- if (!focusBlock)
3635
- throw new Error("No focused text block");
3636
- if (type.name !== types.span.name && !types.inlineObjects.some((t) => t.name === type.name))
3637
- throw new Error("This type cannot be inserted as a child to a text block");
3638
- const child = toSlateValue([{
3639
- _key: editorActor.getSnapshot().context.keyGenerator(),
3640
- _type: types.block.name,
3641
- children: [{
3642
- _key: editorActor.getSnapshot().context.keyGenerator(),
3643
- _type: type.name,
3644
- ...value || {}
3645
- }]
3646
- }], {
3647
- schemaTypes: editorActor.getSnapshot().context.schema
3648
- })[0].children[0], focusChildPath = editor.selection.focus.path.slice(0, 2), isSpanNode = child._type === types.span.name, focusNode = Node.get(editor, focusChildPath);
3649
- return isSpanNode && focusNode._type !== types.span.name && (debug$a("Inserting span child next to inline object child, moving selection + 1"), editor.move({
3650
- distance: 1,
3651
- unit: "character"
3652
- })), Transforms.insertNodes(editor, child, {
3653
- select: !0,
3654
- at: editor.selection
3655
- }), editor.onChange(), editor.selection ? slateRangeToSelection({
3656
- schema: editorActor.getSnapshot().context.schema,
3657
- editor,
3658
- range: editor.selection
3659
- })?.focus.path ?? [] : [];
3660
- },
3661
- insertBlock: (type, value) => (editorActor.send({
3662
- type: "behavior event",
3663
- behaviorEvent: {
3664
- type: "insert.block",
3665
- block: {
3666
- _type: type.name,
3667
- ...value || {}
3668
- },
3669
- placement: "auto"
3670
- },
3671
- editor
3672
- }), editor.selection ? slateRangeToSelection({
3673
- schema: editorActor.getSnapshot().context.schema,
3674
- editor,
3675
- range: editor.selection
3676
- })?.focus.path ?? [] : []),
3677
- hasBlockStyle: (style) => {
3678
- try {
3679
- return isStyleActive({
3680
- editor,
3681
- style
3682
- });
3683
- } catch {
3684
- return !1;
3685
- }
3686
- },
3687
- hasListStyle: (listItem) => {
3688
- try {
3689
- return isListItemActive({
3690
- editor,
3691
- listItem
3692
- });
3693
- } catch {
3694
- return !1;
3695
- }
3696
- },
3697
- isVoid: (element) => ![types.block.name, types.span.name].includes(element._type),
3698
- findByPath: (path) => {
3699
- const slatePath = toSlateRange({
3700
- focus: {
3701
- path,
3702
- offset: 0
3703
- },
3704
- anchor: {
3705
- path,
3706
- offset: 0
3707
- }
3708
- }, editor);
3709
- if (slatePath) {
3710
- const [block, blockPath] = Editor.node(editor, slatePath.focus.path.slice(0, 1));
3711
- if (block && blockPath && typeof block._key == "string") {
3712
- if (path.length === 1 && slatePath.focus.path.length === 1)
3713
- return [fromSlateValue([block], types.block.name)[0], [{
3714
- _key: block._key
3715
- }]];
3716
- const ptBlock = fromSlateValue([block], types.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0];
3717
- if (editor.isTextBlock(ptBlock)) {
3718
- const ptChild = ptBlock.children[slatePath.focus.path[1]];
3719
- if (ptChild)
3720
- return [ptChild, [{
3721
- _key: block._key
3722
- }, "children", {
3723
- _key: ptChild._key
3724
- }]];
3725
- }
3726
- }
3727
- }
3728
- return [void 0, void 0];
3729
- },
3730
- findDOMNode: (element) => {
3731
- let node;
3732
- try {
3733
- const [item] = Array.from(Editor.nodes(editor, {
3734
- at: [],
3735
- match: (n) => n._key === element._key
3736
- }) || [])[0] || [void 0];
3737
- node = ReactEditor.toDOMNode(editor, item);
3738
- } catch {
3739
- }
3740
- return node;
3741
- },
3742
- activeAnnotations: () => {
3743
- if (!editor.selection || editor.selection.focus.path.length < 2)
3744
- return [];
3745
- try {
3746
- const activeAnnotations = [], spans = Editor.nodes(editor, {
3747
- at: editor.selection,
3748
- match: (node) => Text.isText(node) && node.marks !== void 0 && Array.isArray(node.marks) && node.marks.length > 0
3749
- });
3750
- for (const [span, path] of spans) {
3751
- const [block] = Editor.node(editor, path, {
3752
- depth: 1
3753
- });
3754
- editor.isTextBlock(block) && block.markDefs?.forEach((def) => {
3755
- Text.isText(span) && span.marks && Array.isArray(span.marks) && span.marks.includes(def._key) && activeAnnotations.push(def);
3756
- });
3757
- }
3758
- return activeAnnotations;
3759
- } catch {
3760
- return [];
3761
- }
3762
- },
3763
- isAnnotationActive: (annotationType) => isAnnotationActive({
3764
- editor,
3765
- annotation: {
3766
- name: annotationType
3767
- }
3768
- }),
3769
- addAnnotation: (type, value) => {
3770
- let paths;
3771
- return Editor.withoutNormalizing(editor, () => {
3772
- paths = addAnnotationActionImplementation({
3773
- context: {
3774
- keyGenerator: editorActor.getSnapshot().context.keyGenerator,
3775
- schema: types
3776
- },
3777
- action: {
3778
- annotation: {
3779
- name: type.name,
3780
- value: value ?? {}
3781
- },
3782
- editor
3783
- }
3784
- });
3785
- }), editor.onChange(), paths;
3786
- },
3787
- delete: (selection, options) => {
3788
- if (selection) {
3789
- const range = toSlateRange(selection, editor);
3790
- if (!(range && range.anchor.path.length > 0 && range.focus.path.length > 0))
3791
- throw new Error("Invalid range");
3792
- if (range) {
3793
- if (!options?.mode || options?.mode === "selected") {
3794
- debug$a("Deleting content in selection"), Transforms.delete(editor, {
3795
- at: range,
3796
- hanging: !0,
3797
- voids: !0
3798
- }), editor.onChange();
3799
- return;
3800
- }
3801
- options?.mode === "blocks" && (debug$a("Deleting blocks touched by selection"), Transforms.removeNodes(editor, {
3802
- at: range,
3803
- voids: !0,
3804
- match: (node) => editor.isTextBlock(node) || !editor.isTextBlock(node) && Element.isElement(node)
3805
- })), options?.mode === "children" && (debug$a("Deleting children touched by selection"), Transforms.removeNodes(editor, {
3806
- at: range,
3807
- voids: !0,
3808
- match: (node) => node._type === types.span.name || // Text children
3809
- !editor.isTextBlock(node) && Element.isElement(node)
3810
- })), editor.children.length === 0 && (editor.children = [editor.pteCreateTextBlock({
3811
- decorators: []
3812
- })]), editor.onChange();
3813
- }
3814
- }
3815
- },
3816
- removeAnnotation: (type) => {
3817
- editorActor.send({
3818
- type: "behavior event",
3819
- behaviorEvent: {
3820
- type: "annotation.remove",
3821
- annotation: {
3822
- name: type.name
3823
- }
3824
- },
3825
- editor
3826
- });
3827
- },
3828
- getSelection: () => {
3829
- let ptRange = null;
3830
- if (editor.selection) {
3831
- const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection);
3832
- if (existing)
3833
- return existing;
3834
- ptRange = slateRangeToSelection({
3835
- schema: editorActor.getSnapshot().context.schema,
3836
- editor,
3837
- range: editor.selection
3838
- }), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
3839
- }
3840
- return ptRange;
3841
- },
3842
- getValue: () => fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
3843
- isCollapsedSelection: () => !!editor.selection && Range.isCollapsed(editor.selection),
3844
- isExpandedSelection: () => !!editor.selection && Range.isExpanded(editor.selection),
3845
- insertBreak: () => {
3846
- editor.insertBreak(), editor.onChange();
3847
- },
3848
- getFragment: () => fromSlateValue(editor.getFragment(), types.block.name),
3849
- isSelectionsOverlapping: (selectionA, selectionB) => {
3850
- const rangeA = toSlateRange(selectionA, editor), rangeB = toSlateRange(selectionB, editor);
3851
- return Range.isRange(rangeA) && Range.isRange(rangeB) && Range.includes(rangeA, rangeB);
3852
- }
3853
- };
3854
- }
3855
- function isAnnotationActive({
3856
- editor,
3857
- annotation
3858
- }) {
3859
- if (!editor.selection || editor.selection.focus.path.length < 2)
3860
- return !1;
3861
- try {
3862
- const spans = [...Editor.nodes(editor, {
3863
- at: editor.selection,
3864
- match: (node) => Text.isText(node)
3865
- })];
3866
- if (spans.length === 0 || spans.some(([span]) => !isPortableTextSpan$1(span) || !span.marks || span.marks?.length === 0)) return !1;
3867
- const selectionMarkDefs = spans.reduce((accMarkDefs, [, path]) => {
3868
- const [block] = Editor.node(editor, path, {
3869
- depth: 1
3870
- });
3871
- return editor.isTextBlock(block) && block.markDefs ? [...accMarkDefs, ...block.markDefs] : accMarkDefs;
3872
- }, []);
3873
- return spans.every(([span]) => isPortableTextSpan$1(span) ? span.marks?.map((markKey) => selectionMarkDefs.find((def) => def?._key === markKey)?._type)?.includes(annotation.name) : !1);
3874
- } catch {
3875
- return !1;
3876
- }
3877
- }
3878
- const addAnnotationActionImplementation = ({
3879
- context,
3880
- action
3881
- }) => {
3882
- const editor = action.editor;
3883
- if (!editor.selection || Range.isCollapsed(editor.selection))
3884
- return;
3885
- let paths, spanPath, markDefPath;
3886
- const markDefPaths = [], selectedBlocks = Editor.nodes(editor, {
3887
- at: editor.selection,
3888
- match: (node) => editor.isTextBlock(node),
3889
- reverse: Range.isBackward(editor.selection)
3890
- });
3891
- for (const [block, blockPath] of selectedBlocks) {
3892
- if (block.children.length === 0 || block.children.length === 1 && block.children[0].text === "")
3893
- continue;
3894
- const annotationKey = context.keyGenerator(), markDefs = block.markDefs ?? [];
3895
- markDefs.find((markDef) => markDef._type === action.annotation.name && markDef._key === annotationKey) === void 0 && (Transforms.setNodes(editor, {
3896
- markDefs: [...markDefs, {
3897
- _type: action.annotation.name,
3898
- _key: annotationKey,
3899
- ...action.annotation.value
3900
- }]
3901
- }, {
3902
- at: blockPath
3903
- }), markDefPath = [{
3904
- _key: block._key
3905
- }, "markDefs", {
3906
- _key: annotationKey
3907
- }], Range.isBackward(editor.selection) ? markDefPaths.unshift(markDefPath) : markDefPaths.push(markDefPath)), Transforms.setNodes(editor, {}, {
3908
- match: Text.isText,
3909
- split: !0
3910
- });
3911
- const children = Node.children(editor, blockPath);
3912
- for (const [span, path] of children) {
3913
- if (!editor.isTextSpan(span) || !Range.includes(editor.selection, path))
3914
- continue;
3915
- const marks = span.marks ?? [], existingSameTypeAnnotations = marks.filter((mark) => markDefs.some((markDef) => markDef._key === mark && markDef._type === action.annotation.name));
3916
- Transforms.setNodes(editor, {
3917
- marks: [...marks.filter((mark) => !existingSameTypeAnnotations.includes(mark)), annotationKey]
3918
- }, {
3919
- at: path
3920
- }), spanPath = [{
3921
- _key: block._key
3922
- }, "children", {
3923
- _key: span._key
3924
- }];
3925
- }
3926
- }
3927
- return markDefPath && spanPath && (paths = {
3928
- markDefPath,
3929
- markDefPaths,
3930
- spanPath
3931
- }), paths;
3932
- }, removeAnnotationActionImplementation = ({
3933
- action
3934
- }) => {
3935
- const editor = action.editor;
3936
- if (debug$a("Removing annotation", action.annotation.name), !!editor.selection)
3937
- if (Range.isCollapsed(editor.selection)) {
3938
- const [block, blockPath] = Editor.node(editor, editor.selection, {
3939
- depth: 1
3940
- });
3941
- if (!editor.isTextBlock(block))
3942
- return;
3943
- const potentialAnnotations = (block.markDefs ?? []).filter((markDef) => markDef._type === action.annotation.name), [selectedChild, selectedChildPath] = Editor.node(editor, editor.selection, {
3944
- depth: 2
3945
- });
3946
- if (!editor.isTextSpan(selectedChild))
3947
- return;
3948
- const annotationToRemove = selectedChild.marks?.find((mark) => potentialAnnotations.some((markDef) => markDef._key === mark));
3949
- if (!annotationToRemove)
3950
- return;
3951
- const previousSpansWithSameAnnotation = [];
3952
- for (const [child, childPath] of Node.children(editor, blockPath, {
3953
- reverse: !0
3954
- }))
3955
- if (editor.isTextSpan(child) && Path.isBefore(childPath, selectedChildPath))
3956
- if (child.marks?.includes(annotationToRemove))
3957
- previousSpansWithSameAnnotation.push([child, childPath]);
3958
- else
3959
- break;
3960
- const nextSpansWithSameAnnotation = [];
3961
- for (const [child, childPath] of Node.children(editor, blockPath))
3962
- if (editor.isTextSpan(child) && Path.isAfter(childPath, selectedChildPath))
3963
- if (child.marks?.includes(annotationToRemove))
3964
- nextSpansWithSameAnnotation.push([child, childPath]);
3965
- else
3966
- break;
3967
- for (const [child, childPath] of [...previousSpansWithSameAnnotation, [selectedChild, selectedChildPath], ...nextSpansWithSameAnnotation])
3968
- Transforms.setNodes(editor, {
3969
- marks: child.marks?.filter((mark) => mark !== annotationToRemove)
3970
- }, {
3971
- at: childPath
3972
- });
3973
- } else {
3974
- Transforms.setNodes(editor, {}, {
3975
- match: (node) => editor.isTextSpan(node),
3976
- split: !0,
3977
- hanging: !0
3978
- });
3979
- const blocks = Editor.nodes(editor, {
3980
- at: editor.selection,
3981
- match: (node) => editor.isTextBlock(node)
3982
- });
3983
- for (const [block, blockPath] of blocks) {
3984
- const children = Node.children(editor, blockPath);
3985
- for (const [child, childPath] of children) {
3986
- if (!editor.isTextSpan(child) || !Range.includes(editor.selection, childPath))
3987
- continue;
3988
- const markDefs = block.markDefs ?? [], marks = child.marks ?? [], marksWithoutAnnotation = marks.filter((mark) => markDefs.find((markDef2) => markDef2._key === mark)?._type !== action.annotation.name);
3989
- marksWithoutAnnotation.length !== marks.length && Transforms.setNodes(editor, {
3990
- marks: marksWithoutAnnotation
3991
- }, {
3992
- at: childPath
3993
- });
3994
- }
3345
+ for (const [block, blockPath] of blocks) {
3346
+ const children = Node.children(editor, blockPath);
3347
+ for (const [child, childPath] of children) {
3348
+ if (!editor.isTextSpan(child) || !Range.includes(editor.selection, childPath))
3349
+ continue;
3350
+ const markDefs = block.markDefs ?? [], marks = child.marks ?? [], marksWithoutAnnotation = marks.filter((mark) => markDefs.find((markDef2) => markDef2._key === mark)?._type !== action.annotation.name);
3351
+ marksWithoutAnnotation.length !== marks.length && Transforms.setNodes(editor, {
3352
+ marks: marksWithoutAnnotation
3353
+ }, {
3354
+ at: childPath
3355
+ });
3356
+ }
3995
3357
  }
3996
3358
  }
3997
- }, blockSetBehaviorActionImplementation = ({
3359
+ };
3360
+ function toSlatePath(path, editor) {
3361
+ if (!editor)
3362
+ return [];
3363
+ const [block, blockPath] = Array.from(Editor.nodes(editor, {
3364
+ at: [],
3365
+ match: (n) => isKeySegment(path[0]) && n._key === path[0]._key
3366
+ }))[0] || [void 0, void 0];
3367
+ if (!block || !Element.isElement(block))
3368
+ return [];
3369
+ if (editor.isVoid(block))
3370
+ return [blockPath[0], 0];
3371
+ const childPath = [path[2]], childIndex = block.children.findIndex((child) => isEqual([{
3372
+ _key: child._key
3373
+ }], childPath));
3374
+ if (childIndex >= 0 && block.children[childIndex]) {
3375
+ const child = block.children[childIndex];
3376
+ return Element.isElement(child) && editor.isVoid(child) ? blockPath.concat(childIndex).concat(0) : blockPath.concat(childIndex);
3377
+ }
3378
+ return [blockPath[0], 0];
3379
+ }
3380
+ function toSlateRange(selection, editor) {
3381
+ if (!selection || !editor)
3382
+ return null;
3383
+ const anchor = {
3384
+ path: toSlatePath(selection.anchor.path, editor),
3385
+ offset: selection.anchor.offset
3386
+ }, focus = {
3387
+ path: toSlatePath(selection.focus.path, editor),
3388
+ offset: selection.focus.offset
3389
+ };
3390
+ return focus.path.length === 0 || anchor.path.length === 0 ? null : anchor && focus ? {
3391
+ anchor,
3392
+ focus
3393
+ } : null;
3394
+ }
3395
+ function moveRangeByOperation(range, operation) {
3396
+ const anchor = Point.transform(range.anchor, operation), focus = Point.transform(range.focus, operation);
3397
+ return anchor === null || focus === null ? null : Point.equals(anchor, range.anchor) && Point.equals(focus, range.focus) ? range : {
3398
+ anchor,
3399
+ focus
3400
+ };
3401
+ }
3402
+ const blockSetBehaviorActionImplementation = ({
3998
3403
  context,
3999
3404
  action
4000
3405
  }) => {
@@ -4108,7 +3513,187 @@ const addAnnotationActionImplementation = ({
4108
3513
  action
4109
3514
  }) => {
4110
3515
  ReactEditor.blur(action.editor);
4111
- }, decoratorAddActionImplementation = ({
3516
+ };
3517
+ function getFocusBlock({
3518
+ editor
3519
+ }) {
3520
+ if (!editor.selection)
3521
+ return [void 0, void 0];
3522
+ try {
3523
+ return Editor.node(editor, editor.selection.focus.path.slice(0, 1)) ?? [void 0, void 0];
3524
+ } catch {
3525
+ return [void 0, void 0];
3526
+ }
3527
+ }
3528
+ function getPointBlock({
3529
+ editor,
3530
+ point
3531
+ }) {
3532
+ try {
3533
+ const [block] = Editor.node(editor, point.path.slice(0, 1)) ?? [void 0, void 0];
3534
+ return block ? [block, point.path] : [void 0, void 0];
3535
+ } catch {
3536
+ return [void 0, void 0];
3537
+ }
3538
+ }
3539
+ function getFocusChild({
3540
+ editor
3541
+ }) {
3542
+ const [focusBlock, focusBlockPath] = getFocusBlock({
3543
+ editor
3544
+ }), childIndex = editor.selection?.focus.path.at(1);
3545
+ if (!focusBlock || !focusBlockPath || childIndex === void 0)
3546
+ return [void 0, void 0];
3547
+ try {
3548
+ const focusChild = Node.child(focusBlock, childIndex);
3549
+ return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
3550
+ } catch {
3551
+ return [void 0, void 0];
3552
+ }
3553
+ }
3554
+ function getPointChild({
3555
+ editor,
3556
+ point
3557
+ }) {
3558
+ const [block, blockPath] = getPointBlock({
3559
+ editor,
3560
+ point
3561
+ }), childIndex = point.path.at(1);
3562
+ if (!block || !blockPath || childIndex === void 0)
3563
+ return [void 0, void 0];
3564
+ try {
3565
+ const pointChild = Node.child(block, childIndex);
3566
+ return pointChild ? [pointChild, [...blockPath, childIndex]] : [void 0, void 0];
3567
+ } catch {
3568
+ return [void 0, void 0];
3569
+ }
3570
+ }
3571
+ function getFirstBlock({
3572
+ editor
3573
+ }) {
3574
+ const firstBlockPath = Editor.start(editor, []).path.at(0);
3575
+ try {
3576
+ return firstBlockPath !== void 0 ? Editor.node(editor, [firstBlockPath]) ?? [void 0, void 0] : [void 0, void 0];
3577
+ } catch {
3578
+ return [void 0, void 0];
3579
+ }
3580
+ }
3581
+ function getLastBlock({
3582
+ editor
3583
+ }) {
3584
+ const lastBlockPath = Editor.end(editor, []).path.at(0);
3585
+ try {
3586
+ return lastBlockPath !== void 0 ? Editor.node(editor, [lastBlockPath]) ?? [void 0, void 0] : [void 0, void 0];
3587
+ } catch {
3588
+ return [void 0, void 0];
3589
+ }
3590
+ }
3591
+ function getNodeBlock({
3592
+ editor,
3593
+ schema,
3594
+ node
3595
+ }) {
3596
+ if (Editor.isEditor(node))
3597
+ return;
3598
+ if (isBlockElement({
3599
+ editor,
3600
+ schema
3601
+ }, node))
3602
+ return elementToBlock({
3603
+ schema,
3604
+ element: node
3605
+ });
3606
+ const parent = Array.from(Editor.nodes(editor, {
3607
+ mode: "highest",
3608
+ at: [],
3609
+ match: (n) => isBlockElement({
3610
+ editor,
3611
+ schema
3612
+ }, n) && n.children.some((child) => child._key === node._key)
3613
+ })).at(0)?.at(0);
3614
+ return Element.isElement(parent) ? elementToBlock({
3615
+ schema,
3616
+ element: parent
3617
+ }) : void 0;
3618
+ }
3619
+ function elementToBlock({
3620
+ schema,
3621
+ element
3622
+ }) {
3623
+ return fromSlateValue([element], schema.block.name)?.at(0);
3624
+ }
3625
+ function isBlockElement({
3626
+ editor,
3627
+ schema
3628
+ }, node) {
3629
+ return Element.isElement(node) && !editor.isInline(node) && (schema.block.name === node._type || schema.blockObjects.some((blockObject) => blockObject.name === node._type));
3630
+ }
3631
+ function isListItemActive({
3632
+ editor,
3633
+ listItem
3634
+ }) {
3635
+ if (!editor.selection)
3636
+ return !1;
3637
+ const selectedBlocks = [...Editor.nodes(editor, {
3638
+ at: editor.selection,
3639
+ match: (node) => editor.isTextBlock(node)
3640
+ })];
3641
+ return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => editor.isListBlock(node) && node.listItem === listItem) : !1;
3642
+ }
3643
+ function isStyleActive({
3644
+ editor,
3645
+ style
3646
+ }) {
3647
+ if (!editor.selection)
3648
+ return !1;
3649
+ const selectedBlocks = [...Editor.nodes(editor, {
3650
+ at: editor.selection,
3651
+ match: (node) => editor.isTextBlock(node)
3652
+ })];
3653
+ return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => node.style === style) : !1;
3654
+ }
3655
+ function slateRangeToSelection({
3656
+ schema,
3657
+ editor,
3658
+ range
3659
+ }) {
3660
+ const [anchorBlock] = getPointBlock({
3661
+ editor,
3662
+ point: range.anchor
3663
+ }), [focusBlock] = getPointBlock({
3664
+ editor,
3665
+ point: range.focus
3666
+ });
3667
+ if (!anchorBlock || !focusBlock)
3668
+ return null;
3669
+ const [anchorChild] = anchorBlock._type === schema.block.name ? getPointChild({
3670
+ editor,
3671
+ point: range.anchor
3672
+ }) : [void 0, void 0], [focusChild] = focusBlock._type === schema.block.name ? getPointChild({
3673
+ editor,
3674
+ point: range.focus
3675
+ }) : [void 0, void 0], selection = {
3676
+ anchor: {
3677
+ path: [{
3678
+ _key: anchorBlock._key
3679
+ }],
3680
+ offset: range.anchor.offset
3681
+ },
3682
+ focus: {
3683
+ path: [{
3684
+ _key: focusBlock._key
3685
+ }],
3686
+ offset: range.focus.offset
3687
+ },
3688
+ backward: Range.isBackward(range)
3689
+ };
3690
+ return anchorChild && (selection.anchor.path.push("children"), selection.anchor.path.push({
3691
+ _key: anchorChild._key
3692
+ })), focusChild && (selection.focus.path.push("children"), selection.focus.path.push({
3693
+ _key: focusChild._key
3694
+ })), selection;
3695
+ }
3696
+ const decoratorAddActionImplementation = ({
4112
3697
  context,
4113
3698
  action
4114
3699
  }) => {
@@ -4523,7 +4108,7 @@ const insertTextActionImplementation = ({
4523
4108
  }) => {
4524
4109
  const newSelection = toSlateRange(action.selection, action.editor);
4525
4110
  newSelection ? Transforms.select(action.editor, newSelection) : Transforms.deselect(action.editor);
4526
- }, debug$9 = debugWithName("behaviors:action"), behaviorActionImplementations = {
4111
+ }, debug$a = debugWithName("behaviors:action"), behaviorActionImplementations = {
4527
4112
  "annotation.add": addAnnotationActionImplementation,
4528
4113
  "annotation.remove": removeAnnotationActionImplementation,
4529
4114
  "block.set": blockSetBehaviorActionImplementation,
@@ -4554,7 +4139,7 @@ function performAction({
4554
4139
  context,
4555
4140
  action
4556
4141
  }) {
4557
- switch (debug$9(JSON.stringify(omit(action, ["editor"]), null, 2)), action.type) {
4142
+ switch (debug$a(JSON.stringify(omit(action, ["editor"]), null, 2)), action.type) {
4558
4143
  case "annotation.add": {
4559
4144
  behaviorActionImplementations["annotation.add"]({
4560
4145
  context,
@@ -4989,13 +4574,13 @@ function createWithObjectKeys(editorActor, schemaTypes) {
4989
4574
  }, editor;
4990
4575
  };
4991
4576
  }
4992
- const debug$8 = debugWithName("applyPatches"), debugVerbose = debug$8.enabled && !0;
4577
+ const debug$9 = debugWithName("applyPatches"), debugVerbose = debug$9.enabled && !0;
4993
4578
  function createApplyPatch(schemaTypes) {
4994
4579
  return (editor, patch) => {
4995
4580
  let changed = !1;
4996
- debugVerbose && (debug$8(`
4581
+ debugVerbose && (debug$9(`
4997
4582
 
4998
- NEW PATCH =============================================================`), debug$8(JSON.stringify(patch, null, 2)));
4583
+ NEW PATCH =============================================================`), debug$9(JSON.stringify(patch, null, 2)));
4999
4584
  try {
5000
4585
  switch (patch.type) {
5001
4586
  case "insert":
@@ -5011,7 +4596,7 @@ NEW PATCH =============================================================`), debug
5011
4596
  changed = diffMatchPatch(editor, patch);
5012
4597
  break;
5013
4598
  default:
5014
- debug$8("Unhandled patch", patch.type);
4599
+ debug$9("Unhandled patch", patch.type);
5015
4600
  }
5016
4601
  } catch (err) {
5017
4602
  console.error(err);
@@ -5026,9 +4611,9 @@ function diffMatchPatch(editor, patch) {
5026
4611
  childPath
5027
4612
  } = findBlockAndChildFromPath(editor, patch.path);
5028
4613
  if (!block)
5029
- return debug$8("Block not found"), !1;
4614
+ return debug$9("Block not found"), !1;
5030
4615
  if (!child || !childPath)
5031
- return debug$8("Child not found"), !1;
4616
+ return debug$9("Child not found"), !1;
5032
4617
  if (!(block && editor.isTextBlock(block) && patch.path.length === 4 && patch.path[1] === "children" && patch.path[3] === "text") || !Text.isText(child))
5033
4618
  return !1;
5034
4619
  const patches = parse(patch.value), [newValue] = apply(patches, child.text, {
@@ -5058,9 +4643,9 @@ function insertPatch(editor, patch, schemaTypes) {
5058
4643
  childPath: targetChildPath
5059
4644
  } = findBlockAndChildFromPath(editor, patch.path);
5060
4645
  if (!targetBlock || !targetBlockPath)
5061
- return debug$8("Block not found"), !1;
4646
+ return debug$9("Block not found"), !1;
5062
4647
  if (patch.path.length > 1 && patch.path[1] !== "children")
5063
- return debug$8("Ignoring patch targeting void value"), !1;
4648
+ return debug$9("Ignoring patch targeting void value"), !1;
5064
4649
  if (patch.path.length === 1) {
5065
4650
  const {
5066
4651
  items: items2,
@@ -5068,7 +4653,7 @@ function insertPatch(editor, patch, schemaTypes) {
5068
4653
  } = patch, blocksToInsert = toSlateValue(items2, {
5069
4654
  schemaTypes
5070
4655
  }, KEY_TO_SLATE_ELEMENT.get(editor)), targetBlockIndex = targetBlockPath[0], normalizedIdx2 = position2 === "after" ? targetBlockIndex + 1 : targetBlockIndex;
5071
- return debug$8(`Inserting blocks at path [${normalizedIdx2}]`), debugState(editor, "before"), Transforms.insertNodes(editor, blocksToInsert, {
4656
+ return debug$9(`Inserting blocks at path [${normalizedIdx2}]`), debugState(editor, "before"), Transforms.insertNodes(editor, blocksToInsert, {
5072
4657
  at: [normalizedIdx2]
5073
4658
  }), debugState(editor, "after"), !0;
5074
4659
  }
@@ -5077,14 +4662,14 @@ function insertPatch(editor, patch, schemaTypes) {
5077
4662
  position
5078
4663
  } = patch;
5079
4664
  if (!targetChild || !targetChildPath)
5080
- return debug$8("Child not found"), !1;
4665
+ return debug$9("Child not found"), !1;
5081
4666
  const childrenToInsert = targetBlock && toSlateValue([{
5082
4667
  ...targetBlock,
5083
4668
  children: items
5084
4669
  }], {
5085
4670
  schemaTypes
5086
4671
  }, KEY_TO_SLATE_ELEMENT.get(editor)), targetChildIndex = targetChildPath[1], normalizedIdx = position === "after" ? targetChildIndex + 1 : targetChildIndex, childInsertPath = [targetChildPath[0], normalizedIdx];
5087
- return debug$8(`Inserting children at path ${childInsertPath}`), debugState(editor, "before"), childrenToInsert && Element.isElement(childrenToInsert[0]) && Transforms.insertNodes(editor, childrenToInsert[0].children, {
4672
+ return debug$9(`Inserting children at path ${childInsertPath}`), debugState(editor, "before"), childrenToInsert && Element.isElement(childrenToInsert[0]) && Transforms.insertNodes(editor, childrenToInsert[0].children, {
5088
4673
  at: childInsertPath
5089
4674
  }), debugState(editor, "after"), !0;
5090
4675
  }
@@ -5098,14 +4683,14 @@ function setPatch(editor, patch) {
5098
4683
  childPath
5099
4684
  } = findBlockAndChildFromPath(editor, patch.path);
5100
4685
  if (!block)
5101
- return debug$8("Block not found"), !1;
4686
+ return debug$9("Block not found"), !1;
5102
4687
  const isTextBlock2 = editor.isTextBlock(block);
5103
4688
  if (isTextBlock2 && patch.path.length > 1 && patch.path[1] !== "children")
5104
- return debug$8("Ignoring setting void value"), !1;
4689
+ return debug$9("Ignoring setting void value"), !1;
5105
4690
  if (debugState(editor, "before"), isTextBlock2 && child && childPath) {
5106
4691
  if (Text.isText(value) && Text.isText(child)) {
5107
4692
  const newText = child.text;
5108
- value.text !== newText && (debug$8("Setting text property"), editor.apply({
4693
+ value.text !== newText && (debug$9("Setting text property"), editor.apply({
5109
4694
  type: "remove_text",
5110
4695
  path: childPath,
5111
4696
  offset: 0,
@@ -5117,7 +4702,7 @@ function setPatch(editor, patch) {
5117
4702
  text: value.text
5118
4703
  }), editor.onChange());
5119
4704
  } else
5120
- debug$8("Setting non-text property"), editor.apply({
4705
+ debug$9("Setting non-text property"), editor.apply({
5121
4706
  type: "set_node",
5122
4707
  path: childPath,
5123
4708
  properties: {},
@@ -5125,7 +4710,7 @@ function setPatch(editor, patch) {
5125
4710
  });
5126
4711
  return !0;
5127
4712
  } else if (Element.isElement(block) && patch.path.length === 1 && blockPath) {
5128
- debug$8("Setting block property");
4713
+ debug$9("Setting block property");
5129
4714
  const {
5130
4715
  children,
5131
4716
  ...nextRest
@@ -5142,7 +4727,7 @@ function setPatch(editor, patch) {
5142
4727
  ...prevRest
5143
4728
  },
5144
4729
  newProperties: nextRest
5145
- }), debug$8("Setting children"), block.children.forEach((c2, cIndex) => {
4730
+ }), debug$9("Setting children"), block.children.forEach((c2, cIndex) => {
5146
4731
  editor.apply({
5147
4732
  type: "remove_node",
5148
4733
  path: blockPath.concat(block.children.length - 1 - cIndex),
@@ -5173,7 +4758,7 @@ function setPatch(editor, patch) {
5173
4758
  }
5174
4759
  function unsetPatch(editor, patch) {
5175
4760
  if (patch.path.length === 0) {
5176
- debug$8("Removing everything"), debugState(editor, "before");
4761
+ debug$9("Removing everything"), debugState(editor, "before");
5177
4762
  const previousSelection = editor.selection;
5178
4763
  return Transforms.deselect(editor), editor.children.forEach((_child, i) => {
5179
4764
  Transforms.removeNodes(editor, {
@@ -5200,13 +4785,13 @@ function unsetPatch(editor, patch) {
5200
4785
  } = findBlockAndChildFromPath(editor, patch.path);
5201
4786
  if (patch.path.length === 1) {
5202
4787
  if (!block || !blockPath)
5203
- return debug$8("Block not found"), !1;
4788
+ return debug$9("Block not found"), !1;
5204
4789
  const blockIndex = blockPath[0];
5205
- return debug$8(`Removing block at path [${blockIndex}]`), debugState(editor, "before"), Transforms.removeNodes(editor, {
4790
+ return debug$9(`Removing block at path [${blockIndex}]`), debugState(editor, "before"), Transforms.removeNodes(editor, {
5206
4791
  at: [blockIndex]
5207
4792
  }), debugState(editor, "after"), !0;
5208
4793
  }
5209
- return editor.isTextBlock(block) && patch.path[1] === "children" && patch.path.length === 3 ? !child || !childPath ? (debug$8("Child not found"), !1) : (debug$8(`Unsetting child at path ${JSON.stringify(childPath)}`), debugState(editor, "before"), debugVerbose && debug$8(`Removing child at path ${JSON.stringify(childPath)}`), Transforms.removeNodes(editor, {
4794
+ return editor.isTextBlock(block) && patch.path[1] === "children" && patch.path.length === 3 ? !child || !childPath ? (debug$9("Child not found"), !1) : (debug$9(`Unsetting child at path ${JSON.stringify(childPath)}`), debugState(editor, "before"), debugVerbose && debug$9(`Removing child at path ${JSON.stringify(childPath)}`), Transforms.removeNodes(editor, {
5210
4795
  at: childPath
5211
4796
  }), debugState(editor, "after"), !0) : !1;
5212
4797
  }
@@ -5214,7 +4799,7 @@ function isKeyedSegment(segment) {
5214
4799
  return typeof segment == "object" && "_key" in segment;
5215
4800
  }
5216
4801
  function debugState(editor, stateName) {
5217
- debugVerbose && (debug$8(`Children ${stateName}:`, JSON.stringify(editor.children, null, 2)), debug$8(`Selection ${stateName}: `, JSON.stringify(editor.selection, null, 2)));
4802
+ debugVerbose && (debug$9(`Children ${stateName}:`, JSON.stringify(editor.children, null, 2)), debug$9(`Selection ${stateName}: `, JSON.stringify(editor.selection, null, 2)));
5218
4803
  }
5219
4804
  function findBlockFromPath(editor, path) {
5220
4805
  let blockIndex = -1;
@@ -5256,7 +4841,7 @@ function findBlockAndChildFromPath(editor, path) {
5256
4841
  childPath: void 0
5257
4842
  };
5258
4843
  }
5259
- const debug$7 = debugWithName("plugin:withPatches");
4844
+ const debug$8 = debugWithName("plugin:withPatches");
5260
4845
  function createWithPatches({
5261
4846
  editorActor,
5262
4847
  patchFunctions,
@@ -5282,7 +4867,7 @@ function createWithPatches({
5282
4867
  withoutPatching(editor, () => {
5283
4868
  withoutSaving(editor, () => {
5284
4869
  patches.forEach((patch) => {
5285
- debug$7.enabled && debug$7(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
4870
+ debug$8.enabled && debug$8(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
5286
4871
  });
5287
4872
  });
5288
4873
  });
@@ -5295,10 +4880,10 @@ function createWithPatches({
5295
4880
  remotePatches.length !== 0 && (bufferedPatches = bufferedPatches.concat(remotePatches), handleBufferedRemotePatches());
5296
4881
  };
5297
4882
  return subscriptions.push(() => {
5298
- debug$7("Subscribing to remote patches");
4883
+ debug$8("Subscribing to remote patches");
5299
4884
  const sub = editorActor.on("patches", handlePatches);
5300
4885
  return () => {
5301
- debug$7("Unsubscribing to remote patches"), sub.unsubscribe();
4886
+ debug$8("Unsubscribing to remote patches"), sub.unsubscribe();
5302
4887
  };
5303
4888
  }), editor.apply = (operation) => {
5304
4889
  let patches = [];
@@ -5352,7 +4937,7 @@ function createWithPatches({
5352
4937
  }, editor;
5353
4938
  };
5354
4939
  }
5355
- const debug$6 = debugWithName("plugin:withPlaceholderBlock");
4940
+ const debug$7 = debugWithName("plugin:withPlaceholderBlock");
5356
4941
  function createWithPlaceholderBlock(editorActor) {
5357
4942
  return function(editor) {
5358
4943
  const {
@@ -5377,7 +4962,7 @@ function createWithPlaceholderBlock(editorActor) {
5377
4962
  const node = op.node;
5378
4963
  if (op.path[0] === 0 && Editor.isVoid(editor, node)) {
5379
4964
  const nextPath = Path.next(op.path);
5380
- editor.children[nextPath[0]] || (debug$6("Adding placeholder block"), Editor.insertNode(editor, editor.pteCreateTextBlock({
4965
+ editor.children[nextPath[0]] || (debug$7("Adding placeholder block"), Editor.insertNode(editor, editor.pteCreateTextBlock({
5381
4966
  decorators: []
5382
4967
  })));
5383
4968
  }
@@ -5386,7 +4971,7 @@ function createWithPlaceholderBlock(editorActor) {
5386
4971
  }, editor;
5387
4972
  };
5388
4973
  }
5389
- const debug$5 = debugWithName("plugin:withPortableTextBlockStyle");
4974
+ const debug$6 = debugWithName("plugin:withPortableTextBlockStyle");
5390
4975
  function createWithPortableTextBlockStyle(editorActor, types) {
5391
4976
  const defaultStyle = types.styles[0].value;
5392
4977
  return function(editor) {
@@ -5399,7 +4984,7 @@ function createWithPortableTextBlockStyle(editorActor, types) {
5399
4984
  if (op.type === "split_node" && op.path.length === 1 && editor.isTextBlock(op.properties) && op.properties.style !== defaultStyle && op.path[0] === path[0] && !Path.equals(path, op.path)) {
5400
4985
  const [child] = Editor.node(editor, [op.path[0] + 1, 0]);
5401
4986
  if (Text.isText(child) && child.text === "") {
5402
- debug$5(`Normalizing split node to ${defaultStyle} style`, op), editorActor.send({
4987
+ debug$6(`Normalizing split node to ${defaultStyle} style`, op), editorActor.send({
5403
4988
  type: "normalizing"
5404
4989
  }), Transforms.setNodes(editor, {
5405
4990
  style: defaultStyle
@@ -5450,7 +5035,7 @@ function createWithPortableTextSelections(editorActor) {
5450
5035
  }, editor;
5451
5036
  };
5452
5037
  }
5453
- const debug$4 = debugWithName("plugin:withSchemaTypes");
5038
+ const debug$5 = debugWithName("plugin:withSchemaTypes");
5454
5039
  function createWithSchemaTypes({
5455
5040
  editorActor,
5456
5041
  schemaTypes
@@ -5463,7 +5048,7 @@ function createWithSchemaTypes({
5463
5048
  return editor.normalizeNode = (entry) => {
5464
5049
  const [node, path] = entry;
5465
5050
  if (node._type === void 0 && path.length === 2) {
5466
- debug$4("Setting span type on text node without a type");
5051
+ debug$5("Setting span type on text node without a type");
5467
5052
  const span = node, key = span._key || editorActor.getSnapshot().context.keyGenerator();
5468
5053
  editorActor.send({
5469
5054
  type: "normalizing"
@@ -5479,7 +5064,7 @@ function createWithSchemaTypes({
5479
5064
  return;
5480
5065
  }
5481
5066
  if (node._key === void 0 && (path.length === 1 || path.length === 2)) {
5482
- debug$4("Setting missing key on child node without a key");
5067
+ debug$5("Setting missing key on child node without a key");
5483
5068
  const key = editorActor.getSnapshot().context.keyGenerator();
5484
5069
  editorActor.send({
5485
5070
  type: "normalizing"
@@ -5545,12 +5130,12 @@ const withPlugins = (editor, options) => {
5545
5130
  schemaTypes
5546
5131
  }), withPortableTextSelections = createWithPortableTextSelections(editorActor);
5547
5132
  return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e)))))))))));
5548
- }, debug$3 = debugWithName("component:PortableTextEditor:SlateContainer"), slateEditors = /* @__PURE__ */ new WeakMap();
5133
+ }, debug$4 = debugWithName("component:PortableTextEditor:SlateContainer"), slateEditors = /* @__PURE__ */ new WeakMap();
5549
5134
  function createSlateEditor(config) {
5550
5135
  const existingSlateEditor = slateEditors.get(config.editorActor);
5551
5136
  if (existingSlateEditor)
5552
- return debug$3("Reusing existing Slate editor instance", config.editorActor.id), existingSlateEditor;
5553
- debug$3("Creating new Slate editor instance", config.editorActor.id);
5137
+ return debug$4("Reusing existing Slate editor instance", config.editorActor.id), existingSlateEditor;
5138
+ debug$4("Creating new Slate editor instance", config.editorActor.id);
5554
5139
  const unsubscriptions = [], subscriptions = [], instance = withPlugins(withReact(createEditor()), {
5555
5140
  editorActor: config.editorActor,
5556
5141
  subscriptions
@@ -5564,7 +5149,91 @@ function createSlateEditor(config) {
5564
5149
  instance,
5565
5150
  initialValue
5566
5151
  };
5567
- return slateEditors.set(config.editorActor, slateEditor), slateEditor;
5152
+ return slateEditors.set(config.editorActor, slateEditor), slateEditor;
5153
+ }
5154
+ function defineSchema(definition) {
5155
+ return definition;
5156
+ }
5157
+ const temporaryImageName = `tmp-${defaultKeyGenerator()}-image`, temporaryUrlName = `tmp-${defaultKeyGenerator()}-url`, temporaryObjectNames = {
5158
+ image: temporaryImageName,
5159
+ url: temporaryUrlName
5160
+ }, objectNames = {
5161
+ [temporaryImageName]: "image",
5162
+ [temporaryUrlName]: "url"
5163
+ }, defaultObjectTitles = {
5164
+ image: "Image",
5165
+ url: "URL"
5166
+ };
5167
+ function compileSchemaDefinition(definition) {
5168
+ const blockObjects = definition?.blockObjects?.map((blockObject) => defineType({
5169
+ type: "object",
5170
+ // Very naive way to work around `SanitySchema.compile` adding default
5171
+ // fields to objects with certain names.
5172
+ name: temporaryObjectNames[blockObject.name] ?? blockObject.name,
5173
+ title: blockObject.title === void 0 ? (
5174
+ // This avoids the default title which is a title case of the object name
5175
+ defaultObjectTitles[blockObject.name]
5176
+ ) : blockObject.title,
5177
+ fields: []
5178
+ })) ?? [], inlineObjects = definition?.inlineObjects?.map((inlineObject) => defineType({
5179
+ type: "object",
5180
+ // Very naive way to work around `SanitySchema.compile` adding default
5181
+ // fields to objects with certain names.
5182
+ name: temporaryObjectNames[inlineObject.name] ?? inlineObject.name,
5183
+ title: inlineObject.title === void 0 ? (
5184
+ // This avoids the default title which is a title case of the object name
5185
+ defaultObjectTitles[inlineObject.name]
5186
+ ) : inlineObject.title,
5187
+ fields: []
5188
+ })) ?? [], portableTextSchema = defineField({
5189
+ type: "array",
5190
+ name: "portable-text",
5191
+ of: [...blockObjects.map((blockObject) => ({
5192
+ type: blockObject.name
5193
+ })), {
5194
+ type: "block",
5195
+ name: "block",
5196
+ of: inlineObjects.map((inlineObject) => ({
5197
+ type: inlineObject.name
5198
+ })),
5199
+ marks: {
5200
+ decorators: definition?.decorators?.map((decorator) => ({
5201
+ title: decorator.title ?? startCase(decorator.name),
5202
+ value: decorator.name
5203
+ })) ?? [],
5204
+ annotations: definition?.annotations?.map((annotation) => ({
5205
+ name: annotation.name,
5206
+ type: "object",
5207
+ title: annotation.title
5208
+ })) ?? []
5209
+ },
5210
+ lists: definition?.lists?.map((list) => ({
5211
+ value: list.name,
5212
+ title: list.title ?? startCase(list.name)
5213
+ })) ?? [],
5214
+ styles: definition?.styles?.map((style) => ({
5215
+ value: style.name,
5216
+ title: style.title ?? startCase(style.name)
5217
+ })) ?? []
5218
+ }]
5219
+ }), schema = Schema.compile({
5220
+ types: [portableTextSchema, ...blockObjects, ...inlineObjects]
5221
+ }).get("portable-text"), pteSchema = createEditorSchema(schema);
5222
+ return {
5223
+ ...pteSchema,
5224
+ blockObjects: pteSchema.blockObjects.map((blockObject) => objectNames[blockObject.name] !== void 0 ? {
5225
+ ...blockObject,
5226
+ name: objectNames[blockObject.name],
5227
+ type: {
5228
+ ...blockObject.type,
5229
+ name: objectNames[blockObject.name]
5230
+ }
5231
+ } : blockObject),
5232
+ inlineObjects: pteSchema.inlineObjects.map((inlineObject) => objectNames[inlineObject.name] !== void 0 ? {
5233
+ ...inlineObject,
5234
+ name: objectNames[inlineObject.name]
5235
+ } : inlineObject)
5236
+ };
5568
5237
  }
5569
5238
  const abstractAnnotationBehaviors = [defineBehavior({
5570
5239
  on: "annotation.toggle",
@@ -6013,22 +5682,228 @@ const abstractAnnotationBehaviors = [defineBehavior({
6013
5682
  })]]
6014
5683
  }),
6015
5684
  defineBehavior({
6016
- on: "clipboard.cut",
6017
- guard: ({
6018
- snapshot
6019
- }) => {
6020
- const focusSpan = getFocusSpan(snapshot), selectionCollapsed = isSelectionCollapsed(snapshot);
6021
- return focusSpan && selectionCollapsed;
6022
- },
6023
- actions: [() => [{
6024
- type: "noop"
5685
+ on: "clipboard.cut",
5686
+ guard: ({
5687
+ snapshot
5688
+ }) => {
5689
+ const focusSpan = getFocusSpan(snapshot), selectionCollapsed = isSelectionCollapsed(snapshot);
5690
+ return focusSpan && selectionCollapsed;
5691
+ },
5692
+ actions: [() => [{
5693
+ type: "noop"
5694
+ }]]
5695
+ }),
5696
+ defineBehavior({
5697
+ on: "clipboard.cut",
5698
+ guard: ({
5699
+ snapshot
5700
+ }) => snapshot.context.selection ? {
5701
+ selection: snapshot.context.selection
5702
+ } : !1,
5703
+ actions: [({
5704
+ event
5705
+ }, {
5706
+ selection
5707
+ }) => [raise({
5708
+ type: "serialize",
5709
+ originEvent: event
5710
+ }), raise({
5711
+ type: "delete",
5712
+ selection
5713
+ })]]
5714
+ }),
5715
+ defineBehavior({
5716
+ on: "drag.dragstart",
5717
+ actions: [({
5718
+ event
5719
+ }) => [raise({
5720
+ type: "serialize",
5721
+ originEvent: event
5722
+ })]]
5723
+ }),
5724
+ defineBehavior({
5725
+ on: "serialization.success",
5726
+ actions: [({
5727
+ event
5728
+ }) => [{
5729
+ type: "effect",
5730
+ effect: () => {
5731
+ event.originEvent.originEvent.dataTransfer.setData(event.mimeType, event.data);
5732
+ }
5733
+ }]]
5734
+ }),
5735
+ defineBehavior({
5736
+ on: "serialization.failure",
5737
+ actions: [({
5738
+ event
5739
+ }) => [{
5740
+ type: "effect",
5741
+ effect: () => {
5742
+ console.warn(`Serialization of ${event.mimeType} failed with reason "${event.reason}"`);
5743
+ }
5744
+ }]]
5745
+ }),
5746
+ defineBehavior({
5747
+ on: "drag.drop",
5748
+ guard: ({
5749
+ snapshot,
5750
+ event
5751
+ }) => {
5752
+ const dragOrigin = snapshot.beta.internalDrag?.origin, dropPosition = event.position.selection;
5753
+ return dragOrigin ? isOverlappingSelection(dropPosition)({
5754
+ ...snapshot,
5755
+ context: {
5756
+ ...snapshot.context,
5757
+ selection: dragOrigin.selection
5758
+ }
5759
+ }) : !1;
5760
+ },
5761
+ actions: [() => [{
5762
+ type: "noop"
5763
+ }]]
5764
+ }),
5765
+ defineBehavior({
5766
+ on: "drag.drop",
5767
+ actions: [({
5768
+ event
5769
+ }) => [raise({
5770
+ type: "select",
5771
+ selection: event.position.selection
5772
+ }), raise({
5773
+ type: "deserialize",
5774
+ originEvent: event
5775
+ })]]
5776
+ }),
5777
+ defineBehavior({
5778
+ on: "deserialization.success",
5779
+ guard: ({
5780
+ snapshot,
5781
+ event
5782
+ }) => {
5783
+ if (event.originEvent.type !== "drag.drop" || snapshot.beta.internalDrag === void 0)
5784
+ return !1;
5785
+ const dragOrigin = snapshot.beta.internalDrag.origin, dropPosition = event.originEvent.position.selection, droppingOnDragOrigin = dragOrigin ? isOverlappingSelection(dropPosition)({
5786
+ ...snapshot,
5787
+ context: {
5788
+ ...snapshot.context,
5789
+ selection: dragOrigin.selection
5790
+ }
5791
+ }) : !1, draggingEntireBlocks = isSelectingEntireBlocks({
5792
+ context: {
5793
+ ...snapshot.context,
5794
+ selection: dragOrigin.selection
5795
+ }
5796
+ }), draggedBlocks = getSelectedBlocks({
5797
+ context: {
5798
+ ...snapshot.context,
5799
+ selection: dragOrigin.selection
5800
+ }
5801
+ });
5802
+ return droppingOnDragOrigin ? !1 : {
5803
+ draggingEntireBlocks,
5804
+ draggedBlocks,
5805
+ dragOrigin,
5806
+ originEvent: event.originEvent
5807
+ };
5808
+ },
5809
+ actions: [({
5810
+ event
5811
+ }, {
5812
+ draggingEntireBlocks,
5813
+ draggedBlocks,
5814
+ dragOrigin,
5815
+ originEvent
5816
+ }) => [...draggingEntireBlocks ? draggedBlocks.map((block) => raise({
5817
+ type: "delete.block",
5818
+ at: block.path
5819
+ })) : [raise({
5820
+ type: "delete",
5821
+ selection: dragOrigin.selection
5822
+ })], raise({
5823
+ type: "insert.blocks",
5824
+ blocks: event.data,
5825
+ placement: draggingEntireBlocks ? originEvent.position.block === "start" ? "before" : originEvent.position.block === "end" ? "after" : "auto" : "auto"
5826
+ })]]
5827
+ }),
5828
+ /**
5829
+ * If we are pasting text/plain into a text block then we can probably
5830
+ * assume that the intended behavior is that the pasted text inherits
5831
+ * formatting from the text it's pasted into.
5832
+ */
5833
+ defineBehavior({
5834
+ on: "deserialization.success",
5835
+ guard: ({
5836
+ snapshot,
5837
+ event
5838
+ }) => {
5839
+ if (getFocusTextBlock(snapshot) && event.mimeType === "text/plain" && event.originEvent.type === "clipboard.paste") {
5840
+ const activeDecorators = snapshot.context.activeDecorators;
5841
+ return {
5842
+ activeAnnotations: getActiveAnnotations(snapshot),
5843
+ activeDecorators,
5844
+ textRuns: event.data.flatMap((block) => isTextBlock(snapshot.context.schema, block) ? [getTextBlockText(block)] : [])
5845
+ };
5846
+ }
5847
+ return !1;
5848
+ },
5849
+ actions: [(_, {
5850
+ activeAnnotations,
5851
+ activeDecorators,
5852
+ textRuns
5853
+ }) => textRuns.flatMap((textRun, index) => index !== textRuns.length - 1 ? [raise({
5854
+ type: "insert.span",
5855
+ text: textRun,
5856
+ decorators: activeDecorators,
5857
+ annotations: activeAnnotations.map(({
5858
+ _key,
5859
+ _type,
5860
+ ...value
5861
+ }) => ({
5862
+ name: _type,
5863
+ value
5864
+ }))
5865
+ }), raise({
5866
+ type: "insert.break"
5867
+ })] : [raise({
5868
+ type: "insert.span",
5869
+ text: textRun,
5870
+ decorators: activeDecorators,
5871
+ annotations: activeAnnotations.map(({
5872
+ _key,
5873
+ _type,
5874
+ ...value
5875
+ }) => ({
5876
+ name: _type,
5877
+ value
5878
+ }))
5879
+ })])]
5880
+ }),
5881
+ defineBehavior({
5882
+ on: "deserialization.success",
5883
+ actions: [({
5884
+ event
5885
+ }) => [raise({
5886
+ type: "insert.blocks",
5887
+ blocks: event.data,
5888
+ placement: "auto"
5889
+ })]]
5890
+ }),
5891
+ defineBehavior({
5892
+ on: "deserialization.failure",
5893
+ actions: [({
5894
+ event
5895
+ }) => [{
5896
+ type: "effect",
5897
+ effect: () => {
5898
+ console.warn(`Deserialization of ${event.mimeType} failed with reason "${event.reason}"`);
5899
+ }
6025
5900
  }]]
6026
5901
  }),
6027
5902
  defineBehavior({
6028
- on: "clipboard.cut",
5903
+ on: "clipboard.paste",
6029
5904
  guard: ({
6030
5905
  snapshot
6031
- }) => snapshot.context.selection ? {
5906
+ }) => snapshot.context.selection && isSelectionExpanded(snapshot) ? {
6032
5907
  selection: snapshot.context.selection
6033
5908
  } : !1,
6034
5909
  actions: [({
@@ -6036,887 +5911,1049 @@ const abstractAnnotationBehaviors = [defineBehavior({
6036
5911
  }, {
6037
5912
  selection
6038
5913
  }) => [raise({
6039
- type: "serialize",
6040
- originEvent: event
6041
- }), raise({
6042
5914
  type: "delete",
6043
5915
  selection
5916
+ }), raise({
5917
+ type: "deserialize",
5918
+ originEvent: event
6044
5919
  })]]
6045
5920
  }),
6046
5921
  defineBehavior({
6047
- on: "drag.dragstart",
5922
+ on: "clipboard.paste",
6048
5923
  actions: [({
6049
5924
  event
6050
5925
  }) => [raise({
6051
- type: "serialize",
5926
+ type: "deserialize",
6052
5927
  originEvent: event
6053
5928
  })]]
6054
5929
  }),
6055
5930
  defineBehavior({
6056
- on: "serialization.success",
5931
+ on: "input.*",
6057
5932
  actions: [({
6058
5933
  event
6059
- }) => [{
6060
- type: "effect",
6061
- effect: () => {
6062
- event.originEvent.originEvent.dataTransfer.setData(event.mimeType, event.data);
6063
- }
6064
- }]]
5934
+ }) => [raise({
5935
+ type: "deserialize",
5936
+ originEvent: event
5937
+ })]]
6065
5938
  }),
6066
- defineBehavior({
6067
- on: "serialization.failure",
6068
- actions: [({
6069
- event
6070
- }) => [{
6071
- type: "effect",
6072
- effect: () => {
6073
- console.warn(`Serialization of ${event.mimeType} failed with reason "${event.reason}"`);
5939
+ ...abstractAnnotationBehaviors,
5940
+ ...abstractDecoratorBehaviors,
5941
+ ...abstractInsertBehaviors,
5942
+ ...abstractListItemBehaviors,
5943
+ ...abstractMoveBehaviors,
5944
+ ...abstractStyleBehaviors,
5945
+ ...abstractSelectBehaviors,
5946
+ raiseDeserializationSuccessOrFailure,
5947
+ raiseSerializationSuccessOrFailure,
5948
+ raiseInsertSoftBreak
5949
+ ], abstractBehaviorEventTypes = ["annotation.toggle", "decorator.toggle", "deserialize", "deserialization.success", "deserialization.failure", "insert.blocks", "list item.add", "list item.remove", "list item.toggle", "move.block down", "move.block up", "select.previous block", "select.next block", "serialize", "serialization.success", "serialization.failure", "style.add", "style.remove", "style.toggle"];
5950
+ function isAbstractBehaviorEvent(event) {
5951
+ return abstractBehaviorEventTypes.includes(event.type);
5952
+ }
5953
+ const nativeBehaviorEventTypes = ["clipboard.copy", "clipboard.cut", "clipboard.paste", "drag.dragstart", "drag.drag", "drag.dragend", "drag.dragenter", "drag.dragover", "drag.dragleave", "drag.drop", "input.*", "keyboard.keydown", "keyboard.keyup", "mouse.click"];
5954
+ function isNativeBehaviorEvent(event) {
5955
+ return nativeBehaviorEventTypes.includes(event.type);
5956
+ }
5957
+ function isCustomBehaviorEvent(event) {
5958
+ return event.type.startsWith("custom.");
5959
+ }
5960
+ const debug$3 = debugWithName("behaviors:event");
5961
+ function eventCategory(event) {
5962
+ return isNativeBehaviorEvent(event) ? "native" : isAbstractBehaviorEvent(event) ? "abstract" : isCustomBehaviorEvent(event) ? "custom" : "synthetic";
5963
+ }
5964
+ function performEvent({
5965
+ behaviors,
5966
+ event,
5967
+ editor,
5968
+ keyGenerator,
5969
+ schema,
5970
+ getSnapshot,
5971
+ nativeEvent,
5972
+ defaultActionCallback
5973
+ }) {
5974
+ debug$3(`(${eventCategory(event)})`, JSON.stringify(event, null, 2));
5975
+ const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isAbstractBehaviorEvent(event) ? void 0 : {
5976
+ ...event,
5977
+ editor
5978
+ }, eventBehaviors = behaviors.filter((behavior) => {
5979
+ if (behavior.on === "*")
5980
+ return !0;
5981
+ const [listenedNamespace] = behavior.on.includes("*") && behavior.on.includes(".") ? behavior.on.split(".") : [void 0], [eventNamespace] = event.type.includes(".") ? event.type.split(".") : [void 0];
5982
+ return listenedNamespace !== void 0 && eventNamespace !== void 0 && listenedNamespace === eventNamespace || listenedNamespace !== void 0 && eventNamespace === void 0 && listenedNamespace === event.type ? !0 : behavior.on === event.type;
5983
+ });
5984
+ if (eventBehaviors.length === 0) {
5985
+ if (defaultActionCallback) {
5986
+ withApplyingBehaviorActions(editor, () => {
5987
+ try {
5988
+ defaultActionCallback();
5989
+ } catch (error) {
5990
+ console.error(new Error(`Performing action "${event.type}" failed due to: ${error.message}`));
5991
+ }
5992
+ });
5993
+ return;
5994
+ }
5995
+ if (!defaultAction)
5996
+ return;
5997
+ withApplyingBehaviorActions(editor, () => {
5998
+ try {
5999
+ performAction({
6000
+ context: {
6001
+ keyGenerator,
6002
+ schema
6003
+ },
6004
+ action: defaultAction
6005
+ });
6006
+ } catch (error) {
6007
+ console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
6074
6008
  }
6075
- }]]
6076
- }),
6077
- defineBehavior({
6078
- on: "drag.drop",
6079
- guard: ({
6080
- snapshot,
6009
+ }), editor.onChange();
6010
+ return;
6011
+ }
6012
+ const editorSnapshot = getSnapshot();
6013
+ let behaviorOverwritten = !1;
6014
+ for (const eventBehavior of eventBehaviors) {
6015
+ const shouldRun = eventBehavior.guard === void 0 || eventBehavior.guard({
6016
+ context: editorSnapshot.context,
6017
+ snapshot: editorSnapshot,
6081
6018
  event
6082
- }) => {
6083
- const dragOrigin = snapshot.beta.internalDrag?.origin, dropPosition = event.position.selection;
6084
- return dragOrigin ? isOverlappingSelection(dropPosition)({
6085
- ...snapshot,
6086
- context: {
6087
- ...snapshot.context,
6088
- selection: dragOrigin.selection
6019
+ });
6020
+ if (!shouldRun)
6021
+ continue;
6022
+ const actionSets = eventBehavior.actions.map((actionSet) => actionSet({
6023
+ context: editorSnapshot.context,
6024
+ snapshot: editorSnapshot,
6025
+ event
6026
+ }, shouldRun));
6027
+ for (const actionSet of actionSets)
6028
+ actionSet.length !== 0 && (behaviorOverwritten = behaviorOverwritten || actionSet.some((action) => action.type !== "effect"), withApplyingBehaviorActionSet(editor, () => {
6029
+ for (const action of actionSet) {
6030
+ if (action.type === "raise") {
6031
+ performEvent({
6032
+ behaviors,
6033
+ event: action.event,
6034
+ editor,
6035
+ keyGenerator,
6036
+ schema,
6037
+ getSnapshot,
6038
+ defaultActionCallback: void 0,
6039
+ nativeEvent: void 0
6040
+ });
6041
+ continue;
6042
+ }
6043
+ const internalAction = {
6044
+ ...action,
6045
+ editor
6046
+ };
6047
+ try {
6048
+ performAction({
6049
+ context: {
6050
+ keyGenerator,
6051
+ schema
6052
+ },
6053
+ action: internalAction
6054
+ });
6055
+ } catch (error) {
6056
+ console.error(new Error(`Performing action "${internalAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
6057
+ break;
6058
+ }
6089
6059
  }
6090
- }) : !1;
6060
+ }), editor.onChange());
6061
+ if (behaviorOverwritten) {
6062
+ nativeEvent?.preventDefault();
6063
+ break;
6064
+ }
6065
+ }
6066
+ if (!behaviorOverwritten) {
6067
+ if (defaultActionCallback) {
6068
+ withApplyingBehaviorActions(editor, () => {
6069
+ try {
6070
+ defaultActionCallback();
6071
+ } catch (error) {
6072
+ console.error(new Error(`Performing "${event.type}" failed due to: ${error.message}`));
6073
+ }
6074
+ });
6075
+ return;
6076
+ }
6077
+ if (!defaultAction)
6078
+ return;
6079
+ withApplyingBehaviorActions(editor, () => {
6080
+ try {
6081
+ performAction({
6082
+ context: {
6083
+ keyGenerator,
6084
+ schema
6085
+ },
6086
+ action: defaultAction
6087
+ });
6088
+ } catch (error) {
6089
+ console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
6090
+ }
6091
+ }), editor.onChange();
6092
+ }
6093
+ }
6094
+ function slateChildrenToBlocks(schema, value) {
6095
+ const blocks = new Array(value.length);
6096
+ for (let blockIndex = 0; blockIndex < value.length; blockIndex++) {
6097
+ const descendant = value[blockIndex];
6098
+ if (descendant._type !== schema.block.name) {
6099
+ blocks[blockIndex] = {
6100
+ _key: descendant._key,
6101
+ _type: descendant._type,
6102
+ ..."value" in descendant && typeof descendant.value == "object" ? descendant.value : {}
6103
+ };
6104
+ continue;
6105
+ }
6106
+ const children = "children" in descendant ? descendant.children : [], processedChildren = new Array(children.length);
6107
+ for (let childIndex = 0; childIndex < children.length; childIndex++) {
6108
+ const child = children[childIndex];
6109
+ processedChildren[childIndex] = child._type === schema.span.name ? child : {
6110
+ _key: child._key,
6111
+ _type: child._type,
6112
+ ..."value" in child && typeof child.value == "object" ? child.value : {}
6113
+ };
6114
+ }
6115
+ blocks[blockIndex] = {
6116
+ ...descendant,
6117
+ children: processedChildren
6118
+ };
6119
+ }
6120
+ return blocks;
6121
+ }
6122
+ function getActiveDecorators({
6123
+ schema,
6124
+ slateEditorInstance
6125
+ }) {
6126
+ const decorators = schema.decorators.map((decorator) => decorator.value);
6127
+ return ({
6128
+ ...Editor.marks(slateEditorInstance) ?? {}
6129
+ }.marks ?? []).filter((mark) => decorators.includes(mark));
6130
+ }
6131
+ function createEditorSnapshot({
6132
+ converters,
6133
+ editor,
6134
+ keyGenerator,
6135
+ readOnly,
6136
+ schema,
6137
+ hasTag,
6138
+ internalDrag
6139
+ }) {
6140
+ const value = slateChildrenToBlocks(schema, editor.children), selection = editor.selection ? slateRangeToSelection({
6141
+ schema,
6142
+ editor,
6143
+ range: editor.selection
6144
+ }) : null;
6145
+ return {
6146
+ context: {
6147
+ activeDecorators: getActiveDecorators({
6148
+ schema,
6149
+ slateEditorInstance: editor
6150
+ }),
6151
+ converters,
6152
+ keyGenerator,
6153
+ readOnly,
6154
+ schema,
6155
+ selection,
6156
+ value
6091
6157
  },
6092
- actions: [() => [{
6093
- type: "noop"
6094
- }]]
6095
- }),
6096
- defineBehavior({
6097
- on: "drag.drop",
6098
- actions: [({
6099
- event
6100
- }) => [raise({
6101
- type: "select",
6102
- selection: event.position.selection
6103
- }), raise({
6104
- type: "deserialize",
6105
- originEvent: event
6106
- })]]
6107
- }),
6108
- defineBehavior({
6109
- on: "deserialization.success",
6110
- guard: ({
6111
- snapshot,
6112
- event
6158
+ beta: {
6159
+ hasTag,
6160
+ internalDrag
6161
+ }
6162
+ };
6163
+ }
6164
+ const editorMachine = setup({
6165
+ types: {
6166
+ context: {},
6167
+ events: {},
6168
+ emitted: {},
6169
+ input: {},
6170
+ tags: {}
6171
+ },
6172
+ actions: {
6173
+ "add behavior to context": assign({
6174
+ behaviors: ({
6175
+ context,
6176
+ event
6177
+ }) => (assertEvent(event, "add behavior"), /* @__PURE__ */ new Set([...context.behaviors, event.behavior]))
6178
+ }),
6179
+ "remove behavior from context": assign({
6180
+ behaviors: ({
6181
+ context,
6182
+ event
6183
+ }) => (assertEvent(event, "remove behavior"), context.behaviors.delete(event.behavior), /* @__PURE__ */ new Set([...context.behaviors]))
6184
+ }),
6185
+ "assign behaviors": assign({
6186
+ behaviors: ({
6187
+ event
6188
+ }) => (assertEvent(event, "update behaviors"), /* @__PURE__ */ new Set([...event.behaviors]))
6189
+ }),
6190
+ "assign schema": assign({
6191
+ schema: ({
6192
+ event
6193
+ }) => (assertEvent(event, "update schema"), event.schema)
6194
+ }),
6195
+ "emit patch event": enqueueActions(({
6196
+ event,
6197
+ enqueue
6113
6198
  }) => {
6114
- if (event.originEvent.type !== "drag.drop" || snapshot.beta.internalDrag === void 0)
6115
- return !1;
6116
- const dragOrigin = snapshot.beta.internalDrag.origin, dropPosition = event.originEvent.position.selection, droppingOnDragOrigin = dragOrigin ? isOverlappingSelection(dropPosition)({
6117
- ...snapshot,
6118
- context: {
6119
- ...snapshot.context,
6120
- selection: dragOrigin.selection
6121
- }
6122
- }) : !1, draggingEntireBlocks = isSelectingEntireBlocks({
6123
- context: {
6124
- ...snapshot.context,
6125
- selection: dragOrigin.selection
6126
- }
6127
- }), draggedBlocks = getSelectedBlocks({
6128
- context: {
6129
- ...snapshot.context,
6130
- selection: dragOrigin.selection
6131
- }
6199
+ assertEvent(event, "internal.patch"), enqueue.emit(event), enqueue.emit({
6200
+ type: "patch",
6201
+ patch: event.patch
6132
6202
  });
6133
- return droppingOnDragOrigin ? !1 : {
6134
- draggingEntireBlocks,
6135
- draggedBlocks,
6136
- dragOrigin,
6137
- originEvent: event.originEvent
6138
- };
6139
- },
6140
- actions: [({
6141
- event
6142
- }, {
6143
- draggingEntireBlocks,
6144
- draggedBlocks,
6145
- dragOrigin,
6146
- originEvent
6147
- }) => [...draggingEntireBlocks ? draggedBlocks.map((block) => raise({
6148
- type: "delete.block",
6149
- at: block.path
6150
- })) : [raise({
6151
- type: "delete",
6152
- selection: dragOrigin.selection
6153
- })], raise({
6154
- type: "insert.blocks",
6155
- blocks: event.data,
6156
- placement: draggingEntireBlocks ? originEvent.position.block === "start" ? "before" : originEvent.position.block === "end" ? "after" : "auto" : "auto"
6157
- })]]
6158
- }),
6159
- /**
6160
- * If we are pasting text/plain into a text block then we can probably
6161
- * assume that the intended behavior is that the pasted text inherits
6162
- * formatting from the text it's pasted into.
6163
- */
6164
- defineBehavior({
6165
- on: "deserialization.success",
6166
- guard: ({
6167
- snapshot,
6203
+ }),
6204
+ "emit mutation event": emit(({
6168
6205
  event
6206
+ }) => (assertEvent(event, "mutation"), event)),
6207
+ "emit read only": emit({
6208
+ type: "read only"
6209
+ }),
6210
+ "emit editable": emit({
6211
+ type: "editable"
6212
+ }),
6213
+ "defer event": assign({
6214
+ pendingEvents: ({
6215
+ context,
6216
+ event
6217
+ }) => (assertEvent(event, ["internal.patch", "mutation"]), [...context.pendingEvents, event])
6218
+ }),
6219
+ "emit pending events": enqueueActions(({
6220
+ context,
6221
+ enqueue
6169
6222
  }) => {
6170
- if (getFocusTextBlock(snapshot) && event.mimeType === "text/plain" && event.originEvent.type === "clipboard.paste") {
6171
- const activeDecorators = snapshot.context.activeDecorators;
6172
- return {
6173
- activeAnnotations: getActiveAnnotations(snapshot),
6174
- activeDecorators,
6175
- textRuns: event.data.flatMap((block) => isTextBlock(snapshot.context.schema, block) ? [getTextBlockText(block)] : [])
6176
- };
6177
- }
6178
- return !1;
6223
+ for (const event of context.pendingEvents)
6224
+ event.type === "internal.patch" ? (enqueue.emit(event), enqueue.emit({
6225
+ type: "patch",
6226
+ patch: event.patch
6227
+ })) : enqueue.emit(event);
6228
+ }),
6229
+ "emit ready": emit({
6230
+ type: "ready"
6231
+ }),
6232
+ "clear pending events": assign({
6233
+ pendingEvents: []
6234
+ }),
6235
+ "handle behavior event": ({
6236
+ context,
6237
+ event,
6238
+ self
6239
+ }) => {
6240
+ assertEvent(event, ["behavior event"]), performEvent({
6241
+ behaviors: [...context.behaviors.values(), ...defaultBehaviors],
6242
+ event: event.behaviorEvent,
6243
+ editor: event.editor,
6244
+ keyGenerator: context.keyGenerator,
6245
+ schema: context.schema,
6246
+ getSnapshot: () => createEditorSnapshot({
6247
+ converters: [...context.converters],
6248
+ editor: event.editor,
6249
+ keyGenerator: context.keyGenerator,
6250
+ readOnly: self.getSnapshot().matches({
6251
+ "edit mode": "read only"
6252
+ }),
6253
+ schema: context.schema,
6254
+ hasTag: (tag) => self.getSnapshot().hasTag(tag),
6255
+ internalDrag: context.internalDrag
6256
+ }),
6257
+ nativeEvent: event.nativeEvent,
6258
+ defaultActionCallback: event.type === "behavior event" ? event.defaultActionCallback : void 0
6259
+ });
6260
+ }
6261
+ }
6262
+ }).createMachine({
6263
+ id: "editor",
6264
+ context: ({
6265
+ input
6266
+ }) => ({
6267
+ behaviors: /* @__PURE__ */ new Set([...input.behaviors ?? coreBehaviors]),
6268
+ converters: new Set(input.converters ?? []),
6269
+ keyGenerator: input.keyGenerator,
6270
+ pendingEvents: [],
6271
+ schema: input.schema,
6272
+ selection: null,
6273
+ initialReadOnly: input.readOnly ?? !1,
6274
+ maxBlocks: input.maxBlocks,
6275
+ value: input.value
6276
+ }),
6277
+ on: {
6278
+ "notify.blurred": {
6279
+ actions: emit(({
6280
+ event
6281
+ }) => ({
6282
+ ...event,
6283
+ type: "blurred"
6284
+ }))
6285
+ },
6286
+ "notify.done loading": {
6287
+ actions: emit({
6288
+ type: "done loading"
6289
+ })
6290
+ },
6291
+ "notify.error": {
6292
+ actions: emit(({
6293
+ event
6294
+ }) => ({
6295
+ ...event,
6296
+ type: "error"
6297
+ }))
6298
+ },
6299
+ "notify.invalid value": {
6300
+ actions: emit(({
6301
+ event
6302
+ }) => ({
6303
+ ...event,
6304
+ type: "invalid value"
6305
+ }))
6306
+ },
6307
+ "notify.focused": {
6308
+ actions: emit(({
6309
+ event
6310
+ }) => ({
6311
+ ...event,
6312
+ type: "focused"
6313
+ }))
6179
6314
  },
6180
- actions: [(_, {
6181
- activeAnnotations,
6182
- activeDecorators,
6183
- textRuns
6184
- }) => textRuns.flatMap((textRun, index) => index !== textRuns.length - 1 ? [raise({
6185
- type: "insert.span",
6186
- text: textRun,
6187
- decorators: activeDecorators,
6188
- annotations: activeAnnotations.map(({
6189
- _key,
6190
- _type,
6191
- ...value
6315
+ "notify.selection": {
6316
+ actions: [assign({
6317
+ selection: ({
6318
+ event
6319
+ }) => event.selection
6320
+ }), emit(({
6321
+ event
6192
6322
  }) => ({
6193
- name: _type,
6194
- value
6323
+ ...event,
6324
+ type: "selection"
6325
+ }))]
6326
+ },
6327
+ "notify.unset": {
6328
+ actions: emit(({
6329
+ event
6330
+ }) => ({
6331
+ ...event,
6332
+ type: "unset"
6195
6333
  }))
6196
- }), raise({
6197
- type: "insert.break"
6198
- })] : [raise({
6199
- type: "insert.span",
6200
- text: textRun,
6201
- decorators: activeDecorators,
6202
- annotations: activeAnnotations.map(({
6203
- _key,
6204
- _type,
6205
- ...value
6334
+ },
6335
+ "notify.loading": {
6336
+ actions: emit({
6337
+ type: "loading"
6338
+ })
6339
+ },
6340
+ "notify.value changed": {
6341
+ actions: emit(({
6342
+ event
6206
6343
  }) => ({
6207
- name: _type,
6208
- value
6344
+ ...event,
6345
+ type: "value changed"
6209
6346
  }))
6210
- })])]
6211
- }),
6212
- defineBehavior({
6213
- on: "deserialization.success",
6214
- actions: [({
6215
- event
6216
- }) => [raise({
6217
- type: "insert.blocks",
6218
- blocks: event.data,
6219
- placement: "auto"
6220
- })]]
6221
- }),
6222
- defineBehavior({
6223
- on: "deserialization.failure",
6224
- actions: [({
6225
- event
6226
- }) => [{
6227
- type: "effect",
6228
- effect: () => {
6229
- console.warn(`Deserialization of ${event.mimeType} failed with reason "${event.reason}"`);
6230
- }
6231
- }]]
6232
- }),
6233
- defineBehavior({
6234
- on: "clipboard.paste",
6235
- guard: ({
6236
- snapshot
6237
- }) => snapshot.context.selection && isSelectionExpanded(snapshot) ? {
6238
- selection: snapshot.context.selection
6239
- } : !1,
6240
- actions: [({
6241
- event
6242
- }, {
6243
- selection
6244
- }) => [raise({
6245
- type: "delete",
6246
- selection
6247
- }), raise({
6248
- type: "deserialize",
6249
- originEvent: event
6250
- })]]
6251
- }),
6252
- defineBehavior({
6253
- on: "clipboard.paste",
6254
- actions: [({
6255
- event
6256
- }) => [raise({
6257
- type: "deserialize",
6258
- originEvent: event
6259
- })]]
6260
- }),
6261
- defineBehavior({
6262
- on: "input.*",
6263
- actions: [({
6264
- event
6265
- }) => [raise({
6266
- type: "deserialize",
6267
- originEvent: event
6268
- })]]
6269
- }),
6270
- ...abstractAnnotationBehaviors,
6271
- ...abstractDecoratorBehaviors,
6272
- ...abstractInsertBehaviors,
6273
- ...abstractListItemBehaviors,
6274
- ...abstractMoveBehaviors,
6275
- ...abstractStyleBehaviors,
6276
- ...abstractSelectBehaviors,
6277
- raiseDeserializationSuccessOrFailure,
6278
- raiseSerializationSuccessOrFailure,
6279
- raiseInsertSoftBreak
6280
- ], abstractBehaviorEventTypes = ["annotation.toggle", "decorator.toggle", "deserialize", "deserialization.success", "deserialization.failure", "insert.blocks", "list item.add", "list item.remove", "list item.toggle", "move.block down", "move.block up", "select.previous block", "select.next block", "serialize", "serialization.success", "serialization.failure", "style.add", "style.remove", "style.toggle"];
6281
- function isAbstractBehaviorEvent(event) {
6282
- return abstractBehaviorEventTypes.includes(event.type);
6283
- }
6284
- const nativeBehaviorEventTypes = ["clipboard.copy", "clipboard.cut", "clipboard.paste", "drag.dragstart", "drag.drag", "drag.dragend", "drag.dragenter", "drag.dragover", "drag.dragleave", "drag.drop", "input.*", "keyboard.keydown", "keyboard.keyup", "mouse.click"];
6285
- function isNativeBehaviorEvent(event) {
6286
- return nativeBehaviorEventTypes.includes(event.type);
6287
- }
6288
- function isCustomBehaviorEvent(event) {
6289
- return event.type.startsWith("custom.");
6290
- }
6291
- const debug$2 = debugWithName("behaviors:event");
6292
- function eventCategory(event) {
6293
- return isNativeBehaviorEvent(event) ? "native" : isAbstractBehaviorEvent(event) ? "abstract" : isCustomBehaviorEvent(event) ? "custom" : "synthetic";
6294
- }
6295
- function performEvent({
6296
- behaviors,
6297
- event,
6298
- editor,
6299
- keyGenerator,
6300
- schema,
6301
- getSnapshot,
6302
- nativeEvent,
6303
- defaultActionCallback
6304
- }) {
6305
- debug$2(`(${eventCategory(event)})`, JSON.stringify(event, null, 2));
6306
- const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isAbstractBehaviorEvent(event) ? void 0 : {
6307
- ...event,
6308
- editor
6309
- }, eventBehaviors = behaviors.filter((behavior) => {
6310
- if (behavior.on === "*")
6311
- return !0;
6312
- const [listenedNamespace] = behavior.on.includes("*") && behavior.on.includes(".") ? behavior.on.split(".") : [void 0], [eventNamespace] = event.type.includes(".") ? event.type.split(".") : [void 0];
6313
- return listenedNamespace !== void 0 && eventNamespace !== void 0 && listenedNamespace === eventNamespace || listenedNamespace !== void 0 && eventNamespace === void 0 && listenedNamespace === event.type ? !0 : behavior.on === event.type;
6314
- });
6315
- if (eventBehaviors.length === 0) {
6316
- if (defaultActionCallback) {
6317
- withApplyingBehaviorActions(editor, () => {
6318
- try {
6319
- defaultActionCallback();
6320
- } catch (error) {
6321
- console.error(new Error(`Performing action "${event.type}" failed due to: ${error.message}`));
6322
- }
6323
- });
6324
- return;
6347
+ },
6348
+ "add behavior": {
6349
+ actions: "add behavior to context"
6350
+ },
6351
+ "remove behavior": {
6352
+ actions: "remove behavior from context"
6353
+ },
6354
+ patches: {
6355
+ actions: emit(({
6356
+ event
6357
+ }) => event)
6358
+ },
6359
+ "update behaviors": {
6360
+ actions: "assign behaviors"
6361
+ },
6362
+ "update key generator": {
6363
+ actions: assign({
6364
+ keyGenerator: ({
6365
+ event
6366
+ }) => event.keyGenerator
6367
+ })
6368
+ },
6369
+ "update schema": {
6370
+ actions: "assign schema"
6371
+ },
6372
+ "update value": {
6373
+ actions: assign({
6374
+ value: ({
6375
+ event
6376
+ }) => event.value
6377
+ })
6378
+ },
6379
+ "update maxBlocks": {
6380
+ actions: assign({
6381
+ maxBlocks: ({
6382
+ event
6383
+ }) => event.maxBlocks
6384
+ })
6325
6385
  }
6326
- if (!defaultAction)
6327
- return;
6328
- withApplyingBehaviorActions(editor, () => {
6329
- try {
6330
- performAction({
6331
- context: {
6332
- keyGenerator,
6333
- schema
6386
+ },
6387
+ type: "parallel",
6388
+ states: {
6389
+ "edit mode": {
6390
+ initial: "read only",
6391
+ states: {
6392
+ "read only": {
6393
+ initial: "determine initial edit mode",
6394
+ on: {
6395
+ "behavior event": {
6396
+ actions: "handle behavior event",
6397
+ guard: ({
6398
+ event
6399
+ }) => event.behaviorEvent.type === "clipboard.copy" || event.behaviorEvent.type === "mouse.click" || event.behaviorEvent.type === "serialize" || event.behaviorEvent.type === "serialization.failure" || event.behaviorEvent.type === "serialization.success" || event.behaviorEvent.type === "select"
6400
+ }
6401
+ },
6402
+ states: {
6403
+ "determine initial edit mode": {
6404
+ on: {
6405
+ "done syncing initial value": [{
6406
+ target: "#editor.edit mode.read only.read only",
6407
+ guard: ({
6408
+ context
6409
+ }) => context.initialReadOnly
6410
+ }, {
6411
+ target: "#editor.edit mode.editable"
6412
+ }]
6413
+ }
6414
+ },
6415
+ "read only": {
6416
+ on: {
6417
+ "update readOnly": {
6418
+ guard: ({
6419
+ event
6420
+ }) => !event.readOnly,
6421
+ target: "#editor.edit mode.editable",
6422
+ actions: ["emit editable"]
6423
+ }
6424
+ }
6425
+ }
6426
+ }
6427
+ },
6428
+ editable: {
6429
+ on: {
6430
+ "update readOnly": {
6431
+ guard: ({
6432
+ event
6433
+ }) => event.readOnly,
6434
+ target: "#editor.edit mode.read only.read only",
6435
+ actions: ["emit read only"]
6436
+ },
6437
+ "behavior event": {
6438
+ actions: "handle behavior event"
6439
+ }
6334
6440
  },
6335
- action: defaultAction
6336
- });
6337
- } catch (error) {
6338
- console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
6441
+ initial: "idle",
6442
+ states: {
6443
+ idle: {
6444
+ on: {
6445
+ dragstart: {
6446
+ actions: [assign({
6447
+ internalDrag: ({
6448
+ event
6449
+ }) => ({
6450
+ ghost: event.ghost,
6451
+ origin: event.origin
6452
+ })
6453
+ })],
6454
+ target: "dragging internally"
6455
+ }
6456
+ }
6457
+ },
6458
+ "dragging internally": {
6459
+ exit: [({
6460
+ context
6461
+ }) => {
6462
+ if (context.internalDrag?.ghost)
6463
+ try {
6464
+ context.internalDrag.ghost.parentNode?.removeChild(context.internalDrag.ghost);
6465
+ } catch (error) {
6466
+ console.error(new Error(`Removing the internal drag ghost failed due to: ${error.message}`));
6467
+ }
6468
+ }, assign({
6469
+ internalDrag: void 0
6470
+ })],
6471
+ tags: ["dragging internally"],
6472
+ on: {
6473
+ dragend: {
6474
+ target: "idle"
6475
+ },
6476
+ drop: {
6477
+ target: "idle"
6478
+ }
6479
+ }
6480
+ }
6481
+ }
6482
+ }
6339
6483
  }
6340
- }), editor.onChange();
6341
- return;
6342
- }
6343
- const editorSnapshot = getSnapshot();
6344
- let behaviorOverwritten = !1;
6345
- for (const eventBehavior of eventBehaviors) {
6346
- const shouldRun = eventBehavior.guard === void 0 || eventBehavior.guard({
6347
- context: editorSnapshot.context,
6348
- snapshot: editorSnapshot,
6349
- event
6350
- });
6351
- if (!shouldRun)
6352
- continue;
6353
- const actionSets = eventBehavior.actions.map((actionSet) => actionSet({
6354
- context: editorSnapshot.context,
6355
- snapshot: editorSnapshot,
6356
- event
6357
- }, shouldRun));
6358
- for (const actionSet of actionSets)
6359
- actionSet.length !== 0 && (behaviorOverwritten = behaviorOverwritten || actionSet.some((action) => action.type !== "effect"), withApplyingBehaviorActionSet(editor, () => {
6360
- for (const action of actionSet) {
6361
- if (action.type === "raise") {
6362
- performEvent({
6363
- behaviors,
6364
- event: action.event,
6365
- editor,
6366
- keyGenerator,
6367
- schema,
6368
- getSnapshot,
6369
- defaultActionCallback: void 0,
6370
- nativeEvent: void 0
6371
- });
6372
- continue;
6484
+ },
6485
+ setup: {
6486
+ initial: "setting up",
6487
+ states: {
6488
+ "setting up": {
6489
+ exit: ["emit ready"],
6490
+ on: {
6491
+ "internal.patch": {
6492
+ actions: "defer event"
6493
+ },
6494
+ mutation: {
6495
+ actions: "defer event"
6496
+ },
6497
+ "done syncing initial value": {
6498
+ target: "pristine"
6499
+ }
6373
6500
  }
6374
- const internalAction = {
6375
- ...action,
6376
- editor
6377
- };
6378
- try {
6379
- performAction({
6380
- context: {
6381
- keyGenerator,
6382
- schema
6383
- },
6384
- action: internalAction
6385
- });
6386
- } catch (error) {
6387
- console.error(new Error(`Performing action "${internalAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
6388
- break;
6501
+ },
6502
+ pristine: {
6503
+ initial: "idle",
6504
+ states: {
6505
+ idle: {
6506
+ on: {
6507
+ normalizing: {
6508
+ target: "normalizing"
6509
+ },
6510
+ "internal.patch": {
6511
+ actions: "defer event",
6512
+ target: "#editor.setup.dirty"
6513
+ },
6514
+ mutation: {
6515
+ actions: "defer event",
6516
+ target: "#editor.setup.dirty"
6517
+ }
6518
+ }
6519
+ },
6520
+ normalizing: {
6521
+ on: {
6522
+ "done normalizing": {
6523
+ target: "idle"
6524
+ },
6525
+ "internal.patch": {
6526
+ actions: "defer event"
6527
+ },
6528
+ mutation: {
6529
+ actions: "defer event"
6530
+ }
6531
+ }
6532
+ }
6533
+ }
6534
+ },
6535
+ dirty: {
6536
+ entry: ["emit pending events", "clear pending events"],
6537
+ on: {
6538
+ "internal.patch": {
6539
+ actions: "emit patch event"
6540
+ },
6541
+ mutation: {
6542
+ actions: "emit mutation event"
6543
+ }
6389
6544
  }
6390
6545
  }
6391
- }), editor.onChange());
6392
- if (behaviorOverwritten) {
6393
- nativeEvent?.preventDefault();
6394
- break;
6395
- }
6396
- }
6397
- if (!behaviorOverwritten) {
6398
- if (defaultActionCallback) {
6399
- withApplyingBehaviorActions(editor, () => {
6400
- try {
6401
- defaultActionCallback();
6402
- } catch (error) {
6403
- console.error(new Error(`Performing "${event.type}" failed due to: ${error.message}`));
6404
- }
6405
- });
6406
- return;
6407
- }
6408
- if (!defaultAction)
6409
- return;
6410
- withApplyingBehaviorActions(editor, () => {
6411
- try {
6412
- performAction({
6413
- context: {
6414
- keyGenerator,
6415
- schema
6416
- },
6417
- action: defaultAction
6418
- });
6419
- } catch (error) {
6420
- console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`));
6421
6546
  }
6422
- }), editor.onChange();
6423
- }
6424
- }
6425
- function slateChildrenToBlocks(schema, value) {
6426
- const blocks = new Array(value.length);
6427
- for (let blockIndex = 0; blockIndex < value.length; blockIndex++) {
6428
- const descendant = value[blockIndex];
6429
- if (descendant._type !== schema.block.name) {
6430
- blocks[blockIndex] = {
6431
- _key: descendant._key,
6432
- _type: descendant._type,
6433
- ..."value" in descendant && typeof descendant.value == "object" ? descendant.value : {}
6434
- };
6435
- continue;
6436
- }
6437
- const children = "children" in descendant ? descendant.children : [], processedChildren = new Array(children.length);
6438
- for (let childIndex = 0; childIndex < children.length; childIndex++) {
6439
- const child = children[childIndex];
6440
- processedChildren[childIndex] = child._type === schema.span.name ? child : {
6441
- _key: child._key,
6442
- _type: child._type,
6443
- ..."value" in child && typeof child.value == "object" ? child.value : {}
6444
- };
6445
6547
  }
6446
- blocks[blockIndex] = {
6447
- ...descendant,
6448
- children: processedChildren
6449
- };
6450
6548
  }
6451
- return blocks;
6452
- }
6453
- function getActiveDecorators({
6454
- schema,
6455
- slateEditorInstance
6456
- }) {
6457
- const decorators = schema.decorators.map((decorator) => decorator.value);
6458
- return ({
6459
- ...Editor.marks(slateEditorInstance) ?? {}
6460
- }.marks ?? []).filter((mark) => decorators.includes(mark));
6549
+ });
6550
+ function defaultCompare(a, b) {
6551
+ return a === b;
6461
6552
  }
6462
- function createEditorSnapshot({
6463
- converters,
6464
- editor,
6465
- keyGenerator,
6466
- readOnly,
6467
- schema,
6468
- hasTag,
6469
- internalDrag
6553
+ function useEditorSelector(editor, selector, t0) {
6554
+ const $ = c(3), compare = t0 === void 0 ? defaultCompare : t0;
6555
+ let t1;
6556
+ return $[0] !== editor || $[1] !== selector ? (t1 = (editorActorSnapshot) => {
6557
+ const snapshot = getEditorSnapshot({
6558
+ editorActorSnapshot,
6559
+ slateEditorInstance: editor._internal.slateEditor.instance
6560
+ });
6561
+ return selector(snapshot);
6562
+ }, $[0] = editor, $[1] = selector, $[2] = t1) : t1 = $[2], useSelector(editor._internal.editorActor, t1, compare);
6563
+ }
6564
+ function getEditorSnapshot({
6565
+ editorActorSnapshot,
6566
+ slateEditorInstance
6470
6567
  }) {
6471
- const value = slateChildrenToBlocks(schema, editor.children), selection = editor.selection ? slateRangeToSelection({
6472
- schema,
6473
- editor,
6474
- range: editor.selection
6475
- }) : null;
6476
6568
  return {
6477
6569
  context: {
6570
+ converters: [...editorActorSnapshot.context.converters],
6478
6571
  activeDecorators: getActiveDecorators({
6479
- schema,
6480
- slateEditorInstance: editor
6572
+ schema: editorActorSnapshot.context.schema,
6573
+ slateEditorInstance
6481
6574
  }),
6482
- converters,
6483
- keyGenerator,
6484
- readOnly,
6485
- schema,
6486
- selection,
6487
- value
6575
+ keyGenerator: editorActorSnapshot.context.keyGenerator,
6576
+ readOnly: editorActorSnapshot.matches({
6577
+ "edit mode": "read only"
6578
+ }),
6579
+ schema: editorActorSnapshot.context.schema,
6580
+ selection: editorActorSnapshot.context.selection,
6581
+ value: slateChildrenToBlocks(editorActorSnapshot.context.schema, slateEditorInstance.children)
6488
6582
  },
6489
6583
  beta: {
6490
- hasTag,
6491
- internalDrag
6584
+ hasTag: (tag) => editorActorSnapshot.hasTag(tag),
6585
+ internalDrag: editorActorSnapshot.context.internalDrag
6492
6586
  }
6493
6587
  };
6494
6588
  }
6495
- const editorMachine = setup({
6496
- types: {
6497
- context: {},
6498
- events: {},
6499
- emitted: {},
6500
- input: {},
6501
- tags: {}
6502
- },
6503
- actions: {
6504
- "add behavior to context": assign({
6505
- behaviors: ({
6506
- context,
6507
- event
6508
- }) => (assertEvent(event, "add behavior"), /* @__PURE__ */ new Set([...context.behaviors, event.behavior]))
6509
- }),
6510
- "remove behavior from context": assign({
6511
- behaviors: ({
6512
- context,
6513
- event
6514
- }) => (assertEvent(event, "remove behavior"), context.behaviors.delete(event.behavior), /* @__PURE__ */ new Set([...context.behaviors]))
6515
- }),
6516
- "assign behaviors": assign({
6517
- behaviors: ({
6518
- event
6519
- }) => (assertEvent(event, "update behaviors"), /* @__PURE__ */ new Set([...event.behaviors]))
6520
- }),
6521
- "assign schema": assign({
6522
- schema: ({
6523
- event
6524
- }) => (assertEvent(event, "update schema"), event.schema)
6525
- }),
6526
- "emit patch event": enqueueActions(({
6527
- event,
6528
- enqueue
6529
- }) => {
6530
- assertEvent(event, "internal.patch"), enqueue.emit(event), enqueue.emit({
6531
- type: "patch",
6532
- patch: event.patch
6533
- });
6534
- }),
6535
- "emit mutation event": emit(({
6536
- event
6537
- }) => (assertEvent(event, "mutation"), event)),
6538
- "emit read only": emit({
6539
- type: "read only"
6540
- }),
6541
- "emit editable": emit({
6542
- type: "editable"
6543
- }),
6544
- "defer event": assign({
6545
- pendingEvents: ({
6546
- context,
6547
- event
6548
- }) => (assertEvent(event, ["internal.patch", "mutation"]), [...context.pendingEvents, event])
6549
- }),
6550
- "emit pending events": enqueueActions(({
6551
- context,
6552
- enqueue
6553
- }) => {
6554
- for (const event of context.pendingEvents)
6555
- event.type === "internal.patch" ? (enqueue.emit(event), enqueue.emit({
6556
- type: "patch",
6557
- patch: event.patch
6558
- })) : enqueue.emit(event);
6559
- }),
6560
- "emit ready": emit({
6561
- type: "ready"
6562
- }),
6563
- "clear pending events": assign({
6564
- pendingEvents: []
6565
- }),
6566
- "handle behavior event": ({
6567
- context,
6568
- event,
6569
- self
6570
- }) => {
6571
- assertEvent(event, ["behavior event"]), performEvent({
6572
- behaviors: [...context.behaviors.values(), ...defaultBehaviors],
6573
- event: event.behaviorEvent,
6574
- editor: event.editor,
6575
- keyGenerator: context.keyGenerator,
6576
- schema: context.schema,
6577
- getSnapshot: () => createEditorSnapshot({
6578
- converters: [...context.converters],
6579
- editor: event.editor,
6580
- keyGenerator: context.keyGenerator,
6581
- readOnly: self.getSnapshot().matches({
6582
- "edit mode": "read only"
6583
- }),
6584
- schema: context.schema,
6585
- hasTag: (tag) => self.getSnapshot().hasTag(tag),
6586
- internalDrag: context.internalDrag
6587
- }),
6588
- nativeEvent: event.nativeEvent,
6589
- defaultActionCallback: event.type === "behavior event" ? event.defaultActionCallback : void 0
6589
+ const debug$2 = debugWithName("API:editable");
6590
+ function createEditableAPI(editor, editorActor) {
6591
+ const types = editorActor.getSnapshot().context.schema;
6592
+ return {
6593
+ focus: () => {
6594
+ editorActor.send({
6595
+ type: "behavior event",
6596
+ behaviorEvent: {
6597
+ type: "focus"
6598
+ },
6599
+ editor
6590
6600
  });
6591
- }
6592
- }
6593
- }).createMachine({
6594
- id: "editor",
6595
- context: ({
6596
- input
6597
- }) => ({
6598
- behaviors: /* @__PURE__ */ new Set([...input.behaviors ?? coreBehaviors]),
6599
- converters: new Set(input.converters ?? []),
6600
- keyGenerator: input.keyGenerator,
6601
- pendingEvents: [],
6602
- schema: input.schema,
6603
- selection: null,
6604
- initialReadOnly: input.readOnly ?? !1,
6605
- maxBlocks: input.maxBlocks,
6606
- value: input.value
6607
- }),
6608
- on: {
6609
- "notify.blurred": {
6610
- actions: emit(({
6611
- event
6612
- }) => ({
6613
- ...event,
6614
- type: "blurred"
6615
- }))
6616
- },
6617
- "notify.done loading": {
6618
- actions: emit({
6619
- type: "done loading"
6620
- })
6621
- },
6622
- "notify.error": {
6623
- actions: emit(({
6624
- event
6625
- }) => ({
6626
- ...event,
6627
- type: "error"
6628
- }))
6629
- },
6630
- "notify.invalid value": {
6631
- actions: emit(({
6632
- event
6633
- }) => ({
6634
- ...event,
6635
- type: "invalid value"
6636
- }))
6637
- },
6638
- "notify.focused": {
6639
- actions: emit(({
6640
- event
6641
- }) => ({
6642
- ...event,
6643
- type: "focused"
6644
- }))
6645
- },
6646
- "notify.selection": {
6647
- actions: [assign({
6648
- selection: ({
6649
- event
6650
- }) => event.selection
6651
- }), emit(({
6652
- event
6653
- }) => ({
6654
- ...event,
6655
- type: "selection"
6656
- }))]
6657
6601
  },
6658
- "notify.unset": {
6659
- actions: emit(({
6660
- event
6661
- }) => ({
6662
- ...event,
6663
- type: "unset"
6664
- }))
6602
+ blur: () => {
6603
+ editorActor.send({
6604
+ type: "behavior event",
6605
+ behaviorEvent: {
6606
+ type: "blur"
6607
+ },
6608
+ editor
6609
+ });
6665
6610
  },
6666
- "notify.loading": {
6667
- actions: emit({
6668
- type: "loading"
6669
- })
6611
+ toggleMark: (mark) => {
6612
+ editorActor.send({
6613
+ type: "behavior event",
6614
+ behaviorEvent: {
6615
+ type: "decorator.toggle",
6616
+ decorator: mark
6617
+ },
6618
+ editor
6619
+ });
6670
6620
  },
6671
- "notify.value changed": {
6672
- actions: emit(({
6673
- event
6674
- }) => ({
6675
- ...event,
6676
- type: "value changed"
6677
- }))
6621
+ toggleList: (listItem) => {
6622
+ editorActor.send({
6623
+ type: "behavior event",
6624
+ behaviorEvent: {
6625
+ type: "list item.toggle",
6626
+ listItem
6627
+ },
6628
+ editor
6629
+ });
6678
6630
  },
6679
- "add behavior": {
6680
- actions: "add behavior to context"
6631
+ toggleBlockStyle: (style) => {
6632
+ editorActor.send({
6633
+ type: "behavior event",
6634
+ behaviorEvent: {
6635
+ type: "style.toggle",
6636
+ style
6637
+ },
6638
+ editor
6639
+ });
6681
6640
  },
6682
- "remove behavior": {
6683
- actions: "remove behavior from context"
6641
+ isMarkActive: (mark) => {
6642
+ try {
6643
+ return isDecoratorActive({
6644
+ editor,
6645
+ decorator: mark
6646
+ });
6647
+ } catch (err) {
6648
+ return console.warn(err), !1;
6649
+ }
6684
6650
  },
6685
- patches: {
6686
- actions: emit(({
6687
- event
6688
- }) => event)
6651
+ marks: () => ({
6652
+ ...Editor.marks(editor) || {}
6653
+ }).marks || [],
6654
+ undo: () => {
6655
+ editorActor.send({
6656
+ type: "behavior event",
6657
+ behaviorEvent: {
6658
+ type: "history.undo"
6659
+ },
6660
+ editor
6661
+ });
6689
6662
  },
6690
- "update behaviors": {
6691
- actions: "assign behaviors"
6663
+ redo: () => {
6664
+ editorActor.send({
6665
+ type: "behavior event",
6666
+ behaviorEvent: {
6667
+ type: "history.redo"
6668
+ },
6669
+ editor
6670
+ });
6692
6671
  },
6693
- "update key generator": {
6694
- actions: assign({
6695
- keyGenerator: ({
6696
- event
6697
- }) => event.keyGenerator
6698
- })
6672
+ select: (selection) => {
6673
+ const slateSelection = toSlateRange(selection, editor);
6674
+ slateSelection ? Transforms.select(editor, slateSelection) : Transforms.deselect(editor), editor.onChange();
6699
6675
  },
6700
- "update schema": {
6701
- actions: "assign schema"
6676
+ focusBlock: () => {
6677
+ if (editor.selection) {
6678
+ const block = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
6679
+ if (block)
6680
+ return fromSlateValue([block], types.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0];
6681
+ }
6702
6682
  },
6703
- "update value": {
6704
- actions: assign({
6705
- value: ({
6706
- event
6707
- }) => event.value
6708
- })
6683
+ focusChild: () => {
6684
+ if (editor.selection) {
6685
+ const block = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
6686
+ if (block && editor.isTextBlock(block))
6687
+ return fromSlateValue([block], types.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0].children[editor.selection.focus.path[1]];
6688
+ }
6709
6689
  },
6710
- "update maxBlocks": {
6711
- actions: assign({
6712
- maxBlocks: ({
6713
- event
6714
- }) => event.maxBlocks
6715
- })
6716
- }
6717
- },
6718
- type: "parallel",
6719
- states: {
6720
- "edit mode": {
6721
- initial: "read only",
6722
- states: {
6723
- "read only": {
6724
- initial: "determine initial edit mode",
6725
- on: {
6726
- "behavior event": {
6727
- actions: "handle behavior event",
6728
- guard: ({
6729
- event
6730
- }) => event.behaviorEvent.type === "clipboard.copy" || event.behaviorEvent.type === "mouse.click" || event.behaviorEvent.type === "serialize" || event.behaviorEvent.type === "serialization.failure" || event.behaviorEvent.type === "serialization.success" || event.behaviorEvent.type === "select"
6690
+ insertChild: (type, value) => {
6691
+ if (type.name !== types.span.name)
6692
+ return editorActor.send({
6693
+ type: "behavior event",
6694
+ behaviorEvent: {
6695
+ type: "insert.inline object",
6696
+ inlineObject: {
6697
+ name: type.name,
6698
+ value
6731
6699
  }
6732
6700
  },
6733
- states: {
6734
- "determine initial edit mode": {
6735
- on: {
6736
- "done syncing initial value": [{
6737
- target: "#editor.edit mode.read only.read only",
6738
- guard: ({
6739
- context
6740
- }) => context.initialReadOnly
6741
- }, {
6742
- target: "#editor.edit mode.editable"
6743
- }]
6744
- }
6745
- },
6746
- "read only": {
6747
- on: {
6748
- "update readOnly": {
6749
- guard: ({
6750
- event
6751
- }) => !event.readOnly,
6752
- target: "#editor.edit mode.editable",
6753
- actions: ["emit editable"]
6754
- }
6755
- }
6756
- }
6757
- }
6701
+ editor
6702
+ }), editor.selection ? slateRangeToSelection({
6703
+ schema: editorActor.getSnapshot().context.schema,
6704
+ editor,
6705
+ range: editor.selection
6706
+ })?.focus.path ?? [] : [];
6707
+ if (!editor.selection)
6708
+ throw new Error("The editor has no selection");
6709
+ const [focusBlock] = Array.from(Editor.nodes(editor, {
6710
+ at: editor.selection.focus.path.slice(0, 1),
6711
+ match: (n) => n._type === types.block.name
6712
+ }))[0] || [void 0];
6713
+ if (!focusBlock)
6714
+ throw new Error("No focused text block");
6715
+ if (type.name !== types.span.name && !types.inlineObjects.some((t) => t.name === type.name))
6716
+ throw new Error("This type cannot be inserted as a child to a text block");
6717
+ const child = toSlateValue([{
6718
+ _key: editorActor.getSnapshot().context.keyGenerator(),
6719
+ _type: types.block.name,
6720
+ children: [{
6721
+ _key: editorActor.getSnapshot().context.keyGenerator(),
6722
+ _type: type.name,
6723
+ ...value || {}
6724
+ }]
6725
+ }], {
6726
+ schemaTypes: editorActor.getSnapshot().context.schema
6727
+ })[0].children[0], focusChildPath = editor.selection.focus.path.slice(0, 2), isSpanNode = child._type === types.span.name, focusNode = Node.get(editor, focusChildPath);
6728
+ return isSpanNode && focusNode._type !== types.span.name && (debug$2("Inserting span child next to inline object child, moving selection + 1"), editor.move({
6729
+ distance: 1,
6730
+ unit: "character"
6731
+ })), Transforms.insertNodes(editor, child, {
6732
+ select: !0,
6733
+ at: editor.selection
6734
+ }), editor.onChange(), editor.selection ? slateRangeToSelection({
6735
+ schema: editorActor.getSnapshot().context.schema,
6736
+ editor,
6737
+ range: editor.selection
6738
+ })?.focus.path ?? [] : [];
6739
+ },
6740
+ insertBlock: (type, value) => (editorActor.send({
6741
+ type: "behavior event",
6742
+ behaviorEvent: {
6743
+ type: "insert.block",
6744
+ block: {
6745
+ _type: type.name,
6746
+ ...value || {}
6758
6747
  },
6759
- editable: {
6760
- on: {
6761
- "update readOnly": {
6762
- guard: ({
6763
- event
6764
- }) => event.readOnly,
6765
- target: "#editor.edit mode.read only.read only",
6766
- actions: ["emit read only"]
6767
- },
6768
- "behavior event": {
6769
- actions: "handle behavior event"
6770
- }
6771
- },
6772
- initial: "idle",
6773
- states: {
6774
- idle: {
6775
- on: {
6776
- dragstart: {
6777
- actions: [assign({
6778
- internalDrag: ({
6779
- event
6780
- }) => ({
6781
- ghost: event.ghost,
6782
- origin: event.origin
6783
- })
6784
- })],
6785
- target: "dragging internally"
6786
- }
6787
- }
6788
- },
6789
- "dragging internally": {
6790
- exit: [({
6791
- context
6792
- }) => {
6793
- if (context.internalDrag?.ghost)
6794
- try {
6795
- context.internalDrag.ghost.parentNode?.removeChild(context.internalDrag.ghost);
6796
- } catch (error) {
6797
- console.error(new Error(`Removing the internal drag ghost failed due to: ${error.message}`));
6798
- }
6799
- }, assign({
6800
- internalDrag: void 0
6801
- })],
6802
- tags: ["dragging internally"],
6803
- on: {
6804
- dragend: {
6805
- target: "idle"
6806
- },
6807
- drop: {
6808
- target: "idle"
6809
- }
6810
- }
6811
- }
6748
+ placement: "auto"
6749
+ },
6750
+ editor
6751
+ }), editor.selection ? slateRangeToSelection({
6752
+ schema: editorActor.getSnapshot().context.schema,
6753
+ editor,
6754
+ range: editor.selection
6755
+ })?.focus.path ?? [] : []),
6756
+ hasBlockStyle: (style) => {
6757
+ try {
6758
+ return isStyleActive({
6759
+ editor,
6760
+ style
6761
+ });
6762
+ } catch {
6763
+ return !1;
6764
+ }
6765
+ },
6766
+ hasListStyle: (listItem) => {
6767
+ try {
6768
+ return isListItemActive({
6769
+ editor,
6770
+ listItem
6771
+ });
6772
+ } catch {
6773
+ return !1;
6774
+ }
6775
+ },
6776
+ isVoid: (element) => ![types.block.name, types.span.name].includes(element._type),
6777
+ findByPath: (path) => {
6778
+ const slatePath = toSlateRange({
6779
+ focus: {
6780
+ path,
6781
+ offset: 0
6782
+ },
6783
+ anchor: {
6784
+ path,
6785
+ offset: 0
6786
+ }
6787
+ }, editor);
6788
+ if (slatePath) {
6789
+ const [block, blockPath] = Editor.node(editor, slatePath.focus.path.slice(0, 1));
6790
+ if (block && blockPath && typeof block._key == "string") {
6791
+ if (path.length === 1 && slatePath.focus.path.length === 1)
6792
+ return [fromSlateValue([block], types.block.name)[0], [{
6793
+ _key: block._key
6794
+ }]];
6795
+ const ptBlock = fromSlateValue([block], types.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0];
6796
+ if (editor.isTextBlock(ptBlock)) {
6797
+ const ptChild = ptBlock.children[slatePath.focus.path[1]];
6798
+ if (ptChild)
6799
+ return [ptChild, [{
6800
+ _key: block._key
6801
+ }, "children", {
6802
+ _key: ptChild._key
6803
+ }]];
6812
6804
  }
6813
6805
  }
6814
6806
  }
6807
+ return [void 0, void 0];
6815
6808
  },
6816
- setup: {
6817
- initial: "setting up",
6818
- states: {
6819
- "setting up": {
6820
- exit: ["emit ready"],
6821
- on: {
6822
- "internal.patch": {
6823
- actions: "defer event"
6824
- },
6825
- mutation: {
6826
- actions: "defer event"
6827
- },
6828
- "done syncing initial value": {
6829
- target: "pristine"
6830
- }
6831
- }
6832
- },
6833
- pristine: {
6834
- initial: "idle",
6835
- states: {
6836
- idle: {
6837
- on: {
6838
- normalizing: {
6839
- target: "normalizing"
6840
- },
6841
- "internal.patch": {
6842
- actions: "defer event",
6843
- target: "#editor.setup.dirty"
6844
- },
6845
- mutation: {
6846
- actions: "defer event",
6847
- target: "#editor.setup.dirty"
6848
- }
6849
- }
6809
+ findDOMNode: (element) => {
6810
+ let node;
6811
+ try {
6812
+ const [item] = Array.from(Editor.nodes(editor, {
6813
+ at: [],
6814
+ match: (n) => n._key === element._key
6815
+ }) || [])[0] || [void 0];
6816
+ node = ReactEditor.toDOMNode(editor, item);
6817
+ } catch {
6818
+ }
6819
+ return node;
6820
+ },
6821
+ activeAnnotations: () => {
6822
+ if (!editor.selection || editor.selection.focus.path.length < 2)
6823
+ return [];
6824
+ try {
6825
+ const activeAnnotations = [], spans = Editor.nodes(editor, {
6826
+ at: editor.selection,
6827
+ match: (node) => Text.isText(node) && node.marks !== void 0 && Array.isArray(node.marks) && node.marks.length > 0
6828
+ });
6829
+ for (const [span, path] of spans) {
6830
+ const [block] = Editor.node(editor, path, {
6831
+ depth: 1
6832
+ });
6833
+ editor.isTextBlock(block) && block.markDefs?.forEach((def) => {
6834
+ Text.isText(span) && span.marks && Array.isArray(span.marks) && span.marks.includes(def._key) && activeAnnotations.push(def);
6835
+ });
6836
+ }
6837
+ return activeAnnotations;
6838
+ } catch {
6839
+ return [];
6840
+ }
6841
+ },
6842
+ isAnnotationActive: (annotationType) => isAnnotationActive({
6843
+ editor,
6844
+ annotation: {
6845
+ name: annotationType
6846
+ }
6847
+ }),
6848
+ addAnnotation: (type, value) => {
6849
+ let paths;
6850
+ return Editor.withoutNormalizing(editor, () => {
6851
+ paths = addAnnotationActionImplementation({
6852
+ context: {
6853
+ keyGenerator: editorActor.getSnapshot().context.keyGenerator,
6854
+ schema: types
6855
+ },
6856
+ action: {
6857
+ annotation: {
6858
+ name: type.name,
6859
+ value: value ?? {}
6850
6860
  },
6851
- normalizing: {
6852
- on: {
6853
- "done normalizing": {
6854
- target: "idle"
6855
- },
6856
- "internal.patch": {
6857
- actions: "defer event"
6858
- },
6859
- mutation: {
6860
- actions: "defer event"
6861
- }
6862
- }
6863
- }
6861
+ editor
6864
6862
  }
6865
- },
6866
- dirty: {
6867
- entry: ["emit pending events", "clear pending events"],
6868
- on: {
6869
- "internal.patch": {
6870
- actions: "emit patch event"
6871
- },
6872
- mutation: {
6873
- actions: "emit mutation event"
6874
- }
6863
+ });
6864
+ }), editor.onChange(), paths;
6865
+ },
6866
+ delete: (selection, options) => {
6867
+ if (selection) {
6868
+ const range = toSlateRange(selection, editor);
6869
+ if (!(range && range.anchor.path.length > 0 && range.focus.path.length > 0))
6870
+ throw new Error("Invalid range");
6871
+ if (range) {
6872
+ if (!options?.mode || options?.mode === "selected") {
6873
+ debug$2("Deleting content in selection"), Transforms.delete(editor, {
6874
+ at: range,
6875
+ hanging: !0,
6876
+ voids: !0
6877
+ }), editor.onChange();
6878
+ return;
6875
6879
  }
6880
+ options?.mode === "blocks" && (debug$2("Deleting blocks touched by selection"), Transforms.removeNodes(editor, {
6881
+ at: range,
6882
+ voids: !0,
6883
+ match: (node) => editor.isTextBlock(node) || !editor.isTextBlock(node) && Element.isElement(node)
6884
+ })), options?.mode === "children" && (debug$2("Deleting children touched by selection"), Transforms.removeNodes(editor, {
6885
+ at: range,
6886
+ voids: !0,
6887
+ match: (node) => node._type === types.span.name || // Text children
6888
+ !editor.isTextBlock(node) && Element.isElement(node)
6889
+ })), editor.children.length === 0 && (editor.children = [editor.pteCreateTextBlock({
6890
+ decorators: []
6891
+ })]), editor.onChange();
6876
6892
  }
6877
6893
  }
6878
- }
6879
- }
6880
- });
6881
- function defaultCompare(a, b) {
6882
- return a === b;
6883
- }
6884
- function useEditorSelector(editor, selector, t0) {
6885
- const $ = c(3), compare = t0 === void 0 ? defaultCompare : t0;
6886
- let t1;
6887
- return $[0] !== editor || $[1] !== selector ? (t1 = (editorActorSnapshot) => {
6888
- const snapshot = getEditorSnapshot({
6889
- editorActorSnapshot,
6890
- slateEditorInstance: editor._internal.slateEditor.instance
6891
- });
6892
- return selector(snapshot);
6893
- }, $[0] = editor, $[1] = selector, $[2] = t1) : t1 = $[2], useSelector(editor._internal.editorActor, t1, compare);
6894
- }
6895
- function getEditorSnapshot({
6896
- editorActorSnapshot,
6897
- slateEditorInstance
6898
- }) {
6899
- return {
6900
- context: {
6901
- converters: [...editorActorSnapshot.context.converters],
6902
- activeDecorators: getActiveDecorators({
6903
- schema: editorActorSnapshot.context.schema,
6904
- slateEditorInstance
6905
- }),
6906
- keyGenerator: editorActorSnapshot.context.keyGenerator,
6907
- readOnly: editorActorSnapshot.matches({
6908
- "edit mode": "read only"
6909
- }),
6910
- schema: editorActorSnapshot.context.schema,
6911
- selection: editorActorSnapshot.context.selection,
6912
- value: slateChildrenToBlocks(editorActorSnapshot.context.schema, slateEditorInstance.children)
6913
6894
  },
6914
- beta: {
6915
- hasTag: (tag) => editorActorSnapshot.hasTag(tag),
6916
- internalDrag: editorActorSnapshot.context.internalDrag
6895
+ removeAnnotation: (type) => {
6896
+ editorActor.send({
6897
+ type: "behavior event",
6898
+ behaviorEvent: {
6899
+ type: "annotation.remove",
6900
+ annotation: {
6901
+ name: type.name
6902
+ }
6903
+ },
6904
+ editor
6905
+ });
6906
+ },
6907
+ getSelection: () => {
6908
+ let ptRange = null;
6909
+ if (editor.selection) {
6910
+ const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection);
6911
+ if (existing)
6912
+ return existing;
6913
+ ptRange = slateRangeToSelection({
6914
+ schema: editorActor.getSnapshot().context.schema,
6915
+ editor,
6916
+ range: editor.selection
6917
+ }), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
6918
+ }
6919
+ return ptRange;
6920
+ },
6921
+ getValue: () => fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
6922
+ isCollapsedSelection: () => !!editor.selection && Range.isCollapsed(editor.selection),
6923
+ isExpandedSelection: () => !!editor.selection && Range.isExpanded(editor.selection),
6924
+ insertBreak: () => {
6925
+ editor.insertBreak(), editor.onChange();
6926
+ },
6927
+ getFragment: () => fromSlateValue(editor.getFragment(), types.block.name),
6928
+ isSelectionsOverlapping: (selectionA, selectionB) => {
6929
+ const rangeA = toSlateRange(selectionA, editor), rangeB = toSlateRange(selectionB, editor);
6930
+ return Range.isRange(rangeA) && Range.isRange(rangeB) && Range.includes(rangeA, rangeB);
6917
6931
  }
6918
6932
  };
6919
6933
  }
6934
+ function isAnnotationActive({
6935
+ editor,
6936
+ annotation
6937
+ }) {
6938
+ if (!editor.selection || editor.selection.focus.path.length < 2)
6939
+ return !1;
6940
+ try {
6941
+ const spans = [...Editor.nodes(editor, {
6942
+ at: editor.selection,
6943
+ match: (node) => Text.isText(node)
6944
+ })];
6945
+ if (spans.length === 0 || spans.some(([span]) => !isPortableTextSpan$1(span) || !span.marks || span.marks?.length === 0)) return !1;
6946
+ const selectionMarkDefs = spans.reduce((accMarkDefs, [, path]) => {
6947
+ const [block] = Editor.node(editor, path, {
6948
+ depth: 1
6949
+ });
6950
+ return editor.isTextBlock(block) && block.markDefs ? [...accMarkDefs, ...block.markDefs] : accMarkDefs;
6951
+ }, []);
6952
+ return spans.every(([span]) => isPortableTextSpan$1(span) ? span.marks?.map((markKey) => selectionMarkDefs.find((def) => def?._key === markKey)?._type)?.includes(annotation.name) : !1);
6953
+ } catch {
6954
+ return !1;
6955
+ }
6956
+ }
6920
6957
  function createInternalEditor(config) {
6921
6958
  const editorActor = createActor(editorMachine, {
6922
6959
  input: editorConfigToMachineInput(config)
@@ -7025,7 +7062,12 @@ function createInternalEditorFromActor(editorActor) {
7025
7062
  }
7026
7063
  };
7027
7064
  }
7028
- const PortableTextEditorSelectionContext = createContext(null), usePortableTextEditorSelection = () => {
7065
+ const EditorActorContext = createContext({}), PortableTextEditorContext = createContext(null), usePortableTextEditor = () => {
7066
+ const editor = useContext(PortableTextEditorContext);
7067
+ if (!editor)
7068
+ throw new Error("The `usePortableTextEditor` hook must be used inside the <PortableTextEditor> component's context.");
7069
+ return editor;
7070
+ }, PortableTextEditorSelectionContext = createContext(null), usePortableTextEditorSelection = () => {
7029
7071
  const selection = useContext(PortableTextEditorSelectionContext);
7030
7072
  if (selection === void 0)
7031
7073
  throw new Error("The `usePortableTextEditorSelection` hook must be used inside the <PortableTextEditor> component's context.");