-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.ts
More file actions
169 lines (141 loc) · 7.2 KB
/
Copy patheditor.ts
File metadata and controls
169 lines (141 loc) · 7.2 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { fetchVerseWithExtras, getAslTimecodes } from './cache';
import { VerseModal } from './modal';
import { ViewPlugin, Decoration } from '@codemirror/view';
import { RangeSetBuilder } from '@codemirror/state';
const REF_PATTERN = /\{\{(.+?)\}\}/g;
function buildDecorations(view: any, plugin: any) {
const allDecos: Array<{ from: number; to: number; deco: any }> = [];
const cursor = view.state.selection.main;
const bcvs: Array<{ from: number; to: number; bcv: string }> = [];
for (const { from, to } of view.visibleRanges) {
const text = view.state.doc.sliceString(from, to);
const decorated: Array<{ from: number; to: number }> = [];
let match;
while ((match = REF_PATTERN.exec(text)) !== null) {
const blockStart = from + match.index;
const blockEnd = blockStart + match[0].length;
const innerStart = blockStart + 2;
const innerEnd = blockEnd - 2;
if (cursor.from <= blockEnd && cursor.to >= blockStart) continue;
allDecos.push({ from: blockStart, to: innerStart, deco: Decoration.replace({}) });
allDecos.push({ from: innerEnd, to: blockEnd, deco: Decoration.replace({}) });
decorated.push({ from: blockStart, to: innerStart });
decorated.push({ from: innerEnd, to: blockEnd });
const innerText = match[1];
const cleanMatch = match[0].replace(/\*\*/g, '').replace(/\*/g, '');
const engineInput = cleanMatch.replace('{{', '⟪⟪').replace('}}', '⟫⟫');
const parsed = plugin.safeParse?.(engineInput);
if (!parsed) continue;
const clauses: Array<[string, number, number, string[][]]> = JSON.parse(parsed);
const sorted = [...clauses].sort((a, b) => b[0].length - a[0].length);
for (const clause of sorted) {
const [clauseText, , , ranges] = clause;
if (ranges.length === 0) continue;
const bcv = ranges[0][0] === ranges[0][1] ? ranges[0][0] : `${ranges[0][0]}-${ranges[0][1]}`;
const escaped = clauseText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const refRegex = new RegExp(escaped, 'g');
let refMatch;
while ((refMatch = refRegex.exec(innerText)) !== null) {
const refStart = innerStart + refMatch.index;
const refEnd = refStart + clauseText.length;
const overlaps = decorated.some(p => refStart < p.to && refEnd > p.from);
if (!overlaps) {
allDecos.push({ from: refStart, to: refEnd, deco: Decoration.mark({ class: 'cm-traverture-ref' }) });
decorated.push({ from: refStart, to: refEnd });
bcvs.push({ from: refStart, to: refEnd, bcv });
}
}
}
}
if (plugin.settings.autoDetect) {
const decoratedRanges = [...decorated].sort((a, b) => a.from - b.from);
let pos = from;
for (const d of decoratedRanges) {
if (pos < d.from) {
const segment = view.state.doc.sliceString(pos, d.from);
processSegment(pos, segment, plugin, allDecos, decorated, bcvs);
}
pos = Math.max(pos, d.to);
}
if (pos < to) {
const segment = view.state.doc.sliceString(pos, to);
processSegment(pos, segment, plugin, allDecos, decorated, bcvs);
}
}
}
allDecos.sort((a, b) => a.from - b.from);
const builder: any = new RangeSetBuilder();
for (const d of allDecos) {
builder.add(d.from, d.to, d.deco);
}
plugin._editBcvs = bcvs;
return builder.finish();
}
function processSegment(basePos: number, segment: string, plugin: any, allDecos: Array<{ from: number; to: number; deco: any }>, decorated: Array<{ from: number; to: number }>, bcvs: Array<{ from: number; to: number; bcv: string }>) {
const parsed = plugin.safeParse?.(segment);
if (!parsed) return;
const clauses: Array<[string, number, number, string[][]]> = JSON.parse(parsed);
if (clauses.length === 0) return;
for (const clause of clauses) {
const [, startPos, endPos, ranges] = clause;
if (ranges.length === 0) continue;
const bcv = ranges[0][0] === ranges[0][1] ? ranges[0][0] : `${ranges[0][0]}-${ranges[0][1]}`;
const refStart = basePos + startPos;
const refEnd = basePos + endPos;
const overlaps = decorated.some(p => refStart < p.to && refEnd > p.from);
if (!overlaps) {
allDecos.push({ from: refStart, to: refEnd, deco: Decoration.mark({ class: 'cm-traverture-ref' }) });
decorated.push({ from: refStart, to: refEnd });
bcvs.push({ from: refStart, to: refEnd, bcv });
}
}
}
export function createTravertureEditorPlugin(plugin: any) {
plugin._editBcvs = [];
return ViewPlugin.fromClass(
class {
decorations: any;
constructor(view: any) {
this.decorations = buildDecorations(view, plugin);
}
update(update: any) {
if (update.docChanged || update.selectionSet || update.viewportChanged) {
this.decorations = buildDecorations(update.view, plugin);
}
}
},
{
decorations: (v: any) => v.decorations,
eventHandlers: {
mousedown: (e: MouseEvent, _view: any) => {
if (e.button !== 0) return;
const pos = _view.posAtCoords({ x: e.clientX, y: e.clientY });
if (pos === null) return;
const entry = plugin._editBcvs?.find((b: any) => pos > b.from && pos < b.to);
if (entry) {
e.preventDefault();
e.stopPropagation();
void showModal(plugin, entry.bcv);
}
}
}
}
);
}
async function showModal(plugin: any, bcv: string): Promise<void> {
const parts = bcv.split('-');
const startBcv = parts[0];
const endBcv = parts.length > 1 ? parts[1] : parts[0];
const fmtEngine = new (plugin.engine.constructor)(plugin.settings.sourceLanguage, plugin.settings.outputLanguage, plugin.settings.titleFormat, false);
const decoded = JSON.parse(fmtEngine.decode_scriptures(JSON.stringify([[startBcv, endBcv]])));
const displayText = decoded[0] || bcv;
const timecodes = plugin.settings.outputLanguage === 'ase'
? await getAslTimecodes(bcv)
: undefined;
const modal = new VerseModal();
modal.show({ html: `<p><em>Loading...</em></p>`, citation: displayText }, bcv, plugin.settings.outputLanguage, displayText, timecodes);
void fetchVerseWithExtras(bcv, plugin.settings.outputLanguage, modal.getSignal()).then(verseData => {
if (!modal.isVisible()) return;
modal.show(verseData || { html: `<p><em>Verse lookup unavailable</em></p>`, citation: displayText }, bcv, plugin.settings.outputLanguage, displayText, timecodes);
});
}