@folijs/core 0.1.0 → 0.2.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 +16 -0
- package/dist/document.d.ts +5 -4
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +30 -8
- package/dist/document.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- 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 +2 -0
- package/dist/model/fragment.d.ts.map +1 -1
- package/dist/model/fragment.js +8 -0
- 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 +12 -0
- 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 +62 -7
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +82 -8
- 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 +18 -3
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +199 -44
- package/dist/state/commands.js.map +1 -1
- package/dist/state/editor.d.ts +24 -0
- package/dist/state/editor.d.ts.map +1 -1
- package/dist/state/editor.js +14 -1
- package/dist/state/editor.js.map +1 -1
- package/dist/state/format.d.ts +27 -0
- package/dist/state/format.d.ts.map +1 -0
- package/dist/state/format.js +144 -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 +5 -2
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +5 -2
- 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 +186 -0
- package/dist/state/links.js.map +1 -0
- package/dist/state/lists.d.ts +36 -0
- package/dist/state/lists.d.ts.map +1 -0
- package/dist/state/lists.js +383 -0
- package/dist/state/lists.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 +12 -2
- package/dist/state/state.js.map +1 -1
- 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/package.json +1 -1
- package/src/document.ts +34 -8
- package/src/index.ts +14 -0
- package/src/model/attrs.ts +19 -0
- package/src/model/content.ts +56 -0
- package/src/model/fragment.ts +7 -0
- package/src/model/index.ts +1 -1
- package/src/model/mark.ts +3 -2
- package/src/model/node.ts +42 -2
- 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 +135 -12
- package/src/state/attempt.ts +22 -0
- package/src/state/commands.ts +209 -47
- package/src/state/editor.ts +38 -2
- package/src/state/format.ts +133 -0
- package/src/state/history.ts +3 -3
- package/src/state/index.ts +31 -0
- package/src/state/links.ts +185 -0
- package/src/state/lists.ts +427 -0
- package/src/state/selection.ts +72 -3
- package/src/state/state.ts +11 -2
- 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
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type { Mark, MarkType, ResolvedPos } from "../model/index.js";
|
|
2
|
+
import { isSafeHref } from "../schema.js";
|
|
3
|
+
import { TransformError } from "../transform/transform.js";
|
|
4
|
+
import { attempt } from "./attempt.js";
|
|
5
|
+
import { AllSelection, marksAt, TextSelection } from "./selection.js";
|
|
6
|
+
import type { Command, EditorState, Transaction } from "./state.js";
|
|
7
|
+
|
|
8
|
+
const EMAIL = /^[^\s@/:]+@[^\s@/:]+\.\p{L}{2,}$/iu;
|
|
9
|
+
/**
|
|
10
|
+
* A host name with a dot and a letter TLD (`www.` alone is enough; letters in any script), then a
|
|
11
|
+
* port, path or end.
|
|
12
|
+
*/
|
|
13
|
+
const DOMAIN = /^(?:www\.|(?:[\p{L}\p{N}-]+\.)+\p{L}{2,}(?:[:/?#]|$))/iu;
|
|
14
|
+
const AUTOLINK = /^(?:https?:\/\/|mailto:|www\.)\S+$/i;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Cleans up a typed or pasted URL before validation (`isSafeHref`): trims it, turns `\` into
|
|
18
|
+
* `/`, percent-encodes spaces, and adds the scheme people leave out: `mailto:` for an email
|
|
19
|
+
* address, `https://` for a host name (`example.com/page`, `www.example.com`). A relative
|
|
20
|
+
* file name that looks like a host (`page.html`) becomes one too: write `./page.html`.
|
|
21
|
+
*/
|
|
22
|
+
export function normalizeHref(input: string): string {
|
|
23
|
+
const href = input.trim().replace(/\\/g, "/").replace(/ /g, "%20");
|
|
24
|
+
if (EMAIL.test(href)) return `mailto:${href}`;
|
|
25
|
+
if (DOMAIN.test(href)) return `https://${href}`;
|
|
26
|
+
return href;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The href for pasted plain text that is one URL (`http(s)://…`, `mailto:…`, `www.…`), or null:
|
|
31
|
+
* pasting it makes a link.
|
|
32
|
+
*/
|
|
33
|
+
export function autolinkHref(text: string): string | null {
|
|
34
|
+
const trimmed = text.trim();
|
|
35
|
+
if (!AUTOLINK.test(trimmed)) return null;
|
|
36
|
+
const href = normalizeHref(trimmed);
|
|
37
|
+
return isSafeHref(href) ? href : null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const linkType = (state: EditorState): MarkType | undefined => state.schema.marks.link;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The link around a caret (touching it at either edge, so a caret just after a link edits that
|
|
44
|
+
* link), with its extent, or null.
|
|
45
|
+
*/
|
|
46
|
+
function linkAround(
|
|
47
|
+
$pos: ResolvedPos,
|
|
48
|
+
type: MarkType,
|
|
49
|
+
): { from: number; to: number; mark: Mark } | null {
|
|
50
|
+
const parent = $pos.parent;
|
|
51
|
+
if (!parent.inlineContent) return null;
|
|
52
|
+
const index = $pos.index();
|
|
53
|
+
for (const i of $pos.textOffset ? [index] : [index - 1, index]) {
|
|
54
|
+
const mark = type.isInSet(parent.maybeChild(i)?.marks ?? []);
|
|
55
|
+
if (!mark) continue;
|
|
56
|
+
let start = i;
|
|
57
|
+
let end = i + 1;
|
|
58
|
+
while (start > 0 && mark.isInSet(parent.child(start - 1).marks)) start--;
|
|
59
|
+
while (end < parent.childCount && mark.isInSet(parent.child(end).marks)) end++;
|
|
60
|
+
let from = $pos.start();
|
|
61
|
+
let to = from;
|
|
62
|
+
for (let c = 0; c < end; c++) {
|
|
63
|
+
const size = parent.child(c).nodeSize;
|
|
64
|
+
if (c < start) from += size;
|
|
65
|
+
to += size;
|
|
66
|
+
}
|
|
67
|
+
return { from, to, mark };
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Whether the selection is in (or holds) text that can take a link. */
|
|
73
|
+
function canLink(state: EditorState, type: MarkType): boolean {
|
|
74
|
+
const { selection } = state;
|
|
75
|
+
if (selection.empty) {
|
|
76
|
+
const { parent } = selection.$from;
|
|
77
|
+
return parent.inlineContent && parent.type.allowsMarkType(type);
|
|
78
|
+
}
|
|
79
|
+
let found = false;
|
|
80
|
+
state.doc.nodesBetween(selection.from, selection.to, (node) => {
|
|
81
|
+
if (node.isTextblock && node.type.allowsMarkType(type)) found = true;
|
|
82
|
+
return !found;
|
|
83
|
+
});
|
|
84
|
+
return found;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The href of the link at the selection: around a caret, or the one link all selected text has
|
|
89
|
+
* (null when text has no link or different ones).
|
|
90
|
+
*/
|
|
91
|
+
export function linkAt(state: EditorState): string | null {
|
|
92
|
+
const type = linkType(state);
|
|
93
|
+
if (!type) return null;
|
|
94
|
+
const { selection } = state;
|
|
95
|
+
if (selection.empty)
|
|
96
|
+
return (linkAround(selection.$from, type)?.mark.attrs.href as string) ?? null;
|
|
97
|
+
// undefined: no text seen yet; null: text without a link, or with another one.
|
|
98
|
+
let href = undefined as string | null | undefined;
|
|
99
|
+
state.doc.nodesBetween(selection.from, selection.to, (node, _pos, parent) => {
|
|
100
|
+
if (href === null) return false;
|
|
101
|
+
if (!node.isInline || !parent?.type.allowsMarkType(type)) return undefined;
|
|
102
|
+
const value = (type.isInSet(node.marks)?.attrs.href as string | undefined) ?? null;
|
|
103
|
+
href = href === undefined || href === value ? value : null;
|
|
104
|
+
return false;
|
|
105
|
+
});
|
|
106
|
+
return href ?? null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Replaces the selection with `text` (default: the href) linked to `href` (normalized, then
|
|
111
|
+
* validated: an unsafe href does not apply). The text keeps the marks typing would get there.
|
|
112
|
+
*/
|
|
113
|
+
export function insertLink(href: string, text?: string): Command {
|
|
114
|
+
return (state, dispatch) => {
|
|
115
|
+
const type = linkType(state);
|
|
116
|
+
const normalized = normalizeHref(href);
|
|
117
|
+
const label = text ?? normalized;
|
|
118
|
+
const { selection } = state;
|
|
119
|
+
if (!type || !label || !isSafeHref(normalized)) return false;
|
|
120
|
+
if (!(selection instanceof AllSelection) && !canLink(state, type)) return false;
|
|
121
|
+
const link = type.create({ href: normalized });
|
|
122
|
+
return attempt(state, dispatch, (tr) => {
|
|
123
|
+
const marks = state.storedMarks ?? marksAt(selection.$from);
|
|
124
|
+
tr.deleteSelection();
|
|
125
|
+
const pos = tr.selection.from;
|
|
126
|
+
const parent = tr.doc.resolve(pos).parent;
|
|
127
|
+
if (!parent.inlineContent || !parent.type.allowsMarkType(type)) {
|
|
128
|
+
throw new TransformError("No link here");
|
|
129
|
+
}
|
|
130
|
+
const allowed = marks.filter((mark) => parent.type.allowsMarkType(mark.type));
|
|
131
|
+
tr.insertText(label, pos, pos, link.addToSet(allowed));
|
|
132
|
+
tr.setSelection(TextSelection.create(tr.doc, pos + label.length));
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Links the selected text to `href` (normalized, then validated: an unsafe href does not apply),
|
|
139
|
+
* replacing other links there. At a caret in (or at the edge of) a link, changes that link's
|
|
140
|
+
* href; elsewhere inserts the href as linked text.
|
|
141
|
+
*/
|
|
142
|
+
export function setLink(href: string): Command {
|
|
143
|
+
return (state, dispatch) => {
|
|
144
|
+
const type = linkType(state);
|
|
145
|
+
const normalized = normalizeHref(href);
|
|
146
|
+
if (!type || !isSafeHref(normalized) || !canLink(state, type)) return false;
|
|
147
|
+
const mark = type.create({ href: normalized });
|
|
148
|
+
const { selection } = state;
|
|
149
|
+
if (!selection.empty) {
|
|
150
|
+
return attempt(state, dispatch, (tr) => tr.addMark(selection.from, selection.to, mark));
|
|
151
|
+
}
|
|
152
|
+
const around = linkAround(selection.$from, type);
|
|
153
|
+
if (!around) return insertLink(normalized)(state, dispatch);
|
|
154
|
+
return attempt(state, dispatch, (tr) => tr.addMark(around.from, around.to, mark));
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Removes the links in the selection; at a caret, the whole link around it. */
|
|
159
|
+
export const removeLink: Command = (state, dispatch) => {
|
|
160
|
+
const type = linkType(state);
|
|
161
|
+
if (!type) return false;
|
|
162
|
+
const { selection } = state;
|
|
163
|
+
const range = selection.empty ? linkAround(selection.$from, type) : selection;
|
|
164
|
+
if (!range) return false;
|
|
165
|
+
return attempt(state, dispatch, (tr) => tr.removeMark(range.from, range.to, type));
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/** Transaction metadata set by `openLinkEditor`; a link editing UI listens for it. */
|
|
169
|
+
export const linkEditorRequest = "linkEditorRequest";
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Mod-K: asks the UI to open its link editor (the React toolbar's link dialog), with a
|
|
173
|
+
* transaction that changes nothing and carries `linkEditorRequest`. Does not apply where text
|
|
174
|
+
* cannot take a link. Without such a UI, Mod-K does nothing.
|
|
175
|
+
*/
|
|
176
|
+
export const openLinkEditor: Command = (state, dispatch) => {
|
|
177
|
+
const type = linkType(state);
|
|
178
|
+
if (!type || !canLink(state, type)) return false;
|
|
179
|
+
dispatch?.(state.tr.setMeta(linkEditorRequest, true));
|
|
180
|
+
return true;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/** Whether `tr` asks for the link editor (`openLinkEditor`). */
|
|
184
|
+
export const requestsLinkEditor = (tr: Transaction): boolean =>
|
|
185
|
+
tr.getMeta(linkEditorRequest) === true;
|
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
// The list commands follow the design of prosemirror-schema-list (MIT, see THIRD_PARTY_NOTICES.md);
|
|
2
|
+
// independent implementation on Foli's model.
|
|
3
|
+
import {
|
|
4
|
+
type Attrs,
|
|
5
|
+
FoliDocumentError,
|
|
6
|
+
Fragment,
|
|
7
|
+
type Node,
|
|
8
|
+
NodeRange,
|
|
9
|
+
type NodeType,
|
|
10
|
+
type ResolvedPos,
|
|
11
|
+
Slice,
|
|
12
|
+
} from "../model/index.js";
|
|
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";
|
|
19
|
+
|
|
20
|
+
/** The list node types this module treats as lists (any node holding list items). */
|
|
21
|
+
const isList = (node: Node, item: NodeType) => node.firstChild?.type === item && !node.isTextblock;
|
|
22
|
+
|
|
23
|
+
function itemTypeOf(state: EditorState): NodeType | null {
|
|
24
|
+
return state.schema.nodes.listItem ?? null;
|
|
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
|
+
}
|
|
230
|
+
|
|
231
|
+
function liftToOuterList(tr: Transaction, item: NodeType, initial: NodeRange): boolean {
|
|
232
|
+
let range = initial;
|
|
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
|
+
}
|
|
260
|
+
|
|
261
|
+
function liftOutOfList(tr: Transaction, range: NodeRange): boolean {
|
|
262
|
+
const list = range.parent;
|
|
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
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* The (empty) list that goes on after items lifted out of `list`: a numbered list goes on
|
|
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
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Tab: nests the selected list items in the item before them (a sublist of the same type). */
|
|
324
|
+
export function sinkListItem(itemType?: string | NodeType): Command {
|
|
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
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Backspace at the start of a list item's first paragraph: lifts the item (one level), as word
|
|
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
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* The list key bindings: Enter splits or leaves a list item, Backspace at an item's start lifts
|
|
413
|
+
* it, Tab and Shift+Tab nest and lift items (outside lists they do nothing here, so Tab still
|
|
414
|
+
* moves focus), Mod-Shift-7 / Mod-Shift-8 toggle a numbered / bulleted list.
|
|
415
|
+
*/
|
|
416
|
+
export function listKeymap(): Plugin {
|
|
417
|
+
return new Plugin({
|
|
418
|
+
keymap: {
|
|
419
|
+
Enter: splitListItem(),
|
|
420
|
+
Backspace: liftListItemAtStart(),
|
|
421
|
+
Tab: sinkListItem(),
|
|
422
|
+
"Shift-Tab": liftListItem(),
|
|
423
|
+
"Mod-Shift-7": toggleList("orderedList"),
|
|
424
|
+
"Mod-Shift-8": toggleList("bulletList"),
|
|
425
|
+
},
|
|
426
|
+
});
|
|
427
|
+
}
|