@nerd-bible/wordgard 0.5.3-beta7 → 0.6.2-beta10
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/dist/collab.js +3 -0
- package/dist/command.d.ts +15 -7
- package/dist/command.js +38 -18
- package/dist/doc.d.ts +58 -50
- package/dist/doc.js +224 -169
- package/dist/editor.d.ts +212 -111
- package/dist/editor.js +2263 -1767
- package/dist/highlight-code.d.ts +249 -0
- package/dist/highlight-code.js +264 -0
- package/dist/history.js +7 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/phrases.d.ts +3 -2
- package/dist/phrases.js +21 -1
- package/dist/schema.d.ts +37 -1
- package/dist/schema.js +164 -134
- package/dist/set.d.ts +3 -2
- package/dist/set.js +19 -13
- package/dist/state.d.ts +120 -29
- package/dist/state.js +224 -136
- package/dist/table.d.ts +114 -3
- package/dist/table.js +64 -36
- package/package.json +3 -2
package/dist/doc.js
CHANGED
|
@@ -42,40 +42,63 @@ const Token = /*@__PURE__*/(function (Token) {
|
|
|
42
42
|
})(Token.Type || (Token.Type = {}));
|
|
43
43
|
Token.End = {
|
|
44
44
|
tokenType: Token.Type.Close,
|
|
45
|
-
toString() { return "[end]"; }
|
|
45
|
+
toString() { return "[end]"; },
|
|
46
|
+
toJSON() { return "."; }
|
|
46
47
|
};
|
|
48
|
+
function fromJSON(schema, value) {
|
|
49
|
+
if (value === ".")
|
|
50
|
+
return Token.End;
|
|
51
|
+
if (!value || typeof value.type != "string")
|
|
52
|
+
throw new ValidationError("Invalid slice JSON");
|
|
53
|
+
let type = schema.getNode(value.type);
|
|
54
|
+
return type?.isLeaf || ("content" in value) ? schema.nodeFromJSON(value) : schema.tagFromJSON(value);
|
|
55
|
+
}
|
|
56
|
+
Token.fromJSON = fromJSON;
|
|
47
57
|
;return Token})({});
|
|
48
58
|
class Slice {
|
|
49
59
|
content;
|
|
60
|
+
context;
|
|
50
61
|
length;
|
|
51
|
-
constructor(
|
|
62
|
+
constructor(
|
|
63
|
+
content,
|
|
64
|
+
context) {
|
|
52
65
|
this.content = content;
|
|
66
|
+
this.context = context;
|
|
53
67
|
this.length = content.reduce((l, e) => l + (e.tokenType == Token.Type.Node ? e.length : 1), 0);
|
|
54
68
|
}
|
|
55
|
-
static of(content) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (
|
|
67
|
-
|
|
69
|
+
static of(content, context) {
|
|
70
|
+
return content.length || context && context.length ? new Slice(content, context || Slice.empty.context) : Slice.empty;
|
|
71
|
+
}
|
|
72
|
+
concat(other) {
|
|
73
|
+
return new Slice(Slice.concat(this.content, other.content), this.context);
|
|
74
|
+
}
|
|
75
|
+
textContent(options = {}) {
|
|
76
|
+
let { blockSeparator = "\n", leafText } = options;
|
|
77
|
+
let out = new TextOutput(blockSeparator, leafText == null ? undefined : typeof leafText == "string" ? () => leafText : leafText);
|
|
78
|
+
for (let tok of this.content) {
|
|
79
|
+
if (tok.tokenType == Token.Type.Open) {
|
|
80
|
+
if (tok.isTextblock)
|
|
81
|
+
out.openBlock();
|
|
68
82
|
}
|
|
69
|
-
else if (
|
|
70
|
-
if (
|
|
71
|
-
|
|
83
|
+
else if (tok.tokenType == Token.Type.Node) {
|
|
84
|
+
if (tok.isLeaf)
|
|
85
|
+
out.serialize(tok);
|
|
86
|
+
else
|
|
87
|
+
tok.iterate(node => !out.serialize(node));
|
|
72
88
|
}
|
|
73
89
|
}
|
|
74
|
-
return
|
|
90
|
+
return out.text;
|
|
91
|
+
}
|
|
92
|
+
static empty = /*@__PURE__*/(() => new Slice([], []))();
|
|
93
|
+
toString() {
|
|
94
|
+
return `<${this.content.join()}>`;
|
|
95
|
+
}
|
|
96
|
+
static get(doc, from, to) {
|
|
97
|
+
return new Slice(doc.slice(from, to), doc.contextAt(from));
|
|
75
98
|
}
|
|
76
|
-
run(track, startPos = 0) {
|
|
99
|
+
static run(tokens, track, startPos = 0) {
|
|
77
100
|
let pos = startPos;
|
|
78
|
-
for (let elt of
|
|
101
|
+
for (let elt of tokens) {
|
|
79
102
|
if (elt.tokenType == Token.Type.Open)
|
|
80
103
|
track.open(elt, pos++);
|
|
81
104
|
else if (elt.tokenType == Token.Type.Node) {
|
|
@@ -86,18 +109,30 @@ class Slice {
|
|
|
86
109
|
track.close(pos++);
|
|
87
110
|
}
|
|
88
111
|
}
|
|
89
|
-
|
|
112
|
+
static concat(a, b) {
|
|
113
|
+
let tokens = a.slice();
|
|
114
|
+
let i = 0;
|
|
115
|
+
if (a.length && b.length && b[0].tokenType == Token.Type.Node &&
|
|
116
|
+
a[a.length - 1].tokenType == Token.Type.Node) {
|
|
117
|
+
b[0].pushTo(tokens);
|
|
118
|
+
i = 1;
|
|
119
|
+
}
|
|
120
|
+
for (; i < b.length; i++)
|
|
121
|
+
tokens.push(b[i]);
|
|
122
|
+
return tokens;
|
|
123
|
+
}
|
|
124
|
+
static slice(tokens, from, to = 1e9) {
|
|
90
125
|
if (from == to)
|
|
91
|
-
return Slice.empty;
|
|
126
|
+
return Slice.empty.content;
|
|
92
127
|
let result = [], off = 0;
|
|
93
|
-
for (let elt of
|
|
128
|
+
for (let elt of tokens) {
|
|
94
129
|
let start = off;
|
|
95
130
|
off += elt.tokenType == Token.Type.Node ? elt.length : 1;
|
|
96
131
|
if (off <= from)
|
|
97
132
|
continue;
|
|
98
133
|
if (start < from || off > to) {
|
|
99
134
|
let inner = elt.sliceInner(Math.max(0, from - start), Math.min(elt.length, to - start));
|
|
100
|
-
for (let elt of inner
|
|
135
|
+
for (let elt of inner)
|
|
101
136
|
result.push(elt);
|
|
102
137
|
}
|
|
103
138
|
else {
|
|
@@ -106,55 +141,17 @@ class Slice {
|
|
|
106
141
|
if (off >= to)
|
|
107
142
|
break;
|
|
108
143
|
}
|
|
109
|
-
return
|
|
110
|
-
}
|
|
111
|
-
concat(other) {
|
|
112
|
-
let content = this.content.slice();
|
|
113
|
-
let i = 0;
|
|
114
|
-
if (content.length && other.content.length && other.content[0].tokenType == Token.Type.Node &&
|
|
115
|
-
content[content.length - 1].tokenType == Token.Type.Node) {
|
|
116
|
-
other.content[0].pushTo(content);
|
|
117
|
-
i = 1;
|
|
118
|
-
}
|
|
119
|
-
for (; i < other.content.length; i++)
|
|
120
|
-
content.push(other.content[i]);
|
|
121
|
-
return new Slice(content);
|
|
144
|
+
return result;
|
|
122
145
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
for (let
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
else if (tok.tokenType == Token.Type.Node) {
|
|
132
|
-
if (tok.isLeaf)
|
|
133
|
-
out.serialize(tok);
|
|
134
|
-
else
|
|
135
|
-
tok.iterate(node => !out.serialize(node));
|
|
136
|
-
}
|
|
146
|
+
static eq(a, b) {
|
|
147
|
+
if (a.length != b.length)
|
|
148
|
+
return false;
|
|
149
|
+
for (let i = 0; i < a.length; i++) {
|
|
150
|
+
let tA = a[i], tB = b[i];
|
|
151
|
+
if (tA.tokenType != tB.tokenType || (tA.tokenType != Token.Type.Close && !tA.eq(tB)))
|
|
152
|
+
return false;
|
|
137
153
|
}
|
|
138
|
-
return
|
|
139
|
-
}
|
|
140
|
-
static empty = /*@__PURE__*/(() => new Slice([]))();
|
|
141
|
-
toString() {
|
|
142
|
-
return `<${this.content.join()}>`;
|
|
143
|
-
}
|
|
144
|
-
toJSON() {
|
|
145
|
-
return this.content.map(e => e.tokenType == Token.Type.Close ? "." : e.toJSON());
|
|
146
|
-
}
|
|
147
|
-
static fromJSON(schema, json) {
|
|
148
|
-
if (!Array.isArray(json))
|
|
149
|
-
throw new ValidationError("Invalid slice JSON");
|
|
150
|
-
return new Slice(json.map(value => {
|
|
151
|
-
if (value === ".")
|
|
152
|
-
return Token.End;
|
|
153
|
-
if (!value || typeof value.type != "string")
|
|
154
|
-
throw new ValidationError("Invalid slice JSON");
|
|
155
|
-
let type = schema.getNode(value.type);
|
|
156
|
-
return type?.isLeaf || ("content" in value) ? schema.nodeFromJSON(value) : schema.tagFromJSON(value);
|
|
157
|
-
}));
|
|
154
|
+
return true;
|
|
158
155
|
}
|
|
159
156
|
}
|
|
160
157
|
|
|
@@ -415,7 +412,7 @@ const Attributes = /*@__PURE__*/(function (Attributes) {
|
|
|
415
412
|
score--;
|
|
416
413
|
iA += 2;
|
|
417
414
|
}
|
|
418
|
-
else if (iB < b.length
|
|
415
|
+
else if (iB < b.length) {
|
|
419
416
|
score--;
|
|
420
417
|
iB += 2;
|
|
421
418
|
}
|
|
@@ -870,8 +867,8 @@ class Pos {
|
|
|
870
867
|
let d = this.depth;
|
|
871
868
|
if (depth > d)
|
|
872
869
|
throw new RangeError("Asking for parent deeper than position depth");
|
|
873
|
-
for (let
|
|
874
|
-
if (d == depth)
|
|
870
|
+
for (let p = this.parent;; p = p.parent)
|
|
871
|
+
if (d-- == depth)
|
|
875
872
|
return p;
|
|
876
873
|
}
|
|
877
874
|
isAtStart(parent) {
|
|
@@ -1096,6 +1093,7 @@ class BaseType {
|
|
|
1096
1093
|
get isBlock() { return (this.flags & 1) == 0; }
|
|
1097
1094
|
get isAtom() { return (this.flags & 4) > 0; }
|
|
1098
1095
|
get isSelectable() { return (this.flags & 32) > 0; }
|
|
1096
|
+
toString() { return this.name; }
|
|
1099
1097
|
}
|
|
1100
1098
|
class BaseTag {
|
|
1101
1099
|
param;
|
|
@@ -1134,29 +1132,48 @@ const Node = /*@__PURE__*/(function (Node) {
|
|
|
1134
1132
|
})(Node.Type || (Node.Type = {}));
|
|
1135
1133
|
class Group {
|
|
1136
1134
|
parent;
|
|
1135
|
+
name;
|
|
1137
1136
|
constructor(
|
|
1138
|
-
parent
|
|
1137
|
+
parent,
|
|
1138
|
+
name) {
|
|
1139
1139
|
this.parent = parent;
|
|
1140
|
+
this.name = name;
|
|
1140
1141
|
}
|
|
1141
|
-
static define(
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
static
|
|
1145
|
-
static
|
|
1146
|
-
static
|
|
1147
|
-
static
|
|
1148
|
-
static
|
|
1149
|
-
static
|
|
1150
|
-
static
|
|
1142
|
+
static define(spec = {}) {
|
|
1143
|
+
return new Group(spec.parent, spec.name || null);
|
|
1144
|
+
}
|
|
1145
|
+
static All = Group.define({ name: "All" });
|
|
1146
|
+
static Inline = Group.define({ name: "Inline" });
|
|
1147
|
+
static Block = Group.define({ name: "Block" });
|
|
1148
|
+
static Leaf = Group.define({ name: "Leaf" });
|
|
1149
|
+
static Plot = Group.define({ name: "Plot" });
|
|
1150
|
+
static Textblock = Group.define({ name: "Textblock" });
|
|
1151
|
+
static Content = Group.define({ name: "Content" });
|
|
1152
|
+
static TableCell = Group.define({ name: "TableCell" });
|
|
1153
|
+
static ListItem = Group.define({ name: "ListItem" });
|
|
1151
1154
|
static builtin = [Group.All, Group.Inline, Group.Block, Group.Leaf, Group.Plot, Group.Textblock];
|
|
1155
|
+
toString() { return "Node.Group" + (this.name ? ":" + this.name : ""); }
|
|
1152
1156
|
}
|
|
1153
1157
|
Node.Group = Group;
|
|
1158
|
+
function queryToString(q) {
|
|
1159
|
+
if ("and" in q)
|
|
1160
|
+
return `and(${q.and.map(queryToString)})`;
|
|
1161
|
+
if (Array.isArray(q))
|
|
1162
|
+
return `or(q.map(queryToString))`;
|
|
1163
|
+
return String(q);
|
|
1164
|
+
}
|
|
1165
|
+
Node.queryToString = queryToString;
|
|
1154
1166
|
class Role {
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1167
|
+
name;
|
|
1168
|
+
constructor(
|
|
1169
|
+
name) {
|
|
1170
|
+
this.name = name;
|
|
1171
|
+
}
|
|
1172
|
+
static define(name) { return new Role(name || null); }
|
|
1173
|
+
static Code = Role.define("Code");
|
|
1174
|
+
static List = Role.define("List");
|
|
1175
|
+
static LineBreak = Role.define("LineBreak");
|
|
1176
|
+
toString() { return "Node.Role" + (this.name ? ":" + this.name : ""); }
|
|
1160
1177
|
}
|
|
1161
1178
|
Node.Role = Role;
|
|
1162
1179
|
;return Node})({});
|
|
@@ -1198,7 +1215,7 @@ class Leaf extends BaseTag {
|
|
|
1198
1215
|
nodes.push(this);
|
|
1199
1216
|
}
|
|
1200
1217
|
sliceInner(from, to) {
|
|
1201
|
-
return from == to ? Slice.empty :
|
|
1218
|
+
return from == to ? Slice.empty.content : [this.is(Leaf.Text) ? this.sliceText(from, to) : this];
|
|
1202
1219
|
}
|
|
1203
1220
|
sliceText(from, to) {
|
|
1204
1221
|
if (!this.is(Leaf.Text))
|
|
@@ -1210,6 +1227,8 @@ class Leaf extends BaseTag {
|
|
|
1210
1227
|
return Leaf.Text.of(this.param.slice(Math.max(from, 0), Math.max(0, to)), this.marks);
|
|
1211
1228
|
}
|
|
1212
1229
|
static text(text, marks = Mark.none) {
|
|
1230
|
+
if (!text)
|
|
1231
|
+
throw new Error("Cannot create empty text nodes");
|
|
1213
1232
|
return Leaf.Text.of(text, marks);
|
|
1214
1233
|
}
|
|
1215
1234
|
toString() {
|
|
@@ -1270,10 +1289,10 @@ class Plot {
|
|
|
1270
1289
|
}
|
|
1271
1290
|
sliceInner(from, to) {
|
|
1272
1291
|
if (from == to)
|
|
1273
|
-
return Slice.empty;
|
|
1292
|
+
return Slice.empty.content;
|
|
1274
1293
|
let content = [];
|
|
1275
1294
|
this.slicePlot(content, from, to);
|
|
1276
|
-
return
|
|
1295
|
+
return content;
|
|
1277
1296
|
}
|
|
1278
1297
|
slicePlot(out, from, to) {
|
|
1279
1298
|
if (from <= 0) {
|
|
@@ -1393,6 +1412,8 @@ class Plot {
|
|
|
1393
1412
|
constructor(type, param, marks) {
|
|
1394
1413
|
super(param, marks);
|
|
1395
1414
|
this.type = type;
|
|
1415
|
+
if (this.type.isDoc && marks.length)
|
|
1416
|
+
throw new Error("Document nodes cannot have marks");
|
|
1396
1417
|
}
|
|
1397
1418
|
static new(type, param, marks) {
|
|
1398
1419
|
return new Tag(type, param, marks);
|
|
@@ -1953,6 +1974,7 @@ class BuildContext {
|
|
|
1953
1974
|
this.parent = parent;
|
|
1954
1975
|
}
|
|
1955
1976
|
}
|
|
1977
|
+
const noTokens = /*@__PURE__*/(() => Slice.empty.content)();
|
|
1956
1978
|
class Builder {
|
|
1957
1979
|
stack;
|
|
1958
1980
|
modifications = null;
|
|
@@ -1997,11 +2019,9 @@ class Builder {
|
|
|
1997
2019
|
return this.schema.doc(this.stack.children);
|
|
1998
2020
|
}
|
|
1999
2021
|
}
|
|
2000
|
-
function isAdd(m) { return !!m.add; }
|
|
2001
|
-
function isRemove(m) { return !!m.remove; }
|
|
2002
2022
|
function applyModifications(modifications, marks, type) {
|
|
2003
2023
|
for (const m of modifications) {
|
|
2004
|
-
if (
|
|
2024
|
+
if ("add" in m) {
|
|
2005
2025
|
marks = m.add.addToSet(marks);
|
|
2006
2026
|
}
|
|
2007
2027
|
else {
|
|
@@ -2011,7 +2031,7 @@ function applyModifications(modifications, marks, type) {
|
|
|
2011
2031
|
return marks;
|
|
2012
2032
|
}
|
|
2013
2033
|
function modificationToJSON(m) {
|
|
2014
|
-
return
|
|
2034
|
+
return "add" in m ? { add: m.add.name, value: m.add.value } : { remove: m.remove.name, value: m.remove.value };
|
|
2015
2035
|
}
|
|
2016
2036
|
function modificationFromJSON(schema, json) {
|
|
2017
2037
|
let { add, remove } = json;
|
|
@@ -2036,7 +2056,7 @@ function compareModifications(a, b) {
|
|
|
2036
2056
|
return true;
|
|
2037
2057
|
}
|
|
2038
2058
|
function compareModification(a, b) {
|
|
2039
|
-
return
|
|
2059
|
+
return "add" in a ? "add" in b && a.add.eq(b.add) : "remove" in b && a.remove.eq(b.remove);
|
|
2040
2060
|
}
|
|
2041
2061
|
function isNatNum(value) {
|
|
2042
2062
|
return typeof value == "number" && Math.floor(value) == value && value >= 0;
|
|
@@ -2081,7 +2101,7 @@ class ChangeSet {
|
|
|
2081
2101
|
return false;
|
|
2082
2102
|
for (let i = 0; i < this.data.length; i++) {
|
|
2083
2103
|
let a = this.data[i], b = other.data[i];
|
|
2084
|
-
if (a && !(this.sections[(i << 1) + 1] < 0 ? compareModifications(a, b) :
|
|
2104
|
+
if (a && !(this.sections[(i << 1) + 1] < 0 ? compareModifications(a, b) : Slice.eq(a, b)))
|
|
2085
2105
|
return false;
|
|
2086
2106
|
}
|
|
2087
2107
|
return true;
|
|
@@ -2105,7 +2125,7 @@ class ChangeSet {
|
|
|
2105
2125
|
}
|
|
2106
2126
|
else {
|
|
2107
2127
|
cursor = cursor.advance(lenA);
|
|
2108
|
-
this.data[i]
|
|
2128
|
+
Slice.run(this.data[i], builder);
|
|
2109
2129
|
}
|
|
2110
2130
|
}
|
|
2111
2131
|
if (cursor.pos != doc.length)
|
|
@@ -2123,7 +2143,7 @@ class ChangeSet {
|
|
|
2123
2143
|
else if (ins == -2)
|
|
2124
2144
|
result.push([len, this.data[i].map(modificationToJSON)]);
|
|
2125
2145
|
else
|
|
2126
|
-
result.push([len, this.data[i].toJSON()]);
|
|
2146
|
+
result.push([len, this.data[i].map(t => t.toJSON())]);
|
|
2127
2147
|
}
|
|
2128
2148
|
return result;
|
|
2129
2149
|
}
|
|
@@ -2145,9 +2165,11 @@ class ChangeSet {
|
|
|
2145
2165
|
data.push(val.map(m => modificationFromJSON(schema, m)));
|
|
2146
2166
|
}
|
|
2147
2167
|
else {
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2168
|
+
if (!Array.isArray(val))
|
|
2169
|
+
throw new ValidationError("Invalid ChangeSet JSON");
|
|
2170
|
+
let tokens = val.map(v => Token.fromJSON(schema, v));
|
|
2171
|
+
sections.push(len, sliceLen(tokens));
|
|
2172
|
+
data.push(tokens);
|
|
2151
2173
|
}
|
|
2152
2174
|
}
|
|
2153
2175
|
}
|
|
@@ -2232,7 +2254,7 @@ class ChangeSet {
|
|
|
2232
2254
|
this.iterChanges((_f, _t, pos, _to, inserted) => {
|
|
2233
2255
|
if (found != null)
|
|
2234
2256
|
return;
|
|
2235
|
-
for (let tok of inserted
|
|
2257
|
+
for (let tok of inserted) {
|
|
2236
2258
|
if (tok.tokenType == Token.Type.Node) {
|
|
2237
2259
|
if (pred(tok.tag))
|
|
2238
2260
|
return found = pos;
|
|
@@ -2371,7 +2393,7 @@ class ChangeSet {
|
|
|
2371
2393
|
}
|
|
2372
2394
|
else if (data) {
|
|
2373
2395
|
text += `[${data.map(mod => {
|
|
2374
|
-
return `${
|
|
2396
|
+
return `${"add" in mod ? "+" + mod.add : "-" + mod.remove}`;
|
|
2375
2397
|
})}]`;
|
|
2376
2398
|
}
|
|
2377
2399
|
if (text)
|
|
@@ -2397,6 +2419,39 @@ class ChangeSetBuilder {
|
|
|
2397
2419
|
sections = [];
|
|
2398
2420
|
data = [];
|
|
2399
2421
|
pos = 0;
|
|
2422
|
+
addSection(from, to, ins, value) {
|
|
2423
|
+
if (from >= this.pos) {
|
|
2424
|
+
addSection(this.sections, this.data, from - this.pos, -1, null);
|
|
2425
|
+
addSection(this.sections, this.data, to - from, ins, value);
|
|
2426
|
+
this.pos = to;
|
|
2427
|
+
return true;
|
|
2428
|
+
}
|
|
2429
|
+
if (this.sections.length && this.sections[0] > to && this.sections[1] == -1) {
|
|
2430
|
+
this.spliceSection(0, from, to, ins, value);
|
|
2431
|
+
return true;
|
|
2432
|
+
}
|
|
2433
|
+
for (let pos = this.pos, i = this.sections.length; i > 2 && pos > to;) {
|
|
2434
|
+
let nextIns = this.sections[--i], nextLen = this.sections[--i];
|
|
2435
|
+
let start = pos - nextLen;
|
|
2436
|
+
if (nextIns == -1 && start < from) {
|
|
2437
|
+
this.spliceSection(i, from - start, to - start, ins, value);
|
|
2438
|
+
return true;
|
|
2439
|
+
}
|
|
2440
|
+
pos = start;
|
|
2441
|
+
}
|
|
2442
|
+
return false;
|
|
2443
|
+
}
|
|
2444
|
+
spliceSection(i, from, to, ins, value) {
|
|
2445
|
+
let sections = [], data = [];
|
|
2446
|
+
addSection(sections, data, from, -1, null);
|
|
2447
|
+
addSection(sections, data, to - from, ins, value);
|
|
2448
|
+
addSection(sections, data, this.sections[i] - to, -1, null);
|
|
2449
|
+
this.sections.splice(i, 2, ...sections);
|
|
2450
|
+
this.data.splice(i >> 1, 1, ...data);
|
|
2451
|
+
}
|
|
2452
|
+
}
|
|
2453
|
+
function sliceLen(tokens) {
|
|
2454
|
+
return tokens.reduce((l, e) => l + (e.tokenType == Token.Type.Node ? e.length : 1), 0);
|
|
2400
2455
|
}
|
|
2401
2456
|
function createChangeSet(doc, spec, mayCorrect = true) {
|
|
2402
2457
|
let cur = null;
|
|
@@ -2414,14 +2469,11 @@ function createChangeSet(doc, spec, mayCorrect = true) {
|
|
|
2414
2469
|
accum = accum ? accum.compose(transform(set, accum, doc, false, false).set) : set;
|
|
2415
2470
|
};
|
|
2416
2471
|
let section = (from, to, ins, value) => {
|
|
2417
|
-
if (!cur
|
|
2472
|
+
if (!cur?.addSection(from, to, ins, value)) {
|
|
2418
2473
|
flush();
|
|
2419
2474
|
cur = new ChangeSetBuilder(doc.length);
|
|
2475
|
+
cur.addSection(from, to, ins, value);
|
|
2420
2476
|
}
|
|
2421
|
-
if (from > cur.pos)
|
|
2422
|
-
addSection(cur.sections, cur.data, from - cur.pos, -1, null);
|
|
2423
|
-
addSection(cur.sections, cur.data, to - from, ins, value);
|
|
2424
|
-
cur.pos = to;
|
|
2425
2477
|
};
|
|
2426
2478
|
let build = (spec) => {
|
|
2427
2479
|
if (Array.isArray(spec)) {
|
|
@@ -2471,17 +2523,20 @@ function createChangeSet(doc, spec, mayCorrect = true) {
|
|
|
2471
2523
|
if (remove) {
|
|
2472
2524
|
let mods = [{ remove }];
|
|
2473
2525
|
markableSections(doc, from, to, remove.type.spanning, (node, from, to) => {
|
|
2474
|
-
const has = remove.isInSet(node.tag.marks);
|
|
2475
|
-
if (!has || !doc.schema.markAllowed(remove.type, node.type))
|
|
2476
|
-
return false;
|
|
2477
|
-
let modsHere = mods;
|
|
2478
2526
|
if (remove.type.set) {
|
|
2479
|
-
let
|
|
2527
|
+
let has = remove.type.isInSet(node.tag.marks);
|
|
2528
|
+
if (!has)
|
|
2529
|
+
return false;
|
|
2530
|
+
let left = remove.value.filter(a => has.value.some(b => remove.type.set(a, b) == 0));
|
|
2480
2531
|
if (!left.length)
|
|
2481
2532
|
return false;
|
|
2482
|
-
|
|
2533
|
+
section(from, to, -2, left.length == remove.value.length ? mods : [{ remove: remove.type.of(left) }]);
|
|
2534
|
+
}
|
|
2535
|
+
else {
|
|
2536
|
+
if (!remove.isInSet(node.tag.marks))
|
|
2537
|
+
return false;
|
|
2538
|
+
section(from, to, -2, mods);
|
|
2483
2539
|
}
|
|
2484
|
-
section(from, to, -2, modsHere);
|
|
2485
2540
|
return true;
|
|
2486
2541
|
});
|
|
2487
2542
|
}
|
|
@@ -2489,16 +2544,17 @@ function createChangeSet(doc, spec, mayCorrect = true) {
|
|
|
2489
2544
|
else {
|
|
2490
2545
|
if (to == null)
|
|
2491
2546
|
to = from;
|
|
2492
|
-
|
|
2547
|
+
let context = insert instanceof Slice ? insert.context : Slice.empty.context;
|
|
2548
|
+
insert = !insert ? noTokens : insert instanceof Slice ? insert.content : insert;
|
|
2493
2549
|
if (to <= from)
|
|
2494
2550
|
to = from;
|
|
2495
2551
|
if (fit) {
|
|
2496
2552
|
doCorrect = true;
|
|
2497
|
-
({ from, to,
|
|
2498
|
-
fitReplacement(doc, doc.resolve(from), doc.resolve(to), insert,
|
|
2553
|
+
({ from, to, tokens: insert } =
|
|
2554
|
+
fitReplacement(doc, doc.resolve(from), doc.resolve(to), insert, context));
|
|
2499
2555
|
}
|
|
2500
2556
|
if (insert.length || to != from)
|
|
2501
|
-
section(from, to, insert
|
|
2557
|
+
section(from, to, sliceLen(insert), insert);
|
|
2502
2558
|
}
|
|
2503
2559
|
}
|
|
2504
2560
|
};
|
|
@@ -2527,15 +2583,15 @@ function transform(setA, setB, doc, before, fit) {
|
|
|
2527
2583
|
let end = pos + b.len;
|
|
2528
2584
|
addSection(sections, data, b.ins, -1, null);
|
|
2529
2585
|
if (fitter)
|
|
2530
|
-
fitter.replaced(b.
|
|
2586
|
+
fitter.replaced(b.tokens, pos, end, true);
|
|
2531
2587
|
while (pos < end) {
|
|
2532
2588
|
if (a.done)
|
|
2533
2589
|
throw new ValidationError("Mismatched change sets");
|
|
2534
2590
|
let piece = Math.min(a.len, end - pos);
|
|
2535
2591
|
if (a.ins >= 0 && inserted < a.i && a.len <= piece) {
|
|
2536
|
-
addSection(sections, data, 0, a.ins, a.
|
|
2592
|
+
addSection(sections, data, 0, a.ins, a.tokens);
|
|
2537
2593
|
if (fitter)
|
|
2538
|
-
fitter.replaced(a.
|
|
2594
|
+
fitter.replaced(a.tokens, pos - a.off, pos + a.len);
|
|
2539
2595
|
inserted = a.i;
|
|
2540
2596
|
}
|
|
2541
2597
|
a.forward(piece);
|
|
@@ -2554,7 +2610,7 @@ function transform(setA, setB, doc, before, fit) {
|
|
|
2554
2610
|
}
|
|
2555
2611
|
else if (b.ins == 0 && pos + b.len < end) {
|
|
2556
2612
|
if (fitter)
|
|
2557
|
-
fitter.replaced(b.
|
|
2613
|
+
fitter.replaced(b.tokens, pos, pos + b.len, true);
|
|
2558
2614
|
pos += b.len;
|
|
2559
2615
|
b.next();
|
|
2560
2616
|
}
|
|
@@ -2563,13 +2619,13 @@ function transform(setA, setB, doc, before, fit) {
|
|
|
2563
2619
|
}
|
|
2564
2620
|
}
|
|
2565
2621
|
if (inserted < a.i) {
|
|
2566
|
-
addSection(sections, data, len, a.ins, a.
|
|
2622
|
+
addSection(sections, data, len, a.ins, a.tokens);
|
|
2567
2623
|
if (fitter)
|
|
2568
|
-
fitter.replaced(a.
|
|
2624
|
+
fitter.replaced(a.tokens, start - a.off, start + a.len);
|
|
2569
2625
|
inserted = a.i;
|
|
2570
2626
|
}
|
|
2571
2627
|
else {
|
|
2572
|
-
addSection(sections, data, len, 0,
|
|
2628
|
+
addSection(sections, data, len, 0, noTokens);
|
|
2573
2629
|
}
|
|
2574
2630
|
a.forward(pos - start);
|
|
2575
2631
|
}
|
|
@@ -2588,10 +2644,10 @@ function compose(sectionsA, sectionsB, dataA, dataB) {
|
|
|
2588
2644
|
if (a.done && b.done) {
|
|
2589
2645
|
return { sections, data };
|
|
2590
2646
|
}
|
|
2591
|
-
else if (a.ins == 0) { addSection(sections, data, a.len, 0, a.
|
|
2647
|
+
else if (a.ins == 0) { addSection(sections, data, a.len, 0, a.tokens, open);
|
|
2592
2648
|
a.next();
|
|
2593
2649
|
}
|
|
2594
|
-
else if (b.len == 0 && !b.done) { addSection(sections, data, 0, b.ins, b.
|
|
2650
|
+
else if (b.len == 0 && !b.done) { addSection(sections, data, 0, b.ins, b.tokens, open);
|
|
2595
2651
|
b.next();
|
|
2596
2652
|
}
|
|
2597
2653
|
else if (a.done || b.done) {
|
|
@@ -2604,13 +2660,13 @@ function compose(sectionsA, sectionsB, dataA, dataB) {
|
|
|
2604
2660
|
addSection(sections, data, len, (data ? mods : a.ins == -2 || b.ins == -2) ? -2 : -1, mods, open);
|
|
2605
2661
|
}
|
|
2606
2662
|
else if (a.keep) {
|
|
2607
|
-
addSection(sections, data, len, b.off ? 0 : b.ins, b.off ?
|
|
2663
|
+
addSection(sections, data, len, b.off ? 0 : b.ins, b.off ? noTokens : b.tokens, open);
|
|
2608
2664
|
}
|
|
2609
2665
|
else if (b.keep) {
|
|
2610
2666
|
addSection(sections, data, a.off ? 0 : a.len, len, data ? applyModsToSlice(a.slicePart(len), b.mods) : null, open);
|
|
2611
2667
|
}
|
|
2612
2668
|
else {
|
|
2613
|
-
addSection(sections, data, a.off ? 0 : a.len, b.off ? 0 : b.ins, b.off ?
|
|
2669
|
+
addSection(sections, data, a.off ? 0 : a.len, b.off ? 0 : b.ins, b.off ? noTokens : b.tokens, open);
|
|
2614
2670
|
}
|
|
2615
2671
|
open = (a.ins > len || b.ins >= 0 && b.len > len) && (open || sections.length > sectionLen);
|
|
2616
2672
|
a.forward2(len);
|
|
@@ -2627,16 +2683,16 @@ function filterMods(mods, against) {
|
|
|
2627
2683
|
return mods.filter(m => !against.some(a => modCancels(a, m)));
|
|
2628
2684
|
}
|
|
2629
2685
|
function modCancels(mod, other) {
|
|
2630
|
-
if (
|
|
2631
|
-
return
|
|
2686
|
+
if ("add" in other) {
|
|
2687
|
+
return "add" in mod ? mod.add.type == other.add.type && !mod.add.type.set : mod.remove.eq(other.add);
|
|
2632
2688
|
}
|
|
2633
2689
|
else {
|
|
2634
|
-
return
|
|
2690
|
+
return "add" in mod && mod.add.eq(other.remove);
|
|
2635
2691
|
}
|
|
2636
2692
|
}
|
|
2637
2693
|
function invertMods(mods, target) {
|
|
2638
2694
|
return mods.map(mod => {
|
|
2639
|
-
if (
|
|
2695
|
+
if ("remove" in mod)
|
|
2640
2696
|
return { add: mod.remove };
|
|
2641
2697
|
if (!mod.add.type.set) {
|
|
2642
2698
|
let existed = mod.add.type.isInSet(target.marks);
|
|
@@ -2646,11 +2702,11 @@ function invertMods(mods, target) {
|
|
|
2646
2702
|
return { remove: mod.add };
|
|
2647
2703
|
});
|
|
2648
2704
|
}
|
|
2649
|
-
function applyModsToSlice(
|
|
2705
|
+
function applyModsToSlice(tokens, mods) {
|
|
2650
2706
|
if (!mods)
|
|
2651
|
-
return
|
|
2707
|
+
return tokens;
|
|
2652
2708
|
let content = [];
|
|
2653
|
-
for (let tok of
|
|
2709
|
+
for (let tok of tokens) {
|
|
2654
2710
|
if (tok.tokenType == Token.Type.Open) {
|
|
2655
2711
|
content.push(tok.withMarks(applyModifications(mods, tok.marks, tok.type)));
|
|
2656
2712
|
}
|
|
@@ -2665,7 +2721,7 @@ function applyModsToSlice(slice, mods) {
|
|
|
2665
2721
|
content.push(tok);
|
|
2666
2722
|
}
|
|
2667
2723
|
}
|
|
2668
|
-
return
|
|
2724
|
+
return content;
|
|
2669
2725
|
}
|
|
2670
2726
|
class FitLevel {
|
|
2671
2727
|
tag;
|
|
@@ -2740,13 +2796,13 @@ class ChangeFitter {
|
|
|
2740
2796
|
lastCoverFrom = -1;
|
|
2741
2797
|
lastCoverTo = -1;
|
|
2742
2798
|
doubleDeleteDelta = 0;
|
|
2743
|
-
replaced(
|
|
2799
|
+
replaced(tokens, from, to, covering = false) {
|
|
2744
2800
|
this.doubleDeleteDelta = 0;
|
|
2745
2801
|
if (covering) {
|
|
2746
2802
|
this.lastCoverFrom = from;
|
|
2747
2803
|
this.lastCoverTo = to;
|
|
2748
2804
|
}
|
|
2749
|
-
else if (
|
|
2805
|
+
else if (tokens.length) {
|
|
2750
2806
|
let overlapFrom = Math.max(from, this.lastCoverFrom);
|
|
2751
2807
|
let overlapTo = Math.min(to, this.lastCoverTo);
|
|
2752
2808
|
if (overlapFrom < overlapTo) {
|
|
@@ -2759,7 +2815,7 @@ class ChangeFitter {
|
|
|
2759
2815
|
this.inputDelta -= counter.count;
|
|
2760
2816
|
}
|
|
2761
2817
|
this.inserting = true;
|
|
2762
|
-
|
|
2818
|
+
Slice.run(tokens, this, this.pos);
|
|
2763
2819
|
this.inserting = false;
|
|
2764
2820
|
if (this.local)
|
|
2765
2821
|
this.nextSync = Math.max(this.nextSync, localSyncPosAfter(this.inputPos = this.getPos(to)));
|
|
@@ -2922,8 +2978,7 @@ class ChangeFitter {
|
|
|
2922
2978
|
let sections = [], data = [], pos = 0;
|
|
2923
2979
|
for (let { from, to, insert } of this.patches) {
|
|
2924
2980
|
addSection(sections, data, from - pos, -1, null);
|
|
2925
|
-
|
|
2926
|
-
addSection(sections, data, to - from, slice.length, slice);
|
|
2981
|
+
addSection(sections, data, to - from, sliceLen(insert), insert);
|
|
2927
2982
|
pos = to;
|
|
2928
2983
|
}
|
|
2929
2984
|
addSection(sections, data, this.pos - pos, -1, null);
|
|
@@ -2979,11 +3034,11 @@ class SectionIter {
|
|
|
2979
3034
|
get mods() {
|
|
2980
3035
|
return this.data ? this.data[(this.i - 2) >> 1] : null;
|
|
2981
3036
|
}
|
|
2982
|
-
get
|
|
2983
|
-
return this.data ? this.data[(this.i - 2) >> 1] :
|
|
3037
|
+
get tokens() {
|
|
3038
|
+
return this.data ? this.data[(this.i - 2) >> 1] : noTokens;
|
|
2984
3039
|
}
|
|
2985
3040
|
slicePart(len) {
|
|
2986
|
-
return
|
|
3041
|
+
return Slice.slice(this.tokens, this.off, len == null ? undefined : this.off + len);
|
|
2987
3042
|
}
|
|
2988
3043
|
forward(len) {
|
|
2989
3044
|
if (len == this.len)
|
|
@@ -3022,7 +3077,7 @@ function addSection(sections, data, len, ins, value, forceJoin = false) {
|
|
|
3022
3077
|
sections[last] += len;
|
|
3023
3078
|
sections[last + 1] += ins;
|
|
3024
3079
|
if (data)
|
|
3025
|
-
data[data.length - 1] = data[data.length - 1]
|
|
3080
|
+
data[data.length - 1] = Slice.concat(data[data.length - 1], value);
|
|
3026
3081
|
}
|
|
3027
3082
|
else {
|
|
3028
3083
|
sections.push(len, ins);
|
|
@@ -3034,11 +3089,11 @@ function finishCx(cx, schema) {
|
|
|
3034
3089
|
return cx.tag.create(cx.children.length || cx.tag.type.canBeEmpty ? cx.children
|
|
3035
3090
|
: [schema.createDefault(cx.tag.type)]);
|
|
3036
3091
|
}
|
|
3037
|
-
function closeSlice(schema,
|
|
3092
|
+
function closeSlice(schema, tokens, context, depth, closeEnd = false) {
|
|
3038
3093
|
let top = [], stack = null;
|
|
3039
3094
|
for (let i = depth - 1; i >= 0; i--)
|
|
3040
3095
|
stack = new BuildContext(context[i], stack);
|
|
3041
|
-
for (let token of
|
|
3096
|
+
for (let token of tokens) {
|
|
3042
3097
|
if (token.tokenType == Token.Type.Close) {
|
|
3043
3098
|
if (stack) {
|
|
3044
3099
|
let node = finishCx(stack, schema);
|
|
@@ -3064,7 +3119,7 @@ function closeSlice(schema, slice, context, depth, closeEnd = false) {
|
|
|
3064
3119
|
}
|
|
3065
3120
|
if (stack)
|
|
3066
3121
|
splatContext(top, stack);
|
|
3067
|
-
return
|
|
3122
|
+
return top;
|
|
3068
3123
|
}
|
|
3069
3124
|
function splatContext(top, cx) {
|
|
3070
3125
|
if (cx.parent)
|
|
@@ -3076,8 +3131,8 @@ function splatContext(top, cx) {
|
|
|
3076
3131
|
function isFitBarrier(plot) {
|
|
3077
3132
|
return plot.isolating || plot.isInline;
|
|
3078
3133
|
}
|
|
3079
|
-
function fitReplacement(doc, from, to,
|
|
3080
|
-
if (!
|
|
3134
|
+
function fitReplacement(doc, from, to, tokens, context) {
|
|
3135
|
+
if (!tokens.length)
|
|
3081
3136
|
return fitDeletion(doc, from, to);
|
|
3082
3137
|
let preferredContext = -1;
|
|
3083
3138
|
for (let i = 0; i < context.length; i++) {
|
|
@@ -3088,8 +3143,8 @@ function fitReplacement(doc, from, to, slice, context) {
|
|
|
3088
3143
|
break;
|
|
3089
3144
|
}
|
|
3090
3145
|
let firstType = null, closeCount = 0;
|
|
3091
|
-
for (let i = 0, opened = 0; i <
|
|
3092
|
-
let tok =
|
|
3146
|
+
for (let i = 0, opened = 0; i < tokens.length; i++) {
|
|
3147
|
+
let tok = tokens[i];
|
|
3093
3148
|
if (tok.tokenType == Token.Type.Close) {
|
|
3094
3149
|
if (opened)
|
|
3095
3150
|
opened--;
|
|
@@ -3134,7 +3189,7 @@ function fitReplacement(doc, from, to, slice, context) {
|
|
|
3134
3189
|
let cost = (neutral ? 0 : 2) + (i < preferredContext ? context.length - i : i - preferredContext) + (toEnd ? 0 : 1e7);
|
|
3135
3190
|
if (foundCost > cost) {
|
|
3136
3191
|
found = { from: cxFrom.before, to: toEnd ? cxTo.after : to.pos,
|
|
3137
|
-
|
|
3192
|
+
tokens: i >= 0 ? closeSlice(doc.schema, tokens, context, i + 1, toEnd) : tokens };
|
|
3138
3193
|
foundCost = cost;
|
|
3139
3194
|
}
|
|
3140
3195
|
}
|
|
@@ -3151,7 +3206,7 @@ function fitReplacement(doc, from, to, slice, context) {
|
|
|
3151
3206
|
continue;
|
|
3152
3207
|
if (doc.schema.canContain(cx.node.type, type)) {
|
|
3153
3208
|
let pos = before > -1 ? before : after;
|
|
3154
|
-
return { from: pos, to: pos,
|
|
3209
|
+
return { from: pos, to: pos, tokens: i >= 0 ? closeSlice(doc.schema, tokens, context, i + 1, true) : tokens };
|
|
3155
3210
|
}
|
|
3156
3211
|
}
|
|
3157
3212
|
if (isFitBarrier(cx.node.type) || !cx.parent)
|
|
@@ -3166,11 +3221,11 @@ function fitReplacement(doc, from, to, slice, context) {
|
|
|
3166
3221
|
continue;
|
|
3167
3222
|
let depth = i < 0 ? preferredContext : i;
|
|
3168
3223
|
if (doc.schema.canContain(from.parent.node.type, context[depth].type)) {
|
|
3169
|
-
|
|
3224
|
+
tokens = closeSlice(doc.schema, tokens, context, depth + 1, true);
|
|
3170
3225
|
break;
|
|
3171
3226
|
}
|
|
3172
3227
|
}
|
|
3173
|
-
return { from: from.pos, to: to.pos,
|
|
3228
|
+
return { from: from.pos, to: to.pos, tokens };
|
|
3174
3229
|
}
|
|
3175
3230
|
function fitDeletion(doc, from, to) {
|
|
3176
3231
|
let toDepth = to.depth;
|
|
@@ -3184,12 +3239,12 @@ function fitDeletion(doc, from, to) {
|
|
|
3184
3239
|
end++;
|
|
3185
3240
|
}
|
|
3186
3241
|
let toAtEnd = toDepth == depth && cxTo.end == end; if (cx.end < to.pos && cx.parent.end > to.pos && !toAtEnd)
|
|
3187
|
-
return { from: cx.before, to: to.pos,
|
|
3242
|
+
return { from: cx.before, to: to.pos, tokens: noTokens };
|
|
3188
3243
|
if (!cx.node.inlineContent && toAtEnd && cx.parent.start == cxTo.parent.start &&
|
|
3189
3244
|
!(from.parent.start == to.parent.start && from.parent.node.inlineContent))
|
|
3190
|
-
covered = { from: cx.before, to: cxTo.after,
|
|
3245
|
+
covered = { from: cx.before, to: cxTo.after, tokens: noTokens };
|
|
3191
3246
|
}
|
|
3192
|
-
return covered || { from: from.pos, to: to.pos,
|
|
3247
|
+
return covered || { from: from.pos, to: to.pos, tokens: noTokens };
|
|
3193
3248
|
}
|
|
3194
3249
|
|
|
3195
3250
|
function parse(schema, doc, options = {}) {
|
|
@@ -3229,7 +3284,7 @@ function parse(schema, doc, options = {}) {
|
|
|
3229
3284
|
}
|
|
3230
3285
|
};
|
|
3231
3286
|
emitTokens(top.children, true, true);
|
|
3232
|
-
return
|
|
3287
|
+
return Slice.of(tokens, context);
|
|
3233
3288
|
}
|
|
3234
3289
|
parse.slice = slice;
|
|
3235
3290
|
(function (Rule) {
|
|
@@ -3675,12 +3730,12 @@ function serialize(doc, options = {}) {
|
|
|
3675
3730
|
return Elt.Fragment.create(serializeChildren(doc.content, new SerializeContext(options)));
|
|
3676
3731
|
}
|
|
3677
3732
|
;serialize = /*@__PURE__*/(function (serialize) {
|
|
3678
|
-
function node(node, options) {
|
|
3733
|
+
function node(node, options = {}) {
|
|
3679
3734
|
return serializeChildren([node], new SerializeContext(options))[0];
|
|
3680
3735
|
}
|
|
3681
3736
|
serialize.node = node;
|
|
3682
|
-
function slice(slice, options) {
|
|
3683
|
-
return Elt.Fragment.create(serializeChildren(flattenSlice(slice.content,
|
|
3737
|
+
function slice(slice, options = {}) {
|
|
3738
|
+
return Elt.Fragment.create(serializeChildren(flattenSlice(slice.content, slice.context, options.includeContext || 0, !!options.openAttr), new SerializeContext(options, options.openAttr)));
|
|
3684
3739
|
}
|
|
3685
3740
|
serialize.slice = slice;
|
|
3686
3741
|
;return serialize})(serialize);
|