@portabletext/editor 1.50.3 → 1.50.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/behaviors/index.d.cts +1 -0
- package/lib/behaviors/index.d.ts +1 -0
- package/lib/index.cjs +322 -55
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +2 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +323 -55
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.cts +1 -0
- package/lib/plugins/index.d.ts +1 -0
- package/lib/selectors/index.d.cts +1 -0
- package/lib/selectors/index.d.ts +1 -0
- package/lib/utils/index.d.cts +1 -0
- package/lib/utils/index.d.ts +1 -0
- package/package.json +2 -1
- package/src/editor/PortableTextEditor.tsx +22 -22
- package/src/editor/create-slate-editor.tsx +9 -1
- package/src/editor/editor-selector.ts +1 -5
- package/src/editor/editor-snapshot.ts +1 -3
- package/src/editor/plugins/slate-plugin.update-value.ts +30 -0
- package/src/editor/plugins/with-plugins.ts +8 -1
- package/src/internal-utils/apply-operation-to-portable-text.test.ts +175 -0
- package/src/internal-utils/apply-operation-to-portable-text.ts +435 -0
- package/src/internal-utils/create-placeholder-block.ts +20 -0
- package/src/internal-utils/portable-text-node.ts +209 -0
- package/src/types/editor.ts +1 -0
- package/src/internal-utils/__tests__/patchToOperations.test.ts +0 -312
- package/src/internal-utils/slate-children-to-blocks.ts +0 -49
package/lib/index.d.cts
CHANGED
|
@@ -3233,6 +3233,7 @@ export declare class PortableTextEditor extends Component<
|
|
|
3233
3233
|
private editor
|
|
3234
3234
|
private editable
|
|
3235
3235
|
private actors?
|
|
3236
|
+
private subscriptions
|
|
3236
3237
|
private unsubscribers
|
|
3237
3238
|
constructor(props: PortableTextEditorProps)
|
|
3238
3239
|
componentDidMount(): void
|
|
@@ -3741,6 +3742,7 @@ declare interface PortableTextSlateEditor extends ReactEditor {
|
|
|
3741
3742
|
isTextBlock: (value: unknown) => value is PortableTextTextBlock
|
|
3742
3743
|
isTextSpan: (value: unknown) => value is PortableTextSpan
|
|
3743
3744
|
isListBlock: (value: unknown) => value is PortableTextListBlock
|
|
3745
|
+
value: Array<PortableTextBlock>
|
|
3744
3746
|
/**
|
|
3745
3747
|
* Use hotkeys
|
|
3746
3748
|
*/
|
package/lib/index.d.ts
CHANGED
|
@@ -3233,6 +3233,7 @@ export declare class PortableTextEditor extends Component<
|
|
|
3233
3233
|
private editor
|
|
3234
3234
|
private editable
|
|
3235
3235
|
private actors?
|
|
3236
|
+
private subscriptions
|
|
3236
3237
|
private unsubscribers
|
|
3237
3238
|
constructor(props: PortableTextEditorProps)
|
|
3238
3239
|
componentDidMount(): void
|
|
@@ -3741,6 +3742,7 @@ declare interface PortableTextSlateEditor extends ReactEditor {
|
|
|
3741
3742
|
isTextBlock: (value: unknown) => value is PortableTextTextBlock
|
|
3742
3743
|
isTextSpan: (value: unknown) => value is PortableTextSpan
|
|
3743
3744
|
isListBlock: (value: unknown) => value is PortableTextListBlock
|
|
3745
|
+
value: Array<PortableTextBlock>
|
|
3744
3746
|
/**
|
|
3745
3747
|
* Use hotkeys
|
|
3746
3748
|
*/
|
package/lib/index.js
CHANGED
|
@@ -9,7 +9,7 @@ import { Element as Element$1, Text, Range, Editor, Node, Point, Transforms, Pat
|
|
|
9
9
|
import { useSlateStatic, useSelected, withReact, ReactEditor, Slate, useSlate, Editable } from "slate-react";
|
|
10
10
|
import debug$i from "debug";
|
|
11
11
|
import { getBlockEndPoint, isEmptyTextBlock, isEqualSelectionPoints } from "./_chunks-es/util.is-equal-selection-points.js";
|
|
12
|
-
import { getBlockStartPoint, isKeyedSegment as isKeyedSegment$1, parseInlineObject, parseTextBlock, parseBlockObject, parseBlock, sliceBlocks, isTextBlock, parseAnnotation, blockOffsetToSpanSelectionPoint, isSpan$1 as isSpan, isListBlock, getTextBlockText, parseBlocks } from "./_chunks-es/util.slice-blocks.js";
|
|
12
|
+
import { getBlockStartPoint, isKeyedSegment as isKeyedSegment$1, parseInlineObject, parseTextBlock, parseBlockObject, parseBlock, sliceBlocks, isTextBlock, parseAnnotation, blockOffsetToSpanSelectionPoint, isSpan$1 as isSpan, isListBlock, isTypedObject, getTextBlockText, parseBlocks } from "./_chunks-es/util.slice-blocks.js";
|
|
13
13
|
import { isSelectionCollapsed, getFocusTextBlock, getFocusSpan, getSelectedBlocks, isSelectionExpanded, getSelectionStartBlock as getSelectionStartBlock$1, getSelectionEndBlock as getSelectionEndBlock$1, getFocusBlock as getFocusBlock$1, getFocusBlockObject, getPreviousBlock, getNextBlock, getFirstBlock as getFirstBlock$1, getLastBlock as getLastBlock$1, getFocusListBlock } from "./_chunks-es/selector.is-selection-expanded.js";
|
|
14
14
|
import { getFocusInlineObject, isOverlappingSelection, isSelectingEntireBlocks, getTrimmedSelection, getCaretWordSelection, isAtTheEndOfBlock, isAtTheStartOfBlock, isActiveAnnotation, isActiveDecorator, getSelectedTextBlocks, isActiveListItem, isActiveStyle, getActiveAnnotations } from "./_chunks-es/selector.is-selecting-entire-blocks.js";
|
|
15
15
|
import { DOMEditor, isDOMNode } from "slate-dom";
|
|
@@ -31,6 +31,7 @@ import { applyAll, unset, insert, set, setIfMissing, diffMatchPatch as diffMatch
|
|
|
31
31
|
import get from "lodash/get.js";
|
|
32
32
|
import isUndefined from "lodash/isUndefined.js";
|
|
33
33
|
import omitBy from "lodash/omitBy.js";
|
|
34
|
+
import { createDraft, finishDraft } from "immer";
|
|
34
35
|
import startCase from "lodash.startcase";
|
|
35
36
|
import isPlainObject from "lodash/isPlainObject.js";
|
|
36
37
|
function EditorEventListener(props) {
|
|
@@ -1376,6 +1377,20 @@ function compileType(rawType) {
|
|
|
1376
1377
|
types: [rawType]
|
|
1377
1378
|
}).get(rawType.name);
|
|
1378
1379
|
}
|
|
1380
|
+
function createPlaceholderBlock(context) {
|
|
1381
|
+
return {
|
|
1382
|
+
_type: context.schema.block.name,
|
|
1383
|
+
_key: context.keyGenerator(),
|
|
1384
|
+
style: context.schema.styles[0].name ?? "normal",
|
|
1385
|
+
markDefs: [],
|
|
1386
|
+
children: [{
|
|
1387
|
+
_type: context.schema.span.name,
|
|
1388
|
+
_key: context.keyGenerator(),
|
|
1389
|
+
text: "",
|
|
1390
|
+
marks: []
|
|
1391
|
+
}]
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1379
1394
|
const insertTextOperationImplementation = ({
|
|
1380
1395
|
operation
|
|
1381
1396
|
}) => {
|
|
@@ -4744,6 +4759,289 @@ function createWithUtils({
|
|
|
4744
4759
|
})[0], editor;
|
|
4745
4760
|
};
|
|
4746
4761
|
}
|
|
4762
|
+
function isEditorNode(node) {
|
|
4763
|
+
return typeof node == "object" && node !== null ? !("_type" in node) && "children" in node && Array.isArray(node.children) : !1;
|
|
4764
|
+
}
|
|
4765
|
+
function isTextBlockNode(context, node) {
|
|
4766
|
+
return isTypedObject(node) && node._type === context.schema.block.name;
|
|
4767
|
+
}
|
|
4768
|
+
function isSpanNode(context, node) {
|
|
4769
|
+
return typeof node != "object" || node === null || "children" in node ? !1 : "_type" in node ? node._type === context.schema.span.name : "text" in node;
|
|
4770
|
+
}
|
|
4771
|
+
function isPartialSpanNode(node) {
|
|
4772
|
+
return typeof node == "object" && node !== null && "text" in node && typeof node.text == "string";
|
|
4773
|
+
}
|
|
4774
|
+
function isObjectNode(context, node) {
|
|
4775
|
+
return !isEditorNode(node) && !isTextBlockNode(context, node) && !isSpanNode(context, node) && !isPartialSpanNode(node);
|
|
4776
|
+
}
|
|
4777
|
+
function getBlock(root, path) {
|
|
4778
|
+
const index = path.at(0);
|
|
4779
|
+
if (!(index === void 0 || path.length !== 1))
|
|
4780
|
+
return root.children.at(index);
|
|
4781
|
+
}
|
|
4782
|
+
function getNode(context, root, path) {
|
|
4783
|
+
if (path.length === 0)
|
|
4784
|
+
return root;
|
|
4785
|
+
if (path.length === 1)
|
|
4786
|
+
return getBlock(root, path);
|
|
4787
|
+
if (path.length === 2) {
|
|
4788
|
+
const block = getBlock(root, path.slice(0, 1));
|
|
4789
|
+
return !block || !isTextBlockNode(context, block) ? void 0 : block.children.at(path[1]) || void 0;
|
|
4790
|
+
}
|
|
4791
|
+
}
|
|
4792
|
+
function getSpan(context, root, path) {
|
|
4793
|
+
const node = getNode(context, root, path);
|
|
4794
|
+
if (node && isSpanNode(context, node))
|
|
4795
|
+
return node;
|
|
4796
|
+
}
|
|
4797
|
+
function getParent(context, root, path) {
|
|
4798
|
+
if (path.length === 0)
|
|
4799
|
+
return;
|
|
4800
|
+
const parentPath = path.slice(0, -1);
|
|
4801
|
+
if (parentPath.length === 0)
|
|
4802
|
+
return root;
|
|
4803
|
+
const blockIndex = parentPath.at(0);
|
|
4804
|
+
if (blockIndex === void 0 || parentPath.length !== 1)
|
|
4805
|
+
return;
|
|
4806
|
+
const block = root.children.at(blockIndex);
|
|
4807
|
+
if (block && isTextBlockNode(context, block))
|
|
4808
|
+
return block;
|
|
4809
|
+
}
|
|
4810
|
+
function applyOperationToPortableText(context, value, operation) {
|
|
4811
|
+
const draft = createDraft({
|
|
4812
|
+
children: value
|
|
4813
|
+
});
|
|
4814
|
+
try {
|
|
4815
|
+
applyOperationToPortableTextDraft(context, draft, operation);
|
|
4816
|
+
} catch (e) {
|
|
4817
|
+
console.error(e);
|
|
4818
|
+
}
|
|
4819
|
+
return finishDraft(draft).children;
|
|
4820
|
+
}
|
|
4821
|
+
function applyOperationToPortableTextDraft(context, root, operation) {
|
|
4822
|
+
switch (operation.type) {
|
|
4823
|
+
case "insert_node": {
|
|
4824
|
+
const {
|
|
4825
|
+
path,
|
|
4826
|
+
node: insertedNode
|
|
4827
|
+
} = operation, parent = getParent(context, root, path), index = path[path.length - 1];
|
|
4828
|
+
if (!parent || index > parent.children.length)
|
|
4829
|
+
break;
|
|
4830
|
+
if (path.length === 1) {
|
|
4831
|
+
if (isTextBlockNode(context, insertedNode)) {
|
|
4832
|
+
parent.children.splice(index, 0, {
|
|
4833
|
+
...insertedNode,
|
|
4834
|
+
children: insertedNode.children.map((child) => "__inline" in child ? {
|
|
4835
|
+
_key: child._key,
|
|
4836
|
+
_type: child._type,
|
|
4837
|
+
..."value" in child && typeof child.value == "object" ? child.value : {}
|
|
4838
|
+
} : child)
|
|
4839
|
+
});
|
|
4840
|
+
break;
|
|
4841
|
+
}
|
|
4842
|
+
if (Element$1.isElement(insertedNode) && !("__inline" in insertedNode)) {
|
|
4843
|
+
parent.children.splice(index, 0, {
|
|
4844
|
+
_key: insertedNode._key,
|
|
4845
|
+
_type: insertedNode._type,
|
|
4846
|
+
..."value" in insertedNode && typeof insertedNode.value == "object" ? insertedNode.value : {}
|
|
4847
|
+
});
|
|
4848
|
+
break;
|
|
4849
|
+
}
|
|
4850
|
+
}
|
|
4851
|
+
if (path.length === 2) {
|
|
4852
|
+
if (!isTextBlockNode(context, parent))
|
|
4853
|
+
break;
|
|
4854
|
+
if (isPartialSpanNode(insertedNode)) {
|
|
4855
|
+
parent.children.splice(index, 0, insertedNode);
|
|
4856
|
+
break;
|
|
4857
|
+
}
|
|
4858
|
+
if ("__inline" in insertedNode) {
|
|
4859
|
+
parent.children.splice(index, 0, {
|
|
4860
|
+
_key: insertedNode._key,
|
|
4861
|
+
_type: insertedNode._type,
|
|
4862
|
+
..."value" in insertedNode && typeof insertedNode.value == "object" ? insertedNode.value : {}
|
|
4863
|
+
});
|
|
4864
|
+
break;
|
|
4865
|
+
}
|
|
4866
|
+
}
|
|
4867
|
+
break;
|
|
4868
|
+
}
|
|
4869
|
+
case "insert_text": {
|
|
4870
|
+
const {
|
|
4871
|
+
path,
|
|
4872
|
+
offset,
|
|
4873
|
+
text
|
|
4874
|
+
} = operation;
|
|
4875
|
+
if (text.length === 0) break;
|
|
4876
|
+
const span = getSpan(context, root, path);
|
|
4877
|
+
if (!span)
|
|
4878
|
+
break;
|
|
4879
|
+
const before = span.text.slice(0, offset), after = span.text.slice(offset);
|
|
4880
|
+
span.text = before + text + after;
|
|
4881
|
+
break;
|
|
4882
|
+
}
|
|
4883
|
+
case "merge_node": {
|
|
4884
|
+
const {
|
|
4885
|
+
path
|
|
4886
|
+
} = operation, node = getNode(context, root, path), prevPath = Path.previous(path), prev = getNode(context, root, prevPath), parent = getParent(context, root, path);
|
|
4887
|
+
if (!node || !prev || !parent)
|
|
4888
|
+
break;
|
|
4889
|
+
const index = path[path.length - 1];
|
|
4890
|
+
if (isPartialSpanNode(node) && isPartialSpanNode(prev))
|
|
4891
|
+
prev.text += node.text;
|
|
4892
|
+
else if (isTextBlockNode(context, node) && isTextBlockNode(context, prev))
|
|
4893
|
+
prev.children.push(...node.children);
|
|
4894
|
+
else
|
|
4895
|
+
break;
|
|
4896
|
+
parent.children.splice(index, 1);
|
|
4897
|
+
break;
|
|
4898
|
+
}
|
|
4899
|
+
case "move_node": {
|
|
4900
|
+
const {
|
|
4901
|
+
path,
|
|
4902
|
+
newPath
|
|
4903
|
+
} = operation;
|
|
4904
|
+
if (Path.isAncestor(path, newPath))
|
|
4905
|
+
break;
|
|
4906
|
+
const node = getNode(context, root, path), parent = getParent(context, root, path), index = path[path.length - 1];
|
|
4907
|
+
if (!node || !parent)
|
|
4908
|
+
break;
|
|
4909
|
+
parent.children.splice(index, 1);
|
|
4910
|
+
const truePath = Path.transform(path, operation), newParent = getNode(context, root, Path.parent(truePath)), newIndex = truePath[truePath.length - 1];
|
|
4911
|
+
if (!newParent || !("children" in newParent) || !Array.isArray(newParent.children))
|
|
4912
|
+
break;
|
|
4913
|
+
newParent.children.splice(newIndex, 0, node);
|
|
4914
|
+
break;
|
|
4915
|
+
}
|
|
4916
|
+
case "remove_node": {
|
|
4917
|
+
const {
|
|
4918
|
+
path
|
|
4919
|
+
} = operation, index = path[path.length - 1];
|
|
4920
|
+
getParent(context, root, path)?.children.splice(index, 1);
|
|
4921
|
+
break;
|
|
4922
|
+
}
|
|
4923
|
+
case "remove_text": {
|
|
4924
|
+
const {
|
|
4925
|
+
path,
|
|
4926
|
+
offset,
|
|
4927
|
+
text
|
|
4928
|
+
} = operation;
|
|
4929
|
+
if (text.length === 0)
|
|
4930
|
+
break;
|
|
4931
|
+
const span = getSpan(context, root, path);
|
|
4932
|
+
if (!span)
|
|
4933
|
+
break;
|
|
4934
|
+
const before = span.text.slice(0, offset), after = span.text.slice(offset + text.length);
|
|
4935
|
+
span.text = before + after;
|
|
4936
|
+
break;
|
|
4937
|
+
}
|
|
4938
|
+
case "set_node": {
|
|
4939
|
+
const {
|
|
4940
|
+
path,
|
|
4941
|
+
properties,
|
|
4942
|
+
newProperties
|
|
4943
|
+
} = operation, node = getNode(context, root, path);
|
|
4944
|
+
if (!node || isEditorNode(node))
|
|
4945
|
+
break;
|
|
4946
|
+
if (isObjectNode(context, node)) {
|
|
4947
|
+
const valueBefore = "value" in properties && typeof properties.value == "object" ? properties.value : {}, valueAfter = "value" in newProperties && typeof newProperties.value == "object" ? newProperties.value : {};
|
|
4948
|
+
for (const key in newProperties) {
|
|
4949
|
+
if (key === "value")
|
|
4950
|
+
continue;
|
|
4951
|
+
const value = newProperties[key];
|
|
4952
|
+
value == null ? delete node[key] : node[key] = value;
|
|
4953
|
+
}
|
|
4954
|
+
for (const key in properties)
|
|
4955
|
+
key !== "value" && (newProperties.hasOwnProperty(key) || delete node[key]);
|
|
4956
|
+
for (const key in valueAfter) {
|
|
4957
|
+
const value = valueAfter[key];
|
|
4958
|
+
value == null ? delete node[key] : node[key] = value;
|
|
4959
|
+
}
|
|
4960
|
+
for (const key in valueBefore)
|
|
4961
|
+
valueAfter.hasOwnProperty(key) || delete node[key];
|
|
4962
|
+
break;
|
|
4963
|
+
}
|
|
4964
|
+
if (isTextBlockNode(context, node)) {
|
|
4965
|
+
for (const key in newProperties) {
|
|
4966
|
+
if (key === "children" || key === "text")
|
|
4967
|
+
break;
|
|
4968
|
+
const value = newProperties[key];
|
|
4969
|
+
value == null ? delete node[key] : node[key] = value;
|
|
4970
|
+
}
|
|
4971
|
+
for (const key in properties)
|
|
4972
|
+
newProperties.hasOwnProperty(key) || delete node[key];
|
|
4973
|
+
break;
|
|
4974
|
+
}
|
|
4975
|
+
if (isPartialSpanNode(node)) {
|
|
4976
|
+
for (const key in newProperties) {
|
|
4977
|
+
if (key === "text")
|
|
4978
|
+
break;
|
|
4979
|
+
const value = newProperties[key];
|
|
4980
|
+
value == null ? delete node[key] : node[key] = value;
|
|
4981
|
+
}
|
|
4982
|
+
for (const key in properties)
|
|
4983
|
+
newProperties.hasOwnProperty(key) || delete node[key];
|
|
4984
|
+
break;
|
|
4985
|
+
}
|
|
4986
|
+
break;
|
|
4987
|
+
}
|
|
4988
|
+
case "split_node": {
|
|
4989
|
+
const {
|
|
4990
|
+
path,
|
|
4991
|
+
position,
|
|
4992
|
+
properties
|
|
4993
|
+
} = operation;
|
|
4994
|
+
if (path.length === 0)
|
|
4995
|
+
break;
|
|
4996
|
+
const parent = getParent(context, root, path), index = path[path.length - 1];
|
|
4997
|
+
if (!parent)
|
|
4998
|
+
break;
|
|
4999
|
+
if (isEditorNode(parent)) {
|
|
5000
|
+
const block = getBlock(root, path);
|
|
5001
|
+
if (!block || !isTextBlockNode(context, block))
|
|
5002
|
+
break;
|
|
5003
|
+
const before = block.children.slice(0, position), after = block.children.slice(position);
|
|
5004
|
+
block.children = before;
|
|
5005
|
+
const newTextBlockNode = {
|
|
5006
|
+
...properties,
|
|
5007
|
+
children: after,
|
|
5008
|
+
_type: context.schema.block.name
|
|
5009
|
+
};
|
|
5010
|
+
parent.children.splice(index + 1, 0, newTextBlockNode);
|
|
5011
|
+
break;
|
|
5012
|
+
}
|
|
5013
|
+
if (isTextBlockNode(context, parent)) {
|
|
5014
|
+
const node = getNode(context, root, path);
|
|
5015
|
+
if (!node || !isSpanNode(context, node))
|
|
5016
|
+
break;
|
|
5017
|
+
const before = node.text.slice(0, position), after = node.text.slice(position);
|
|
5018
|
+
node.text = before;
|
|
5019
|
+
const newSpanNode = {
|
|
5020
|
+
...properties,
|
|
5021
|
+
text: after
|
|
5022
|
+
};
|
|
5023
|
+
parent.children.splice(index + 1, 0, newSpanNode);
|
|
5024
|
+
}
|
|
5025
|
+
break;
|
|
5026
|
+
}
|
|
5027
|
+
}
|
|
5028
|
+
return root;
|
|
5029
|
+
}
|
|
5030
|
+
function pluginUpdateValue(context, editor) {
|
|
5031
|
+
const {
|
|
5032
|
+
apply: apply2
|
|
5033
|
+
} = editor;
|
|
5034
|
+
return editor.apply = (operation) => {
|
|
5035
|
+
if (operation.type === "set_selection") {
|
|
5036
|
+
apply2(operation);
|
|
5037
|
+
return;
|
|
5038
|
+
}
|
|
5039
|
+
editor.value = applyOperationToPortableText({
|
|
5040
|
+
keyGenerator: context.keyGenerator,
|
|
5041
|
+
schema: context.schema
|
|
5042
|
+
}, editor.value, operation), apply2(operation);
|
|
5043
|
+
}, editor;
|
|
5044
|
+
}
|
|
4747
5045
|
const withPlugins = (editor, options) => {
|
|
4748
5046
|
const e = editor, {
|
|
4749
5047
|
editorActor,
|
|
@@ -4760,7 +5058,7 @@ const withPlugins = (editor, options) => {
|
|
|
4760
5058
|
}), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor), withPortableTextBlockStyle = createWithPortableTextBlockStyle(editorActor), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
|
|
4761
5059
|
editorActor
|
|
4762
5060
|
}), withPortableTextSelections = createWithPortableTextSelections(editorActor);
|
|
4763
|
-
return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e)))))))))));
|
|
5061
|
+
return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(pluginUpdateValue(editorActor.getSnapshot().context, e))))))))))));
|
|
4764
5062
|
}, debug$a = debugWithName("setup");
|
|
4765
5063
|
function createSlateEditor(config) {
|
|
4766
5064
|
debug$a("Creating new Slate editor instance");
|
|
@@ -4769,10 +5067,10 @@ function createSlateEditor(config) {
|
|
|
4769
5067
|
relayActor: config.relayActor,
|
|
4770
5068
|
subscriptions: config.subscriptions
|
|
4771
5069
|
});
|
|
4772
|
-
KEY_TO_VALUE_ELEMENT.set(instance, {}), KEY_TO_SLATE_ELEMENT.set(instance, {});
|
|
4773
|
-
const initialValue =
|
|
4774
|
-
|
|
4775
|
-
})
|
|
5070
|
+
KEY_TO_VALUE_ELEMENT.set(instance, {}), KEY_TO_SLATE_ELEMENT.set(instance, {}), instance.value = [createPlaceholderBlock(config.editorActor.getSnapshot().context)];
|
|
5071
|
+
const initialValue = toSlateValue(instance.value, {
|
|
5072
|
+
schemaTypes: config.editorActor.getSnapshot().context.schema
|
|
5073
|
+
});
|
|
4776
5074
|
return {
|
|
4777
5075
|
instance,
|
|
4778
5076
|
initialValue
|
|
@@ -6564,34 +6862,6 @@ function sortByPriority(items) {
|
|
|
6564
6862
|
result.includes(item) || result.push(item);
|
|
6565
6863
|
return [...result, ...itemsWithoutPriority];
|
|
6566
6864
|
}
|
|
6567
|
-
function slateChildrenToBlocks(schema, value) {
|
|
6568
|
-
const blocks = new Array(value.length);
|
|
6569
|
-
for (let blockIndex = 0; blockIndex < value.length; blockIndex++) {
|
|
6570
|
-
const descendant = value[blockIndex];
|
|
6571
|
-
if (descendant._type !== schema.block.name) {
|
|
6572
|
-
blocks[blockIndex] = {
|
|
6573
|
-
_key: descendant._key,
|
|
6574
|
-
_type: descendant._type,
|
|
6575
|
-
..."value" in descendant && typeof descendant.value == "object" ? descendant.value : {}
|
|
6576
|
-
};
|
|
6577
|
-
continue;
|
|
6578
|
-
}
|
|
6579
|
-
const children = "children" in descendant ? descendant.children : [], processedChildren = new Array(children.length);
|
|
6580
|
-
for (let childIndex = 0; childIndex < children.length; childIndex++) {
|
|
6581
|
-
const child = children[childIndex];
|
|
6582
|
-
processedChildren[childIndex] = child._type === schema.span.name ? child : {
|
|
6583
|
-
_key: child._key,
|
|
6584
|
-
_type: child._type,
|
|
6585
|
-
..."value" in child && typeof child.value == "object" ? child.value : {}
|
|
6586
|
-
};
|
|
6587
|
-
}
|
|
6588
|
-
blocks[blockIndex] = {
|
|
6589
|
-
...descendant,
|
|
6590
|
-
children: processedChildren
|
|
6591
|
-
};
|
|
6592
|
-
}
|
|
6593
|
-
return blocks;
|
|
6594
|
-
}
|
|
6595
6865
|
function getActiveDecorators({
|
|
6596
6866
|
schema,
|
|
6597
6867
|
slateEditorInstance
|
|
@@ -6610,7 +6880,7 @@ function createEditorSnapshot({
|
|
|
6610
6880
|
hasTag,
|
|
6611
6881
|
internalDrag
|
|
6612
6882
|
}) {
|
|
6613
|
-
const
|
|
6883
|
+
const selection = editor.selection ? slateRangeToSelection({
|
|
6614
6884
|
schema,
|
|
6615
6885
|
editor,
|
|
6616
6886
|
range: editor.selection
|
|
@@ -6626,7 +6896,7 @@ function createEditorSnapshot({
|
|
|
6626
6896
|
readOnly,
|
|
6627
6897
|
schema,
|
|
6628
6898
|
selection,
|
|
6629
|
-
value
|
|
6899
|
+
value: editor.value
|
|
6630
6900
|
},
|
|
6631
6901
|
beta: {
|
|
6632
6902
|
hasTag,
|
|
@@ -7342,7 +7612,7 @@ function getEditorSnapshot({
|
|
|
7342
7612
|
}),
|
|
7343
7613
|
schema: editorActorSnapshot.context.schema,
|
|
7344
7614
|
selection: editorActorSnapshot.context.selection,
|
|
7345
|
-
value:
|
|
7615
|
+
value: slateEditorInstance.value
|
|
7346
7616
|
},
|
|
7347
7617
|
beta: {
|
|
7348
7618
|
hasTag: (tag) => editorActorSnapshot.hasTag(tag),
|
|
@@ -7729,8 +7999,8 @@ function createEditableAPI(editor, editorActor) {
|
|
|
7729
7999
|
}]
|
|
7730
8000
|
}], {
|
|
7731
8001
|
schemaTypes: editorActor.getSnapshot().context.schema
|
|
7732
|
-
})[0].children[0], focusChildPath = editor.selection.focus.path.slice(0, 2),
|
|
7733
|
-
return
|
|
8002
|
+
})[0].children[0], focusChildPath = editor.selection.focus.path.slice(0, 2), isSpanNode2 = child._type === types.span.name, focusNode = Node.get(editor, focusChildPath);
|
|
8003
|
+
return isSpanNode2 && focusNode._type !== types.span.name && (debug$6("Inserting span child next to inline object child, moving selection + 1"), editor.move({
|
|
7734
8004
|
distance: 1,
|
|
7735
8005
|
unit: "character"
|
|
7736
8006
|
})), Transforms.insertNodes(editor, child, {
|
|
@@ -8770,8 +9040,8 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
|
|
|
8770
9040
|
debug$5("Updating changed child", currentBlockChild, oldBlockChild), Transforms.setNodes(slateEditor, currentBlockChild, {
|
|
8771
9041
|
at: path
|
|
8772
9042
|
});
|
|
8773
|
-
const
|
|
8774
|
-
|
|
9043
|
+
const isSpanNode2 = Text.isText(currentBlockChild) && currentBlockChild._type === "span" && Text.isText(oldBlockChild) && oldBlockChild._type === "span";
|
|
9044
|
+
isSpanNode2 && isTextChanged ? (oldBlockChild.text.length > 0 && Transforms.delete(slateEditor, {
|
|
8775
9045
|
at: {
|
|
8776
9046
|
focus: {
|
|
8777
9047
|
path,
|
|
@@ -8784,7 +9054,7 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
|
|
|
8784
9054
|
}
|
|
8785
9055
|
}), Transforms.insertText(slateEditor, currentBlockChild.text, {
|
|
8786
9056
|
at: path
|
|
8787
|
-
}), slateEditor.onChange()) :
|
|
9057
|
+
}), slateEditor.onChange()) : isSpanNode2 || (debug$5("Updating changed inline object child", currentBlockChild), Transforms.setNodes(slateEditor, {
|
|
8788
9058
|
_key: VOID_CHILD_KEY
|
|
8789
9059
|
}, {
|
|
8790
9060
|
at: [...path, 0],
|
|
@@ -9154,6 +9424,7 @@ class PortableTextEditor extends Component {
|
|
|
9154
9424
|
/*
|
|
9155
9425
|
* The editor API (currently implemented with Slate).
|
|
9156
9426
|
*/
|
|
9427
|
+
subscriptions = [];
|
|
9157
9428
|
unsubscribers = [];
|
|
9158
9429
|
constructor(props) {
|
|
9159
9430
|
if (super(props), props.editor)
|
|
@@ -9170,23 +9441,20 @@ class PortableTextEditor extends Component {
|
|
|
9170
9441
|
readOnly: props.readOnly,
|
|
9171
9442
|
schema: props.schemaType
|
|
9172
9443
|
});
|
|
9173
|
-
this.
|
|
9174
|
-
const subscription = actors.relayActor.on("*", (event) => {
|
|
9175
|
-
const change = eventToChange(event);
|
|
9176
|
-
change && (props.onChange(change), this.change$.next(change));
|
|
9177
|
-
});
|
|
9178
|
-
return () => {
|
|
9179
|
-
subscription.unsubscribe();
|
|
9180
|
-
};
|
|
9181
|
-
})());
|
|
9182
|
-
for (const subscription of subscriptions)
|
|
9183
|
-
this.unsubscribers.push(subscription());
|
|
9184
|
-
this.actors = actors, this.editor = editor, this.schemaTypes = actors.editorActor.getSnapshot().context.getLegacySchema();
|
|
9444
|
+
this.subscriptions = subscriptions, this.actors = actors, this.editor = editor, this.schemaTypes = actors.editorActor.getSnapshot().context.getLegacySchema();
|
|
9185
9445
|
}
|
|
9186
9446
|
this.editable = this.editor._internal.editable;
|
|
9187
9447
|
}
|
|
9188
9448
|
componentDidMount() {
|
|
9189
|
-
|
|
9449
|
+
if (!this.actors)
|
|
9450
|
+
return;
|
|
9451
|
+
for (const subscription of this.subscriptions)
|
|
9452
|
+
this.unsubscribers.push(subscription());
|
|
9453
|
+
const relayActorSubscription = this.actors.relayActor.on("*", (event) => {
|
|
9454
|
+
const change = eventToChange(event);
|
|
9455
|
+
change && (this.props.editor || this.props.onChange(change), this.change$.next(change));
|
|
9456
|
+
});
|
|
9457
|
+
this.unsubscribers.push(relayActorSubscription.unsubscribe), this.actors.editorActor.start(), this.actors.mutationActor.start(), this.actors.relayActor.start(), this.actors.syncActor.start();
|
|
9190
9458
|
}
|
|
9191
9459
|
componentDidUpdate(prevProps) {
|
|
9192
9460
|
!this.props.editor && !prevProps.editor && this.props.schemaType !== prevProps.schemaType && console.warn("Updating schema type is no longer supported"), !this.props.editor && !prevProps.editor && (this.props.readOnly !== prevProps.readOnly && this.editor._internal.editorActor.send({
|