@portabletext/editor 2.3.3 → 2.3.4

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 (37) hide show
  1. package/lib/_chunks-dts/behavior.types.action.d.cts +5 -142
  2. package/lib/_chunks-dts/behavior.types.action.d.ts +69 -206
  3. package/lib/index.cjs +43 -279
  4. package/lib/index.cjs.map +1 -1
  5. package/lib/index.js +5 -245
  6. package/lib/index.js.map +1 -1
  7. package/lib/utils/index.d.cts +2 -2
  8. package/package.json +7 -5
  9. package/src/converters/converter.portable-text.deserialize.test.ts +6 -4
  10. package/src/converters/converter.text-html.deserialize.test.ts +15 -15
  11. package/src/converters/converter.text-html.serialize.test.ts +8 -8
  12. package/src/converters/converter.text-plain.test.ts +8 -8
  13. package/src/editor/create-editor.ts +12 -10
  14. package/src/editor/editor-schema.ts +2 -312
  15. package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +2 -2
  16. package/src/editor.ts +1 -1
  17. package/src/index.ts +3 -3
  18. package/src/internal-utils/__tests__/values.test.ts +2 -3
  19. package/src/internal-utils/apply-operation-to-portable-text.test.ts +2 -3
  20. package/src/internal-utils/build-index-maps.test.ts +2 -3
  21. package/src/internal-utils/create-test-snapshot.ts +2 -4
  22. package/src/internal-utils/drag-selection.test.ts +2 -5
  23. package/src/internal-utils/operation-to-patches.test.ts +5 -7
  24. package/src/internal-utils/parse-blocks.test.ts +21 -30
  25. package/src/internal-utils/selection-text.ts +2 -3
  26. package/src/internal-utils/terse-pt.test.ts +2 -3
  27. package/src/internal-utils/test-editor.tsx +1 -4
  28. package/src/internal-utils/to-slate-range.test.ts +2 -3
  29. package/src/plugins/plugin.internal.auto-close-brackets.test.tsx +1 -1
  30. package/src/plugins/plugin.markdown.test.tsx +1 -1
  31. package/src/selectors/selector.get-selection-text.test.ts +2 -3
  32. package/src/selectors/selector.get-trimmed-selection.test.ts +2 -3
  33. package/src/utils/util.block-offset.test.ts +2 -3
  34. package/src/utils/util.slice-blocks.test.ts +2 -3
  35. package/src/utils/util.slice-text-block.test.ts +2 -3
  36. package/src/editor/editor-schema-definition.ts +0 -105
  37. package/src/editor/legacy-schema.ts +0 -115
package/lib/index.js CHANGED
@@ -17,6 +17,9 @@ import getRandomValues from "get-random-values-esm";
17
17
  import { defineBehavior, forward, raise, effect } from "./behaviors/index.js";
18
18
  import uniq from "lodash/uniq.js";
19
19
  import { Subject } from "rxjs";
20
+ import { compileSchemaDefinitionToPortableTextMemberSchemaTypes, createPortableTextMemberSchemaTypes, portableTextMemberSchemaTypesToSchema } from "@portabletext/sanity-bridge";
21
+ import { compileSchema } from "@portabletext/schema";
22
+ import { defineSchema } from "@portabletext/schema";
20
23
  import { setup, assign, enqueueActions, emit, assertEvent, stateIn, fromCallback, and, not, raise as raise$1, createActor } from "xstate";
21
24
  import { htmlToBlocks } from "@portabletext/block-tools";
22
25
  import { toHTML } from "@portabletext/to-html";
@@ -31,8 +34,6 @@ import isUndefined from "lodash/isUndefined.js";
31
34
  import omitBy from "lodash/omitBy.js";
32
35
  import { createDraft, finishDraft } from "immer";
33
36
  import { createKeyboardShortcut, code, underline, italic, bold, undo, redo } from "@portabletext/keyboard-shortcuts";
34
- import { defineType, defineField } from "@sanity/types";
35
- import startCase from "lodash.startcase";
36
37
  import isPlainObject from "lodash/isPlainObject.js";
