@portabletext/editor 1.47.0 → 1.47.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +7 -7
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
  4. package/lib/_chunks-cjs/editor-provider.cjs +379 -326
  5. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  6. package/lib/_chunks-cjs/parse-blocks.cjs +4 -4
  7. package/lib/_chunks-cjs/parse-blocks.cjs.map +1 -1
  8. package/lib/_chunks-cjs/selector.is-overlapping-selection.cjs.map +1 -1
  9. package/lib/_chunks-es/behavior.core.js +7 -7
  10. package/lib/_chunks-es/behavior.core.js.map +1 -1
  11. package/lib/_chunks-es/behavior.markdown.js.map +1 -1
  12. package/lib/_chunks-es/editor-provider.js +380 -327
  13. package/lib/_chunks-es/editor-provider.js.map +1 -1
  14. package/lib/_chunks-es/parse-blocks.js +4 -4
  15. package/lib/_chunks-es/parse-blocks.js.map +1 -1
  16. package/lib/_chunks-es/selector.is-overlapping-selection.js.map +1 -1
  17. package/lib/behaviors/index.cjs.map +1 -1
  18. package/lib/behaviors/index.d.cts +70 -28
  19. package/lib/behaviors/index.d.ts +70 -28
  20. package/lib/behaviors/index.js.map +1 -1
  21. package/lib/index.cjs +12 -14
  22. package/lib/index.cjs.map +1 -1
  23. package/lib/index.d.cts +61 -3
  24. package/lib/index.d.ts +61 -3
  25. package/lib/index.js +12 -14
  26. package/lib/index.js.map +1 -1
  27. package/lib/plugins/index.cjs.map +1 -1
  28. package/lib/plugins/index.d.cts +68 -23
  29. package/lib/plugins/index.d.ts +68 -23
  30. package/lib/plugins/index.js.map +1 -1
  31. package/lib/selectors/index.d.cts +69 -27
  32. package/lib/selectors/index.d.ts +69 -27
  33. package/lib/utils/index.d.cts +74 -29
  34. package/lib/utils/index.d.ts +74 -29
  35. package/package.json +6 -6
  36. package/src/behavior-actions/behavior.action.insert.block.ts +1 -1
  37. package/src/behavior-actions/behavior.action.split.block.ts +1 -1
  38. package/src/behavior-actions/behavior.guards.ts +1 -1
  39. package/src/behaviors/behavior.core.decorators.ts +4 -4
  40. package/src/behaviors/behavior.core.insert-break.ts +4 -4
  41. package/src/behaviors/behavior.decorator-pair.ts +1 -1
  42. package/src/behaviors/behavior.links.ts +1 -1
  43. package/src/behaviors/behavior.markdown.ts +1 -1
  44. package/src/behaviors/behavior.perform-event.ts +1 -1
  45. package/src/converters/converter.portable-text.deserialize.test.ts +2 -3
  46. package/src/converters/converter.text-html.deserialize.test.ts +62 -5
  47. package/src/converters/converter.text-html.serialize.test.ts +9 -5
  48. package/src/converters/converter.text-html.ts +66 -64
  49. package/src/converters/converter.text-plain.test.ts +9 -4
  50. package/src/converters/converter.text-plain.ts +91 -87
  51. package/src/converters/converters.core.ts +13 -8
  52. package/src/editor/Editable.tsx +18 -13
  53. package/src/editor/PortableTextEditor.tsx +5 -5
  54. package/src/editor/__tests__/PortableTextEditorTester.tsx +4 -3
  55. package/src/editor/__tests__/insert-block.test.tsx +10 -10
  56. package/src/editor/__tests__/self-solving.test.tsx +2 -2
  57. package/src/editor/components/Synchronizer.tsx +1 -1
  58. package/src/editor/create-editor.ts +51 -18
  59. package/src/editor/editor-machine.ts +1 -1
  60. package/src/editor/{define-schema.ts → editor-schema.ts} +114 -5
  61. package/src/editor/editor-snapshot.ts +1 -1
  62. package/src/editor/get-active-decorators.ts +2 -2
  63. package/src/editor/{create-editor-schema.ts → legacy-schema.ts} +3 -3
  64. package/src/editor/mutation-machine.ts +1 -1
  65. package/src/editor/plugins/createWithObjectKeys.ts +6 -9
  66. package/src/editor/plugins/createWithPatches.ts +12 -11
  67. package/src/editor/plugins/createWithPortableTextBlockStyle.ts +2 -6
  68. package/src/editor/plugins/createWithPortableTextMarkModel.ts +15 -12
  69. package/src/editor/plugins/createWithSchemaTypes.ts +24 -16
  70. package/src/editor/plugins/createWithUndoRedo.ts +3 -4
  71. package/src/editor/plugins/createWithUtils.ts +12 -10
  72. package/src/editor/plugins/with-plugins.ts +5 -15
  73. package/src/editor/range-decorations-machine.ts +1 -1
  74. package/src/editor/sync-machine.ts +1 -1
  75. package/src/index.ts +2 -2
  76. package/src/internal-utils/__tests__/operationToPatches.test.ts +12 -10
  77. package/src/internal-utils/__tests__/patchToOperations.test.ts +3 -2
  78. package/src/internal-utils/__tests__/values.test.ts +3 -2
  79. package/src/internal-utils/applyPatch.ts +7 -9
  80. package/src/internal-utils/create-test-snapshot.ts +1 -1
  81. package/src/internal-utils/drag-selection.test.ts +1 -1
  82. package/src/internal-utils/operationToPatches.ts +4 -6
  83. package/src/internal-utils/parse-blocks.test.ts +1 -1
  84. package/src/internal-utils/parse-blocks.ts +8 -7
  85. package/src/internal-utils/slate-children-to-blocks.ts +1 -1
  86. package/src/internal-utils/slate-utils.ts +1 -1
  87. package/src/internal-utils/validateValue.ts +4 -6
  88. package/src/internal-utils/values.ts +5 -5
  89. package/src/plugins/plugin.decorator-shortcut.ts +1 -1
  90. package/src/plugins/plugin.markdown.test.tsx +1 -1
  91. package/src/plugins/plugin.markdown.tsx +1 -1
  92. package/src/selectors/selector.get-selection-text.test.ts +1 -1
  93. package/src/selectors/selector.get-trimmed-selection.test.ts +1 -1
@@ -1,6 +1,6 @@
1
1
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
2
2
  import { c } from "react-compiler-runtime";
3
- import React, { useEffect, createContext, useContext, useState, startTransition, Component } from "react";
3
+ import React, { useEffect, useMemo, 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";
@@ -25,9 +25,9 @@ import omit from "lodash/omit.js";
25
25
  import { selectionPointToBlockOffset, blockOffsetsToSelection } from "./util.selection-point-to-block-offset.js";
26
26
  import { getTrimmedSelection, isActiveAnnotation, isActiveDecorator, getSelectedTextBlocks, isActiveListItem, isActiveStyle, isSelectingEntireBlocks, getActiveAnnotations } from "./selector.is-selecting-entire-blocks.js";
27
27
  import { DOMEditor } from "slate-dom";
28
- import startCase from "lodash.startcase";
29
28
  import { defineBehavior, raise, coreBehaviors } from "./behavior.core.js";
30
29
  import { getFocusTextBlock, getPreviousBlock, getNextBlock, getFocusSpan, isSelectionCollapsed, isOverlappingSelection, getSelectedBlocks, isSelectionExpanded } from "./selector.is-overlapping-selection.js";
30
+ import startCase from "lodash.startcase";
31
31
  import { Subject } from "rxjs";
32
32
  import { useEffectEvent } from "use-effect-event";
33
33
  const rootName = "sanity-pte:";
@@ -72,7 +72,7 @@ function toSlateValue(value, {
72
72
  __inline: !0
73
73
  }, keyMap)) : child;
