@portabletext/editor 3.0.2 → 3.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/_chunks-dts/index.d.ts +1 -1
- package/lib/index.js +79 -75
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/editor/PortableTextEditor.tsx +1 -1
- package/src/editor/create-slate-editor.tsx +6 -4
- package/src/editor/plugins/createWithEditableAPI.ts +17 -12
- package/src/internal-utils/__tests__/values.test.ts +85 -107
- package/src/internal-utils/applyPatch.ts +17 -11
- package/src/internal-utils/operation-to-patches.ts +20 -27
- package/src/internal-utils/slate-utils.ts +2 -2
- package/src/internal-utils/values.test.ts +289 -0
- package/src/internal-utils/values.ts +54 -53
- package/src/operations/behavior.operation.block.unset.ts +4 -4
|
@@ -518,7 +518,7 @@ declare class PortableTextEditor extends Component<PortableTextEditorProps<Inter
|
|
|
518
518
|
* ```
|
|
519
519
|
* import * as selectors from '@portabletext/editor/selectors'
|
|
520
520
|
* const editor = useEditor()
|
|
521
|
-
* const
|
|
521
|
+
* const selectedValue = useEditorSelector(editor, selectors.getSelectedValue)
|
|
522
522
|
* ```
|
|
523
523
|
*/
|
|
524
524
|
static getFragment: (editor: PortableTextEditor) => PortableTextBlock[] | undefined;
|
package/lib/index.js
CHANGED
|
@@ -40,13 +40,6 @@ function keepObjectEquality(object, keyMap) {
|
|
|
40
40
|
const value = keyMap[object._key];
|
|
41
41
|
return value && isEqual(object, value) ? value : (keyMap[object._key] = object, object);
|
|
42
42
|
}
|
|
43
|
-
function toSlateValue(value, {
|
|
44
|
-
schemaTypes
|
|
45
|
-
}, keyMap = {}) {
|
|
46
|
-
return value && Array.isArray(value) ? value.map((block) => toSlateBlock(block, {
|
|
47
|
-
schemaTypes
|
|
48
|
-
}, keyMap)) : [];
|
|
49
|
-
}
|
|
50
43
|
function toSlateBlock(block, {
|
|
51
44
|
schemaTypes
|
|
52
45
|
}, keyMap = {}) {
|
|
@@ -64,7 +57,11 @@ function toSlateBlock(block, {
|
|
|
64
57
|
_key: childKey,
|
|
65
58
|
...childProps
|
|
66
59
|
} = child, propKeys = Object.keys(childProps);
|
|
67
|
-
return childType
|
|
60
|
+
return childType === void 0 && propKeys.length === 1 && propKeys.at(0) === "text" ? {
|
|
61
|
+
_key: childKey,
|
|
62
|
+
_type: schemaTypes.span.name,
|
|
63
|
+
text: childProps.text
|
|
64
|
+
} : childType !== schemaTypes.span.name ? (hasInlines = !0, keepObjectEquality({
|
|
68
65
|
_type: childType,
|
|
69
66
|
_key: childKey,
|
|
70
67
|
children: [{
|
|
@@ -97,52 +94,53 @@ function toSlateBlock(block, {
|
|
|
97
94
|
}, keyMap);
|
|
98
95
|
}
|
|
99
96
|
function fromSlateValue(value, textBlockType, keyMap = {}) {
|
|
100
|
-
return value.map((block) =>
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
97
|
+
return value.map((block) => fromSlateBlock(block, textBlockType, keyMap));
|
|
98
|
+
}
|
|
99
|
+
function fromSlateBlock(block, textBlockType, keyMap = {}) {
|
|
100
|
+
const {
|
|
101
|
+
_key,
|
|
102
|
+
_type
|
|
103
|
+
} = block;
|
|
104
|
+
if (!_key || !_type)
|
|
105
|
+
throw new Error("Not a valid block");
|
|
106
|
+
if (_type === textBlockType && "children" in block && Array.isArray(block.children) && _key) {
|
|
107
|
+
let hasInlines = !1;
|
|
108
|
+
const children = block.children.map((child) => {
|
|
109
|
+
const {
|
|
110
|
+
_type: _cType
|
|
111
|
+
} = child;
|
|
112
|
+
if ("value" in child && _cType !== "span") {
|
|
113
|
+
hasInlines = !0;
|
|
110
114
|
const {
|
|
111
|
-
|
|
115
|
+
value: v,
|
|
116
|
+
_key: k,
|
|
117
|
+
_type: t,
|
|
118
|
+
__inline: _i,
|
|
119
|
+
children: _c,
|
|
120
|
+
...rest
|
|
112
121
|
} = child;
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
...v,
|
|
126
|
-
_key: k,
|
|
127
|
-
_type: t
|
|
128
|
-
}, keyMap);
|
|
129
|
-
}
|
|
130
|
-
return child;
|
|
131
|
-
});
|
|
132
|
-
return hasInlines ? keepObjectEquality({
|
|
133
|
-
...block,
|
|
134
|
-
children,
|
|
135
|
-
_key,
|
|
136
|
-
_type
|
|
137
|
-
}, keyMap) : block;
|
|
138
|
-
}
|
|
139
|
-
const blockValue = "value" in block && block.value;
|
|
140
|
-
return keepObjectEquality({
|
|
122
|
+
return keepObjectEquality({
|
|
123
|
+
...rest,
|
|
124
|
+
...v,
|
|
125
|
+
_key: k,
|
|
126
|
+
_type: t
|
|
127
|
+
}, keyMap);
|
|
128
|
+
}
|
|
129
|
+
return child;
|
|
130
|
+
});
|
|
131
|
+
return hasInlines ? keepObjectEquality({
|
|
132
|
+
...block,
|
|
133
|
+
children,
|
|
141
134
|
_key,
|
|
142
|
-
_type
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
135
|
+
_type
|
|
136
|
+
}, keyMap) : block;
|
|
137
|
+
}
|
|
138
|
+
const blockValue = "value" in block && block.value;
|
|
139
|
+
return keepObjectEquality({
|
|
140
|
+
_key,
|
|
141
|
+
_type,
|
|
142
|
+
...typeof blockValue == "object" ? blockValue : {}
|
|
143
|
+
}, keyMap);
|
|
146
144
|
}
|
|
147
145
|
function isEqualToEmptyEditor(children, schemaTypes) {
|
|
148
146
|
return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && Element$1.isElement(children[0]) && children[0]._type === schemaTypes.block.name && "style" in children[0] && children[0].style === schemaTypes.styles[0].name && !("listItem" in children[0]) && Array.isArray(children[0].children) && children[0].children.length === 1 && Text.isText(children[0].children[0]) && children[0].children[0]._type === "span" && !children[0].children[0].marks?.join("") && children[0].children[0].text === "";
|
|
@@ -278,7 +276,7 @@ function elementToBlock({
|
|
|
278
276
|
schema,
|
|
279
277
|
element
|
|
280
278
|
}) {
|
|
281
|
-
return
|
|
279
|
+
return fromSlateBlock(element, schema.block.name);
|
|
282
280
|
}
|
|
283
281
|
function isBlockElement({
|
|
284
282
|
editor,
|
|
@@ -4305,7 +4303,7 @@ const debug$c = debugWithName("behavior.operation.history.redo"), historyRedoOpe
|
|
|
4305
4303
|
})?.[0];
|
|
4306
4304
|
if (!block)
|
|
4307
4305
|
throw new Error(`Unable to find block at ${JSON.stringify(operation.at)}`);
|
|
4308
|
-
const parsedBlock =
|
|
4306
|
+
const parsedBlock = fromSlateBlock(block, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(operation.editor));
|
|
4309
4307
|
if (!parsedBlock)
|
|
4310
4308
|
throw new Error(`Unable to parse block at ${JSON.stringify(operation.at)}`);
|
|
4311
4309
|
if (isTextBlock(context, parsedBlock)) {
|
|
@@ -5637,13 +5635,13 @@ function insertPatch(editor, patch, schema) {
|
|
|
5637
5635
|
const {
|
|
5638
5636
|
items: items2,
|
|
5639
5637
|
position: position2
|
|
5640
|
-
} = patch, blocksToInsert =
|
|
5638
|
+
} = patch, blocksToInsert = items2.map((item) => toSlateBlock(item, {
|
|
5641
5639
|
schemaTypes: schema
|
|
5642
|
-
}, KEY_TO_SLATE_ELEMENT.get(editor)), targetBlockIndex = block.index, normalizedIdx2 = position2 === "after" ? targetBlockIndex + 1 : targetBlockIndex, editorWasEmptyBefore = isEqualToEmptyEditor(editor.children, schema);
|
|
5640
|
+
}, KEY_TO_SLATE_ELEMENT.get(editor))), targetBlockIndex = block.index, normalizedIdx2 = position2 === "after" ? targetBlockIndex + 1 : targetBlockIndex, editorWasEmptyBefore = isEqualToEmptyEditor(editor.children, schema);
|
|
5643
5641
|
return Transforms.insertNodes(editor, blocksToInsert, {
|
|
5644
5642
|
at: [normalizedIdx2]
|
|
5645
5643
|
}), editorWasEmptyBefore && typeof patch.path[0] == "number" && patch.path[0] === 0 && Transforms.removeNodes(editor, {
|
|
5646
|
-
at: [position2 === "before" ? targetBlockIndex +
|
|
5644
|
+
at: [position2 === "before" ? targetBlockIndex + blocksToInsert.length : targetBlockIndex]
|
|
5647
5645
|
}), !0;
|
|
5648
5646
|
}
|
|
5649
5647
|
const {
|
|
@@ -5652,13 +5650,13 @@ function insertPatch(editor, patch, schema) {
|
|
|
5652
5650
|
} = patch, targetChild = findBlockChild(block, patch.path);
|
|
5653
5651
|
if (!targetChild)
|
|
5654
5652
|
return !1;
|
|
5655
|
-
const childrenToInsert =
|
|
5653
|
+
const childrenToInsert = toSlateBlock({
|
|
5656
5654
|
...block.node,
|
|
5657
5655
|
children: items
|
|
5658
|
-
}
|
|
5656
|
+
}, {
|
|
5659
5657
|
schemaTypes: schema
|
|
5660
5658
|
}, KEY_TO_SLATE_ELEMENT.get(editor)), normalizedIdx = position === "after" ? targetChild.index + 1 : targetChild.index, childInsertPath = [block.index, normalizedIdx];
|
|
5661
|
-
return childrenToInsert && Element$1.isElement(childrenToInsert
|
|
5659
|
+
return childrenToInsert && Element$1.isElement(childrenToInsert) && Transforms.insertNodes(editor, childrenToInsert.children, {
|
|
5662
5660
|
at: childInsertPath
|
|
5663
5661
|
}), !0;
|
|
5664
5662
|
}
|
|
@@ -6072,9 +6070,9 @@ function insertNodePatch(schema, children, operation, beforeValue) {
|
|
|
6072
6070
|
const block = beforeValue[operation.path[0]];
|
|
6073
6071
|
if (operation.path.length === 1) {
|
|
6074
6072
|
const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block?._key : beforeBlock?._key;
|
|
6075
|
-
return targetKey ? [insert([
|
|
6073
|
+
return targetKey ? [insert([fromSlateBlock(operation.node, schema.block.name)], position, [{
|
|
6076
6074
|
_key: targetKey
|
|
6077
|
-
}])] : [setIfMissing(beforeValue, []), insert([
|
|
6075
|
+
}])] : [setIfMissing(beforeValue, []), insert([fromSlateBlock(operation.node, schema.block.name)], "before", [operation.path[0]])];
|
|
6078
6076
|
} else if (isTextBlock({
|
|
6079
6077
|
schema
|
|
6080
6078
|
}, block) && operation.path.length === 2 && children[operation.path[0]]) {
|
|
@@ -6107,7 +6105,7 @@ function splitNodePatch(schema, children, operation, beforeValue) {
|
|
|
6107
6105
|
if (isTextBlock({
|
|
6108
6106
|
schema
|
|
6109
6107
|
}, oldBlock)) {
|
|
6110
|
-
const targetValue =
|
|
6108
|
+
const targetValue = fromSlateBlock(children[operation.path[0] + 1], schema.block.name);
|
|
6111
6109
|
targetValue && (patches.push(insert([targetValue], "after", [{
|
|
6112
6110
|
_key: splitBlock._key
|
|
6113
6111
|
}])), oldBlock.children.slice(operation.position).forEach((span) => {
|
|
@@ -6126,10 +6124,10 @@ function splitNodePatch(schema, children, operation, beforeValue) {
|
|
|
6126
6124
|
if (isSpan({
|
|
6127
6125
|
schema
|
|
6128
6126
|
}, splitSpan)) {
|
|
6129
|
-
const targetSpans =
|
|
6127
|
+
const targetSpans = fromSlateBlock({
|
|
6130
6128
|
...splitBlock,
|
|
6131
6129
|
children: splitBlock.children.slice(operation.path[1] + 1, operation.path[1] + 2)
|
|
6132
|
-
}
|
|
6130
|
+
}, schema.block.name).children;
|
|
6133
6131
|
patches.push(insert(targetSpans, "after", [{
|
|
6134
6132
|
_key: splitBlock._key
|
|
6135
6133
|
}, "children", {
|
|
@@ -6168,7 +6166,7 @@ function mergeNodePatch(schema, children, operation, beforeValue) {
|
|
|
6168
6166
|
const patches = [], block = beforeValue[operation.path[0]], updatedBlock = children[operation.path[0]];
|
|
6169
6167
|
if (operation.path.length === 1)
|
|
6170
6168
|
if (block?._key) {
|
|
6171
|
-
const newBlock =
|
|
6169
|
+
const newBlock = fromSlateBlock(children[operation.path[0] - 1], schema.block.name);
|
|
6172
6170
|
patches.push(set(newBlock, [{
|
|
6173
6171
|
_key: newBlock._key
|
|
6174
6172
|
}])), patches.push(unset([{
|
|
@@ -6206,7 +6204,7 @@ function moveNodePatch(schema, beforeValue, operation) {
|
|
|
6206
6204
|
const position = operation.path[0] > operation.newPath[0] ? "before" : "after";
|
|
6207
6205
|
patches.push(unset([{
|
|
6208
6206
|
_key: block._key
|
|
6209
|
-
}])), patches.push(insert([
|
|
6207
|
+
}])), patches.push(insert([fromSlateBlock(block, schema.block.name)], position, [{
|
|
6210
6208
|
_key: targetBlock._key
|
|
6211
6209
|
}]));
|
|
6212
6210
|
} else if (operation.path.length === 2 && isTextBlock({
|
|
@@ -6214,7 +6212,7 @@ function moveNodePatch(schema, beforeValue, operation) {
|
|
|
6214
6212
|
}, block) && isTextBlock({
|
|
6215
6213
|
schema
|
|
6216
6214
|
}, targetBlock)) {
|
|
6217
|
-
const child = block.children[operation.path[1]], targetChild = targetBlock.children[operation.newPath[1]], position = operation.newPath[1] === targetBlock.children.length ? "after" : "before", childToInsert =
|
|
6215
|
+
const child = block.children[operation.path[1]], targetChild = targetBlock.children[operation.newPath[1]], position = operation.newPath[1] === targetBlock.children.length ? "after" : "before", childToInsert = fromSlateBlock(block, schema.block.name).children[operation.path[1]];
|
|
6218
6216
|
patches.push(unset([{
|
|
6219
6217
|
_key: block._key
|
|
6220
6218
|
}, "children", {
|
|
@@ -6801,9 +6799,9 @@ function createSlateEditor(config) {
|
|
|
6801
6799
|
blockIndexMap: instance.blockIndexMap,
|
|
6802
6800
|
listIndexMap: instance.listIndexMap
|
|
6803
6801
|
});
|
|
6804
|
-
const initialValue =
|
|
6802
|
+
const initialValue = [toSlateBlock(placeholderBlock, {
|
|
6805
6803
|
schemaTypes: config.editorActor.getSnapshot().context.schema
|
|
6806
|
-
});
|
|
6804
|
+
})];
|
|
6807
6805
|
return {
|
|
6808
6806
|
instance,
|
|
6809
6807
|
initialValue
|
|
@@ -10931,14 +10929,14 @@ function createEditableAPI(editor, editorActor) {
|
|
|
10931
10929
|
if (editor.selection) {
|
|
10932
10930
|
const block = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
10933
10931
|
if (block)
|
|
10934
|
-
return
|
|
10932
|
+
return fromSlateBlock(block, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor));
|
|
10935
10933
|
}
|
|
10936
10934
|
},
|
|
10937
10935
|
focusChild: () => {
|
|
10938
10936
|
if (editor.selection) {
|
|
10939
10937
|
const block = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
10940
10938
|
if (block && editor.isTextBlock(block))
|
|
10941
|
-
return
|
|
10939
|
+
return fromSlateBlock(block, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)).children[editor.selection.focus.path[1]];
|
|
10942
10940
|
}
|
|
10943
10941
|
},
|
|
10944
10942
|
insertChild: (type, value) => (editorActor.send({
|
|
@@ -11015,10 +11013,10 @@ function createEditableAPI(editor, editorActor) {
|
|
|
11015
11013
|
const [block, blockPath] = Editor.node(editor, slatePath.focus.path.slice(0, 1));
|
|
11016
11014
|
if (block && blockPath && typeof block._key == "string") {
|
|
11017
11015
|
if (path.length === 1 && slatePath.focus.path.length === 1)
|
|
11018
|
-
return [
|
|
11016
|
+
return [fromSlateBlock(block, types.block.name), [{
|
|
11019
11017
|
_key: block._key
|
|
11020
11018
|
}]];
|
|
11021
|
-
const ptBlock =
|
|
11019
|
+
const ptBlock = fromSlateBlock(block, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor));
|
|
11022
11020
|
if (editor.isTextBlock(ptBlock)) {
|
|
11023
11021
|
const ptChild = ptBlock.children[slatePath.focus.path[1]];
|
|
11024
11022
|
if (ptChild)
|
|
@@ -11182,7 +11180,13 @@ function createEditableAPI(editor, editorActor) {
|
|
|
11182
11180
|
insertBreak: () => {
|
|
11183
11181
|
editor.insertBreak(), editor.onChange();
|
|
11184
11182
|
},
|
|
11185
|
-
getFragment: () =>
|
|
11183
|
+
getFragment: () => {
|
|
11184
|
+
const snapshot = getEditorSnapshot({
|
|
11185
|
+
editorActorSnapshot: editorActor.getSnapshot(),
|
|
11186
|
+
slateEditorInstance: editor
|
|
11187
|
+
});
|
|
11188
|
+
return getSelectedValue(snapshot);
|
|
11189
|
+
},
|
|
11186
11190
|
isSelectionsOverlapping: (selectionA, selectionB) => {
|
|
11187
11191
|
const rangeA = toSlateRange({
|
|
11188
11192
|
context: {
|
|
@@ -12805,7 +12809,7 @@ class PortableTextEditor extends Component {
|
|
|
12805
12809
|
* ```
|
|
12806
12810
|
* import * as selectors from '@portabletext/editor/selectors'
|
|
12807
12811
|
* const editor = useEditor()
|
|
12808
|
-
* const
|
|
12812
|
+
* const selectedValue = useEditorSelector(editor, selectors.getSelectedValue)
|
|
12809
12813
|
* ```
|
|
12810
12814
|
*/
|
|
12811
12815
|
static getFragment = (editor) => editor.editable?.getFragment();
|