@portabletext/editor 1.1.4 → 1.1.5
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.d.mts +228 -30
- package/lib/index.d.ts +228 -30
- package/lib/index.esm.js +88 -100
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +88 -100
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +88 -100
- package/lib/index.mjs.map +1 -1
- package/package.json +24 -19
- package/src/editor/Editable.tsx +6 -11
- package/src/editor/PortableTextEditor.tsx +26 -30
- package/src/editor/__tests__/self-solving.test.tsx +1 -1
- package/src/editor/components/SlateContainer.tsx +2 -13
- package/src/editor/components/Synchronizer.tsx +0 -3
- package/src/editor/editor-machine.ts +7 -2
- package/src/editor/hooks/useSyncValue.ts +3 -5
- package/src/editor/key-generator.ts +6 -0
- package/src/editor/plugins/createWithEditableAPI.ts +8 -5
- package/src/editor/plugins/createWithHotKeys.ts +1 -0
- package/src/editor/plugins/createWithInsertBreak.ts +6 -2
- package/src/editor/plugins/createWithInsertData.ts +7 -4
- package/src/editor/plugins/createWithObjectKeys.ts +12 -5
- package/src/editor/plugins/createWithPatches.ts +0 -1
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +4 -5
- package/src/editor/plugins/createWithSchemaTypes.ts +3 -4
- package/src/editor/plugins/createWithUtils.ts +5 -4
- package/src/editor/plugins/index.ts +5 -13
- package/src/index.ts +2 -2
- package/src/types/options.ts +0 -1
- package/src/utils/__tests__/operationToPatches.test.ts +0 -2
- package/src/utils/__tests__/patchToOperations.test.ts +1 -2
- package/src/editor/hooks/usePortableTextEditorKeyGenerator.ts +0 -27
package/lib/index.mjs
CHANGED
|
@@ -20,8 +20,8 @@ import { isHotkey } from "is-hotkey-esm";
|
|
|
20
20
|
import { htmlToBlocks, normalizeBlock } from "@sanity/block-tools";
|
|
21
21
|
import isPlainObject from "lodash/isPlainObject.js";
|
|
22
22
|
import throttle from "lodash/throttle.js";
|
|
23
|
-
import { randomKey } from "@sanity/util/content";
|
|
24
23
|
import debounce from "lodash/debounce.js";
|
|
24
|
+
import { randomKey } from "@sanity/util/content";
|
|
25
25
|
const rootName = "sanity-pte:";
|
|
26
26
|
debug$m(rootName);
|
|
27
27
|
function debugWithName(name) {
|
|
@@ -1084,7 +1084,7 @@ function createOperationToPatches(types) {
|
|
|
1084
1084
|
};
|
|
1085
1085
|
}
|
|
1086
1086
|
const debug$j = debugWithName("API:editable");
|
|
1087
|
-
function createWithEditableAPI(portableTextEditor, types
|
|
1087
|
+
function createWithEditableAPI(editorActor, portableTextEditor, types) {
|
|
1088
1088
|
return function(editor) {
|
|
1089
1089
|
return portableTextEditor.setEditable({
|
|
1090
1090
|
focus: () => {
|
|
@@ -1164,11 +1164,11 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
1164
1164
|
const child = toSlateValue(
|
|
1165
1165
|
[
|
|
1166
1166
|
{
|
|
1167
|
-
_key: keyGenerator(),
|
|
1167
|
+
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
1168
1168
|
_type: types.block.name,
|
|
1169
1169
|
children: [
|
|
1170
1170
|
{
|
|
1171
|
-
_key: keyGenerator(),
|
|
1171
|
+
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
1172
1172
|
_type: type.name,
|
|
1173
1173
|
...value || {}
|
|
1174
1174
|
}
|
|
@@ -1196,7 +1196,7 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
1196
1196
|
const block = toSlateValue(
|
|
1197
1197
|
[
|
|
1198
1198
|
{
|
|
1199
|
-
_key: keyGenerator(),
|
|
1199
|
+
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
1200
1200
|
_type: type.name,
|
|
1201
1201
|
...value || {}
|
|
1202
1202
|
}
|
|
@@ -1360,7 +1360,7 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
1360
1360
|
for (const [block, blockPath] of selectedBlocks) {
|
|
1361
1361
|
if (block.children.length === 0 || block.children.length === 1 && block.children[0].text === "")
|
|
1362
1362
|
continue;
|
|
1363
|
-
const annotationKey = keyGenerator(), markDefs = block.markDefs ?? [];
|
|
1363
|
+
const annotationKey = editorActor.getSnapshot().context.keyGenerator(), markDefs = block.markDefs ?? [];
|
|
1364
1364
|
markDefs.find(
|
|
1365
1365
|
(markDef) => markDef._type === type.name && markDef._key === annotationKey
|
|
1366
1366
|
) === void 0 && (Transforms.setNodes(
|
|
@@ -1569,7 +1569,7 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
1569
1569
|
}), editor;
|
|
1570
1570
|
};
|
|
1571
1571
|
}
|
|
1572
|
-
function createWithInsertBreak(
|
|
1572
|
+
function createWithInsertBreak(editorActor, types) {
|
|
1573
1573
|
return function(editor) {
|
|
1574
1574
|
const { insertBreak } = editor;
|
|
1575
1575
|
return editor.insertBreak = () => {
|
|
@@ -1658,7 +1658,10 @@ function createWithInsertBreak(types, keyGenerator) {
|
|
|
1658
1658
|
(decorator) => decorator.value === mark
|
|
1659
1659
|
) || prevNodeSpans.some(
|
|
1660
1660
|
(prevNodeSpan) => prevNodeSpan.marks?.includes(mark)
|
|
1661
|
-
) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(
|
|
1661
|
+
) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(
|
|
1662
|
+
mark,
|
|
1663
|
+
editorActor.getSnapshot().context.keyGenerator()
|
|
1664
|
+
);
|
|
1662
1665
|
const newMarks = marks.map(
|
|
1663
1666
|
(mark) => newMarkDefKeys.get(mark) ?? mark
|
|
1664
1667
|
);
|
|
@@ -1739,7 +1742,7 @@ function createWithMaxBlocks(maxBlocks) {
|
|
|
1739
1742
|
}, editor;
|
|
1740
1743
|
};
|
|
1741
1744
|
}
|
|
1742
|
-
function createWithObjectKeys(editorActor, schemaTypes
|
|
1745
|
+
function createWithObjectKeys(editorActor, schemaTypes) {
|
|
1743
1746
|
return function(editor) {
|
|
1744
1747
|
const { apply: apply2, normalizeNode } = editor;
|
|
1745
1748
|
return editor.apply = (operation) => {
|
|
@@ -1756,7 +1759,7 @@ function createWithObjectKeys(editorActor, schemaTypes, keyGenerator) {
|
|
|
1756
1759
|
...operation,
|
|
1757
1760
|
properties: {
|
|
1758
1761
|
...operation.properties,
|
|
1759
|
-
_key: keyGenerator()
|
|
1762
|
+
_key: editorActor.getSnapshot().context.keyGenerator()
|
|
1760
1763
|
}
|
|
1761
1764
|
});
|
|
1762
1765
|
return;
|
|
@@ -1766,7 +1769,7 @@ function createWithObjectKeys(editorActor, schemaTypes, keyGenerator) {
|
|
|
1766
1769
|
...operation,
|
|
1767
1770
|
node: {
|
|
1768
1771
|
...operation.node,
|
|
1769
|
-
_key: keyGenerator()
|
|
1772
|
+
_key: editorActor.getSnapshot().context.keyGenerator()
|
|
1770
1773
|
}
|
|
1771
1774
|
});
|
|
1772
1775
|
return;
|
|
@@ -1776,12 +1779,20 @@ function createWithObjectKeys(editorActor, schemaTypes, keyGenerator) {
|
|
|
1776
1779
|
const [node, path] = entry;
|
|
1777
1780
|
if (Element$1.isElement(node) && node._type === schemaTypes.block.name) {
|
|
1778
1781
|
if (!node._key) {
|
|
1779
|
-
editorActor.send({ type: "normalizing" }), Transforms.setNodes(
|
|
1782
|
+
editorActor.send({ type: "normalizing" }), Transforms.setNodes(
|
|
1783
|
+
editor,
|
|
1784
|
+
{ _key: editorActor.getSnapshot().context.keyGenerator() },
|
|
1785
|
+
{ at: path }
|
|
1786
|
+
), editorActor.send({ type: "done normalizing" });
|
|
1780
1787
|
return;
|
|
1781
1788
|
}
|
|
1782
1789
|
for (const [child, childPath] of Node.children(editor, path))
|
|
1783
1790
|
if (!child._key) {
|
|
1784
|
-
editorActor.send({ type: "normalizing" }), Transforms.setNodes(
|
|
1791
|
+
editorActor.send({ type: "normalizing" }), Transforms.setNodes(
|
|
1792
|
+
editor,
|
|
1793
|
+
{ _key: editorActor.getSnapshot().context.keyGenerator() },
|
|
1794
|
+
{ at: childPath }
|
|
1795
|
+
), editorActor.send({ type: "done normalizing" });
|
|
1785
1796
|
return;
|
|
1786
1797
|
}
|
|
1787
1798
|
}
|
|
@@ -3273,7 +3284,7 @@ function isPortableTextBlock(node) {
|
|
|
3273
3284
|
);
|
|
3274
3285
|
}
|
|
3275
3286
|
const debug$c = debugWithName("plugin:withPortableTextMarkModel");
|
|
3276
|
-
function createWithPortableTextMarkModel(editorActor, types
|
|
3287
|
+
function createWithPortableTextMarkModel(editorActor, types) {
|
|
3277
3288
|
return function(editor) {
|
|
3278
3289
|
const { apply: apply2, normalizeNode } = editor, decorators = types.decorators.map((t) => t.value), forceNewSelection = () => {
|
|
3279
3290
|
editor.selection && (Transforms.select(editor, { ...editor.selection }), editor.selection = { ...editor.selection });
|
|
@@ -3434,12 +3445,12 @@ function createWithPortableTextMarkModel(editorActor, types, keyGenerator) {
|
|
|
3434
3445
|
if (atTheBeginningOfSpan) {
|
|
3435
3446
|
previousSpanHasSameMarks ? Transforms.insertNodes(editor, {
|
|
3436
3447
|
_type: "span",
|
|
3437
|
-
_key: keyGenerator(),
|
|
3448
|
+
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
3438
3449
|
text: op.text,
|
|
3439
3450
|
marks: previousSpan?.marks ?? []
|
|
3440
3451
|
}) : previousSpanHasSameAnnotation ? apply2(op) : Transforms.insertNodes(editor, {
|
|
3441
3452
|
_type: "span",
|
|
3442
|
-
_key: keyGenerator(),
|
|
3453
|
+
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
3443
3454
|
text: op.text,
|
|
3444
3455
|
marks: []
|
|
3445
3456
|
});
|
|
@@ -3448,12 +3459,12 @@ function createWithPortableTextMarkModel(editorActor, types, keyGenerator) {
|
|
|
3448
3459
|
if (atTheEndOfSpan) {
|
|
3449
3460
|
nextSpanHasSameMarks ? Transforms.insertNodes(editor, {
|
|
3450
3461
|
_type: "span",
|
|
3451
|
-
_key: keyGenerator(),
|
|
3462
|
+
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
3452
3463
|
text: op.text,
|
|
3453
3464
|
marks: nextSpan?.marks ?? []
|
|
3454
3465
|
}) : nextSpanHasSameAnnotation ? apply2(op) : Transforms.insertNodes(editor, {
|
|
3455
3466
|
_type: "span",
|
|
3456
|
-
_key: keyGenerator(),
|
|
3467
|
+
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
3457
3468
|
text: op.text,
|
|
3458
3469
|
marks: []
|
|
3459
3470
|
});
|
|
@@ -3686,8 +3697,7 @@ function createWithPortableTextSelections(editorActor, types) {
|
|
|
3686
3697
|
const debug$a = debugWithName("plugin:withSchemaTypes");
|
|
3687
3698
|
function createWithSchemaTypes({
|
|
3688
3699
|
editorActor,
|
|
3689
|
-
schemaTypes
|
|
3690
|
-
keyGenerator
|
|
3700
|
+
schemaTypes
|
|
3691
3701
|
}) {
|
|
3692
3702
|
return function(editor) {
|
|
3693
3703
|
editor.isTextBlock = (value) => isPortableTextTextBlock(value) && value._type === schemaTypes.block.name, editor.isTextSpan = (value) => isPortableTextSpan$1(value) && value._type === schemaTypes.span.name, editor.isListBlock = (value) => isPortableTextListBlock(value) && value._type === schemaTypes.block.name, editor.isVoid = (element) => schemaTypes.block.name !== element._type && (schemaTypes.blockObjects.map((obj) => obj.name).includes(element._type) || schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type)), editor.isInline = (element) => schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type) && "__inline" in element && element.__inline === !0;
|
|
@@ -3696,7 +3706,7 @@ function createWithSchemaTypes({
|
|
|
3696
3706
|
const [node, path] = entry;
|
|
3697
3707
|
if (node._type === void 0 && path.length === 2) {
|
|
3698
3708
|
debug$a("Setting span type on text node without a type");
|
|
3699
|
-
const span = node, key = span._key || keyGenerator();
|
|
3709
|
+
const span = node, key = span._key || editorActor.getSnapshot().context.keyGenerator();
|
|
3700
3710
|
editorActor.send({ type: "normalizing" }), Transforms.setNodes(
|
|
3701
3711
|
editor,
|
|
3702
3712
|
{ ...span, _type: schemaTypes.span.name, _key: key },
|
|
@@ -3706,7 +3716,7 @@ function createWithSchemaTypes({
|
|
|
3706
3716
|
}
|
|
3707
3717
|
if (node._key === void 0 && (path.length === 1 || path.length === 2)) {
|
|
3708
3718
|
debug$a("Setting missing key on child node without a key");
|
|
3709
|
-
const key = keyGenerator();
|
|
3719
|
+
const key = editorActor.getSnapshot().context.keyGenerator();
|
|
3710
3720
|
editorActor.send({ type: "normalizing" }), Transforms.setNodes(editor, { _key: key }, { at: path }), editorActor.send({ type: "done normalizing" });
|
|
3711
3721
|
return;
|
|
3712
3722
|
}
|
|
@@ -3716,8 +3726,8 @@ function createWithSchemaTypes({
|
|
|
3716
3726
|
}
|
|
3717
3727
|
const debug$9 = debugWithName("plugin:withUtils");
|
|
3718
3728
|
function createWithUtils({
|
|
3729
|
+
editorActor,
|
|
3719
3730
|
schemaTypes,
|
|
3720
|
-
keyGenerator,
|
|
3721
3731
|
portableTextEditor
|
|
3722
3732
|
}) {
|
|
3723
3733
|
return function(editor) {
|
|
@@ -3743,13 +3753,13 @@ function createWithUtils({
|
|
|
3743
3753
|
[
|
|
3744
3754
|
{
|
|
3745
3755
|
_type: schemaTypes.block.name,
|
|
3746
|
-
_key: keyGenerator(),
|
|
3756
|
+
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
3747
3757
|
style: schemaTypes.styles[0].value || "normal",
|
|
3748
3758
|
markDefs: [],
|
|
3749
3759
|
children: [
|
|
3750
3760
|
{
|
|
3751
3761
|
_type: "span",
|
|
3752
|
-
_key: keyGenerator(),
|
|
3762
|
+
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
3753
3763
|
text: "",
|
|
3754
3764
|
marks: options.decorators.filter(
|
|
3755
3765
|
(decorator) => schemaTypes.decorators.find(({ value }) => value === decorator)
|
|
@@ -3817,7 +3827,7 @@ function createWithHotkeys(types, portableTextEditor, hotkeysFromOptions) {
|
|
|
3817
3827
|
{
|
|
3818
3828
|
at: nextPath
|
|
3819
3829
|
}
|
|
3820
|
-
), editor.onChange();
|
|
3830
|
+
), Transforms.select(editor, { path: [...nextPath, 0], offset: 0 }), editor.onChange();
|
|
3821
3831
|
return;
|
|
3822
3832
|
}
|
|
3823
3833
|
}
|
|
@@ -4160,7 +4170,7 @@ function validateValue(value, types, keyGenerator) {
|
|
|
4160
4170
|
}) && (valid = !1), { valid, resolution, value });
|
|
4161
4171
|
}
|
|
4162
4172
|
const debug$7 = debugWithName("plugin:withInsertData");
|
|
4163
|
-
function createWithInsertData(editorActor, schemaTypes
|
|
4173
|
+
function createWithInsertData(editorActor, schemaTypes) {
|
|
4164
4174
|
return function(editor) {
|
|
4165
4175
|
const blockTypeName = schemaTypes.block.name, spanTypeName = schemaTypes.span.name, whitespaceOnPasteMode = schemaTypes.block.options.unstable_whitespaceOnPasteMode, toPlainText = (blocks) => blocks.map((block) => editor.isTextBlock(block) ? block.children.map((child) => child._type === spanTypeName ? child.text : `[${schemaTypes.inlineObjects.find((t) => t.name === child._type)?.title || "Object"}]`).join("") : `[${schemaTypes.blockObjects.find((t) => t.name === block._type)?.title || "Object"}]`).join(`
|
|
4166
4176
|
|
|
@@ -4208,10 +4218,14 @@ function createWithInsertData(editorActor, schemaTypes, keyGenerator) {
|
|
|
4208
4218
|
const slateValue = _regenerateKeys(
|
|
4209
4219
|
editor,
|
|
4210
4220
|
toSlateValue(parsed, { schemaTypes }),
|
|
4211
|
-
keyGenerator,
|
|
4221
|
+
editorActor.getSnapshot().context.keyGenerator,
|
|
4212
4222
|
spanTypeName,
|
|
4213
4223
|
schemaTypes
|
|
4214
|
-
), validation = validateValue(
|
|
4224
|
+
), validation = validateValue(
|
|
4225
|
+
parsed,
|
|
4226
|
+
schemaTypes,
|
|
4227
|
+
editorActor.getSnapshot().context.keyGenerator
|
|
4228
|
+
);
|
|
4215
4229
|
if (!validation.valid && !validation.resolution?.autoResolve) {
|
|
4216
4230
|
const errorDescription = `${validation.resolution?.description}`;
|
|
4217
4231
|
return editorActor.send({
|
|
@@ -4252,7 +4266,7 @@ function createWithInsertData(editorActor, schemaTypes, keyGenerator) {
|
|
|
4252
4266
|
const validation = validateValue(
|
|
4253
4267
|
portableText,
|
|
4254
4268
|
schemaTypes,
|
|
4255
|
-
keyGenerator
|
|
4269
|
+
editorActor.getSnapshot().context.keyGenerator
|
|
4256
4270
|
);
|
|
4257
4271
|
if (!validation.valid) {
|
|
4258
4272
|
const errorDescription = `Could not validate the resulting portable text to insert.
|
|
@@ -4356,27 +4370,21 @@ function _insertFragment(editor, fragment, schemaTypes) {
|
|
|
4356
4370
|
}), editor.onChange();
|
|
4357
4371
|
}
|
|
4358
4372
|
const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, options) => {
|
|
4359
|
-
const e = editor, {
|
|
4373
|
+
const e = editor, { portableTextEditor, patches$, readOnly, maxBlocks } = options, { editorActor, schemaTypes } = portableTextEditor;
|
|
4360
4374
|
e.subscriptions = [], e.destroy ? e.destroy() : originalFnMap.set(e, {
|
|
4361
4375
|
apply: e.apply,
|
|
4362
4376
|
onChange: e.onChange,
|
|
4363
4377
|
normalizeNode: e.normalizeNode
|
|
4364
4378
|
});
|
|
4365
|
-
const operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(
|
|
4366
|
-
editorActor,
|
|
4367
|
-
schemaTypes,
|
|
4368
|
-
keyGenerator
|
|
4369
|
-
), withSchemaTypes = createWithSchemaTypes({
|
|
4379
|
+
const operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(editorActor, schemaTypes), withSchemaTypes = createWithSchemaTypes({
|
|
4370
4380
|
editorActor,
|
|
4371
|
-
schemaTypes
|
|
4372
|
-
keyGenerator
|
|
4381
|
+
schemaTypes
|
|
4373
4382
|
}), withEditableAPI = createWithEditableAPI(
|
|
4383
|
+
editorActor,
|
|
4374
4384
|
portableTextEditor,
|
|
4375
|
-
schemaTypes
|
|
4376
|
-
keyGenerator
|
|
4385
|
+
schemaTypes
|
|
4377
4386
|
), withPatches = createWithPatches({
|
|
4378
4387
|
editorActor,
|
|
4379
|
-
keyGenerator,
|
|
4380
4388
|
patches$,
|
|
4381
4389
|
patchFunctions: operationToPatches,
|
|
4382
4390
|
readOnly,
|
|
@@ -4387,13 +4395,12 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
|
|
|
4387
4395
|
blockSchemaType: schemaTypes.block
|
|
4388
4396
|
}), withPortableTextMarkModel = createWithPortableTextMarkModel(
|
|
4389
4397
|
editorActor,
|
|
4390
|
-
schemaTypes
|
|
4391
|
-
keyGenerator
|
|
4398
|
+
schemaTypes
|
|
4392
4399
|
), withPortableTextBlockStyle = createWithPortableTextBlockStyle(
|
|
4393
4400
|
editorActor,
|
|
4394
4401
|
schemaTypes
|
|
4395
|
-
), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(
|
|
4396
|
-
|
|
4402
|
+
), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(editorActor, schemaTypes), withUtils = createWithUtils({
|
|
4403
|
+
editorActor,
|
|
4397
4404
|
schemaTypes,
|
|
4398
4405
|
portableTextEditor
|
|
4399
4406
|
}), withPortableTextSelections = createWithPortableTextSelections(
|
|
@@ -4461,10 +4468,9 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
|
|
|
4461
4468
|
};
|
|
4462
4469
|
}, debug$6 = debugWithName("component:PortableTextEditor:SlateContainer");
|
|
4463
4470
|
function SlateContainer(props) {
|
|
4464
|
-
const { patches$, portableTextEditor, readOnly, maxBlocks
|
|
4471
|
+
const { patches$, portableTextEditor, readOnly, maxBlocks } = props, [[slateEditor, subscribe]] = useState(() => {
|
|
4465
4472
|
debug$6("Creating new Slate editor instance");
|
|
4466
4473
|
const { editor, subscribe: _sub } = withPlugins(withReact(createEditor()), {
|
|
4467
|
-
keyGenerator,
|
|
4468
4474
|
maxBlocks,
|
|
4469
4475
|
patches$,
|
|
4470
4476
|
portableTextEditor,
|
|
@@ -4479,33 +4485,18 @@ function SlateContainer(props) {
|
|
|
4479
4485
|
};
|
|
4480
4486
|
}, [subscribe]), useEffect(() => {
|
|
4481
4487
|
debug$6("Re-initializing plugin chain"), withPlugins(slateEditor, {
|
|
4482
|
-
keyGenerator,
|
|
4483
4488
|
maxBlocks,
|
|
4484
4489
|
patches$,
|
|
4485
4490
|
portableTextEditor,
|
|
4486
4491
|
readOnly
|
|
4487
4492
|
});
|
|
4488
|
-
}, [
|
|
4489
|
-
keyGenerator,
|
|
4490
|
-
portableTextEditor,
|
|
4491
|
-
maxBlocks,
|
|
4492
|
-
readOnly,
|
|
4493
|
-
patches$,
|
|
4494
|
-
slateEditor
|
|
4495
|
-
]);
|
|
4493
|
+
}, [portableTextEditor, maxBlocks, readOnly, patches$, slateEditor]);
|
|
4496
4494
|
const initialValue = useMemo(() => [slateEditor.pteCreateTextBlock({ decorators: [] })], [slateEditor]);
|
|
4497
4495
|
return useEffect(() => () => {
|
|
4498
4496
|
debug$6("Destroying Slate editor"), slateEditor.destroy();
|
|
4499
4497
|
}, [slateEditor]), /* @__PURE__ */ jsx(Slate, { editor: slateEditor, initialValue, children: props.children });
|
|
4500
4498
|
}
|
|
4501
|
-
const
|
|
4502
|
-
const keyGenerator = useContext(PortableTextEditorKeyGeneratorContext);
|
|
4503
|
-
if (keyGenerator === void 0)
|
|
4504
|
-
throw new Error(
|
|
4505
|
-
"The `usePortableTextEditorKeyGenerator` hook must be used inside the <PortableTextEditor> component's context."
|
|
4506
|
-
);
|
|
4507
|
-
return keyGenerator;
|
|
4508
|
-
}, PortableTextEditorReadOnlyContext = createContext(!1), usePortableTextEditorReadOnlyStatus = () => {
|
|
4499
|
+
const PortableTextEditorReadOnlyContext = createContext(!1), usePortableTextEditorReadOnlyStatus = () => {
|
|
4509
4500
|
const readOnly = useContext(PortableTextEditorReadOnlyContext);
|
|
4510
4501
|
if (readOnly === void 0)
|
|
4511
4502
|
throw new Error(
|
|
@@ -4514,7 +4505,7 @@ const defaultKeyGenerator = () => randomKey(12), PortableTextEditorKeyGeneratorC
|
|
|
4514
4505
|
return readOnly;
|
|
4515
4506
|
}, debug$5 = debugWithName("hook:useSyncValue"), CURRENT_VALUE = /* @__PURE__ */ new WeakMap();
|
|
4516
4507
|
function useSyncValue(props) {
|
|
4517
|
-
const { editorActor, portableTextEditor, readOnly
|
|
4508
|
+
const { editorActor, portableTextEditor, readOnly } = props, { schemaTypes } = portableTextEditor, previousValue = useRef(), slateEditor = useSlate(), updateValueFunctionRef = useRef(), updateFromCurrentValue = useCallback(() => {
|
|
4518
4509
|
const currentValue = CURRENT_VALUE.get(portableTextEditor);
|
|
4519
4510
|
if (previousValue.current === currentValue) {
|
|
4520
4511
|
debug$5("Value is the same object as previous, not need to sync");
|
|
@@ -4580,7 +4571,7 @@ function useSyncValue(props) {
|
|
|
4580
4571
|
const validationValue = [value[currentBlockIndex]], validation = validateValue(
|
|
4581
4572
|
validationValue,
|
|
4582
4573
|
schemaTypes,
|
|
4583
|
-
keyGenerator
|
|
4574
|
+
editorActor.getSnapshot().context.keyGenerator
|
|
4584
4575
|
);
|
|
4585
4576
|
!validation.valid && validation.resolution?.autoResolve && validation.resolution?.patches.length > 0 && !readOnly && previousValue.current && previousValue.current !== value && (console.warn(
|
|
4586
4577
|
`${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${validation.resolution?.description}`
|
|
@@ -4605,7 +4596,7 @@ function useSyncValue(props) {
|
|
|
4605
4596
|
const validationValue = [value[currentBlockIndex]], validation = validateValue(
|
|
4606
4597
|
validationValue,
|
|
4607
4598
|
schemaTypes,
|
|
4608
|
-
keyGenerator
|
|
4599
|
+
editorActor.getSnapshot().context.keyGenerator
|
|
4609
4600
|
);
|
|
4610
4601
|
debug$5.enabled && debug$5(
|
|
4611
4602
|
"Validating and inserting new block in the end of the value",
|
|
@@ -4652,7 +4643,6 @@ function useSyncValue(props) {
|
|
|
4652
4643
|
return updateValueFunctionRef.current = updateFunction, updateFunction;
|
|
4653
4644
|
}, [
|
|
4654
4645
|
editorActor,
|
|
4655
|
-
keyGenerator,
|
|
4656
4646
|
portableTextEditor,
|
|
4657
4647
|
readOnly,
|
|
4658
4648
|
schemaTypes,
|
|
@@ -4720,9 +4710,8 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
|
|
|
4720
4710
|
}
|
|
4721
4711
|
const debug$4 = debugWithName("component:PortableTextEditor:Synchronizer"), debugVerbose$1 = debug$4.enabled && !1, FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === "test" ? 500 : 1e3;
|
|
4722
4712
|
function Synchronizer(props) {
|
|
4723
|
-
const portableTextEditor = usePortableTextEditor(),
|
|
4713
|
+
const portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), { editorActor, getValue, onChange, value } = props, pendingPatches = useRef([]), syncValue = useSyncValue({
|
|
4724
4714
|
editorActor,
|
|
4725
|
-
keyGenerator,
|
|
4726
4715
|
portableTextEditor,
|
|
4727
4716
|
readOnly
|
|
4728
4717
|
}), slateEditor = useSlate();
|
|
@@ -4836,7 +4825,8 @@ const networkLogic = fromCallback(({ sendBack }) => {
|
|
|
4836
4825
|
types: {
|
|
4837
4826
|
context: {},
|
|
4838
4827
|
events: {},
|
|
4839
|
-
emitted: {}
|
|
4828
|
+
emitted: {},
|
|
4829
|
+
input: {}
|
|
4840
4830
|
},
|
|
4841
4831
|
actions: {
|
|
4842
4832
|
"emit patch event": emit(({ event }) => (assertEvent(event, "patch"), event)),
|
|
@@ -4857,9 +4847,10 @@ const networkLogic = fromCallback(({ sendBack }) => {
|
|
|
4857
4847
|
}
|
|
4858
4848
|
}).createMachine({
|
|
4859
4849
|
id: "editor",
|
|
4860
|
-
context: {
|
|
4850
|
+
context: ({ input }) => ({
|
|
4851
|
+
keyGenerator: input.keyGenerator,
|
|
4861
4852
|
pendingEvents: []
|
|
4862
|
-
},
|
|
4853
|
+
}),
|
|
4863
4854
|
invoke: {
|
|
4864
4855
|
id: "networkLogic",
|
|
4865
4856
|
src: "networkLogic"
|
|
@@ -4947,7 +4938,7 @@ function PortableTextEditorSelectionProvider(props) {
|
|
|
4947
4938
|
};
|
|
4948
4939
|
}, [props.editorActor]), /* @__PURE__ */ jsx(PortableTextEditorSelectionContext.Provider, { value: selection, children: props.children });
|
|
4949
4940
|
}
|
|
4950
|
-
const debug$2 = debugWithName("component:PortableTextEditor");
|
|
4941
|
+
const defaultKeyGenerator = () => randomKey(12), debug$2 = debugWithName("component:PortableTextEditor");
|
|
4951
4942
|
class PortableTextEditor extends Component {
|
|
4952
4943
|
/**
|
|
4953
4944
|
* @internal
|
|
@@ -4971,7 +4962,11 @@ class PortableTextEditor extends Component {
|
|
|
4971
4962
|
throw new Error('PortableTextEditor: missing "schemaType" property');
|
|
4972
4963
|
props.incomingPatches$ && console.warn(
|
|
4973
4964
|
"The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"
|
|
4974
|
-
), this.editorActor = createActor(editorMachine
|
|
4965
|
+
), this.editorActor = createActor(editorMachine, {
|
|
4966
|
+
input: {
|
|
4967
|
+
keyGenerator: props.keyGenerator || defaultKeyGenerator
|
|
4968
|
+
}
|
|
4969
|
+
}), this.editorActor.start(), this.schemaTypes = getPortableTextMemberSchemaTypes(
|
|
4975
4970
|
props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)
|
|
4976
4971
|
);
|
|
4977
4972
|
}
|
|
@@ -4988,35 +4983,28 @@ class PortableTextEditor extends Component {
|
|
|
4988
4983
|
return this.editable.getValue();
|
|
4989
4984
|
};
|
|
4990
4985
|
render() {
|
|
4991
|
-
const { value, children, patches$, incomingPatches$ } = this.props, _patches$ = incomingPatches$ || patches$, maxBlocks = typeof this.props.maxBlocks > "u" ? void 0 : Number.parseInt(this.props.maxBlocks.toString(), 10) || void 0, readOnly = !!this.props.readOnly
|
|
4986
|
+
const { value, children, patches$, incomingPatches$ } = this.props, _patches$ = incomingPatches$ || patches$, maxBlocks = typeof this.props.maxBlocks > "u" ? void 0 : Number.parseInt(this.props.maxBlocks.toString(), 10) || void 0, readOnly = !!this.props.readOnly;
|
|
4992
4987
|
return /* @__PURE__ */ jsx(
|
|
4993
4988
|
SlateContainer,
|
|
4994
4989
|
{
|
|
4995
|
-
keyGenerator,
|
|
4996
4990
|
maxBlocks,
|
|
4997
4991
|
patches$: _patches$,
|
|
4998
4992
|
portableTextEditor: this,
|
|
4999
4993
|
readOnly,
|
|
5000
|
-
children: /* @__PURE__ */ jsx(
|
|
5001
|
-
|
|
5002
|
-
|
|
5003
|
-
|
|
5004
|
-
|
|
5005
|
-
|
|
5006
|
-
|
|
5007
|
-
|
|
5008
|
-
|
|
5009
|
-
|
|
5010
|
-
|
|
5011
|
-
|
|
5012
|
-
|
|
5013
|
-
|
|
5014
|
-
}
|
|
5015
|
-
),
|
|
5016
|
-
children
|
|
5017
|
-
]
|
|
5018
|
-
}
|
|
5019
|
-
) }) }) })
|
|
4994
|
+
children: /* @__PURE__ */ jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsx(PortableTextEditorReadOnlyContext.Provider, { value: readOnly, children: /* @__PURE__ */ jsxs(PortableTextEditorSelectionProvider, { editorActor: this.editorActor, children: [
|
|
4995
|
+
/* @__PURE__ */ jsx(
|
|
4996
|
+
Synchronizer,
|
|
4997
|
+
{
|
|
4998
|
+
editorActor: this.editorActor,
|
|
4999
|
+
getValue: this.getValue,
|
|
5000
|
+
onChange: (change) => {
|
|
5001
|
+
this.props.onChange(change), this.change$.next(change);
|
|
5002
|
+
},
|
|
5003
|
+
value
|
|
5004
|
+
}
|
|
5005
|
+
),
|
|
5006
|
+
children
|
|
5007
|
+
] }) }) })
|
|
5020
5008
|
}
|
|
5021
5009
|
);
|
|
5022
5010
|
}
|
|
@@ -5282,7 +5270,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
5282
5270
|
scrollSelectionIntoView,
|
|
5283
5271
|
spellCheck,
|
|
5284
5272
|
...restProps
|
|
5285
|
-
} = props, portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(),
|
|
5273
|
+
} = props, portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), ref = useRef(null), [editableElement, setEditableElement] = useState(
|
|
5286
5274
|
null
|
|
5287
5275
|
), [hasInvalidValue, setHasInvalidValue] = useState(!1), [rangeDecorationState, setRangeDecorationsState] = useState([]);
|
|
5288
5276
|
useImperativeHandle(
|
|
@@ -5290,8 +5278,8 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
5290
5278
|
() => ref.current
|
|
5291
5279
|
);
|
|
5292
5280
|
const rangeDecorationsRef = useRef(rangeDecorations), { editorActor, schemaTypes } = portableTextEditor, slateEditor = useSlate(), blockTypeName = schemaTypes.block.name, withInsertData = useMemo(
|
|
5293
|
-
() => createWithInsertData(editorActor, schemaTypes
|
|
5294
|
-
[editorActor,
|
|
5281
|
+
() => createWithInsertData(editorActor, schemaTypes),
|
|
5282
|
+
[editorActor, schemaTypes]
|
|
5295
5283
|
), withHotKeys = useMemo(
|
|
5296
5284
|
() => createWithHotkeys(schemaTypes, portableTextEditor, hotkeys),
|
|
5297
5285
|
[hotkeys, portableTextEditor, schemaTypes]
|