@portabletext/editor 1.0.2 → 1.0.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.
package/lib/index.mjs CHANGED
@@ -2306,16 +2306,16 @@ function apply$1(value, patch) {
2306
2306
  return OPERATIONS$1[patch.type](value, patch.value);
2307
2307
  }
2308
2308
  const OPERATIONS = {
2309
- replace(currentValue, nextValue) {
2309
+ replace(_currentValue, nextValue) {
2310
2310
  return nextValue;
2311
2311
  },
2312
- set(currentValue, nextValue) {
2312
+ set(_currentValue, nextValue) {
2313
2313
  return nextValue;
2314
2314
  },
2315
2315
  setIfMissing(currentValue, nextValue) {
2316
2316
  return currentValue === void 0 ? nextValue : currentValue;
2317
2317
  },
2318
- unset(currentValue, nextValue) {
2318
+ unset(_currentValue, _nextValue) {
2319
2319
  },
2320
2320
  diffMatchPatch(currentValue, nextValue) {
2321
2321
  const [result] = apply$4(parse(nextValue), currentValue, {
@@ -3772,7 +3772,8 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3772
3772
  editor,
3773
3773
  toSlateValue(parsed, { schemaTypes }),
3774
3774
  keyGenerator,
3775
- spanTypeName
3775
+ spanTypeName,
3776
+ schemaTypes
3776
3777
  ), validation = validateValue(parsed, schemaTypes, keyGenerator);
3777
3778
  if (!validation.valid && !((_a = validation.resolution) != null && _a.autoResolve)) {
3778
3779
  const errorDescription = `${(_b = validation.resolution) == null ? void 0 : _b.description}`;
@@ -3853,21 +3854,32 @@ const entityMap = {
3853
3854
  function escapeHtml(str) {
3854
3855
  return String(str).replace(/[&<>"'`=/]/g, (s) => entityMap[s]);
3855
3856
  }
3856
- function _regenerateKeys(editor, fragment, keyGenerator, spanTypeName) {
3857
+ function _regenerateKeys(editor, fragment, keyGenerator, spanTypeName, editorTypes) {
3857
3858
  return fragment.map((node) => {
3858
3859
  const newNode = { ...node };
3859
- editor.isTextBlock(newNode) && (newNode.markDefs = (newNode.markDefs || []).map((def) => {
3860
- const oldKey = def._key, newKey = keyGenerator();
3861
- return newNode.children = newNode.children.map(
3862
- (child) => child._type === spanTypeName && editor.isTextSpan(child) ? {
3863
- ...child,
3864
- marks: child.marks && child.marks.includes(oldKey) ? (
3865
- // eslint-disable-next-line max-nested-callbacks
3866
- [...child.marks].filter((mark) => mark !== oldKey).concat(newKey)
3867
- ) : child.marks
3868
- } : child
3869
- ), { ...def, _key: newKey };
3870
- }));
3860
+ if (editor.isTextBlock(newNode)) {
3861
+ const annotations = editorTypes.annotations.map((t) => t.name);
3862
+ if (annotations.length === 0) {
3863
+ const { markDefs, ...NewNodeNoDefs } = newNode;
3864
+ return { ...NewNodeNoDefs, _key: keyGenerator() };
3865
+ }
3866
+ if ((newNode.markDefs || []).some((def) => !annotations.includes(def._type))) {
3867
+ const allowedAnnotations = (newNode.markDefs || []).filter((def) => annotations.includes(def._type));
3868
+ return { ...newNode, markDefs: allowedAnnotations, _key: keyGenerator() };
3869
+ }
3870
+ newNode.markDefs = (newNode.markDefs || []).map((def) => {
3871
+ const oldKey = def._key, newKey = keyGenerator();
3872
+ return newNode.children = newNode.children.map(
3873
+ (child) => child._type === spanTypeName && editor.isTextSpan(child) ? {
3874
+ ...child,
3875
+ marks: child.marks && child.marks.includes(oldKey) ? (
3876
+ // eslint-disable-next-line max-nested-callbacks
3877
+ [...child.marks].filter((mark) => mark !== oldKey).concat(newKey)
3878
+ ) : child.marks
3879
+ } : child
3880
+ ), { ...def, _key: newKey };
3881
+ });
3882
+ }
3871
3883
  const nodeWithNewKeys = { ...newNode, _key: keyGenerator() };
3872
3884
  return editor.isTextBlock(nodeWithNewKeys) && (nodeWithNewKeys.children = nodeWithNewKeys.children.map((child) => ({
3873
3885
  ...child,
@@ -4720,35 +4732,20 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4720
4732
  [onCopy]
4721
4733
  ), handlePaste = useCallback(
4722
4734
  (event) => {
4723
- if (event.preventDefault(), !!slateEditor.selection) {
4724
- if (!onPaste) {
4725
- debug("Pasting normally"), slateEditor.insertData(event.clipboardData);
4726
- return;
4727
- }
4728
- new Promise((resolve) => {
4729
- const value = PortableTextEditor.getValue(portableTextEditor), ptRange = toPortableTextRange(value, slateEditor.selection, schemaTypes), path = (ptRange == null ? void 0 : ptRange.focus.path) || [];
4730
- resolve(
4731
- onPaste({
4732
- event,
4733
- value,
4734
- path,
4735
- schemaTypes
4736
- })
4737
- );
4738
- }).then((result) => {
4739
- if (debug("Custom paste function from client resolved", result), change$.next({ type: "loading", isLoading: !0 }), !result || !result.insert) {
4740
- debug("No result from custom paste handler, pasting normally"), slateEditor.insertData(event.clipboardData);
4741
- return;
4742
- }
4743
- if (result && result.insert) {
4744
- slateEditor.insertFragment(
4745
- toSlateValue(result.insert, { schemaTypes })
4746
- ), change$.next({ type: "loading", isLoading: !1 });
4747
- return;
4748
- }
4749
- console.warn("Your onPaste function returned something unexpected:", result);
4750
- }).catch((error) => (change$.next({ type: "loading", isLoading: !1 }), console.error(error), error));
4735
+ if (event.preventDefault(), !slateEditor.selection)
4736
+ return;
4737
+ if (!onPaste) {
4738
+ debug("Pasting normally"), slateEditor.insertData(event.clipboardData);
4739
+ return;
4751
4740
  }
4741
+ const value = PortableTextEditor.getValue(portableTextEditor), ptRange = toPortableTextRange(value, slateEditor.selection, schemaTypes), path = (ptRange == null ? void 0 : ptRange.focus.path) || [], onPasteResult = onPaste({ event, value, path, schemaTypes });
4742
+ onPasteResult === void 0 ? (debug("No result from custom paste handler, pasting normally"), slateEditor.insertData(event.clipboardData)) : (change$.next({ type: "loading", isLoading: !0 }), Promise.resolve(onPasteResult).then((result) => {
4743
+ debug("Custom paste function from client resolved", result), result && result.insert ? slateEditor.insertFragment(
4744
+ toSlateValue(result.insert, { schemaTypes })
4745
+ ) : console.warn("Your onPaste function returned something unexpected:", result);
4746
+ }).catch((error) => (console.error(error), error)).finally(() => {
4747
+ change$.next({ type: "loading", isLoading: !1 });
4748
+ }));
4752
4749
  },
4753
4750
  [change$, onPaste, portableTextEditor, schemaTypes, slateEditor]
4754
4751
  ), handleOnFocus = useCallback(
@@ -4873,22 +4870,9 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4873
4870
  }
4874
4871
  ) : null;
4875
4872
  });
4876
- function compactPatches(patches) {
4877
- const lastPatch = patches.slice(-1)[0];
4878
- if (lastPatch && lastPatch.type === "unset" && lastPatch.path.length === 0)
4879
- return [lastPatch];
4880
- let finalPatches = patches;
4881
- return finalPatches = finalPatches.filter((patch, index) => {
4882
- if (!patch)
4883
- return !1;
4884
- const nextPatch = finalPatches[index + 1];
4885
- return !(nextPatch && nextPatch.type === "set" && patch.type === "set" && isEqual(patch.path, nextPatch.path));
4886
- }), finalPatches.length !== patches.length ? finalPatches : patches;
4887
- }
4888
4873
  export {
4889
4874
  PortableTextEditable,
4890
4875
  PortableTextEditor,
4891
- compactPatches,
4892
4876
  defaultKeyGenerator as keyGenerator,
4893
4877
  usePortableTextEditor,
4894
4878
  usePortableTextEditorSelection