-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuseUsfmMarkersForMenu.ts
More file actions
55 lines (52 loc) · 1.91 KB
/
useUsfmMarkersForMenu.ts
File metadata and controls
55 lines (52 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { LexicalEditor } from "lexical";
import { useMemo } from "react";
import { getMarker, GetMarkerAction, ScriptureReference } from "shared";
// getMarker() takes a marker string and gets its data from a usfm markers map object that is merged with overwrites that fit the PERF editor context.
// getMarkerAction() returns a function to generate a LexicalNode and insert it in the editor, this lexical node is a custom node made for the PERF editor
// NOTE: You can create your own typeahead plugin by creating your own getMarker() and getMarkerAction() functions adapted to your editor needs.
export default function useUsfmMarkersForMenu({
scriptureReference,
contextMarker,
getMarkerAction,
autoNumbering,
}: {
scriptureReference: ScriptureReference;
contextMarker: string | undefined;
getMarkerAction: GetMarkerAction;
autoNumbering?: boolean;
}) {
const markersMenuItems = useMemo(() => {
if (!contextMarker || !scriptureReference) return;
const marker = getMarker(contextMarker);
if (!marker?.children) return;
return Object.values(marker.children).flatMap((markers) =>
markers.map((marker) => {
const markerData = getMarker(marker);
const { action } = getMarkerAction(marker, markerData);
return {
name: marker,
label: marker,
description: markerData?.description ?? "",
action: ({
editor,
newVerseRChapterNum,
noteText,
}: {
editor: LexicalEditor;
newVerseRChapterNum?: number;
noteText?: string;
}) => {
action({
editor,
reference: scriptureReference,
autoNumbering,
newVerseRChapterNum,
noteText,
});
},
};
}),
);
}, [autoNumbering, contextMarker, getMarkerAction, scriptureReference]);
return { markersMenuItems };
}