74
74
  });
75
- return !hasMissingStyle && !hasMissingMarkDefs && !hasMissingChildren && !hasInlines && Element.isElement(block) ? block : (hasMissingStyle && (rest.style = schemaTypes.styles[0].value), keepObjectEquality({
75
+ return !hasMissingStyle && !hasMissingMarkDefs && !hasMissingChildren && !hasInlines && Element.isElement(block) ? block : (hasMissingStyle && (rest.style = schemaTypes.styles[0].name), keepObjectEquality({
76
76
  _type,
77
77
  _key,
78
78
  ...rest,
@@ -141,7 +141,7 @@ function fromSlateValue(value, textBlockType, keyMap = {}) {
141
141
  });
142
142
  }
143
143
  function isEqualToEmptyEditor(children, schemaTypes) {
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 === "";
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].name && !("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 === "";
145
145
  }
146
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
147
  types: {
@@ -502,7 +502,7 @@ function validateValue(value, types, keyGenerator) {
502
502
  }
503
503
  }, !0;
504
504
  }
505
- const orphanedMarks = allUsedMarks.filter((mark) => !types.decorators.map((dec) => dec.value).includes(mark)).filter((mark) => textBlock.markDefs === void 0 || !textBlock.markDefs.find((def) => def._key === mark));
505
+ const orphanedMarks = allUsedMarks.filter((mark) => !types.decorators.map((dec) => dec.name).includes(mark)).filter((mark) => textBlock.markDefs === void 0 || !textBlock.markDefs.find((def) => def._key === mark));
506
506
  if (orphanedMarks.length > 0) {
507
507
  const spanChildren = textBlock.children.filter((cld) => cld._type === types.span.name && Array.isArray(cld.marks) && cld.marks.some((mark) => orphanedMarks.includes(mark)));
508
508
  if (spanChildren) {
@@ -1355,11 +1355,10 @@ const debug$f = debugWithName("plugin:withUndoRedo"), SAVING = /* @__PURE__ */ n
1355
1355
  }, getRemotePatches = (editor) => (REMOTE_PATCHES.get(editor) || REMOTE_PATCHES.set(editor, []), REMOTE_PATCHES.get(editor) || []);
1356
1356
  function createWithUndoRedo(options) {
1357
1357
  const {
1358
- editorActor,
1359
- blockSchemaType
1358
+ editorActor
1360
1359
  } = options;
1361
1360
  return (editor) => {
1362
- let previousSnapshot = fromSlateValue(editor.children, blockSchemaType.name);
1361
+ let previousSnapshot = fromSlateValue(editor.children, editorActor.getSnapshot().context.schema.block.name);
1363
1362
  const remotePatches = getRemotePatches(editor);
1364
1363
  let previousBehaviorActionSetId = getCurrentBehaviorActionSetId(editor);
1365
1364
  options.subscriptions.push(() => {
@@ -2085,7 +2084,7 @@ function Synchronizer(props) {
2085
2084
  }, $[6] = slateEditor, $[7] = t0.context.keyGenerator, $[8] = t2, $[9] = t3.context.schema, $[10] = t4) : t4 = $[10];
2086
2085
  const syncActorRef = useActorRef(syncMachine, t4);
2087
2086
  let t5;
2088
- $[11] !== editorActor ? (t5 = editorActor.getSnapshot(), $[11] = editorActor, $[12] = t5) : t5 = $[12];
2087
+ $[11] !== props.editorActor ? (t5 = props.editorActor.getSnapshot(), $[11] = props.editorActor, $[12] = t5) : t5 = $[12];
2089
2088
  let t6;
2090
2089
  $[13] !== slateEditor || $[14] !== t5.context.schema ? (t6 = {
2091
2090
  input: {
@@ -2288,121 +2287,128 @@ const converterJson = {
2288
2287
  mimeType: "application/x-portable-text"
2289
2288
  };
2290
2289
  }
2291
- }, converterTextHtml = {
2292
- mimeType: "text/html",
2293
- serialize: ({
2294
- snapshot,
2295
- event
2296
- }) => {
2297
- const selection = snapshot.beta.internalDrag?.origin.selection ?? snapshot.context.selection;
2298
- if (!selection)
2299
- return {
2290
+ };
2291
+ function createConverterTextHtml(legacySchema) {
2292
+ return {
2293
+ mimeType: "text/html",
2294
+ serialize: ({
2295
+ snapshot,
2296
+ event
2297
+ }) => {
2298
+ const selection = snapshot.beta.internalDrag?.origin.selection ?? snapshot.context.selection;
2299
+ if (!selection)
2300
+ return {
2301
+ type: "serialization.failure",
2302
+ mimeType: "text/html",
2303
+ originEvent: event.originEvent,
2304
+ reason: "No selection"
2305
+ };
2306
+ const blocks = sliceBlocks({
2307
+ blocks: snapshot.context.value,
2308
+ selection
2309
+ }), html = toHTML(blocks, {
2310
+ onMissingComponent: !1,
2311
+ components: {
2312
+ unknownType: ({
2313
+ children
2314
+ }) => children !== void 0 ? `${children}` : ""
2315
+ }
2316
+ });
2317
+ return html === "" ? {
2300
2318
  type: "serialization.failure",
2301
2319
  mimeType: "text/html",
2302
2320
  originEvent: event.originEvent,
2303
- reason: "No selection"
2321
+ reason: "Serialized HTML is empty"
2322
+ } : {
2323
+ type: "serialization.success",
2324
+ data: html,
2325
+ mimeType: "text/html",
2326
+ originEvent: event.originEvent
2304
2327
  };
2305
- const blocks = sliceBlocks({
2306
- blocks: snapshot.context.value,
2307
- selection
2308
- }), html = toHTML(blocks, {
2309
- onMissingComponent: !1,
2310
- components: {
2311
- unknownType: ({
2312
- children
2313
- }) => children !== void 0 ? `${children}` : ""
2314
- }
2315
- });
2316
- return html === "" ? {
2317
- type: "serialization.failure",
2318
- mimeType: "text/html",
2319
- originEvent: event.originEvent,
2320
- reason: "Serialized HTML is empty"
2321
- } : {
2322
- type: "serialization.success",
2323
- data: html,
2324
- mimeType: "text/html",
2325
- originEvent: event.originEvent
2326
- };
2327
- },
2328
- deserialize: ({
2329
- snapshot,
2330
- event
2331
- }) => {
2332
- const parsedBlocks = htmlToBlocks(event.data, snapshot.context.schema.portableText, {
2333
- keyGenerator: snapshot.context.keyGenerator,
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
- }
2328
+ },
2329
+ deserialize: ({
2330
+ snapshot,
2331
+ event
2332
+ }) => {
2333
+ const parsedBlocks = htmlToBlocks(event.data, legacySchema.portableText, {
2334
+ keyGenerator: snapshot.context.keyGenerator,
2335
+ unstable_whitespaceOnPasteMode: legacySchema.block.options.unstable_whitespaceOnPasteMode
2336
+ }).flatMap((block) => {
2337
+ const parsedBlock = parseBlock({
2338
+ context: snapshot.context,
2339
+ block,
2340
+ options: {
2341
+ refreshKeys: !1
2342
+ }
2343
+ });
2344
+ return parsedBlock ? [parsedBlock] : [];
2342
2345
  });
2343
- return parsedBlock ? [parsedBlock] : [];
2344
- });
2345
- return parsedBlocks.length === 0 ? {
2346
- type: "deserialization.failure",
2347
- mimeType: "text/html",
2348
- reason: "No blocks deserialized"
2349
- } : {
2350
- type: "deserialization.success",
2351
- data: parsedBlocks,
2352
- mimeType: "text/html"
2353
- };
2354
- }
2355
- }, converterTextPlain = {
2356
- mimeType: "text/plain",
2357
- serialize: ({
2358
- snapshot,
2359
- event
2360
- }) => {
2361
- const selection = snapshot.beta.internalDrag?.origin.selection ?? snapshot.context.selection;
2362
- return selection ? {
2363
- type: "serialization.success",
2364
- data: sliceBlocks({
2365
- blocks: snapshot.context.value,
2366
- selection
2367
- }).map((block) => isPortableTextTextBlock(block) ? block.children.map((child) => child._type === snapshot.context.schema.span.name ? child.text : snapshot.beta.hasTag("dragging internally") ? `[${snapshot.context.schema.inlineObjects.find((inlineObjectType) => inlineObjectType.name === child._type)?.title ?? "Object"}]` : "").join("") : snapshot.beta.hasTag("dragging internally") ? `[${snapshot.context.schema.blockObjects.find((blockObjectType) => blockObjectType.name === block._type)?.title ?? "Object"}]` : "").filter((block) => block !== "").join(`
2346
+ return parsedBlocks.length === 0 ? {
2347
+ type: "deserialization.failure",
2348
+ mimeType: "text/html",
2349
+ reason: "No blocks deserialized"
2350
+ } : {
2351
+ type: "deserialization.success",
2352
+ data: parsedBlocks,
2353
+ mimeType: "text/html"
2354
+ };
2355
+ }
2356
+ };
2357
+ }
2358
+ function createConverterTextPlain(legacySchema) {
2359
+ return {
2360
+ mimeType: "text/plain",
2361
+ serialize: ({
2362
+ snapshot,
2363
+ event
2364
+ }) => {
2365
+ const selection = snapshot.beta.internalDrag?.origin.selection ?? snapshot.context.selection;
2366
+ return selection ? {
2367
+ type: "serialization.success",
2368
+ data: sliceBlocks({
2369
+ blocks: snapshot.context.value,
2370
+ selection
2371
+ }).map((block) => isPortableTextTextBlock(block) ? block.children.map((child) => child._type === snapshot.context.schema.span.name ? child.text : snapshot.beta.hasTag("dragging internally") ? `[${snapshot.context.schema.inlineObjects.find((inlineObjectType) => inlineObjectType.name === child._type)?.title ?? "Object"}]` : "").join("") : snapshot.beta.hasTag("dragging internally") ? `[${snapshot.context.schema.blockObjects.find((blockObjectType) => blockObjectType.name === block._type)?.title ?? "Object"}]` : "").filter((block) => block !== "").join(`
2368
2372
 
2369
2373
  `),
2370
- mimeType: "text/plain",
2371
- originEvent: event.originEvent
2372
- } : {
2373
- type: "serialization.failure",
2374
- mimeType: "text/plain",
2375
- originEvent: event.originEvent,
2376
- reason: "No selection"
2377
- };
2378
- },
2379
- deserialize: ({
2380
- snapshot,
2381
- event
2382
- }) => {
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, {
2384
- keyGenerator: snapshot.context.keyGenerator
2385
- }).flatMap((block) => {
2386
- const parsedBlock = parseBlock({
2387
- context: snapshot.context,
2388
- block,
2389
- options: {
2390
- refreshKeys: !1
2391
- }
2374
+ mimeType: "text/plain",
2375
+ originEvent: event.originEvent
2376
+ } : {
2377
+ type: "serialization.failure",
2378
+ mimeType: "text/plain",
2379
+ originEvent: event.originEvent,
2380
+ reason: "No selection"
2381
+ };
2382
+ },
2383
+ deserialize: ({
2384
+ snapshot,
2385
+ event
2386
+ }) => {
2387
+ 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, legacySchema.portableText, {
2388
+ keyGenerator: snapshot.context.keyGenerator
2389
+ }).flatMap((block) => {
2390
+ const parsedBlock = parseBlock({
2391
+ context: snapshot.context,
2392
+ block,
2393
+ options: {
2394
+ refreshKeys: !1
2395
+ }
2396
+ });
2397
+ return parsedBlock ? [parsedBlock] : [];
2392
2398
  });
2393
- return parsedBlock ? [parsedBlock] : [];
2394
- });
2395
- return parsedBlocks.length === 0 ? {
2396
- type: "deserialization.failure",
2397
- mimeType: "text/plain",
2398
- reason: "No blocks deserialized"
2399
- } : {
2400
- type: "deserialization.success",
2401
- data: parsedBlocks,
2402
- mimeType: "text/plain"
2403
- };
2404
- }
2405
- }, entityMap = {
2399
+ return parsedBlocks.length === 0 ? {
2400
+ type: "deserialization.failure",
2401
+ mimeType: "text/plain",
2402
+ reason: "No blocks deserialized"
2403
+ } : {
2404
+ type: "deserialization.success",
2405
+ data: parsedBlocks,
2406
+ mimeType: "text/plain"
2407
+ };
2408
+ }
2409
+ };
2410
+ }
2411
+ const entityMap = {
2406
2412
  "&": "&amp;",
2407
2413
  "<": "&lt;",
2408
2414
  ">": "&gt;",
@@ -2415,68 +2421,18 @@ const converterJson = {
2415
2421
  function escapeHtml(str) {
2416
2422
  return String(str).replace(/[&<>"'`=/]/g, (s) => entityMap[s]);
2417
2423
  }
2418
- const coreConverters = [converterJson, converterPortableText, converterTextHtml, converterTextPlain];
2424
+ function createCoreConverters(legacySchema) {
2425
+ return [converterJson, converterPortableText, createConverterTextHtml(legacySchema), createConverterTextPlain(legacySchema)];
2426
+ }
2419
2427
  function compileType(rawType) {
2420
2428
  return Schema.compile({
2421
2429
  name: "blockTypeSchema",
2422
2430
  types: [rawType]
2423
2431
  }).get(rawType.name);
2424
2432
  }
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
2433
  const debug$c = debugWithName("operationToPatches");
2478
- function createOperationToPatches(types) {
2479
- const textBlockName = types.block.name;
2434
+ function createOperationToPatches(editorActor) {
2435
+ const textBlockName = editorActor.getSnapshot().context.schema.block.name;
2480
2436
  function insertTextPatch(editor, operation, beforeValue) {
2481
2437
  debug$c.enabled && debug$c("Operation", JSON.stringify(operation, null, 2));
2482
2438
  const block = editor.isTextBlock(editor.children[operation.path[0]]) && editor.children[operation.path[0]];
@@ -2745,12 +2701,12 @@ function getNextSpan({
2745
2701
  return nextSpan;
2746
2702
  }
2747
2703
  const debug$b = debugWithName("plugin:withPortableTextMarkModel");
2748
- function createWithPortableTextMarkModel(editorActor, types) {
2704
+ function createWithPortableTextMarkModel(editorActor) {
2749
2705
  return function(editor) {
2750
2706
  const {
2751
2707
  apply: apply2,
2752
2708
  normalizeNode
2753
- } = editor, decorators = types.decorators.map((t) => t.value);
2709
+ } = editor, decorators = editorActor.getSnapshot().context.schema.decorators.map((t) => t.name);
2754
2710
  return editor.normalizeNode = (nodeEntry) => {
2755
2711
  const [node, path] = nodeEntry;
2756
2712
  if (editor.isTextBlock(node)) {
@@ -2795,7 +2751,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
2795
2751
  return;
2796
2752
  }
2797
2753
  if (editor.isTextSpan(node)) {
2798
- const blockPath = Path.parent(path), [block] = Editor.node(editor, blockPath), decorators2 = types.decorators.map((decorator) => decorator.value), annotations = node.marks?.filter((mark) => !decorators2.includes(mark));
2754
+ const blockPath = Path.parent(path), [block] = Editor.node(editor, blockPath), decorators2 = editorActor.getSnapshot().context.schema.decorators.map((decorator) => decorator.name), annotations = node.marks?.filter((mark) => !decorators2.includes(mark));
2799
2755
  if (editor.isTextBlock(block) && node.text === "" && annotations && annotations.length > 0) {
2800
2756
  debug$b("Removing annotations from empty span node"), editorActor.send({
2801
2757
  type: "normalizing"
@@ -2810,7 +2766,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
2810
2766
  }
2811
2767
  }
2812
2768
  if (editor.isTextBlock(node)) {
2813
- const decorators2 = types.decorators.map((decorator) => decorator.value);
2769
+ const decorators2 = editorActor.getSnapshot().context.schema.decorators.map((decorator) => decorator.name);
2814
2770
  for (const [child, childPath] of Node.children(editor, path))
2815
2771
  if (editor.isTextSpan(child)) {
2816
2772
  const marks = child.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !node.markDefs?.find((def) => def._key === mark));
@@ -2831,7 +2787,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
2831
2787
  if (editor.isTextSpan(node)) {
2832
2788
  const blockPath = Path.parent(path), [block] = Editor.node(editor, blockPath);
2833
2789
  if (editor.isTextBlock(block)) {
2834
- const decorators2 = types.decorators.map((decorator) => decorator.value), marks = node.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !block.markDefs?.find((def) => def._key === mark));
2790
+ const decorators2 = editorActor.getSnapshot().context.schema.decorators.map((decorator) => decorator.name), marks = node.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !block.markDefs?.find((def) => def._key === mark));
2835
2791
  if (orphanedAnnotations.length > 0) {
2836
2792
  debug$b("Removing orphaned annotations from span node"), editorActor.send({
2837
2793
  type: "normalizing"
@@ -3082,7 +3038,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
3082
3038
  }
3083
3039
  }
3084
3040
  }
3085
- if (op.type === "merge_node" && op.path.length === 1 && "markDefs" in op.properties && op.properties._type === types.block.name && Array.isArray(op.properties.markDefs) && op.properties.markDefs.length > 0 && op.path[0] - 1 >= 0) {
3041
+ if (op.type === "merge_node" && op.path.length === 1 && "markDefs" in op.properties && op.properties._type === editorActor.getSnapshot().context.schema.block.name && Array.isArray(op.properties.markDefs) && op.properties.markDefs.length > 0 && op.path[0] - 1 >= 0) {
3086
3042
  const [targetBlock, targetPath] = Editor.node(editor, [op.path[0] - 1]);
3087
3043
  if (editor.isTextBlock(targetBlock)) {
3088
3044
  const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [], newMarkDefs = uniq([...oldDefs, ...op.properties.markDefs]);
@@ -4079,7 +4035,7 @@ const moveBackwardActionImplementation = ({
4079
4035
  continue;
4080
4036
  const marks = child.marks ?? [];
4081
4037
  for (const mark of marks)
4082
- schema.decorators.some((decorator) => decorator.value === mark) || prevNodeSpans.some((prevNodeSpan) => prevNodeSpan.marks?.includes(mark)) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(mark, keyGenerator());
4038
+ schema.decorators.some((decorator) => decorator.name === mark) || prevNodeSpans.some((prevNodeSpan) => prevNodeSpan.marks?.includes(mark)) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(mark, keyGenerator());
4083
4039
  const newMarks = marks.map((mark) => newMarkDefKeys.get(mark) ?? mark);
4084
4040
  isEqual(marks, newMarks) || Transforms.setNodes(editor, {
4085
4041
  marks: newMarks
@@ -4500,7 +4456,7 @@ function createWithMaxBlocks(editorActor) {
4500
4456
  }, editor;
4501
4457
  };
4502
4458
  }
4503
- function createWithObjectKeys(editorActor, schemaTypes) {
4459
+ function createWithObjectKeys(editorActor) {
4504
4460
  return function(editor) {
4505
4461
  const {
4506
4462
  apply: apply2,
@@ -4540,7 +4496,7 @@ function createWithObjectKeys(editorActor, schemaTypes) {
4540
4496
  apply2(operation);
4541
4497
  }, editor.normalizeNode = (entry) => {
4542
4498
  const [node, path] = entry;
4543
- if (Element.isElement(node) && node._type === schemaTypes.block.name) {
4499
+ if (Element.isElement(node) && node._type === editorActor.getSnapshot().context.schema.block.name) {
4544
4500
  if (!node._key) {
4545
4501
  editorActor.send({
4546
4502
  type: "normalizing"
@@ -4572,7 +4528,7 @@ function createWithObjectKeys(editorActor, schemaTypes) {
4572
4528
  };
4573
4529
  }
4574
4530
  const debug$9 = debugWithName("applyPatches"), debugVerbose = debug$9.enabled && !0;
4575
- function createApplyPatch(schemaTypes) {
4531
+ function createApplyPatch(schema) {
4576
4532
  return (editor, patch) => {
4577
4533
  let changed = !1;
4578
4534
  debugVerbose && (debug$9(`
@@ -4581,7 +4537,7 @@ NEW PATCH =============================================================`), debug
4581
4537
  try {
4582
4538
  switch (patch.type) {
4583
4539
  case "insert":
4584
- changed = insertPatch(editor, patch, schemaTypes);
4540
+ changed = insertPatch(editor, patch, schema);
4585
4541
  break;
4586
4542
  case "unset":
4587
4543
  changed = unsetPatch(editor, patch);
@@ -4632,7 +4588,7 @@ function diffMatchPatch(editor, patch) {
4632
4588
  }) : op === DIFF_EQUAL && (offset += text.length);
4633
4589
  return debugState(editor, "after"), !0;
4634
4590
  }
4635
- function insertPatch(editor, patch, schemaTypes) {
4591
+ function insertPatch(editor, patch, schema) {
4636
4592
  const {
4637
4593
  block: targetBlock,
4638
4594
  child: targetChild,
@@ -4648,7 +4604,7 @@ function insertPatch(editor, patch, schemaTypes) {
4648
4604
  items: items2,
4649
4605
  position: position2
4650
4606
  } = patch, blocksToInsert = toSlateValue(items2, {
4651
- schemaTypes
4607
+ schemaTypes: schema
4652
4608
  }, KEY_TO_SLATE_ELEMENT.get(editor)), targetBlockIndex = targetBlockPath[0], normalizedIdx2 = position2 === "after" ? targetBlockIndex + 1 : targetBlockIndex;
4653
4609
  return debug$9(`Inserting blocks at path [${normalizedIdx2}]`), debugState(editor, "before"), Transforms.insertNodes(editor, blocksToInsert, {
4654
4610
  at: [normalizedIdx2]
@@ -4664,7 +4620,7 @@ function insertPatch(editor, patch, schemaTypes) {
4664
4620
  ...targetBlock,
4665
4621
  children: items
4666
4622
  }], {
4667
- schemaTypes
4623
+ schemaTypes: schema
4668
4624
  }, KEY_TO_SLATE_ELEMENT.get(editor)), targetChildIndex = targetChildPath[1], normalizedIdx = position === "after" ? targetChildIndex + 1 : targetChildIndex, childInsertPath = [targetChildPath[0], normalizedIdx];
4669
4625
  return debug$9(`Inserting children at path ${childInsertPath}`), debugState(editor, "before"), childrenToInsert && Element.isElement(childrenToInsert[0]) && Transforms.insertNodes(editor, childrenToInsert[0].children, {
4670
4626
  at: childInsertPath
@@ -4842,11 +4798,10 @@ const debug$8 = debugWithName("plugin:withPatches");
4842
4798
  function createWithPatches({
4843
4799
  editorActor,
4844
4800
  patchFunctions,
4845
- schemaTypes,
4846
4801
  subscriptions
4847
4802
  }) {
4848
4803
  let previousChildren;
4849
- const applyPatch = createApplyPatch(schemaTypes);
4804
+ const applyPatch = createApplyPatch(editorActor.getSnapshot().context.schema);
4850
4805
  return function(editor) {
4851
4806
  IS_PROCESSING_REMOTE_CHANGES.set(editor, !1), PATCHING.set(editor, !0), previousChildren = [...editor.children];
4852
4807
  const {
@@ -4885,9 +4840,9 @@ function createWithPatches({
4885
4840
  }), editor.apply = (operation) => {
4886
4841
  let patches = [];
4887
4842
  previousChildren = editor.children;
4888
- const editorWasEmpty = isEqualToEmptyEditor(previousChildren, schemaTypes);
4843
+ const editorWasEmpty = isEqualToEmptyEditor(previousChildren, editorActor.getSnapshot().context.schema);
4889
4844
  apply2(operation);
4890
- const editorIsEmpty = isEqualToEmptyEditor(editor.children, schemaTypes);
4845
+ const editorIsEmpty = isEqualToEmptyEditor(editor.children, editorActor.getSnapshot().context.schema);
4891
4846
  if (!isPatching(editor))
4892
4847
  return editor;
4893
4848
  switch (editorWasEmpty && !editorIsEmpty && operation.type !== "set_selection" && patches.push(insert(previousChildren, "before", [0])), operation.type) {
@@ -4918,7 +4873,7 @@ function createWithPatches({
4918
4873
  }
4919
4874
  if (!editorWasEmpty && editorIsEmpty && ["merge_node", "set_node", "remove_text", "remove_node"].includes(operation.type) && (patches = [...patches, unset([])], editorActor.send({
4920
4875
  type: "notify.unset",
4921
- previousValue: fromSlateValue(previousChildren, schemaTypes.block.name, KEY_TO_VALUE_ELEMENT.get(editor))
4876
+ previousValue: fromSlateValue(previousChildren, editorActor.getSnapshot().context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor))
4922
4877
  })), editorWasEmpty && patches.length > 0 && (patches = [setIfMissing([], []), ...patches]), patches.length > 0)
4923
4878
  for (const patch of patches)
4924
4879
  editorActor.send({
@@ -4928,7 +4883,7 @@ function createWithPatches({
4928
4883
  origin: "local"
4929
4884
  },
4930
4885
  actionId: getCurrentActionId(editor),
4931
- value: fromSlateValue(editor.children, schemaTypes.block.name, KEY_TO_VALUE_ELEMENT.get(editor))
4886
+ value: fromSlateValue(editor.children, editorActor.getSnapshot().context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor))
4932
4887
  });
4933
4888
  return editor;
4934
4889
  }, editor;
@@ -4969,8 +4924,8 @@ function createWithPlaceholderBlock(editorActor) {
4969
4924
  };
4970
4925
  }
4971
4926
  const debug$6 = debugWithName("plugin:withPortableTextBlockStyle");
4972
- function createWithPortableTextBlockStyle(editorActor, types) {
4973
- const defaultStyle = types.styles[0].value;
4927
+ function createWithPortableTextBlockStyle(editorActor) {
4928
+ const defaultStyle = editorActor.getSnapshot().context.schema.styles[0].name;
4974
4929
  return function(editor) {
4975
4930
  const {
4976
4931
  normalizeNode
@@ -5034,11 +4989,10 @@ function createWithPortableTextSelections(editorActor) {
5034
4989
  }
5035
4990
  const debug$5 = debugWithName("plugin:withSchemaTypes");
5036
4991
  function createWithSchemaTypes({
5037
- editorActor,
5038
- schemaTypes
4992
+ editorActor
5039
4993
  }) {
5040
4994
  return function(editor) {
5041
- editor.isTextBlock = (value) => isPortableTextTextBlock(value) && value._type === schemaTypes.block.name, editor.isTextSpan = (value) => isPortableTextSpan$1(value) && value._type === schemaTypes.span.name, editor.isListBlock = (value) => isPortableTextListBlock(value) && value._type === schemaTypes.block.name, editor.isVoid = (element) => schemaTypes.block.name !== element._type && (schemaTypes.blockObjects.map((obj) => obj.name).includes(element._type) || schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type)), editor.isInline = (element) => schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type) && "__inline" in element && element.__inline === !0;
4995
+ editor.isTextBlock = (value) => isPortableTextTextBlock(value) && value._type === editorActor.getSnapshot().context.schema.block.name, editor.isTextSpan = (value) => isPortableTextSpan$1(value) && value._type === editorActor.getSnapshot().context.schema.span.name, editor.isListBlock = (value) => isPortableTextListBlock(value) && value._type === editorActor.getSnapshot().context.schema.block.name, editor.isVoid = (element) => editorActor.getSnapshot().context.schema.block.name !== element._type && (editorActor.getSnapshot().context.schema.blockObjects.map((obj) => obj.name).includes(element._type) || editorActor.getSnapshot().context.schema.inlineObjects.map((obj) => obj.name).includes(element._type)), editor.isInline = (element) => editorActor.getSnapshot().context.schema.inlineObjects.map((obj) => obj.name).includes(element._type) && "__inline" in element && element.__inline === !0;
5042
4996
  const {
5043
4997
  normalizeNode
5044
4998
  } = editor;
@@ -5051,7 +5005,7 @@ function createWithSchemaTypes({
5051
5005
  type: "normalizing"
5052
5006
  }), Transforms.setNodes(editor, {
5053
5007
  ...span,
5054
- _type: schemaTypes.span.name,
5008
+ _type: editorActor.getSnapshot().context.schema.span.name,
5055
5009
  _key: key
5056
5010
  }, {
5057
5011
  at: path
@@ -5079,14 +5033,13 @@ function createWithSchemaTypes({
5079
5033
  };
5080
5034
  }
5081
5035
  function createWithUtils({
5082
- editorActor,
5083
- schemaTypes
5036
+ editorActor
5084
5037
  }) {
5085
5038
  return function(editor) {
5086
5039
  return editor.pteCreateTextBlock = (options) => toSlateValue([{
5087
- _type: schemaTypes.block.name,
5040
+ _type: editorActor.getSnapshot().context.schema.block.name,
5088
5041
  _key: editorActor.getSnapshot().context.keyGenerator(),
5089
- style: schemaTypes.styles[0].value || "normal",
5042
+ style: editorActor.getSnapshot().context.schema.styles[0].name || "normal",
5090
5043
  ...options.listItem ? {
5091
5044
  listItem: options.listItem
5092
5045
  } : {},
@@ -5098,33 +5051,29 @@ function createWithUtils({
5098
5051
  _type: "span",
5099
5052
  _key: editorActor.getSnapshot().context.keyGenerator(),
5100
5053
  text: "",
5101
- marks: options.decorators.filter((decorator) => schemaTypes.decorators.find(({
5102
- value
5103
- }) => value === decorator))
5054
+ marks: options.decorators.filter((decorator) => editorActor.getSnapshot().context.schema.decorators.find(({
5055
+ name
5056
+ }) => name === decorator))
5104
5057
  }]
5105
5058
  }], {
5106
- schemaTypes
5059
+ schemaTypes: editorActor.getSnapshot().context.schema
5107
5060
  })[0], editor;
5108
5061
  };
5109
5062
  }
5110
5063
  const withPlugins = (editor, options) => {
5111
5064
  const e = editor, {
5112
5065
  editorActor
5113
- } = options, schemaTypes = editorActor.getSnapshot().context.schema, operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(editorActor, schemaTypes), withSchemaTypes = createWithSchemaTypes({
5114
- editorActor,
5115
- schemaTypes
5066
+ } = options, operationToPatches = createOperationToPatches(editorActor), withObjectKeys = createWithObjectKeys(editorActor), withSchemaTypes = createWithSchemaTypes({
5067
+ editorActor
5116
5068
  }), withPatches = createWithPatches({
5117
5069
  editorActor,
5118
5070
  patchFunctions: operationToPatches,
5119
- schemaTypes,
5120
5071
  subscriptions: options.subscriptions
5121
5072
  }), withMaxBlocks = createWithMaxBlocks(editorActor), withUndoRedo = createWithUndoRedo({
5122
5073
  editorActor,
5123
- blockSchemaType: schemaTypes.block,
5124
5074
  subscriptions: options.subscriptions
5125
- }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor, schemaTypes), withPortableTextBlockStyle = createWithPortableTextBlockStyle(editorActor, schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
5126
- editorActor,
5127
- schemaTypes
5075
+ }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor), withPortableTextBlockStyle = createWithPortableTextBlockStyle(editorActor), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
5076
+ editorActor
5128
5077
  }), withPortableTextSelections = createWithPortableTextSelections(editorActor);
5129
5078
  return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e)))))))))));
5130
5079
  }, debug$4 = debugWithName("component:PortableTextEditor:SlateContainer"), slateEditors = /* @__PURE__ */ new WeakMap();
@@ -5148,100 +5097,6 @@ function createSlateEditor(config) {
5148
5097
  };
5149
5098
  return slateEditors.set(config.editorActor, slateEditor), slateEditor;
5150
5099
  }
5151
- function defineSchema(definition) {
5152
- return definition;
5153
- }
5154
- const temporaryImageName = `tmp-${defaultKeyGenerator()}-image`, temporaryUrlName = `tmp-${defaultKeyGenerator()}-url`, temporaryObjectNames = {
5155
- image: temporaryImageName,
5156
- url: temporaryUrlName
5157
- }, objectNames = {
5158
- [temporaryImageName]: "image",
5159
- [temporaryUrlName]: "url"
5160
- }, defaultObjectTitles = {
5161
- image: "Image",
5162
- url: "URL"
5163
- };
5164
- function compileSchemaDefinition(definition) {
5165
- const blockObjects = definition?.blockObjects?.map((blockObject) => defineType({
5166
- type: "object",
5167
- // Very naive way to work around `SanitySchema.compile` adding default
5168
- // fields to objects with certain names.
5169
- name: temporaryObjectNames[blockObject.name] ?? blockObject.name,
5170
- title: blockObject.title === void 0 ? (
5171
- // This avoids the default title which is a title case of the object name
5172
- defaultObjectTitles[blockObject.name]
5173
- ) : blockObject.title,
5174
- fields: blockObject.fields?.map((field) => ({
5175
- name: field.name,
5176
- type: field.type
5177
- })) ?? []
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: inlineObject.fields?.map((field) => ({
5188
- name: field.name,
5189
- type: field.type
5190
- })) ?? []
5191
- })) ?? [], portableTextSchema = defineField({
5192
- type: "array",
5193
- name: "portable-text",
5194
- of: [...blockObjects.map((blockObject) => ({
5195
- type: blockObject.name
5196
- })), {
5197
- type: "block",
5198
- name: "block",
5199
- of: inlineObjects.map((inlineObject) => ({
5200
- type: inlineObject.name
5201
- })),
5202
- marks: {
5203
- decorators: definition?.decorators?.map((decorator) => ({
5204
- title: decorator.title ?? startCase(decorator.name),
5205
- value: decorator.name
5206
- })) ?? [],
5207
- annotations: definition?.annotations?.map((annotation) => ({
5208
- name: annotation.name,
5209
- type: "object",
5210
- title: annotation.title,
5211
- fields: annotation.fields?.map((field) => ({
5212
- name: field.name,
5213
- type: field.type
5214
- })) ?? []
5215
- })) ?? []
5216
- },
5217
- lists: definition?.lists?.map((list) => ({
5218
- value: list.name,
5219
- title: list.title ?? startCase(list.name)
5220
- })) ?? [],
5221
- styles: definition?.styles?.map((style) => ({
5222
- value: style.name,
5223
- title: style.title ?? startCase(style.name)
5224
- })) ?? []
5225
- }]
5226
- }), schema = Schema.compile({
5227
- types: [portableTextSchema, ...blockObjects, ...inlineObjects]
5228
- }).get("portable-text"), pteSchema = createEditorSchema(schema);
5229
- return {
5230
- ...pteSchema,
5231
- blockObjects: pteSchema.blockObjects.map((blockObject) => objectNames[blockObject.name] !== void 0 ? {
5232
- ...blockObject,
5233
- name: objectNames[blockObject.name],
5234
- type: {
5235
- ...blockObject.type,
5236
- name: objectNames[blockObject.name]
5237
- }
5238
- } : blockObject),
5239
- inlineObjects: pteSchema.inlineObjects.map((inlineObject) => objectNames[inlineObject.name] !== void 0 ? {
5240
- ...inlineObject,
5241
- name: objectNames[inlineObject.name]
5242
- } : inlineObject)
5243
- };
5244
- }
5245
5100
  const abstractAnnotationBehaviors = [defineBehavior({
5246
5101
  on: "annotation.toggle",
5247
5102
  guard: ({
@@ -6218,7 +6073,7 @@ function getActiveDecorators({
6218
6073
  schema,
6219
6074
  slateEditorInstance
6220
6075
  }) {
6221
- const decorators = schema.decorators.map((decorator) => decorator.value);
6076
+ const decorators = schema.decorators.map((decorator) => decorator.name);
6222
6077
  return ({
6223
6078
  ...Editor.marks(slateEditorInstance) ?? {}
6224
6079
  }.marks ?? []).filter((mark) => decorators.includes(mark));
@@ -6643,6 +6498,201 @@ const editorMachine = setup({
6643
6498
  }
6644
6499
  }
6645
6500
  });
6501
+ function createLegacySchema(portableTextType) {
6502
+ if (!portableTextType)
6503
+ throw new Error("Parameter 'portabletextType' missing (required)");
6504
+ const blockType = portableTextType.of?.find(findBlockType);
6505
+ if (!blockType)
6506
+ throw new Error("Block type is not defined in this schema (required)");
6507
+ const childrenField = blockType.fields?.find((field) => field.name === "children");
6508
+ if (!childrenField)
6509
+ throw new Error("Children field for block type found in schema (required)");
6510
+ const ofType = childrenField.type.of;
6511
+ if (!ofType)
6512
+ throw new Error("Valid types for block children not found in schema (required)");
6513
+ const spanType = ofType.find((memberType) => memberType.name === "span");
6514
+ if (!spanType)
6515
+ throw new Error("Span type not found in schema (required)");
6516
+ const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes = portableTextType.of?.filter((field) => field.name !== blockType.name) || [];
6517
+ return {
6518
+ styles: resolveEnabledStyles(blockType),
6519
+ decorators: resolveEnabledDecorators(spanType),
6520
+ lists: resolveEnabledListItems(blockType),
6521
+ block: blockType,
6522
+ span: spanType,
6523
+ portableText: portableTextType,
6524
+ inlineObjects: inlineObjectTypes,
6525
+ blockObjects: blockObjectTypes,
6526
+ annotations: spanType.annotations
6527
+ };
6528
+ }
6529
+ function resolveEnabledStyles(blockType) {
6530
+ const styleField = blockType.fields?.find((btField) => btField.name === "style");
6531
+ if (!styleField)
6532
+ throw new Error("A field with name 'style' is not defined in the block type (required).");
6533
+ const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter((style) => style.value);
6534
+ if (!textStyles || textStyles.length === 0)
6535
+ throw new Error("The style fields need at least one style defined. I.e: {title: 'Normal', value: 'normal'}.");
6536
+ return textStyles;
6537
+ }
6538
+ function resolveEnabledDecorators(spanType) {
6539
+ return spanType.decorators;
6540
+ }
6541
+ function resolveEnabledListItems(blockType) {
6542
+ const listField = blockType.fields?.find((btField) => btField.name === "listItem");
6543
+ if (!listField)
6544
+ throw new Error("A field with name 'listItem' is not defined in the block type (required).");
6545
+ const listItems = listField.type.options?.list && listField.type.options.list.filter((list) => list.value);
6546
+ if (!listItems)
6547
+ throw new Error("The list field need at least to be an empty array");
6548
+ return listItems;
6549
+ }
6550
+ function findBlockType(type) {
6551
+ return type.type ? findBlockType(type.type) : type.name === "block" ? type : null;
6552
+ }
6553
+ function defineSchema(definition) {
6554
+ return definition;
6555
+ }
6556
+ const temporaryImageName = `tmp-${defaultKeyGenerator()}-image`, temporaryUrlName = `tmp-${defaultKeyGenerator()}-url`, temporaryObjectNames = {
6557
+ image: temporaryImageName,
6558
+ url: temporaryUrlName
6559
+ }, objectNames = {
6560
+ [temporaryImageName]: "image",
6561
+ [temporaryUrlName]: "url"
6562
+ }, defaultObjectTitles = {
6563
+ image: "Image",
6564
+ url: "URL"
6565
+ };
6566
+ function legacySchemaToEditorSchema(schema) {
6567
+ return {
6568
+ annotations: schema.annotations.map((annotation) => ({
6569
+ name: annotation.name,
6570
+ fields: annotation.fields.map((field) => ({
6571
+ name: field.name,
6572
+ type: field.type.jsonType
6573
+ })),
6574
+ title: annotation.title
6575
+ })),
6576
+ block: {
6577
+ name: schema.block.name
6578
+ },
6579
+ blockObjects: schema.blockObjects.map((blockObject) => ({
6580
+ name: blockObject.name,
6581
+ fields: blockObject.fields.map((field) => ({
6582
+ name: field.name,
6583
+ type: field.type.jsonType
6584
+ })),
6585
+ title: blockObject.title
6586
+ })),
6587
+ decorators: schema.decorators.map((decorator) => ({
6588
+ name: decorator.value,
6589
+ title: decorator.title,
6590
+ value: decorator.value
6591
+ })),
6592
+ inlineObjects: schema.inlineObjects.map((inlineObject) => ({
6593
+ name: inlineObject.name,
6594
+ fields: inlineObject.fields.map((field) => ({
6595
+ name: field.name,
6596
+ type: field.type.jsonType
6597
+ })),
6598
+ title: inlineObject.title
6599
+ })),
6600
+ span: {
6601
+ name: schema.span.name
6602
+ },
6603
+ styles: schema.styles.map((style) => ({
6604
+ name: style.value,
6605
+ title: style.title,
6606
+ value: style.value
6607
+ })),
6608
+ lists: schema.lists.map((list) => ({
6609
+ name: list.value,
6610
+ title: list.title,
6611
+ value: list.value
6612
+ }))
6613
+ };
6614
+ }
6615
+ function compileSchemaDefinitionToLegacySchema(definition) {
6616
+ const blockObjects = definition?.blockObjects?.map((blockObject) => defineType({
6617
+ type: "object",
6618
+ // Very naive way to work around `SanitySchema.compile` adding default
6619
+ // fields to objects with certain names.
6620
+ name: temporaryObjectNames[blockObject.name] ?? blockObject.name,
6621
+ title: blockObject.title === void 0 ? (
6622
+ // This avoids the default title which is a title case of the object name
6623
+ defaultObjectTitles[blockObject.name]
6624
+ ) : blockObject.title,
6625
+ fields: blockObject.fields?.map((field) => ({
6626
+ name: field.name,
6627
+ type: field.type
6628
+ })) ?? []
6629
+ })) ?? [], inlineObjects = definition?.inlineObjects?.map((inlineObject) => defineType({
6630
+ type: "object",
6631
+ // Very naive way to work around `SanitySchema.compile` adding default
6632
+ // fields to objects with certain names.
6633
+ name: temporaryObjectNames[inlineObject.name] ?? inlineObject.name,
6634
+ title: inlineObject.title === void 0 ? (
6635
+ // This avoids the default title which is a title case of the object name
6636
+ defaultObjectTitles[inlineObject.name]
6637
+ ) : inlineObject.title,
6638
+ fields: inlineObject.fields?.map((field) => ({
6639
+ name: field.name,
6640
+ type: field.type
6641
+ })) ?? []
6642
+ })) ?? [], portableTextSchema = defineField({
6643
+ type: "array",
6644
+ name: "portable-text",
6645
+ of: [...blockObjects.map((blockObject) => ({
6646
+ type: blockObject.name
6647
+ })), {
6648
+ type: "block",
6649
+ name: "block",
6650
+ of: inlineObjects.map((inlineObject) => ({
6651
+ type: inlineObject.name
6652
+ })),
6653
+ marks: {
6654
+ decorators: definition?.decorators?.map((decorator) => ({
6655
+ title: decorator.title ?? startCase(decorator.name),
6656
+ value: decorator.name
6657
+ })) ?? [],
6658
+ annotations: definition?.annotations?.map((annotation) => ({
6659
+ name: annotation.name,
6660
+ type: "object",
6661
+ title: annotation.title,
6662
+ fields: annotation.fields?.map((field) => ({
6663
+ name: field.name,
6664
+ type: field.type
6665
+ })) ?? []
6666
+ })) ?? []
6667
+ },
6668
+ lists: definition?.lists?.map((list) => ({
6669
+ value: list.name,
6670
+ title: list.title ?? startCase(list.name)
6671
+ })) ?? [],
6672
+ styles: definition?.styles?.map((style) => ({
6673
+ value: style.name,
6674
+ title: style.title ?? startCase(style.name)
6675
+ })) ?? []
6676
+ }]
6677
+ }), schema = Schema.compile({
6678
+ types: [portableTextSchema, ...blockObjects, ...inlineObjects]
6679
+ }).get("portable-text"), pteSchema = createLegacySchema(schema);
6680
+ return {
6681
+ ...pteSchema,
6682
+ blockObjects: pteSchema.blockObjects.map((blockObject) => objectNames[blockObject.name] !== void 0 ? {
6683
+ ...blockObject,
6684
+ name: objectNames[blockObject.name],
6685
+ type: {
6686
+ ...blockObject.type,
6687
+ name: objectNames[blockObject.name]
6688
+ }
6689
+ } : blockObject),
6690
+ inlineObjects: pteSchema.inlineObjects.map((inlineObject) => objectNames[inlineObject.name] !== void 0 ? {
6691
+ ...inlineObject,
6692
+ name: objectNames[inlineObject.name]
6693
+ } : inlineObject)
6694
+ };
6695
+ }
6646
6696
  function defaultCompare(a, b) {
6647
6697
  return a === b;
6648
6698
  }
@@ -7051,35 +7101,37 @@ function isAnnotationActive({
7051
7101
  }
7052
7102
  }
7053
7103
  function createInternalEditor(config) {
7054
- const editorActor = createActor(editorMachine, {
7055
- input: editorConfigToMachineInput(config)
7104
+ const legacySchema = config.schemaDefinition ? compileSchemaDefinitionToLegacySchema(config.schemaDefinition) : createLegacySchema(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)), editorSchema = legacySchemaToEditorSchema(legacySchema), editorActor = createActor(editorMachine, {
7105
+ input: editorConfigToMachineInput({
7106
+ ...config,
7107
+ schema: editorSchema,
7108
+ legacySchema
7109
+ })
7056
7110
  });
7057
- return editorActor.start(), createInternalEditorFromActor(editorActor);
7111
+ return editorActor.start(), createInternalEditorFromActor(editorActor, legacySchema);
7058
7112
  }
7059
7113
  function useCreateInternalEditor(config) {
7060
- const $ = c(6);
7061
- let t0;
7062
- $[0] !== config ? (t0 = editorConfigToMachineInput(config), $[0] = config, $[1] = t0) : t0 = $[1];
7063
- let t1;
7064
- $[2] !== t0 ? (t1 = {
7065
- input: t0
7066
- }, $[2] = t0, $[3] = t1) : t1 = $[3];
7067
- const editorActor = useActorRef(editorMachine, t1);
7068
- let t2, t3;
7069
- return $[4] !== editorActor ? (t3 = createInternalEditorFromActor(editorActor), $[4] = editorActor, $[5] = t3) : t3 = $[5], t2 = t3, t2;
7114
+ const legacySchema = config.schemaDefinition ? compileSchemaDefinitionToLegacySchema(config.schemaDefinition) : createLegacySchema(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)), editorSchema = legacySchemaToEditorSchema(legacySchema), editorActor = useActorRef(editorMachine, {
7115
+ input: editorConfigToMachineInput({
7116
+ ...config,
7117
+ schema: editorSchema,
7118
+ legacySchema
7119
+ })
7120
+ });
7121
+ return useMemo(() => createInternalEditorFromActor(editorActor, legacySchema), [editorActor, legacySchema]);
7070
7122
  }
7071
7123
  function editorConfigToMachineInput(config) {
7072
7124
  return {
7073
7125
  behaviors: config.behaviors,
7074
- converters: coreConverters,
7126
+ converters: createCoreConverters(config.legacySchema),
7075
7127
  keyGenerator: config.keyGenerator ?? defaultKeyGenerator,
7076
7128
  maxBlocks: config.maxBlocks,
7077
7129
  readOnly: config.readOnly,
7078
- schema: config.schemaDefinition ? compileSchemaDefinition(config.schemaDefinition) : createEditorSchema(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)),
7130
+ schema: config.schema,
7079
7131
  initialValue: config.initialValue
7080
7132
  };
7081
7133
  }
7082
- function createInternalEditorFromActor(editorActor) {
7134
+ function createInternalEditorFromActor(editorActor, legacySchema) {
7083
7135
  const slateEditor = createSlateEditor({
7084
7136
  editorActor
7085
7137
  }), editable = createEditableAPI(slateEditor.instance, editorActor);
@@ -7154,6 +7206,7 @@ function createInternalEditorFromActor(editorActor) {
7154
7206
  _internal: {
7155
7207
  editable,
7156
7208
  editorActor,
7209
+ legacySchema,
7157
7210
  slateEditor
7158
7211
  }
7159
7212
  };
@@ -7209,12 +7262,12 @@ class PortableTextEditor extends Component {
7209
7262
  initialValue: props.value,
7210
7263
  maxBlocks: props.maxBlocks === void 0 ? void 0 : Number.parseInt(props.maxBlocks.toString(), 10),
7211
7264
  readOnly: props.readOnly
7212
- }), this.schemaTypes = this.editor._internal.editorActor.getSnapshot().context.schema, this.editable = this.editor._internal.editable;
7265
+ }), this.schemaTypes = this.editor._internal.legacySchema, this.editable = this.editor._internal.editable;
7213
7266
  }
7214
7267
  componentDidUpdate(prevProps) {
7215
- !this.props.editor && !prevProps.editor && this.props.schemaType !== prevProps.schemaType && (this.schemaTypes = createEditorSchema(this.props.schemaType.hasOwnProperty("jsonType") ? this.props.schemaType : compileType(this.props.schemaType)), this.editor._internal.editorActor.send({
7268
+ !this.props.editor && !prevProps.editor && this.props.schemaType !== prevProps.schemaType && (this.schemaTypes = createLegacySchema(this.props.schemaType.hasOwnProperty("jsonType") ? this.props.schemaType : compileType(this.props.schemaType)), this.editor._internal.editorActor.send({
7216
7269
  type: "update schema",
7217
- schema: this.schemaTypes
7270
+ schema: legacySchemaToEditorSchema(this.schemaTypes)
7218
7271
  })), !this.props.editor && !prevProps.editor && (this.props.readOnly !== prevProps.readOnly && this.editor._internal.editorActor.send({
7219
7272
  type: "update readOnly",
7220
7273
  readOnly: this.props.readOnly ?? !1