37
38
  function EditorEventListener(props) {
38
39
  const $ = c(4), editor = useEditor();
@@ -8473,245 +8474,7 @@ const debug$7 = debugWithName("editor machine"), editorMachine = setup({
8473
8474
  }
8474
8475
  }
8475
8476
  }
8476
- });
8477
- function createLegacySchema(portableTextType) {
8478
- if (!portableTextType)
8479
- throw new Error("Parameter 'portabletextType' missing (required)");
8480
- const blockType = portableTextType.of?.find(findBlockType);
8481
- if (!blockType)
8482
- throw new Error("Block type is not defined in this schema (required)");
8483
- const childrenField = blockType.fields?.find((field) => field.name === "children");
8484
- if (!childrenField)
8485
- throw new Error("Children field for block type found in schema (required)");
8486
- const ofType = childrenField.type.of;
8487
- if (!ofType)
8488
- throw new Error("Valid types for block children not found in schema (required)");
8489
- const spanType = ofType.find((memberType) => memberType.name === "span");
8490
- if (!spanType)
8491
- throw new Error("Span type not found in schema (required)");
8492
- const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes = portableTextType.of?.filter((field) => field.name !== blockType.name) || [];
8493
- return {
8494
- styles: resolveEnabledStyles(blockType),
8495
- decorators: resolveEnabledDecorators(spanType),
8496
- lists: resolveEnabledListItems(blockType),
8497
- block: blockType,
8498
- span: spanType,
8499
- portableText: portableTextType,
8500
- inlineObjects: inlineObjectTypes,
8501
- blockObjects: blockObjectTypes,
8502
- annotations: spanType.annotations
8503
- };
8504
- }
8505
- function resolveEnabledStyles(blockType) {
8506
- const styleField = blockType.fields?.find((btField) => btField.name === "style");
8507
- if (!styleField)
8508
- throw new Error("A field with name 'style' is not defined in the block type (required).");
8509
- const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter((style) => style.value);
8510
- if (!textStyles || textStyles.length === 0)
8511
- throw new Error("The style fields need at least one style defined. I.e: {title: 'Normal', value: 'normal'}.");
8512
- return textStyles;
8513
- }
8514
- function resolveEnabledDecorators(spanType) {
8515
- return spanType.decorators;
8516
- }
8517
- function resolveEnabledListItems(blockType) {
8518
- const listField = blockType.fields?.find((btField) => btField.name === "listItem");
8519
- if (!listField)
8520
- throw new Error("A field with name 'listItem' is not defined in the block type (required).");
8521
- const listItems = listField.type.options?.list && listField.type.options.list.filter((list) => list.value);
8522
- if (!listItems)
8523
- throw new Error("The list field need at least to be an empty array");
8524
- return listItems;
8525
- }
8526
- function findBlockType(type) {
8527
- return type.type ? findBlockType(type.type) : type.name === "block" ? type : null;
8528
- }
8529
- const temporaryImageName = `tmp-${defaultKeyGenerator()}-image`, temporaryUrlName = `tmp-${defaultKeyGenerator()}-url`, temporaryObjectNames = {
8530
- image: temporaryImageName,
8531
- url: temporaryUrlName
8532
- }, objectNames = {
8533
- [temporaryImageName]: "image",
8534
- [temporaryUrlName]: "url"
8535
- }, defaultObjectTitles = {
8536
- image: "Image",
8537
- url: "URL"
8538
- };
8539
- function legacySchemaToEditorSchema(schema) {
8540
- return {
8541
- annotations: schema.annotations.map((annotation) => ({
8542
- name: annotation.name,
8543
- fields: annotation.fields.map((field) => ({
8544
- name: field.name,
8545
- type: field.type.jsonType,
8546
- title: field.type.title
8547
- })),
8548
- title: annotation.title
8549
- })),
8550
- block: {
8551
- name: schema.block.name
8552
- },
8553
- blockObjects: schema.blockObjects.map((blockObject) => ({
8554
- name: blockObject.name,
8555
- fields: blockObject.fields.map((field) => ({
8556
- name: field.name,
8557
- type: field.type.jsonType,
8558
- title: field.type.title
8559
- })),
8560
- title: blockObject.title
8561
- })),
8562
- decorators: schema.decorators.map((decorator) => ({
8563
- name: decorator.value,
8564
- title: decorator.title,
8565
- value: decorator.value
8566
- })),
8567
- inlineObjects: schema.inlineObjects.map((inlineObject) => ({
8568
- name: inlineObject.name,
8569
- fields: inlineObject.fields.map((field) => ({
8570
- name: field.name,
8571
- type: field.type.jsonType,
8572
- title: field.type.title
8573
- })),
8574
- title: inlineObject.title
8575
- })),
8576
- span: {
8577
- name: schema.span.name
8578
- },
8579
- styles: schema.styles.map((style) => ({
8580
- name: style.value,
8581
- title: style.title,
8582
- value: style.value
8583
- })),
8584
- lists: schema.lists.map((list) => ({
8585
- name: list.value,
8586
- title: list.title,
8587
- value: list.value
8588
- }))
8589
- };
8590
- }
8591
- function compileSchemaDefinition(definition) {
8592
- const styles = (definition.styles ?? []).map((style) => ({
8593
- ...style,
8594
- value: style.name
8595
- }));
8596
- return {
8597
- block: {
8598
- name: "block"
8599
- },
8600
- span: {
8601
- name: "span"
8602
- },
8603
- styles: styles.some((style) => style.value === "normal") ? styles : [{
8604
- value: "normal",
8605
- name: "normal",
8606
- title: "Normal"
8607
- }, ...styles],
8608
- lists: (definition.lists ?? []).map((list) => ({
8609
- ...list,
8610
- value: list.name
8611
- })),
8612
- decorators: (definition.decorators ?? []).map((decorator) => ({
8613
- ...decorator,
8614
- value: decorator.name
8615
- })),
8616
- annotations: (definition.annotations ?? []).map((annotation) => ({
8617
- ...annotation,
8618
- fields: annotation.fields ?? []
8619
- })),
8620
- blockObjects: (definition.blockObjects ?? []).map((blockObject) => ({
8621
- ...blockObject,
8622
- fields: blockObject.fields ?? []
8623
- })),
8624
- inlineObjects: (definition.inlineObjects ?? []).map((inlineObject) => ({
8625
- ...inlineObject,
8626
- fields: inlineObject.fields ?? []
8627
- }))
8628
- };
8629
- }
8630
- function compileSchemaDefinitionToLegacySchema(definition) {
8631
- const blockObjects = definition?.blockObjects?.map((blockObject) => defineType({
8632
- type: "object",
8633
- // Very naive way to work around `SanitySchema.compile` adding default
8634
- // fields to objects with certain names.
8635
- name: temporaryObjectNames[blockObject.name] ?? blockObject.name,
8636
- title: blockObject.title === void 0 ? (
8637
- // This avoids the default title which is a title case of the object name
8638
- defaultObjectTitles[blockObject.name]
8639
- ) : blockObject.title,
8640
- fields: blockObject.fields?.map((field) => ({
8641
- name: field.name,
8642
- type: field.type,
8643
- title: field.title ?? startCase(field.name)
8644
- })) ?? []
8645
- })) ?? [], inlineObjects = definition?.inlineObjects?.map((inlineObject) => defineType({
8646
- type: "object",
8647
- // Very naive way to work around `SanitySchema.compile` adding default
8648
- // fields to objects with certain names.
8649
- name: temporaryObjectNames[inlineObject.name] ?? inlineObject.name,
8650
- title: inlineObject.title === void 0 ? (
8651
- // This avoids the default title which is a title case of the object name
8652
- defaultObjectTitles[inlineObject.name]
8653
- ) : inlineObject.title,
8654
- fields: inlineObject.fields?.map((field) => ({
8655
- name: field.name,
8656
- type: field.type,
8657
- title: field.title ?? startCase(field.name)
8658
- })) ?? []
8659
- })) ?? [], portableTextSchema = defineField({
8660
- type: "array",
8661
- name: "portable-text",
8662
- of: [...blockObjects.map((blockObject) => ({
8663
- type: blockObject.name
8664
- })), {
8665
- type: "block",
8666
- name: "block",
8667
- of: inlineObjects.map((inlineObject) => ({
8668
- type: inlineObject.name
8669
- })),
8670
- marks: {
8671
- decorators: definition?.decorators?.map((decorator) => ({
8672
- title: decorator.title ?? startCase(decorator.name),
8673
- value: decorator.name
8674
- })) ?? [],
8675
- annotations: definition?.annotations?.map((annotation) => ({
8676
- name: annotation.name,
8677
- type: "object",
8678
- title: annotation.title,
8679
- fields: annotation.fields?.map((field) => ({
8680
- name: field.name,
8681
- title: field.title ?? startCase(field.name),
8682
- type: field.type
8683
- })) ?? []
8684
- })) ?? []
8685
- },
8686
- lists: definition?.lists?.map((list) => ({
8687
- value: list.name,
8688
- title: list.title ?? startCase(list.name)
8689
- })) ?? [],
8690
- styles: definition?.styles?.map((style) => ({
8691
- value: style.name,
8692
- title: style.title ?? startCase(style.name)
8693
- })) ?? []
8694
- }]
8695
- }), schema = Schema.compile({
8696
- types: [portableTextSchema, ...blockObjects, ...inlineObjects]
8697
- }).get("portable-text"), pteSchema = createLegacySchema(schema);
8698
- return {
8699
- ...pteSchema,
8700
- blockObjects: pteSchema.blockObjects.map((blockObject) => objectNames[blockObject.name] !== void 0 ? {
8701
- ...blockObject,
8702
- name: objectNames[blockObject.name],
8703
- type: {
8704
- ...blockObject.type,
8705
- name: objectNames[blockObject.name]
8706
- }
8707
- } : blockObject),
8708
- inlineObjects: pteSchema.inlineObjects.map((inlineObject) => objectNames[inlineObject.name] !== void 0 ? {
8709
- ...inlineObject,
8710
- name: objectNames[inlineObject.name]
8711
- } : inlineObject)
8712
- };
8713
- }
8714
- const debug$6 = debugWithName("mutation-machine"), mutationMachine = setup({
8477
+ }), debug$6 = debugWithName("mutation-machine"), mutationMachine = setup({
8715
8478
  types: {
8716
8479
  context: {},
8717
8480
  events: {},
@@ -10349,7 +10112,7 @@ function editorConfigToMachineInput(config) {
10349
10112
  };
10350
10113
  }
10351
10114
  function compileSchemasFromEditorConfig(config) {
10352
- const legacySchema = config.schemaDefinition ? compileSchemaDefinitionToLegacySchema(config.schemaDefinition) : createLegacySchema(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)), schema = config.schemaDefinition ? compileSchemaDefinition(config.schemaDefinition) : legacySchemaToEditorSchema(legacySchema);
10115
+ const legacySchema = config.schemaDefinition ? compileSchemaDefinitionToPortableTextMemberSchemaTypes(config.schemaDefinition) : createPortableTextMemberSchemaTypes(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)), schema = config.schemaDefinition ? compileSchema(config.schemaDefinition) : portableTextMemberSchemaTypesToSchema(legacySchema);
10353
10116
  return {
10354
10117
  legacySchema,
10355
10118
  schema
@@ -12111,9 +11874,6 @@ function EditorProvider(props) {
12111
11874
  let t7;
12112
11875
  return $[25] !== internalEditor_0.editor || $[26] !== t6 ? (t7 = /* @__PURE__ */ jsx(EditorContext.Provider, { value: internalEditor_0.editor, children: t6 }), $[25] = internalEditor_0.editor, $[26] = t6, $[27] = t7) : t7 = $[27], t7;
12113
11876
  }
12114
- function defineSchema(definition) {
12115
- return definition;
12116
- }
12117
11877
  const usePortableTextEditorSelection = () => {
12118
11878
  const $ = c(3), editorActor = useContext(EditorActorContext), [selection, setSelection2] = useState(null);
12119
11879
  let t0, t1;