@portabletext/editor 1.9.0 → 1.10.1

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.
package/lib/index.js CHANGED
@@ -323,7 +323,9 @@ function createMarkdownBehaviors(config) {
323
323
  const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
324
324
  if (!selectionCollapsed || !focusTextBlock || !focusSpan)
325
325
  return !1;
326
- const caretAtTheEndOfQuote = context.selection.focus.offset === 1, looksLikeMarkdownQuote = /^>/.test(focusSpan.node.text), blockquoteStyle = config.mapBlockquoteStyle?.(context.schema);
326
+ const caretAtTheEndOfQuote = context.selection.focus.offset === 1, looksLikeMarkdownQuote = /^>/.test(focusSpan.node.text), blockquoteStyle = config.blockquoteStyle?.({
327
+ schema: context.schema
328
+ });
327
329
  return caretAtTheEndOfQuote && looksLikeMarkdownQuote && blockquoteStyle !== void 0 ? {
328
330
  focusTextBlock,
329
331
  focusSpan,
@@ -364,20 +366,26 @@ function createMarkdownBehaviors(config) {
364
366
  context,
365
367
  event
366
368
  }) => {
367
- if (event.text !== "-")
369
+ const hrCharacter = event.text === "-" ? "-" : event.text === "*" ? "*" : event.text === "_" ? "_" : void 0;
370
+ if (hrCharacter === void 0)
368
371
  return !1;
369
- const breakObject = config.mapBreakObject?.(context.schema), focusBlock = getFocusTextBlock(context), selectionCollapsed = selectionIsCollapsed(context);
372
+ const breakObject = config.horizontalRuleObject?.({
373
+ schema: context.schema
374
+ }), focusBlock = getFocusTextBlock(context), selectionCollapsed = selectionIsCollapsed(context);
370
375
  if (!breakObject || !focusBlock || !selectionCollapsed)
371
376
  return !1;
372
377
  const onlyText = focusBlock.node.children.every(isPortableTextSpan), blockText = focusBlock.node.children.map((child) => child.text ?? "").join("");
373
- return onlyText && blockText === "--" ? {
378
+ return onlyText && blockText === `${hrCharacter}${hrCharacter}` ? {
374
379
  breakObject,
375
- focusBlock
380
+ focusBlock,
381
+ hrCharacter
376
382
  } : !1;
377
383
  },
378
- actions: [() => [{
384
+ actions: [(_, {
385
+ hrCharacter
386
+ }) => [{
379
387
  type: "insert text",
380
- text: "-"
388
+ text: hrCharacter
381
389
  }], (_, {
382
390
  breakObject,
383
391
  focusBlock
@@ -411,15 +419,18 @@ function createMarkdownBehaviors(config) {
411
419
  const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
412
420
  if (!selectionCollapsed || !focusTextBlock || !focusSpan)
413
421
  return !1;
414
- const markdownHeadingSearch = /^#+/.exec(focusSpan.node.text), headingLevel = markdownHeadingSearch ? markdownHeadingSearch[0].length : void 0;
415
- if (context.selection.focus.offset !== headingLevel)
422
+ const markdownHeadingSearch = /^#+/.exec(focusSpan.node.text), level = markdownHeadingSearch ? markdownHeadingSearch[0].length : void 0;
423
+ if (context.selection.focus.offset !== level)
416
424
  return !1;
417
- const headingStyle = headingLevel !== void 0 ? config.mapHeadingStyle?.(context.schema, headingLevel) : void 0;
418
- return headingLevel !== void 0 && headingStyle !== void 0 ? {
425
+ const style = level !== void 0 ? config.headingStyle?.({
426
+ schema: context.schema,
427
+ level
428
+ }) : void 0;
429
+ return level !== void 0 && style !== void 0 ? {
419
430
  focusTextBlock,
420
431
  focusSpan,
421
- style: headingStyle,
422
- level: headingLevel
432
+ style,
433
+ level
423
434
  } : !1;
424
435
  },
425
436
  actions: [() => [{
@@ -459,7 +470,9 @@ function createMarkdownBehaviors(config) {
459
470
  const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
460
471
  if (!selectionCollapsed || !focusTextBlock || !focusSpan)
461
472
  return !1;
462
- const atTheBeginningOfBLock = focusTextBlock.node.children[0]._key === focusSpan.node._key && context.selection.focus.offset === 0, defaultStyle = config.mapDefaultStyle?.(context.schema);
473
+ const atTheBeginningOfBLock = focusTextBlock.node.children[0]._key === focusSpan.node._key && context.selection.focus.offset === 0, defaultStyle = config.defaultStyle?.({
474
+ schema: context.schema
475
+ });
463
476
  return atTheBeginningOfBLock && defaultStyle && focusTextBlock.node.style !== defaultStyle ? {
464
477
  defaultStyle,
465
478
  focusTextBlock
@@ -484,7 +497,11 @@ function createMarkdownBehaviors(config) {
484
497
  const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
485
498
  if (!selectionCollapsed || !focusTextBlock || !focusSpan)
486
499
  return !1;
487
- const defaultStyle = config.mapDefaultStyle?.(context.schema), looksLikeUnorderedList = /^(-|\*)/.test(focusSpan.node.text), unorderedListStyle = config.mapUnorderedListStyle?.(context.schema), caretAtTheEndOfUnorderedList = context.selection.focus.offset === 1;
500
+ const defaultStyle = config.defaultStyle?.({
501
+ schema: context.schema
502
+ }), looksLikeUnorderedList = /^(-|\*)/.test(focusSpan.node.text), unorderedListStyle = config.unorderedListStyle?.({
503
+ schema: context.schema
504
+ }), caretAtTheEndOfUnorderedList = context.selection.focus.offset === 1;
488
505
  if (defaultStyle && caretAtTheEndOfUnorderedList && looksLikeUnorderedList && unorderedListStyle !== void 0)
489
506
  return {
490
507
  focusTextBlock,
@@ -493,7 +510,9 @@ function createMarkdownBehaviors(config) {
493
510
  listItemLength: 1,
494
511
  style: defaultStyle
495
512
  };
496
- const looksLikeOrderedList = /^1./.test(focusSpan.node.text), orderedListStyle = config.mapOrderedListStyle?.(context.schema), caretAtTheEndOfOrderedList = context.selection.focus.offset === 2;
513
+ const looksLikeOrderedList = /^1./.test(focusSpan.node.text), orderedListStyle = config.orderedListStyle?.({
514
+ schema: context.schema
515
+ }), caretAtTheEndOfOrderedList = context.selection.focus.offset === 2;
497
516
  return defaultStyle && caretAtTheEndOfOrderedList && looksLikeOrderedList && orderedListStyle !== void 0 ? {
498
517
  focusTextBlock,
499
518
  focusSpan,
@@ -540,7 +559,7 @@ function createLinkBehaviors(config) {
540
559
  context,
541
560
  event
542
561
  }) => {
543
- const selectionCollapsed = selectionIsCollapsed(context), text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.mapLinkAnnotation?.({
562
+ const selectionCollapsed = selectionIsCollapsed(context), text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
544
563
  url,
545
564
  schema: context.schema
546
565
  }) : void 0;
@@ -563,7 +582,7 @@ function createLinkBehaviors(config) {
563
582
  const focusSpan = getFocusSpan(context), selectionCollapsed = selectionIsCollapsed(context);
564
583
  if (!focusSpan || !selectionCollapsed)
565
584
  return !1;
566
- const text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.mapLinkAnnotation?.({
585
+ const text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
567
586
  url,
568
587
  schema: context.schema
569
588
  }) : void 0;
@@ -2917,11 +2936,10 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
2917
2936
  }
2918
2937
  const debug$i = debugWithName("component:PortableTextEditor:Synchronizer"), debugVerbose$3 = debug$i.enabled && !1, FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === "test" ? 500 : 1e3;
2919
2938
  function Synchronizer(props) {
2920
- const $ = reactCompilerRuntime.c(35), portableTextEditor = usePortableTextEditor(), readOnly = react$1.useSelector(props.editorActor, _temp$1), {
2939
+ const $ = reactCompilerRuntime.c(35), portableTextEditor = usePortableTextEditor(), readOnly = react$1.useSelector(props.editorActor, _temp$1), value = react$1.useSelector(props.editorActor, _temp2), {
2921
2940
  editorActor,
2922
2941
  getValue,
2923
- onChange,
2924
- value
2942
+ onChange
2925
2943
  } = props;
2926
2944
  let t0;
2927
2945
  $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = [], $[0] = t0) : t0 = $[0];
@@ -2973,52 +2991,52 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
2973
2991
  });
2974
2992
  debug$i("Subscribing to editor changes");
2975
2993
  const sub = editorActor.on("*", (event) => {
2976
- bb20: switch (event.type) {
2994
+ bb22: switch (event.type) {
2977
2995
  case "patch": {
2978
2996
  IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !0), pendingPatches.current.push(event.patch), onFlushPendingPatchesThrottled(), handleChange(event);
2979
- break bb20;
2997
+ break bb22;
2980
2998
  }
2981
2999
  case "loading": {
2982
3000
  handleChange({
2983
3001
  type: "loading",
2984
3002
  isLoading: !0
2985
3003
  });
2986
- break bb20;
3004
+ break bb22;
2987
3005
  }
2988
3006
  case "done loading": {
2989
3007
  handleChange({
2990
3008
  type: "loading",
2991
3009
  isLoading: !1
2992
3010
  });
2993
- break bb20;
3011
+ break bb22;
2994
3012
  }
2995
3013
  case "focused": {
2996
3014
  handleChange({
2997
3015
  type: "focus",
2998
3016
  event: event.event
2999
3017
  });
3000
- break bb20;
3018
+ break bb22;
3001
3019
  }
3002
3020
  case "offline": {
3003
3021
  handleChange({
3004
3022
  type: "connection",
3005
3023
  value: "offline"
3006
3024
  });
3007
- break bb20;
3025
+ break bb22;
3008
3026
  }
3009
3027
  case "online": {
3010
3028
  handleChange({
3011
3029
  type: "connection",
3012
3030
  value: "online"
3013
3031
  });
3014
- break bb20;
3032
+ break bb22;
3015
3033
  }
3016
3034
  case "value changed": {
3017
3035
  handleChange({
3018
3036
  type: "value",
3019
3037
  value: event.value
3020
3038
  });
3021
- break bb20;
3039
+ break bb22;
3022
3040
  }
3023
3041
  case "invalid value": {
3024
3042
  handleChange({
@@ -3026,21 +3044,21 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
3026
3044
  resolution: event.resolution,
3027
3045
  value: event.value
3028
3046
  });
3029
- break bb20;
3047
+ break bb22;
3030
3048
  }
3031
3049
  case "error": {
3032
3050
  handleChange({
3033
3051
  ...event,
3034
3052
  level: "warning"
3035
3053
  });
3036
- break bb20;
3054
+ break bb22;
3037
3055
  }
3038
3056
  case "annotation.add":
3039
3057
  case "annotation.remove":
3040
3058
  case "annotation.toggle":
3041
3059
  case "focus":
3042
3060
  case "patches":
3043
- break bb20;
3061
+ break bb22;
3044
3062
  default:
3045
3063
  handleChange(event);
3046
3064
  }
@@ -3069,6 +3087,9 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
3069
3087
  }), isInitialValueFromProps.current = !1);
3070
3088
  }, t14 = [editorActor, syncValue, value], $[30] = editorActor, $[31] = syncValue, $[32] = value, $[33] = t13, $[34] = t14) : (t13 = $[33], t14 = $[34]), react.useEffect(t13, t14), null;
3071
3089
  }
3090
+ function _temp2(s_0) {
3091
+ return s_0.context.value;
3092
+ }
3072
3093
  function _temp$1(s) {
3073
3094
  return s.context.readOnly;
3074
3095
  }
@@ -5686,8 +5707,8 @@ const networkLogic = xstate.fromCallback(({
5686
5707
  type: "offline"
5687
5708
  });
5688
5709
  };
5689
- return window.addEventListener("online", onlineHandler), window.addEventListener("offline", offlineHandler), () => {
5690
- window.removeEventListener("online", onlineHandler), window.removeEventListener("offline", offlineHandler);
5710
+ return window && (window.addEventListener("online", onlineHandler), window.addEventListener("offline", offlineHandler)), () => {
5711
+ window && (window.removeEventListener("online", onlineHandler), window.removeEventListener("offline", offlineHandler));
5691
5712
  };
5692
5713
  }), editorMachine = xstate.setup({
5693
5714
  types: {
@@ -5802,7 +5823,8 @@ const networkLogic = xstate.fromCallback(({
5802
5823
  pendingEvents: [],
5803
5824
  schema: input.schema,
5804
5825
  readOnly: !1,
5805
- maxBlocks: void 0
5826
+ maxBlocks: void 0,
5827
+ value: input.value
5806
5828
  }),
5807
5829
  invoke: {
5808
5830
  id: "networkLogic",
@@ -5912,6 +5934,13 @@ const networkLogic = xstate.fromCallback(({
5912
5934
  "update schema": {
5913
5935
  actions: "assign schema"
5914
5936
  },
5937
+ "update value": {
5938
+ actions: xstate.assign({
5939
+ value: ({
5940
+ event
5941
+ }) => event.value
5942
+ })
5943
+ },
5915
5944
  "toggle readOnly": {
5916
5945
  actions: xstate.assign({
5917
5946
  readOnly: ({
@@ -6073,7 +6102,8 @@ class PortableTextEditor extends react.Component {
6073
6102
  props.incomingPatches$ && console.warn("The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"), this.schemaTypes = getPortableTextMemberSchemaTypes(props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)), this.editorActor = xstate.createActor(editorMachine, {
6074
6103
  input: {
6075
6104
  keyGenerator: props.keyGenerator || defaultKeyGenerator,
6076
- schema: this.schemaTypes
6105
+ schema: this.schemaTypes,
6106
+ value: props.value
6077
6107
  }
6078
6108
  }), this.editorActor.start(), this.slateEditor = createSlateEditor({
6079
6109
  editorActor: this.editorActor
@@ -6095,7 +6125,10 @@ class PortableTextEditor extends react.Component {
6095
6125
  }), this.props.maxBlocks !== prevProps.maxBlocks && this.editorActor.send({
6096
6126
  type: "update maxBlocks",
6097
6127
  maxBlocks: this.props.maxBlocks === void 0 ? void 0 : Number.parseInt(this.props.maxBlocks.toString(), 10)
6098
- })), this.props.editorRef !== prevProps.editorRef && this.props.editorRef && (this.props.editorRef.current = this);
6128
+ }), this.props.value !== prevProps.value && this.editorActor.send({
6129
+ type: "update value",
6130
+ value: this.props.value
6131
+ }), this.props.editorRef !== prevProps.editorRef && this.props.editorRef && (this.props.editorRef.current = this));
6099
6132
  }
6100
6133
  setEditable = (editable) => {
6101
6134
  this.editable = {
@@ -6114,7 +6147,7 @@ class PortableTextEditor extends react.Component {
6114
6147
  /* @__PURE__ */ jsxRuntime.jsx(EditorActorContext.Provider, { value: this.editorActor, children: /* @__PURE__ */ jsxRuntime.jsx(slateReact.Slate, { editor: this.slateEditor.instance, initialValue: this.slateEditor.initialValue, children: /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsxRuntime.jsxs(PortableTextEditorSelectionProvider, { editorActor: this.editorActor, children: [
6115
6148
  /* @__PURE__ */ jsxRuntime.jsx(Synchronizer, { editorActor: this.editorActor, getValue: this.getValue, onChange: (change) => {
6116
6149
  this.props.editor || this.props.onChange(change), this.change$.next(change);
6117
- }, value: this.props.value }),
6150
+ } }),
6118
6151
  this.props.children
6119
6152
  ] }) }) }) })
6120
6153
  ] });
@@ -6880,39 +6913,40 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6880
6913
  });
6881
6914
  PortableTextEditable.displayName = "ForwardRef(PortableTextEditable)";
6882
6915
  function useEditor(config) {
6883
- const $ = reactCompilerRuntime.c(20), t0 = config.keyGenerator ?? defaultKeyGenerator;
6916
+ const $ = reactCompilerRuntime.c(21), t0 = config.keyGenerator ?? defaultKeyGenerator;
6884
6917
  let t1;
6885
6918
  $[0] !== config.schema || $[1] !== config.schemaDefinition ? (t1 = config.schemaDefinition ? compileSchemaDefinition(config.schemaDefinition) : getPortableTextMemberSchemaTypes(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)), $[0] = config.schema, $[1] = config.schemaDefinition, $[2] = t1) : t1 = $[2];
6886
6919
  let t2;
6887
- $[3] !== config.behaviors || $[4] !== t0 || $[5] !== t1 ? (t2 = {
6920
+ $[3] !== config.behaviors || $[4] !== config.initialValue || $[5] !== t0 || $[6] !== t1 ? (t2 = {
6888
6921
  input: {
6889
6922
  behaviors: config.behaviors,
6890
6923
  keyGenerator: t0,
6891
- schema: t1
6924
+ schema: t1,
6925
+ value: config.initialValue
6892
6926
  }
6893
- }, $[3] = config.behaviors, $[4] = t0, $[5] = t1, $[6] = t2) : t2 = $[6];
6927
+ }, $[3] = config.behaviors, $[4] = config.initialValue, $[5] = t0, $[6] = t1, $[7] = t2) : t2 = $[7];
6894
6928
  const editorActor = react$1.useActorRef(editorMachine, t2);
6895
6929
  let t3;
6896
- $[7] !== editorActor ? (t3 = createSlateEditor({
6930
+ $[8] !== editorActor ? (t3 = createSlateEditor({
6897
6931
  editorActor
6898
- }), $[7] = editorActor, $[8] = t3) : t3 = $[8];
6932
+ }), $[8] = editorActor, $[9] = t3) : t3 = $[9];
6899
6933
  const slateEditor = t3, readOnly = react$1.useSelector(editorActor, _temp);
6900
6934
  let t4, t5;
6901
- $[9] !== editorActor ? (t4 = (event) => {
6935
+ $[10] !== editorActor ? (t4 = (event) => {
6902
6936
  editorActor.send(event);
6903
- }, t5 = (event_0, listener) => editorActor.on(event_0, listener), $[9] = editorActor, $[10] = t4, $[11] = t5) : (t4 = $[10], t5 = $[11]);
6937
+ }, t5 = (event_0, listener) => editorActor.on(event_0, listener), $[10] = editorActor, $[11] = t4, $[12] = t5) : (t4 = $[11], t5 = $[12]);
6904
6938
  let t6;
6905
- $[12] !== editorActor || $[13] !== slateEditor ? (t6 = {
6939
+ $[13] !== editorActor || $[14] !== slateEditor ? (t6 = {
6906
6940
  editorActor,
6907
6941
  slateEditor
6908
- }, $[12] = editorActor, $[13] = slateEditor, $[14] = t6) : t6 = $[14];
6942
+ }, $[13] = editorActor, $[14] = slateEditor, $[15] = t6) : t6 = $[15];
6909
6943
  let t7;
6910
- return $[15] !== readOnly || $[16] !== t4 || $[17] !== t5 || $[18] !== t6 ? (t7 = {
6944
+ return $[16] !== readOnly || $[17] !== t4 || $[18] !== t5 || $[19] !== t6 ? (t7 = {
6911
6945
  send: t4,
6912
6946
  on: t5,
6913
6947
  readOnly,
6914
6948
  _internal: t6
6915
- }, $[15] = readOnly, $[16] = t4, $[17] = t5, $[18] = t6, $[19] = t7) : t7 = $[19], t7;
6949
+ }, $[16] = readOnly, $[17] = t4, $[18] = t5, $[19] = t6, $[20] = t7) : t7 = $[20], t7;
6916
6950
  }
6917
6951
  function _temp(s) {
6918
6952
  return s.context.readOnly;