@daasboard/editor 0.0.0-stage → 0.1.0
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/LICENSE +5 -0
- package/README.md +6 -2
- package/dist/camera/CameraInput.d.ts +106 -0
- package/dist/camera/CameraInput.js +398 -0
- package/dist/clipboard/ObjectClipboard.d.ts +40 -0
- package/dist/clipboard/ObjectClipboard.js +243 -0
- package/dist/clipboard/PasteCommitError.d.ts +4 -0
- package/dist/clipboard/PasteCommitError.js +7 -0
- package/dist/clipboard/carrier.d.ts +5 -0
- package/dist/clipboard/carrier.js +59 -0
- package/dist/clipboard/objects.d.ts +21 -0
- package/dist/clipboard/objects.js +362 -0
- package/dist/clipboard/placement.d.ts +11 -0
- package/dist/clipboard/placement.js +21 -0
- package/dist/clipboard/systemImages.d.ts +1 -0
- package/dist/clipboard/systemImages.js +24 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +22 -0
- package/dist/input/TouchInput.d.ts +159 -0
- package/dist/input/TouchInput.js +656 -0
- package/dist/input/visiblePart.d.ts +8 -0
- package/dist/input/visiblePart.js +15 -0
- package/dist/interaction/InteractionLayer.d.ts +83 -0
- package/dist/interaction/InteractionLayer.js +520 -0
- package/dist/props/PropertyController.d.ts +130 -0
- package/dist/props/PropertyController.js +1448 -0
- package/dist/props/PropertyRegistry.d.ts +38 -0
- package/dist/props/PropertyRegistry.js +21 -0
- package/dist/props/builtinProperties.d.ts +23 -0
- package/dist/props/builtinProperties.js +350 -0
- package/dist/props/defaults.d.ts +6 -0
- package/dist/props/defaults.js +29 -0
- package/dist/selection/SelectionModel.d.ts +25 -0
- package/dist/selection/SelectionModel.js +79 -0
- package/dist/text/CanvasEditorView.d.ts +99 -0
- package/dist/text/CanvasEditorView.js +308 -0
- package/dist/text/InputSink.d.ts +157 -0
- package/dist/text/InputSink.js +841 -0
- package/dist/text/TextEditSession.d.ts +173 -0
- package/dist/text/TextEditSession.js +1328 -0
- package/dist/text/clipboard.d.ts +11 -0
- package/dist/text/clipboard.js +489 -0
- package/dist/text/commands.d.ts +48 -0
- package/dist/text/commands.js +329 -0
- package/dist/text/contextMirror.d.ts +51 -0
- package/dist/text/contextMirror.js +292 -0
- package/dist/text/flat.d.ts +35 -0
- package/dist/text/flat.js +556 -0
- package/dist/text/graphemes.d.ts +16 -0
- package/dist/text/graphemes.js +219 -0
- package/dist/text/index.d.ts +13 -0
- package/dist/text/index.js +13 -0
- package/dist/text/inputReducer.d.ts +24 -0
- package/dist/text/inputReducer.js +44 -0
- package/dist/text/keymap.d.ts +17 -0
- package/dist/text/keymap.js +148 -0
- package/dist/text/schema.d.ts +12 -0
- package/dist/text/schema.js +44 -0
- package/dist/text/sessionFocus.d.ts +20 -0
- package/dist/text/sessionFocus.js +51 -0
- package/dist/text/types.d.ts +114 -0
- package/dist/text/types.js +1 -0
- package/dist/tools/CreateTool.d.ts +78 -0
- package/dist/tools/CreateTool.js +258 -0
- package/dist/tools/SelectTool.d.ts +155 -0
- package/dist/tools/SelectTool.js +808 -0
- package/dist/tools/ToolManager.d.ts +63 -0
- package/dist/tools/ToolManager.js +146 -0
- package/dist/tools/connectorEndpoints.d.ts +17 -0
- package/dist/tools/connectorEndpoints.js +53 -0
- package/dist/tools/frameRotation.d.ts +8 -0
- package/dist/tools/frameRotation.js +66 -0
- package/dist/tools/gradientHandles.d.ts +67 -0
- package/dist/tools/gradientHandles.js +110 -0
- package/dist/tools/presetPaths.d.ts +5 -0
- package/dist/tools/presetPaths.js +49 -0
- package/dist/tools/snapResize.d.ts +7 -0
- package/dist/tools/snapResize.js +65 -0
- package/dist/tools/snapping.d.ts +30 -0
- package/dist/tools/snapping.js +280 -0
- package/dist/tools/transform.d.ts +52 -0
- package/dist/tools/transform.js +277 -0
- package/package.json +36 -3
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type TextDelta } from "@daasboard/doc";
|
|
2
|
+
import type { TextContent, TextParagraphContent } from "@daasboard/render-core";
|
|
3
|
+
import { Node as PMNode } from "prosemirror-model";
|
|
4
|
+
export type Attrs = Record<string, unknown>;
|
|
5
|
+
export interface Flat {
|
|
6
|
+
text: string;
|
|
7
|
+
attrs: Attrs[];
|
|
8
|
+
}
|
|
9
|
+
export declare const EMPTY_ATTRS: Attrs;
|
|
10
|
+
export declare function isCodePointBoundary(s: string, i: number): boolean;
|
|
11
|
+
export declare function snapDown(s: string, i: number): number;
|
|
12
|
+
export declare function snapUp(s: string, i: number): number;
|
|
13
|
+
export declare function sameAttrs(a: Attrs, b: Attrs, keys: readonly string[]): boolean;
|
|
14
|
+
export declare function attrsEqual(a: Attrs, b: Attrs): boolean;
|
|
15
|
+
export declare function pmToFlat(doc: PMNode, fromPara?: number, toPara?: number): Flat;
|
|
16
|
+
export declare function pmToText(doc: PMNode): string;
|
|
17
|
+
export declare function flatToPm(flat: Flat): PMNode;
|
|
18
|
+
export declare function paragraphReadsAs(node: PMNode, p: TextParagraphContent): boolean;
|
|
19
|
+
export declare function contentToPm(content: TextContent, previous?: PMNode): PMNode;
|
|
20
|
+
export declare function pmToContent(doc: PMNode): TextContent;
|
|
21
|
+
export declare function paragraphStarts(doc: PMNode): number[];
|
|
22
|
+
export declare function paragraphIndexAtOffset(doc: PMNode, off: number): number;
|
|
23
|
+
export declare function paragraphIndexAtPos(doc: PMNode, pos: number): number;
|
|
24
|
+
export declare function pmPosToOffset(doc: PMNode, pos: number): number;
|
|
25
|
+
export declare function offsetToPmPos(doc: PMNode, offset: number): number;
|
|
26
|
+
export declare function formatChange(before: Attrs, after: Attrs): Attrs | null;
|
|
27
|
+
export declare function diffToDelta(before: Flat, after: Flat): TextDelta;
|
|
28
|
+
export declare function changedParagraphs(before: PMNode, after: PMNode): {
|
|
29
|
+
start: number;
|
|
30
|
+
endBefore: number;
|
|
31
|
+
endAfter: number;
|
|
32
|
+
} | null;
|
|
33
|
+
export declare function pmDiffToDelta(before: PMNode, after: PMNode): TextDelta;
|
|
34
|
+
export declare function applyDeltaToFlat(flat: Flat, delta: TextDelta): Flat;
|
|
35
|
+
export declare const ALL_ATTR_KEYS: readonly string[];
|
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
import { HARD_LINE_BREAK, paragraphAttrs as contentParagraphAttrs, paragraphFromAttrs, runAttrs, runFromAttrs } from "@daasboard/doc";
|
|
2
|
+
import { Fragment } from "prosemirror-model";
|
|
3
|
+
import { INLINE_MARK_KEYS, PARAGRAPH_ATTR_KEYS, schema } from "./schema.js";
|
|
4
|
+
export const EMPTY_ATTRS = Object.freeze({});
|
|
5
|
+
const isHigh = (c) => c >= 0xd800 && c <= 0xdbff;
|
|
6
|
+
const isLow = (c) => c >= 0xdc00 && c <= 0xdfff;
|
|
7
|
+
export function isCodePointBoundary(s, i) {
|
|
8
|
+
return !(i > 0 && i < s.length && isLow(s.charCodeAt(i)) && isHigh(s.charCodeAt(i - 1)));
|
|
9
|
+
}
|
|
10
|
+
export function snapDown(s, i) {
|
|
11
|
+
return isCodePointBoundary(s, i) ? i : i - 1;
|
|
12
|
+
}
|
|
13
|
+
export function snapUp(s, i) {
|
|
14
|
+
return isCodePointBoundary(s, i) ? i : i + 1;
|
|
15
|
+
}
|
|
16
|
+
const absent = (v) => v === undefined || v === null;
|
|
17
|
+
export function sameAttrs(a, b, keys) {
|
|
18
|
+
if (a === b)
|
|
19
|
+
return true;
|
|
20
|
+
for (const k of keys) {
|
|
21
|
+
const x = a[k];
|
|
22
|
+
const y = b[k];
|
|
23
|
+
if (x !== y && !(absent(x) && absent(y)))
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
export function attrsEqual(a, b) {
|
|
29
|
+
if (a === b)
|
|
30
|
+
return true;
|
|
31
|
+
for (const k in a)
|
|
32
|
+
if (a[k] !== b[k] && !(absent(a[k]) && absent(b[k])))
|
|
33
|
+
return false;
|
|
34
|
+
for (const k in b)
|
|
35
|
+
if (!(k in a) && !absent(b[k]))
|
|
36
|
+
return false;
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
const attrsByMarks = new WeakMap();
|
|
40
|
+
const attrsByParagraph = new WeakMap();
|
|
41
|
+
function marksToAttrs(marks) {
|
|
42
|
+
if (marks.length === 0)
|
|
43
|
+
return EMPTY_ATTRS;
|
|
44
|
+
let a = attrsByMarks.get(marks);
|
|
45
|
+
if (a)
|
|
46
|
+
return a;
|
|
47
|
+
a = {};
|
|
48
|
+
for (const m of marks)
|
|
49
|
+
a[m.type.name] = m.type.name === "link" ? m.attrs.href : m.attrs.value;
|
|
50
|
+
attrsByMarks.set(marks, a);
|
|
51
|
+
return a;
|
|
52
|
+
}
|
|
53
|
+
function paragraphAttrs(node) {
|
|
54
|
+
const src = node.attrs;
|
|
55
|
+
let a = attrsByParagraph.get(src);
|
|
56
|
+
if (a)
|
|
57
|
+
return a;
|
|
58
|
+
a = {};
|
|
59
|
+
if (src.align != null)
|
|
60
|
+
a.align = src.align;
|
|
61
|
+
if (typeof src.lineHeight === "number")
|
|
62
|
+
a.lineHeight = src.lineHeight;
|
|
63
|
+
if (src.list != null)
|
|
64
|
+
a.list = src.list;
|
|
65
|
+
if (typeof src.indent === "number" && src.indent !== 0)
|
|
66
|
+
a.indent = src.indent;
|
|
67
|
+
if (typeof src.start === "number")
|
|
68
|
+
a.start = src.start;
|
|
69
|
+
if (src.checked === true)
|
|
70
|
+
a.checked = true;
|
|
71
|
+
if (Object.keys(a).length === 0)
|
|
72
|
+
a = EMPTY_ATTRS;
|
|
73
|
+
attrsByParagraph.set(src, a);
|
|
74
|
+
return a;
|
|
75
|
+
}
|
|
76
|
+
function attrsToMarks(a) {
|
|
77
|
+
const marks = [];
|
|
78
|
+
for (const k of INLINE_MARK_KEYS) {
|
|
79
|
+
const v = a[k];
|
|
80
|
+
if (absent(v))
|
|
81
|
+
continue;
|
|
82
|
+
marks.push(k === "link" ? schema.marks.link.create({ href: v }) : schema.marks[k].create({ value: v }));
|
|
83
|
+
}
|
|
84
|
+
return marks;
|
|
85
|
+
}
|
|
86
|
+
function paragraphNodeAttrs(pa) {
|
|
87
|
+
return { align: pa.align ?? null, lineHeight: pa.lineHeight ?? null, list: pa.list ?? null, indent: typeof pa.indent === "number" ? pa.indent : 0, start: pa.start ?? null, checked: pa.checked === true ? true : null };
|
|
88
|
+
}
|
|
89
|
+
export function pmToFlat(doc, fromPara = 0, toPara = doc.childCount) {
|
|
90
|
+
let text = "";
|
|
91
|
+
const attrs = [];
|
|
92
|
+
for (let i = fromPara; i < toPara; i++) {
|
|
93
|
+
const para = doc.child(i);
|
|
94
|
+
para.forEach((child) => {
|
|
95
|
+
if (!child.isText && child.type !== schema.nodes.hard_break)
|
|
96
|
+
return;
|
|
97
|
+
const a = marksToAttrs(child.marks);
|
|
98
|
+
const t = child.isText ? child.text : HARD_LINE_BREAK;
|
|
99
|
+
text += t;
|
|
100
|
+
for (let k = 0; k < t.length; k++)
|
|
101
|
+
attrs.push(a);
|
|
102
|
+
});
|
|
103
|
+
text += "\n";
|
|
104
|
+
attrs.push(paragraphAttrs(para));
|
|
105
|
+
}
|
|
106
|
+
return { text, attrs };
|
|
107
|
+
}
|
|
108
|
+
export function pmToText(doc) {
|
|
109
|
+
let text = "";
|
|
110
|
+
for (let i = 0; i < doc.childCount; i++)
|
|
111
|
+
text += doc.child(i).textContent + "\n";
|
|
112
|
+
return text;
|
|
113
|
+
}
|
|
114
|
+
export function flatToPm(flat) {
|
|
115
|
+
const paragraphs = [];
|
|
116
|
+
const inlines = [];
|
|
117
|
+
const { text, attrs } = flat;
|
|
118
|
+
let runStart = 0;
|
|
119
|
+
const flushRun = (end) => {
|
|
120
|
+
if (end > runStart)
|
|
121
|
+
inlines.push(schema.text(text.slice(runStart, end), attrsToMarks(attrs[runStart] ?? EMPTY_ATTRS)));
|
|
122
|
+
runStart = end;
|
|
123
|
+
};
|
|
124
|
+
for (let i = 0; i < text.length; i++) {
|
|
125
|
+
if (text[i] === HARD_LINE_BREAK) {
|
|
126
|
+
flushRun(i);
|
|
127
|
+
inlines.push(schema.nodes.hard_break.create(null, null, attrsToMarks(attrs[i] ?? EMPTY_ATTRS)));
|
|
128
|
+
runStart = i + 1;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (text.charCodeAt(i) === 10) {
|
|
132
|
+
flushRun(i);
|
|
133
|
+
paragraphs.push(schema.nodes.paragraph.create(paragraphNodeAttrs(attrs[i] ?? EMPTY_ATTRS), Fragment.from(inlines.splice(0))));
|
|
134
|
+
runStart = i + 1;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (i > runStart && !sameAttrs(attrs[i] ?? EMPTY_ATTRS, attrs[i - 1] ?? EMPTY_ATTRS, INLINE_MARK_KEYS))
|
|
138
|
+
flushRun(i);
|
|
139
|
+
}
|
|
140
|
+
if (runStart < text.length || inlines.length > 0) {
|
|
141
|
+
flushRun(text.length);
|
|
142
|
+
paragraphs.push(schema.nodes.paragraph.create(null, Fragment.from(inlines.splice(0))));
|
|
143
|
+
}
|
|
144
|
+
if (paragraphs.length === 0)
|
|
145
|
+
paragraphs.push(schema.nodes.paragraph.create());
|
|
146
|
+
return schema.nodes.doc.create(null, Fragment.from(paragraphs));
|
|
147
|
+
}
|
|
148
|
+
const nodeByParagraph = new WeakMap();
|
|
149
|
+
function paragraphNode(p) {
|
|
150
|
+
const runs = [];
|
|
151
|
+
for (const r of p.runs) {
|
|
152
|
+
const marks = runMarks(r);
|
|
153
|
+
const pieces = r.text.split(HARD_LINE_BREAK);
|
|
154
|
+
for (let i = 0; i < pieces.length; i++) {
|
|
155
|
+
if (i)
|
|
156
|
+
runs.push(schema.nodes.hard_break.create(null, null, marks));
|
|
157
|
+
if (pieces[i])
|
|
158
|
+
runs.push(schema.text(pieces[i], marks));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const attrs = { align: p.align ?? null, lineHeight: p.lineHeight ?? null, list: p.list ?? null, indent: p.indent ?? 0, start: p.start ?? null, checked: p.checked ? true : null };
|
|
162
|
+
const node = schema.nodes.paragraph.create(attrs, Fragment.fromArray(runs));
|
|
163
|
+
nodeByParagraph.set(p, node);
|
|
164
|
+
return node;
|
|
165
|
+
}
|
|
166
|
+
export function paragraphReadsAs(node, p) {
|
|
167
|
+
if (nodeByParagraph.get(p) === node)
|
|
168
|
+
return true;
|
|
169
|
+
if (!attrsEqual(paragraphAttrs(node), contentParagraphAttrs(p)))
|
|
170
|
+
return false;
|
|
171
|
+
const runs = p.runs;
|
|
172
|
+
let ri = 0;
|
|
173
|
+
let ro = 0;
|
|
174
|
+
let same = true;
|
|
175
|
+
node.forEach((child) => {
|
|
176
|
+
if (!same || (!child.isText && child.type !== schema.nodes.hard_break))
|
|
177
|
+
return;
|
|
178
|
+
const t = child.isText ? child.text : HARD_LINE_BREAK;
|
|
179
|
+
const a = marksToAttrs(child.marks);
|
|
180
|
+
let k = 0;
|
|
181
|
+
while (k < t.length) {
|
|
182
|
+
while (ri < runs.length && ro === runs[ri].text.length) {
|
|
183
|
+
ri++;
|
|
184
|
+
ro = 0;
|
|
185
|
+
}
|
|
186
|
+
const run = runs[ri];
|
|
187
|
+
if (!run) {
|
|
188
|
+
same = false;
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const take = Math.min(t.length - k, run.text.length - ro);
|
|
192
|
+
const text = take === t.length && take === run.text.length ? t === run.text : t.slice(k, k + take) === run.text.slice(ro, ro + take);
|
|
193
|
+
if (!text || !attrsEqual(a, runAttrs(run))) {
|
|
194
|
+
same = false;
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
k += take;
|
|
198
|
+
ro += take;
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
if (!same)
|
|
202
|
+
return false;
|
|
203
|
+
for (; ri < runs.length; ri++, ro = 0)
|
|
204
|
+
if (runs[ri].text.length > ro)
|
|
205
|
+
return false;
|
|
206
|
+
nodeByParagraph.set(p, node);
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
export function contentToPm(content, previous) {
|
|
210
|
+
const ps = content.paragraphs.length ? content.paragraphs : [{ runs: [] }];
|
|
211
|
+
const n = ps.length;
|
|
212
|
+
const m = previous?.childCount ?? 0;
|
|
213
|
+
let lo = 0;
|
|
214
|
+
while (lo < n && lo < m && paragraphReadsAs(previous.child(lo), ps[lo]))
|
|
215
|
+
lo++;
|
|
216
|
+
if (lo === n && n === m)
|
|
217
|
+
return previous;
|
|
218
|
+
let hi = 0;
|
|
219
|
+
while (hi < n - lo && hi < m - lo && paragraphReadsAs(previous.child(m - 1 - hi), ps[n - 1 - hi]))
|
|
220
|
+
hi++;
|
|
221
|
+
const paragraphs = new Array(n);
|
|
222
|
+
for (let i = 0; i < n; i++) {
|
|
223
|
+
if (i < lo)
|
|
224
|
+
paragraphs[i] = previous.child(i);
|
|
225
|
+
else if (i >= n - hi)
|
|
226
|
+
paragraphs[i] = previous.child(m - n + i);
|
|
227
|
+
else {
|
|
228
|
+
const p = ps[i];
|
|
229
|
+
paragraphs[i] = nodeByParagraph.get(p) ?? (n === m && paragraphReadsAs(previous.child(i), p) ? previous.child(i) : paragraphNode(p));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return schema.nodes.doc.create(null, Fragment.from(paragraphs));
|
|
233
|
+
}
|
|
234
|
+
function runMarks(r) {
|
|
235
|
+
const marks = [];
|
|
236
|
+
if (r.weight !== undefined)
|
|
237
|
+
marks.push(schema.marks.weight.create({ value: r.weight }));
|
|
238
|
+
if (r.italic !== undefined)
|
|
239
|
+
marks.push(schema.marks.italic.create({ value: r.italic }));
|
|
240
|
+
if (r.underline !== undefined)
|
|
241
|
+
marks.push(schema.marks.underline.create({ value: r.underline }));
|
|
242
|
+
if (r.strike !== undefined)
|
|
243
|
+
marks.push(schema.marks.strike.create({ value: r.strike }));
|
|
244
|
+
if (r.color)
|
|
245
|
+
marks.push(schema.marks.color.create({ value: r.color }));
|
|
246
|
+
if (r.size)
|
|
247
|
+
marks.push(schema.marks.size.create({ value: r.size }));
|
|
248
|
+
if (r.family)
|
|
249
|
+
marks.push(schema.marks.family.create({ value: r.family }));
|
|
250
|
+
if (r.letterSpacing !== undefined)
|
|
251
|
+
marks.push(schema.marks.letterSpacing.create({ value: r.letterSpacing }));
|
|
252
|
+
if (r.textCase)
|
|
253
|
+
marks.push(schema.marks.textCase.create({ value: r.textCase }));
|
|
254
|
+
if (r.link)
|
|
255
|
+
marks.push(schema.marks.link.create({ href: r.link }));
|
|
256
|
+
return marks;
|
|
257
|
+
}
|
|
258
|
+
const contentByParagraph = new WeakMap();
|
|
259
|
+
function paragraphContent(para) {
|
|
260
|
+
let p = contentByParagraph.get(para);
|
|
261
|
+
if (p)
|
|
262
|
+
return p;
|
|
263
|
+
const runs = [];
|
|
264
|
+
para.forEach((child) => {
|
|
265
|
+
if (!child.isText && child.type !== schema.nodes.hard_break)
|
|
266
|
+
return;
|
|
267
|
+
runs.push(runFromAttrs(child.isText ? child.text : HARD_LINE_BREAK, marksToAttrs(child.marks)));
|
|
268
|
+
});
|
|
269
|
+
p = paragraphFromAttrs(paragraphAttrs(para), runs);
|
|
270
|
+
contentByParagraph.set(para, p);
|
|
271
|
+
return p;
|
|
272
|
+
}
|
|
273
|
+
export function pmToContent(doc) {
|
|
274
|
+
const paragraphs = new Array(doc.childCount);
|
|
275
|
+
for (let i = 0; i < doc.childCount; i++)
|
|
276
|
+
paragraphs[i] = paragraphContent(doc.child(i));
|
|
277
|
+
return { paragraphs };
|
|
278
|
+
}
|
|
279
|
+
const startsByDoc = new WeakMap();
|
|
280
|
+
export function paragraphStarts(doc) {
|
|
281
|
+
let starts = startsByDoc.get(doc);
|
|
282
|
+
if (starts)
|
|
283
|
+
return starts;
|
|
284
|
+
starts = new Array(doc.childCount + 1);
|
|
285
|
+
let off = 0;
|
|
286
|
+
for (let i = 0; i < doc.childCount; i++) {
|
|
287
|
+
starts[i] = off;
|
|
288
|
+
off += doc.child(i).content.size + 1;
|
|
289
|
+
}
|
|
290
|
+
starts[doc.childCount] = off;
|
|
291
|
+
startsByDoc.set(doc, starts);
|
|
292
|
+
return starts;
|
|
293
|
+
}
|
|
294
|
+
export function paragraphIndexAtOffset(doc, off) {
|
|
295
|
+
const starts = paragraphStarts(doc);
|
|
296
|
+
let lo = 0;
|
|
297
|
+
let hi = doc.childCount - 1;
|
|
298
|
+
while (lo < hi) {
|
|
299
|
+
const mid = (lo + hi + 1) >> 1;
|
|
300
|
+
if (starts[mid] <= off)
|
|
301
|
+
lo = mid;
|
|
302
|
+
else
|
|
303
|
+
hi = mid - 1;
|
|
304
|
+
}
|
|
305
|
+
return lo;
|
|
306
|
+
}
|
|
307
|
+
export function paragraphIndexAtPos(doc, pos) {
|
|
308
|
+
const starts = paragraphStarts(doc);
|
|
309
|
+
let lo = 0;
|
|
310
|
+
let hi = doc.childCount - 1;
|
|
311
|
+
while (lo < hi) {
|
|
312
|
+
const mid = (lo + hi + 1) >> 1;
|
|
313
|
+
if (starts[mid] + mid <= pos)
|
|
314
|
+
lo = mid;
|
|
315
|
+
else
|
|
316
|
+
hi = mid - 1;
|
|
317
|
+
}
|
|
318
|
+
return lo;
|
|
319
|
+
}
|
|
320
|
+
export function pmPosToOffset(doc, pos) {
|
|
321
|
+
const starts = paragraphStarts(doc);
|
|
322
|
+
const i = paragraphIndexAtPos(doc, pos);
|
|
323
|
+
const size = starts[i + 1] - starts[i] - 1;
|
|
324
|
+
const local = Math.max(0, Math.min(size, pos - (starts[i] + i) - 1));
|
|
325
|
+
return starts[i] + local;
|
|
326
|
+
}
|
|
327
|
+
export function offsetToPmPos(doc, offset) {
|
|
328
|
+
const starts = paragraphStarts(doc);
|
|
329
|
+
const total = starts[doc.childCount];
|
|
330
|
+
const off = Math.max(0, Math.min(total - 1, offset));
|
|
331
|
+
const i = paragraphIndexAtOffset(doc, off);
|
|
332
|
+
return off + i + 1;
|
|
333
|
+
}
|
|
334
|
+
export function formatChange(before, after) {
|
|
335
|
+
if (before === after)
|
|
336
|
+
return null;
|
|
337
|
+
let change = null;
|
|
338
|
+
for (const k in after) {
|
|
339
|
+
const v = after[k];
|
|
340
|
+
if (absent(v) || before[k] === v)
|
|
341
|
+
continue;
|
|
342
|
+
(change ??= {})[k] = v;
|
|
343
|
+
}
|
|
344
|
+
for (const k in before) {
|
|
345
|
+
if (absent(before[k]) || !absent(after[k]))
|
|
346
|
+
continue;
|
|
347
|
+
(change ??= {})[k] = null;
|
|
348
|
+
}
|
|
349
|
+
return change;
|
|
350
|
+
}
|
|
351
|
+
function presentAttrs(a) {
|
|
352
|
+
let out;
|
|
353
|
+
for (const k in a)
|
|
354
|
+
if (!absent(a[k]))
|
|
355
|
+
(out ??= {})[k] = a[k];
|
|
356
|
+
return out;
|
|
357
|
+
}
|
|
358
|
+
class DeltaBuilder {
|
|
359
|
+
ops = [];
|
|
360
|
+
retain(n, attributes) {
|
|
361
|
+
if (n <= 0)
|
|
362
|
+
return;
|
|
363
|
+
const last = this.ops[this.ops.length - 1];
|
|
364
|
+
if (last && "retain" in last && !last.attributes && !attributes)
|
|
365
|
+
last.retain += n;
|
|
366
|
+
else
|
|
367
|
+
this.ops.push(attributes ? { retain: n, attributes } : { retain: n });
|
|
368
|
+
}
|
|
369
|
+
insert(s, attributes) {
|
|
370
|
+
if (!s)
|
|
371
|
+
return;
|
|
372
|
+
this.ops.push(attributes ? { insert: s, attributes } : { insert: s });
|
|
373
|
+
}
|
|
374
|
+
delete(n) {
|
|
375
|
+
if (n > 0)
|
|
376
|
+
this.ops.push({ delete: n });
|
|
377
|
+
}
|
|
378
|
+
finish() {
|
|
379
|
+
const last = this.ops[this.ops.length - 1];
|
|
380
|
+
if (last && "retain" in last && !last.attributes)
|
|
381
|
+
this.ops.pop();
|
|
382
|
+
return this.ops;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
function sameChange(x, y) {
|
|
386
|
+
const kx = Object.keys(x);
|
|
387
|
+
const ky = Object.keys(y);
|
|
388
|
+
if (kx.length !== ky.length)
|
|
389
|
+
return false;
|
|
390
|
+
for (const k of kx)
|
|
391
|
+
if (!(k in y) || x[k] !== y[k])
|
|
392
|
+
return false;
|
|
393
|
+
return true;
|
|
394
|
+
}
|
|
395
|
+
function emitFormatRuns(out, before, after, from, to, aFrom) {
|
|
396
|
+
let runStart = from;
|
|
397
|
+
let curB;
|
|
398
|
+
let curA;
|
|
399
|
+
let curChange = null;
|
|
400
|
+
for (let j = from; j < to; j++) {
|
|
401
|
+
const b = before[aFrom + (j - from)];
|
|
402
|
+
const a = after[j];
|
|
403
|
+
if (b === curB && a === curA)
|
|
404
|
+
continue;
|
|
405
|
+
const change = formatChange(b, a);
|
|
406
|
+
if (j > from && !(change === curChange || (change && curChange && sameChange(change, curChange)))) {
|
|
407
|
+
out.retain(j - runStart, curChange);
|
|
408
|
+
runStart = j;
|
|
409
|
+
}
|
|
410
|
+
curB = b;
|
|
411
|
+
curA = a;
|
|
412
|
+
curChange = change;
|
|
413
|
+
}
|
|
414
|
+
out.retain(to - runStart, curChange);
|
|
415
|
+
}
|
|
416
|
+
function emitInsertRuns(out, text, attrs, from, to) {
|
|
417
|
+
let runStart = from;
|
|
418
|
+
let cur;
|
|
419
|
+
for (let j = from; j < to; j++) {
|
|
420
|
+
const a = attrs[j];
|
|
421
|
+
if (a === cur)
|
|
422
|
+
continue;
|
|
423
|
+
if (j > from && !attrsEqual(a, cur)) {
|
|
424
|
+
out.insert(text.slice(runStart, j), presentAttrs(cur));
|
|
425
|
+
runStart = j;
|
|
426
|
+
}
|
|
427
|
+
cur = a;
|
|
428
|
+
}
|
|
429
|
+
if (to > runStart)
|
|
430
|
+
out.insert(text.slice(runStart, to), presentAttrs(cur));
|
|
431
|
+
}
|
|
432
|
+
const cpLenBefore = (s, i) => (i >= 2 && isLow(s.charCodeAt(i - 1)) && isHigh(s.charCodeAt(i - 2)) ? 2 : 1);
|
|
433
|
+
export function diffToDelta(before, after) {
|
|
434
|
+
const a = before.text;
|
|
435
|
+
const b = after.text;
|
|
436
|
+
const ba = before.attrs;
|
|
437
|
+
const aa = after.attrs;
|
|
438
|
+
const max = Math.min(a.length, b.length);
|
|
439
|
+
let p = 0;
|
|
440
|
+
while (p < max && a.charCodeAt(p) === b.charCodeAt(p))
|
|
441
|
+
p++;
|
|
442
|
+
p = snapDown(a, p);
|
|
443
|
+
let s = 0;
|
|
444
|
+
while (s < max - p && a.charCodeAt(a.length - 1 - s) === b.charCodeAt(b.length - 1 - s))
|
|
445
|
+
s++;
|
|
446
|
+
s = Math.min(a.length - snapUp(a, a.length - s), b.length - snapUp(b, b.length - s));
|
|
447
|
+
let aEnd = a.length - s;
|
|
448
|
+
let bEnd = b.length - s;
|
|
449
|
+
let lo = 0;
|
|
450
|
+
while (lo < p && (ba[lo] === aa[lo] || attrsEqual(ba[lo], aa[lo])))
|
|
451
|
+
lo++;
|
|
452
|
+
while (p > lo) {
|
|
453
|
+
const n = cpLenBefore(b, p);
|
|
454
|
+
if (n !== cpLenBefore(b, bEnd) || n !== cpLenBefore(a, aEnd))
|
|
455
|
+
break;
|
|
456
|
+
if (b.slice(p - n, p) !== b.slice(bEnd - n, bEnd) || a.slice(p - n, p) !== a.slice(aEnd - n, aEnd))
|
|
457
|
+
break;
|
|
458
|
+
p -= n;
|
|
459
|
+
aEnd -= n;
|
|
460
|
+
bEnd -= n;
|
|
461
|
+
}
|
|
462
|
+
s = a.length - aEnd;
|
|
463
|
+
let tail = 0;
|
|
464
|
+
while (tail < s && (ba[a.length - 1 - tail] === aa[b.length - 1 - tail] || attrsEqual(ba[a.length - 1 - tail], aa[b.length - 1 - tail])))
|
|
465
|
+
tail++;
|
|
466
|
+
const hi = b.length - tail;
|
|
467
|
+
const out = new DeltaBuilder();
|
|
468
|
+
if (lo === p && aEnd === p && bEnd === p && hi === bEnd)
|
|
469
|
+
return out.finish();
|
|
470
|
+
out.retain(lo, null);
|
|
471
|
+
emitFormatRuns(out, ba, aa, lo, p, lo);
|
|
472
|
+
out.delete(aEnd - p);
|
|
473
|
+
emitInsertRuns(out, b, aa, p, bEnd);
|
|
474
|
+
emitFormatRuns(out, ba, aa, bEnd, hi, aEnd);
|
|
475
|
+
return out.finish();
|
|
476
|
+
}
|
|
477
|
+
export function changedParagraphs(before, after) {
|
|
478
|
+
const nb = before.childCount;
|
|
479
|
+
const na = after.childCount;
|
|
480
|
+
const max = Math.min(nb, na);
|
|
481
|
+
let start = 0;
|
|
482
|
+
while (start < max && before.child(start) === after.child(start))
|
|
483
|
+
start++;
|
|
484
|
+
if (start === nb && start === na)
|
|
485
|
+
return null;
|
|
486
|
+
let shared = 0;
|
|
487
|
+
while (shared < max - start && before.child(nb - 1 - shared) === after.child(na - 1 - shared))
|
|
488
|
+
shared++;
|
|
489
|
+
return { start, endBefore: nb - shared, endAfter: na - shared };
|
|
490
|
+
}
|
|
491
|
+
export function pmDiffToDelta(before, after) {
|
|
492
|
+
const range = changedParagraphs(before, after);
|
|
493
|
+
if (!range)
|
|
494
|
+
return [];
|
|
495
|
+
const delta = diffToDelta(pmToFlat(before, range.start, range.endBefore), pmToFlat(after, range.start, range.endAfter));
|
|
496
|
+
const prefix = paragraphStarts(after)[range.start];
|
|
497
|
+
if (!prefix || !delta.length)
|
|
498
|
+
return delta;
|
|
499
|
+
const first = delta[0];
|
|
500
|
+
if ("retain" in first && !first.attributes)
|
|
501
|
+
delta[0] = { retain: first.retain + prefix };
|
|
502
|
+
else
|
|
503
|
+
delta.unshift({ retain: prefix });
|
|
504
|
+
return delta;
|
|
505
|
+
}
|
|
506
|
+
export function applyDeltaToFlat(flat, delta) {
|
|
507
|
+
const chunks = [];
|
|
508
|
+
const attrs = [];
|
|
509
|
+
let i = 0;
|
|
510
|
+
for (const op of delta) {
|
|
511
|
+
if ("insert" in op) {
|
|
512
|
+
const a = op.attributes ? (presentAttrs(op.attributes) ?? EMPTY_ATTRS) : EMPTY_ATTRS;
|
|
513
|
+
chunks.push(op.insert);
|
|
514
|
+
for (let k = 0; k < op.insert.length; k++)
|
|
515
|
+
attrs.push(a);
|
|
516
|
+
}
|
|
517
|
+
else if ("delete" in op) {
|
|
518
|
+
i += op.delete;
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
const end = Math.min(flat.text.length, i + op.retain);
|
|
522
|
+
chunks.push(flat.text.slice(i, end));
|
|
523
|
+
if (op.attributes) {
|
|
524
|
+
const memo = new Map();
|
|
525
|
+
for (let k = i; k < end; k++) {
|
|
526
|
+
const src = flat.attrs[k];
|
|
527
|
+
let dst = memo.get(src);
|
|
528
|
+
if (!dst) {
|
|
529
|
+
dst = { ...src };
|
|
530
|
+
for (const key in op.attributes) {
|
|
531
|
+
const v = op.attributes[key];
|
|
532
|
+
if (absent(v))
|
|
533
|
+
delete dst[key];
|
|
534
|
+
else
|
|
535
|
+
dst[key] = v;
|
|
536
|
+
}
|
|
537
|
+
memo.set(src, dst);
|
|
538
|
+
}
|
|
539
|
+
attrs.push(dst);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
else {
|
|
543
|
+
for (let k = i; k < end; k++)
|
|
544
|
+
attrs.push(flat.attrs[k]);
|
|
545
|
+
}
|
|
546
|
+
i = end;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
if (i < flat.text.length) {
|
|
550
|
+
chunks.push(flat.text.slice(i));
|
|
551
|
+
for (let k = i; k < flat.text.length; k++)
|
|
552
|
+
attrs.push(flat.attrs[k]);
|
|
553
|
+
}
|
|
554
|
+
return { text: chunks.join(""), attrs };
|
|
555
|
+
}
|
|
556
|
+
export const ALL_ATTR_KEYS = [...INLINE_MARK_KEYS, ...PARAGRAPH_ATTR_KEYS];
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare function nextGraphemeEndFallback(text: string, i: number): number;
|
|
2
|
+
export declare function prevGraphemeStartFallback(text: string, i: number): number;
|
|
3
|
+
export declare function nextGraphemeEnd(text: string, i: number): number;
|
|
4
|
+
export declare function prevGraphemeStart(text: string, i: number): number;
|
|
5
|
+
export declare function nextWordEnd(text: string, i: number): number;
|
|
6
|
+
export declare function nextWordStart(text: string, i: number): number;
|
|
7
|
+
export declare function prevWordStart(text: string, i: number): number;
|
|
8
|
+
export declare function wordRangeAt(text: string, i: number, affinity?: "upstream" | "downstream", includeTrailingSpace?: boolean): {
|
|
9
|
+
start: number;
|
|
10
|
+
end: number;
|
|
11
|
+
};
|
|
12
|
+
export type WordRanges = (i: number, affinity?: "upstream" | "downstream", includeTrailingSpace?: boolean) => {
|
|
13
|
+
start: number;
|
|
14
|
+
end: number;
|
|
15
|
+
};
|
|
16
|
+
export declare function wordRanges(text: string): WordRanges;
|