@folijs/core 0.1.0 → 0.3.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 +39 -0
- package/README.md +1 -1
- package/dist/document.d.ts +10 -15
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +41 -19
- 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/attrs.d.ts +5 -0
- package/dist/model/attrs.d.ts.map +1 -1
- package/dist/model/attrs.js +17 -0
- package/dist/model/attrs.js.map +1 -1
- package/dist/model/content.d.ts +10 -0
- package/dist/model/content.d.ts.map +1 -1
- package/dist/model/content.js +44 -0
- package/dist/model/content.js.map +1 -1
- package/dist/model/fragment.d.ts +7 -2
- package/dist/model/fragment.d.ts.map +1 -1
- package/dist/model/fragment.js +13 -2
- package/dist/model/fragment.js.map +1 -1
- package/dist/model/index.d.ts +1 -1
- package/dist/model/index.d.ts.map +1 -1
- package/dist/model/index.js +1 -1
- package/dist/model/index.js.map +1 -1
- package/dist/model/mark.d.ts.map +1 -1
- package/dist/model/mark.js +4 -3
- package/dist/model/mark.js.map +1 -1
- package/dist/model/node.d.ts +13 -1
- package/dist/model/node.d.ts.map +1 -1
- package/dist/model/node.js +35 -3
- package/dist/model/node.js.map +1 -1
- package/dist/model/resolvedpos.d.ts +26 -2
- package/dist/model/resolvedpos.d.ts.map +1 -1
- package/dist/model/resolvedpos.js +54 -2
- package/dist/model/resolvedpos.js.map +1 -1
- package/dist/model/schema.d.ts +25 -0
- package/dist/model/schema.d.ts.map +1 -1
- package/dist/model/schema.js +23 -0
- package/dist/model/schema.js.map +1 -1
- package/dist/model/slice.d.ts +7 -0
- package/dist/model/slice.d.ts.map +1 -1
- package/dist/model/slice.js +53 -0
- package/dist/model/slice.js.map +1 -1
- package/dist/schema.d.ts +72 -8
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +93 -9
- package/dist/schema.js.map +1 -1
- package/dist/state/attempt.d.ts +4 -0
- package/dist/state/attempt.d.ts.map +1 -0
- package/dist/state/attempt.js +19 -0
- package/dist/state/attempt.js.map +1 -0
- package/dist/state/commands.d.ts +21 -5
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +260 -68
- package/dist/state/commands.js.map +1 -1
- package/dist/state/editor.d.ts +41 -7
- package/dist/state/editor.d.ts.map +1 -1
- package/dist/state/editor.js +51 -10
- package/dist/state/editor.js.map +1 -1
- package/dist/state/format.d.ts +13 -0
- package/dist/state/format.d.ts.map +1 -0
- package/dist/state/format.js +20 -0
- package/dist/state/format.js.map +1 -0
- package/dist/state/history.js +2 -2
- package/dist/state/history.js.map +1 -1
- package/dist/state/index.d.ts +10 -3
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +10 -3
- package/dist/state/index.js.map +1 -1
- package/dist/state/links.d.ts +42 -0
- package/dist/state/links.d.ts.map +1 -0
- package/dist/state/links.js +194 -0
- package/dist/state/links.js.map +1 -0
- package/dist/state/lists.d.ts +23 -0
- package/dist/state/lists.d.ts.map +1 -0
- package/dist/state/lists.js +34 -0
- package/dist/state/lists.js.map +1 -0
- package/dist/state/mark.d.ts +25 -0
- package/dist/state/mark.d.ts.map +1 -0
- package/dist/state/mark.js +40 -0
- package/dist/state/mark.js.map +1 -0
- package/dist/state/selection.d.ts +18 -2
- package/dist/state/selection.d.ts.map +1 -1
- package/dist/state/selection.js +63 -2
- package/dist/state/selection.js.map +1 -1
- package/dist/state/state.d.ts.map +1 -1
- package/dist/state/state.js +23 -2
- package/dist/state/state.js.map +1 -1
- package/dist/state/word.d.ts +72 -0
- package/dist/state/word.d.ts.map +1 -0
- package/dist/state/word.js +173 -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/transform/index.d.ts +2 -1
- package/dist/transform/index.d.ts.map +1 -1
- package/dist/transform/index.js +2 -1
- package/dist/transform/index.js.map +1 -1
- package/dist/transform/stepmap.d.ts +8 -6
- package/dist/transform/stepmap.d.ts.map +1 -1
- package/dist/transform/stepmap.js +18 -15
- package/dist/transform/stepmap.js.map +1 -1
- package/dist/transform/steps.d.ts +27 -2
- package/dist/transform/steps.d.ts.map +1 -1
- package/dist/transform/steps.js +115 -4
- package/dist/transform/steps.js.map +1 -1
- package/dist/transform/structure.d.ts +26 -0
- package/dist/transform/structure.d.ts.map +1 -0
- package/dist/transform/structure.js +116 -0
- package/dist/transform/structure.js.map +1 -0
- package/dist/transform/transform.d.ts +34 -11
- package/dist/transform/transform.d.ts.map +1 -1
- package/dist/transform/transform.js +145 -18
- package/dist/transform/transform.js.map +1 -1
- 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 +27 -0
- package/dist/v3/schema.d.ts.map +1 -0
- package/dist/v3/schema.js +429 -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 +49 -23
- package/src/index.ts +17 -6
- package/src/model/attrs.ts +19 -0
- package/src/model/content.ts +56 -0
- package/src/model/fragment.ts +18 -3
- package/src/model/index.ts +1 -1
- package/src/model/mark.ts +3 -2
- package/src/model/node.ts +48 -3
- package/src/model/resolvedpos.ts +55 -2
- package/src/model/schema.ts +43 -0
- package/src/model/slice.ts +81 -1
- package/src/schema.ts +148 -13
- package/src/state/attempt.ts +22 -0
- package/src/state/commands.ts +287 -69
- package/src/state/editor.ts +103 -21
- package/src/state/format.ts +35 -0
- package/src/state/history.ts +3 -3
- package/src/state/index.ts +63 -1
- package/src/state/links.ts +193 -0
- package/src/state/lists.ts +50 -0
- package/src/state/mark.ts +47 -0
- package/src/state/selection.ts +72 -3
- package/src/state/state.ts +22 -2
- package/src/state/word.ts +245 -0
- package/src/state/wordclip.ts +445 -0
- package/src/state/wordformat.ts +982 -0
- package/src/transform/index.ts +8 -0
- package/src/transform/stepmap.ts +18 -12
- package/src/transform/steps.ts +126 -3
- package/src/transform/structure.ts +135 -0
- package/src/transform/transform.ts +165 -16
- 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 +479 -0
- package/src/v3/units.ts +34 -0
package/src/state/selection.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Mark, type Node, type ResolvedPos } from "../model/index.js";
|
|
|
2
2
|
import type { Mappable } from "../transform/stepmap.js";
|
|
3
3
|
|
|
4
4
|
export interface SelectionJSON {
|
|
5
|
-
type: "text" | "all";
|
|
5
|
+
type: "text" | "node" | "all";
|
|
6
6
|
anchor: number;
|
|
7
7
|
head: number;
|
|
8
8
|
}
|
|
@@ -78,6 +78,13 @@ export abstract class Selection {
|
|
|
78
78
|
static fromJSON(doc: Node, json: SelectionJSON): Selection {
|
|
79
79
|
if (json.type === "all") return new AllSelection(doc);
|
|
80
80
|
const clamp = (pos: number) => Math.max(0, Math.min(pos, doc.content.size));
|
|
81
|
+
if (json.type === "node") {
|
|
82
|
+
const $pos = doc.resolve(clamp(json.anchor));
|
|
83
|
+
const node = $pos.nodeAfter;
|
|
84
|
+
return node && !$pos.textOffset && NodeSelection.isSelectable(node)
|
|
85
|
+
? new NodeSelection($pos)
|
|
86
|
+
: Selection.near($pos);
|
|
87
|
+
}
|
|
81
88
|
return TextSelection.between(doc.resolve(clamp(json.anchor)), doc.resolve(clamp(json.head)));
|
|
82
89
|
}
|
|
83
90
|
}
|
|
@@ -141,6 +148,55 @@ export class TextSelection extends Selection {
|
|
|
141
148
|
}
|
|
142
149
|
}
|
|
143
150
|
|
|
151
|
+
/**
|
|
152
|
+
* One node selected as a unit (a horizontal rule, later an image): `from` is the position
|
|
153
|
+
* before it, `to` the position after it. Only selectable nodes (not text) can be selected.
|
|
154
|
+
*/
|
|
155
|
+
export class NodeSelection extends Selection {
|
|
156
|
+
readonly node: Node;
|
|
157
|
+
|
|
158
|
+
constructor($pos: ResolvedPos) {
|
|
159
|
+
const node = $pos.nodeAfter;
|
|
160
|
+
if (!node || $pos.textOffset || !NodeSelection.isSelectable(node)) {
|
|
161
|
+
throw new RangeError(`No selectable node at position ${$pos.pos}`);
|
|
162
|
+
}
|
|
163
|
+
super($pos, $pos.doc.resolve($pos.pos + node.nodeSize));
|
|
164
|
+
this.node = node;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
static create(doc: Node, from: number): NodeSelection {
|
|
168
|
+
return new NodeSelection(doc.resolve(from));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
static isSelectable(node: Node): boolean {
|
|
172
|
+
return !node.isText && node.type.spec.selectable !== false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Still the same node when it survives the mapping; a text selection near it otherwise. */
|
|
176
|
+
map(doc: Node, mapping: Mappable): Selection {
|
|
177
|
+
const from = mapping.mapResult(this.anchor, 1);
|
|
178
|
+
const to = mapping.mapResult(this.head, -1);
|
|
179
|
+
const $pos = doc.resolve(from.pos);
|
|
180
|
+
const node = $pos.nodeAfter;
|
|
181
|
+
// The node now at the start must still end at the mapped end: otherwise it was deleted
|
|
182
|
+
// (both ends collapse) or its content moved out (a lift maps its edges onto a child's).
|
|
183
|
+
if (
|
|
184
|
+
from.deleted ||
|
|
185
|
+
to.deleted ||
|
|
186
|
+
!node ||
|
|
187
|
+
$pos.textOffset ||
|
|
188
|
+
from.pos + node.nodeSize !== to.pos
|
|
189
|
+
) {
|
|
190
|
+
return Selection.near($pos);
|
|
191
|
+
}
|
|
192
|
+
return NodeSelection.isSelectable(node) ? new NodeSelection($pos) : Selection.near($pos);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
toJSON(): SelectionJSON {
|
|
196
|
+
return { type: "node", anchor: this.anchor, head: this.head };
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
144
200
|
/** The whole document. */
|
|
145
201
|
export class AllSelection extends Selection {
|
|
146
202
|
constructor(doc: Node) {
|
|
@@ -156,9 +212,22 @@ export class AllSelection extends Selection {
|
|
|
156
212
|
}
|
|
157
213
|
}
|
|
158
214
|
|
|
159
|
-
/**
|
|
215
|
+
/**
|
|
216
|
+
* Marks that text typed at `$pos` should get: those of the text before it (or after, at a block
|
|
217
|
+
* start). A mark that is not inclusive (a link) continues only where the text after has it too.
|
|
218
|
+
*/
|
|
160
219
|
export function marksAt($pos: ResolvedPos): readonly Mark[] {
|
|
161
220
|
if (!$pos.parent.inlineContent) return Mark.none;
|
|
162
221
|
if ($pos.textOffset) return $pos.parent.child($pos.index()).marks;
|
|
163
|
-
|
|
222
|
+
const before = $pos.nodeBefore;
|
|
223
|
+
const after = $pos.nodeAfter;
|
|
224
|
+
const main = before ?? after;
|
|
225
|
+
const other = before ? after : null;
|
|
226
|
+
let marks = main?.marks ?? Mark.none;
|
|
227
|
+
for (const mark of marks) {
|
|
228
|
+
if (!mark.type.inclusive && !(other && mark.isInSet(other.marks))) {
|
|
229
|
+
marks = mark.removeFromSet(marks);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return marks;
|
|
164
233
|
}
|
package/src/state/state.ts
CHANGED
|
@@ -7,8 +7,9 @@ import {
|
|
|
7
7
|
Slice,
|
|
8
8
|
} from "../model/index.js";
|
|
9
9
|
import type { Step } from "../transform/steps.js";
|
|
10
|
-
import { Transform } from "../transform/transform.js";
|
|
11
|
-
import {
|
|
10
|
+
import { Transform, TransformError } from "../transform/transform.js";
|
|
11
|
+
import { keepLastMark } from "./mark.js";
|
|
12
|
+
import { AllSelection, NodeSelection, Selection, TextSelection } from "./selection.js";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* A command: checks whether it applies to `state` and, given `dispatch`, performs it.
|
|
@@ -128,7 +129,26 @@ export class Transaction extends Transform {
|
|
|
128
129
|
return this.setSelection(Selection.atStart(this.doc));
|
|
129
130
|
}
|
|
130
131
|
const start = this.steps.length;
|
|
132
|
+
const { $from } = selection;
|
|
133
|
+
const index = $from.index();
|
|
134
|
+
if (selection instanceof NodeSelection && !$from.parent.canReplace(index, index + 1)) {
|
|
135
|
+
// The parent cannot lose its only (or a required) child: leave an empty default block.
|
|
136
|
+
const type = $from.parent.contentMatchAt(index).defaultType;
|
|
137
|
+
if (!type?.isTextblock) throw new TransformError("The selected node cannot be deleted");
|
|
138
|
+
this.replace(selection.from, selection.to, new Slice(Fragment.from(type.create()), 0, 0));
|
|
139
|
+
return this.setSelection(TextSelection.create(this.doc, selection.from + 1));
|
|
140
|
+
}
|
|
141
|
+
// Across paragraphs: the merged one ends with the last one's mark (schema v3: its section).
|
|
142
|
+
const { $to } = selection;
|
|
143
|
+
const last =
|
|
144
|
+
$from.depth > 0 &&
|
|
145
|
+
$from.parent !== $to.parent &&
|
|
146
|
+
$from.parent.isTextblock &&
|
|
147
|
+
$to.parent.isTextblock
|
|
148
|
+
? $to.parent
|
|
149
|
+
: null;
|
|
131
150
|
this.delete(selection.from, selection.to);
|
|
151
|
+
if (last) keepLastMark(this, $from.before(), last);
|
|
132
152
|
const caret = this.mapping.slice(start).map(selection.from, -1);
|
|
133
153
|
return this.setSelection(TextSelection.create(this.doc, caret));
|
|
134
154
|
}
|
|
@@ -0,0 +1,245 @@
|
|
|
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 type { FoliDocument } from "../v3/document.js";
|
|
14
|
+
import { Resolver, type RunFormat } from "../v3/resolve.js";
|
|
15
|
+
import { MARK_ATTRS } from "./mark.js";
|
|
16
|
+
import { NodeSelection, TextSelection } from "./selection.js";
|
|
17
|
+
import { type EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
|
|
18
|
+
|
|
19
|
+
/** A v3 document without its content: what the editor keeps beside the edited tree. */
|
|
20
|
+
export type WordEnvelope = Omit<FoliDocument, "content">;
|
|
21
|
+
|
|
22
|
+
export interface WordContext {
|
|
23
|
+
readonly envelope: WordEnvelope;
|
|
24
|
+
readonly resolver: Resolver;
|
|
25
|
+
/**
|
|
26
|
+
* Made when the document is loaded and kept while it is edited: a copy carries it, so a paste
|
|
27
|
+
* tells this document's lists from another's (Word's `w:nsid`; Foli makes equal definitions
|
|
28
|
+
* in every document).
|
|
29
|
+
*/
|
|
30
|
+
readonly id: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const key = new PluginKey<WordContext>("word");
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The plugin that gives commands a v3 document's envelope. A transaction changes it with
|
|
37
|
+
* `withEnvelope` (a new list's numbering definition); undo leaves such additions, as Word does.
|
|
38
|
+
*/
|
|
39
|
+
export function wordPlugin(envelope: WordEnvelope): Plugin<WordContext> {
|
|
40
|
+
// Not a secret: only tells documents apart.
|
|
41
|
+
const id = `${Date.now().toString(36)}${Math.random().toString(36).slice(2)}`;
|
|
42
|
+
const context: WordContext = { envelope, resolver: new Resolver(envelope), id };
|
|
43
|
+
return new Plugin({
|
|
44
|
+
key,
|
|
45
|
+
state: {
|
|
46
|
+
init: () => context,
|
|
47
|
+
apply: (tr, value) => {
|
|
48
|
+
const next = tr.getMeta(key) as WordEnvelope | undefined;
|
|
49
|
+
return next ? { envelope: next, resolver: new Resolver(next), id: value.id } : value;
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Makes `tr` change the document's envelope to `envelope` (styles, numbering…). */
|
|
56
|
+
export function withEnvelope(tr: Transaction, envelope: WordEnvelope): Transaction {
|
|
57
|
+
return tr.setMeta(key, envelope);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The envelope and resolver of the v3 document in `state`, or null for a v2 editor. */
|
|
61
|
+
export function wordContext(state: EditorState): WordContext | null {
|
|
62
|
+
return key.getState(state) ?? null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* What the paragraph mark carries alone (`MARK_ATTRS`): a split leaves it on the second
|
|
67
|
+
* paragraph, which keeps the original mark.
|
|
68
|
+
*/
|
|
69
|
+
const MARK_ONLY = Object.fromEntries(MARK_ATTRS.map((key) => [key, null]));
|
|
70
|
+
|
|
71
|
+
/** The id of the default paragraph style: the last one marked default, as `Resolver` reads it. */
|
|
72
|
+
function defaultParagraphStyle(envelope: WordEnvelope): string | undefined {
|
|
73
|
+
let id: string | undefined;
|
|
74
|
+
for (const style of envelope.styles)
|
|
75
|
+
if (style.type === "paragraph" && style.isDefault) id = style.id;
|
|
76
|
+
return id;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The attributes of the two paragraphs Enter makes of `paragraph`. The first gets a new mark
|
|
81
|
+
* with the same properties; the second keeps the original mark. At the end of a paragraph whose
|
|
82
|
+
* style names another as the next (a heading: Normal), the second paragraph is in that style,
|
|
83
|
+
* without the first's direct formatting. `before` is null when the first keeps every attribute.
|
|
84
|
+
*/
|
|
85
|
+
export function splitAttrs(
|
|
86
|
+
context: WordContext,
|
|
87
|
+
paragraph: Node,
|
|
88
|
+
atEnd: boolean,
|
|
89
|
+
): { before: Attrs | null; after: Attrs } {
|
|
90
|
+
const attrs = paragraph.attrs;
|
|
91
|
+
const markOnly = Object.keys(MARK_ONLY).some((k) => attrs[k] !== null);
|
|
92
|
+
const before = markOnly ? { ...attrs, ...MARK_ONLY } : null;
|
|
93
|
+
if (!atEnd) return { before, after: attrs };
|
|
94
|
+
const id = (attrs.style as string | null) ?? defaultParagraphStyle(context.envelope);
|
|
95
|
+
const style = context.envelope.styles.find((s) => s.id === id);
|
|
96
|
+
const next = style?.next;
|
|
97
|
+
if (next === undefined || next === id) return { before, after: attrs };
|
|
98
|
+
const mark = Object.fromEntries(Object.keys(MARK_ONLY).map((k) => [k, attrs[k] ?? null]));
|
|
99
|
+
return { before, after: { ...mark, style: next } };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Run toggles: whether text shows the property, from its resolved formatting. */
|
|
103
|
+
const TOGGLES: Record<string, (run: RunFormat) => boolean> = {
|
|
104
|
+
bold: (run) => run.bold,
|
|
105
|
+
italic: (run) => run.italic,
|
|
106
|
+
underline: (run) => run.underline !== null,
|
|
107
|
+
strike: (run) => run.strike,
|
|
108
|
+
doubleStrike: (run) => run.doubleStrike,
|
|
109
|
+
caps: (run) => run.caps,
|
|
110
|
+
smallCaps: (run) => run.smallCaps,
|
|
111
|
+
hidden: (run) => run.hidden,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/** Whether `type` is a run property the toggles handle on a v3 document. */
|
|
115
|
+
export function isWordToggle(context: WordContext | null, type: MarkType): boolean {
|
|
116
|
+
return context !== null && Object.hasOwn(TOGGLES, type.name);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Whether text with `marks` in `paragraph` shows the toggle `type` (marks included or not). */
|
|
120
|
+
function shows(
|
|
121
|
+
context: WordContext,
|
|
122
|
+
paragraph: Node,
|
|
123
|
+
marks: readonly Mark[],
|
|
124
|
+
type: MarkType,
|
|
125
|
+
own = true,
|
|
126
|
+
): boolean {
|
|
127
|
+
const { run } = context.resolver.paragraph(paragraph.attrs);
|
|
128
|
+
const json = marks.filter((m) => own || m.type !== type).map((m) => m.toJSON());
|
|
129
|
+
return (TOGGLES[type.name] as (run: RunFormat) => boolean)(context.resolver.run(run, json));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The direct mark that makes text show the toggle `type` as `on`, given what it shows without
|
|
134
|
+
* one (`inherited`); null when no mark is needed. Bold and italic keep their complex-script twin.
|
|
135
|
+
*/
|
|
136
|
+
function directMark(type: MarkType, on: boolean, inherited: boolean, existing?: Mark): Mark | null {
|
|
137
|
+
const twin = existing?.attrs.complexScript ?? null;
|
|
138
|
+
const hasTwin = Object.hasOwn(type.spec.attrs ?? {}, "complexScript");
|
|
139
|
+
if (on === inherited)
|
|
140
|
+
return hasTwin && twin !== null ? type.create({ value: null, complexScript: twin }) : null;
|
|
141
|
+
if (type.name === "underline") return type.create({ style: on ? "single" : "none" });
|
|
142
|
+
return type.create(hasTwin ? { value: on, complexScript: twin } : { value: on });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The inline nodes a toggle formats in a range (not markers: nothing to format), and those it
|
|
147
|
+
* judges: text nodes, or every one when there is none.
|
|
148
|
+
*/
|
|
149
|
+
function inlineIn(state: EditorState, from: number, to: number, type: MarkType) {
|
|
150
|
+
const found: { node: Node; pos: number; parent: Node }[] = [];
|
|
151
|
+
state.doc.nodesBetween(from, to, (node, pos, parent) => {
|
|
152
|
+
if (!node.isInline || !parent?.type.allowsMarkType(type)) return undefined;
|
|
153
|
+
if (MARKERS.has(node.type.name)) return false;
|
|
154
|
+
found.push({ node, pos, parent });
|
|
155
|
+
return false;
|
|
156
|
+
});
|
|
157
|
+
const text = found.filter((f) => f.node.isText);
|
|
158
|
+
return text.length ? { all: found, judged: text } : { all: found, judged: found };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Whether all the selected text (or the text typed at a caret) shows the toggle `type`. */
|
|
162
|
+
export function wordToggleActive(
|
|
163
|
+
context: WordContext,
|
|
164
|
+
state: EditorState,
|
|
165
|
+
type: MarkType,
|
|
166
|
+
caretMarks: readonly Mark[] | null,
|
|
167
|
+
): boolean {
|
|
168
|
+
const { selection } = state;
|
|
169
|
+
if (caretMarks) return shows(context, selection.$from.parent, caretMarks, type);
|
|
170
|
+
const { judged } = inlineIn(state, selection.from, selection.to, type);
|
|
171
|
+
return judged.length > 0 && judged.every((f) => shows(context, f.parent, f.node.marks, type));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The stored marks for a toggle at a caret: what makes the next typed text show the opposite of
|
|
176
|
+
* what text typed there would show now.
|
|
177
|
+
*/
|
|
178
|
+
export function toggledCaretMarks(
|
|
179
|
+
context: WordContext,
|
|
180
|
+
paragraph: Node,
|
|
181
|
+
marks: readonly Mark[],
|
|
182
|
+
type: MarkType,
|
|
183
|
+
): readonly Mark[] {
|
|
184
|
+
const on = !shows(context, paragraph, marks, type);
|
|
185
|
+
const existing = type.isInSet(marks);
|
|
186
|
+
const rest = existing ? existing.removeFromSet(marks) : marks;
|
|
187
|
+
const mark = directMark(type, on, shows(context, paragraph, marks, type, false), existing);
|
|
188
|
+
return mark ? mark.addToSet(rest) : rest;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The mark changes that make every inline node in `from..to` show the toggle `type` as `on`:
|
|
193
|
+
* per node, a direct mark where its style does not already give that, none where it does.
|
|
194
|
+
*/
|
|
195
|
+
export function toggleRanges(
|
|
196
|
+
context: WordContext,
|
|
197
|
+
state: EditorState,
|
|
198
|
+
from: number,
|
|
199
|
+
to: number,
|
|
200
|
+
type: MarkType,
|
|
201
|
+
on: boolean,
|
|
202
|
+
): { from: number; to: number; mark: Mark | null }[] {
|
|
203
|
+
return inlineIn(state, from, to, type).all.map(({ node, pos, parent }) => ({
|
|
204
|
+
from: Math.max(from, pos),
|
|
205
|
+
to: Math.min(to, pos + node.nodeSize),
|
|
206
|
+
mark: directMark(
|
|
207
|
+
type,
|
|
208
|
+
on,
|
|
209
|
+
shows(context, parent, node.marks, type, false),
|
|
210
|
+
type.isInSet(node.marks),
|
|
211
|
+
),
|
|
212
|
+
}));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Zero-width markers: nothing to see, so the caret and deletion step over them. */
|
|
216
|
+
const MARKERS = new Set([
|
|
217
|
+
"bookmarkStart",
|
|
218
|
+
"bookmarkEnd",
|
|
219
|
+
"commentStart",
|
|
220
|
+
"commentEnd",
|
|
221
|
+
// A comment's reference mark: deleting it would take the comment's anchor.
|
|
222
|
+
"commentReference",
|
|
223
|
+
]);
|
|
224
|
+
|
|
225
|
+
/** Whether `node` is an invisible marker of a v3 document. */
|
|
226
|
+
export function isMarker(context: WordContext | null, node: Node | null | undefined): boolean {
|
|
227
|
+
return context !== null && !!node && MARKERS.has(node.type.name);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Opaque content: deleted only once selected, as Word selects a field before deleting it. */
|
|
231
|
+
export function isOpaque(context: WordContext | null, node: Node | null | undefined): boolean {
|
|
232
|
+
return (
|
|
233
|
+
context !== null &&
|
|
234
|
+
!!node &&
|
|
235
|
+
(node.type.name === "opaqueInline" || node.type.name === "opaqueBlock")
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** A node selection of the node at `pos`, or a text selection when it cannot be selected. */
|
|
240
|
+
export function selectionAt(state: EditorState, pos: number): NodeSelection | TextSelection {
|
|
241
|
+
const node = state.doc.nodeAt(pos);
|
|
242
|
+
return node && NodeSelection.isSelectable(node)
|
|
243
|
+
? NodeSelection.create(state.doc, pos)
|
|
244
|
+
: TextSelection.create(state.doc, pos);
|
|
245
|
+
}
|