@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.esm.js CHANGED
@@ -344,7 +344,9 @@ function createMarkdownBehaviors(config) {
344
344
  const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
345
345
  if (!selectionCollapsed || !focusTextBlock || !focusSpan)
346
346
  return !1;
347
- const caretAtTheEndOfQuote = context.selection.focus.offset === 1, looksLikeMarkdownQuote = /^>/.test(focusSpan.node.text), blockquoteStyle = config.mapBlockquoteStyle?.(context.schema);
347
+ const caretAtTheEndOfQuote = context.selection.focus.offset === 1, looksLikeMarkdownQuote = /^>/.test(focusSpan.node.text), blockquoteStyle = config.blockquoteStyle?.({
348
+ schema: context.schema
349
+ });
348
350
  return caretAtTheEndOfQuote && looksLikeMarkdownQuote && blockquoteStyle !== void 0 ? {
349
351
  focusTextBlock,
350
352
  focusSpan,
@@ -385,20 +387,26 @@ function createMarkdownBehaviors(config) {
385
387
  context,
386
388
  event
387
389
  }) => {
388
- if (event.text !== "-")
390
+ const hrCharacter = event.text === "-" ? "-" : event.text === "*" ? "*" : event.text === "_" ? "_" : void 0;
391
+ if (hrCharacter === void 0)
389
392
  return !1;
390
- const breakObject = config.mapBreakObject?.(context.schema), focusBlock = getFocusTextBlock(context), selectionCollapsed = selectionIsCollapsed(context);
393
+ const breakObject = config.horizontalRuleObject?.({
394
+ schema: context.schema
395
+ }), focusBlock = getFocusTextBlock(context), selectionCollapsed = selectionIsCollapsed(context);
391
396
  if (!breakObject || !focusBlock || !selectionCollapsed)
392
397
  return !1;
393
398
  const onlyText = focusBlock.node.children.every(isPortableTextSpan), blockText = focusBlock.node.children.map((child) => child.text ?? "").join("");
394
- return onlyText && blockText === "--" ? {
399
+ return onlyText && blockText === `${hrCharacter}${hrCharacter}` ? {
395
400
  breakObject,
396
- focusBlock
401
+ focusBlock,
402
+ hrCharacter
397
403
  } : !1;
398
404
  },
399
- actions: [() => [{
405
+ actions: [(_, {
406
+ hrCharacter
407
+ }) => [{
400
408
  type: "insert text",
401
- text: "-"
409
+ text: hrCharacter
402
410
  }], (_, {
403
411
  breakObject,
404
412
  focusBlock
@@ -432,15 +440,18 @@ function createMarkdownBehaviors(config) {
432
440
  const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
433
441
  if (!selectionCollapsed || !focusTextBlock || !focusSpan)
434
442
  return !1;
435
- const markdownHeadingSearch = /^#+/.exec(focusSpan.node.text), headingLevel = markdownHeadingSearch ? markdownHeadingSearch[0].length : void 0;
436
- if (context.selection.focus.offset !== headingLevel)
443
+ const markdownHeadingSearch = /^#+/.exec(focusSpan.node.text), level = markdownHeadingSearch ? markdownHeadingSearch[0].length : void 0;
444
+ if (context.selection.focus.offset !== level)
437
445
  return !1;
438
- const headingStyle = headingLevel !== void 0 ? config.mapHeadingStyle?.(context.schema, headingLevel) : void 0;
439
- return headingLevel !== void 0 && headingStyle !== void 0 ? {
446
+ const style = level !== void 0 ? config.headingStyle?.({
447
+ schema: context.schema,
448
+ level
449
+ }) : void 0;
450
+ return level !== void 0 && style !== void 0 ? {
440
451
  focusTextBlock,
441
452
  focusSpan,
442
- style: headingStyle,
443
- level: headingLevel
453
+ style,
454
+ level
444
455
  } : !1;
445
456
  },
446
457
  actions: [() => [{
@@ -480,7 +491,9 @@ function createMarkdownBehaviors(config) {
480
491
  const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
481
492
  if (!selectionCollapsed || !focusTextBlock || !focusSpan)
482
493
  return !1;
483
- const atTheBeginningOfBLock = focusTextBlock.node.children[0]._key === focusSpan.node._key && context.selection.focus.offset === 0, defaultStyle = config.mapDefaultStyle?.(context.schema);
494
+ const atTheBeginningOfBLock = focusTextBlock.node.children[0]._key === focusSpan.node._key && context.selection.focus.offset === 0, defaultStyle = config.defaultStyle?.({
495
+ schema: context.schema
496
+ });
484
497
  return atTheBeginningOfBLock && defaultStyle && focusTextBlock.node.style !== defaultStyle ? {
485
498
  defaultStyle,
486
499
  focusTextBlock
@@ -505,7 +518,11 @@ function createMarkdownBehaviors(config) {
505
518
  const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
506
519
  if (!selectionCollapsed || !focusTextBlock || !focusSpan)
507
520
  return !1;
508
- const defaultStyle = config.mapDefaultStyle?.(context.schema), looksLikeUnorderedList = /^(-|\*)/.test(focusSpan.node.text), unorderedListStyle = config.mapUnorderedListStyle?.(context.schema), caretAtTheEndOfUnorderedList = context.selection.focus.offset === 1;
521
+ const defaultStyle = config.defaultStyle?.({
522
+ schema: context.schema
523
+ }), looksLikeUnorderedList = /^(-|\*)/.test(focusSpan.node.text), unorderedListStyle = config.unorderedListStyle?.({
524
+ schema: context.schema
525
+ }), caretAtTheEndOfUnorderedList = context.selection.focus.offset === 1;
509
526
  if (defaultStyle && caretAtTheEndOfUnorderedList && looksLikeUnorderedList && unorderedListStyle !== void 0)
510
527
  return {
511
528
  focusTextBlock,
@@ -514,7 +531,9 @@ function createMarkdownBehaviors(config) {
514
531
  listItemLength: 1,
515
532
  style: defaultStyle
516
533
  };
517
- const looksLikeOrderedList = /^1./.test(focusSpan.node.text), orderedListStyle = config.mapOrderedListStyle?.(context.schema), caretAtTheEndOfOrderedList = context.selection.focus.offset === 2;
534
+ const looksLikeOrderedList = /^1./.test(focusSpan.node.text), orderedListStyle = config.orderedListStyle?.({
535
+ schema: context.schema
536
+ }), caretAtTheEndOfOrderedList = context.selection.focus.offset === 2;
518
537
  return defaultStyle && caretAtTheEndOfOrderedList && looksLikeOrderedList && orderedListStyle !== void 0 ? {
519
538
  focusTextBlock,
520
539
  focusSpan,
@@ -561,7 +580,7 @@ function createLinkBehaviors(config) {
561
580
  context,
562
581
  event
563
582
  }) => {
564
- const selectionCollapsed = selectionIsCollapsed(context), text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.mapLinkAnnotation?.({
583
+ const selectionCollapsed = selectionIsCollapsed(context), text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
565
584
  url,
566
585
  schema: context.schema
567
586
  }) : void 0;
@@ -584,7 +603,7 @@ function createLinkBehaviors(config) {
584
603
  const focusSpan = getFocusSpan(context), selectionCollapsed = selectionIsCollapsed(context);
585
604
  if (!focusSpan || !selectionCollapsed)
586
605
  return !1;
587
- const text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.mapLinkAnnotation?.({
606
+ const text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
588
607
  url,
589
608
  schema: context.schema
590
609
  }) : void 0;
@@ -2938,11 +2957,10 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
2938
2957
  }
2939
2958
  const debug$i = debugWithName("component:PortableTextEditor:Synchronizer"), debugVerbose$3 = debug$i.enabled && !1, FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === "test" ? 500 : 1e3;
2940
2959
  function Synchronizer(props) {
2941
- const $ = c(35), portableTextEditor = usePortableTextEditor(), readOnly = useSelector(props.editorActor, _temp$1), {
2960
+ const $ = c(35), portableTextEditor = usePortableTextEditor(), readOnly = useSelector(props.editorActor, _temp$1), value = useSelector(props.editorActor, _temp2), {
2942
2961
  editorActor,
2943
2962
  getValue,
2944
- onChange,
2945
- value
2963
+ onChange
2946
2964
  } = props;
2947
2965
  let t0;
2948
2966
  $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = [], $[0] = t0) : t0 = $[0];
@@ -2994,52 +3012,52 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
2994
3012
  });
2995
3013
  debug$i("Subscribing to editor changes");
2996
3014
  const sub = editorActor.on("*", (event) => {
2997
- bb20: switch (event.type) {
3015
+ bb22: switch (event.type) {
2998
3016
  case "patch": {
2999
3017
  IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !0), pendingPatches.current.push(event.patch), onFlushPendingPatchesThrottled(), handleChange(event);
3000
- break bb20;
3018
+ break bb22;
3001
3019
  }
3002
3020
  case "loading": {
3003
3021
  handleChange({
3004
3022
  type: "loading",
3005
3023
  isLoading: !0
3006
3024
  });
3007
- break bb20;
3025
+ break bb22;
3008
3026
  }
3009
3027
  case "done loading": {
3010
3028
  handleChange({
3011
3029
  type: "loading",
3012
3030
  isLoading: !1
3013
3031
  });
3014
- break bb20;
3032
+ break bb22;
3015
3033
  }
3016
3034
  case "focused": {
3017
3035
  handleChange({
3018
3036
  type: "focus",
3019
3037
  event: event.event
3020
3038
  });
3021
- break bb20;
3039
+ break bb22;
3022
3040
  }
3023
3041
  case "offline": {
3024
3042
  handleChange({
3025
3043
  type: "connection",
3026
3044
  value: "offline"
3027
3045
  });
3028
- break bb20;
3046
+ break bb22;
3029
3047
  }
3030
3048
  case "online": {
3031
3049
  handleChange({
3032
3050
  type: "connection",
3033
3051
  value: "online"
3034
3052
  });
3035
- break bb20;
3053
+ break bb22;
3036
3054
  }
3037
3055
  case "value changed": {
3038
3056
  handleChange({
3039
3057
  type: "value",
3040
3058
  value: event.value
3041
3059
  });
3042
- break bb20;
3060
+ break bb22;
3043
3061
  }
3044
3062
  case "invalid value": {
3045
3063
  handleChange({
@@ -3047,21 +3065,21 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
3047
3065
  resolution: event.resolution,
3048
3066
  value: event.value
3049
3067
  });
3050
- break bb20;
3068
+ break bb22;
3051
3069
  }
3052
3070
  case "error": {
3053
3071
  handleChange({
3054
3072
  ...event,
3055
3073
  level: "warning"
3056
3074
  });
3057
- break bb20;
3075
+ break bb22;
3058
3076
  }
3059
3077
  case "annotation.add":
3060
3078
  case "annotation.remove":
3061
3079
  case "annotation.toggle":
3062
3080
  case "focus":
3063
3081
  case "patches":
3064
- break bb20;
3082
+ break bb22;
3065
3083
  default:
3066
3084
  handleChange(event);
3067
3085
  }
@@ -3090,6 +3108,9 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
3090
3108
  }), isInitialValueFromProps.current = !1);
3091
3109
  }, t14 = [editorActor, syncValue, value], $[30] = editorActor, $[31] = syncValue, $[32] = value, $[33] = t13, $[34] = t14) : (t13 = $[33], t14 = $[34]), useEffect(t13, t14), null;
3092
3110
  }
3111
+ function _temp2(s_0) {
3112
+ return s_0.context.value;
3113
+ }
3093
3114
  function _temp$1(s) {
3094
3115
  return s.context.readOnly;
3095
3116
  }
@@ -5707,8 +5728,8 @@ const networkLogic = fromCallback(({
5707
5728
  type: "offline"
5708
5729
  });
5709
5730
  };
5710
- return window.addEventListener("online", onlineHandler), window.addEventListener("offline", offlineHandler), () => {
5711
- window.removeEventListener("online", onlineHandler), window.removeEventListener("offline", offlineHandler);
5731
+ return window && (window.addEventListener("online", onlineHandler), window.addEventListener("offline", offlineHandler)), () => {
5732
+ window && (window.removeEventListener("online", onlineHandler), window.removeEventListener("offline", offlineHandler));
5712
5733
  };
5713
5734
  }), editorMachine = setup({
5714
5735
  types: {
@@ -5823,7 +5844,8 @@ const networkLogic = fromCallback(({
5823
5844
  pendingEvents: [],
5824
5845
  schema: input.schema,
5825
5846
  readOnly: !1,
5826
- maxBlocks: void 0
5847
+ maxBlocks: void 0,
5848
+ value: input.value
5827
5849
  }),
5828
5850
  invoke: {
5829
5851
  id: "networkLogic",
@@ -5933,6 +5955,13 @@ const networkLogic = fromCallback(({
5933
5955
  "update schema": {
5934
5956
  actions: "assign schema"
5935
5957
  },
5958
+ "update value": {
5959
+ actions: assign({
5960
+ value: ({
5961
+ event
5962
+ }) => event.value
5963
+ })
5964
+ },
5936
5965
  "toggle readOnly": {
5937
5966
  actions: assign({
5938
5967
  readOnly: ({
@@ -6094,7 +6123,8 @@ class PortableTextEditor extends Component {
6094
6123
  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 = createActor(editorMachine, {
6095
6124
  input: {
6096
6125
  keyGenerator: props.keyGenerator || defaultKeyGenerator,
6097
- schema: this.schemaTypes
6126
+ schema: this.schemaTypes,
6127
+ value: props.value
6098
6128
  }
6099
6129
  }), this.editorActor.start(), this.slateEditor = createSlateEditor({
6100
6130
  editorActor: this.editorActor
@@ -6116,7 +6146,10 @@ class PortableTextEditor extends Component {
6116
6146
  }), this.props.maxBlocks !== prevProps.maxBlocks && this.editorActor.send({
6117
6147
  type: "update maxBlocks",
6118
6148
  maxBlocks: this.props.maxBlocks === void 0 ? void 0 : Number.parseInt(this.props.maxBlocks.toString(), 10)
6119
- })), this.props.editorRef !== prevProps.editorRef && this.props.editorRef && (this.props.editorRef.current = this);
6149
+ }), this.props.value !== prevProps.value && this.editorActor.send({
6150
+ type: "update value",
6151
+ value: this.props.value
6152
+ }), this.props.editorRef !== prevProps.editorRef && this.props.editorRef && (this.props.editorRef.current = this));
6120
6153
  }
6121
6154
  setEditable = (editable) => {
6122
6155
  this.editable = {
@@ -6135,7 +6168,7 @@ class PortableTextEditor extends Component {
6135
6168
  /* @__PURE__ */ jsx(EditorActorContext.Provider, { value: this.editorActor, children: /* @__PURE__ */ jsx(Slate, { editor: this.slateEditor.instance, initialValue: this.slateEditor.initialValue, children: /* @__PURE__ */ jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsxs(PortableTextEditorSelectionProvider, { editorActor: this.editorActor, children: [
6136
6169
  /* @__PURE__ */ jsx(Synchronizer, { editorActor: this.editorActor, getValue: this.getValue, onChange: (change) => {
6137
6170
  this.props.editor || this.props.onChange(change), this.change$.next(change);
6138
- }, value: this.props.value }),
6171
+ } }),
6139
6172
  this.props.children
6140
6173
  ] }) }) }) })
6141
6174
  ] });
@@ -6901,39 +6934,40 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6901
6934
  });
6902
6935
  PortableTextEditable.displayName = "ForwardRef(PortableTextEditable)";
6903
6936
  function useEditor(config) {
6904
- const $ = c(20), t0 = config.keyGenerator ?? defaultKeyGenerator;
6937
+ const $ = c(21), t0 = config.keyGenerator ?? defaultKeyGenerator;
6905
6938
  let t1;
6906
6939
  $[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];
6907
6940
  let t2;
6908
- $[3] !== config.behaviors || $[4] !== t0 || $[5] !== t1 ? (t2 = {
6941
+ $[3] !== config.behaviors || $[4] !== config.initialValue || $[5] !== t0 || $[6] !== t1 ? (t2 = {
6909
6942
  input: {
6910
6943
  behaviors: config.behaviors,
6911
6944
  keyGenerator: t0,
6912
- schema: t1
6945
+ schema: t1,
6946
+ value: config.initialValue
6913
6947
  }
6914
- }, $[3] = config.behaviors, $[4] = t0, $[5] = t1, $[6] = t2) : t2 = $[6];
6948
+ }, $[3] = config.behaviors, $[4] = config.initialValue, $[5] = t0, $[6] = t1, $[7] = t2) : t2 = $[7];
6915
6949
  const editorActor = useActorRef(editorMachine, t2);
6916
6950
  let t3;
6917
- $[7] !== editorActor ? (t3 = createSlateEditor({
6951
+ $[8] !== editorActor ? (t3 = createSlateEditor({
6918
6952
  editorActor
6919
- }), $[7] = editorActor, $[8] = t3) : t3 = $[8];
6953
+ }), $[8] = editorActor, $[9] = t3) : t3 = $[9];
6920
6954
  const slateEditor = t3, readOnly = useSelector(editorActor, _temp);
6921
6955
  let t4, t5;
6922
- $[9] !== editorActor ? (t4 = (event) => {
6956
+ $[10] !== editorActor ? (t4 = (event) => {
6923
6957
  editorActor.send(event);
6924
- }, t5 = (event_0, listener) => editorActor.on(event_0, listener), $[9] = editorActor, $[10] = t4, $[11] = t5) : (t4 = $[10], t5 = $[11]);
6958
+ }, t5 = (event_0, listener) => editorActor.on(event_0, listener), $[10] = editorActor, $[11] = t4, $[12] = t5) : (t4 = $[11], t5 = $[12]);
6925
6959
  let t6;
6926
- $[12] !== editorActor || $[13] !== slateEditor ? (t6 = {
6960
+ $[13] !== editorActor || $[14] !== slateEditor ? (t6 = {
6927
6961
  editorActor,
6928
6962
  slateEditor
6929
- }, $[12] = editorActor, $[13] = slateEditor, $[14] = t6) : t6 = $[14];
6963
+ }, $[13] = editorActor, $[14] = slateEditor, $[15] = t6) : t6 = $[15];
6930
6964
  let t7;
6931
- return $[15] !== readOnly || $[16] !== t4 || $[17] !== t5 || $[18] !== t6 ? (t7 = {
6965
+ return $[16] !== readOnly || $[17] !== t4 || $[18] !== t5 || $[19] !== t6 ? (t7 = {
6932
6966
  send: t4,
6933
6967
  on: t5,
6934
6968
  readOnly,
6935
6969
  _internal: t6
6936
- }, $[15] = readOnly, $[16] = t4, $[17] = t5, $[18] = t6, $[19] = t7) : t7 = $[19], t7;
6970
+ }, $[16] = readOnly, $[17] = t4, $[18] = t5, $[19] = t6, $[20] = t7) : t7 = $[20], t7;
6937
6971
  }
6938
6972
  function _temp(s) {
6939
6973
  return s.context.readOnly;