@portabletext/plugin-sdk-value 3.0.34 → 3.0.35
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.map +1 -1
- package/package.json +6 -16
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) {\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\
|
|
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\nexport function 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\nexport function 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;AAEO,SAASC,aAAaC,UAAwB;AACnD,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,UAAM,CAACk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AA,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.35",
|
|
4
4
|
"description": "Synchronizes the Portable Text Editor value with the Sanity SDK, allowing for two-way editing.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"portabletext",
|
|
@@ -46,30 +46,23 @@
|
|
|
46
46
|
"@sanity/sdk-react": "^2.1.2",
|
|
47
47
|
"@sanity/tsconfig": "^2.0.0",
|
|
48
48
|
"@sanity/types": "^4.21.1",
|
|
49
|
-
"@testing-library/react": "^16.3.0",
|
|
50
|
-
"@testing-library/user-event": "^14.6.1",
|
|
51
49
|
"@types/react": "^19.2.7",
|
|
52
|
-
"@vitejs/plugin-react": "^5.1.1",
|
|
53
|
-
"@vitest/browser": "^4.0.14",
|
|
54
|
-
"@vitest/browser-playwright": "^4.0.14",
|
|
55
50
|
"babel-plugin-react-compiler": "^1.0.0",
|
|
56
51
|
"eslint": "^9.39.1",
|
|
57
52
|
"eslint-plugin-react-hooks": "^7.0.1",
|
|
58
|
-
"playwright": "^1.57.0",
|
|
59
53
|
"react": "^19.2.3",
|
|
60
54
|
"react-dom": "^19.2.3",
|
|
61
|
-
"react-error-boundary": "^6.0.0",
|
|
62
55
|
"typescript": "5.9.3",
|
|
63
56
|
"typescript-eslint": "^8.48.0",
|
|
64
57
|
"vitest": "^4.0.14",
|
|
65
|
-
"@portabletext/editor": "^3.3.
|
|
58
|
+
"@portabletext/editor": "^3.3.15",
|
|
66
59
|
"@portabletext/patches": "^2.0.2"
|
|
67
60
|
},
|
|
68
61
|
"peerDependencies": {
|
|
69
62
|
"@sanity/sdk-react": "^2.1.2",
|
|
70
63
|
"react": "^18.3 || ^19",
|
|
71
64
|
"react-dom": "^18.3 || ^19",
|
|
72
|
-
"@portabletext/editor": "^3.3.
|
|
65
|
+
"@portabletext/editor": "^3.3.15"
|
|
73
66
|
},
|
|
74
67
|
"engines": {
|
|
75
68
|
"node": ">=20.19 <22 || >=22.12"
|
|
@@ -83,11 +76,8 @@
|
|
|
83
76
|
"clean": "del .turbo && del dist && del node_modules",
|
|
84
77
|
"dev": "pkg-utils watch",
|
|
85
78
|
"lint:fix": "biome lint --write .",
|
|
86
|
-
"test:
|
|
87
|
-
"test:
|
|
88
|
-
"test:
|
|
89
|
-
"test:browser:firefox:watch": "vitest watch --project \"browser (firefox)\"",
|
|
90
|
-
"test:browser:webkit": "vitest --run --project \"browser (webkit)\"",
|
|
91
|
-
"test:browser:webkit:watch": "vitest watch --project \"browser (webkit)\""
|
|
79
|
+
"test:unit": "vitest --run --project unit",
|
|
80
|
+
"test:unit:watch": "vitest --project unit",
|
|
81
|
+
"test:watch": "vitest"
|
|
92
82
|
}
|
|
93
83
|
}
|