@portabletext/editor 2.21.2 → 3.0.0
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 +50 -210
- package/lib/_chunks-es/selector.is-at-the-start-of-block.js +103 -20
- package/lib/_chunks-es/selector.is-at-the-start-of-block.js.map +1 -1
- package/lib/_chunks-es/{util.get-text-block-text.js → util.slice-blocks.js} +29 -5
- package/lib/_chunks-es/util.slice-blocks.js.map +1 -0
- package/lib/_chunks-es/util.slice-text-block.js +13 -2
- package/lib/_chunks-es/util.slice-text-block.js.map +1 -1
- package/lib/behaviors/index.d.ts +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +323 -320
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.ts +2 -133
- package/lib/plugins/index.js +2 -796
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.d.ts +2 -24
- package/lib/selectors/index.js +28 -130
- package/lib/selectors/index.js.map +1 -1
- package/lib/utils/index.d.ts +3 -3
- package/lib/utils/index.js +97 -9
- package/lib/utils/index.js.map +1 -1
- package/package.json +7 -9
- package/src/behaviors/behavior.perform-event.ts +7 -7
- package/src/editor/PortableTextEditor.tsx +0 -19
- package/src/editor/create-editor.ts +0 -3
- package/src/editor/editor-machine.ts +0 -10
- package/src/editor/event-to-change.tsx +5 -1
- package/src/editor/plugins/create-with-event-listeners.ts +0 -4
- package/src/editor/plugins/createWithObjectKeys.ts +2 -1
- package/src/editor/plugins/createWithPatches.ts +3 -3
- package/src/editor/plugins/createWithPlaceholderBlock.ts +2 -1
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +2 -1
- package/src/editor/plugins/with-plugins.ts +10 -14
- package/src/editor/relay-machine.ts +0 -4
- package/src/editor/sync-machine.ts +2 -2
- package/src/editor.ts +0 -4
- package/src/history/behavior.operation.history.redo.ts +67 -0
- package/src/history/behavior.operation.history.undo.ts +71 -0
- package/src/history/event.history.undo.test.tsx +672 -0
- package/src/history/history.preserving-keys.test.tsx +112 -0
- package/src/history/remote-patches.ts +20 -0
- package/src/history/slate-plugin.history.ts +146 -0
- package/src/history/slate-plugin.redoing.ts +21 -0
- package/src/history/slate-plugin.undoing.ts +21 -0
- package/src/history/slate-plugin.without-history.ts +23 -0
- package/src/history/transform-operation.ts +245 -0
- package/src/history/undo-redo-collaboration.test.tsx +541 -0
- package/src/history/undo-redo.feature +125 -0
- package/src/history/undo-redo.test.tsx +195 -0
- package/src/history/undo-step.ts +148 -0
- package/src/index.ts +0 -1
- package/src/internal-utils/applyPatch.ts +46 -1
- package/src/operations/behavior.operations.ts +2 -4
- package/src/plugins/index.ts +0 -3
- package/src/selectors/index.ts +0 -3
- package/src/test/vitest/step-definitions.tsx +88 -8
- package/src/test/vitest/test-editor.tsx +1 -1
- package/lib/_chunks-es/selector.get-selection-text.js +0 -92
- package/lib/_chunks-es/selector.get-selection-text.js.map +0 -1
- package/lib/_chunks-es/selector.get-text-before.js +0 -36
- package/lib/_chunks-es/selector.get-text-before.js.map +0 -1
- package/lib/_chunks-es/util.get-text-block-text.js.map +0 -1
- package/lib/_chunks-es/util.is-empty-text-block.js +0 -40
- package/lib/_chunks-es/util.is-empty-text-block.js.map +0 -1
- package/lib/_chunks-es/util.merge-text-blocks.js +0 -101
- package/lib/_chunks-es/util.merge-text-blocks.js.map +0 -1
- package/src/editor/plugins/createWithMaxBlocks.ts +0 -53
- package/src/editor/plugins/createWithUndoRedo.ts +0 -628
- package/src/editor/with-undo-step.ts +0 -37
- package/src/editor/withUndoRedo.ts +0 -34
- package/src/editor-event-listener.tsx +0 -28
- package/src/plugins/plugin.decorator-shortcut.ts +0 -238
- package/src/plugins/plugin.markdown.test.tsx +0 -42
- package/src/plugins/plugin.markdown.tsx +0 -131
- package/src/plugins/plugin.one-line.tsx +0 -123
- package/src/selectors/selector.get-list-state.test.ts +0 -189
- package/src/selectors/selector.get-list-state.ts +0 -96
- package/src/selectors/selector.get-selected-slice.ts +0 -13
- package/src/selectors/selector.get-trimmed-selection.test.ts +0 -657
- package/src/selectors/selector.get-trimmed-selection.ts +0 -189
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { isTextBlock, isSpan } from "@portabletext/schema";
|
|
2
|
-
import { getBlockKeyFromSelectionPoint, getChildKeyFromSelectionPoint, getSelectionStartPoint as getSelectionStartPoint$1, getSelectionEndPoint, sliceBlocks } from "./util.get-text-block-text.js";
|
|
3
|
-
import { isKeySegment } from "@sanity/types";
|
|
4
|
-
const getFocusBlock = (snapshot) => {
|
|
5
|
-
if (!snapshot.context.selection)
|
|
6
|
-
return;
|
|
7
|
-
const key = getBlockKeyFromSelectionPoint(snapshot.context.selection.focus), index = key ? snapshot.blockIndexMap.get(key) : void 0, node = index !== void 0 ? snapshot.context.value.at(index) : void 0;
|
|
8
|
-
return node && key ? {
|
|
9
|
-
node,
|
|
10
|
-
path: [{
|
|
11
|
-
_key: key
|
|
12
|
-
}]
|
|
13
|
-
} : void 0;
|
|
14
|
-
}, getFocusTextBlock = (snapshot) => {
|
|
15
|
-
const focusBlock = getFocusBlock(snapshot);
|
|
16
|
-
return focusBlock && isTextBlock(snapshot.context, focusBlock.node) ? {
|
|
17
|
-
node: focusBlock.node,
|
|
18
|
-
path: focusBlock.path
|
|
19
|
-
} : void 0;
|
|
20
|
-
}, getFocusChild = (snapshot) => {
|
|
21
|
-
if (!snapshot.context.selection)
|
|
22
|
-
return;
|
|
23
|
-
const focusBlock = getFocusTextBlock(snapshot);
|
|
24
|
-
if (!focusBlock)
|
|
25
|
-
return;
|
|
26
|
-
const key = getChildKeyFromSelectionPoint(snapshot.context.selection.focus), node = key ? focusBlock.node.children.find((span) => span._key === key) : void 0;
|
|
27
|
-
return node && key ? {
|
|
28
|
-
node,
|
|
29
|
-
path: [...focusBlock.path, "children", {
|
|
30
|
-
_key: key
|
|
31
|
-
}]
|
|
32
|
-
} : void 0;
|
|
33
|
-
}, getFocusSpan = (snapshot) => {
|
|
34
|
-
const focusChild = getFocusChild(snapshot);
|
|
35
|
-
return focusChild && isSpan(snapshot.context, focusChild.node) ? {
|
|
36
|
-
node: focusChild.node,
|
|
37
|
-
path: focusChild.path
|
|
38
|
-
} : void 0;
|
|
39
|
-
}, getSelectionStartPoint = (snapshot) => {
|
|
40
|
-
if (snapshot.context.selection)
|
|
41
|
-
return snapshot.context.selection.backward ? snapshot.context.selection.focus : snapshot.context.selection.anchor;
|
|
42
|
-
}, isSelectionCollapsed = (snapshot) => snapshot.context.selection ? JSON.stringify(snapshot.context.selection.anchor.path) === JSON.stringify(snapshot.context.selection.focus.path) && snapshot.context.selection?.anchor.offset === snapshot.context.selection?.focus.offset : !1, isSelectionExpanded = (snapshot) => snapshot.context.selection !== null && !isSelectionCollapsed(snapshot), getSelectedValue = (snapshot) => {
|
|
43
|
-
const selection = snapshot.context.selection;
|
|
44
|
-
if (!selection)
|
|
45
|
-
return [];
|
|
46
|
-
const startPoint = getSelectionStartPoint$1(selection), endPoint = getSelectionEndPoint(selection), startBlockKey = getBlockKeyFromSelectionPoint(startPoint), endBlockKey = getBlockKeyFromSelectionPoint(endPoint);
|
|
47
|
-
if (!startBlockKey || !endBlockKey)
|
|
48
|
-
return [];
|
|
49
|
-
const startBlockIndex = snapshot.blockIndexMap.get(startBlockKey), endBlockIndex = snapshot.blockIndexMap.get(endBlockKey);
|
|
50
|
-
if (startBlockIndex === void 0 || endBlockIndex === void 0)
|
|
51
|
-
return [];
|
|
52
|
-
const startBlock = snapshot.context.value.at(startBlockIndex), slicedStartBlock = startBlock ? sliceBlocks({
|
|
53
|
-
context: snapshot.context,
|
|
54
|
-
blocks: [startBlock]
|
|
55
|
-
}).at(0) : void 0;
|
|
56
|
-
if (startBlockIndex === endBlockIndex)
|
|
57
|
-
return slicedStartBlock ? [slicedStartBlock] : [];
|
|
58
|
-
const endBlock = snapshot.context.value.at(endBlockIndex), slicedEndBlock = endBlock ? sliceBlocks({
|
|
59
|
-
context: snapshot.context,
|
|
60
|
-
blocks: [endBlock]
|
|
61
|
-
}).at(0) : void 0, middleBlocks = snapshot.context.value.slice(startBlockIndex + 1, endBlockIndex);
|
|
62
|
-
return [...slicedStartBlock ? [slicedStartBlock] : [], ...middleBlocks, ...slicedEndBlock ? [slicedEndBlock] : []];
|
|
63
|
-
}, getPreviousInlineObject = (snapshot) => {
|
|
64
|
-
const focusTextBlock = getFocusTextBlock(snapshot), selectionStartPoint = getSelectionStartPoint(snapshot), selectionStartPointChildKey = selectionStartPoint && isKeySegment(selectionStartPoint.path[2]) ? selectionStartPoint.path[2]._key : void 0;
|
|
65
|
-
if (!focusTextBlock || !selectionStartPointChildKey)
|
|
66
|
-
return;
|
|
67
|
-
let inlineObject;
|
|
68
|
-
for (const child of focusTextBlock.node.children) {
|
|
69
|
-
if (child._key === selectionStartPointChildKey)
|
|
70
|
-
break;
|
|
71
|
-
isSpan(snapshot.context, child) || (inlineObject = {
|
|
72
|
-
node: child,
|
|
73
|
-
path: [...focusTextBlock.path, "children", {
|
|
74
|
-
_key: child._key
|
|
75
|
-
}]
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
return inlineObject;
|
|
79
|
-
}, getSelectionText = (snapshot) => getSelectedValue(snapshot).reduce((text, block) => isTextBlock(snapshot.context, block) ? text + block.children.reduce((text2, child) => isSpan(snapshot.context, child) ? text2 + child.text : text2, "") : text, "");
|
|
80
|
-
export {
|
|
81
|
-
getFocusBlock,
|
|
82
|
-
getFocusChild,
|
|
83
|
-
getFocusSpan,
|
|
84
|
-
getFocusTextBlock,
|
|
85
|
-
getPreviousInlineObject,
|
|
86
|
-
getSelectedValue,
|
|
87
|
-
getSelectionStartPoint,
|
|
88
|
-
getSelectionText,
|
|
89
|
-
isSelectionCollapsed,
|
|
90
|
-
isSelectionExpanded
|
|
91
|
-
};
|
|
92
|
-
//# sourceMappingURL=selector.get-selection-text.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"selector.get-selection-text.js","sources":["../../src/selectors/selector.get-focus-block.ts","../../src/selectors/selector.get-focus-text-block.ts","../../src/selectors/selector.get-focus-child.ts","../../src/selectors/selector.get-focus-span.ts","../../src/selectors/selector.get-selection-start-point.ts","../../src/selectors/selector.is-selection-collapsed.ts","../../src/selectors/selector.is-selection-expanded.ts","../../src/selectors/selector.get-selected-value.ts","../../src/selectors/selector.get-previous-inline-object.ts","../../src/selectors/selector.get-selection-text.ts"],"sourcesContent":["import type {PortableTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport type {BlockPath} from '../types/paths'\nimport {getBlockKeyFromSelectionPoint} from '../utils/util.selection-point'\n\n/**\n * @public\n */\nexport const getFocusBlock: EditorSelector<\n {node: PortableTextBlock; path: BlockPath} | undefined\n> = (snapshot) => {\n if (!snapshot.context.selection) {\n return undefined\n }\n\n const key = getBlockKeyFromSelectionPoint(snapshot.context.selection.focus)\n const index = key ? snapshot.blockIndexMap.get(key) : undefined\n\n const node =\n index !== undefined ? snapshot.context.value.at(index) : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n","import {isTextBlock} from '@portabletext/schema'\nimport type {PortableTextTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport type {BlockPath} from '../types/paths'\nimport {getFocusBlock} from './selector.get-focus-block'\n\n/**\n * @public\n */\nexport const getFocusTextBlock: EditorSelector<\n {node: PortableTextTextBlock; path: BlockPath} | undefined\n> = (snapshot) => {\n const focusBlock = getFocusBlock(snapshot)\n\n return focusBlock && isTextBlock(snapshot.context, focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n","import type {PortableTextObject, PortableTextSpan} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport type {ChildPath} from '../types/paths'\nimport {getChildKeyFromSelectionPoint} from '../utils/util.selection-point'\nimport {getFocusTextBlock} from './selector.get-focus-text-block'\n\n/**\n * @public\n */\nexport const getFocusChild: EditorSelector<\n | {\n node: PortableTextObject | PortableTextSpan\n path: ChildPath\n }\n | undefined\n> = (snapshot) => {\n if (!snapshot.context.selection) {\n return undefined\n }\n\n const focusBlock = getFocusTextBlock(snapshot)\n\n if (!focusBlock) {\n return undefined\n }\n\n const key = getChildKeyFromSelectionPoint(snapshot.context.selection.focus)\n\n const node = key\n ? focusBlock.node.children.find((span) => span._key === key)\n : undefined\n\n return node && key\n ? {node, path: [...focusBlock.path, 'children', {_key: key}]}\n : undefined\n}\n","import {isSpan} from '@portabletext/schema'\nimport type {PortableTextSpan} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport type {ChildPath} from '../types/paths'\nimport {getFocusChild} from './selector.get-focus-child'\n\n/**\n * @public\n */\nexport const getFocusSpan: EditorSelector<\n {node: PortableTextSpan; path: ChildPath} | undefined\n> = (snapshot) => {\n const focusChild = getFocusChild(snapshot)\n\n return focusChild && isSpan(snapshot.context, focusChild.node)\n ? {node: focusChild.node, path: focusChild.path}\n : undefined\n}\n","import type {EditorSelector} from '../editor/editor-selector'\nimport type {EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport const getSelectionStartPoint: EditorSelector<\n EditorSelectionPoint | undefined\n> = (snapshot) => {\n if (!snapshot.context.selection) {\n return undefined\n }\n\n return snapshot.context.selection.backward\n ? snapshot.context.selection.focus\n : snapshot.context.selection.anchor\n}\n","import type {EditorSelector} from '../editor/editor-selector'\n\n/**\n * @public\n */\nexport const isSelectionCollapsed: EditorSelector<boolean> = (snapshot) => {\n if (!snapshot.context.selection) {\n return false\n }\n\n return (\n JSON.stringify(snapshot.context.selection.anchor.path) ===\n JSON.stringify(snapshot.context.selection.focus.path) &&\n snapshot.context.selection?.anchor.offset ===\n snapshot.context.selection?.focus.offset\n )\n}\n","import type {EditorSelector} from '../editor/editor-selector'\nimport {isSelectionCollapsed} from './selector.is-selection-collapsed'\n\n/**\n * @public\n */\nexport const isSelectionExpanded: EditorSelector<boolean> = (snapshot) => {\n return snapshot.context.selection !== null && !isSelectionCollapsed(snapshot)\n}\n","import type {PortableTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {getSelectionEndPoint} from '../utils/util.get-selection-end-point'\nimport {getSelectionStartPoint} from '../utils/util.get-selection-start-point'\nimport {getBlockKeyFromSelectionPoint} from '../utils/util.selection-point'\nimport {sliceBlocks} from '../utils/util.slice-blocks'\n\n/**\n * @public\n */\nexport const getSelectedValue: EditorSelector<Array<PortableTextBlock>> = (\n snapshot,\n) => {\n const selection = snapshot.context.selection\n\n if (!selection) {\n return []\n }\n\n const startPoint = getSelectionStartPoint(selection)\n const endPoint = getSelectionEndPoint(selection)\n const startBlockKey = getBlockKeyFromSelectionPoint(startPoint)\n const endBlockKey = getBlockKeyFromSelectionPoint(endPoint)\n\n if (!startBlockKey || !endBlockKey) {\n return []\n }\n\n const startBlockIndex = snapshot.blockIndexMap.get(startBlockKey)\n const endBlockIndex = snapshot.blockIndexMap.get(endBlockKey)\n\n if (startBlockIndex === undefined || endBlockIndex === undefined) {\n return []\n }\n\n const startBlock = snapshot.context.value.at(startBlockIndex)\n const slicedStartBlock = startBlock\n ? sliceBlocks({\n context: snapshot.context,\n blocks: [startBlock],\n }).at(0)\n : undefined\n\n if (startBlockIndex === endBlockIndex) {\n return slicedStartBlock ? [slicedStartBlock] : []\n }\n\n const endBlock = snapshot.context.value.at(endBlockIndex)\n const slicedEndBlock = endBlock\n ? sliceBlocks({\n context: snapshot.context,\n blocks: [endBlock],\n }).at(0)\n : undefined\n\n const middleBlocks = snapshot.context.value.slice(\n startBlockIndex + 1,\n endBlockIndex,\n )\n\n return [\n ...(slicedStartBlock ? [slicedStartBlock] : []),\n ...middleBlocks,\n ...(slicedEndBlock ? [slicedEndBlock] : []),\n ]\n}\n","import {isSpan} from '@portabletext/schema'\nimport {isKeySegment, type PortableTextObject} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport type {ChildPath} from '../types/paths'\nimport {getFocusTextBlock} from './selector.get-focus-text-block'\nimport {getSelectionStartPoint} from './selector.get-selection-start-point'\n\n/**\n * @public\n */\nexport const getPreviousInlineObject: EditorSelector<\n | {\n node: PortableTextObject\n path: ChildPath\n }\n | undefined\n> = (snapshot) => {\n const focusTextBlock = getFocusTextBlock(snapshot)\n const selectionStartPoint = getSelectionStartPoint(snapshot)\n const selectionStartPointChildKey =\n selectionStartPoint && isKeySegment(selectionStartPoint.path[2])\n ? selectionStartPoint.path[2]._key\n : undefined\n\n if (!focusTextBlock || !selectionStartPointChildKey) {\n return undefined\n }\n\n let inlineObject:\n | {\n node: PortableTextObject\n path: ChildPath\n }\n | undefined\n\n for (const child of focusTextBlock.node.children) {\n if (child._key === selectionStartPointChildKey) {\n break\n }\n\n if (!isSpan(snapshot.context, child)) {\n inlineObject = {\n node: child,\n path: [...focusTextBlock.path, 'children', {_key: child._key}],\n }\n }\n }\n\n return inlineObject\n}\n","import {isSpan, isTextBlock} from '@portabletext/schema'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {getSelectedValue} from './selector.get-selected-value'\n\n/**\n * @public\n */\nexport const getSelectionText: EditorSelector<string> = (snapshot) => {\n const selectedValue = getSelectedValue(snapshot)\n\n return selectedValue.reduce((text, block) => {\n if (!isTextBlock(snapshot.context, block)) {\n return text\n }\n\n return (\n text +\n block.children.reduce((text, child) => {\n if (isSpan(snapshot.context, child)) {\n return text + child.text\n }\n\n return text\n }, '')\n )\n }, '')\n}\n"],"names":["getFocusBlock","snapshot","context","selection","key","getBlockKeyFromSelectionPoint","focus","index","blockIndexMap","get","undefined","node","value","at","path","_key","getFocusTextBlock","focusBlock","isTextBlock","getFocusChild","getChildKeyFromSelectionPoint","children","find","span","getFocusSpan","focusChild","isSpan","getSelectionStartPoint","backward","anchor","isSelectionCollapsed","JSON","stringify","offset","isSelectionExpanded","getSelectedValue","startPoint","endPoint","getSelectionEndPoint","startBlockKey","endBlockKey","startBlockIndex","endBlockIndex","startBlock","slicedStartBlock","sliceBlocks","blocks","endBlock","slicedEndBlock","middleBlocks","slice","getPreviousInlineObject","focusTextBlock","selectionStartPoint","selectionStartPointChildKey","isKeySegment","inlineObject","child","getSelectionText","reduce","text","block"],"mappings":";;;AAQO,MAAMA,gBAERC,CAAAA,aAAa;AAChB,MAAI,CAACA,SAASC,QAAQC;AACpB;AAGF,QAAMC,MAAMC,8BAA8BJ,SAASC,QAAQC,UAAUG,KAAK,GACpEC,QAAQH,MAAMH,SAASO,cAAcC,IAAIL,GAAG,IAAIM,QAEhDC,OACJJ,UAAUG,SAAYT,SAASC,QAAQU,MAAMC,GAAGN,KAAK,IAAIG;AAE3D,SAAOC,QAAQP,MAAM;AAAA,IAACO;AAAAA,IAAMG,MAAM,CAAC;AAAA,MAACC,MAAMX;AAAAA,IAAAA,CAAI;AAAA,EAAA,IAAKM;AACrD,GCbaM,oBAERf,CAAAA,aAAa;AAChB,QAAMgB,aAAajB,cAAcC,QAAQ;AAEzC,SAAOgB,cAAcC,YAAYjB,SAASC,SAASe,WAAWN,IAAI,IAC9D;AAAA,IAACA,MAAMM,WAAWN;AAAAA,IAAMG,MAAMG,WAAWH;AAAAA,EAAAA,IACzCJ;AACN,GCRaS,gBAMRlB,CAAAA,aAAa;AAChB,MAAI,CAACA,SAASC,QAAQC;AACpB;AAGF,QAAMc,aAAaD,kBAAkBf,QAAQ;AAE7C,MAAI,CAACgB;AACH;AAGF,QAAMb,MAAMgB,8BAA8BnB,SAASC,QAAQC,UAAUG,KAAK,GAEpEK,OAAOP,MACTa,WAAWN,KAAKU,SAASC,KAAMC,UAASA,KAAKR,SAASX,GAAG,IACzDM;AAEJ,SAAOC,QAAQP,MACX;AAAA,IAACO;AAAAA,IAAMG,MAAM,CAAC,GAAGG,WAAWH,MAAM,YAAY;AAAA,MAACC,MAAMX;AAAAA,IAAAA,CAAI;AAAA,EAAA,IACzDM;AACN,GC1Bac,eAERvB,CAAAA,aAAa;AAChB,QAAMwB,aAAaN,cAAclB,QAAQ;AAEzC,SAAOwB,cAAcC,OAAOzB,S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SAASuD,KAAK,IACzBG,QAAOH,MAAMG,OAGfA,OACN,EAAE,IAXEA,MAaR,EAAE;"}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { getSelectionStartPoint, getBlockStartPoint } from "./util.get-text-block-text.js";
|
|
2
|
-
import { getFocusBlock, getSelectionText } from "./selector.get-selection-text.js";
|
|
3
|
-
const getBlockTextBefore = (snapshot) => {
|
|
4
|
-
if (!snapshot.context.selection)
|
|
5
|
-
return "";
|
|
6
|
-
const startPoint = getSelectionStartPoint(snapshot.context.selection), block = getFocusBlock({
|
|
7
|
-
...snapshot,
|
|
8
|
-
context: {
|
|
9
|
-
...snapshot.context,
|
|
10
|
-
selection: {
|
|
11
|
-
anchor: startPoint,
|
|
12
|
-
focus: startPoint
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
if (!block)
|
|
17
|
-
return "";
|
|
18
|
-
const startOfBlock = getBlockStartPoint({
|
|
19
|
-
context: snapshot.context,
|
|
20
|
-
block
|
|
21
|
-
});
|
|
22
|
-
return getSelectionText({
|
|
23
|
-
...snapshot,
|
|
24
|
-
context: {
|
|
25
|
-
...snapshot.context,
|
|
26
|
-
selection: {
|
|
27
|
-
anchor: startOfBlock,
|
|
28
|
-
focus: startPoint
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
export {
|
|
34
|
-
getBlockTextBefore
|
|
35
|
-
};
|
|
36
|
-
//# sourceMappingURL=selector.get-text-before.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"selector.get-text-before.js","sources":["../../src/selectors/selector.get-text-before.ts"],"sourcesContent":["import type {EditorSelector} from '../editor/editor-selector'\nimport {getBlockStartPoint} from '../utils/util.get-block-start-point'\nimport {getSelectionStartPoint} from '../utils/util.get-selection-start-point'\nimport {getFocusBlock} from './selector.get-focus-block'\nimport {getSelectionText} from './selector.get-selection-text'\n\n/**\n * @public\n */\nexport const getBlockTextBefore: EditorSelector<string> = (snapshot) => {\n if (!snapshot.context.selection) {\n return ''\n }\n\n const startPoint = getSelectionStartPoint(snapshot.context.selection)\n const block = getFocusBlock({\n ...snapshot,\n context: {\n ...snapshot.context,\n selection: {\n anchor: startPoint,\n focus: startPoint,\n },\n },\n })\n\n if (!block) {\n return ''\n }\n\n const startOfBlock = getBlockStartPoint({\n context: snapshot.context,\n block,\n })\n\n return getSelectionText({\n ...snapshot,\n context: {\n ...snapshot.context,\n selection: {\n anchor: startOfBlock,\n focus: startPoint,\n },\n },\n })\n}\n"],"names":["getBlockTextBefore","snapshot","context","selection","startPoint","getSelectionStartPoint","block","getFocusBlock","anchor","focus","startOfBlock","getBlockStartPoint","getSelectionText"],"mappings":";;AASO,MAAMA,qBAA8CC,CAAAA,aAAa;AACtE,MAAI,CAACA,SAASC,QAAQC;AACpB,WAAO;AAGT,QAAMC,aAAaC,uBAAuBJ,SAASC,QAAQC,SAAS,GAC9DG,QAAQC,cAAc;AAAA,IAC1B,GAAGN;AAAAA,IACHC,SAAS;AAAA,MACP,GAAGD,SAASC;AAAAA,MACZC,WAAW;AAAA,QACTK,QAAQJ;AAAAA,QACRK,OAAOL;AAAAA,MAAAA;AAAAA,IACT;AAAA,EACF,CACD;AAED,MAAI,CAACE;AACH,WAAO;AAGT,QAAMI,eAAeC,mBAAmB;AAAA,IACtCT,SAASD,SAASC;AAAAA,IAClBI;AAAAA,EAAAA,CACD;AAED,SAAOM,iBAAiB;AAAA,IACtB,GAAGX;AAAAA,IACHC,SAAS;AAAA,MACP,GAAGD,SAASC;AAAAA,MACZC,WAAW;AAAA,QACTK,QAAQE;AAAAA,QACRD,OAAOL;AAAAA,MAAAA;AAAAA,IACT;AAAA,EACF,CACD;AACH;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"util.get-text-block-text.js","sources":["../../src/utils/util.get-block-start-point.ts","../../src/utils/util.is-keyed-segment.ts","../../src/utils/util.selection-point.ts","../../src/utils/util.block-offset.ts","../../src/utils/asserters.ts","../../src/utils/parse-blocks.ts","../../src/utils/key-generator.ts","../../src/utils/util.get-selection-end-point.ts","../../src/utils/util.get-selection-start-point.ts","../../src/utils/util.slice-blocks.ts","../../src/utils/util.get-text-block-text.ts"],"sourcesContent":["import {isTextBlock} from '@portabletext/schema'\nimport type {PortableTextBlock} from '@sanity/types'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport type {EditorSelectionPoint} from '../types/editor'\nimport type {BlockPath} from '../types/paths'\n\n/**\n * @public\n */\nexport function getBlockStartPoint({\n context,\n block,\n}: {\n context: Pick<EditorContext, 'schema'>\n block: {\n node: PortableTextBlock\n path: BlockPath\n }\n}): EditorSelectionPoint {\n if (isTextBlock(context, block.node)) {\n return {\n path: [...block.path, 'children', {_key: block.node.children[0]._key}],\n offset: 0,\n }\n }\n\n return {\n path: block.path,\n offset: 0,\n }\n}\n","import type {KeyedSegment} from '@sanity/types'\n\n/**\n * @public\n */\nexport function isKeyedSegment(segment: unknown): segment is KeyedSegment {\n return typeof segment === 'object' && segment !== null && '_key' in segment\n}\n","import type {EditorSelectionPoint} from '../types/editor'\nimport {isKeyedSegment} from './util.is-keyed-segment'\n\nexport function getBlockKeyFromSelectionPoint(point: EditorSelectionPoint) {\n const blockPathSegment = point.path.at(0)\n\n if (isKeyedSegment(blockPathSegment)) {\n return blockPathSegment._key\n }\n\n return undefined\n}\n\nexport function getChildKeyFromSelectionPoint(point: EditorSelectionPoint) {\n const childPathSegment = point.path.at(2)\n\n if (isKeyedSegment(childPathSegment)) {\n return childPathSegment._key\n }\n\n return undefined\n}\n","import {isSpan, isTextBlock} from '@portabletext/schema'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelectionPoint} from '../types/editor'\nimport type {ChildPath} from '../types/paths'\nimport {\n getBlockKeyFromSelectionPoint,\n getChildKeyFromSelectionPoint,\n} from './util.selection-point'\n\n/**\n * @public\n */\nexport function blockOffsetToSpanSelectionPoint({\n context,\n blockOffset,\n direction,\n}: {\n context: Pick<EditorContext, 'schema' | 'value'>\n blockOffset: BlockOffset\n direction: 'forward' | 'backward'\n}) {\n let offsetLeft = blockOffset.offset\n let selectionPoint: {path: ChildPath; offset: number} | undefined\n let skippedInlineObject = false\n\n for (const block of context.value) {\n if (block._key !== blockOffset.path[0]._key) {\n continue\n }\n\n if (!isTextBlock(context, block)) {\n continue\n }\n\n for (const child of block.children) {\n if (direction === 'forward') {\n if (!isSpan(context, child)) {\n continue\n }\n\n if (offsetLeft <= child.text.length) {\n selectionPoint = {\n path: [...blockOffset.path, 'children', {_key: child._key}],\n offset: offsetLeft,\n }\n break\n }\n\n offsetLeft -= child.text.length\n\n continue\n }\n\n if (!isSpan(context, child)) {\n skippedInlineObject = true\n continue\n }\n\n if (offsetLeft === 0 && selectionPoint && !skippedInlineObject) {\n if (skippedInlineObject) {\n selectionPoint = {\n path: [...blockOffset.path, 'children', {_key: child._key}],\n offset: 0,\n }\n }\n break\n }\n\n if (offsetLeft > child.text.length) {\n offsetLeft -= child.text.length\n continue\n }\n\n if (offsetLeft <= child.text.length) {\n selectionPoint = {\n path: [...blockOffset.path, 'children', {_key: child._key}],\n offset: offsetLeft,\n }\n\n offsetLeft -= child.text.length\n\n if (offsetLeft !== 0) {\n break\n }\n }\n }\n }\n\n return selectionPoint\n}\n\n/**\n * @public\n */\nexport function spanSelectionPointToBlockOffset({\n context,\n selectionPoint,\n}: {\n context: Pick<EditorContext, 'schema' | 'value'>\n selectionPoint: EditorSelectionPoint\n}): BlockOffset | undefined {\n let offset = 0\n\n const blockKey = getBlockKeyFromSelectionPoint(selectionPoint)\n const spanKey = getChildKeyFromSelectionPoint(selectionPoint)\n\n if (!blockKey || !spanKey) {\n return undefined\n }\n\n for (const block of context.value) {\n if (block._key !== blockKey) {\n continue\n }\n\n if (!isTextBlock(context, block)) {\n continue\n }\n\n for (const child of block.children) {\n if (!isSpan(context, child)) {\n continue\n }\n\n if (child._key === spanKey) {\n return {\n path: [{_key: block._key}],\n offset: offset + selectionPoint.offset,\n }\n }\n\n offset += child.text.length\n }\n }\n}\n","import type {TypedObject} from '@sanity/types'\n\nexport function isTypedObject(object: unknown): object is TypedObject {\n return isRecord(object) && typeof object._type === 'string'\n}\n\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return !!value && (typeof value === 'object' || typeof value === 'function')\n}\n","import {isTextBlock} from '@portabletext/schema'\nimport type {\n PortableTextBlock,\n PortableTextListBlock,\n PortableTextObject,\n PortableTextSpan,\n PortableTextTextBlock,\n TypedObject,\n} from '@sanity/types'\nimport type {EditorSchema} from '../editor/editor-schema'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport {isTypedObject} from './asserters'\n\nexport function parseBlocks({\n context,\n blocks,\n options,\n}: {\n context: Pick<EditorContext, 'keyGenerator' | 'schema'>\n blocks: unknown\n options: {\n removeUnusedMarkDefs: boolean\n validateFields: boolean\n }\n}): Array<PortableTextBlock> {\n if (!Array.isArray(blocks)) {\n return []\n }\n\n return blocks.flatMap((block) => {\n const parsedBlock = parseBlock({context, block, options})\n\n return parsedBlock ? [parsedBlock] : []\n })\n}\n\nexport function parseBlock({\n context,\n block,\n options,\n}: {\n context: Pick<EditorContext, 'keyGenerator' | 'schema'>\n block: unknown\n options: {\n removeUnusedMarkDefs: boolean\n validateFields: boolean\n }\n}): PortableTextBlock | undefined {\n return (\n parseTextBlock({block, context, options}) ??\n parseBlockObject({blockObject: block, context, options})\n )\n}\n\nexport function parseBlockObject({\n blockObject,\n context,\n options,\n}: {\n blockObject: unknown\n context: Pick<EditorContext, 'keyGenerator' | 'schema'>\n options: {validateFields: boolean}\n}): PortableTextObject | undefined {\n if (!isTypedObject(blockObject)) {\n return undefined\n }\n\n const schemaType = context.schema.blockObjects.find(\n ({name}) => name === blockObject._type,\n )\n\n if (!schemaType) {\n return undefined\n }\n\n return parseObject({\n object: blockObject,\n context: {\n keyGenerator: context.keyGenerator,\n schemaType,\n },\n options,\n })\n}\n\nexport function isListBlock(\n context: Pick<EditorContext, 'schema'>,\n block: unknown,\n): block is PortableTextListBlock {\n return (\n isTextBlock(context, block) &&\n block.level !== undefined &&\n block.listItem !== undefined\n )\n}\n\nexport function parseTextBlock({\n block,\n context,\n options,\n}: {\n block: unknown\n context: Pick<EditorContext, 'keyGenerator' | 'schema'>\n options: {\n removeUnusedMarkDefs: boolean\n validateFields: boolean\n }\n}): PortableTextTextBlock | undefined {\n if (!isTypedObject(block)) {\n return undefined\n }\n\n const customFields: Record<string, unknown> = {}\n\n for (const key of Object.keys(block)) {\n if (\n key === '_type' ||\n key === '_key' ||\n key === 'children' ||\n key === 'markDefs' ||\n key === 'style' ||\n key === 'listItem' ||\n key === 'level'\n ) {\n continue\n }\n\n if (options.validateFields) {\n if (context.schema.block.fields?.some((field) => field.name === key)) {\n customFields[key] = block[key]\n }\n } else {\n customFields[key] = block[key]\n }\n }\n\n if (block._type !== context.schema.block.name) {\n return undefined\n }\n\n const _key =\n typeof block._key === 'string' ? block._key : context.keyGenerator()\n\n const unparsedMarkDefs: Array<unknown> = Array.isArray(block.markDefs)\n ? block.markDefs\n : []\n const markDefKeyMap = new Map<string, string>()\n const markDefs = unparsedMarkDefs.flatMap((markDef) => {\n if (!isTypedObject(markDef)) {\n return []\n }\n\n const schemaType = context.schema.annotations.find(\n ({name}) => name === markDef._type,\n )\n\n if (!schemaType) {\n return []\n }\n\n if (typeof markDef._key !== 'string') {\n // If the `markDef` doesn't have a `_key` then we don't know what spans\n // it belongs to and therefore we have to discard it.\n return []\n }\n\n const parsedAnnotation = parseObject({\n object: markDef,\n context: {\n schemaType,\n keyGenerator: context.keyGenerator,\n },\n options,\n })\n\n if (!parsedAnnotation) {\n return []\n }\n\n markDefKeyMap.set(markDef._key, parsedAnnotation._key)\n\n return [parsedAnnotation]\n })\n\n const unparsedChildren: Array<unknown> = Array.isArray(block.children)\n ? block.children\n : []\n\n const children = unparsedChildren\n .map(\n (child) =>\n parseSpan({span: child, context, markDefKeyMap, options}) ??\n parseInlineObject({inlineObject: child, context, options}),\n )\n .filter((child) => child !== undefined)\n const marks = children.flatMap((child) => child.marks ?? [])\n\n const parsedBlock: PortableTextTextBlock = {\n _type: context.schema.block.name,\n _key,\n children:\n children.length > 0\n ? children\n : [\n {\n _key: context.keyGenerator(),\n _type: context.schema.span.name,\n text: '',\n marks: [],\n },\n ],\n markDefs: options.removeUnusedMarkDefs\n ? markDefs.filter((markDef) => marks.includes(markDef._key))\n : markDefs,\n ...customFields,\n }\n\n if (\n typeof block.style === 'string' &&\n context.schema.styles.find((style) => style.name === block.style)\n ) {\n parsedBlock.style = block.style\n } else {\n const defaultStyle = context.schema.styles.at(0)?.name\n\n if (defaultStyle !== undefined) {\n parsedBlock.style = defaultStyle\n } else {\n console.error('Expected default style')\n }\n }\n\n if (\n typeof block.listItem === 'string' &&\n context.schema.lists.find((list) => list.name === block.listItem)\n ) {\n parsedBlock.listItem = block.listItem\n }\n\n if (typeof block.level === 'number') {\n parsedBlock.level = block.level\n }\n\n return parsedBlock\n}\n\nexport function parseSpan({\n span,\n context,\n markDefKeyMap,\n options,\n}: {\n span: unknown\n context: Pick<EditorContext, 'keyGenerator' | 'schema'>\n markDefKeyMap: Map<string, string>\n options: {validateFields: boolean}\n}): PortableTextSpan | undefined {\n if (!isTypedObject(span)) {\n return undefined\n }\n\n const customFields: Record<string, unknown> = {}\n\n for (const key of Object.keys(span)) {\n if (\n key !== '_type' &&\n key !== '_key' &&\n key !== 'text' &&\n key !== 'marks'\n ) {\n customFields[key] = span[key]\n }\n }\n\n // In reality, the span schema name is always 'span', but we only the check here anyway\n if (span._type !== context.schema.span.name || span._type !== 'span') {\n return undefined\n }\n\n const unparsedMarks: Array<unknown> = Array.isArray(span.marks)\n ? span.marks\n : []\n const marks = unparsedMarks.flatMap((mark) => {\n if (typeof mark !== 'string') {\n return []\n }\n\n const markDefKey = markDefKeyMap.get(mark)\n\n if (markDefKey !== undefined) {\n return [markDefKey]\n }\n\n if (\n context.schema.decorators.some((decorator) => decorator.name === mark)\n ) {\n return [mark]\n }\n\n return []\n })\n\n return {\n _type: 'span',\n _key: typeof span._key === 'string' ? span._key : context.keyGenerator(),\n text: typeof span.text === 'string' ? span.text : '',\n marks,\n ...(options.validateFields ? {} : customFields),\n }\n}\n\nexport function parseInlineObject({\n inlineObject,\n context,\n options,\n}: {\n inlineObject: unknown\n context: Pick<EditorContext, 'keyGenerator' | 'schema'>\n options: {validateFields: boolean}\n}): PortableTextObject | undefined {\n if (!isTypedObject(inlineObject)) {\n return undefined\n }\n\n const schemaType = context.schema.inlineObjects.find(\n ({name}) => name === inlineObject._type,\n )\n\n if (!schemaType) {\n return undefined\n }\n\n return parseObject({\n object: inlineObject,\n context: {\n keyGenerator: context.keyGenerator,\n schemaType,\n },\n options,\n })\n}\n\nexport function parseAnnotation({\n annotation,\n context,\n options,\n}: {\n annotation: TypedObject\n context: Pick<EditorContext, 'keyGenerator' | 'schema'>\n options: {validateFields: boolean}\n}): PortableTextObject | undefined {\n if (!isTypedObject(annotation)) {\n return undefined\n }\n\n const schemaType = context.schema.annotations.find(\n ({name}) => name === annotation._type,\n )\n\n if (!schemaType) {\n return undefined\n }\n\n return parseObject({\n object: annotation,\n context: {\n keyGenerator: context.keyGenerator,\n schemaType,\n },\n options,\n })\n}\n\nfunction parseObject({\n object,\n context,\n options,\n}: {\n object: TypedObject\n context: Pick<EditorContext, 'keyGenerator'> & {\n schemaType: EditorSchema['blockObjects'][0]\n }\n options: {validateFields: boolean}\n}): PortableTextObject {\n const {_type, _key, ...customFields} = object\n\n // Validates all props on the object and only takes those that match\n // the name of a field\n const values = options.validateFields\n ? context.schemaType.fields.reduce<Record<string, unknown>>(\n (fieldValues, field) => {\n const fieldValue = object[field.name]\n\n if (fieldValue !== undefined) {\n fieldValues[field.name] = fieldValue\n }\n\n return fieldValues\n },\n {},\n )\n : customFields\n\n return {\n _type: context.schemaType.name,\n _key:\n typeof object._key === 'string' ? object._key : context.keyGenerator(),\n ...values,\n }\n}\n","/**\n * @public\n */\nexport const defaultKeyGenerator = (): string => randomKey(12)\n\nconst getByteHexTable = (() => {\n let table: any[]\n return () => {\n if (table) {\n return table\n }\n\n table = []\n for (let i = 0; i < 256; ++i) {\n table[i] = (i + 0x100).toString(16).slice(1)\n }\n return table\n }\n})()\n\n// WHATWG crypto RNG - https://w3c.github.io/webcrypto/Overview.html\nfunction whatwgRNG(length = 16) {\n const rnds8 = new Uint8Array(length)\n crypto.getRandomValues(rnds8)\n return rnds8\n}\n\nfunction randomKey(length?: number): string {\n const table = getByteHexTable()\n return whatwgRNG(length)\n .reduce((str, n) => str + table[n], '')\n .slice(0, length)\n}\n","import type {EditorSelection, EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function getSelectionEndPoint<\n TEditorSelection extends NonNullable<EditorSelection> | null,\n TEditorSelectionPoint extends\n EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>\n ? EditorSelectionPoint\n : null,\n>(selection: TEditorSelection): TEditorSelectionPoint {\n if (!selection) {\n return null as TEditorSelectionPoint\n }\n\n return (\n selection.backward ? selection.anchor : selection.focus\n ) as TEditorSelectionPoint\n}\n","import type {EditorSelection, EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function getSelectionStartPoint<\n TEditorSelection extends NonNullable<EditorSelection> | null,\n TEditorSelectionPoint extends\n EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>\n ? EditorSelectionPoint\n : null,\n>(selection: TEditorSelection): TEditorSelectionPoint {\n if (!selection) {\n return null as TEditorSelectionPoint\n }\n\n return (\n selection.backward ? selection.focus : selection.anchor\n ) as TEditorSelectionPoint\n}\n","import {isSpan, isTextBlock} from '@portabletext/schema'\nimport type {PortableTextBlock} from '@sanity/types'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport {defaultKeyGenerator} from './key-generator'\nimport {parseBlock} from './parse-blocks'\nimport {getSelectionEndPoint} from './util.get-selection-end-point'\nimport {getSelectionStartPoint} from './util.get-selection-start-point'\nimport {\n getBlockKeyFromSelectionPoint,\n getChildKeyFromSelectionPoint,\n} from './util.selection-point'\n\n/**\n * @public\n */\nexport function sliceBlocks({\n context,\n blocks,\n}: {\n context: Pick<EditorContext, 'schema' | 'selection'>\n blocks: Array<PortableTextBlock>\n}): Array<PortableTextBlock> {\n const slice: Array<PortableTextBlock> = []\n\n if (!context.selection) {\n return slice\n }\n\n let startBlock: PortableTextBlock | undefined\n const middleBlocks: PortableTextBlock[] = []\n let endBlock: PortableTextBlock | undefined\n\n const startPoint = getSelectionStartPoint(context.selection)\n const endPoint = getSelectionEndPoint(context.selection)\n const startBlockKey = getBlockKeyFromSelectionPoint(startPoint)\n const startChildKey = getChildKeyFromSelectionPoint(startPoint)\n const endBlockKey = getBlockKeyFromSelectionPoint(endPoint)\n const endChildKey = getChildKeyFromSelectionPoint(endPoint)\n\n if (!startBlockKey || !endBlockKey) {\n return slice\n }\n\n for (const block of blocks) {\n if (!isTextBlock(context, block)) {\n if (block._key === startBlockKey && block._key === endBlockKey) {\n startBlock = block\n break\n }\n }\n\n if (block._key === startBlockKey) {\n if (!isTextBlock(context, block)) {\n startBlock = block\n continue\n }\n\n if (startChildKey) {\n for (const child of block.children) {\n if (child._key === startChildKey) {\n if (isSpan(context, child)) {\n const text =\n child._key === endChildKey\n ? child.text.slice(startPoint.offset, endPoint.offset)\n : child.text.slice(startPoint.offset)\n\n startBlock = {\n ...block,\n children: [\n {\n ...child,\n text,\n },\n ],\n }\n } else {\n startBlock = {\n ...block,\n children: [child],\n }\n }\n\n if (block._key === endBlockKey && startChildKey === endChildKey) {\n break\n }\n continue\n }\n\n if (startBlock && isTextBlock(context, startBlock)) {\n if (\n endChildKey &&\n child._key === endChildKey &&\n isSpan(context, child)\n ) {\n startBlock.children.push({\n ...child,\n text: child.text.slice(0, endPoint.offset),\n })\n } else {\n startBlock.children.push(child)\n }\n\n if (\n block._key === endBlockKey &&\n endChildKey &&\n child._key === endChildKey\n ) {\n break\n }\n }\n }\n\n if (startBlockKey === endBlockKey) {\n break\n }\n\n continue\n }\n\n startBlock = block\n\n if (startBlockKey === endBlockKey) {\n break\n }\n }\n\n if (block._key === endBlockKey) {\n if (!isTextBlock(context, block)) {\n endBlock = block\n break\n }\n\n if (endChildKey) {\n endBlock = {\n ...block,\n children: [],\n }\n\n for (const child of block.children) {\n if (endBlock && isTextBlock(context, endBlock)) {\n if (child._key === endChildKey && isSpan(context, child)) {\n endBlock.children.push({\n ...child,\n text: child.text.slice(0, endPoint.offset),\n })\n\n break\n }\n\n endBlock.children.push(child)\n\n if (endChildKey && child._key === endChildKey) {\n break\n }\n }\n }\n\n break\n }\n\n endBlock = block\n\n break\n }\n\n if (startBlock) {\n middleBlocks.push(\n parseBlock({\n context: {\n ...context,\n keyGenerator: defaultKeyGenerator,\n },\n block,\n options: {removeUnusedMarkDefs: true, validateFields: false},\n }) ?? block,\n )\n }\n }\n\n const parsedStartBlock = startBlock\n ? parseBlock({\n context: {\n ...context,\n keyGenerator: defaultKeyGenerator,\n },\n block: startBlock,\n options: {removeUnusedMarkDefs: true, validateFields: false},\n })\n : undefined\n\n const parsedEndBlock = endBlock\n ? parseBlock({\n context: {\n ...context,\n keyGenerator: defaultKeyGenerator,\n },\n block: endBlock,\n options: {removeUnusedMarkDefs: true, validateFields: false},\n })\n : undefined\n\n return [\n ...(parsedStartBlock ? [parsedStartBlock] : []),\n ...middleBlocks,\n ...(parsedEndBlock ? [parsedEndBlock] : []),\n ]\n}\n","import type {PortableTextTextBlock} from '@sanity/types'\n\n/**\n * @public\n */\nexport function getTextBlockText(block: PortableTextTextBlock) {\n return block.children.map((child) => child.text ?? '').join('')\n}\n"],"names":["getBlockStartPoint","context","block","isTextBlock","node","path","_key","children","offset","isKeyedSegment","segment","getBlockKeyFromSelectionPoint","point","blockPathSegment","at","getChildKeyFromSelectionPoint","childPathSegment","blockOffsetToSpanSelectionPoint","blockOffset","direction","offsetLeft","selectionPoint","skippedInlineObject","value","child","isSpan","text","length","spanSelectionPointToBlockOffset","blockKey","spanKey","isTypedObject","object","isRecord","_type","parseBlocks","blocks","options","Array","isArray","flatMap","parsedBlock","parseBlock","parseTextBlock","parseBlockObject","blockObject","schemaType","schema","blockObjects","find","name","parseObject","keyGenerator","isListBlock","level","undefined","listItem","customFields","key","Object","keys","validateFields","fields","some","field","unparsedMarkDefs","markDefs","markDefKeyMap","Map","markDef","annotations","parsedAnnotation","set","map","parseSpan","span","parseInlineObject","inlineObject","filter","marks","removeUnusedMarkDefs","includes","style","styles","defaultStyle","console","error","lists","list","mark","markDefKey","get","decorators","decorator","inlineObjects","parseAnnotation","annotation","values","reduce","fieldValues","fieldValue","defaultKeyGenerator","randomKey","getByteHexTable","table","i","toString","slice","whatwgRNG","rnds8","Uint8Array","crypto","getRandomValues","str","n","getSelectionEndPoint","selection","backward","anchor","focus","getSelectionStartPoint","sliceBlocks","startBlock","middleBlocks","endBlock","startPoint","endPoint","startBlockKey","startChildKey","endBlockKey","endChildKey","push","parsedStartBlock","parsedEndBlock","getTextBlockText","join"],"mappings":";AASO,SAASA,mBAAmB;AAAA,EACjCC;AAAAA,EACAC;AAOF,GAAyB;AACvB,SAAIC,YAAYF,SAASC,MAAME,IAAI,IAC1B;AAAA,IACLC,MAAM,CAAC,GAAGH,MAAMG,MAAM,YAAY;AAAA,MAACC,MAAMJ,MAAME,KAAKG,SAAS,CAAC,EAAED;AAAAA,IAAAA,CAAK;AAAA,IACrEE,QAAQ;AAAA,EAAA,IAIL;AAAA,IACLH,MAAMH,MAAMG;AAAAA,IACZG,QAAQ;AAAA,EAAA;AAEZ;ACzBO,SAASC,eAAeC,SAA2C;AACxE,SAAO,OAAOA,WAAY,YAAYA,YAAY,QAAQ,UAAUA;AACtE;ACJO,SAASC,8BAA8BC,OAA6B;AACzE,QAAMC,mBAAmBD,MAAMP,KAAKS,GAAG,CAAC;AAExC,MAAIL,eAAeI,gBAAgB;AACjC,WAAOA,iBAAiBP;AAI5B;AAEO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ACL,GAAI6E,mBAAmB,CAACA,gBAAgB,IAAI,CAAA,GAC5C,GAAGT,cACH,GAAIU,iBAAiB,CAACA,cAAc,IAAI,CAAA,CAAG;AAE/C;ACzMO,SAASC,iBAAiBpI,OAA8B;AAC7D,SAAOA,MAAMK,SAASkE,IAAKjD,CAAAA,UAAUA,MAAME,QAAQ,EAAE,EAAE6G,KAAK,EAAE;AAChE;"}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { isTextBlock, isSpan } from "@portabletext/schema";
|
|
2
|
-
import { getTextBlockText } from "./util.get-text-block-text.js";
|
|
3
|
-
function getBlockEndPoint({
|
|
4
|
-
context,
|
|
5
|
-
block
|
|
6
|
-
}) {
|
|
7
|
-
if (isTextBlock(context, block.node)) {
|
|
8
|
-
const lastChild = block.node.children[block.node.children.length - 1];
|
|
9
|
-
if (lastChild)
|
|
10
|
-
return {
|
|
11
|
-
path: [...block.path, "children", {
|
|
12
|
-
_key: lastChild._key
|
|
13
|
-
}],
|
|
14
|
-
offset: isSpan(context, lastChild) ? lastChild.text.length : 0
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
return {
|
|
18
|
-
path: block.path,
|
|
19
|
-
offset: 0
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
function isSelectionCollapsed(selection) {
|
|
23
|
-
return selection ? JSON.stringify(selection.anchor.path) === JSON.stringify(selection.focus.path) && selection.anchor.offset === selection.focus.offset : !1;
|
|
24
|
-
}
|
|
25
|
-
function isEqualSelectionPoints(a, b) {
|
|
26
|
-
return a.offset === b.offset && JSON.stringify(a.path) === JSON.stringify(b.path);
|
|
27
|
-
}
|
|
28
|
-
function isEmptyTextBlock(context, block) {
|
|
29
|
-
if (!isTextBlock(context, block))
|
|
30
|
-
return !1;
|
|
31
|
-
const onlyText = block.children.every((child) => isSpan(context, child)), blockText = getTextBlockText(block);
|
|
32
|
-
return onlyText && blockText === "";
|
|
33
|
-
}
|
|
34
|
-
export {
|
|
35
|
-
getBlockEndPoint,
|
|
36
|
-
isEmptyTextBlock,
|
|
37
|
-
isEqualSelectionPoints,
|
|
38
|
-
isSelectionCollapsed
|
|
39
|
-
};
|
|
40
|
-
//# sourceMappingURL=util.is-empty-text-block.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"util.is-empty-text-block.js","sources":["../../src/utils/util.get-block-end-point.ts","../../src/utils/util.is-selection-collapsed.ts","../../src/utils/util.is-equal-selection-points.ts","../../src/utils/util.is-empty-text-block.ts"],"sourcesContent":["import {isSpan, isTextBlock} from '@portabletext/schema'\nimport type {PortableTextBlock} from '@sanity/types'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport type {EditorSelectionPoint} from '../types/editor'\nimport type {BlockPath} from '../types/paths'\n\n/**\n * @public\n */\nexport function getBlockEndPoint({\n context,\n block,\n}: {\n context: Pick<EditorContext, 'schema'>\n block: {\n node: PortableTextBlock\n path: BlockPath\n }\n}): EditorSelectionPoint {\n if (isTextBlock(context, block.node)) {\n const lastChild = block.node.children[block.node.children.length - 1]\n\n if (lastChild) {\n return {\n path: [...block.path, 'children', {_key: lastChild._key}],\n offset: isSpan(context, lastChild) ? lastChild.text.length : 0,\n }\n }\n }\n\n return {\n path: block.path,\n offset: 0,\n }\n}\n","import type {EditorSelection} from '../types/editor'\n\n/**\n * @public\n */\nexport function isSelectionCollapsed(selection: EditorSelection) {\n if (!selection) {\n return false\n }\n\n return (\n JSON.stringify(selection.anchor.path) ===\n JSON.stringify(selection.focus.path) &&\n selection.anchor.offset === selection.focus.offset\n )\n}\n","import type {EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function isEqualSelectionPoints(\n a: EditorSelectionPoint,\n b: EditorSelectionPoint,\n) {\n return (\n a.offset === b.offset && JSON.stringify(a.path) === JSON.stringify(b.path)\n )\n}\n","import {isSpan, isTextBlock} from '@portabletext/schema'\nimport type {PortableTextBlock} from '@sanity/types'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport {getTextBlockText} from './util.get-text-block-text'\n\n/**\n * @public\n */\nexport function isEmptyTextBlock(\n context: Pick<EditorContext, 'schema'>,\n block: PortableTextBlock,\n) {\n if (!isTextBlock(context, block)) {\n return false\n }\n\n const onlyText = block.children.every((child) => isSpan(context, child))\n const blockText = getTextBlockText(block)\n\n return onlyText && blockText === ''\n}\n"],"names":["getBlockEndPoint","context","block","isTextBlock","node","lastChild","children","length","path","_key","offset","isSpan","text","isSelectionCollapsed","selection","JSON","stringify","anchor","focus","isEqualSelectionPoints","a","b","isEmptyTextBlock","onlyText","every","child","blockText","getTextBlockText"],"mappings":";;AASO,SAASA,iBAAiB;AAAA,EAC/BC;AAAAA,EACAC;AAOF,GAAyB;AACvB,MAAIC,YAAYF,SAASC,MAAME,IAAI,GAAG;AACpC,UAAMC,YAAYH,MAAME,KAAKE,SAASJ,MAAME,KAAKE,SAASC,SAAS,CAAC;AAEpE,QAAIF;AACF,aAAO;AAAA,QACLG,MAAM,CAAC,GAAGN,MAAMM,MAAM,YAAY;AAAA,UAACC,MAAMJ,UAAUI;AAAAA,QAAAA,CAAK;AAAA,QACxDC,QAAQC,OAAOV,SAASI,SAAS,IAAIA,UAAUO,KAAKL,SAAS;AAAA,MAAA;AAAA,EAGnE;AAEA,SAAO;AAAA,IACLC,MAAMN,MAAMM;AAAAA,IACZE,QAAQ;AAAA,EAAA;AAEZ;AC7BO,SAASG,qBAAqBC,WAA4B;AAC/D,SAAKA,YAKHC,KAAKC,UAAUF,UAAUG,OAAOT,IAAI,MAClCO,KAAKC,UAAUF,UAAUI,MAAMV,IAAI,KACrCM,UAAUG,OAAOP,WAAWI,UAAUI,MAAMR,SANrC;AAQX;ACVO,SAASS,uBACdC,GACAC,GACA;AACA,SACED,EAAEV,WAAWW,EAAEX,UAAUK,KAAKC,UAAUI,EAAEZ,IAAI,MAAMO,KAAKC,UAAUK,EAAEb,IAAI;AAE7E;ACJO,SAASc,iBACdrB,SACAC,OACA;AACA,MAAI,CAACC,YAAYF,SAASC,KAAK;AAC7B,WAAO;AAGT,QAAMqB,WAAWrB,MAAMI,SAASkB,MAAOC,CAAAA,UAAUd,OAAOV,SAASwB,KAAK,CAAC,GACjEC,YAAYC,iBAAiBzB,KAAK;AAExC,SAAOqB,YAAYG,cAAc;AACnC;"}
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { blockOffsetToSpanSelectionPoint, getBlockKeyFromSelectionPoint, getChildKeyFromSelectionPoint, parseBlock } from "./util.get-text-block-text.js";
|
|
2
|
-
import { isTextBlock, isSpan } from "@portabletext/schema";
|
|
3
|
-
function blockOffsetToBlockSelectionPoint({
|
|
4
|
-
context,
|
|
5
|
-
blockOffset
|
|
6
|
-
}) {
|
|
7
|
-
let selectionPoint;
|
|
8
|
-
for (const block of context.value)
|
|
9
|
-
if (block._key === blockOffset.path[0]._key) {
|
|
10
|
-
selectionPoint = {
|
|
11
|
-
path: [{
|
|
12
|
-
_key: block._key
|
|
13
|
-
}],
|
|
14
|
-
offset: blockOffset.offset
|
|
15
|
-
};
|
|
16
|
-
break;
|
|
17
|
-
}
|
|
18
|
-
return selectionPoint;
|
|
19
|
-
}
|
|
20
|
-
function blockOffsetToSelectionPoint({
|
|
21
|
-
context,
|
|
22
|
-
blockOffset,
|
|
23
|
-
direction
|
|
24
|
-
}) {
|
|
25
|
-
return blockOffsetToSpanSelectionPoint({
|
|
26
|
-
context,
|
|
27
|
-
blockOffset,
|
|
28
|
-
direction
|
|
29
|
-
}) || blockOffsetToBlockSelectionPoint({
|
|
30
|
-
context,
|
|
31
|
-
blockOffset
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
function blockOffsetsToSelection({
|
|
35
|
-
context,
|
|
36
|
-
offsets,
|
|
37
|
-
backward
|
|
38
|
-
}) {
|
|
39
|
-
const anchor = blockOffsetToSelectionPoint({
|
|
40
|
-
context,
|
|
41
|
-
blockOffset: offsets.anchor,
|
|
42
|
-
direction: backward ? "backward" : "forward"
|
|
43
|
-
}), focus = blockOffsetToSelectionPoint({
|
|
44
|
-
context,
|
|
45
|
-
blockOffset: offsets.focus,
|
|
46
|
-
direction: backward ? "forward" : "backward"
|
|
47
|
-
});
|
|
48
|
-
return !anchor || !focus ? null : {
|
|
49
|
-
anchor,
|
|
50
|
-
focus,
|
|
51
|
-
backward
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
function childSelectionPointToBlockOffset({
|
|
55
|
-
context,
|
|
56
|
-
selectionPoint
|
|
57
|
-
}) {
|
|
58
|
-
let offset = 0;
|
|
59
|
-
const blockKey = getBlockKeyFromSelectionPoint(selectionPoint), childKey = getChildKeyFromSelectionPoint(selectionPoint);
|
|
60
|
-
if (!(!blockKey || !childKey)) {
|
|
61
|
-
for (const block of context.value)
|
|
62
|
-
if (block._key === blockKey && isTextBlock(context, block))
|
|
63
|
-
for (const child of block.children) {
|
|
64
|
-
if (child._key === childKey)
|
|
65
|
-
return {
|
|
66
|
-
path: [{
|
|
67
|
-
_key: block._key
|
|
68
|
-
}],
|
|
69
|
-
offset: offset + selectionPoint.offset
|
|
70
|
-
};
|
|
71
|
-
isSpan(context, child) && (offset += child.text.length);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
function mergeTextBlocks({
|
|
76
|
-
context,
|
|
77
|
-
targetBlock,
|
|
78
|
-
incomingBlock
|
|
79
|
-
}) {
|
|
80
|
-
const parsedIncomingBlock = parseBlock({
|
|
81
|
-
context,
|
|
82
|
-
block: incomingBlock,
|
|
83
|
-
options: {
|
|
84
|
-
removeUnusedMarkDefs: !0,
|
|
85
|
-
validateFields: !1
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
return !parsedIncomingBlock || !isTextBlock(context, parsedIncomingBlock) ? targetBlock : {
|
|
89
|
-
...targetBlock,
|
|
90
|
-
children: [...targetBlock.children, ...parsedIncomingBlock.children],
|
|
91
|
-
markDefs: [...targetBlock.markDefs ?? [], ...parsedIncomingBlock.markDefs ?? []]
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
export {
|
|
95
|
-
blockOffsetToBlockSelectionPoint,
|
|
96
|
-
blockOffsetToSelectionPoint,
|
|
97
|
-
blockOffsetsToSelection,
|
|
98
|
-
childSelectionPointToBlockOffset,
|
|
99
|
-
mergeTextBlocks
|
|
100
|
-
};
|
|
101
|
-
//# sourceMappingURL=util.merge-text-blocks.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"util.merge-text-blocks.js","sources":["../../src/utils/util.block-offset-to-block-selection-point.ts","../../src/utils/util.block-offset-to-selection-point.ts","../../src/utils/util.block-offsets-to-selection.ts","../../src/utils/util.child-selection-point-to-block-offset.ts","../../src/utils/util.merge-text-blocks.ts"],"sourcesContent":["import type {EditorContext} from '../editor/editor-snapshot'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function blockOffsetToBlockSelectionPoint({\n context,\n blockOffset,\n}: {\n context: Pick<EditorContext, 'value'>\n blockOffset: BlockOffset\n}): EditorSelectionPoint | undefined {\n let selectionPoint: EditorSelectionPoint | undefined\n\n for (const block of context.value) {\n if (block._key === blockOffset.path[0]._key) {\n selectionPoint = {\n path: [{_key: block._key}],\n offset: blockOffset.offset,\n }\n break\n }\n }\n\n return selectionPoint\n}\n","import type {EditorContext} from '../editor/editor-snapshot'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelectionPoint} from '../types/editor'\nimport {blockOffsetToSpanSelectionPoint} from './util.block-offset'\nimport {blockOffsetToBlockSelectionPoint} from './util.block-offset-to-block-selection-point'\n\n/**\n * @public\n */\nexport function blockOffsetToSelectionPoint({\n context,\n blockOffset,\n direction,\n}: {\n context: Pick<EditorContext, 'schema' | 'value'>\n blockOffset: BlockOffset\n direction: 'forward' | 'backward'\n}): EditorSelectionPoint | undefined {\n const spanSelectionPoint = blockOffsetToSpanSelectionPoint({\n context,\n blockOffset,\n direction,\n })\n\n if (!spanSelectionPoint) {\n return blockOffsetToBlockSelectionPoint({\n context,\n blockOffset,\n })\n }\n\n return spanSelectionPoint\n}\n","import type {EditorContext} from '../editor/editor-snapshot'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelection} from '../types/editor'\nimport {blockOffsetToSelectionPoint} from './util.block-offset-to-selection-point'\n\n/**\n * @public\n */\nexport function blockOffsetsToSelection({\n context,\n offsets,\n backward,\n}: {\n context: Pick<EditorContext, 'schema' | 'value'>\n offsets: {anchor: BlockOffset; focus: BlockOffset}\n backward?: boolean\n}): EditorSelection {\n const anchor = blockOffsetToSelectionPoint({\n context,\n blockOffset: offsets.anchor,\n direction: backward ? 'backward' : 'forward',\n })\n const focus = blockOffsetToSelectionPoint({\n context,\n blockOffset: offsets.focus,\n direction: backward ? 'forward' : 'backward',\n })\n\n if (!anchor || !focus) {\n return null\n }\n\n return {\n anchor,\n focus,\n backward,\n }\n}\n","import {isSpan, isTextBlock} from '@portabletext/schema'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelectionPoint} from '../types/editor'\nimport {\n getBlockKeyFromSelectionPoint,\n getChildKeyFromSelectionPoint,\n} from './util.selection-point'\n\n/**\n * @public\n */\nexport function childSelectionPointToBlockOffset({\n context,\n selectionPoint,\n}: {\n context: Pick<EditorContext, 'schema' | 'value'>\n selectionPoint: EditorSelectionPoint\n}): BlockOffset | undefined {\n let offset = 0\n\n const blockKey = getBlockKeyFromSelectionPoint(selectionPoint)\n const childKey = getChildKeyFromSelectionPoint(selectionPoint)\n\n if (!blockKey || !childKey) {\n return undefined\n }\n\n for (const block of context.value) {\n if (block._key !== blockKey) {\n continue\n }\n\n if (!isTextBlock(context, block)) {\n continue\n }\n\n for (const child of block.children) {\n if (child._key === childKey) {\n return {\n path: [{_key: block._key}],\n offset: offset + selectionPoint.offset,\n }\n }\n\n if (isSpan(context, child)) {\n offset += child.text.length\n }\n }\n }\n}\n","import {isTextBlock} from '@portabletext/schema'\nimport type {PortableTextTextBlock} from '@sanity/types'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport {parseBlock} from './parse-blocks'\n\n/**\n * @beta\n */\nexport function mergeTextBlocks({\n context,\n targetBlock,\n incomingBlock,\n}: {\n context: Pick<EditorContext, 'keyGenerator' | 'schema'>\n targetBlock: PortableTextTextBlock\n incomingBlock: PortableTextTextBlock\n}) {\n const parsedIncomingBlock = parseBlock({\n context,\n block: incomingBlock,\n options: {removeUnusedMarkDefs: true, validateFields: false},\n })\n\n if (!parsedIncomingBlock || !isTextBlock(context, parsedIncomingBlock)) {\n return targetBlock\n }\n\n return {\n ...targetBlock,\n children: [...targetBlock.children, ...parsedIncomingBlock.children],\n markDefs: [\n ...(targetBlock.markDefs ?? []),\n ...(parsedIncomingBlock.markDefs ?? []),\n ],\n }\n}\n"],"names":["blockOffsetToBlockSelectionPoint","context","blockOffset","selectionPoint","block","value","_key","path","offset","blockOffsetToSelectionPoint","direction","blockOffsetToSpanSelectionPoint","blockOffsetsToSelection","offsets","backward","anchor","focus","childSelectionPointToBlockOffset","blockKey","getBlockKeyFromSelectionPoint","childKey","getChildKeyFromSelectionPoint","isTextBlock","child","children","isSpan","text","length","mergeTextBlocks","targetBlock","incomingBlock","parsedIncomingBlock","parseBlock","options","removeUnusedMarkDefs","validateFields","markDefs"],"mappings":";;AAOO,SAASA,iCAAiC;AAAA,EAC/CC;AAAAA,EACAC;AAIF,GAAqC;AACnC,MAAIC;AAEJ,aAAWC,SAASH,QAAQI;AAC1B,QAAID,MAAME,SAASJ,YAAYK,KAAK,CAAC,EAAED,MAAM;AAC3CH,uBAAiB;AAAA,QACfI,MAAM,CAAC;AAAA,UAACD,MAAMF,MAAME;AAAAA,QAAAA,CAAK;AAAA,QACzBE,QAAQN,YAAYM;AAAAA,MAAAA;AAEtB;AAAA,IACF;AAGF,SAAOL;AACT;AClBO,SAASM,4BAA4B;AAAA,EAC1CR;AAAAA,EACAC;AAAAA,EACAQ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C,WAAW;AAAA,IACrC/B;AAAAA,IACAG,OAAO0B;AAAAA,IACPG,SAAS;AAAA,MAACC,sBAAsB;AAAA,MAAMC,gBAAgB;AAAA,IAAA;AAAA,EAAK,CAC5D;AAED,SAAI,CAACJ,uBAAuB,CAACT,YAAYrB,SAAS8B,mBAAmB,IAC5DF,cAGF;AAAA,IACL,GAAGA;AAAAA,IACHL,UAAU,CAAC,GAAGK,YAAYL,UAAU,GAAGO,oBAAoBP,QAAQ;AAAA,IACnEY,UAAU,CACR,GAAIP,YAAYO,YAAY,CAAA,GAC5B,GAAIL,oBAAoBK,YAAY,CAAA,CAAG;AAAA,EAAA;AAG7C;"}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
2
|
-
import type {EditorActor} from '../editor-machine'
|
|
3
|
-
import {isChangingRemotely} from '../withChanges'
|
|
4
|
-
import {isRedoing, isUndoing} from '../withUndoRedo'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* This plugin makes sure that the PTE maxBlocks prop is respected
|
|
8
|
-
*
|
|
9
|
-
*/
|
|
10
|
-
export function createWithMaxBlocks(editorActor: EditorActor) {
|
|
11
|
-
return function withMaxBlocks(
|
|
12
|
-
editor: PortableTextSlateEditor,
|
|
13
|
-
): PortableTextSlateEditor {
|
|
14
|
-
const {apply} = editor
|
|
15
|
-
editor.apply = (operation) => {
|
|
16
|
-
if (editorActor.getSnapshot().matches({'edit mode': 'read only'})) {
|
|
17
|
-
apply(operation)
|
|
18
|
-
return
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* We don't want to run any side effects when the editor is processing
|
|
23
|
-
* remote changes.
|
|
24
|
-
*/
|
|
25
|
-
if (isChangingRemotely(editor)) {
|
|
26
|
-
apply(operation)
|
|
27
|
-
return
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* We don't want to run any side effects when the editor is undoing or
|
|
32
|
-
* redoing operations.
|
|
33
|
-
*/
|
|
34
|
-
if (isUndoing(editor) || isRedoing(editor)) {
|
|
35
|
-
apply(operation)
|
|
36
|
-
return
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const rows = editorActor.getSnapshot().context.maxBlocks ?? -1
|
|
40
|
-
if (rows > 0 && editor.children.length >= rows) {
|
|
41
|
-
if (
|
|
42
|
-
(operation.type === 'insert_node' ||
|
|
43
|
-
operation.type === 'split_node') &&
|
|
44
|
-
operation.path.length === 1
|
|
45
|
-
) {
|
|
46
|
-
return
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
apply(operation)
|
|
50
|
-
}
|
|
51
|
-
return editor
|
|
52
|
-
}
|
|
53
|
-
}
|