@folijs/core 0.2.0 → 0.4.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/CHANGELOG.md +45 -0
- package/README.md +1 -1
- package/dist/document.d.ts +10 -16
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +22 -22
- package/dist/document.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/model/fragment.d.ts +5 -2
- package/dist/model/fragment.d.ts.map +1 -1
- package/dist/model/fragment.js +5 -2
- package/dist/model/fragment.js.map +1 -1
- package/dist/model/node.d.ts +1 -1
- package/dist/model/node.d.ts.map +1 -1
- package/dist/model/node.js.map +1 -1
- package/dist/schema.d.ts +10 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +11 -1
- package/dist/schema.js.map +1 -1
- package/dist/state/commands.d.ts +7 -6
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +162 -97
- package/dist/state/commands.js.map +1 -1
- package/dist/state/comments.d.ts +47 -0
- package/dist/state/comments.d.ts.map +1 -0
- package/dist/state/comments.js +304 -0
- package/dist/state/comments.js.map +1 -0
- package/dist/state/editor.d.ts +67 -16
- package/dist/state/editor.d.ts.map +1 -1
- package/dist/state/editor.js +74 -18
- package/dist/state/editor.js.map +1 -1
- package/dist/state/format.d.ts +8 -22
- package/dist/state/format.d.ts.map +1 -1
- package/dist/state/format.js +16 -140
- package/dist/state/format.js.map +1 -1
- package/dist/state/history.d.ts +1 -1
- package/dist/state/history.d.ts.map +1 -1
- package/dist/state/history.js +3 -2
- package/dist/state/history.js.map +1 -1
- package/dist/state/index.d.ts +11 -4
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +11 -4
- package/dist/state/index.js.map +1 -1
- package/dist/state/links.d.ts.map +1 -1
- package/dist/state/links.js +10 -2
- package/dist/state/links.js.map +1 -1
- package/dist/state/lists.d.ts +19 -32
- package/dist/state/lists.d.ts.map +1 -1
- package/dist/state/lists.js +23 -372
- package/dist/state/lists.js.map +1 -1
- package/dist/state/mark.d.ts +28 -0
- package/dist/state/mark.d.ts.map +1 -0
- package/dist/state/mark.js +57 -0
- package/dist/state/mark.js.map +1 -0
- package/dist/state/review.d.ts +43 -0
- package/dist/state/review.d.ts.map +1 -0
- package/dist/state/review.js +282 -0
- package/dist/state/review.js.map +1 -0
- package/dist/state/state.d.ts +2 -0
- package/dist/state/state.d.ts.map +1 -1
- package/dist/state/state.js +18 -0
- package/dist/state/state.js.map +1 -1
- package/dist/state/track.d.ts +47 -0
- package/dist/state/track.d.ts.map +1 -0
- package/dist/state/track.js +534 -0
- package/dist/state/track.js.map +1 -0
- package/dist/state/word.d.ts +100 -0
- package/dist/state/word.d.ts.map +1 -0
- package/dist/state/word.js +253 -0
- package/dist/state/word.js.map +1 -0
- package/dist/state/wordclip.d.ts +43 -0
- package/dist/state/wordclip.d.ts.map +1 -0
- package/dist/state/wordclip.js +412 -0
- package/dist/state/wordclip.js.map +1 -0
- package/dist/state/wordformat.d.ts +136 -0
- package/dist/state/wordformat.d.ts.map +1 -0
- package/dist/state/wordformat.js +846 -0
- package/dist/state/wordformat.js.map +1 -0
- package/dist/v3/check.d.ts +33 -0
- package/dist/v3/check.d.ts.map +1 -0
- package/dist/v3/check.js +90 -0
- package/dist/v3/check.js.map +1 -0
- package/dist/v3/document.d.ts +217 -0
- package/dist/v3/document.d.ts.map +1 -0
- package/dist/v3/document.js +225 -0
- package/dist/v3/document.js.map +1 -0
- package/dist/v3/index.d.ts +6 -0
- package/dist/v3/index.d.ts.map +1 -0
- package/dist/v3/index.js +6 -0
- package/dist/v3/index.js.map +1 -0
- package/dist/v3/migrate.d.ts +19 -0
- package/dist/v3/migrate.d.ts.map +1 -0
- package/dist/v3/migrate.js +227 -0
- package/dist/v3/migrate.js.map +1 -0
- package/dist/v3/resolve.d.ts +129 -0
- package/dist/v3/resolve.d.ts.map +1 -0
- package/dist/v3/resolve.js +534 -0
- package/dist/v3/resolve.js.map +1 -0
- package/dist/v3/schema.d.ts +29 -0
- package/dist/v3/schema.d.ts.map +1 -0
- package/dist/v3/schema.js +431 -0
- package/dist/v3/schema.js.map +1 -0
- package/dist/v3/units.d.ts +16 -0
- package/dist/v3/units.d.ts.map +1 -0
- package/dist/v3/units.js +28 -0
- package/dist/v3/units.js.map +1 -0
- package/package.json +1 -1
- package/src/document.ts +26 -26
- package/src/index.ts +3 -6
- package/src/model/fragment.ts +11 -3
- package/src/model/node.ts +6 -1
- package/src/schema.ts +13 -1
- package/src/state/commands.ts +173 -91
- package/src/state/comments.ts +342 -0
- package/src/state/editor.ts +176 -36
- package/src/state/format.ts +24 -122
- package/src/state/history.ts +3 -2
- package/src/state/index.ts +73 -3
- package/src/state/links.ts +10 -2
- package/src/state/lists.ts +31 -408
- package/src/state/mark.ts +64 -0
- package/src/state/review.ts +330 -0
- package/src/state/state.ts +18 -0
- package/src/state/track.ts +602 -0
- package/src/state/word.ts +330 -0
- package/src/state/wordclip.ts +445 -0
- package/src/state/wordformat.ts +982 -0
- package/src/v3/check.ts +119 -0
- package/src/v3/document.ts +456 -0
- package/src/v3/index.ts +33 -0
- package/src/v3/migrate.ts +267 -0
- package/src/v3/resolve.ts +646 -0
- package/src/v3/schema.ts +482 -0
- package/src/v3/units.ts +34 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editing a schema v3 document as Word edits it (8.7). An editor holding a v3 document keeps
|
|
3
|
+
* the document's envelope (styles, numbering, defaults, theme) in a plugin state field, and the
|
|
4
|
+
* generic commands consult it: Enter splits a paragraph as Word does, the bold, italic,
|
|
5
|
+
* underline and strike toggles act on the formatting text shows (a heading's style bold
|
|
6
|
+
* included), and Backspace and Delete step over the invisible markers (bookmarks, comment
|
|
7
|
+
* ranges) and select opaque content before deleting it.
|
|
8
|
+
*
|
|
9
|
+
* shortcut: v2 and v3 editing live side by side until 8.7d makes v3 the only schema; then these
|
|
10
|
+
* become the commands themselves.
|
|
11
|
+
*/
|
|
12
|
+
import type { Attrs, Mark, MarkType, Node } from "../model/index.js";
|
|
13
|
+
import { Step, StepResult } from "../transform/steps.js";
|
|
14
|
+
import type { CommentJSON, FoliDocument } from "../v3/document.js";
|
|
15
|
+
import { Resolver, type RunFormat } from "../v3/resolve.js";
|
|
16
|
+
import { MARK_ATTRS } from "./mark.js";
|
|
17
|
+
import { NodeSelection, TextSelection } from "./selection.js";
|
|
18
|
+
import { type EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
|
|
19
|
+
|
|
20
|
+
/** A v3 document without its content: what the editor keeps beside the edited tree. */
|
|
21
|
+
export type WordEnvelope = Omit<FoliDocument, "content">;
|
|
22
|
+
|
|
23
|
+
export interface WordContext {
|
|
24
|
+
readonly envelope: WordEnvelope;
|
|
25
|
+
readonly resolver: Resolver;
|
|
26
|
+
/**
|
|
27
|
+
* Made when the document is loaded and kept while it is edited: a copy carries it, so a paste
|
|
28
|
+
* tells this document's lists from another's (Word's `w:nsid`; Foli makes equal definitions
|
|
29
|
+
* in every document).
|
|
30
|
+
*/
|
|
31
|
+
readonly id: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const key = new PluginKey<WordContext>("word");
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Sets, adds or removes the envelope's comment `id` (`comment` null removes it). A step, unlike
|
|
38
|
+
* `withEnvelope`, so undo and redo take comments with the text they annotate. It leaves the
|
|
39
|
+
* tree as it is; `index` is where an added comment goes in the list (a removed one's place).
|
|
40
|
+
*/
|
|
41
|
+
export class CommentStep extends Step {
|
|
42
|
+
constructor(
|
|
43
|
+
readonly id: number,
|
|
44
|
+
readonly comment: CommentJSON | null,
|
|
45
|
+
readonly previous: CommentJSON | null,
|
|
46
|
+
readonly index: number,
|
|
47
|
+
) {
|
|
48
|
+
super();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
apply(doc: Node): StepResult {
|
|
52
|
+
return StepResult.ok(doc);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
invert(): Step {
|
|
56
|
+
return new CommentStep(this.id, this.previous, this.comment, this.index);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
map(): Step {
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** `comments` after the comment steps among `steps`. */
|
|
65
|
+
export function applyCommentSteps(
|
|
66
|
+
comments: readonly CommentJSON[],
|
|
67
|
+
steps: readonly Step[],
|
|
68
|
+
): readonly CommentJSON[] {
|
|
69
|
+
let next = comments;
|
|
70
|
+
for (const step of steps) {
|
|
71
|
+
if (!(step instanceof CommentStep)) continue;
|
|
72
|
+
const at = next.findIndex((c) => c.id === step.id);
|
|
73
|
+
// A change made since (outside history) wins: undo does not bring back or overwrite it.
|
|
74
|
+
if ((step.previous === null) !== at < 0) continue;
|
|
75
|
+
const rest = at < 0 ? next : [...next.slice(0, at), ...next.slice(at + 1)];
|
|
76
|
+
if (!step.comment) next = rest;
|
|
77
|
+
else {
|
|
78
|
+
const index = at < 0 ? Math.min(step.index, rest.length) : at;
|
|
79
|
+
next = [...rest.slice(0, index), step.comment, ...rest.slice(index)];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return next;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Makes `tr` change the comments from `from` (the state's) to `to`, one `CommentStep` per
|
|
87
|
+
* comment added, changed or removed, after any comment steps `tr` already has. Comments kept
|
|
88
|
+
* keep their order (a new one goes at its index in `to`).
|
|
89
|
+
*/
|
|
90
|
+
export function setComments(
|
|
91
|
+
tr: Transaction,
|
|
92
|
+
from: readonly CommentJSON[],
|
|
93
|
+
to: readonly CommentJSON[],
|
|
94
|
+
): Transaction {
|
|
95
|
+
let current = applyCommentSteps(from, tr.steps);
|
|
96
|
+
const wanted = new Map(to.map((c) => [c.id, c]));
|
|
97
|
+
for (const c of current)
|
|
98
|
+
if (!wanted.has(c.id)) {
|
|
99
|
+
tr.step(new CommentStep(c.id, null, c, current.indexOf(c)));
|
|
100
|
+
current = applyCommentSteps(current, tr.steps.slice(-1));
|
|
101
|
+
}
|
|
102
|
+
to.forEach((c, index) => {
|
|
103
|
+
const before = current.find((p) => p.id === c.id) ?? null;
|
|
104
|
+
if (before === c || (before && JSON.stringify(before) === JSON.stringify(c))) return;
|
|
105
|
+
tr.step(new CommentStep(c.id, c, before, index));
|
|
106
|
+
current = applyCommentSteps(current, tr.steps.slice(-1));
|
|
107
|
+
});
|
|
108
|
+
return tr;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The plugin that gives commands a v3 document's envelope. A transaction changes it with
|
|
113
|
+
* `withEnvelope` (a new list's numbering definition); undo leaves such additions, as Word does.
|
|
114
|
+
* Comments change only through `CommentStep`s, which undo takes back.
|
|
115
|
+
*/
|
|
116
|
+
export function wordPlugin(envelope: WordEnvelope): Plugin<WordContext> {
|
|
117
|
+
// Not a secret: only tells documents apart.
|
|
118
|
+
const id = `${Date.now().toString(36)}${Math.random().toString(36).slice(2)}`;
|
|
119
|
+
const context: WordContext = { envelope, resolver: new Resolver(envelope), id };
|
|
120
|
+
return new Plugin({
|
|
121
|
+
key,
|
|
122
|
+
state: {
|
|
123
|
+
init: () => context,
|
|
124
|
+
apply: (tr, value) => {
|
|
125
|
+
const next = tr.getMeta(key) as WordEnvelope | undefined;
|
|
126
|
+
const comments = applyCommentSteps(value.envelope.comments, tr.steps);
|
|
127
|
+
if (!next && comments === value.envelope.comments) return value;
|
|
128
|
+
const envelope = { ...(next ?? value.envelope), comments: comments as CommentJSON[] };
|
|
129
|
+
// Only comments changed: they do not affect formatting, the resolver stays.
|
|
130
|
+
const resolver = next ? new Resolver(envelope) : value.resolver;
|
|
131
|
+
return { envelope, resolver, id: value.id };
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Makes `tr` change the document's envelope to `envelope` (styles, numbering…). Its comments
|
|
139
|
+
* are ignored: those change through `setComments`, so undo takes them back.
|
|
140
|
+
*/
|
|
141
|
+
export function withEnvelope(tr: Transaction, envelope: WordEnvelope): Transaction {
|
|
142
|
+
return tr.setMeta(key, envelope);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The envelope and resolver of the v3 document in `state`, or null for a v2 editor. */
|
|
146
|
+
export function wordContext(state: EditorState): WordContext | null {
|
|
147
|
+
return key.getState(state) ?? null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* What the paragraph mark carries alone (`MARK_ATTRS`): a split leaves it on the second
|
|
152
|
+
* paragraph, which keeps the original mark.
|
|
153
|
+
*/
|
|
154
|
+
const MARK_ONLY = Object.fromEntries(MARK_ATTRS.map((key) => [key, null]));
|
|
155
|
+
|
|
156
|
+
/** The id of the default paragraph style: the last one marked default, as `Resolver` reads it. */
|
|
157
|
+
function defaultParagraphStyle(envelope: WordEnvelope): string | undefined {
|
|
158
|
+
let id: string | undefined;
|
|
159
|
+
for (const style of envelope.styles)
|
|
160
|
+
if (style.type === "paragraph" && style.isDefault) id = style.id;
|
|
161
|
+
return id;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The attributes of the two paragraphs Enter makes of `paragraph`. The first gets a new mark
|
|
166
|
+
* with the same properties; the second keeps the original mark. At the end of a paragraph whose
|
|
167
|
+
* style names another as the next (a heading: Normal), the second paragraph is in that style,
|
|
168
|
+
* without the first's direct formatting. `before` is null when the first keeps every attribute.
|
|
169
|
+
*/
|
|
170
|
+
export function splitAttrs(
|
|
171
|
+
context: WordContext,
|
|
172
|
+
paragraph: Node,
|
|
173
|
+
atEnd: boolean,
|
|
174
|
+
): { before: Attrs | null; after: Attrs } {
|
|
175
|
+
const attrs = paragraph.attrs;
|
|
176
|
+
const markOnly = Object.keys(MARK_ONLY).some((k) => attrs[k] !== null);
|
|
177
|
+
const before = markOnly ? { ...attrs, ...MARK_ONLY } : null;
|
|
178
|
+
if (!atEnd) return { before, after: attrs };
|
|
179
|
+
const id = (attrs.style as string | null) ?? defaultParagraphStyle(context.envelope);
|
|
180
|
+
const style = context.envelope.styles.find((s) => s.id === id);
|
|
181
|
+
const next = style?.next;
|
|
182
|
+
if (next === undefined || next === id) return { before, after: attrs };
|
|
183
|
+
const mark = Object.fromEntries(Object.keys(MARK_ONLY).map((k) => [k, attrs[k] ?? null]));
|
|
184
|
+
return { before, after: { ...mark, style: next } };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Run toggles: whether text shows the property, from its resolved formatting. */
|
|
188
|
+
const TOGGLES: Record<string, (run: RunFormat) => boolean> = {
|
|
189
|
+
bold: (run) => run.bold,
|
|
190
|
+
italic: (run) => run.italic,
|
|
191
|
+
underline: (run) => run.underline !== null,
|
|
192
|
+
strike: (run) => run.strike,
|
|
193
|
+
doubleStrike: (run) => run.doubleStrike,
|
|
194
|
+
caps: (run) => run.caps,
|
|
195
|
+
smallCaps: (run) => run.smallCaps,
|
|
196
|
+
hidden: (run) => run.hidden,
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/** Whether `type` is a run property the toggles handle on a v3 document. */
|
|
200
|
+
export function isWordToggle(context: WordContext | null, type: MarkType): boolean {
|
|
201
|
+
return context !== null && Object.hasOwn(TOGGLES, type.name);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Whether text with `marks` in `paragraph` shows the toggle `type` (marks included or not). */
|
|
205
|
+
function shows(
|
|
206
|
+
context: WordContext,
|
|
207
|
+
paragraph: Node,
|
|
208
|
+
marks: readonly Mark[],
|
|
209
|
+
type: MarkType,
|
|
210
|
+
own = true,
|
|
211
|
+
): boolean {
|
|
212
|
+
const { run } = context.resolver.paragraph(paragraph.attrs);
|
|
213
|
+
const json = marks.filter((m) => own || m.type !== type).map((m) => m.toJSON());
|
|
214
|
+
return (TOGGLES[type.name] as (run: RunFormat) => boolean)(context.resolver.run(run, json));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The direct mark that makes text show the toggle `type` as `on`, given what it shows without
|
|
219
|
+
* one (`inherited`); null when no mark is needed. Bold and italic keep their complex-script twin.
|
|
220
|
+
*/
|
|
221
|
+
function directMark(type: MarkType, on: boolean, inherited: boolean, existing?: Mark): Mark | null {
|
|
222
|
+
const twin = existing?.attrs.complexScript ?? null;
|
|
223
|
+
const hasTwin = Object.hasOwn(type.spec.attrs ?? {}, "complexScript");
|
|
224
|
+
if (on === inherited)
|
|
225
|
+
return hasTwin && twin !== null ? type.create({ value: null, complexScript: twin }) : null;
|
|
226
|
+
if (type.name === "underline") return type.create({ style: on ? "single" : "none" });
|
|
227
|
+
return type.create(hasTwin ? { value: on, complexScript: twin } : { value: on });
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The inline nodes a toggle formats in a range (not markers: nothing to format), and those it
|
|
232
|
+
* judges: text nodes, or every one when there is none.
|
|
233
|
+
*/
|
|
234
|
+
function inlineIn(state: EditorState, from: number, to: number, type: MarkType) {
|
|
235
|
+
const found: { node: Node; pos: number; parent: Node }[] = [];
|
|
236
|
+
state.doc.nodesBetween(from, to, (node, pos, parent) => {
|
|
237
|
+
if (!node.isInline || !parent?.type.allowsMarkType(type)) return undefined;
|
|
238
|
+
if (MARKERS.has(node.type.name)) return false;
|
|
239
|
+
found.push({ node, pos, parent });
|
|
240
|
+
return false;
|
|
241
|
+
});
|
|
242
|
+
const text = found.filter((f) => f.node.isText);
|
|
243
|
+
return text.length ? { all: found, judged: text } : { all: found, judged: found };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Whether all the selected text (or the text typed at a caret) shows the toggle `type`. */
|
|
247
|
+
export function wordToggleActive(
|
|
248
|
+
context: WordContext,
|
|
249
|
+
state: EditorState,
|
|
250
|
+
type: MarkType,
|
|
251
|
+
caretMarks: readonly Mark[] | null,
|
|
252
|
+
): boolean {
|
|
253
|
+
const { selection } = state;
|
|
254
|
+
if (caretMarks) return shows(context, selection.$from.parent, caretMarks, type);
|
|
255
|
+
const { judged } = inlineIn(state, selection.from, selection.to, type);
|
|
256
|
+
return judged.length > 0 && judged.every((f) => shows(context, f.parent, f.node.marks, type));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* The stored marks for a toggle at a caret: what makes the next typed text show the opposite of
|
|
261
|
+
* what text typed there would show now.
|
|
262
|
+
*/
|
|
263
|
+
export function toggledCaretMarks(
|
|
264
|
+
context: WordContext,
|
|
265
|
+
paragraph: Node,
|
|
266
|
+
marks: readonly Mark[],
|
|
267
|
+
type: MarkType,
|
|
268
|
+
): readonly Mark[] {
|
|
269
|
+
const on = !shows(context, paragraph, marks, type);
|
|
270
|
+
const existing = type.isInSet(marks);
|
|
271
|
+
const rest = existing ? existing.removeFromSet(marks) : marks;
|
|
272
|
+
const mark = directMark(type, on, shows(context, paragraph, marks, type, false), existing);
|
|
273
|
+
return mark ? mark.addToSet(rest) : rest;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* The mark changes that make every inline node in `from..to` show the toggle `type` as `on`:
|
|
278
|
+
* per node, a direct mark where its style does not already give that, none where it does.
|
|
279
|
+
*/
|
|
280
|
+
export function toggleRanges(
|
|
281
|
+
context: WordContext,
|
|
282
|
+
state: EditorState,
|
|
283
|
+
from: number,
|
|
284
|
+
to: number,
|
|
285
|
+
type: MarkType,
|
|
286
|
+
on: boolean,
|
|
287
|
+
): { from: number; to: number; mark: Mark | null }[] {
|
|
288
|
+
return inlineIn(state, from, to, type).all.map(({ node, pos, parent }) => ({
|
|
289
|
+
from: Math.max(from, pos),
|
|
290
|
+
to: Math.min(to, pos + node.nodeSize),
|
|
291
|
+
mark: directMark(
|
|
292
|
+
type,
|
|
293
|
+
on,
|
|
294
|
+
shows(context, parent, node.marks, type, false),
|
|
295
|
+
type.isInSet(node.marks),
|
|
296
|
+
),
|
|
297
|
+
}));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Zero-width markers: nothing to see, so the caret and deletion step over them. */
|
|
301
|
+
const MARKERS = new Set([
|
|
302
|
+
"bookmarkStart",
|
|
303
|
+
"bookmarkEnd",
|
|
304
|
+
"commentStart",
|
|
305
|
+
"commentEnd",
|
|
306
|
+
// A comment's reference mark: deleting it would take the comment's anchor.
|
|
307
|
+
"commentReference",
|
|
308
|
+
]);
|
|
309
|
+
|
|
310
|
+
/** Whether `node` is an invisible marker of a v3 document. */
|
|
311
|
+
export function isMarker(context: WordContext | null, node: Node | null | undefined): boolean {
|
|
312
|
+
return context !== null && !!node && MARKERS.has(node.type.name);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** Opaque content: deleted only once selected, as Word selects a field before deleting it. */
|
|
316
|
+
export function isOpaque(context: WordContext | null, node: Node | null | undefined): boolean {
|
|
317
|
+
return (
|
|
318
|
+
context !== null &&
|
|
319
|
+
!!node &&
|
|
320
|
+
(node.type.name === "opaqueInline" || node.type.name === "opaqueBlock")
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** A node selection of the node at `pos`, or a text selection when it cannot be selected. */
|
|
325
|
+
export function selectionAt(state: EditorState, pos: number): NodeSelection | TextSelection {
|
|
326
|
+
const node = state.doc.nodeAt(pos);
|
|
327
|
+
return node && NodeSelection.isSelectable(node)
|
|
328
|
+
? NodeSelection.create(state.doc, pos)
|
|
329
|
+
: TextSelection.create(state.doc, pos);
|
|
330
|
+
}
|