@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
package/src/state/selection.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Mark, type Node, type ResolvedPos } from "../model/index.js";
|
|
|
2
2
|
import type { Mappable } from "../transform/stepmap.js";
|
|
3
3
|
|
|
4
4
|
export interface SelectionJSON {
|
|
5
|
-
type: "text" | "all";
|
|
5
|
+
type: "text" | "node" | "all";
|
|
6
6
|
anchor: number;
|
|
7
7
|
head: number;
|
|
8
8
|
}
|
|
@@ -78,6 +78,13 @@ export abstract class Selection {
|
|
|
78
78
|
static fromJSON(doc: Node, json: SelectionJSON): Selection {
|
|
79
79
|
if (json.type === "all") return new AllSelection(doc);
|
|
80
80
|
const clamp = (pos: number) => Math.max(0, Math.min(pos, doc.content.size));
|
|
81
|
+
if (json.type === "node") {
|
|
82
|
+
const $pos = doc.resolve(clamp(json.anchor));
|
|
83
|
+
const node = $pos.nodeAfter;
|
|
84
|
+
return node && !$pos.textOffset && NodeSelection.isSelectable(node)
|
|
85
|
+
? new NodeSelection($pos)
|
|
86
|
+
: Selection.near($pos);
|
|
87
|
+
}
|
|
81
88
|
return TextSelection.between(doc.resolve(clamp(json.anchor)), doc.resolve(clamp(json.head)));
|
|
82
89
|
}
|
|
83
90
|
}
|
|
@@ -141,6 +148,55 @@ export class TextSelection extends Selection {
|
|
|
141
148
|
}
|
|
142
149
|
}
|
|
143
150
|
|
|
151
|
+
/**
|
|
152
|
+
* One node selected as a unit (a horizontal rule, later an image): `from` is the position
|
|
153
|
+
* before it, `to` the position after it. Only selectable nodes (not text) can be selected.
|
|
154
|
+
*/
|
|
155
|
+
export class NodeSelection extends Selection {
|
|
156
|
+
readonly node: Node;
|
|
157
|
+
|
|
158
|
+
constructor($pos: ResolvedPos) {
|
|
159
|
+
const node = $pos.nodeAfter;
|
|
160
|
+
if (!node || $pos.textOffset || !NodeSelection.isSelectable(node)) {
|
|
161
|
+
throw new RangeError(`No selectable node at position ${$pos.pos}`);
|
|
162
|
+
}
|
|
163
|
+
super($pos, $pos.doc.resolve($pos.pos + node.nodeSize));
|
|
164
|
+
this.node = node;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
static create(doc: Node, from: number): NodeSelection {
|
|
168
|
+
return new NodeSelection(doc.resolve(from));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
static isSelectable(node: Node): boolean {
|
|
172
|
+
return !node.isText && node.type.spec.selectable !== false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Still the same node when it survives the mapping; a text selection near it otherwise. */
|
|
176
|
+
map(doc: Node, mapping: Mappable): Selection {
|
|
177
|
+
const from = mapping.mapResult(this.anchor, 1);
|
|
178
|
+
const to = mapping.mapResult(this.head, -1);
|
|
179
|
+
const $pos = doc.resolve(from.pos);
|
|
180
|
+
const node = $pos.nodeAfter;
|
|
181
|
+
// The node now at the start must still end at the mapped end: otherwise it was deleted
|
|
182
|
+
// (both ends collapse) or its content moved out (a lift maps its edges onto a child's).
|
|
183
|
+
if (
|
|
184
|
+
from.deleted ||
|
|
185
|
+
to.deleted ||
|
|
186
|
+
!node ||
|
|
187
|
+
$pos.textOffset ||
|
|
188
|
+
from.pos + node.nodeSize !== to.pos
|
|
189
|
+
) {
|
|
190
|
+
return Selection.near($pos);
|
|
191
|
+
}
|
|
192
|
+
return NodeSelection.isSelectable(node) ? new NodeSelection($pos) : Selection.near($pos);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
toJSON(): SelectionJSON {
|
|
196
|
+
return { type: "node", anchor: this.anchor, head: this.head };
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
144
200
|
/** The whole document. */
|
|
145
201
|
export class AllSelection extends Selection {
|
|
146
202
|
constructor(doc: Node) {
|
|
@@ -156,9 +212,22 @@ export class AllSelection extends Selection {
|
|
|
156
212
|
}
|
|
157
213
|
}
|
|
158
214
|
|
|
159
|
-
/**
|
|
215
|
+
/**
|
|
216
|
+
* Marks that text typed at `$pos` should get: those of the text before it (or after, at a block
|
|
217
|
+
* start). A mark that is not inclusive (a link) continues only where the text after has it too.
|
|
218
|
+
*/
|
|
160
219
|
export function marksAt($pos: ResolvedPos): readonly Mark[] {
|
|
161
220
|
if (!$pos.parent.inlineContent) return Mark.none;
|
|
162
221
|
if ($pos.textOffset) return $pos.parent.child($pos.index()).marks;
|
|
163
|
-
|
|
222
|
+
const before = $pos.nodeBefore;
|
|
223
|
+
const after = $pos.nodeAfter;
|
|
224
|
+
const main = before ?? after;
|
|
225
|
+
const other = before ? after : null;
|
|
226
|
+
let marks = main?.marks ?? Mark.none;
|
|
227
|
+
for (const mark of marks) {
|
|
228
|
+
if (!mark.type.inclusive && !(other && mark.isInSet(other.marks))) {
|
|
229
|
+
marks = mark.removeFromSet(marks);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return marks;
|
|
164
233
|
}
|
package/src/state/state.ts
CHANGED
|
@@ -7,8 +7,8 @@ import {
|
|
|
7
7
|
Slice,
|
|
8
8
|
} from "../model/index.js";
|
|
9
9
|
import type { Step } from "../transform/steps.js";
|
|
10
|
-
import { Transform } from "../transform/transform.js";
|
|
11
|
-
import { AllSelection, Selection, TextSelection } from "./selection.js";
|
|
10
|
+
import { Transform, TransformError } from "../transform/transform.js";
|
|
11
|
+
import { AllSelection, NodeSelection, Selection, TextSelection } from "./selection.js";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* A command: checks whether it applies to `state` and, given `dispatch`, performs it.
|
|
@@ -128,6 +128,15 @@ export class Transaction extends Transform {
|
|
|
128
128
|
return this.setSelection(Selection.atStart(this.doc));
|
|
129
129
|
}
|
|
130
130
|
const start = this.steps.length;
|
|
131
|
+
const { $from } = selection;
|
|
132
|
+
const index = $from.index();
|
|
133
|
+
if (selection instanceof NodeSelection && !$from.parent.canReplace(index, index + 1)) {
|
|
134
|
+
// The parent cannot lose its only (or a required) child: leave an empty default block.
|
|
135
|
+
const type = $from.parent.contentMatchAt(index).defaultType;
|
|
136
|
+
if (!type?.isTextblock) throw new TransformError("The selected node cannot be deleted");
|
|
137
|
+
this.replace(selection.from, selection.to, new Slice(Fragment.from(type.create()), 0, 0));
|
|
138
|
+
return this.setSelection(TextSelection.create(this.doc, selection.from + 1));
|
|
139
|
+
}
|
|
131
140
|
this.delete(selection.from, selection.to);
|
|
132
141
|
const caret = this.mapping.slice(start).map(selection.from, -1);
|
|
133
142
|
return this.setSelection(TextSelection.create(this.doc, caret));
|
package/src/transform/index.ts
CHANGED
|
@@ -4,8 +4,16 @@ export {
|
|
|
4
4
|
NodeMarkupBatchStep,
|
|
5
5
|
NodeMarkupStep,
|
|
6
6
|
RemoveMarkStep,
|
|
7
|
+
ReplaceAroundStep,
|
|
7
8
|
ReplaceStep,
|
|
8
9
|
Step,
|
|
9
10
|
StepResult,
|
|
10
11
|
} from "./steps.js";
|
|
12
|
+
export {
|
|
13
|
+
canJoin,
|
|
14
|
+
canSplit,
|
|
15
|
+
findWrapping,
|
|
16
|
+
liftTarget,
|
|
17
|
+
type NodeTypeWithAttrs,
|
|
18
|
+
} from "./structure.js";
|
|
11
19
|
export { Transform, TransformError } from "./transform.js";
|
package/src/transform/stepmap.ts
CHANGED
|
@@ -33,7 +33,11 @@ export class StepMap implements Mappable {
|
|
|
33
33
|
readonly inverted = false,
|
|
34
34
|
) {}
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Maps `pos`. `sides` (from `sides()` of a map with the same ranges) overrides `assoc` per
|
|
38
|
+
* range at pure insertions, where the side decides which side of the new content `pos` ends up.
|
|
39
|
+
*/
|
|
40
|
+
mapResult(pos: number, assoc = 1, sides?: readonly number[]): MapResult {
|
|
37
41
|
let diff = 0;
|
|
38
42
|
const oldIndex = this.inverted ? 2 : 1;
|
|
39
43
|
const newIndex = this.inverted ? 1 : 2;
|
|
@@ -44,7 +48,13 @@ export class StepMap implements Mappable {
|
|
|
44
48
|
const newSize = this.ranges[i + newIndex] as number;
|
|
45
49
|
const end = start + oldSize;
|
|
46
50
|
if (pos <= end) {
|
|
47
|
-
const side = !oldSize
|
|
51
|
+
const side = !oldSize
|
|
52
|
+
? sides?.[i / 3] || assoc
|
|
53
|
+
: pos === start
|
|
54
|
+
? -1
|
|
55
|
+
: pos === end
|
|
56
|
+
? 1
|
|
57
|
+
: assoc;
|
|
48
58
|
// Recoverable unless the position sits on the range edge it associates with; a start edge
|
|
49
59
|
// with right association must be recoverable, or it drifts past re-inserted content.
|
|
50
60
|
const recoverable = pos !== (assoc < 0 ? start : end);
|
|
@@ -91,19 +101,15 @@ export class StepMap implements Mappable {
|
|
|
91
101
|
}
|
|
92
102
|
|
|
93
103
|
/**
|
|
94
|
-
* Which side of
|
|
95
|
-
*
|
|
96
|
-
* shortcut: one side for the whole map, exact only for single-range maps (every step today);
|
|
97
|
-
* record a side per range when multi-range steps (ReplaceAroundStep, Phase 7) arrive.
|
|
104
|
+
* Which side of each changed range `pos` (in input coordinates) lies on: -1 at or before the
|
|
105
|
+
* range, 1 at or after it, 0 strictly inside. One entry per range, in order.
|
|
98
106
|
*/
|
|
99
|
-
|
|
100
|
-
|
|
107
|
+
sides(pos: number): (-1 | 0 | 1)[] {
|
|
108
|
+
const sides: (-1 | 0 | 1)[] = [];
|
|
101
109
|
this.forEach((oldStart, oldEnd) => {
|
|
102
|
-
|
|
103
|
-
if (pos <= oldStart) side = -1;
|
|
104
|
-
else if (pos >= oldEnd) side = 1;
|
|
110
|
+
sides.push(pos <= oldStart ? -1 : pos >= oldEnd ? 1 : 0);
|
|
105
111
|
});
|
|
106
|
-
return
|
|
112
|
+
return sides;
|
|
107
113
|
}
|
|
108
114
|
|
|
109
115
|
invert(): StepMap {
|
package/src/transform/steps.ts
CHANGED
|
@@ -59,17 +59,25 @@ export abstract class Step {
|
|
|
59
59
|
abstract map(mapping: Mappable): Step | null;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
/**
|
|
62
|
+
/**
|
|
63
|
+
* Replaces `from..to` with a slice. A `structure` step only moves node boundaries (split, join):
|
|
64
|
+
* it fails instead of deleting content that, after mapping through other changes, now sits in
|
|
65
|
+
* its range.
|
|
66
|
+
*/
|
|
63
67
|
export class ReplaceStep extends Step {
|
|
64
68
|
constructor(
|
|
65
69
|
readonly from: number,
|
|
66
70
|
readonly to: number,
|
|
67
71
|
readonly slice: Slice,
|
|
72
|
+
readonly structure = false,
|
|
68
73
|
) {
|
|
69
74
|
super();
|
|
70
75
|
}
|
|
71
76
|
|
|
72
77
|
apply(doc: Node): StepResult {
|
|
78
|
+
if (this.structure && contentBetween(doc, this.from, this.to)) {
|
|
79
|
+
return StepResult.fail("Structure replace would overwrite content");
|
|
80
|
+
}
|
|
73
81
|
return StepResult.fromReplace(doc, this.from, this.to, this.slice);
|
|
74
82
|
}
|
|
75
83
|
|
|
@@ -85,7 +93,7 @@ export class ReplaceStep extends Step {
|
|
|
85
93
|
const from = mapping.mapResult(this.from, 1);
|
|
86
94
|
const to = mapping.mapResult(this.to, -1);
|
|
87
95
|
if (from.deleted && to.deleted) return null;
|
|
88
|
-
return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice);
|
|
96
|
+
return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice, this.structure);
|
|
89
97
|
}
|
|
90
98
|
|
|
91
99
|
/**
|
|
@@ -93,7 +101,7 @@ export class ReplaceStep extends Step {
|
|
|
93
101
|
* (e.g. consecutive keystrokes); null otherwise. Keeps typing history to one step per run.
|
|
94
102
|
*/
|
|
95
103
|
merge(next: Step): ReplaceStep | null {
|
|
96
|
-
if (!(next instanceof ReplaceStep)) return null;
|
|
104
|
+
if (!(next instanceof ReplaceStep) || next.structure || this.structure) return null;
|
|
97
105
|
const join = (a: Slice, b: Slice) =>
|
|
98
106
|
a.size + b.size === 0
|
|
99
107
|
? Slice.empty
|
|
@@ -112,6 +120,121 @@ export class ReplaceStep extends Step {
|
|
|
112
120
|
}
|
|
113
121
|
}
|
|
114
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Replaces `from..to` with a slice while keeping the content between `gapFrom` and `gapTo`,
|
|
125
|
+
* which is inserted into the slice at `insert`. Wrapping blocks in a list (the slice holds the
|
|
126
|
+
* empty wrappers) and lifting them out (the slice holds the split ancestors) are such steps:
|
|
127
|
+
* the moved content is not deleted and re-inserted, so positions inside it map through.
|
|
128
|
+
*/
|
|
129
|
+
export class ReplaceAroundStep extends Step {
|
|
130
|
+
constructor(
|
|
131
|
+
readonly from: number,
|
|
132
|
+
readonly to: number,
|
|
133
|
+
readonly gapFrom: number,
|
|
134
|
+
readonly gapTo: number,
|
|
135
|
+
readonly slice: Slice,
|
|
136
|
+
readonly insert: number,
|
|
137
|
+
readonly structure = false,
|
|
138
|
+
) {
|
|
139
|
+
super();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
apply(doc: Node): StepResult {
|
|
143
|
+
if (
|
|
144
|
+
this.from < 0 ||
|
|
145
|
+
this.from > this.gapFrom ||
|
|
146
|
+
this.gapFrom > this.gapTo ||
|
|
147
|
+
this.gapTo > this.to ||
|
|
148
|
+
this.to > doc.content.size
|
|
149
|
+
) {
|
|
150
|
+
return StepResult.fail(`Invalid gap range ${this.from}..${this.to}`);
|
|
151
|
+
}
|
|
152
|
+
if (
|
|
153
|
+
this.structure &&
|
|
154
|
+
(contentBetween(doc, this.from, this.gapFrom) || contentBetween(doc, this.gapTo, this.to))
|
|
155
|
+
) {
|
|
156
|
+
return StepResult.fail("Structure gap-replace would overwrite content");
|
|
157
|
+
}
|
|
158
|
+
const gap = doc.slice(this.gapFrom, this.gapTo);
|
|
159
|
+
if (gap.openStart || gap.openEnd) return StepResult.fail("Gap is not a flat range");
|
|
160
|
+
let inserted: Slice | null;
|
|
161
|
+
try {
|
|
162
|
+
inserted = this.slice.insertAt(this.insert, gap.content);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (error instanceof RangeError) return StepResult.fail(error.message);
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
if (!inserted) return StepResult.fail("Content does not fit in gap");
|
|
168
|
+
return StepResult.fromReplace(doc, this.from, this.to, inserted);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
override getMap(): StepMap {
|
|
172
|
+
return new StepMap([
|
|
173
|
+
this.from,
|
|
174
|
+
this.gapFrom - this.from,
|
|
175
|
+
this.insert,
|
|
176
|
+
this.gapTo,
|
|
177
|
+
this.to - this.gapTo,
|
|
178
|
+
this.slice.size - this.insert,
|
|
179
|
+
]);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
invert(doc: Node): Step {
|
|
183
|
+
const gap = this.gapTo - this.gapFrom;
|
|
184
|
+
return new ReplaceAroundStep(
|
|
185
|
+
this.from,
|
|
186
|
+
this.from + this.slice.size + gap,
|
|
187
|
+
this.from + this.insert,
|
|
188
|
+
this.from + this.insert + gap,
|
|
189
|
+
doc.slice(this.from, this.to).removeBetween(this.gapFrom - this.from, this.gapTo - this.from),
|
|
190
|
+
this.gapFrom - this.from,
|
|
191
|
+
this.structure,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
map(mapping: Mappable): Step | null {
|
|
196
|
+
const from = mapping.mapResult(this.from, 1);
|
|
197
|
+
const to = mapping.mapResult(this.to, -1);
|
|
198
|
+
const gapFrom = this.from === this.gapFrom ? from.pos : mapping.map(this.gapFrom, -1);
|
|
199
|
+
const gapTo = this.to === this.gapTo ? to.pos : mapping.map(this.gapTo, 1);
|
|
200
|
+
if ((from.deleted && to.deleted) || gapFrom < from.pos || gapTo > to.pos) return null;
|
|
201
|
+
// The content it moves is gone: a lift would only split its ancestors around nothing.
|
|
202
|
+
if (this.gapTo > this.gapFrom && gapTo <= gapFrom) return null;
|
|
203
|
+
return new ReplaceAroundStep(
|
|
204
|
+
from.pos,
|
|
205
|
+
to.pos,
|
|
206
|
+
gapFrom,
|
|
207
|
+
gapTo,
|
|
208
|
+
this.slice,
|
|
209
|
+
this.insert,
|
|
210
|
+
this.structure,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Whether `from..to` holds anything but node boundaries: text, a leaf, or a node that is not
|
|
217
|
+
* the first or last along the path. Used by structure steps.
|
|
218
|
+
*/
|
|
219
|
+
function contentBetween(doc: Node, from: number, to: number): boolean {
|
|
220
|
+
const $from = doc.resolve(from);
|
|
221
|
+
let dist = to - from;
|
|
222
|
+
let depth = $from.depth;
|
|
223
|
+
while (dist > 0 && depth > 0 && $from.indexAfter(depth) === $from.node(depth).childCount) {
|
|
224
|
+
depth--;
|
|
225
|
+
dist--;
|
|
226
|
+
}
|
|
227
|
+
if (dist > 0) {
|
|
228
|
+
let next = $from.node(depth).maybeChild($from.indexAfter(depth));
|
|
229
|
+
while (dist > 0) {
|
|
230
|
+
if (!next || next.isLeaf) return true;
|
|
231
|
+
next = next.firstChild;
|
|
232
|
+
dist--;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
|
|
115
238
|
/** Rebuilds inline nodes of a fragment with `f`, descending into block content. */
|
|
116
239
|
function mapInline(
|
|
117
240
|
fragment: Fragment,
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// The structure helpers follow the design of prosemirror-transform's structure.ts (MIT, see
|
|
2
|
+
// THIRD_PARTY_NOTICES.md); independent implementation on Foli's model.
|
|
3
|
+
import type { Attrs, Node, NodeRange, NodeType } from "../model/index.js";
|
|
4
|
+
|
|
5
|
+
/** A node type with attributes, as `findWrapping` returns and `Transform.wrap`/`split` take. */
|
|
6
|
+
export interface NodeTypeWithAttrs {
|
|
7
|
+
type: NodeType;
|
|
8
|
+
attrs?: Attrs | null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Whether the children of `node` before `start` and from `end` on are valid on their own. */
|
|
12
|
+
function canCut(node: Node, start: number, end: number): boolean {
|
|
13
|
+
return (
|
|
14
|
+
(start === 0 || node.canReplace(start, node.childCount)) &&
|
|
15
|
+
(end === node.childCount || node.canReplace(0, end))
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The depth the blocks of `range` can be lifted to (out of their parent, e.g. out of a list
|
|
21
|
+
* item), or null when they cannot be lifted. Ancestors are split around the range as needed.
|
|
22
|
+
*/
|
|
23
|
+
export function liftTarget(range: NodeRange): number | null {
|
|
24
|
+
const content = range.parent.content.cutByIndex(range.startIndex, range.endIndex);
|
|
25
|
+
for (let depth = range.depth; ; depth--) {
|
|
26
|
+
const node = range.$from.node(depth);
|
|
27
|
+
const index = range.$from.index(depth);
|
|
28
|
+
const endIndex = range.$to.indexAfter(depth);
|
|
29
|
+
if (depth < range.depth && node.canReplace(index, endIndex, content)) return depth;
|
|
30
|
+
if (depth === 0 || !canCut(node, index, endIndex)) return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The wrappers (outermost first) that put the blocks of `range` inside a node of `type`: any
|
|
36
|
+
* wrappers its parent needs around `type`, `type` itself, then any wrappers needed between
|
|
37
|
+
* `type` and the blocks (for a list: the list, then a list item). Null when impossible.
|
|
38
|
+
*/
|
|
39
|
+
export function findWrapping(
|
|
40
|
+
range: NodeRange,
|
|
41
|
+
type: NodeType,
|
|
42
|
+
attrs: Attrs | null = null,
|
|
43
|
+
): NodeTypeWithAttrs[] | null {
|
|
44
|
+
const around = wrappingOutside(range, type);
|
|
45
|
+
const inner = around && wrappingInside(range, type);
|
|
46
|
+
if (!around || !inner) return null;
|
|
47
|
+
return [
|
|
48
|
+
...around.map((t) => ({ type: t, attrs: null })),
|
|
49
|
+
{ type, attrs },
|
|
50
|
+
...inner.map((t) => ({ type: t, attrs: null })),
|
|
51
|
+
];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function wrappingOutside(range: NodeRange, type: NodeType): readonly NodeType[] | null {
|
|
55
|
+
const { parent, startIndex, endIndex } = range;
|
|
56
|
+
const around = parent.contentMatchAt(startIndex).findWrapping(type);
|
|
57
|
+
if (!around) return null;
|
|
58
|
+
const outer = around[0] ?? type;
|
|
59
|
+
return parent.canReplaceWith(startIndex, endIndex, outer) ? around : null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function wrappingInside(range: NodeRange, type: NodeType): readonly NodeType[] | null {
|
|
63
|
+
const { parent, startIndex, endIndex } = range;
|
|
64
|
+
const inside = type.contentMatch.findWrapping(parent.child(startIndex).type);
|
|
65
|
+
if (!inside) return null;
|
|
66
|
+
let match = (inside[inside.length - 1] ?? type).contentMatch;
|
|
67
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
68
|
+
const next = match.matchType(parent.child(i).type);
|
|
69
|
+
if (!next) return null;
|
|
70
|
+
match = next;
|
|
71
|
+
}
|
|
72
|
+
return match.validEnd ? inside : null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Whether splitting at `pos` through `depth` ancestors is valid, giving the new nodes after the
|
|
77
|
+
* split the types in `typesAfter`: entry 0 for the outermost split node, entry `depth - 1` for
|
|
78
|
+
* the innermost (the one holding `pos`); missing or null entries keep the original type.
|
|
79
|
+
*/
|
|
80
|
+
export function canSplit(
|
|
81
|
+
doc: Node,
|
|
82
|
+
pos: number,
|
|
83
|
+
depth = 1,
|
|
84
|
+
typesAfter?: readonly (NodeTypeWithAttrs | null | undefined)[],
|
|
85
|
+
): boolean {
|
|
86
|
+
const $pos = doc.resolve(pos);
|
|
87
|
+
const base = $pos.depth - depth;
|
|
88
|
+
if (base < 0) return false;
|
|
89
|
+
if (typesAfter?.some((entry) => entry && !attrsValid(entry))) return false;
|
|
90
|
+
const innerType = typesAfter?.[depth - 1]?.type ?? $pos.parent.type;
|
|
91
|
+
if (
|
|
92
|
+
!$pos.parent.canReplace($pos.index(), $pos.parent.childCount) ||
|
|
93
|
+
!innerType.validContent($pos.parent.content.cutByIndex($pos.index(), $pos.parent.childCount))
|
|
94
|
+
) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
for (let d = $pos.depth - 1, i = depth - 2; d > base; d--, i--) {
|
|
98
|
+
const node = $pos.node(d);
|
|
99
|
+
const index = $pos.index(d);
|
|
100
|
+
let rest = node.content.cutByIndex(index, node.childCount);
|
|
101
|
+
const override = typesAfter?.[i + 1];
|
|
102
|
+
if (override) {
|
|
103
|
+
rest = rest.replaceChild(0, override.type.createOpen(override.attrs));
|
|
104
|
+
}
|
|
105
|
+
const after = typesAfter?.[i]?.type ?? node.type;
|
|
106
|
+
if (!node.canReplace(index + 1, node.childCount) || !after.validContent(rest)) return false;
|
|
107
|
+
}
|
|
108
|
+
const index = $pos.indexAfter(base);
|
|
109
|
+
const baseType = typesAfter?.[0]?.type ?? $pos.node(base + 1).type;
|
|
110
|
+
return $pos.node(base).canReplaceWith(index, index, baseType);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function attrsValid({ type, attrs }: NodeTypeWithAttrs): boolean {
|
|
114
|
+
try {
|
|
115
|
+
type.createOpen(attrs);
|
|
116
|
+
return true;
|
|
117
|
+
} catch {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Whether the nodes on either side of `pos` can be joined into one. */
|
|
123
|
+
export function canJoin(doc: Node, pos: number): boolean {
|
|
124
|
+
if (pos <= 0 || pos >= doc.content.size) return false;
|
|
125
|
+
const $pos = doc.resolve(pos);
|
|
126
|
+
const before = $pos.nodeBefore;
|
|
127
|
+
const after = $pos.nodeAfter;
|
|
128
|
+
// A leaf after the boundary has no content to join: "joining" would delete it.
|
|
129
|
+
if (!before || !after || before.isLeaf || after.isLeaf) return false;
|
|
130
|
+
const index = $pos.index();
|
|
131
|
+
return (
|
|
132
|
+
before.canReplace(before.childCount, before.childCount, after.content) &&
|
|
133
|
+
$pos.parent.canReplace(index, index + 1)
|
|
134
|
+
);
|
|
135
|
+
}
|