@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.js CHANGED
@@ -2286,16 +2286,16 @@ function apply$1(value, patch) {
2286
2286
  return OPERATIONS$1[patch.type](value, patch.value);
2287
2287
  }
2288
2288
  const OPERATIONS = {
2289
- replace(currentValue, nextValue) {
2289
+ replace(_currentValue, nextValue) {
2290
2290
  return nextValue;
2291
2291
  },
2292
- set(currentValue, nextValue) {
2292
+ set(_currentValue, nextValue) {
2293
2293
  return nextValue;
2294
2294
  },
2295
2295
  setIfMissing(currentValue, nextValue) {
2296
2296
  return currentValue === void 0 ? nextValue : currentValue;
2297
2297
  },
2298
- unset(currentValue, nextValue) {
2298
+ unset(_currentValue, _nextValue) {
2299
2299
  },
2300
2300
  diffMatchPatch(currentValue, nextValue) {
2301
2301
  const [result] = apply$4(parse(nextValue), currentValue, {
@@ -3752,7 +3752,8 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
3752
3752
  editor,
3753
3753
  toSlateValue(parsed, { schemaTypes }),
3754
3754
  keyGenerator,
3755
- spanTypeName
3755
+ spanTypeName,
3756
+ schemaTypes
3756
3757
  ), validation = validateValue(parsed, schemaTypes, keyGenerator);
3757
3758
  if (!validation.valid && !((_a = validation.resolution) != null && _a.autoResolve)) {
3758
3759
  const errorDescription = `${(_b = validation.resolution) == null ? void 0 : _b.description}`;
@@ -3833,21 +3834,32 @@ const entityMap = {
3833
3834
  function escapeHtml(str) {
3834
3835
  return String(str).replace(/[&<>"'`=/]/g, (s) => entityMap[s]);
3835
3836
  }
3836
- function _regenerateKeys(editor, fragment, keyGenerator, spanTypeName) {
3837
+ function _regenerateKeys(editor, fragment, keyGenerator, spanTypeName, editorTypes) {
3837
3838
  return fragment.map((node) => {
3838
3839
  const newNode = { ...node };
3839
- editor.isTextBlock(newNode) && (newNode.markDefs = (newNode.markDefs || []).map((def) => {
3840
- const oldKey = def._key, newKey = keyGenerator();
3841
- return newNode.children = newNode.children.map(
3842
- (child) => child._type === spanTypeName && editor.isTextSpan(child) ? {
3843
- ...child,
3844
- marks: child.marks && child.marks.includes(oldKey) ? (
3845
- // eslint-disable-next-line max-nested-callbacks
3846
- [...child.marks].filter((mark) => mark !== oldKey).concat(newKey)
3847
- ) : child.marks
3848
- } : child
3849
- ), { ...def, _key: newKey };
3850
- }));
3840
+ if (editor.isTextBlock(newNode)) {
3841
+ const annotations = editorTypes.annotations.map((t) => t.name);
3842
+ if (annotations.length === 0) {
3843
+ const { markDefs, ...NewNodeNoDefs } = newNode;
3844
+ return { ...NewNodeNoDefs, _key: keyGenerator() };
3845
+ }
3846
+ if ((newNode.markDefs || []).some((def) => !annotations.includes(def._type))) {
3847
+ const allowedAnnotations = (newNode.markDefs || []).filter((def) => annotations.includes(def._type));
3848
+ return { ...newNode, markDefs: allowedAnnotations, _key: keyGenerator() };
3849
+ }
3850
+ newNode.markDefs = (newNode.markDefs || []).map((def) => {
3851
+ const oldKey = def._key, newKey = keyGenerator();
3852
+ return newNode.children = newNode.children.map(
3853
+ (child) => child._type === spanTypeName && editor.isTextSpan(child) ? {
3854
+ ...child,
3855
+ marks: child.marks && child.marks.includes(oldKey) ? (
3856
+ // eslint-disable-next-line max-nested-callbacks
3857
+ [...child.marks].filter((mark) => mark !== oldKey).concat(newKey)
3858
+ ) : child.marks
3859
+ } : child
3860
+ ), { ...def, _key: newKey };
3861
+ });
3862
+ }
3851
3863
  const nodeWithNewKeys = { ...newNode, _key: keyGenerator() };
3852
3864
  return editor.isTextBlock(nodeWithNewKeys) && (nodeWithNewKeys.children = nodeWithNewKeys.children.map((child) => ({
3853
3865
  ...child,
@@ -4700,35 +4712,20 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4700
4712
  [onCopy]
4701
4713
  ), handlePaste = react.useCallback(
4702
4714
  (event) => {
4703
- if (event.preventDefault(), !!slateEditor.selection) {
4704
- if (!onPaste) {
4705
- debug("Pasting normally"), slateEditor.insertData(event.clipboardData);
4706
- return;
4707
- }
4708
- new Promise((resolve) => {
4709
- const value = PortableTextEditor.getValue(portableTextEditor), ptRange = toPortableTextRange(value, slateEditor.selection, schemaTypes), path = (ptRange == null ? void 0 : ptRange.focus.path) || [];
4710
- resolve(
4711
- onPaste({
4712
- event,
4713
- value,
4714
- path,
4715
- schemaTypes
4716
- })
4717
- );
4718
- }).then((result) => {
4719
- if (debug("Custom paste function from client resolved", result), change$.next({ type: "loading", isLoading: !0 }), !result || !result.insert) {
4720
- debug("No result from custom paste handler, pasting normally"), slateEditor.insertData(event.clipboardData);
4721
- return;
4722
- }
4723
- if (result && result.insert) {
4724
- slateEditor.insertFragment(
4725
- toSlateValue(result.insert, { schemaTypes })
4726
- ), change$.next({ type: "loading", isLoading: !1 });
4727
- return;
4728
- }
4729
- console.warn("Your onPaste function returned something unexpected:", result);
4730
- }).catch((error) => (change$.next({ type: "loading", isLoading: !1 }), console.error(error), error));
4715
+ if (event.preventDefault(), !slateEditor.selection)
4716
+ return;
4717
+ if (!onPaste) {
4718
+ debug("Pasting normally"), slateEditor.insertData(event.clipboardData);
4719
+ return;
4731
4720
  }
4721
+ 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 });
4722
+ 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) => {
4723
+ debug("Custom paste function from client resolved", result), result && result.insert ? slateEditor.insertFragment(
4724
+ toSlateValue(result.insert, { schemaTypes })
4725
+ ) : console.warn("Your onPaste function returned something unexpected:", result);
4726
+ }).catch((error) => (console.error(error), error)).finally(() => {
4727
+ change$.next({ type: "loading", isLoading: !1 });
4728
+ }));
4732
4729
  },
4733
4730
  [change$, onPaste, portableTextEditor, schemaTypes, slateEditor]
4734
4731
  ), handleOnFocus = react.useCallback(
@@ -4853,21 +4850,8 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
4853
4850
  }
4854
4851
  ) : null;
4855
4852
  });
4856
- function compactPatches(patches) {
4857
- const lastPatch = patches.slice(-1)[0];
4858
- if (lastPatch && lastPatch.type === "unset" && lastPatch.path.length === 0)
4859
- return [lastPatch];
4860
- let finalPatches = patches;
4861
- return finalPatches = finalPatches.filter((patch, index) => {
4862
- if (!patch)
4863
- return !1;
4864
- const nextPatch = finalPatches[index + 1];
4865
- return !(nextPatch && nextPatch.type === "set" && patch.type === "set" && isEqual__default.default(patch.path, nextPatch.path));
4866
- }), finalPatches.length !== patches.length ? finalPatches : patches;
4867
- }
4868
4853
  exports.PortableTextEditable = PortableTextEditable;
4869
4854
  exports.PortableTextEditor = PortableTextEditor;
4870
- exports.compactPatches = compactPatches;
4871
4855
  exports.keyGenerator = defaultKeyGenerator;
4872
4856
  exports.usePortableTextEditor = usePortableTextEditor;
4873
4857
  exports.usePortableTextEditorSelection = usePortableTextEditorSelection;