@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
package/src/state/lists.ts
CHANGED
|
@@ -1,425 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
// independent implementation on Foli's model.
|
|
1
|
+
import { type Command, Plugin } from "./state.js";
|
|
3
2
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} from "
|
|
13
|
-
import { ReplaceAroundStep } from "../transform/steps.js";
|
|
14
|
-
import { canJoin, canSplit, findWrapping, liftTarget } from "../transform/structure.js";
|
|
15
|
-
import { TransformError } from "../transform/transform.js";
|
|
16
|
-
import { closeHistory } from "./history.js";
|
|
17
|
-
import { Selection, TextSelection } from "./selection.js";
|
|
18
|
-
import { type Command, type EditorState, Plugin, type Transaction } from "./state.js";
|
|
3
|
+
endWordList,
|
|
4
|
+
type ListKind,
|
|
5
|
+
liftWordList,
|
|
6
|
+
removeWordListNumber,
|
|
7
|
+
sinkWordList,
|
|
8
|
+
toggleWordList,
|
|
9
|
+
wordListKindAt,
|
|
10
|
+
wrapInWordList,
|
|
11
|
+
} from "./wordformat.js";
|
|
19
12
|
|
|
20
|
-
/** The
|
|
21
|
-
const
|
|
13
|
+
/** The kind of list all selected paragraphs are in ("bulletList", "orderedList"), or null. */
|
|
14
|
+
export const listKindAt = wordListKindAt;
|
|
22
15
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function nodeTypeOf(state: EditorState, type: string | NodeType): NodeType | null {
|
|
28
|
-
return typeof type === "string" ? (state.schema.nodes[type] ?? null) : type;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Runs `build`; a TransformError means the command does not apply (nothing dispatched). A list
|
|
33
|
-
* change is its own undo step (it never merges with the edit before it).
|
|
34
|
-
*/
|
|
35
|
-
function apply(
|
|
36
|
-
state: EditorState,
|
|
37
|
-
dispatch: ((tr: Transaction) => void) | undefined,
|
|
38
|
-
build: (tr: Transaction) => boolean,
|
|
39
|
-
): boolean {
|
|
40
|
-
const tr = state.tr;
|
|
41
|
-
try {
|
|
42
|
-
if (!build(tr)) return false;
|
|
43
|
-
} catch (error) {
|
|
44
|
-
if (error instanceof TransformError) return false;
|
|
45
|
-
throw error;
|
|
46
|
-
}
|
|
47
|
-
dispatch?.(tr.setMeta(closeHistory, true));
|
|
48
|
-
return true;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* The range of whole list items the selection covers (in text), or null outside a list. A
|
|
53
|
-
* selection that runs past the innermost list around its start is cut at that list's end.
|
|
54
|
-
*/
|
|
55
|
-
function itemRange(state: EditorState, item: NodeType): NodeRange | null {
|
|
56
|
-
const $from = textStart(state);
|
|
57
|
-
const { $to } = state.selection;
|
|
58
|
-
let $end = $to.parent.inlineContent ? $to : Selection.near($to, -1).$to;
|
|
59
|
-
const depth = listDepth($from, item);
|
|
60
|
-
if (depth >= 0 && $end.pos > $from.end(depth)) {
|
|
61
|
-
$end = Selection.near(state.doc.resolve($from.end(depth)), -1).$to;
|
|
62
|
-
}
|
|
63
|
-
return $from.blockRange($end, (node) => node.childCount > 0 && node.firstChild?.type === item);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
interface FoundList {
|
|
67
|
-
list: Node;
|
|
68
|
-
pos: number;
|
|
69
|
-
depth: number;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* The items of the innermost list of `type` that the text between `from` and `to` touches, or
|
|
74
|
-
* null when no list of `type` does.
|
|
75
|
-
*/
|
|
76
|
-
function innermostListRange(
|
|
77
|
-
doc: Node,
|
|
78
|
-
from: number,
|
|
79
|
-
to: number,
|
|
80
|
-
type: NodeType,
|
|
81
|
-
item: NodeType,
|
|
82
|
-
): NodeRange | null {
|
|
83
|
-
let found: FoundList | null = null as FoundList | null;
|
|
84
|
-
doc.nodesBetween(from, to, (node, pos) => {
|
|
85
|
-
if (node.isTextblock) return false;
|
|
86
|
-
if (node.type === type && isList(node, item)) {
|
|
87
|
-
const depth = doc.resolve(pos).depth;
|
|
88
|
-
if (!found || depth > found.depth) found = { list: node, pos, depth };
|
|
89
|
-
}
|
|
90
|
-
return undefined;
|
|
91
|
-
});
|
|
92
|
-
if (!found) return null;
|
|
93
|
-
const { list, pos, depth } = found;
|
|
94
|
-
let first = -1;
|
|
95
|
-
let last = -1;
|
|
96
|
-
list.forEach((child, offset) => {
|
|
97
|
-
const start = pos + 1 + offset;
|
|
98
|
-
const end = start + child.nodeSize;
|
|
99
|
-
if (start < to && end > from) {
|
|
100
|
-
if (first < 0) first = start;
|
|
101
|
-
last = end;
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
if (first < 0) return null;
|
|
105
|
-
return new NodeRange(doc.resolve(first + 1), doc.resolve(last - 1), depth + 1);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** The depth of the innermost list around `$pos`, or -1. */
|
|
109
|
-
function listDepth($pos: ResolvedPos, item: NodeType): number {
|
|
110
|
-
for (let depth = $pos.depth; depth > 0; depth--) {
|
|
111
|
-
if (isList($pos.node(depth), item)) return depth;
|
|
112
|
-
}
|
|
113
|
-
return -1;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Where the selection starts, in text: a selection of everything starts before the first block,
|
|
118
|
-
* outside every list; its first text position is in the first block.
|
|
119
|
-
*/
|
|
120
|
-
function textStart(state: EditorState): ResolvedPos {
|
|
121
|
-
const { $from } = state.selection;
|
|
122
|
-
return $from.parent.inlineContent ? $from : Selection.near($from, 1).$from;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** The type of the innermost list around the selection's start, or null (for the toolbar). */
|
|
126
|
-
export function listTypeAt(state: EditorState): NodeType | null {
|
|
127
|
-
const item = itemTypeOf(state);
|
|
128
|
-
if (!item) return null;
|
|
129
|
-
const $from = textStart(state);
|
|
130
|
-
const depth = listDepth($from, item);
|
|
131
|
-
return depth < 0 ? null : $from.node(depth).type;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** Wraps the selected blocks in a list of `listType` (each run of blocks becomes one item). */
|
|
135
|
-
export function wrapInList(listType: string | NodeType, attrs: Attrs | null = null): Command {
|
|
136
|
-
return (state, dispatch) => {
|
|
137
|
-
const type = nodeTypeOf(state, listType);
|
|
138
|
-
if (!type) return false;
|
|
139
|
-
const { $from, $to } = state.selection;
|
|
140
|
-
const range = $from.blockRange($to);
|
|
141
|
-
const wrappers = range && findWrapping(range, type, attrs);
|
|
142
|
-
if (!range || !wrappers) return false;
|
|
143
|
-
if (!dispatch) return true; // a dry run (the toolbar) builds nothing
|
|
144
|
-
return apply(state, dispatch, (tr) => {
|
|
145
|
-
tr.wrap(range, wrappers);
|
|
146
|
-
return true;
|
|
147
|
-
});
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Enter in a list item: splits the item at the selection (the rest of the paragraph and the
|
|
153
|
-
* blocks after it start a new item). In an empty paragraph that is the item's only content,
|
|
154
|
-
* the item is lifted instead (out of a nested list, or out of the list), as word processors do.
|
|
155
|
-
*/
|
|
156
|
-
export function splitListItem(itemType?: string | NodeType): Command {
|
|
157
|
-
return (state, dispatch) => {
|
|
158
|
-
const item = itemType ? nodeTypeOf(state, itemType) : itemTypeOf(state);
|
|
159
|
-
const { $from, $to } = state.selection;
|
|
160
|
-
if (item && !$from.sameParent($to)) return splitAfterDelete(state, dispatch, item);
|
|
161
|
-
if (!item || $from.depth < 2 || !$from.parent.isTextblock) return false;
|
|
162
|
-
if ($from.node(-1).type !== item) return false;
|
|
163
|
-
if (!$from.parent.content.size && $from.node(-1).childCount === $from.indexAfter(-1)) {
|
|
164
|
-
// An empty last block: the whole item, or a trailing empty paragraph in it.
|
|
165
|
-
if ($from.node(-1).childCount === 1) return liftListItem(item)(state, dispatch);
|
|
166
|
-
const boundary = $from.before();
|
|
167
|
-
if (!canSplit(state.doc, boundary, 1)) return false;
|
|
168
|
-
return apply(state, dispatch, (tr) => {
|
|
169
|
-
tr.split(boundary, 1);
|
|
170
|
-
tr.setSelection(TextSelection.create(tr.doc, boundary + 3));
|
|
171
|
-
return true;
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
const atEnd = $to.pos === $from.end();
|
|
175
|
-
const next = atEnd ? $from.node(-1).contentMatchAt(0).defaultType : null;
|
|
176
|
-
return apply(state, dispatch, (tr) => {
|
|
177
|
-
tr.delete($from.pos, $to.pos);
|
|
178
|
-
const types = next ? [null, { type: next }] : undefined;
|
|
179
|
-
if (!canSplit(tr.doc, $from.pos, 2, types)) return false;
|
|
180
|
-
tr.split($from.pos, 2, types);
|
|
181
|
-
tr.setSelection(TextSelection.create(tr.doc, $from.pos + 4));
|
|
182
|
-
return true;
|
|
183
|
-
});
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/** Enter over a range across blocks: deletes it, then splits the list item where it was. */
|
|
188
|
-
function splitAfterDelete(
|
|
189
|
-
state: EditorState,
|
|
190
|
-
dispatch: ((tr: Transaction) => void) | undefined,
|
|
191
|
-
item: NodeType,
|
|
192
|
-
): boolean {
|
|
193
|
-
const tr = state.tr;
|
|
194
|
-
try {
|
|
195
|
-
tr.deleteSelection();
|
|
196
|
-
} catch (error) {
|
|
197
|
-
if (error instanceof TransformError) return false;
|
|
198
|
-
throw error;
|
|
199
|
-
}
|
|
200
|
-
const deleted = state.apply(tr);
|
|
201
|
-
if (!deleted.selection.$from.sameParent(deleted.selection.$to)) return false;
|
|
202
|
-
const splits: Transaction[] = [];
|
|
203
|
-
if (!splitListItem(item)(deleted, (split) => splits.push(split))) return false;
|
|
204
|
-
const split = splits[0];
|
|
205
|
-
if (dispatch && split) {
|
|
206
|
-
for (const step of split.steps) tr.step(step);
|
|
207
|
-
const { from, to } = split.selection;
|
|
208
|
-
tr.setSelection(TextSelection.create(tr.doc, from, to));
|
|
209
|
-
dispatch(tr.setMeta(closeHistory, true));
|
|
210
|
-
}
|
|
211
|
-
return true;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Shift+Tab: lifts the selected list items one level: out of a nested list into the parent
|
|
216
|
-
* list, or out of a top-level list (they become plain blocks).
|
|
217
|
-
*/
|
|
218
|
-
export function liftListItem(itemType?: string | NodeType): Command {
|
|
219
|
-
return (state, dispatch) => {
|
|
220
|
-
const item = itemType ? nodeTypeOf(state, itemType) : itemTypeOf(state);
|
|
221
|
-
if (!item) return false;
|
|
222
|
-
const range = itemRange(state, item);
|
|
223
|
-
if (!range) return false;
|
|
224
|
-
const nested = range.depth > 0 && range.$from.node(range.depth - 1).type === item;
|
|
225
|
-
return apply(state, dispatch, (tr) =>
|
|
226
|
-
nested ? liftToOuterList(tr, item, range) : liftOutOfList(tr, range),
|
|
227
|
-
);
|
|
228
|
-
};
|
|
229
|
-
}
|
|
16
|
+
/** Numbers the selected paragraphs as a list of `kind` (one right before goes on). */
|
|
17
|
+
export const wrapInList: (kind: ListKind) => Command = wrapInWordList;
|
|
230
18
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const end = range.end;
|
|
234
|
-
const endOfList = range.$to.end(range.depth);
|
|
235
|
-
if (end < endOfList) {
|
|
236
|
-
// Items after the lifted ones become a list nested in the last lifted item.
|
|
237
|
-
tr.step(
|
|
238
|
-
new ReplaceAroundStep(
|
|
239
|
-
end - 1,
|
|
240
|
-
endOfList,
|
|
241
|
-
end,
|
|
242
|
-
endOfList,
|
|
243
|
-
new Slice(Fragment.from(item.createOpen(null, range.parent.copy())), 1, 0),
|
|
244
|
-
1,
|
|
245
|
-
true,
|
|
246
|
-
),
|
|
247
|
-
);
|
|
248
|
-
range = new NodeRange(tr.doc.resolve(range.$from.pos), tr.doc.resolve(endOfList), range.depth);
|
|
249
|
-
}
|
|
250
|
-
const target = liftTarget(range);
|
|
251
|
-
if (target === null) return false;
|
|
252
|
-
tr.lift(range, target);
|
|
253
|
-
const after = tr.mapping.map(end, -1) - 1;
|
|
254
|
-
if (canJoin(tr.doc, after)) {
|
|
255
|
-
const $after = tr.doc.resolve(after);
|
|
256
|
-
if ($after.nodeBefore?.type === $after.nodeAfter?.type) tr.join(after);
|
|
257
|
-
}
|
|
258
|
-
return true;
|
|
259
|
-
}
|
|
19
|
+
/** The list buttons: removes the selection's numbering when it is a list of `kind`, else numbers it. */
|
|
20
|
+
export const toggleList: (kind: ListKind) => Command = toggleWordList;
|
|
260
21
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
const rest = continuation(list, range.startIndex);
|
|
264
|
-
// Merge the lifted items into one, so that one lift takes their content out.
|
|
265
|
-
for (let pos = range.end, i = range.endIndex - 1; i > range.startIndex; i--) {
|
|
266
|
-
pos -= list.child(i).nodeSize;
|
|
267
|
-
tr.delete(pos - 1, pos + 1);
|
|
268
|
-
}
|
|
269
|
-
const $start = tr.doc.resolve(range.start);
|
|
270
|
-
const merged = $start.nodeAfter;
|
|
271
|
-
if (!merged || tr.mapping.map(range.end) !== range.start + merged.nodeSize) return false;
|
|
272
|
-
const atStart = range.startIndex === 0;
|
|
273
|
-
const atEnd = range.endIndex === list.childCount;
|
|
274
|
-
const parent = $start.node(-1);
|
|
275
|
-
const indexBefore = $start.index(-1);
|
|
276
|
-
if (
|
|
277
|
-
!parent.canReplace(
|
|
278
|
-
indexBefore + (atStart ? 0 : 1),
|
|
279
|
-
indexBefore + 1,
|
|
280
|
-
merged.content.append(atEnd ? Fragment.empty : Fragment.from(list)),
|
|
281
|
-
)
|
|
282
|
-
) {
|
|
283
|
-
return false;
|
|
284
|
-
}
|
|
285
|
-
const start = $start.pos;
|
|
286
|
-
const end = start + merged.nodeSize;
|
|
287
|
-
// Strip the list item and the list around it; where the list continues, close or reopen it.
|
|
288
|
-
tr.step(
|
|
289
|
-
new ReplaceAroundStep(
|
|
290
|
-
start - (atStart ? 1 : 0),
|
|
291
|
-
end + (atEnd ? 1 : 0),
|
|
292
|
-
start + 1,
|
|
293
|
-
end - 1,
|
|
294
|
-
new Slice(
|
|
295
|
-
(atStart ? Fragment.empty : Fragment.from(list.copy(Fragment.empty))).append(
|
|
296
|
-
atEnd ? Fragment.empty : Fragment.from(rest),
|
|
297
|
-
),
|
|
298
|
-
atStart ? 0 : 1,
|
|
299
|
-
atEnd ? 0 : 1,
|
|
300
|
-
),
|
|
301
|
-
atStart ? 0 : 1,
|
|
302
|
-
true,
|
|
303
|
-
),
|
|
304
|
-
);
|
|
305
|
-
return true;
|
|
306
|
-
}
|
|
22
|
+
/** Enter in an empty numbered paragraph: one level up, or at the top level ends the list. */
|
|
23
|
+
export const splitListItem: Command = endWordList;
|
|
307
24
|
|
|
308
|
-
/**
|
|
309
|
-
|
|
310
|
-
* counting from the items before the lifted ones (as word processors do).
|
|
311
|
-
*/
|
|
312
|
-
function continuation(list: Node, before: number): Node {
|
|
313
|
-
const start = list.attrs.start;
|
|
314
|
-
if (typeof start !== "number" || before === 0) return list.copy(Fragment.empty);
|
|
315
|
-
try {
|
|
316
|
-
return list.type.createOpen({ ...list.attrs, start: start + before });
|
|
317
|
-
} catch (error) {
|
|
318
|
-
if (error instanceof FoliDocumentError) return list.copy(Fragment.empty);
|
|
319
|
-
throw error;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
25
|
+
/** Tab at the start of a numbered paragraph, or over several paragraphs: one level deeper. */
|
|
26
|
+
export const sinkListItem: Command = sinkWordList;
|
|
322
27
|
|
|
323
|
-
/** Tab
|
|
324
|
-
export
|
|
325
|
-
return (state, dispatch) => {
|
|
326
|
-
const item = itemType ? nodeTypeOf(state, itemType) : itemTypeOf(state);
|
|
327
|
-
if (!item) return false;
|
|
328
|
-
const range = itemRange(state, item);
|
|
329
|
-
if (!range || range.startIndex === 0) return false;
|
|
330
|
-
const parent = range.parent;
|
|
331
|
-
const before = parent.child(range.startIndex - 1);
|
|
332
|
-
if (before.type !== item) return false;
|
|
333
|
-
return apply(state, dispatch, (tr) => {
|
|
334
|
-
// Join a nested list that already ends the item before, else open a new one.
|
|
335
|
-
const nestedBefore = before.lastChild?.type === parent.type;
|
|
336
|
-
const inner = Fragment.from(nestedBefore ? item.createOpen() : null);
|
|
337
|
-
const slice = new Slice(
|
|
338
|
-
Fragment.from(item.createOpen(null, Fragment.from(parent.type.createOpen(null, inner)))),
|
|
339
|
-
nestedBefore ? 3 : 1,
|
|
340
|
-
0,
|
|
341
|
-
);
|
|
342
|
-
const { start, end } = range;
|
|
343
|
-
tr.step(
|
|
344
|
-
new ReplaceAroundStep(start - (nestedBefore ? 3 : 1), end, start, end, slice, 1, true),
|
|
345
|
-
);
|
|
346
|
-
return true;
|
|
347
|
-
});
|
|
348
|
-
};
|
|
349
|
-
}
|
|
28
|
+
/** Shift+Tab in a list: one level up (out of the list at the top). */
|
|
29
|
+
export const liftListItem: Command = liftWordList;
|
|
350
30
|
|
|
351
|
-
/**
|
|
352
|
-
|
|
353
|
-
* processors do, instead of merging it into the item before.
|
|
354
|
-
*/
|
|
355
|
-
export function liftListItemAtStart(itemType?: string | NodeType): Command {
|
|
356
|
-
return (state, dispatch) => {
|
|
357
|
-
const item = itemType ? nodeTypeOf(state, itemType) : itemTypeOf(state);
|
|
358
|
-
const { $from, empty } = state.selection;
|
|
359
|
-
if (!item || !empty || $from.parentOffset > 0 || $from.depth < 2) return false;
|
|
360
|
-
if ($from.node(-1).type !== item || $from.index(-1) !== 0) return false;
|
|
361
|
-
return liftListItem(item)(state, dispatch);
|
|
362
|
-
};
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* The list buttons: in a list of `listType`, lifts the selected items out of it; in a list of
|
|
367
|
-
* another type, changes that list's type; elsewhere, wraps the selection in a new list.
|
|
368
|
-
*/
|
|
369
|
-
export function toggleList(listType: string | NodeType, attrs: Attrs | null = null): Command {
|
|
370
|
-
return (state, dispatch) => {
|
|
371
|
-
const type = nodeTypeOf(state, listType);
|
|
372
|
-
const item = itemTypeOf(state);
|
|
373
|
-
if (!type || !item) return false;
|
|
374
|
-
const $from = textStart(state);
|
|
375
|
-
const depth = listDepth($from, item);
|
|
376
|
-
if (depth < 0) return wrapInList(type, attrs)(state, dispatch);
|
|
377
|
-
const list = $from.node(depth);
|
|
378
|
-
if (list.type !== type) {
|
|
379
|
-
return apply(state, dispatch, (tr) => {
|
|
380
|
-
tr.setNodeMarkup($from.before(depth), type, attrs);
|
|
381
|
-
return true;
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
// Out of every list of this type the selection touches (innermost first, so items of an
|
|
385
|
-
// outer list that are not selected stay), in one undo step.
|
|
386
|
-
return apply(state, dispatch, (tr) => {
|
|
387
|
-
let lifted = false;
|
|
388
|
-
// shortcut: one pass per list level and list touched (each re-resolves the document);
|
|
389
|
-
// fine for documents of a few hundred lists, batch the passes if that ever shows up.
|
|
390
|
-
for (let guard = 0; guard < 10_000; guard++) {
|
|
391
|
-
const current = state.apply(tr);
|
|
392
|
-
const { from, to } = current.selection;
|
|
393
|
-
const range = innermostListRange(current.doc, from, to, type, item);
|
|
394
|
-
if (!range) break;
|
|
395
|
-
const pass = current.tr;
|
|
396
|
-
const nested = range.depth > 0 && range.$from.node(range.depth - 1).type === item;
|
|
397
|
-
try {
|
|
398
|
-
if (!(nested ? liftToOuterList(pass, item, range) : liftOutOfList(pass, range))) break;
|
|
399
|
-
} catch (error) {
|
|
400
|
-
if (error instanceof TransformError) break;
|
|
401
|
-
throw error;
|
|
402
|
-
}
|
|
403
|
-
for (const step of pass.steps) tr.step(step);
|
|
404
|
-
lifted = true;
|
|
405
|
-
}
|
|
406
|
-
return lifted;
|
|
407
|
-
});
|
|
408
|
-
};
|
|
409
|
-
}
|
|
31
|
+
/** Backspace at the start of a numbered paragraph: removes its number, keeping the text in place. */
|
|
32
|
+
export const liftListItemAtStart: Command = removeWordListNumber;
|
|
410
33
|
|
|
411
34
|
/**
|
|
412
|
-
* The list key bindings: Enter
|
|
413
|
-
*
|
|
414
|
-
*
|
|
35
|
+
* The list key bindings, as Word's: Enter and Backspace in a numbered paragraph, Tab and
|
|
36
|
+
* Shift+Tab for levels (elsewhere Tab types a tab), Mod-Shift-7 / Mod-Shift-8 toggle a
|
|
37
|
+
* numbered / bulleted list.
|
|
415
38
|
*/
|
|
416
39
|
export function listKeymap(): Plugin {
|
|
417
40
|
return new Plugin({
|
|
418
41
|
keymap: {
|
|
419
|
-
Enter: splitListItem
|
|
420
|
-
Backspace: liftListItemAtStart
|
|
421
|
-
Tab: sinkListItem
|
|
422
|
-
"Shift-Tab": liftListItem
|
|
42
|
+
Enter: splitListItem,
|
|
43
|
+
Backspace: liftListItemAtStart,
|
|
44
|
+
Tab: sinkListItem,
|
|
45
|
+
"Shift-Tab": liftListItem,
|
|
423
46
|
"Mod-Shift-7": toggleList("orderedList"),
|
|
424
47
|
"Mod-Shift-8": toggleList("bulletList"),
|
|
425
48
|
},
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What Word keeps on a paragraph's mark, and the marks typed text must not take: shared by the
|
|
3
|
+
* transaction (deleting across paragraphs) and the commands. No imports from the state module
|
|
4
|
+
* (it is imported by it).
|
|
5
|
+
*/
|
|
6
|
+
import type { Mark, Node } from "../model/index.js";
|
|
7
|
+
import type { Transform } from "../transform/transform.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Attributes the paragraph mark carries in Word (schema v3): the section break, the mark's
|
|
11
|
+
* tracked change and the paragraph's own attributes (`w14:paraId`). Whichever paragraph keeps
|
|
12
|
+
* the mark keeps them.
|
|
13
|
+
*/
|
|
14
|
+
export const MARK_ATTRS = ["section", "revision", "formatChange", "attributes"] as const;
|
|
15
|
+
|
|
16
|
+
/** Inline nodes that stand between runs: bookmarks and comment ranges. */
|
|
17
|
+
const MARKERS = new Set(["bookmarkStart", "bookmarkEnd", "commentStart", "commentEnd"]);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* After a join, or a deletion across paragraphs, the merged paragraph at `pos` ends with the
|
|
21
|
+
* last paragraph's mark, as in Word: it takes that paragraph's mark attributes (deleting a
|
|
22
|
+
* section break merges its section into the next one). When the first paragraph left nothing
|
|
23
|
+
* (it was empty, or deleted from its start), it is gone: the last one stays whole, as in Word.
|
|
24
|
+
* `tail` is where the content kept of the last one starts in it (a deletion into it). Nothing for
|
|
25
|
+
* a schema without them (v2).
|
|
26
|
+
*/
|
|
27
|
+
export function keepLastMark(tr: Transform, pos: number, last: Node, tail = 0): void {
|
|
28
|
+
const merged = tr.doc.nodeAt(pos);
|
|
29
|
+
if (!merged?.isTextblock || !Object.hasOwn(merged.attrs, "revision")) return;
|
|
30
|
+
// What the first paragraph left, bookmarks and comment ranges aside (headings often start
|
|
31
|
+
// with a bookmark).
|
|
32
|
+
let left = false;
|
|
33
|
+
merged.content.cut(0, merged.content.size - (last.content.size - tail)).forEach((child) => {
|
|
34
|
+
if (!MARKERS.has(child.type.name)) left = true;
|
|
35
|
+
});
|
|
36
|
+
if (!left) {
|
|
37
|
+
if (merged.type !== last.type || JSON.stringify(merged.attrs) !== JSON.stringify(last.attrs))
|
|
38
|
+
tr.setNodeMarkup(pos, last.type, last.attrs);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const attrs = { ...merged.attrs };
|
|
42
|
+
let changed = false;
|
|
43
|
+
for (const key of MARK_ATTRS) {
|
|
44
|
+
// Schema v2 has none of them: both sides null, nothing changes.
|
|
45
|
+
const value = last.attrs[key] ?? null;
|
|
46
|
+
if (JSON.stringify(attrs[key] ?? null) === JSON.stringify(value)) continue;
|
|
47
|
+
attrs[key] = value;
|
|
48
|
+
changed = true;
|
|
49
|
+
}
|
|
50
|
+
if (changed) tr.setNodeMarkup(pos, merged.type, attrs);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Tracked-change marks: typed or pasted text is not part of someone's recorded change. */
|
|
54
|
+
const REVIEW_MARKS = new Set(["insertion", "deletion", "formatChange"]);
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The marks text typed among `marks` gets: without tracked-change marks.
|
|
58
|
+
* shortcut: until Phase 9 records changes, typed text is never marked as one.
|
|
59
|
+
*/
|
|
60
|
+
export function typedMarks(marks: readonly Mark[]): readonly Mark[] {
|
|
61
|
+
return marks.some((m) => REVIEW_MARKS.has(m.type.name))
|
|
62
|
+
? marks.filter((m) => !REVIEW_MARKS.has(m.type.name))
|
|
63
|
+
: marks;
|
|
64
|
+
}
|