@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/model/fragment.ts
CHANGED
|
@@ -97,8 +97,16 @@ export class Fragment {
|
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
/**
|
|
101
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Text between two positions; `blockSeparator` goes between textblocks, `leafText` for leaves
|
|
102
|
+
* (a string, or one per leaf).
|
|
103
|
+
*/
|
|
104
|
+
textBetween(
|
|
105
|
+
from: number,
|
|
106
|
+
to: number,
|
|
107
|
+
blockSeparator = "",
|
|
108
|
+
leafText: string | ((leaf: Node) => string) = "",
|
|
109
|
+
): string {
|
|
102
110
|
let text = "";
|
|
103
111
|
let first = true;
|
|
104
112
|
this.nodesBetween(from, to, (node, pos) => {
|
|
@@ -108,7 +116,7 @@ export class Fragment {
|
|
|
108
116
|
} else if (node.isText) {
|
|
109
117
|
text += node.textContent.slice(Math.max(from, pos) - pos, to - pos);
|
|
110
118
|
} else if (node.isLeaf) {
|
|
111
|
-
text += leafText;
|
|
119
|
+
text += typeof leafText === "string" ? leafText : leafText(node);
|
|
112
120
|
}
|
|
113
121
|
return undefined;
|
|
114
122
|
});
|
package/src/model/node.ts
CHANGED
|
@@ -115,7 +115,12 @@ export class Node {
|
|
|
115
115
|
this.nodesBetween(0, this.content.size, f);
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
textBetween(
|
|
118
|
+
textBetween(
|
|
119
|
+
from: number,
|
|
120
|
+
to: number,
|
|
121
|
+
blockSeparator?: string,
|
|
122
|
+
leafText?: string | ((leaf: Node) => string),
|
|
123
|
+
): string {
|
|
119
124
|
return this.content.textBetween(from, to, blockSeparator, leafText);
|
|
120
125
|
}
|
|
121
126
|
|
package/src/schema.ts
CHANGED
|
@@ -41,6 +41,18 @@ export function isSafeHref(href: unknown): href is string {
|
|
|
41
41
|
return (SAFE_LINK_PROTOCOLS as readonly string[]).includes(`${scheme[1]?.toLowerCase()}:`);
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Whether a link's attributes may come in from outside the document (a command, a paste from
|
|
46
|
+
* elsewhere): a safe `href`, or none with a bookmark (`anchor`, schema v3: Word's table of
|
|
47
|
+
* contents and cross-references).
|
|
48
|
+
*/
|
|
49
|
+
export function isSafeLink(
|
|
50
|
+
attrs: { href?: unknown; anchor?: unknown } | null | undefined,
|
|
51
|
+
): boolean {
|
|
52
|
+
if (attrs?.href === null || attrs?.href === undefined) return typeof attrs?.anchor === "string";
|
|
53
|
+
return isSafeHref(attrs.href);
|
|
54
|
+
}
|
|
55
|
+
|
|
44
56
|
/** Whether `color` is a canonical color value: `#rrggbb`, lowercase. */
|
|
45
57
|
export function isColor(color: unknown): color is string {
|
|
46
58
|
return typeof color === "string" && /^#[0-9a-f]{6}$/.test(color);
|
|
@@ -59,7 +71,7 @@ const color = {
|
|
|
59
71
|
isColor(value) ? undefined : "color must be a lowercase #rrggbb value",
|
|
60
72
|
};
|
|
61
73
|
|
|
62
|
-
export const
|
|
74
|
+
export const schemaV2 = new Schema({
|
|
63
75
|
nodes: {
|
|
64
76
|
doc: { content: "block+" },
|
|
65
77
|
paragraph: { group: "block", content: "inline*", attrs: { align } },
|
package/src/state/commands.ts
CHANGED
|
@@ -9,9 +9,11 @@ import {
|
|
|
9
9
|
type ResolvedPos,
|
|
10
10
|
Slice,
|
|
11
11
|
} from "../model/index.js";
|
|
12
|
+
import { isSafeLink } from "../schema.js";
|
|
12
13
|
import { attempt } from "./attempt.js";
|
|
13
14
|
import { redo, undo } from "./history.js";
|
|
14
15
|
import { openLinkEditor } from "./links.js";
|
|
16
|
+
import { keepLastMark, typedMarks } from "./mark.js";
|
|
15
17
|
import { AllSelection, marksAt, NodeSelection, Selection, TextSelection } from "./selection.js";
|
|
16
18
|
import {
|
|
17
19
|
type Command,
|
|
@@ -20,6 +22,19 @@ import {
|
|
|
20
22
|
Plugin,
|
|
21
23
|
type Transaction,
|
|
22
24
|
} from "./state.js";
|
|
25
|
+
import { trackState } from "./track.js";
|
|
26
|
+
import {
|
|
27
|
+
isMarker,
|
|
28
|
+
isOpaque,
|
|
29
|
+
isWordToggle,
|
|
30
|
+
selectionAt,
|
|
31
|
+
splitAttrs,
|
|
32
|
+
toggledCaretMarks,
|
|
33
|
+
toggleRanges,
|
|
34
|
+
wordContext,
|
|
35
|
+
wordToggleActive,
|
|
36
|
+
} from "./word.js";
|
|
37
|
+
import { defaultParagraphStyleOf, setParagraphStyle, setStyleNamed } from "./wordformat.js";
|
|
23
38
|
|
|
24
39
|
export type DeleteUnit = "char" | "word";
|
|
25
40
|
|
|
@@ -31,6 +46,13 @@ const LEAF = "\n";
|
|
|
31
46
|
const graphemes = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
32
47
|
const words = new Intl.Segmenter(undefined, { granularity: "word" });
|
|
33
48
|
|
|
49
|
+
/** `state` with a caret at `pos` (a command then runs as if the caret were there). */
|
|
50
|
+
function at(state: EditorState, pos: number): EditorState {
|
|
51
|
+
return pos === state.selection.head
|
|
52
|
+
? state
|
|
53
|
+
: state.apply(state.tr.setSelection(TextSelection.create(state.doc, pos)));
|
|
54
|
+
}
|
|
55
|
+
|
|
34
56
|
function caret(state: EditorState): ResolvedPos | null {
|
|
35
57
|
const { selection } = state;
|
|
36
58
|
return selection.empty && selection instanceof TextSelection ? selection.$head : null;
|
|
@@ -62,16 +84,28 @@ export function insertText(text: string): Command {
|
|
|
62
84
|
(selection.empty ? marksAt($from) : ($from.nodeAfter?.marks ?? marksAt($from)));
|
|
63
85
|
tr.deleteSelection();
|
|
64
86
|
const $pos = tr.selection.$from;
|
|
65
|
-
const allowed = marks.filter((mark) =>
|
|
87
|
+
const allowed = typedMarks(marks).filter((mark) =>
|
|
88
|
+
$pos.parent.type.allowsMarkType(mark.type),
|
|
89
|
+
);
|
|
66
90
|
tr.insertText(text, $pos.pos, $pos.pos, allowed);
|
|
67
91
|
tr.setSelection(TextSelection.create(tr.doc, $pos.pos + text.length));
|
|
68
92
|
});
|
|
69
93
|
};
|
|
70
94
|
}
|
|
71
95
|
|
|
72
|
-
/**
|
|
73
|
-
|
|
74
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Length (in positions) of the last grapheme or word before `$pos` in its textblock. With
|
|
98
|
+
* `textOnly` (schema v3) a word stops at the inline node before it (a tab, a break, a field, a
|
|
99
|
+
* marker): those go one per key, as Word deletes them; the node itself counts as one position.
|
|
100
|
+
*/
|
|
101
|
+
function backwardExtent($pos: ResolvedPos, unit: DeleteUnit, textOnly = false): number {
|
|
102
|
+
let from = 0;
|
|
103
|
+
if (textOnly && unit === "word")
|
|
104
|
+
$pos.parent.forEach((child, offset) => {
|
|
105
|
+
if (!child.isText && offset < $pos.parentOffset) from = offset + child.nodeSize;
|
|
106
|
+
});
|
|
107
|
+
if (from === $pos.parentOffset && from > 0) return 1;
|
|
108
|
+
const text = $pos.parent.textBetween(from, $pos.parentOffset, "", LEAF);
|
|
75
109
|
if (unit === "char") return [...graphemes.segment(text)].pop()?.segment.length ?? 0;
|
|
76
110
|
const segments = [...words.segment(text)];
|
|
77
111
|
let length = 0;
|
|
@@ -80,9 +114,15 @@ function backwardExtent($pos: ResolvedPos, unit: DeleteUnit): number {
|
|
|
80
114
|
return length + (segments.pop()?.segment.length ?? 0);
|
|
81
115
|
}
|
|
82
116
|
|
|
83
|
-
/** Length of the first grapheme or word after `$pos` in its textblock. */
|
|
84
|
-
function forwardExtent($pos: ResolvedPos, unit: DeleteUnit): number {
|
|
85
|
-
|
|
117
|
+
/** Length of the first grapheme or word after `$pos` in its textblock (`textOnly`: as backward). */
|
|
118
|
+
function forwardExtent($pos: ResolvedPos, unit: DeleteUnit, textOnly = false): number {
|
|
119
|
+
let to = $pos.parent.content.size;
|
|
120
|
+
if (textOnly && unit === "word")
|
|
121
|
+
$pos.parent.forEach((child, offset) => {
|
|
122
|
+
if (!child.isText && offset >= $pos.parentOffset && offset < to) to = offset;
|
|
123
|
+
});
|
|
124
|
+
if (to === $pos.parentOffset && to < $pos.parent.content.size) return 1;
|
|
125
|
+
const text = $pos.parent.textBetween($pos.parentOffset, to, "", LEAF);
|
|
86
126
|
const segments = [...(unit === "char" ? graphemes : words).segment(text)];
|
|
87
127
|
if (unit === "char") return segments[0]?.segment.length ?? 0;
|
|
88
128
|
let length = 0;
|
|
@@ -91,14 +131,43 @@ function forwardExtent($pos: ResolvedPos, unit: DeleteUnit): number {
|
|
|
91
131
|
return length + (segments.shift()?.segment.length ?? 0);
|
|
92
132
|
}
|
|
93
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Whether Backspace and Delete step over deleted text: while tracking, and wherever the review
|
|
136
|
+
* view hides it.
|
|
137
|
+
*/
|
|
138
|
+
function skipsDeleted(state: EditorState): boolean {
|
|
139
|
+
const track = trackState(state);
|
|
140
|
+
return track?.on === true || (track?.view ?? "markup") !== "markup";
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Content a tracked change deleted (still in the document until accepted). */
|
|
144
|
+
const isDeleted = (node: Node) => node.marks.some((m) => m.type.name === "deletion");
|
|
145
|
+
|
|
94
146
|
/** Backspace: deletes the selection, else the previous character or word, else joins with the previous block. */
|
|
95
147
|
export function deleteBackward(unit: DeleteUnit = "char"): Command {
|
|
96
148
|
return (state, dispatch) => {
|
|
97
149
|
if (!state.selection.empty) return deleteSelection(state, dispatch);
|
|
98
|
-
const
|
|
99
|
-
if (
|
|
100
|
-
|
|
101
|
-
|
|
150
|
+
const found = caret(state);
|
|
151
|
+
if (!found) return false;
|
|
152
|
+
// Invisible markers (a bookmark, a comment's range) stay: the character before them goes.
|
|
153
|
+
// While tracking, so does text already deleted: Backspace deletes what is before it.
|
|
154
|
+
const word = wordContext(state);
|
|
155
|
+
const tracking = skipsDeleted(state);
|
|
156
|
+
let $pos = found;
|
|
157
|
+
while ($pos.parentOffset > 0) {
|
|
158
|
+
const before = $pos.nodeBefore;
|
|
159
|
+
if (isMarker(word, before)) $pos = state.doc.resolve($pos.pos - 1);
|
|
160
|
+
else if (tracking && before && isDeleted(before))
|
|
161
|
+
$pos = state.doc.resolve($pos.pos - before.nodeSize);
|
|
162
|
+
else break;
|
|
163
|
+
}
|
|
164
|
+
if ($pos.parentOffset === 0) return joinBackward(at(state, $pos.pos), dispatch);
|
|
165
|
+
if (isOpaque(word, $pos.nodeBefore)) {
|
|
166
|
+
const node = $pos.nodeBefore as Node;
|
|
167
|
+
dispatch?.(state.tr.setSelection(selectionAt(state, $pos.pos - node.nodeSize)));
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
const size = backwardExtent($pos, unit, word !== null);
|
|
102
171
|
return attempt(state, dispatch, (tr) => tr.delete($pos.pos - size, $pos.pos));
|
|
103
172
|
};
|
|
104
173
|
}
|
|
@@ -107,17 +176,32 @@ export function deleteBackward(unit: DeleteUnit = "char"): Command {
|
|
|
107
176
|
export function deleteForward(unit: DeleteUnit = "char"): Command {
|
|
108
177
|
return (state, dispatch) => {
|
|
109
178
|
if (!state.selection.empty) return deleteSelection(state, dispatch);
|
|
110
|
-
const
|
|
111
|
-
if (
|
|
112
|
-
|
|
113
|
-
const
|
|
179
|
+
const found = caret(state);
|
|
180
|
+
if (!found) return false;
|
|
181
|
+
const word = wordContext(state);
|
|
182
|
+
const tracking = skipsDeleted(state);
|
|
183
|
+
let $pos = found;
|
|
184
|
+
while ($pos.parentOffset < $pos.parent.content.size) {
|
|
185
|
+
const after = $pos.nodeAfter;
|
|
186
|
+
if (isMarker(word, after)) $pos = state.doc.resolve($pos.pos + 1);
|
|
187
|
+
else if (tracking && after && isDeleted(after))
|
|
188
|
+
$pos = state.doc.resolve($pos.pos + after.nodeSize);
|
|
189
|
+
else break;
|
|
190
|
+
}
|
|
191
|
+
if ($pos.parentOffset === $pos.parent.content.size)
|
|
192
|
+
return joinForward(at(state, $pos.pos), dispatch);
|
|
193
|
+
if (isOpaque(word, $pos.nodeAfter)) {
|
|
194
|
+
dispatch?.(state.tr.setSelection(selectionAt(state, $pos.pos)));
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
const size = forwardExtent($pos, unit, word !== null);
|
|
114
198
|
return attempt(state, dispatch, (tr) => tr.delete($pos.pos, $pos.pos + size));
|
|
115
199
|
};
|
|
116
200
|
}
|
|
117
201
|
|
|
118
202
|
/**
|
|
119
|
-
* At the start of a textblock: merges it into the previous textblock, or
|
|
120
|
-
* block. Does nothing at the start of the first block.
|
|
203
|
+
* At the start of a textblock: merges it into the previous textblock, or selects a previous
|
|
204
|
+
* opaque block (a table), as Word does. Does nothing at the start of the first block.
|
|
121
205
|
*/
|
|
122
206
|
export const joinBackward: Command = (state, dispatch) => {
|
|
123
207
|
const $pos = caret(state);
|
|
@@ -126,83 +210,41 @@ export const joinBackward: Command = (state, dispatch) => {
|
|
|
126
210
|
if (index === 0) return false;
|
|
127
211
|
const before = $pos.node($pos.depth - 1).child(index - 1);
|
|
128
212
|
const boundary = $pos.before();
|
|
129
|
-
if (before.isTextblock)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
if (target?.isLeaf) {
|
|
140
|
-
// A container ending in a leaf block (a rule): delete the leaf.
|
|
141
|
-
const end = boundary - (levels - 1);
|
|
142
|
-
const leaf = target;
|
|
143
|
-
return attempt(state, dispatch, (tr) => tr.delete(end - leaf.nodeSize, end));
|
|
213
|
+
if (before.isTextblock)
|
|
214
|
+
return attempt(state, dispatch, (tr) => {
|
|
215
|
+
tr.join(boundary);
|
|
216
|
+
// The merged paragraph ends with this one's mark (schema v3: its section break).
|
|
217
|
+
keepLastMark(tr, boundary - before.nodeSize, $pos.parent);
|
|
218
|
+
});
|
|
219
|
+
if (before.isLeaf && isOpaque(wordContext(state), before)) {
|
|
220
|
+
dispatch?.(state.tr.setSelection(selectionAt(state, boundary - before.nodeSize)));
|
|
221
|
+
return true;
|
|
144
222
|
}
|
|
145
|
-
|
|
146
|
-
const parent = $pos.node($pos.depth - 1);
|
|
147
|
-
if (!parent.canReplace(index, index + 1)) return false;
|
|
148
|
-
const joinAt = boundary - levels;
|
|
149
|
-
const content = $pos.parent.content;
|
|
150
|
-
return attempt(state, dispatch, (tr) => {
|
|
151
|
-
tr.insert(joinAt, content);
|
|
152
|
-
const start = tr.mapping.map(boundary);
|
|
153
|
-
tr.delete(start, start + $pos.parent.nodeSize);
|
|
154
|
-
tr.setSelection(TextSelection.create(tr.doc, joinAt));
|
|
155
|
-
});
|
|
223
|
+
return false;
|
|
156
224
|
};
|
|
157
225
|
|
|
158
226
|
/**
|
|
159
|
-
* At the end of a textblock: merges the next textblock into it, or
|
|
160
|
-
* the end of a container's last block (a list item), what follows the container comes in.
|
|
227
|
+
* At the end of a textblock: merges the next textblock into it, or selects a next opaque block.
|
|
161
228
|
*/
|
|
162
229
|
export const joinForward: Command = (state, dispatch) => {
|
|
163
230
|
const $pos = caret(state);
|
|
164
231
|
if (!$pos?.parent.isTextblock || $pos.parentOffset < $pos.parent.content.size || $pos.depth < 1) {
|
|
165
232
|
return false;
|
|
166
233
|
}
|
|
167
|
-
|
|
168
|
-
let base = $pos.depth - 1;
|
|
169
|
-
while (base > 0 && $pos.index(base) === $pos.node(base).childCount - 1) base--;
|
|
170
|
-
const after = $pos.node(base).maybeChild($pos.index(base) + 1);
|
|
234
|
+
const after = $pos.node($pos.depth - 1).maybeChild($pos.index($pos.depth - 1) + 1);
|
|
171
235
|
if (!after) return false;
|
|
172
|
-
const boundary = $pos.after(
|
|
173
|
-
if (after.isTextblock
|
|
174
|
-
return attempt(state, dispatch, (tr) =>
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
// Before a container (a list): its first textblock joins this one; the containers it leaves
|
|
180
|
-
// empty go too (a list item, a list). Not when its container must keep it (a list item that
|
|
181
|
-
// goes on with a nested list): that needs a lift first.
|
|
182
|
-
let levels = 1;
|
|
183
|
-
let target: Node | null = after;
|
|
184
|
-
while (target && !target.isTextblock && !target.isLeaf) {
|
|
185
|
-
target = target.firstChild;
|
|
186
|
-
levels++;
|
|
236
|
+
const boundary = $pos.after();
|
|
237
|
+
if (after.isTextblock) {
|
|
238
|
+
return attempt(state, dispatch, (tr) => {
|
|
239
|
+
tr.join(boundary);
|
|
240
|
+
keepLastMark(tr, $pos.before(), after);
|
|
241
|
+
});
|
|
187
242
|
}
|
|
188
|
-
if (
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
while (depth > base + 1) {
|
|
192
|
-
const holder = $target.node(depth - 1);
|
|
193
|
-
const at = $target.index(depth - 1);
|
|
194
|
-
if (holder.canReplace(at, at + 1)) break;
|
|
195
|
-
if (holder.childCount !== 1) return false;
|
|
196
|
-
depth--;
|
|
243
|
+
if (after.isLeaf && isOpaque(wordContext(state), after)) {
|
|
244
|
+
dispatch?.(state.tr.setSelection(selectionAt(state, boundary)));
|
|
245
|
+
return true;
|
|
197
246
|
}
|
|
198
|
-
|
|
199
|
-
const to = $target.after(depth);
|
|
200
|
-
const content = target.content;
|
|
201
|
-
return attempt(state, dispatch, (tr) => {
|
|
202
|
-
tr.delete(from, to);
|
|
203
|
-
tr.insert($pos.pos, content);
|
|
204
|
-
tr.setSelection(TextSelection.create(tr.doc, $pos.pos));
|
|
205
|
-
});
|
|
247
|
+
return false;
|
|
206
248
|
};
|
|
207
249
|
|
|
208
250
|
/**
|
|
@@ -225,8 +267,16 @@ export const splitBlock: Command = (state, dispatch) => {
|
|
|
225
267
|
tr.deleteSelection();
|
|
226
268
|
const $pos = tr.selection.$from;
|
|
227
269
|
const atEnd = $pos.parentOffset === $pos.parent.content.size;
|
|
228
|
-
const
|
|
229
|
-
|
|
270
|
+
const word = wordContext(state);
|
|
271
|
+
if (word && $pos.parent.type.name === "paragraph") {
|
|
272
|
+
// Word: the first paragraph gets a new mark, the second keeps the original one.
|
|
273
|
+
const { before, after } = splitAttrs(word, $pos.parent, atEnd);
|
|
274
|
+
tr.split($pos.pos, 1, [{ type: $pos.parent.type, attrs: after }]);
|
|
275
|
+
if (before) tr.setNodeMarkup($pos.before(), $pos.parent.type, before);
|
|
276
|
+
} else {
|
|
277
|
+
const normal = defaultTextblockType(state.schema);
|
|
278
|
+
tr.split($pos.pos, 1, atEnd && $pos.parent.type !== normal ? [{ type: normal }] : undefined);
|
|
279
|
+
}
|
|
230
280
|
tr.setSelection(TextSelection.create(tr.doc, $pos.pos + 2));
|
|
231
281
|
if (marks.length) tr.setStoredMarks(marks);
|
|
232
282
|
});
|
|
@@ -315,6 +365,9 @@ export function arrowAcrossLeaf(dir: -1 | 1, from: "char" | "line" | "none"): Co
|
|
|
315
365
|
};
|
|
316
366
|
}
|
|
317
367
|
|
|
368
|
+
/** Inserts a tab (schema v3) in place of the selection; does not apply where there is none. */
|
|
369
|
+
export const insertTab: Command = (state, dispatch) => insertLeaf("tab", state, dispatch);
|
|
370
|
+
|
|
318
371
|
/** Inserts a hard break (a line break inside the block) in place of the selection. */
|
|
319
372
|
export const insertHardBreak: Command = (state, dispatch) =>
|
|
320
373
|
insertLeaf("hardBreak", state, dispatch);
|
|
@@ -339,7 +392,7 @@ function insertLeaf(name: string, ...[state, dispatch]: Parameters<Command>): bo
|
|
|
339
392
|
type.create(
|
|
340
393
|
null,
|
|
341
394
|
null,
|
|
342
|
-
marks.filter((m) => tr.doc.resolve(pos).parent.type.allowsMarkType(m.type)),
|
|
395
|
+
typedMarks(marks).filter((m) => tr.doc.resolve(pos).parent.type.allowsMarkType(m.type)),
|
|
343
396
|
),
|
|
344
397
|
);
|
|
345
398
|
tr.setSelection(TextSelection.create(tr.doc, pos + 1));
|
|
@@ -381,6 +434,11 @@ export function toggleMark(type: string | MarkType, attrs?: Attrs | null): Comma
|
|
|
381
434
|
if ($caret) {
|
|
382
435
|
if (!$caret.parent.type.allowsMarkType(mark)) return false;
|
|
383
436
|
const marks = state.storedMarks ?? marksAt($caret);
|
|
437
|
+
const word = wordContext(state);
|
|
438
|
+
if (word && isWordToggle(word, mark)) {
|
|
439
|
+
dispatch?.(state.tr.setStoredMarks(toggledCaretMarks(word, $caret.parent, marks, mark)));
|
|
440
|
+
return true;
|
|
441
|
+
}
|
|
384
442
|
const existing = mark.isInSet(marks);
|
|
385
443
|
const created = existing ? null : createMark(mark, attrs);
|
|
386
444
|
if (!existing && !created) return false;
|
|
@@ -397,6 +455,16 @@ export function toggleMark(type: string | MarkType, attrs?: Attrs | null): Comma
|
|
|
397
455
|
return !applicable;
|
|
398
456
|
});
|
|
399
457
|
if (!applicable) return false;
|
|
458
|
+
const word = wordContext(state);
|
|
459
|
+
if (word && isWordToggle(word, mark)) {
|
|
460
|
+
const on = !wordToggleActive(word, state, mark, null);
|
|
461
|
+
return attempt(state, dispatch, (tr) => {
|
|
462
|
+
for (const range of toggleRanges(word, state, selection.from, selection.to, mark, on)) {
|
|
463
|
+
tr.removeMark(range.from, range.to, mark);
|
|
464
|
+
if (range.mark) tr.addMark(range.from, range.to, range.mark);
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
}
|
|
400
468
|
const remove = rangeHasMark(state, selection.from, selection.to, mark);
|
|
401
469
|
const created = remove ? null : createMark(mark, attrs);
|
|
402
470
|
if (!remove && !created) return false;
|
|
@@ -409,6 +477,8 @@ export function toggleMark(type: string | MarkType, attrs?: Attrs | null): Comma
|
|
|
409
477
|
|
|
410
478
|
/** The mark, or null when `attrs` are invalid for it (e.g. a link with an unsafe href). */
|
|
411
479
|
function createMark(type: MarkType, attrs?: Attrs | null): Mark | null {
|
|
480
|
+
// Schema v3 keeps any link target a file has (`file:`…); a command adds only safe ones.
|
|
481
|
+
if (type.name === "link" && !isSafeLink(attrs)) return null;
|
|
412
482
|
try {
|
|
413
483
|
return type.create(attrs);
|
|
414
484
|
} catch (error) {
|
|
@@ -422,21 +492,31 @@ export function isMarkActive(state: EditorState, type: string | MarkType): boole
|
|
|
422
492
|
const mark = markType(state, type);
|
|
423
493
|
if (!mark) return false;
|
|
424
494
|
const $caret = caret(state);
|
|
495
|
+
const word = wordContext(state);
|
|
496
|
+
if (word && isWordToggle(word, mark))
|
|
497
|
+
return wordToggleActive(
|
|
498
|
+
word,
|
|
499
|
+
state,
|
|
500
|
+
mark,
|
|
501
|
+
$caret ? (state.storedMarks ?? marksAt($caret)) : null,
|
|
502
|
+
);
|
|
425
503
|
if ($caret) return mark.isInSet(state.storedMarks ?? marksAt($caret)) !== undefined;
|
|
426
504
|
return rangeHasMark(state, state.selection.from, state.selection.to, mark);
|
|
427
505
|
}
|
|
428
506
|
|
|
429
507
|
/**
|
|
430
|
-
*
|
|
431
|
-
*
|
|
508
|
+
* Gives the selected paragraphs a block type as Word does: `heading` (with its `level`) is
|
|
509
|
+
* Word's heading style of that level, `paragraph` the document's default paragraph style.
|
|
432
510
|
*/
|
|
433
511
|
export function setBlockType(type: string | NodeType, attrs: Attrs | null = null): Command {
|
|
434
512
|
return (state, dispatch) => {
|
|
435
|
-
const
|
|
436
|
-
if (!
|
|
437
|
-
const
|
|
438
|
-
|
|
439
|
-
|
|
513
|
+
const word = wordContext(state);
|
|
514
|
+
if (!word) return false;
|
|
515
|
+
const name = typeof type === "string" ? type : type.name;
|
|
516
|
+
if (name === "heading")
|
|
517
|
+
return setStyleNamed(`heading ${String(attrs?.level ?? 1)}`)(state, dispatch);
|
|
518
|
+
const style = name === "paragraph" ? defaultParagraphStyleOf(word) : null;
|
|
519
|
+
return style !== null && setParagraphStyle(style)(state, dispatch);
|
|
440
520
|
};
|
|
441
521
|
}
|
|
442
522
|
|
|
@@ -451,6 +531,8 @@ export function baseKeymap(): Plugin {
|
|
|
451
531
|
Enter: splitBlock,
|
|
452
532
|
"Shift-Enter": insertHardBreak,
|
|
453
533
|
"Mod-Enter": insertPageBreak,
|
|
534
|
+
// Types a tab (a list's Tab runs first); Escape then Tab leaves the editor (the view).
|
|
535
|
+
Tab: insertTab,
|
|
454
536
|
Backspace: deleteBackward(),
|
|
455
537
|
Delete: deleteForward(),
|
|
456
538
|
"Mod-b": toggleMark("bold"),
|