@portabletext/editor 1.20.0 → 1.21.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/_chunks-cjs/behavior.core.cjs +8 -68
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.get-text-before.cjs +11 -39
- package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
- package/lib/_chunks-cjs/util.get-block-start-point.cjs +30 -0
- package/lib/_chunks-cjs/util.get-block-start-point.cjs.map +1 -0
- package/lib/_chunks-cjs/util.is-empty-text-block.cjs +71 -0
- package/lib/_chunks-cjs/util.is-empty-text-block.cjs.map +1 -0
- package/lib/_chunks-cjs/util.is-keyed-segment.cjs +6 -0
- package/lib/_chunks-cjs/util.is-keyed-segment.cjs.map +1 -0
- package/lib/_chunks-es/behavior.core.js +7 -66
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/selector.get-text-before.js +5 -31
- package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
- package/lib/_chunks-es/util.get-block-start-point.js +31 -0
- package/lib/_chunks-es/util.get-block-start-point.js.map +1 -0
- package/lib/_chunks-es/util.is-empty-text-block.js +73 -0
- package/lib/_chunks-es/util.is-empty-text-block.js.map +1 -0
- package/lib/_chunks-es/util.is-keyed-segment.js +7 -0
- package/lib/_chunks-es/util.is-keyed-segment.js.map +1 -0
- package/lib/behaviors/index.cjs +7 -7
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +179 -119
- package/lib/behaviors/index.d.ts +179 -119
- package/lib/behaviors/index.js +3 -2
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +112 -21
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +870 -141
- package/lib/index.d.ts +870 -141
- package/lib/index.js +111 -19
- package/lib/index.js.map +1 -1
- package/lib/selectors/index.cjs +73 -1
- package/lib/selectors/index.cjs.map +1 -1
- package/lib/selectors/index.d.cts +14 -0
- package/lib/selectors/index.d.ts +14 -0
- package/lib/selectors/index.js +73 -0
- package/lib/selectors/index.js.map +1 -1
- package/lib/utils/index.cjs +11 -0
- package/lib/utils/index.cjs.map +1 -0
- package/lib/utils/index.d.cts +87 -0
- package/lib/utils/index.d.ts +87 -0
- package/lib/utils/index.js +13 -0
- package/lib/utils/index.js.map +1 -0
- package/package.json +15 -9
- package/src/behavior-actions/behavior.action-utils.insert-block.ts +1 -1
- package/src/behavior-actions/behavior.action.insert-block-object.ts +1 -1
- package/src/behavior-actions/behavior.action.insert-inline-object.ts +1 -1
- package/src/behavior-actions/behavior.action.text-block.set.ts +1 -1
- package/src/behavior-actions/behavior.action.text-block.unset.ts +1 -1
- package/src/behavior-actions/behavior.actions.ts +5 -5
- package/src/behaviors/behavior.code-editor.ts +1 -1
- package/src/behaviors/behavior.core.block-objects.ts +2 -2
- package/src/behaviors/behavior.core.decorators.ts +1 -1
- package/src/behaviors/behavior.core.lists.ts +2 -2
- package/src/behaviors/behavior.emoji-picker.ts +1 -1
- package/src/behaviors/behavior.links.ts +1 -1
- package/src/behaviors/behavior.markdown.ts +2 -2
- package/src/behaviors/behavior.types.ts +52 -19
- package/src/behaviors/index.ts +1 -0
- package/src/editor/Editable.tsx +12 -12
- package/src/editor/PortableTextEditor.tsx +2 -2
- package/src/editor/components/DraggableBlock.tsx +2 -2
- package/src/editor/components/Element.tsx +3 -3
- package/src/editor/components/Leaf.tsx +1 -1
- package/src/editor/components/Synchronizer.tsx +1 -1
- package/src/editor/create-editor.ts +22 -20
- package/src/editor/create-slate-editor.tsx +5 -2
- package/src/editor/editor-machine.ts +115 -23
- package/src/editor/get-value.ts +2 -2
- package/src/editor/hooks/usePortableTextEditorSelection.tsx +1 -1
- package/src/editor/mutation-machine.ts +2 -2
- package/src/editor/plugins/create-with-event-listeners.ts +50 -4
- package/src/editor/plugins/createWithEditableAPI.ts +7 -7
- package/src/editor/plugins/createWithHotKeys.ts +2 -2
- package/src/editor/plugins/createWithInsertData.ts +7 -7
- package/src/editor/plugins/createWithMaxBlocks.ts +2 -2
- package/src/editor/plugins/createWithObjectKeys.ts +2 -2
- package/src/editor/plugins/createWithPatches.ts +10 -10
- package/src/editor/plugins/createWithPlaceholderBlock.ts +3 -3
- package/src/editor/plugins/createWithPortableTextBlockStyle.ts +1 -1
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +4 -4
- package/src/editor/plugins/createWithPortableTextSelections.ts +6 -6
- package/src/editor/plugins/createWithSchemaTypes.ts +1 -1
- package/src/editor/plugins/createWithUndoRedo.ts +5 -5
- package/src/editor/plugins/createWithUtils.ts +2 -2
- package/src/editor/plugins/with-plugins.ts +1 -1
- package/src/editor/sync-machine.ts +8 -5
- package/src/index.ts +5 -1
- package/src/selectors/index.ts +2 -0
- package/src/selectors/selector.get-selection-text.test.ts +75 -0
- package/src/selectors/selector.get-selection-text.ts +3 -3
- package/src/selectors/selector.get-text-before.ts +7 -4
- package/src/selectors/selector.is-point-after-selection.ts +82 -0
- package/src/selectors/selector.is-point-before-selection.ts +82 -0
- package/src/utils/_exports/index.ts +1 -0
- package/src/utils/index.ts +11 -0
- package/src/{editor/utils/utils.block-offset.test.ts → utils/util.block-offset.test.ts} +1 -1
- package/src/{editor/utils/utils.block-offset.ts → utils/util.block-offset.ts} +23 -7
- package/src/{editor/utils/utils.get-start-point.ts → utils/util.get-block-start-point.ts} +5 -2
- package/src/utils/util.get-text-block-text.ts +8 -0
- package/src/{editor/utils/utils.ts → utils/util.is-empty-text-block.ts} +4 -5
- package/src/{editor/utils/utils.is-keyed-segment.ts → utils/util.is-keyed-segment.ts} +3 -0
- package/src/{editor/utils/utils.reverse-selection.ts → utils/util.reverse-selection.ts} +4 -1
- /package/src/{utils → internal-utils}/__tests__/dmpToOperations.test.ts +0 -0
- /package/src/{utils → internal-utils}/__tests__/operationToPatches.test.ts +0 -0
- /package/src/{utils → internal-utils}/__tests__/patchToOperations.test.ts +0 -0
- /package/src/{utils → internal-utils}/__tests__/ranges.test.ts +0 -0
- /package/src/{utils → internal-utils}/__tests__/valueNormalization.test.tsx +0 -0
- /package/src/{utils → internal-utils}/__tests__/values.test.ts +0 -0
- /package/src/{utils → internal-utils}/applyPatch.ts +0 -0
- /package/src/{utils → internal-utils}/debug.ts +0 -0
- /package/src/{utils → internal-utils}/is-hotkey.test.ts +0 -0
- /package/src/{utils → internal-utils}/is-hotkey.ts +0 -0
- /package/src/{utils → internal-utils}/looks-like-url.test.ts +0 -0
- /package/src/{utils → internal-utils}/looks-like-url.ts +0 -0
- /package/src/{utils → internal-utils}/operationToPatches.ts +0 -0
- /package/src/{utils → internal-utils}/paths.ts +0 -0
- /package/src/{utils → internal-utils}/ranges.ts +0 -0
- /package/src/{utils → internal-utils}/schema.ts +0 -0
- /package/src/{utils → internal-utils}/selection.ts +0 -0
- /package/src/{utils → internal-utils}/sibling-utils.ts +0 -0
- /package/src/{utils → internal-utils}/validateValue.ts +0 -0
- /package/src/{utils → internal-utils}/values.ts +0 -0
- /package/src/{utils → internal-utils}/weakMaps.ts +0 -0
- /package/src/{utils → internal-utils}/withChanges.ts +0 -0
- /package/src/{utils → internal-utils}/withUndoRedo.ts +0 -0
- /package/src/{utils → internal-utils}/withoutPatching.ts +0 -0
package/lib/index.js
CHANGED
|
@@ -21,7 +21,8 @@ import get from "lodash/get.js";
|
|
|
21
21
|
import isUndefined from "lodash/isUndefined.js";
|
|
22
22
|
import omitBy from "lodash/omitBy.js";
|
|
23
23
|
import { createGuards } from "./_chunks-es/selector.is-selection-collapsed.js";
|
|
24
|
-
import { blockOffsetToSpanSelectionPoint
|
|
24
|
+
import { blockOffsetToSpanSelectionPoint } from "./_chunks-es/util.is-empty-text-block.js";
|
|
25
|
+
import { coreBehaviors, isCustomBehaviorEvent, isHotkey } from "./_chunks-es/behavior.core.js";
|
|
25
26
|
import getRandomValues from "get-random-values-esm";
|
|
26
27
|
import { htmlToBlocks, normalizeBlock } from "@sanity/block-tools";
|
|
27
28
|
function createEditorSchema(portableTextType) {
|
|
@@ -2788,6 +2789,14 @@ function createWithEventListeners(editorActor, subscriptions) {
|
|
|
2788
2789
|
});
|
|
2789
2790
|
break;
|
|
2790
2791
|
}
|
|
2792
|
+
case "custom.*": {
|
|
2793
|
+
editorActor.send({
|
|
2794
|
+
type: "custom behavior event",
|
|
2795
|
+
behaviorEvent: event.event,
|
|
2796
|
+
editor
|
|
2797
|
+
});
|
|
2798
|
+
break;
|
|
2799
|
+
}
|
|
2791
2800
|
case "decorator.toggle": {
|
|
2792
2801
|
editorActor.send({
|
|
2793
2802
|
type: "behavior event",
|
|
@@ -2872,9 +2881,18 @@ function createWithEventListeners(editorActor, subscriptions) {
|
|
|
2872
2881
|
};
|
|
2873
2882
|
});
|
|
2874
2883
|
const {
|
|
2884
|
+
deleteBackward: deleteBackward2,
|
|
2885
|
+
deleteForward: deleteForward2,
|
|
2886
|
+
insertBreak,
|
|
2887
|
+
insertSoftBreak,
|
|
2888
|
+
insertText: insertText2,
|
|
2875
2889
|
select
|
|
2876
2890
|
} = editor;
|
|
2877
2891
|
return editor.deleteBackward = (unit) => {
|
|
2892
|
+
if (isApplyingBehaviorActions(editor)) {
|
|
2893
|
+
deleteBackward2(unit);
|
|
2894
|
+
return;
|
|
2895
|
+
}
|
|
2878
2896
|
editorActor.send({
|
|
2879
2897
|
type: "behavior event",
|
|
2880
2898
|
behaviorEvent: {
|
|
@@ -2884,6 +2902,10 @@ function createWithEventListeners(editorActor, subscriptions) {
|
|
|
2884
2902
|
editor
|
|
2885
2903
|
});
|
|
2886
2904
|
}, editor.deleteForward = (unit) => {
|
|
2905
|
+
if (isApplyingBehaviorActions(editor)) {
|
|
2906
|
+
deleteForward2(unit);
|
|
2907
|
+
return;
|
|
2908
|
+
}
|
|
2887
2909
|
editorActor.send({
|
|
2888
2910
|
type: "behavior event",
|
|
2889
2911
|
behaviorEvent: {
|
|
@@ -2893,6 +2915,10 @@ function createWithEventListeners(editorActor, subscriptions) {
|
|
|
2893
2915
|
editor
|
|
2894
2916
|
});
|
|
2895
2917
|
}, editor.insertBreak = () => {
|
|
2918
|
+
if (isApplyingBehaviorActions(editor)) {
|
|
2919
|
+
insertBreak();
|
|
2920
|
+
return;
|
|
2921
|
+
}
|
|
2896
2922
|
editorActor.send({
|
|
2897
2923
|
type: "behavior event",
|
|
2898
2924
|
behaviorEvent: {
|
|
@@ -2901,6 +2927,10 @@ function createWithEventListeners(editorActor, subscriptions) {
|
|
|
2901
2927
|
editor
|
|
2902
2928
|
});
|
|
2903
2929
|
}, editor.insertSoftBreak = () => {
|
|
2930
|
+
if (isApplyingBehaviorActions(editor)) {
|
|
2931
|
+
insertSoftBreak();
|
|
2932
|
+
return;
|
|
2933
|
+
}
|
|
2904
2934
|
editorActor.send({
|
|
2905
2935
|
type: "behavior event",
|
|
2906
2936
|
behaviorEvent: {
|
|
@@ -2909,6 +2939,10 @@ function createWithEventListeners(editorActor, subscriptions) {
|
|
|
2909
2939
|
editor
|
|
2910
2940
|
});
|
|
2911
2941
|
}, editor.insertText = (text, options) => {
|
|
2942
|
+
if (isApplyingBehaviorActions(editor)) {
|
|
2943
|
+
insertText2(text, options);
|
|
2944
|
+
return;
|
|
2945
|
+
}
|
|
2912
2946
|
editorActor.send({
|
|
2913
2947
|
type: "behavior event",
|
|
2914
2948
|
behaviorEvent: {
|
|
@@ -2916,7 +2950,10 @@ function createWithEventListeners(editorActor, subscriptions) {
|
|
|
2916
2950
|
text,
|
|
2917
2951
|
options
|
|
2918
2952
|
},
|
|
2919
|
-
editor
|
|
2953
|
+
editor,
|
|
2954
|
+
defaultActionCallback: () => {
|
|
2955
|
+
insertText2(text, options);
|
|
2956
|
+
}
|
|
2920
2957
|
});
|
|
2921
2958
|
}, editor.select = (location) => {
|
|
2922
2959
|
if (isApplyingBehaviorActions(editor)) {
|
|
@@ -2930,7 +2967,10 @@ function createWithEventListeners(editorActor, subscriptions) {
|
|
|
2930
2967
|
type: "select",
|
|
2931
2968
|
selection: toPortableTextRange(fromSlateValue(editor.children, editorActor.getSnapshot().context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), range, editorActor.getSnapshot().context.schema)
|
|
2932
2969
|
},
|
|
2933
|
-
editor
|
|
2970
|
+
editor,
|
|
2971
|
+
defaultActionCallback: () => {
|
|
2972
|
+
select(location);
|
|
2973
|
+
}
|
|
2934
2974
|
});
|
|
2935
2975
|
}, editor;
|
|
2936
2976
|
};
|
|
@@ -5606,20 +5646,36 @@ const editorMachine = setup({
|
|
|
5606
5646
|
event,
|
|
5607
5647
|
enqueue
|
|
5608
5648
|
}) => {
|
|
5609
|
-
assertEvent(event, ["behavior event"]);
|
|
5610
|
-
const defaultAction = event.behaviorEvent.type === "copy" || event.behaviorEvent.type === "key.down" || event.behaviorEvent.type === "key.up" || event.behaviorEvent.type === "paste" ? void 0 : {
|
|
5649
|
+
assertEvent(event, ["behavior event", "custom behavior event"]);
|
|
5650
|
+
const defaultAction = event.type === "custom behavior event" || event.behaviorEvent.type === "copy" || event.behaviorEvent.type === "key.down" || event.behaviorEvent.type === "key.up" || event.behaviorEvent.type === "paste" ? void 0 : {
|
|
5611
5651
|
...event.behaviorEvent,
|
|
5612
5652
|
editor: event.editor
|
|
5613
|
-
}, eventBehaviors = [...context.behaviors.values()].filter((behavior) => behavior.on === event.behaviorEvent.type);
|
|
5653
|
+
}, defaultActionCallback = event.type === "behavior event" ? event.defaultActionCallback : void 0, eventBehaviors = [...context.behaviors.values()].filter((behavior) => behavior.on === event.behaviorEvent.type);
|
|
5614
5654
|
if (eventBehaviors.length === 0) {
|
|
5655
|
+
if (defaultActionCallback) {
|
|
5656
|
+
withApplyingBehaviorActions(event.editor, () => {
|
|
5657
|
+
Editor.withoutNormalizing(event.editor, () => {
|
|
5658
|
+
try {
|
|
5659
|
+
defaultActionCallback();
|
|
5660
|
+
} catch (error) {
|
|
5661
|
+
console.error(new Error(`Performing action "${event.behaviorEvent.type}" failed due to: ${error.message}`));
|
|
5662
|
+
}
|
|
5663
|
+
});
|
|
5664
|
+
});
|
|
5665
|
+
return;
|
|
5666
|
+
}
|
|
5615
5667
|
if (!defaultAction)
|
|
5616
5668
|
return;
|
|
5617
5669
|
withApplyingBehaviorActions(event.editor, () => {
|
|
5618
5670
|
Editor.withoutNormalizing(event.editor, () => {
|
|
5619
|
-
|
|
5620
|
-
|
|
5621
|
-
|
|
5622
|
-
|
|
5671
|
+
try {
|
|
5672
|
+
performAction({
|
|
5673
|
+
context,
|
|
5674
|
+
action: defaultAction
|
|
5675
|
+
});
|
|
5676
|
+
} catch (error) {
|
|
5677
|
+
console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`));
|
|
5678
|
+
}
|
|
5623
5679
|
});
|
|
5624
5680
|
}), event.editor.onChange();
|
|
5625
5681
|
return;
|
|
@@ -5651,7 +5707,11 @@ const editorMachine = setup({
|
|
|
5651
5707
|
Editor.withoutNormalizing(event.editor, () => {
|
|
5652
5708
|
for (const actionIntend of actionIntends) {
|
|
5653
5709
|
if (actionIntend.type === "raise") {
|
|
5654
|
-
enqueue.raise({
|
|
5710
|
+
isCustomBehaviorEvent(actionIntend.event) ? enqueue.raise({
|
|
5711
|
+
type: "custom behavior event",
|
|
5712
|
+
behaviorEvent: actionIntend.event,
|
|
5713
|
+
editor: event.editor
|
|
5714
|
+
}) : enqueue.raise({
|
|
5655
5715
|
type: "behavior event",
|
|
5656
5716
|
behaviorEvent: actionIntend.event,
|
|
5657
5717
|
editor: event.editor
|
|
@@ -5662,10 +5722,15 @@ const editorMachine = setup({
|
|
|
5662
5722
|
...actionIntend,
|
|
5663
5723
|
editor: event.editor
|
|
5664
5724
|
};
|
|
5665
|
-
|
|
5666
|
-
|
|
5667
|
-
|
|
5668
|
-
|
|
5725
|
+
try {
|
|
5726
|
+
performAction({
|
|
5727
|
+
context,
|
|
5728
|
+
action
|
|
5729
|
+
});
|
|
5730
|
+
} catch (error) {
|
|
5731
|
+
console.error(new Error(`Performing action "${action.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`));
|
|
5732
|
+
break;
|
|
5733
|
+
}
|
|
5669
5734
|
}
|
|
5670
5735
|
});
|
|
5671
5736
|
}), event.editor.onChange();
|
|
@@ -5675,14 +5740,30 @@ const editorMachine = setup({
|
|
|
5675
5740
|
}
|
|
5676
5741
|
}
|
|
5677
5742
|
if (!behaviorOverwritten) {
|
|
5743
|
+
if (defaultActionCallback) {
|
|
5744
|
+
withApplyingBehaviorActions(event.editor, () => {
|
|
5745
|
+
Editor.withoutNormalizing(event.editor, () => {
|
|
5746
|
+
try {
|
|
5747
|
+
defaultActionCallback();
|
|
5748
|
+
} catch (error) {
|
|
5749
|
+
console.error(new Error(`Performing "${event.behaviorEvent.type}" failed due to: ${error.message}`));
|
|
5750
|
+
}
|
|
5751
|
+
});
|
|
5752
|
+
});
|
|
5753
|
+
return;
|
|
5754
|
+
}
|
|
5678
5755
|
if (!defaultAction)
|
|
5679
5756
|
return;
|
|
5680
5757
|
withApplyingBehaviorActions(event.editor, () => {
|
|
5681
5758
|
Editor.withoutNormalizing(event.editor, () => {
|
|
5682
|
-
|
|
5683
|
-
|
|
5684
|
-
|
|
5685
|
-
|
|
5759
|
+
try {
|
|
5760
|
+
performAction({
|
|
5761
|
+
context,
|
|
5762
|
+
action: defaultAction
|
|
5763
|
+
});
|
|
5764
|
+
} catch (error) {
|
|
5765
|
+
console.error(new Error(`Performing action "${defaultAction.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`));
|
|
5766
|
+
}
|
|
5686
5767
|
});
|
|
5687
5768
|
}), event.editor.onChange();
|
|
5688
5769
|
}
|
|
@@ -5829,6 +5910,9 @@ const editorMachine = setup({
|
|
|
5829
5910
|
"behavior event": {
|
|
5830
5911
|
actions: "handle behavior event"
|
|
5831
5912
|
},
|
|
5913
|
+
"custom behavior event": {
|
|
5914
|
+
actions: "handle behavior event"
|
|
5915
|
+
},
|
|
5832
5916
|
"annotation.*": {
|
|
5833
5917
|
actions: emit(({
|
|
5834
5918
|
event
|
|
@@ -5839,6 +5923,14 @@ const editorMachine = setup({
|
|
|
5839
5923
|
event
|
|
5840
5924
|
}) => event)
|
|
5841
5925
|
},
|
|
5926
|
+
"custom.*": {
|
|
5927
|
+
actions: emit(({
|
|
5928
|
+
event
|
|
5929
|
+
}) => ({
|
|
5930
|
+
type: "custom.*",
|
|
5931
|
+
event
|
|
5932
|
+
}))
|
|
5933
|
+
},
|
|
5842
5934
|
"decorator.*": {
|
|
5843
5935
|
actions: emit(({
|
|
5844
5936
|
event
|