@portabletext/editor 3.0.3 → 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 +74 -74
- 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.ts +46 -51
- 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 = {}) {
|
|
@@ -101,52 +94,53 @@ function toSlateBlock(block, {
|
|
|
101
94
|
}, keyMap);
|
|
102
95
|
}
|
|
103
96
|
function fromSlateValue(value, textBlockType, keyMap = {}) {
|
|
104
|
-
return value.map((block) =>
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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;
|
|
114
114
|
const {
|
|
115
|
-
|
|
115
|
+
value: v,
|
|
116
|
+
_key: k,
|
|
117
|
+
_type: t,
|
|
118
|
+
__inline: _i,
|
|
119
|
+
children: _c,
|
|
120
|
+
...rest
|
|
116
121
|
} = child;
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
...v,
|
|
130
|
-
_key: k,
|
|
131
|
-
_type: t
|
|
132
|
-
}, keyMap);
|
|
133
|
-
}
|
|
134
|
-
return child;
|
|
135
|
-
});
|
|
136
|
-
return hasInlines ? keepObjectEquality({
|
|
137
|
-
...block,
|
|
138
|
-
children,
|
|
139
|
-
_key,
|
|
140
|
-
_type
|
|
141
|
-
}, keyMap) : block;
|
|
142
|
-
}
|
|
143
|
-
const blockValue = "value" in block && block.value;
|
|
144
|
-
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,
|
|
145
134
|
_key,
|
|
146
|
-
_type
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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);
|
|
150
144
|
}
|
|
151
145
|
function isEqualToEmptyEditor(children, schemaTypes) {
|
|
152
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 === "";
|
|
@@ -282,7 +276,7 @@ function elementToBlock({
|
|
|
282
276
|
schema,
|
|
283
277
|
element
|
|
284
278
|
}) {
|
|
285
|
-
return
|
|
279
|
+
return fromSlateBlock(element, schema.block.name);
|
|
286
280
|
}
|
|
287
281
|
function isBlockElement({
|
|
288
282
|
editor,
|
|
@@ -4309,7 +4303,7 @@ const debug$c = debugWithName("behavior.operation.history.redo"), historyRedoOpe
|
|
|
4309
4303
|
})?.[0];
|
|
4310
4304
|
if (!block)
|
|
4311
4305
|
throw new Error(`Unable to find block at ${JSON.stringify(operation.at)}`);
|
|
4312
|
-
const parsedBlock =
|
|
4306
|
+
const parsedBlock = fromSlateBlock(block, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(operation.editor));
|
|
4313
4307
|
if (!parsedBlock)
|
|
4314
4308
|
throw new Error(`Unable to parse block at ${JSON.stringify(operation.at)}`);
|
|
4315
4309
|
if (isTextBlock(context, parsedBlock)) {
|
|
@@ -5641,13 +5635,13 @@ function insertPatch(editor, patch, schema) {
|
|
|
5641
5635
|
const {
|
|
5642
5636
|
items: items2,
|
|
5643
5637
|
position: position2
|
|
5644
|
-
} = patch, blocksToInsert =
|
|
5638
|
+
} = patch, blocksToInsert = items2.map((item) => toSlateBlock(item, {
|
|
5645
5639
|
schemaTypes: schema
|
|
5646
|
-
}, 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);
|
|
5647
5641
|
return Transforms.insertNodes(editor, blocksToInsert, {
|
|
5648
5642
|
at: [normalizedIdx2]
|
|
5649
5643
|
}), editorWasEmptyBefore && typeof patch.path[0] == "number" && patch.path[0] === 0 && Transforms.removeNodes(editor, {
|
|
5650
|
-
at: [position2 === "before" ? targetBlockIndex +
|
|
5644
|
+
at: [position2 === "before" ? targetBlockIndex + blocksToInsert.length : targetBlockIndex]
|
|
5651
5645
|
}), !0;
|
|
5652
5646
|
}
|
|
5653
5647
|
const {
|
|
@@ -5656,13 +5650,13 @@ function insertPatch(editor, patch, schema) {
|
|
|
5656
5650
|
} = patch, targetChild = findBlockChild(block, patch.path);
|
|
5657
5651
|
if (!targetChild)
|
|
5658
5652
|
return !1;
|
|
5659
|
-
const childrenToInsert =
|
|
5653
|
+
const childrenToInsert = toSlateBlock({
|
|
5660
5654
|
...block.node,
|
|
5661
5655
|
children: items
|
|
5662
|
-
}
|
|
5656
|
+
}, {
|
|
5663
5657
|
schemaTypes: schema
|
|
5664
5658
|
}, KEY_TO_SLATE_ELEMENT.get(editor)), normalizedIdx = position === "after" ? targetChild.index + 1 : targetChild.index, childInsertPath = [block.index, normalizedIdx];
|
|
5665
|
-
return childrenToInsert && Element$1.isElement(childrenToInsert
|
|
5659
|
+
return childrenToInsert && Element$1.isElement(childrenToInsert) && Transforms.insertNodes(editor, childrenToInsert.children, {
|
|
5666
5660
|
at: childInsertPath
|
|
5667
5661
|
}), !0;
|
|
5668
5662
|
}
|
|
@@ -6076,9 +6070,9 @@ function insertNodePatch(schema, children, operation, beforeValue) {
|
|
|
6076
6070
|
const block = beforeValue[operation.path[0]];
|
|
6077
6071
|
if (operation.path.length === 1) {
|
|
6078
6072
|
const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block?._key : beforeBlock?._key;
|
|
6079
|
-
return targetKey ? [insert([
|
|
6073
|
+
return targetKey ? [insert([fromSlateBlock(operation.node, schema.block.name)], position, [{
|
|
6080
6074
|
_key: targetKey
|
|
6081
|
-
}])] : [setIfMissing(beforeValue, []), insert([
|
|
6075
|
+
}])] : [setIfMissing(beforeValue, []), insert([fromSlateBlock(operation.node, schema.block.name)], "before", [operation.path[0]])];
|
|
6082
6076
|
} else if (isTextBlock({
|
|
6083
6077
|
schema
|
|
6084
6078
|
}, block) && operation.path.length === 2 && children[operation.path[0]]) {
|
|
@@ -6111,7 +6105,7 @@ function splitNodePatch(schema, children, operation, beforeValue) {
|
|
|
6111
6105
|
if (isTextBlock({
|
|
6112
6106
|
schema
|
|
6113
6107
|
}, oldBlock)) {
|
|
6114
|
-
const targetValue =
|
|
6108
|
+
const targetValue = fromSlateBlock(children[operation.path[0] + 1], schema.block.name);
|
|
6115
6109
|
targetValue && (patches.push(insert([targetValue], "after", [{
|
|
6116
6110
|
_key: splitBlock._key
|
|
6117
6111
|
}])), oldBlock.children.slice(operation.position).forEach((span) => {
|
|
@@ -6130,10 +6124,10 @@ function splitNodePatch(schema, children, operation, beforeValue) {
|
|
|
6130
6124
|
if (isSpan({
|
|
6131
6125
|
schema
|
|
6132
6126
|
}, splitSpan)) {
|
|
6133
|
-
const targetSpans =
|
|
6127
|
+
const targetSpans = fromSlateBlock({
|
|
6134
6128
|
...splitBlock,
|
|
6135
6129
|
children: splitBlock.children.slice(operation.path[1] + 1, operation.path[1] + 2)
|
|
6136
|
-
}
|
|
6130
|
+
}, schema.block.name).children;
|
|
6137
6131
|
patches.push(insert(targetSpans, "after", [{
|
|
6138
6132
|
_key: splitBlock._key
|
|
6139
6133
|
}, "children", {
|
|
@@ -6172,7 +6166,7 @@ function mergeNodePatch(schema, children, operation, beforeValue) {
|
|
|
6172
6166
|
const patches = [], block = beforeValue[operation.path[0]], updatedBlock = children[operation.path[0]];
|
|
6173
6167
|
if (operation.path.length === 1)
|
|
6174
6168
|
if (block?._key) {
|
|
6175
|
-
const newBlock =
|
|
6169
|
+
const newBlock = fromSlateBlock(children[operation.path[0] - 1], schema.block.name);
|
|
6176
6170
|
patches.push(set(newBlock, [{
|
|
6177
6171
|
_key: newBlock._key
|
|
6178
6172
|
}])), patches.push(unset([{
|
|
@@ -6210,7 +6204,7 @@ function moveNodePatch(schema, beforeValue, operation) {
|
|
|
6210
6204
|
const position = operation.path[0] > operation.newPath[0] ? "before" : "after";
|
|
6211
6205
|
patches.push(unset([{
|
|
6212
6206
|
_key: block._key
|
|
6213
|
-
}])), patches.push(insert([
|
|
6207
|
+
}])), patches.push(insert([fromSlateBlock(block, schema.block.name)], position, [{
|
|
6214
6208
|
_key: targetBlock._key
|
|
6215
6209
|
}]));
|
|
6216
6210
|
} else if (operation.path.length === 2 && isTextBlock({
|
|
@@ -6218,7 +6212,7 @@ function moveNodePatch(schema, beforeValue, operation) {
|
|
|
6218
6212
|
}, block) && isTextBlock({
|
|
6219
6213
|
schema
|
|
6220
6214
|
}, targetBlock)) {
|
|
6221
|
-
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]];
|
|
6222
6216
|
patches.push(unset([{
|
|
6223
6217
|
_key: block._key
|
|
6224
6218
|
}, "children", {
|
|
@@ -6805,9 +6799,9 @@ function createSlateEditor(config) {
|
|
|
6805
6799
|
blockIndexMap: instance.blockIndexMap,
|
|
6806
6800
|
listIndexMap: instance.listIndexMap
|
|
6807
6801
|
});
|
|
6808
|
-
const initialValue =
|
|
6802
|
+
const initialValue = [toSlateBlock(placeholderBlock, {
|
|
6809
6803
|
schemaTypes: config.editorActor.getSnapshot().context.schema
|
|
6810
|
-
});
|
|
6804
|
+
})];
|
|
6811
6805
|
return {
|
|
6812
6806
|
instance,
|
|
6813
6807
|
initialValue
|
|
@@ -10935,14 +10929,14 @@ function createEditableAPI(editor, editorActor) {
|
|
|
10935
10929
|
if (editor.selection) {
|
|
10936
10930
|
const block = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
10937
10931
|
if (block)
|
|
10938
|
-
return
|
|
10932
|
+
return fromSlateBlock(block, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor));
|
|
10939
10933
|
}
|
|
10940
10934
|
},
|
|
10941
10935
|
focusChild: () => {
|
|
10942
10936
|
if (editor.selection) {
|
|
10943
10937
|
const block = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
10944
10938
|
if (block && editor.isTextBlock(block))
|
|
10945
|
-
return
|
|
10939
|
+
return fromSlateBlock(block, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)).children[editor.selection.focus.path[1]];
|
|
10946
10940
|
}
|
|
10947
10941
|
},
|
|
10948
10942
|
insertChild: (type, value) => (editorActor.send({
|
|
@@ -11019,10 +11013,10 @@ function createEditableAPI(editor, editorActor) {
|
|
|
11019
11013
|
const [block, blockPath] = Editor.node(editor, slatePath.focus.path.slice(0, 1));
|
|
11020
11014
|
if (block && blockPath && typeof block._key == "string") {
|
|
11021
11015
|
if (path.length === 1 && slatePath.focus.path.length === 1)
|
|
11022
|
-
return [
|
|
11016
|
+
return [fromSlateBlock(block, types.block.name), [{
|
|
11023
11017
|
_key: block._key
|
|
11024
11018
|
}]];
|
|
11025
|
-
const ptBlock =
|
|
11019
|
+
const ptBlock = fromSlateBlock(block, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor));
|
|
11026
11020
|
if (editor.isTextBlock(ptBlock)) {
|
|
11027
11021
|
const ptChild = ptBlock.children[slatePath.focus.path[1]];
|
|
11028
11022
|
if (ptChild)
|
|
@@ -11186,7 +11180,13 @@ function createEditableAPI(editor, editorActor) {
|
|
|
11186
11180
|
insertBreak: () => {
|
|
11187
11181
|
editor.insertBreak(), editor.onChange();
|
|
11188
11182
|
},
|
|
11189
|
-
getFragment: () =>
|
|
11183
|
+
getFragment: () => {
|
|
11184
|
+
const snapshot = getEditorSnapshot({
|
|
11185
|
+
editorActorSnapshot: editorActor.getSnapshot(),
|
|
11186
|
+
slateEditorInstance: editor
|
|
11187
|
+
});
|
|
11188
|
+
return getSelectedValue(snapshot);
|
|
11189
|
+
},
|
|
11190
11190
|
isSelectionsOverlapping: (selectionA, selectionB) => {
|
|
11191
11191
|
const rangeA = toSlateRange({
|
|
11192
11192
|
context: {
|
|
@@ -12809,7 +12809,7 @@ class PortableTextEditor extends Component {
|
|
|
12809
12809
|
* ```
|
|
12810
12810
|
* import * as selectors from '@portabletext/editor/selectors'
|
|
12811
12811
|
* const editor = useEditor()
|
|
12812
|
-
* const
|
|
12812
|
+
* const selectedValue = useEditorSelector(editor, selectors.getSelectedValue)
|
|
12813
12813
|
* ```
|
|
12814
12814
|
*/
|
|
12815
12815
|
static getFragment = (editor) => editor.editable?.getFragment();
|