@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/commands.ts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
type Attrs,
|
|
3
|
+
FoliDocumentError,
|
|
4
|
+
Fragment,
|
|
5
|
+
type Mark,
|
|
6
|
+
type MarkType,
|
|
7
|
+
type Node,
|
|
8
|
+
type NodeType,
|
|
9
|
+
type ResolvedPos,
|
|
10
|
+
Slice,
|
|
11
|
+
} from "../model/index.js";
|
|
12
|
+
import { isSafeLink } from "../schema.js";
|
|
13
|
+
import { attempt } from "./attempt.js";
|
|
4
14
|
import { redo, undo } from "./history.js";
|
|
5
|
-
import {
|
|
15
|
+
import { openLinkEditor } from "./links.js";
|
|
16
|
+
import { keepLastMark, typedMarks } from "./mark.js";
|
|
17
|
+
import { AllSelection, marksAt, NodeSelection, Selection, TextSelection } from "./selection.js";
|
|
6
18
|
import {
|
|
7
19
|
type Command,
|
|
8
20
|
defaultTextblockType,
|
|
@@ -10,6 +22,18 @@ import {
|
|
|
10
22
|
Plugin,
|
|
11
23
|
type Transaction,
|
|
12
24
|
} from "./state.js";
|
|
25
|
+
import {
|
|
26
|
+
isMarker,
|
|
27
|
+
isOpaque,
|
|
28
|
+
isWordToggle,
|
|
29
|
+
selectionAt,
|
|
30
|
+
splitAttrs,
|
|
31
|
+
toggledCaretMarks,
|
|
32
|
+
toggleRanges,
|
|
33
|
+
wordContext,
|
|
34
|
+
wordToggleActive,
|
|
35
|
+
} from "./word.js";
|
|
36
|
+
import { defaultParagraphStyleOf, setParagraphStyle, setStyleNamed } from "./wordformat.js";
|
|
13
37
|
|
|
14
38
|
export type DeleteUnit = "char" | "word";
|
|
15
39
|
|
|
@@ -21,24 +45,11 @@ const LEAF = "\n";
|
|
|
21
45
|
const graphemes = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
22
46
|
const words = new Intl.Segmenter(undefined, { granularity: "word" });
|
|
23
47
|
|
|
24
|
-
/**
|
|
25
|
-
function
|
|
26
|
-
state
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
): boolean {
|
|
30
|
-
const tr = state.tr;
|
|
31
|
-
try {
|
|
32
|
-
build(tr);
|
|
33
|
-
} catch (error) {
|
|
34
|
-
if (error instanceof TransformError) return false;
|
|
35
|
-
throw error;
|
|
36
|
-
}
|
|
37
|
-
if (!tr.docChanged && tr.selection.eq(state.selection) && tr.storedMarks === state.storedMarks) {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
dispatch?.(tr);
|
|
41
|
-
return true;
|
|
48
|
+
/** `state` with a caret at `pos` (a command then runs as if the caret were there). */
|
|
49
|
+
function at(state: EditorState, pos: number): EditorState {
|
|
50
|
+
return pos === state.selection.head
|
|
51
|
+
? state
|
|
52
|
+
: state.apply(state.tr.setSelection(TextSelection.create(state.doc, pos)));
|
|
42
53
|
}
|
|
43
54
|
|
|
44
55
|
function caret(state: EditorState): ResolvedPos | null {
|
|
@@ -56,6 +67,14 @@ export function insertText(text: string): Command {
|
|
|
56
67
|
return (state, dispatch) => {
|
|
57
68
|
if (!text) return false;
|
|
58
69
|
const { selection } = state;
|
|
70
|
+
if (selectedBlock(state)) {
|
|
71
|
+
// Typing over a selected block (a rule) replaces it with a paragraph of the text.
|
|
72
|
+
return replaceSelectedBlock(
|
|
73
|
+
state,
|
|
74
|
+
dispatch,
|
|
75
|
+
state.schema.text(text, state.storedMarks ?? []),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
59
78
|
if (!(selection instanceof AllSelection) && !selection.$from.parent.inlineContent) return false;
|
|
60
79
|
return attempt(state, dispatch, (tr) => {
|
|
61
80
|
const $from = selection.$from;
|
|
@@ -64,16 +83,28 @@ export function insertText(text: string): Command {
|
|
|
64
83
|
(selection.empty ? marksAt($from) : ($from.nodeAfter?.marks ?? marksAt($from)));
|
|
65
84
|
tr.deleteSelection();
|
|
66
85
|
const $pos = tr.selection.$from;
|
|
67
|
-
const allowed = marks.filter((mark) =>
|
|
86
|
+
const allowed = typedMarks(marks).filter((mark) =>
|
|
87
|
+
$pos.parent.type.allowsMarkType(mark.type),
|
|
88
|
+
);
|
|
68
89
|
tr.insertText(text, $pos.pos, $pos.pos, allowed);
|
|
69
90
|
tr.setSelection(TextSelection.create(tr.doc, $pos.pos + text.length));
|
|
70
91
|
});
|
|
71
92
|
};
|
|
72
93
|
}
|
|
73
94
|
|
|
74
|
-
/**
|
|
75
|
-
|
|
76
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Length (in positions) of the last grapheme or word before `$pos` in its textblock. With
|
|
97
|
+
* `textOnly` (schema v3) a word stops at the inline node before it (a tab, a break, a field, a
|
|
98
|
+
* marker): those go one per key, as Word deletes them; the node itself counts as one position.
|
|
99
|
+
*/
|
|
100
|
+
function backwardExtent($pos: ResolvedPos, unit: DeleteUnit, textOnly = false): number {
|
|
101
|
+
let from = 0;
|
|
102
|
+
if (textOnly && unit === "word")
|
|
103
|
+
$pos.parent.forEach((child, offset) => {
|
|
104
|
+
if (!child.isText && offset < $pos.parentOffset) from = offset + child.nodeSize;
|
|
105
|
+
});
|
|
106
|
+
if (from === $pos.parentOffset && from > 0) return 1;
|
|
107
|
+
const text = $pos.parent.textBetween(from, $pos.parentOffset, "", LEAF);
|
|
77
108
|
if (unit === "char") return [...graphemes.segment(text)].pop()?.segment.length ?? 0;
|
|
78
109
|
const segments = [...words.segment(text)];
|
|
79
110
|
let length = 0;
|
|
@@ -82,9 +113,15 @@ function backwardExtent($pos: ResolvedPos, unit: DeleteUnit): number {
|
|
|
82
113
|
return length + (segments.pop()?.segment.length ?? 0);
|
|
83
114
|
}
|
|
84
115
|
|
|
85
|
-
/** Length of the first grapheme or word after `$pos` in its textblock. */
|
|
86
|
-
function forwardExtent($pos: ResolvedPos, unit: DeleteUnit): number {
|
|
87
|
-
|
|
116
|
+
/** Length of the first grapheme or word after `$pos` in its textblock (`textOnly`: as backward). */
|
|
117
|
+
function forwardExtent($pos: ResolvedPos, unit: DeleteUnit, textOnly = false): number {
|
|
118
|
+
let to = $pos.parent.content.size;
|
|
119
|
+
if (textOnly && unit === "word")
|
|
120
|
+
$pos.parent.forEach((child, offset) => {
|
|
121
|
+
if (!child.isText && offset >= $pos.parentOffset && offset < to) to = offset;
|
|
122
|
+
});
|
|
123
|
+
if (to === $pos.parentOffset && to < $pos.parent.content.size) return 1;
|
|
124
|
+
const text = $pos.parent.textBetween($pos.parentOffset, to, "", LEAF);
|
|
88
125
|
const segments = [...(unit === "char" ? graphemes : words).segment(text)];
|
|
89
126
|
if (unit === "char") return segments[0]?.segment.length ?? 0;
|
|
90
127
|
let length = 0;
|
|
@@ -97,10 +134,20 @@ function forwardExtent($pos: ResolvedPos, unit: DeleteUnit): number {
|
|
|
97
134
|
export function deleteBackward(unit: DeleteUnit = "char"): Command {
|
|
98
135
|
return (state, dispatch) => {
|
|
99
136
|
if (!state.selection.empty) return deleteSelection(state, dispatch);
|
|
100
|
-
const
|
|
101
|
-
if (
|
|
102
|
-
|
|
103
|
-
const
|
|
137
|
+
const found = caret(state);
|
|
138
|
+
if (!found) return false;
|
|
139
|
+
// Invisible markers (a bookmark, a comment's range) stay: the character before them goes.
|
|
140
|
+
const word = wordContext(state);
|
|
141
|
+
let $pos = found;
|
|
142
|
+
while ($pos.parentOffset > 0 && isMarker(word, $pos.nodeBefore))
|
|
143
|
+
$pos = state.doc.resolve($pos.pos - 1);
|
|
144
|
+
if ($pos.parentOffset === 0) return joinBackward(at(state, $pos.pos), dispatch);
|
|
145
|
+
if (isOpaque(word, $pos.nodeBefore)) {
|
|
146
|
+
const node = $pos.nodeBefore as Node;
|
|
147
|
+
dispatch?.(state.tr.setSelection(selectionAt(state, $pos.pos - node.nodeSize)));
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
const size = backwardExtent($pos, unit, word !== null);
|
|
104
151
|
return attempt(state, dispatch, (tr) => tr.delete($pos.pos - size, $pos.pos));
|
|
105
152
|
};
|
|
106
153
|
}
|
|
@@ -109,17 +156,26 @@ export function deleteBackward(unit: DeleteUnit = "char"): Command {
|
|
|
109
156
|
export function deleteForward(unit: DeleteUnit = "char"): Command {
|
|
110
157
|
return (state, dispatch) => {
|
|
111
158
|
if (!state.selection.empty) return deleteSelection(state, dispatch);
|
|
112
|
-
const
|
|
113
|
-
if (
|
|
114
|
-
|
|
115
|
-
|
|
159
|
+
const found = caret(state);
|
|
160
|
+
if (!found) return false;
|
|
161
|
+
const word = wordContext(state);
|
|
162
|
+
let $pos = found;
|
|
163
|
+
while ($pos.parentOffset < $pos.parent.content.size && isMarker(word, $pos.nodeAfter))
|
|
164
|
+
$pos = state.doc.resolve($pos.pos + 1);
|
|
165
|
+
if ($pos.parentOffset === $pos.parent.content.size)
|
|
166
|
+
return joinForward(at(state, $pos.pos), dispatch);
|
|
167
|
+
if (isOpaque(word, $pos.nodeAfter)) {
|
|
168
|
+
dispatch?.(state.tr.setSelection(selectionAt(state, $pos.pos)));
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
const size = forwardExtent($pos, unit, word !== null);
|
|
116
172
|
return attempt(state, dispatch, (tr) => tr.delete($pos.pos, $pos.pos + size));
|
|
117
173
|
};
|
|
118
174
|
}
|
|
119
175
|
|
|
120
176
|
/**
|
|
121
|
-
* At the start of a textblock: merges it into the previous textblock, or
|
|
122
|
-
* block. Does nothing at the start of the first block.
|
|
177
|
+
* At the start of a textblock: merges it into the previous textblock, or selects a previous
|
|
178
|
+
* opaque block (a table), as Word does. Does nothing at the start of the first block.
|
|
123
179
|
*/
|
|
124
180
|
export const joinBackward: Command = (state, dispatch) => {
|
|
125
181
|
const $pos = caret(state);
|
|
@@ -128,24 +184,40 @@ export const joinBackward: Command = (state, dispatch) => {
|
|
|
128
184
|
if (index === 0) return false;
|
|
129
185
|
const before = $pos.node($pos.depth - 1).child(index - 1);
|
|
130
186
|
const boundary = $pos.before();
|
|
131
|
-
if (before.isTextblock)
|
|
132
|
-
|
|
133
|
-
|
|
187
|
+
if (before.isTextblock)
|
|
188
|
+
return attempt(state, dispatch, (tr) => {
|
|
189
|
+
tr.join(boundary);
|
|
190
|
+
// The merged paragraph ends with this one's mark (schema v3: its section break).
|
|
191
|
+
keepLastMark(tr, boundary - before.nodeSize, $pos.parent);
|
|
192
|
+
});
|
|
193
|
+
if (before.isLeaf && isOpaque(wordContext(state), before)) {
|
|
194
|
+
dispatch?.(state.tr.setSelection(selectionAt(state, boundary - before.nodeSize)));
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
134
197
|
return false;
|
|
135
198
|
};
|
|
136
199
|
|
|
137
|
-
/**
|
|
200
|
+
/**
|
|
201
|
+
* At the end of a textblock: merges the next textblock into it, or selects a next opaque block.
|
|
202
|
+
*/
|
|
138
203
|
export const joinForward: Command = (state, dispatch) => {
|
|
139
204
|
const $pos = caret(state);
|
|
140
205
|
if (!$pos?.parent.isTextblock || $pos.parentOffset < $pos.parent.content.size || $pos.depth < 1) {
|
|
141
206
|
return false;
|
|
142
207
|
}
|
|
143
|
-
const
|
|
144
|
-
|
|
208
|
+
const after = $pos.node($pos.depth - 1).maybeChild($pos.index($pos.depth - 1) + 1);
|
|
209
|
+
if (!after) return false;
|
|
145
210
|
const boundary = $pos.after();
|
|
146
|
-
if (after
|
|
147
|
-
|
|
148
|
-
|
|
211
|
+
if (after.isTextblock) {
|
|
212
|
+
return attempt(state, dispatch, (tr) => {
|
|
213
|
+
tr.join(boundary);
|
|
214
|
+
keepLastMark(tr, $pos.before(), after);
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
if (after.isLeaf && isOpaque(wordContext(state), after)) {
|
|
218
|
+
dispatch?.(state.tr.setSelection(selectionAt(state, boundary)));
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
149
221
|
return false;
|
|
150
222
|
};
|
|
151
223
|
|
|
@@ -155,19 +227,121 @@ export const joinForward: Command = (state, dispatch) => {
|
|
|
155
227
|
*/
|
|
156
228
|
export const splitBlock: Command = (state, dispatch) => {
|
|
157
229
|
const { selection } = state;
|
|
230
|
+
const block = selectedBlock(state);
|
|
231
|
+
if (block) {
|
|
232
|
+
// Enter on a selected block (a rule): a new paragraph after it.
|
|
233
|
+
return attempt(state, dispatch, (tr) => {
|
|
234
|
+
tr.insert(selection.to, defaultTextblockType(state.schema).create());
|
|
235
|
+
tr.setSelection(TextSelection.create(tr.doc, selection.to + 1));
|
|
236
|
+
});
|
|
237
|
+
}
|
|
158
238
|
if (!(selection instanceof AllSelection) && !selection.$from.parent.isTextblock) return false;
|
|
159
239
|
return attempt(state, dispatch, (tr) => {
|
|
160
240
|
const marks = state.storedMarks ?? marksAt(selection.$from);
|
|
161
241
|
tr.deleteSelection();
|
|
162
242
|
const $pos = tr.selection.$from;
|
|
163
243
|
const atEnd = $pos.parentOffset === $pos.parent.content.size;
|
|
164
|
-
const
|
|
165
|
-
|
|
244
|
+
const word = wordContext(state);
|
|
245
|
+
if (word && $pos.parent.type.name === "paragraph") {
|
|
246
|
+
// Word: the first paragraph gets a new mark, the second keeps the original one.
|
|
247
|
+
const { before, after } = splitAttrs(word, $pos.parent, atEnd);
|
|
248
|
+
tr.split($pos.pos, 1, [{ type: $pos.parent.type, attrs: after }]);
|
|
249
|
+
if (before) tr.setNodeMarkup($pos.before(), $pos.parent.type, before);
|
|
250
|
+
} else {
|
|
251
|
+
const normal = defaultTextblockType(state.schema);
|
|
252
|
+
tr.split($pos.pos, 1, atEnd && $pos.parent.type !== normal ? [{ type: normal }] : undefined);
|
|
253
|
+
}
|
|
166
254
|
tr.setSelection(TextSelection.create(tr.doc, $pos.pos + 2));
|
|
167
255
|
if (marks.length) tr.setStoredMarks(marks);
|
|
168
256
|
});
|
|
169
257
|
};
|
|
170
258
|
|
|
259
|
+
/** The selected block node (a rule), when the selection is a node selection of a block. */
|
|
260
|
+
function selectedBlock(state: EditorState): Node | null {
|
|
261
|
+
const { selection } = state;
|
|
262
|
+
return selection instanceof NodeSelection && selection.node.isBlock ? selection.node : null;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Replaces the selected block with a default textblock holding `content` (caret at its end). */
|
|
266
|
+
function replaceSelectedBlock(
|
|
267
|
+
state: EditorState,
|
|
268
|
+
dispatch: ((tr: Transaction) => void) | undefined,
|
|
269
|
+
content: Node,
|
|
270
|
+
): boolean {
|
|
271
|
+
const { from, to } = state.selection;
|
|
272
|
+
const block = defaultTextblockType(state.schema).create(null, content);
|
|
273
|
+
return attempt(state, dispatch, (tr) => {
|
|
274
|
+
tr.replace(from, to, new Slice(Fragment.from(block), 0, 0));
|
|
275
|
+
tr.setSelection(TextSelection.create(tr.doc, from + 1 + content.nodeSize));
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The leaf block or textblock next to `pos` (a position between blocks) in direction `dir`,
|
|
281
|
+
* through container boundaries (the rule ending the list item before), or null at the end.
|
|
282
|
+
*/
|
|
283
|
+
function blockBeside(doc: Node, pos: number, dir: -1 | 1): { node: Node; pos: number } | null {
|
|
284
|
+
let $pos = doc.resolve(pos);
|
|
285
|
+
for (;;) {
|
|
286
|
+
let node = dir < 0 ? $pos.nodeBefore : $pos.nodeAfter;
|
|
287
|
+
if (node) {
|
|
288
|
+
let start = dir < 0 ? $pos.pos - node.nodeSize : $pos.pos;
|
|
289
|
+
while (!node.isTextblock && !node.isLeaf) {
|
|
290
|
+
const child: Node | null = dir < 0 ? node.lastChild : node.firstChild;
|
|
291
|
+
if (!child) return null;
|
|
292
|
+
start = dir < 0 ? start + node.nodeSize - 1 - child.nodeSize : start + 1;
|
|
293
|
+
node = child;
|
|
294
|
+
}
|
|
295
|
+
return { node, pos: start };
|
|
296
|
+
}
|
|
297
|
+
if ($pos.depth === 0) return null;
|
|
298
|
+
$pos = doc.resolve(dir < 0 ? $pos.before() : $pos.after());
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Arrow keys around a block leaf (a rule, which has no caret stop). From a selected leaf: to the
|
|
304
|
+
* text beside it (the end of the text before, the start of the text after), or the next leaf.
|
|
305
|
+
* From a caret: selects the leaf beside the caret's textblock, when the caret is at its edge
|
|
306
|
+
* (`"char"`: Left/Right at the textblock's first or last position) or the view found it on the
|
|
307
|
+
* first or last line (`"line"`: Up/Down). `"none"` only moves from a selected leaf. `dir` -1 is
|
|
308
|
+
* back (left, up), 1 forward.
|
|
309
|
+
*/
|
|
310
|
+
export function arrowAcrossLeaf(dir: -1 | 1, from: "char" | "line" | "none"): Command {
|
|
311
|
+
return (state, dispatch) => {
|
|
312
|
+
const { selection, doc } = state;
|
|
313
|
+
if (selectedBlock(state)) {
|
|
314
|
+
const beside = blockBeside(doc, dir < 0 ? selection.from : selection.to, dir);
|
|
315
|
+
// At the document's edge: stay (a native arrow would leave a broken DOM range).
|
|
316
|
+
if (!beside) return true;
|
|
317
|
+
const next = beside.node.isTextblock
|
|
318
|
+
? TextSelection.create(
|
|
319
|
+
doc,
|
|
320
|
+
dir < 0 ? beside.pos + beside.node.nodeSize - 1 : beside.pos + 1,
|
|
321
|
+
)
|
|
322
|
+
: NodeSelection.isSelectable(beside.node)
|
|
323
|
+
? NodeSelection.create(doc, beside.pos)
|
|
324
|
+
: Selection.near(doc.resolve(beside.pos), dir);
|
|
325
|
+
dispatch?.(state.tr.setSelection(next));
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
if (from === "none" || !selection.empty || !(selection instanceof TextSelection)) return false;
|
|
329
|
+
const { $from } = selection;
|
|
330
|
+
if (!$from.parent.isTextblock || $from.depth < 1) return false;
|
|
331
|
+
const atEdge =
|
|
332
|
+
dir < 0 ? $from.parentOffset === 0 : $from.parentOffset === $from.parent.content.size;
|
|
333
|
+
if (from === "char" && !atEdge) return false;
|
|
334
|
+
const beside = blockBeside(doc, dir < 0 ? $from.before() : $from.after(), dir);
|
|
335
|
+
if (!beside || beside.node.isTextblock || !NodeSelection.isSelectable(beside.node))
|
|
336
|
+
return false;
|
|
337
|
+
dispatch?.(state.tr.setSelection(NodeSelection.create(doc, beside.pos)));
|
|
338
|
+
return true;
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** Inserts a tab (schema v3) in place of the selection; does not apply where there is none. */
|
|
343
|
+
export const insertTab: Command = (state, dispatch) => insertLeaf("tab", state, dispatch);
|
|
344
|
+
|
|
171
345
|
/** Inserts a hard break (a line break inside the block) in place of the selection. */
|
|
172
346
|
export const insertHardBreak: Command = (state, dispatch) =>
|
|
173
347
|
insertLeaf("hardBreak", state, dispatch);
|
|
@@ -192,7 +366,7 @@ function insertLeaf(name: string, ...[state, dispatch]: Parameters<Command>): bo
|
|
|
192
366
|
type.create(
|
|
193
367
|
null,
|
|
194
368
|
null,
|
|
195
|
-
marks.filter((m) => tr.doc.resolve(pos).parent.type.allowsMarkType(m.type)),
|
|
369
|
+
typedMarks(marks).filter((m) => tr.doc.resolve(pos).parent.type.allowsMarkType(m.type)),
|
|
196
370
|
),
|
|
197
371
|
);
|
|
198
372
|
tr.setSelection(TextSelection.create(tr.doc, pos + 1));
|
|
@@ -234,10 +408,17 @@ export function toggleMark(type: string | MarkType, attrs?: Attrs | null): Comma
|
|
|
234
408
|
if ($caret) {
|
|
235
409
|
if (!$caret.parent.type.allowsMarkType(mark)) return false;
|
|
236
410
|
const marks = state.storedMarks ?? marksAt($caret);
|
|
411
|
+
const word = wordContext(state);
|
|
412
|
+
if (word && isWordToggle(word, mark)) {
|
|
413
|
+
dispatch?.(state.tr.setStoredMarks(toggledCaretMarks(word, $caret.parent, marks, mark)));
|
|
414
|
+
return true;
|
|
415
|
+
}
|
|
237
416
|
const existing = mark.isInSet(marks);
|
|
417
|
+
const created = existing ? null : createMark(mark, attrs);
|
|
418
|
+
if (!existing && !created) return false;
|
|
238
419
|
dispatch?.(
|
|
239
420
|
state.tr.setStoredMarks(
|
|
240
|
-
existing ? existing.removeFromSet(marks) :
|
|
421
|
+
existing ? existing.removeFromSet(marks) : (created as Mark).addToSet(marks),
|
|
241
422
|
),
|
|
242
423
|
);
|
|
243
424
|
return true;
|
|
@@ -248,39 +429,68 @@ export function toggleMark(type: string | MarkType, attrs?: Attrs | null): Comma
|
|
|
248
429
|
return !applicable;
|
|
249
430
|
});
|
|
250
431
|
if (!applicable) return false;
|
|
432
|
+
const word = wordContext(state);
|
|
433
|
+
if (word && isWordToggle(word, mark)) {
|
|
434
|
+
const on = !wordToggleActive(word, state, mark, null);
|
|
435
|
+
return attempt(state, dispatch, (tr) => {
|
|
436
|
+
for (const range of toggleRanges(word, state, selection.from, selection.to, mark, on)) {
|
|
437
|
+
tr.removeMark(range.from, range.to, mark);
|
|
438
|
+
if (range.mark) tr.addMark(range.from, range.to, range.mark);
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
const remove = rangeHasMark(state, selection.from, selection.to, mark);
|
|
443
|
+
const created = remove ? null : createMark(mark, attrs);
|
|
444
|
+
if (!remove && !created) return false;
|
|
251
445
|
return attempt(state, dispatch, (tr) => {
|
|
252
|
-
if (
|
|
253
|
-
|
|
254
|
-
} else {
|
|
255
|
-
tr.addMark(selection.from, selection.to, mark.create(attrs));
|
|
256
|
-
}
|
|
446
|
+
if (remove) tr.removeMark(selection.from, selection.to, mark);
|
|
447
|
+
else tr.addMark(selection.from, selection.to, created as Mark);
|
|
257
448
|
});
|
|
258
449
|
};
|
|
259
450
|
}
|
|
260
451
|
|
|
452
|
+
/** The mark, or null when `attrs` are invalid for it (e.g. a link with an unsafe href). */
|
|
453
|
+
function createMark(type: MarkType, attrs?: Attrs | null): Mark | null {
|
|
454
|
+
// Schema v3 keeps any link target a file has (`file:`…); a command adds only safe ones.
|
|
455
|
+
if (type.name === "link" && !isSafeLink(attrs)) return null;
|
|
456
|
+
try {
|
|
457
|
+
return type.create(attrs);
|
|
458
|
+
} catch (error) {
|
|
459
|
+
if (error instanceof FoliDocumentError) return null;
|
|
460
|
+
throw error;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
261
464
|
/** Whether the mark is active: in stored marks at a caret, or on all selected text. */
|
|
262
465
|
export function isMarkActive(state: EditorState, type: string | MarkType): boolean {
|
|
263
466
|
const mark = markType(state, type);
|
|
264
467
|
if (!mark) return false;
|
|
265
468
|
const $caret = caret(state);
|
|
469
|
+
const word = wordContext(state);
|
|
470
|
+
if (word && isWordToggle(word, mark))
|
|
471
|
+
return wordToggleActive(
|
|
472
|
+
word,
|
|
473
|
+
state,
|
|
474
|
+
mark,
|
|
475
|
+
$caret ? (state.storedMarks ?? marksAt($caret)) : null,
|
|
476
|
+
);
|
|
266
477
|
if ($caret) return mark.isInSet(state.storedMarks ?? marksAt($caret)) !== undefined;
|
|
267
478
|
return rangeHasMark(state, state.selection.from, state.selection.to, mark);
|
|
268
479
|
}
|
|
269
480
|
|
|
270
|
-
/**
|
|
481
|
+
/**
|
|
482
|
+
* Gives the selected paragraphs a block type as Word does: `heading` (with its `level`) is
|
|
483
|
+
* Word's heading style of that level, `paragraph` the document's default paragraph style.
|
|
484
|
+
*/
|
|
271
485
|
export function setBlockType(type: string | NodeType, attrs: Attrs | null = null): Command {
|
|
272
486
|
return (state, dispatch) => {
|
|
273
|
-
const
|
|
274
|
-
if (!
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
return !node.isTextblock;
|
|
281
|
-
});
|
|
282
|
-
if (!changes) return false;
|
|
283
|
-
return attempt(state, dispatch, (tr) => tr.setBlockType(from, to, nodeType, attrs));
|
|
487
|
+
const word = wordContext(state);
|
|
488
|
+
if (!word) return false;
|
|
489
|
+
const name = typeof type === "string" ? type : type.name;
|
|
490
|
+
if (name === "heading")
|
|
491
|
+
return setStyleNamed(`heading ${String(attrs?.level ?? 1)}`)(state, dispatch);
|
|
492
|
+
const style = name === "paragraph" ? defaultParagraphStyleOf(word) : null;
|
|
493
|
+
return style !== null && setParagraphStyle(style)(state, dispatch);
|
|
284
494
|
};
|
|
285
495
|
}
|
|
286
496
|
|
|
@@ -295,12 +505,20 @@ export function baseKeymap(): Plugin {
|
|
|
295
505
|
Enter: splitBlock,
|
|
296
506
|
"Shift-Enter": insertHardBreak,
|
|
297
507
|
"Mod-Enter": insertPageBreak,
|
|
508
|
+
// Types a tab (a list's Tab runs first); Escape then Tab leaves the editor (the view).
|
|
509
|
+
Tab: insertTab,
|
|
298
510
|
Backspace: deleteBackward(),
|
|
299
511
|
Delete: deleteForward(),
|
|
300
512
|
"Mod-b": toggleMark("bold"),
|
|
301
513
|
"Mod-i": toggleMark("italic"),
|
|
302
514
|
"Mod-u": toggleMark("underline"),
|
|
515
|
+
"Mod-k": openLinkEditor,
|
|
303
516
|
"Mod-a": selectAll,
|
|
517
|
+
// Up/Down from a caret need the view's line geometry: it runs arrowAcrossLeaf(dir, "line").
|
|
518
|
+
ArrowLeft: arrowAcrossLeaf(-1, "char"),
|
|
519
|
+
ArrowRight: arrowAcrossLeaf(1, "char"),
|
|
520
|
+
ArrowUp: arrowAcrossLeaf(-1, "none"),
|
|
521
|
+
ArrowDown: arrowAcrossLeaf(1, "none"),
|
|
304
522
|
"Mod-z": undo,
|
|
305
523
|
"Mod-Shift-z": redo,
|
|
306
524
|
"Mod-y": redo,
|