@portabletext/plugin-sdk-value 3.0.19 → 3.0.21
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/dist/index.js +22 -11
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/plugin.sdk-value.tsx +54 -18
package/dist/index.js
CHANGED
|
@@ -13,20 +13,30 @@ function isKeyPath(node) {
|
|
|
13
13
|
}
|
|
14
14
|
function arrayifyPath(pathExpr) {
|
|
15
15
|
const node = parsePath(pathExpr);
|
|
16
|
-
if (!node)
|
|
17
|
-
|
|
16
|
+
if (!node)
|
|
17
|
+
return [];
|
|
18
|
+
if (node.type !== "Path")
|
|
19
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE);
|
|
18
20
|
return Array.from(getSegments(node)).map((segment) => {
|
|
19
|
-
if (segment.type === "Identifier")
|
|
20
|
-
|
|
21
|
-
if (segment.
|
|
21
|
+
if (segment.type === "Identifier")
|
|
22
|
+
return segment.name;
|
|
23
|
+
if (segment.type !== "Subscript")
|
|
24
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE);
|
|
25
|
+
if (segment.elements.length !== 1)
|
|
26
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE);
|
|
22
27
|
const [element] = segment.elements;
|
|
23
|
-
if (element.type === "Number")
|
|
24
|
-
|
|
25
|
-
if (element.
|
|
28
|
+
if (element.type === "Number")
|
|
29
|
+
return element.value;
|
|
30
|
+
if (element.type !== "Comparison")
|
|
31
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE);
|
|
32
|
+
if (element.operator !== "==")
|
|
33
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE);
|
|
26
34
|
const keyPathNode = [element.left, element.right].find(isKeyPath);
|
|
27
|
-
if (!keyPathNode)
|
|
35
|
+
if (!keyPathNode)
|
|
36
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE);
|
|
28
37
|
const other = element.left === keyPathNode ? element.right : element.left;
|
|
29
|
-
if (other.type !== "String")
|
|
38
|
+
if (other.type !== "String")
|
|
39
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE);
|
|
30
40
|
return {
|
|
31
41
|
_key: other.value
|
|
32
42
|
};
|
|
@@ -58,7 +68,8 @@ function convertPatches(patches) {
|
|
|
58
68
|
items,
|
|
59
69
|
...rest
|
|
60
70
|
} = values, position = Object.keys(rest).at(0);
|
|
61
|
-
if (!position)
|
|
71
|
+
if (!position)
|
|
72
|
+
return [];
|
|
62
73
|
const pathExpr = rest[position];
|
|
63
74
|
return [{
|
|
64
75
|
type,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/plugin.sdk-value.tsx"],"sourcesContent":["import {\n useEditor,\n type PortableTextBlock,\n type Patch as PtePatch,\n} from '@portabletext/editor'\nimport type {\n JSONValue,\n Path,\n PathSegment,\n InsertPatch as PteInsertPatch,\n} from '@portabletext/patches'\nimport {diffValue, type SanityPatchOperations} from '@sanity/diff-patch'\nimport {\n parsePath,\n type ExprNode,\n type PathNode,\n type SegmentNode,\n type ThisNode,\n} from '@sanity/json-match'\nimport {\n getDocumentState,\n useEditDocument,\n useSanityInstance,\n type DocumentHandle,\n} from '@sanity/sdk-react'\nimport {useEffect} from 'react'\n\ninterface SDKValuePluginProps extends DocumentHandle {\n path: string\n}\n\ntype InsertPatch = Required<Pick<SanityPatchOperations, 'insert'>>\n\nconst ARRAYIFY_ERROR_MESSAGE =\n 'Unexpected path format from diffValue output. Please report this issue.'\n\nfunction* getSegments(\n node: PathNode,\n): Generator<Exclude<SegmentNode, ThisNode>> {\n if (node.base) yield* getSegments(node.base)\n if (node.segment.type !== 'This') yield node.segment\n}\n\nfunction isKeyPath(node: ExprNode): node is PathNode {\n if (node.type !== 'Path') return false\n if (node.base) return false\n if (node.recursive) return false\n if (node.segment.type !== 'Identifier') return false\n return node.segment.name === '_key'\n}\n\nfunction arrayifyPath(pathExpr: string): Path {\n const node = parsePath(pathExpr)\n if (!node) return []\n if (node.type !== 'Path') throw new Error(ARRAYIFY_ERROR_MESSAGE)\n\n return Array.from(getSegments(node)).map((segment): PathSegment => {\n if (segment.type === 'Identifier') return segment.name\n if (segment.type !== 'Subscript') throw new Error(ARRAYIFY_ERROR_MESSAGE)\n if (segment.elements.length !== 1) throw new Error(ARRAYIFY_ERROR_MESSAGE)\n\n const [element] = segment.elements\n if (element.type === 'Number') return element.value\n\n if (element.type !== 'Comparison') throw new Error(ARRAYIFY_ERROR_MESSAGE)\n if (element.operator !== '==') throw new Error(ARRAYIFY_ERROR_MESSAGE)\n const keyPathNode = [element.left, element.right].find(isKeyPath)\n if (!keyPathNode) throw new Error(ARRAYIFY_ERROR_MESSAGE)\n const other = element.left === keyPathNode ? element.right : element.left\n if (other.type !== 'String') throw new Error(ARRAYIFY_ERROR_MESSAGE)\n return {_key: other.value}\n })\n}\n\nfunction convertPatches(patches: SanityPatchOperations[]): PtePatch[] {\n return patches.flatMap((p) => {\n return Object.entries(p).flatMap(([type, values]): PtePatch[] => {\n const origin = 'remote'\n\n switch (type) {\n case 'set':\n case 'setIfMissing':\n case 'diffMatchPatch':\n case 'inc':\n case 'dec': {\n return Object.entries(values).map(\n ([pathExpr, value]) =>\n ({type, value, origin, path: arrayifyPath(pathExpr)}) as PtePatch,\n )\n }\n case 'unset': {\n if (!Array.isArray(values)) return []\n return values.map(arrayifyPath).map((path) => ({type, origin, path}))\n }\n case 'insert': {\n const {items, ...rest} = values as InsertPatch['insert']\n type InsertPosition = PteInsertPatch['position']\n const position = Object.keys(rest).at(0) as InsertPosition | undefined\n\n if (!position) return []\n const pathExpr = (rest as {[K in InsertPosition]: string})[position]\n const insertPatch: PteInsertPatch = {\n type,\n origin,\n position,\n path: arrayifyPath(pathExpr),\n items: items as JSONValue[],\n }\n\n return [insertPatch]\n }\n\n default: {\n return []\n }\n }\n })\n })\n}\n/**\n * @public\n */\nexport function SDKValuePlugin(props: SDKValuePluginProps) {\n // NOTE: the real `useEditDocument` suspends until the document is loaded into the SDK store\n const setSdkValue = useEditDocument(props)\n const instance = useSanityInstance(props)\n const editor = useEditor()\n\n useEffect(() => {\n const getEditorValue = () => editor.getSnapshot().context.value\n const {getCurrent: getSdkValue, subscribe: onSdkValueChange} =\n getDocumentState<PortableTextBlock[]>(instance, props)\n\n const editorSubscription = editor.on('patch', () =>\n setSdkValue(getEditorValue()),\n )\n const unsubscribeToEditorChanges = () => editorSubscription.unsubscribe()\n const unsubscribeToSdkChanges = onSdkValueChange(() => {\n const snapshot = getEditorValue()\n const patches = convertPatches(diffValue(snapshot, getSdkValue()))\n\n if (patches.length) {\n editor.send({type: 'patches', patches, snapshot})\n }\n })\n\n // update initial value\n editor.send({type: 'update value', value: getSdkValue() ?? []})\n\n return () => {\n unsubscribeToEditorChanges()\n unsubscribeToSdkChanges()\n }\n }, [editor, instance, props, setSdkValue])\n\n return null\n}\n"],"names":["ARRAYIFY_ERROR_MESSAGE","getSegments","node","base","segment","type","isKeyPath","recursive","name","arrayifyPath","pathExpr","parsePath","Error","Array","from","map","elements","length","element","value","operator","keyPathNode","left","right","find","other","_key","convertPatches","patches","flatMap","p","Object","entries","values","origin","path","isArray","items","rest","position","keys","at","SDKValuePlugin","props","$","_c","setSdkValue","useEditDocument","instance","useSanityInstance","editor","useEditor","t0","t1","getEditorValue","getSnapshot","context","getCurrent","getSdkValue","subscribe","onSdkValueChange","getDocumentState","editorSubscription","on","unsubscribeToEditorChanges","unsubscribe","unsubscribeToSdkChanges","snapshot","diffValue","send","useEffect"],"mappings":";;;;;;AAiCA,MAAMA,yBACJ;AAEF,UAAUC,YACRC,MAC2C;AACvCA,OAAKC,
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/plugin.sdk-value.tsx"],"sourcesContent":["import {\n useEditor,\n type PortableTextBlock,\n type Patch as PtePatch,\n} from '@portabletext/editor'\nimport type {\n JSONValue,\n Path,\n PathSegment,\n InsertPatch as PteInsertPatch,\n} from '@portabletext/patches'\nimport {diffValue, type SanityPatchOperations} from '@sanity/diff-patch'\nimport {\n parsePath,\n type ExprNode,\n type PathNode,\n type SegmentNode,\n type ThisNode,\n} from '@sanity/json-match'\nimport {\n getDocumentState,\n useEditDocument,\n useSanityInstance,\n type DocumentHandle,\n} from '@sanity/sdk-react'\nimport {useEffect} from 'react'\n\ninterface SDKValuePluginProps extends DocumentHandle {\n path: string\n}\n\ntype InsertPatch = Required<Pick<SanityPatchOperations, 'insert'>>\n\nconst ARRAYIFY_ERROR_MESSAGE =\n 'Unexpected path format from diffValue output. Please report this issue.'\n\nfunction* getSegments(\n node: PathNode,\n): Generator<Exclude<SegmentNode, ThisNode>> {\n if (node.base) {\n yield* getSegments(node.base)\n }\n if (node.segment.type !== 'This') {\n yield node.segment\n }\n}\n\nfunction isKeyPath(node: ExprNode): node is PathNode {\n if (node.type !== 'Path') {\n return false\n }\n if (node.base) {\n return false\n }\n if (node.recursive) {\n return false\n }\n if (node.segment.type !== 'Identifier') {\n return false\n }\n return node.segment.name === '_key'\n}\n\nfunction arrayifyPath(pathExpr: string): Path {\n const node = parsePath(pathExpr)\n if (!node) {\n return []\n }\n if (node.type !== 'Path') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n\n return Array.from(getSegments(node)).map((segment): PathSegment => {\n if (segment.type === 'Identifier') {\n return segment.name\n }\n if (segment.type !== 'Subscript') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n if (segment.elements.length !== 1) {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n\n const [element] = segment.elements\n if (element.type === 'Number') {\n return element.value\n }\n\n if (element.type !== 'Comparison') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n if (element.operator !== '==') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n const keyPathNode = [element.left, element.right].find(isKeyPath)\n if (!keyPathNode) {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n const other = element.left === keyPathNode ? element.right : element.left\n if (other.type !== 'String') {\n throw new Error(ARRAYIFY_ERROR_MESSAGE)\n }\n return {_key: other.value}\n })\n}\n\nfunction convertPatches(patches: SanityPatchOperations[]): PtePatch[] {\n return patches.flatMap((p) => {\n return Object.entries(p).flatMap(([type, values]): PtePatch[] => {\n const origin = 'remote'\n\n switch (type) {\n case 'set':\n case 'setIfMissing':\n case 'diffMatchPatch':\n case 'inc':\n case 'dec': {\n return Object.entries(values).map(\n ([pathExpr, value]) =>\n ({type, value, origin, path: arrayifyPath(pathExpr)}) as PtePatch,\n )\n }\n case 'unset': {\n if (!Array.isArray(values)) {\n return []\n }\n return values.map(arrayifyPath).map((path) => ({type, origin, path}))\n }\n case 'insert': {\n const {items, ...rest} = values as InsertPatch['insert']\n type InsertPosition = PteInsertPatch['position']\n const position = Object.keys(rest).at(0) as InsertPosition | undefined\n\n if (!position) {\n return []\n }\n const pathExpr = (rest as {[K in InsertPosition]: string})[position]\n const insertPatch: PteInsertPatch = {\n type,\n origin,\n position,\n path: arrayifyPath(pathExpr),\n items: items as JSONValue[],\n }\n\n return [insertPatch]\n }\n\n default: {\n return []\n }\n }\n })\n })\n}\n/**\n * @public\n */\nexport function SDKValuePlugin(props: SDKValuePluginProps) {\n // NOTE: the real `useEditDocument` suspends until the document is loaded into the SDK store\n const setSdkValue = useEditDocument(props)\n const instance = useSanityInstance(props)\n const editor = useEditor()\n\n useEffect(() => {\n const getEditorValue = () => editor.getSnapshot().context.value\n const {getCurrent: getSdkValue, subscribe: onSdkValueChange} =\n getDocumentState<PortableTextBlock[]>(instance, props)\n\n const editorSubscription = editor.on('patch', () =>\n setSdkValue(getEditorValue()),\n )\n const unsubscribeToEditorChanges = () => editorSubscription.unsubscribe()\n const unsubscribeToSdkChanges = onSdkValueChange(() => {\n const snapshot = getEditorValue()\n const patches = convertPatches(diffValue(snapshot, getSdkValue()))\n\n if (patches.length) {\n editor.send({type: 'patches', patches, snapshot})\n }\n })\n\n // update initial value\n editor.send({type: 'update value', value: getSdkValue() ?? []})\n\n return () => {\n unsubscribeToEditorChanges()\n unsubscribeToSdkChanges()\n }\n }, [editor, instance, props, setSdkValue])\n\n return null\n}\n"],"names":["ARRAYIFY_ERROR_MESSAGE","getSegments","node","base","segment","type","isKeyPath","recursive","name","arrayifyPath","pathExpr","parsePath","Error","Array","from","map","elements","length","element","value","operator","keyPathNode","left","right","find","other","_key","convertPatches","patches","flatMap","p","Object","entries","values","origin","path","isArray","items","rest","position","keys","at","SDKValuePlugin","props","$","_c","setSdkValue","useEditDocument","instance","useSanityInstance","editor","useEditor","t0","t1","getEditorValue","getSnapshot","context","getCurrent","getSdkValue","subscribe","onSdkValueChange","getDocumentState","editorSubscription","on","unsubscribeToEditorChanges","unsubscribe","unsubscribeToSdkChanges","snapshot","diffValue","send","useEffect"],"mappings":";;;;;;AAiCA,MAAMA,yBACJ;AAEF,UAAUC,YACRC,MAC2C;AACvCA,OAAKC,SACP,OAAOF,YAAYC,KAAKC,IAAI,IAE1BD,KAAKE,QAAQC,SAAS,WACxB,MAAMH,KAAKE;AAEf;AAEA,SAASE,UAAUJ,MAAkC;AAUnD,SATIA,KAAKG,SAAS,UAGdH,KAAKC,QAGLD,KAAKK,aAGLL,KAAKE,QAAQC,SAAS,eACjB,KAEFH,KAAKE,QAAQI,SAAS;AAC/B;AAEA,SAASC,aAAaC,UAAwB;AAC5C,QAAMR,OAAOS,UAAUD,QAAQ;AAC/B,MAAI,CAACR;AACH,WAAO,CAAA;AAET,MAAIA,KAAKG,SAAS;AAChB,UAAM,IAAIO,MAAMZ,sBAAsB;AAGxC,SAAOa,MAAMC,KAAKb,YAAYC,IAAI,CAAC,EAAEa,IAAKX,CAAAA,YAAyB;AACjE,QAAIA,QAAQC,SAAS;AACnB,aAAOD,QAAQI;AAEjB,QAAIJ,QAAQC,SAAS;AACnB,YAAM,IAAIO,MAAMZ,sBAAsB;AAExC,QAAII,QAAQY,SAASC,WAAW;AAC9B,YAAM,IAAIL,MAAMZ,sBAAsB;AAGxC,UA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AAA,KAAA,CAAA;AAAA,IAAA,CAAoB,GAEvD,MAAA;AACLM,iCAAAA,GACAE,wBAAAA;AAAAA,IAAyB;AAAA,EAC1B,GACAb,KAAA,CAACH,QAAQF,UAAUL,OAAOG,WAAW,GAACF,OAAAM,QAAAN,OAAAI,UAAAJ,OAAAD,OAAAC,OAAAE,aAAAF,OAAAQ,IAAAR,OAAAS,OAAAD,KAAAR,EAAA,CAAA,GAAAS,KAAAT,EAAA,CAAA,IAzBzC0B,UAAUlB,IAyBPC,EAAsC,GAElC;AAAI;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portabletext/plugin-sdk-value",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.21",
|
|
4
4
|
"description": "Synchronizes the Portable Text Editor value with the Sanity SDK, allowing for two-way editing.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"portabletext",
|
|
@@ -63,14 +63,14 @@
|
|
|
63
63
|
"typescript": "5.9.3",
|
|
64
64
|
"typescript-eslint": "^8.48.0",
|
|
65
65
|
"vitest": "^4.0.14",
|
|
66
|
-
"@portabletext/editor": "^3.
|
|
66
|
+
"@portabletext/editor": "^3.3.1",
|
|
67
67
|
"@portabletext/patches": "^2.0.0"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
70
|
"@sanity/sdk-react": "^2.1.2",
|
|
71
71
|
"react": "^18.3 || ^19",
|
|
72
72
|
"react-dom": "^18.3 || ^19",
|
|
73
|
-
"@portabletext/editor": "^3.
|
|
73
|
+
"@portabletext/editor": "^3.3.1"
|
|
74
74
|
},
|
|
75
75
|
"engines": {
|
|
76
76
|
"node": ">=20.19 <22 || >=22.12"
|
package/src/plugin.sdk-value.tsx
CHANGED
|
@@ -37,37 +37,69 @@ const ARRAYIFY_ERROR_MESSAGE =
|
|
|
37
37
|
function* getSegments(
|
|
38
38
|
node: PathNode,
|
|
39
39
|
): Generator<Exclude<SegmentNode, ThisNode>> {
|
|
40
|
-
if (node.base)
|
|
41
|
-
|
|
40
|
+
if (node.base) {
|
|
41
|
+
yield* getSegments(node.base)
|
|
42
|
+
}
|
|
43
|
+
if (node.segment.type !== 'This') {
|
|
44
|
+
yield node.segment
|
|
45
|
+
}
|
|
42
46
|
}
|
|
43
47
|
|
|
44
48
|
function isKeyPath(node: ExprNode): node is PathNode {
|
|
45
|
-
if (node.type !== 'Path')
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (node.
|
|
49
|
+
if (node.type !== 'Path') {
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
if (node.base) {
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
if (node.recursive) {
|
|
56
|
+
return false
|
|
57
|
+
}
|
|
58
|
+
if (node.segment.type !== 'Identifier') {
|
|
59
|
+
return false
|
|
60
|
+
}
|
|
49
61
|
return node.segment.name === '_key'
|
|
50
62
|
}
|
|
51
63
|
|
|
52
64
|
function arrayifyPath(pathExpr: string): Path {
|
|
53
65
|
const node = parsePath(pathExpr)
|
|
54
|
-
if (!node)
|
|
55
|
-
|
|
66
|
+
if (!node) {
|
|
67
|
+
return []
|
|
68
|
+
}
|
|
69
|
+
if (node.type !== 'Path') {
|
|
70
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE)
|
|
71
|
+
}
|
|
56
72
|
|
|
57
73
|
return Array.from(getSegments(node)).map((segment): PathSegment => {
|
|
58
|
-
if (segment.type === 'Identifier')
|
|
59
|
-
|
|
60
|
-
|
|
74
|
+
if (segment.type === 'Identifier') {
|
|
75
|
+
return segment.name
|
|
76
|
+
}
|
|
77
|
+
if (segment.type !== 'Subscript') {
|
|
78
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE)
|
|
79
|
+
}
|
|
80
|
+
if (segment.elements.length !== 1) {
|
|
81
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE)
|
|
82
|
+
}
|
|
61
83
|
|
|
62
84
|
const [element] = segment.elements
|
|
63
|
-
if (element.type === 'Number')
|
|
85
|
+
if (element.type === 'Number') {
|
|
86
|
+
return element.value
|
|
87
|
+
}
|
|
64
88
|
|
|
65
|
-
if (element.type !== 'Comparison')
|
|
66
|
-
|
|
89
|
+
if (element.type !== 'Comparison') {
|
|
90
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE)
|
|
91
|
+
}
|
|
92
|
+
if (element.operator !== '==') {
|
|
93
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE)
|
|
94
|
+
}
|
|
67
95
|
const keyPathNode = [element.left, element.right].find(isKeyPath)
|
|
68
|
-
if (!keyPathNode)
|
|
96
|
+
if (!keyPathNode) {
|
|
97
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE)
|
|
98
|
+
}
|
|
69
99
|
const other = element.left === keyPathNode ? element.right : element.left
|
|
70
|
-
if (other.type !== 'String')
|
|
100
|
+
if (other.type !== 'String') {
|
|
101
|
+
throw new Error(ARRAYIFY_ERROR_MESSAGE)
|
|
102
|
+
}
|
|
71
103
|
return {_key: other.value}
|
|
72
104
|
})
|
|
73
105
|
}
|
|
@@ -89,7 +121,9 @@ function convertPatches(patches: SanityPatchOperations[]): PtePatch[] {
|
|
|
89
121
|
)
|
|
90
122
|
}
|
|
91
123
|
case 'unset': {
|
|
92
|
-
if (!Array.isArray(values))
|
|
124
|
+
if (!Array.isArray(values)) {
|
|
125
|
+
return []
|
|
126
|
+
}
|
|
93
127
|
return values.map(arrayifyPath).map((path) => ({type, origin, path}))
|
|
94
128
|
}
|
|
95
129
|
case 'insert': {
|
|
@@ -97,7 +131,9 @@ function convertPatches(patches: SanityPatchOperations[]): PtePatch[] {
|
|
|
97
131
|
type InsertPosition = PteInsertPatch['position']
|
|
98
132
|
const position = Object.keys(rest).at(0) as InsertPosition | undefined
|
|
99
133
|
|
|
100
|
-
if (!position)
|
|
134
|
+
if (!position) {
|
|
135
|
+
return []
|
|
136
|
+
}
|
|
101
137
|
const pathExpr = (rest as {[K in InsertPosition]: string})[position]
|
|
102
138
|
const insertPatch: PteInsertPatch = {
|
|
103
139
|
type,
|