Skip to content

Commit 5aef764

Browse files
authored
Revert "BREAKING CHANGE: use JSONPath indexes (#268)" (#273)
- This partially reverts commit d8ebcfc. - `README.md`, `App.tsx`, & `selection.model.ts` were reverted, other files modified to suit.
1 parent 3670ef9 commit 5aef764

File tree

6 files changed

+23
-16
lines changed

6 files changed

+23
-16
lines changed

packages/platform/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ const nodeOptions: UsjNodeOptions = { [immutableNoteCallerNodeName]: { onClick:
5959
const options: EditorOptions = { isReadonly: false, textDirection: "ltr", nodes: nodeOptions };
6060
// Word "man" inside first q1 of PSA 1:1.
6161
const annotationRange1 = {
62-
start: { jsonPathIndexes: [3, 1], offset: 15 },
63-
end: { jsonPathIndexes: [3, 1], offset: 18 },
62+
start: { jsonPath: "$.content[3].content[1]", offset: 15 },
63+
end: { jsonPath: "$.content[3].content[1]", offset: 18 },
6464
};
6565
// Phrase "man who" inside first q1 of PSA 1:1.
6666
const annotationRange2 = {
67-
start: { jsonPathIndexes: [3, 1], offset: 15 },
68-
end: { jsonPathIndexes: [3, 1], offset: 22 },
67+
start: { jsonPath: "$.content[3].content[1]", offset: 15 },
68+
end: { jsonPath: "$.content[3].content[1]", offset: 22 },
6969
};
70-
const cursorLocation = { start: { jsonPathIndexes: [3, 1], offset: 15 } };
70+
const cursorLocation = { start: { jsonPath: "$.content[3].content[1]", offset: 15 } };
7171

7272
export default function App() {
7373
const marginalRef = useRef<MarginalRef | null>(null);

packages/platform/src/App.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ const nodeOptions: UsjNodeOptions = {
3636
};
3737
// Word "man" inside first q1 of PSA 1:1.
3838
const annotationRange1 = {
39-
start: { jsonPathIndexes: [10, 2], offset: 15 },
40-
end: { jsonPathIndexes: [10, 2], offset: 18 },
39+
start: { jsonPath: "$.content[10].content[2]", offset: 15 },
40+
end: { jsonPath: "$.content[10].content[2]", offset: 18 },
4141
};
4242
// Phrase "man who" inside first q1 of PSA 1:1.
4343
const annotationRange2 = {
44-
start: { jsonPathIndexes: [10, 2], offset: 15 },
45-
end: { jsonPathIndexes: [10, 2], offset: 22 },
44+
start: { jsonPath: "$.content[10].content[2]", offset: 15 },
45+
end: { jsonPath: "$.content[10].content[2]", offset: 22 },
4646
};
4747
// Word "stand" inside first q2 of PSA 1:1.
4848
const annotationRange3 = {
49-
start: { jsonPathIndexes: [11, 0], offset: 4 },
50-
end: { jsonPathIndexes: [11, 0], offset: 9 },
49+
start: { jsonPath: "$.content[11].content[0]", offset: 4 },
50+
end: { jsonPath: "$.content[11].content[0]", offset: 9 },
5151
};
5252
const defaultAnnotations: Annotations = {
5353
annotation1: {

packages/shared-react/annotation/selection.model.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
export type UsjLocation = {
2-
/* JsonPath indexes of the location in the USJ, e.g. JsonPath "$.content[1].content[2]" has indexes `[1, 2]` */
3-
jsonPathIndexes: number[];
4-
/* Offset of the location in the text */
2+
jsonPath: string;
53
offset: number;
64
};
75

packages/shared-react/annotation/selection.utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
indexesFromUsjJsonPath,
3+
usjJsonPathFromIndexes,
4+
} from "@biblionexus-foundation/scripture-utilities";
15
import {
26
$createPoint,
37
$createRangeSelection,
@@ -48,8 +52,9 @@ function $findTextNodeInMarks(
4852
function $getNodeFromLocation(
4953
location: UsjLocation,
5054
): [LexicalNode | undefined, number | undefined] {
55+
const jsonPathIndexes = indexesFromUsjJsonPath(location.jsonPath);
5156
let currentNode: LexicalNode | undefined = $getRoot();
52-
for (const index of location.jsonPathIndexes) {
57+
for (const index of jsonPathIndexes) {
5358
if (!currentNode || !$isElementNode(currentNode)) return [undefined, undefined];
5459

5560
currentNode = currentNode.getChildAtIndex(index) ?? undefined;
@@ -111,7 +116,7 @@ function getLocationFromNode(node: LexicalNode, offset: number): UsjLocation {
111116
}
112117
current = parent;
113118
}
114-
return { jsonPathIndexes, offset };
119+
return { jsonPath: usjJsonPathFromIndexes(jsonPathIndexes), offset };
115120
}
116121

117122
/**

packages/shared-react/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"react-dom": ">=18.3.1"
1717
},
1818
"dependencies": {
19+
"@biblionexus-foundation/scripture-utilities": "workspace:*",
1920
"@floating-ui/dom": "^1.6.13",
2021
"@lexical/react": "^0.27.2",
2122
"@lexical/utils": "^0.27.2",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)