@jvs-milkdown/utils 1.0.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/LICENSE +21 -0
- package/README.md +9 -0
- package/lib/composable/$command.d.ts +9 -0
- package/lib/composable/$command.d.ts.map +1 -0
- package/lib/composable/$ctx.d.ts +6 -0
- package/lib/composable/$ctx.d.ts.map +1 -0
- package/lib/composable/$input-rule.d.ts +8 -0
- package/lib/composable/$input-rule.d.ts.map +1 -0
- package/lib/composable/$mark.d.ts +11 -0
- package/lib/composable/$mark.d.ts.map +1 -0
- package/lib/composable/$node.d.ts +11 -0
- package/lib/composable/$node.d.ts.map +1 -0
- package/lib/composable/$paste-rule.d.ts +8 -0
- package/lib/composable/$paste-rule.d.ts.map +1 -0
- package/lib/composable/$prose.d.ts +9 -0
- package/lib/composable/$prose.d.ts.map +1 -0
- package/lib/composable/$shortcut.d.ts +10 -0
- package/lib/composable/$shortcut.d.ts.map +1 -0
- package/lib/composable/$view.d.ts +11 -0
- package/lib/composable/$view.d.ts.map +1 -0
- package/lib/composable/composed/$attr.d.ts +7 -0
- package/lib/composable/composed/$attr.d.ts.map +1 -0
- package/lib/composable/composed/$mark-schema.d.ts +18 -0
- package/lib/composable/composed/$mark-schema.d.ts.map +1 -0
- package/lib/composable/composed/$node-schema.d.ts +18 -0
- package/lib/composable/composed/$node-schema.d.ts.map +1 -0
- package/lib/composable/composed/$node-schema.spec.d.ts +2 -0
- package/lib/composable/composed/$node-schema.spec.d.ts.map +1 -0
- package/lib/composable/composed/$remark.d.ts +13 -0
- package/lib/composable/composed/$remark.d.ts.map +1 -0
- package/lib/composable/composed/$use-keymap.d.ts +25 -0
- package/lib/composable/composed/$use-keymap.d.ts.map +1 -0
- package/lib/composable/composed/index.d.ts +6 -0
- package/lib/composable/composed/index.d.ts.map +1 -0
- package/lib/composable/index.d.ts +12 -0
- package/lib/composable/index.d.ts.map +1 -0
- package/lib/composable/utils.d.ts +7 -0
- package/lib/composable/utils.d.ts.map +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +549 -0
- package/lib/index.js.map +1 -0
- package/lib/macro/call-command.d.ts +8 -0
- package/lib/macro/call-command.d.ts.map +1 -0
- package/lib/macro/force-update.d.ts +3 -0
- package/lib/macro/force-update.d.ts.map +1 -0
- package/lib/macro/get-html.d.ts +3 -0
- package/lib/macro/get-html.d.ts.map +1 -0
- package/lib/macro/get-markdown.d.ts +6 -0
- package/lib/macro/get-markdown.d.ts.map +1 -0
- package/lib/macro/index.d.ts +12 -0
- package/lib/macro/index.d.ts.map +1 -0
- package/lib/macro/insert-pos.d.ts +3 -0
- package/lib/macro/insert-pos.d.ts.map +1 -0
- package/lib/macro/insert.d.ts +3 -0
- package/lib/macro/insert.d.ts.map +1 -0
- package/lib/macro/markdown-to-slice.d.ts +3 -0
- package/lib/macro/markdown-to-slice.d.ts.map +1 -0
- package/lib/macro/outline.d.ts +7 -0
- package/lib/macro/outline.d.ts.map +1 -0
- package/lib/macro/replace-all.d.ts +3 -0
- package/lib/macro/replace-all.d.ts.map +1 -0
- package/lib/macro/replace-range.d.ts +6 -0
- package/lib/macro/replace-range.d.ts.map +1 -0
- package/lib/macro/set-attr.d.ts +4 -0
- package/lib/macro/set-attr.d.ts.map +1 -0
- package/lib/pipe.d.ts +7 -0
- package/lib/pipe.d.ts.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +35 -0
- package/src/composable/$command.ts +102 -0
- package/src/composable/$ctx.ts +31 -0
- package/src/composable/$input-rule.ts +55 -0
- package/src/composable/$mark.ts +87 -0
- package/src/composable/$node.ts +90 -0
- package/src/composable/$paste-rule.ts +59 -0
- package/src/composable/$prose.ts +70 -0
- package/src/composable/$shortcut.ts +66 -0
- package/src/composable/$view.ts +110 -0
- package/src/composable/composed/$attr.ts +29 -0
- package/src/composable/composed/$mark-schema.ts +65 -0
- package/src/composable/composed/$node-schema.spec.ts +139 -0
- package/src/composable/composed/$node-schema.ts +65 -0
- package/src/composable/composed/$remark.ts +53 -0
- package/src/composable/composed/$use-keymap.ts +91 -0
- package/src/composable/composed/index.ts +5 -0
- package/src/composable/index.ts +12 -0
- package/src/composable/utils.ts +63 -0
- package/src/index.ts +3 -0
- package/src/macro/call-command.ts +28 -0
- package/src/macro/force-update.ts +14 -0
- package/src/macro/get-html.ts +20 -0
- package/src/macro/get-markdown.ts +27 -0
- package/src/macro/index.ts +11 -0
- package/src/macro/insert-pos.ts +27 -0
- package/src/macro/insert.ts +38 -0
- package/src/macro/markdown-to-slice.ts +18 -0
- package/src/macro/outline.ts +21 -0
- package/src/macro/replace-all.ts +46 -0
- package/src/macro/replace-range.ts +18 -0
- package/src/macro/set-attr.ts +16 -0
- package/src/pipe.ts +72 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
import { CommandsReady, InitReady, KeymapReady, SchemaReady, commandsCtx, commandsTimerCtx, createCmdKey, editorStateOptionsCtx, editorStateTimerCtx, editorViewCtx, editorViewTimerCtx, inputRulesCtx, keymapCtx, markViewCtx, marksCtx, nodeViewCtx, nodesCtx, parserCtx, pasteRulesCtx, pasteRulesTimerCtx, prosePluginsCtx, remarkPluginsCtx, schemaCtx, schemaTimerCtx, serializerCtx } from "@jvs-milkdown/core";
|
|
2
|
+
import { createSlice, createTimer } from "@jvs-milkdown/ctx";
|
|
3
|
+
import { customAlphabet } from "nanoid";
|
|
4
|
+
import { missingMarkInSchema, missingNodeInSchema } from "@jvs-milkdown/exception";
|
|
5
|
+
import { DOMParser, DOMSerializer, NodeType, Slice } from "@jvs-milkdown/prose/model";
|
|
6
|
+
import { isTextOnlySlice } from "@jvs-milkdown/prose";
|
|
7
|
+
import { EditorState } from "@jvs-milkdown/prose/state";
|
|
8
|
+
//#region src/composable/utils.ts
|
|
9
|
+
var nanoid = customAlphabet("abcedfghicklmn", 10);
|
|
10
|
+
function addTimer(runner, injectTo, timerName) {
|
|
11
|
+
const timer = createTimer(timerName || nanoid());
|
|
12
|
+
let doneCalled = false;
|
|
13
|
+
const plugin = (ctx) => {
|
|
14
|
+
ctx.record(timer);
|
|
15
|
+
ctx.update(injectTo, (x) => x.concat(timer));
|
|
16
|
+
return async () => {
|
|
17
|
+
const done = () => {
|
|
18
|
+
ctx.done(timer);
|
|
19
|
+
doneCalled = true;
|
|
20
|
+
};
|
|
21
|
+
const cleanup = await runner(ctx, plugin, done);
|
|
22
|
+
if (!doneCalled) ctx.done(timer);
|
|
23
|
+
return () => {
|
|
24
|
+
ctx.update(injectTo, (x) => x.filter((y) => y !== timer));
|
|
25
|
+
ctx.clearTimer(timer);
|
|
26
|
+
if (cleanup) {
|
|
27
|
+
const result = cleanup();
|
|
28
|
+
if (result && "then" in result) result.catch(console.error);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
plugin.timer = timer;
|
|
34
|
+
return plugin;
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/composable/$command.ts
|
|
38
|
+
function $command(key, cmd) {
|
|
39
|
+
const cmdKey = createCmdKey(key);
|
|
40
|
+
const plugin = (ctx) => async () => {
|
|
41
|
+
plugin.key = cmdKey;
|
|
42
|
+
await ctx.wait(CommandsReady);
|
|
43
|
+
const command = cmd(ctx);
|
|
44
|
+
ctx.get(commandsCtx).create(cmdKey, command);
|
|
45
|
+
plugin.run = (payload) => ctx.get(commandsCtx).call(key, payload);
|
|
46
|
+
return () => {
|
|
47
|
+
ctx.get(commandsCtx).remove(cmdKey);
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
return plugin;
|
|
51
|
+
}
|
|
52
|
+
function $commandAsync(key, cmd, timerName) {
|
|
53
|
+
const cmdKey = createCmdKey(key);
|
|
54
|
+
return addTimer(async (ctx, plugin) => {
|
|
55
|
+
await ctx.wait(CommandsReady);
|
|
56
|
+
const command = await cmd(ctx);
|
|
57
|
+
ctx.get(commandsCtx).create(cmdKey, command);
|
|
58
|
+
plugin.run = (payload) => ctx.get(commandsCtx).call(key, payload);
|
|
59
|
+
plugin.key = cmdKey;
|
|
60
|
+
return () => {
|
|
61
|
+
ctx.get(commandsCtx).remove(cmdKey);
|
|
62
|
+
};
|
|
63
|
+
}, commandsTimerCtx, timerName);
|
|
64
|
+
}
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/composable/$input-rule.ts
|
|
67
|
+
function $inputRule(inputRule) {
|
|
68
|
+
const plugin = (ctx) => async () => {
|
|
69
|
+
await ctx.wait(SchemaReady);
|
|
70
|
+
const ir = inputRule(ctx);
|
|
71
|
+
ctx.update(inputRulesCtx, (irs) => [...irs, ir]);
|
|
72
|
+
plugin.inputRule = ir;
|
|
73
|
+
return () => {
|
|
74
|
+
ctx.update(inputRulesCtx, (irs) => irs.filter((x) => x !== ir));
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
return plugin;
|
|
78
|
+
}
|
|
79
|
+
function $inputRuleAsync(inputRule, timerName) {
|
|
80
|
+
return addTimer(async (ctx, plugin) => {
|
|
81
|
+
await ctx.wait(SchemaReady);
|
|
82
|
+
const ir = await inputRule(ctx);
|
|
83
|
+
ctx.update(inputRulesCtx, (irs) => [...irs, ir]);
|
|
84
|
+
plugin.inputRule = ir;
|
|
85
|
+
return () => {
|
|
86
|
+
ctx.update(inputRulesCtx, (irs) => irs.filter((x) => x !== ir));
|
|
87
|
+
};
|
|
88
|
+
}, editorStateTimerCtx, timerName);
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/composable/$paste-rule.ts
|
|
92
|
+
function $pasteRule(pasteRule) {
|
|
93
|
+
const plugin = (ctx) => async () => {
|
|
94
|
+
await ctx.wait(SchemaReady);
|
|
95
|
+
const pr = pasteRule(ctx);
|
|
96
|
+
ctx.update(pasteRulesCtx, (prs) => [...prs, pr]);
|
|
97
|
+
plugin.pasteRule = pr;
|
|
98
|
+
return () => {
|
|
99
|
+
ctx.update(pasteRulesCtx, (prs) => prs.filter((x) => x !== pr));
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
return plugin;
|
|
103
|
+
}
|
|
104
|
+
function $pasteRuleAsync(pasteRule, timerName) {
|
|
105
|
+
return addTimer(async (ctx, plugin) => {
|
|
106
|
+
await ctx.wait(SchemaReady);
|
|
107
|
+
const pr = await pasteRule(ctx);
|
|
108
|
+
ctx.update(pasteRulesCtx, (prs) => [...prs, pr]);
|
|
109
|
+
plugin.pasteRule = pr;
|
|
110
|
+
return () => {
|
|
111
|
+
ctx.update(pasteRulesCtx, (prs) => prs.filter((x) => x !== pr));
|
|
112
|
+
};
|
|
113
|
+
}, pasteRulesTimerCtx, timerName);
|
|
114
|
+
}
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/composable/$mark.ts
|
|
117
|
+
function $mark(id, schema) {
|
|
118
|
+
const plugin = (ctx) => async () => {
|
|
119
|
+
const markSchema = schema(ctx);
|
|
120
|
+
ctx.update(marksCtx, (ns) => [...ns.filter((n) => n[0] !== id), [id, markSchema]]);
|
|
121
|
+
plugin.id = id;
|
|
122
|
+
plugin.schema = markSchema;
|
|
123
|
+
return () => {
|
|
124
|
+
ctx.update(marksCtx, (ns) => ns.filter(([x]) => x !== id));
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
plugin.type = (ctx) => {
|
|
128
|
+
const markType = ctx.get(schemaCtx).marks[id];
|
|
129
|
+
if (!markType) throw missingMarkInSchema(id);
|
|
130
|
+
return markType;
|
|
131
|
+
};
|
|
132
|
+
return plugin;
|
|
133
|
+
}
|
|
134
|
+
function $markAsync(id, schema, timerName) {
|
|
135
|
+
const plugin = addTimer(async (ctx, plugin, done) => {
|
|
136
|
+
const markSchema = await schema(ctx);
|
|
137
|
+
ctx.update(marksCtx, (ns) => [...ns.filter((n) => n[0] !== id), [id, markSchema]]);
|
|
138
|
+
plugin.id = id;
|
|
139
|
+
plugin.schema = markSchema;
|
|
140
|
+
done();
|
|
141
|
+
return () => {
|
|
142
|
+
ctx.update(marksCtx, (ns) => ns.filter(([x]) => x !== id));
|
|
143
|
+
};
|
|
144
|
+
}, schemaTimerCtx, timerName);
|
|
145
|
+
plugin.type = (ctx) => {
|
|
146
|
+
const markType = ctx.get(schemaCtx).marks[id];
|
|
147
|
+
if (!markType) throw missingMarkInSchema(id);
|
|
148
|
+
return markType;
|
|
149
|
+
};
|
|
150
|
+
return plugin;
|
|
151
|
+
}
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/composable/$node.ts
|
|
154
|
+
function $node(id, schema) {
|
|
155
|
+
const plugin = (ctx) => async () => {
|
|
156
|
+
const nodeSchema = schema(ctx);
|
|
157
|
+
ctx.update(nodesCtx, (ns) => [...ns.filter((n) => n[0] !== id), [id, nodeSchema]]);
|
|
158
|
+
plugin.id = id;
|
|
159
|
+
plugin.schema = nodeSchema;
|
|
160
|
+
return () => {
|
|
161
|
+
ctx.update(nodesCtx, (ns) => ns.filter(([x]) => x !== id));
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
plugin.type = (ctx) => {
|
|
165
|
+
const nodeType = ctx.get(schemaCtx).nodes[id];
|
|
166
|
+
if (!nodeType) throw missingNodeInSchema(id);
|
|
167
|
+
return nodeType;
|
|
168
|
+
};
|
|
169
|
+
return plugin;
|
|
170
|
+
}
|
|
171
|
+
function $nodeAsync(id, schema, timerName) {
|
|
172
|
+
const plugin = addTimer(async (ctx, plugin, done) => {
|
|
173
|
+
const nodeSchema = await schema(ctx);
|
|
174
|
+
ctx.update(nodesCtx, (ns) => [...ns.filter((n) => n[0] !== id), [id, nodeSchema]]);
|
|
175
|
+
plugin.id = id;
|
|
176
|
+
plugin.schema = nodeSchema;
|
|
177
|
+
done();
|
|
178
|
+
return () => {
|
|
179
|
+
ctx.update(nodesCtx, (ns) => ns.filter(([x]) => x !== id));
|
|
180
|
+
};
|
|
181
|
+
}, schemaTimerCtx, timerName);
|
|
182
|
+
plugin.type = (ctx) => {
|
|
183
|
+
const nodeType = ctx.get(schemaCtx).nodes[id];
|
|
184
|
+
if (!nodeType) throw missingNodeInSchema(id);
|
|
185
|
+
return nodeType;
|
|
186
|
+
};
|
|
187
|
+
return plugin;
|
|
188
|
+
}
|
|
189
|
+
//#endregion
|
|
190
|
+
//#region src/composable/$prose.ts
|
|
191
|
+
function $prose(prose) {
|
|
192
|
+
let prosePlugin;
|
|
193
|
+
const plugin = (ctx) => async () => {
|
|
194
|
+
await ctx.wait(SchemaReady);
|
|
195
|
+
prosePlugin = prose(ctx);
|
|
196
|
+
ctx.update(prosePluginsCtx, (ps) => [...ps, prosePlugin]);
|
|
197
|
+
return () => {
|
|
198
|
+
ctx.update(prosePluginsCtx, (ps) => ps.filter((x) => x !== prosePlugin));
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
plugin.plugin = () => prosePlugin;
|
|
202
|
+
plugin.key = () => prosePlugin.spec.key;
|
|
203
|
+
return plugin;
|
|
204
|
+
}
|
|
205
|
+
function $proseAsync(prose, timerName) {
|
|
206
|
+
let prosePlugin;
|
|
207
|
+
const plugin = addTimer(async (ctx) => {
|
|
208
|
+
await ctx.wait(SchemaReady);
|
|
209
|
+
prosePlugin = await prose(ctx);
|
|
210
|
+
ctx.update(prosePluginsCtx, (ps) => [...ps, prosePlugin]);
|
|
211
|
+
return () => {
|
|
212
|
+
ctx.update(prosePluginsCtx, (ps) => ps.filter((x) => x !== prosePlugin));
|
|
213
|
+
};
|
|
214
|
+
}, editorStateTimerCtx, timerName);
|
|
215
|
+
plugin.plugin = () => prosePlugin;
|
|
216
|
+
plugin.key = () => prosePlugin.spec.key;
|
|
217
|
+
return plugin;
|
|
218
|
+
}
|
|
219
|
+
//#endregion
|
|
220
|
+
//#region src/composable/$shortcut.ts
|
|
221
|
+
function $shortcut(shortcut) {
|
|
222
|
+
const plugin = (ctx) => async () => {
|
|
223
|
+
await ctx.wait(KeymapReady);
|
|
224
|
+
const km = ctx.get(keymapCtx);
|
|
225
|
+
const keymap = shortcut(ctx);
|
|
226
|
+
const dispose = km.addObjectKeymap(keymap);
|
|
227
|
+
plugin.keymap = keymap;
|
|
228
|
+
return () => {
|
|
229
|
+
dispose();
|
|
230
|
+
};
|
|
231
|
+
};
|
|
232
|
+
return plugin;
|
|
233
|
+
}
|
|
234
|
+
function $shortcutAsync(shortcut, timerName) {
|
|
235
|
+
return addTimer(async (ctx, plugin) => {
|
|
236
|
+
await ctx.wait(KeymapReady);
|
|
237
|
+
const km = ctx.get(keymapCtx);
|
|
238
|
+
const keymap = await shortcut(ctx);
|
|
239
|
+
const dispose = km.addObjectKeymap(keymap);
|
|
240
|
+
plugin.keymap = keymap;
|
|
241
|
+
return () => {
|
|
242
|
+
dispose();
|
|
243
|
+
};
|
|
244
|
+
}, editorStateTimerCtx, timerName);
|
|
245
|
+
}
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region src/composable/$view.ts
|
|
248
|
+
function $view(type, view) {
|
|
249
|
+
const plugin = (ctx) => async () => {
|
|
250
|
+
await ctx.wait(SchemaReady);
|
|
251
|
+
const v = view(ctx);
|
|
252
|
+
if (type.type(ctx) instanceof NodeType) ctx.update(nodeViewCtx, (ps) => [...ps, [type.id, v]]);
|
|
253
|
+
else ctx.update(markViewCtx, (ps) => [...ps, [type.id, v]]);
|
|
254
|
+
plugin.view = v;
|
|
255
|
+
plugin.type = type;
|
|
256
|
+
return () => {
|
|
257
|
+
if (type.type(ctx) instanceof NodeType) ctx.update(nodeViewCtx, (ps) => ps.filter((x) => x[0] !== type.id));
|
|
258
|
+
else ctx.update(markViewCtx, (ps) => ps.filter((x) => x[0] !== type.id));
|
|
259
|
+
};
|
|
260
|
+
};
|
|
261
|
+
return plugin;
|
|
262
|
+
}
|
|
263
|
+
function $viewAsync(type, view, timerName) {
|
|
264
|
+
return addTimer(async (ctx, plugin) => {
|
|
265
|
+
await ctx.wait(SchemaReady);
|
|
266
|
+
const v = await view(ctx);
|
|
267
|
+
if (type.type(ctx) instanceof NodeType) ctx.update(nodeViewCtx, (ps) => [...ps, [type.id, v]]);
|
|
268
|
+
else ctx.update(markViewCtx, (ps) => [...ps, [type.id, v]]);
|
|
269
|
+
plugin.view = v;
|
|
270
|
+
plugin.type = type;
|
|
271
|
+
return () => {
|
|
272
|
+
if (type.type(ctx) instanceof NodeType) ctx.update(nodeViewCtx, (ps) => ps.filter((x) => x[0] !== type.id));
|
|
273
|
+
else ctx.update(markViewCtx, (ps) => ps.filter((x) => x[0] !== type.id));
|
|
274
|
+
};
|
|
275
|
+
}, editorViewTimerCtx, timerName);
|
|
276
|
+
}
|
|
277
|
+
//#endregion
|
|
278
|
+
//#region src/composable/$ctx.ts
|
|
279
|
+
function $ctx(value, name) {
|
|
280
|
+
const slice = createSlice(value, name);
|
|
281
|
+
const plugin = (ctx) => {
|
|
282
|
+
ctx.inject(slice);
|
|
283
|
+
return () => {
|
|
284
|
+
return () => {
|
|
285
|
+
ctx.remove(slice);
|
|
286
|
+
};
|
|
287
|
+
};
|
|
288
|
+
};
|
|
289
|
+
plugin.key = slice;
|
|
290
|
+
return plugin;
|
|
291
|
+
}
|
|
292
|
+
//#endregion
|
|
293
|
+
//#region src/composable/composed/$node-schema.ts
|
|
294
|
+
function $nodeSchema(id, schema) {
|
|
295
|
+
const schemaCtx = $ctx(schema, id);
|
|
296
|
+
const nodeSchema = $node(id, (ctx) => {
|
|
297
|
+
return ctx.get(schemaCtx.key)(ctx);
|
|
298
|
+
});
|
|
299
|
+
const result = [schemaCtx, nodeSchema];
|
|
300
|
+
result.id = nodeSchema.id;
|
|
301
|
+
result.node = nodeSchema;
|
|
302
|
+
result.type = (ctx) => nodeSchema.type(ctx);
|
|
303
|
+
result.ctx = schemaCtx;
|
|
304
|
+
result.key = schemaCtx.key;
|
|
305
|
+
result.extendSchema = (handler) => {
|
|
306
|
+
return $nodeSchema(id, handler(schema));
|
|
307
|
+
};
|
|
308
|
+
return result;
|
|
309
|
+
}
|
|
310
|
+
//#endregion
|
|
311
|
+
//#region src/composable/composed/$mark-schema.ts
|
|
312
|
+
function $markSchema(id, schema) {
|
|
313
|
+
const schemaCtx = $ctx(schema, id);
|
|
314
|
+
const markSchema = $mark(id, (ctx) => {
|
|
315
|
+
return ctx.get(schemaCtx.key)(ctx);
|
|
316
|
+
});
|
|
317
|
+
const result = [schemaCtx, markSchema];
|
|
318
|
+
result.id = markSchema.id;
|
|
319
|
+
result.mark = markSchema;
|
|
320
|
+
result.type = (ctx) => markSchema.type(ctx);
|
|
321
|
+
result.ctx = schemaCtx;
|
|
322
|
+
result.key = schemaCtx.key;
|
|
323
|
+
result.extendSchema = (handler) => {
|
|
324
|
+
return $markSchema(id, handler(schema));
|
|
325
|
+
};
|
|
326
|
+
return result;
|
|
327
|
+
}
|
|
328
|
+
//#endregion
|
|
329
|
+
//#region src/composable/composed/$use-keymap.ts
|
|
330
|
+
function $useKeymap(name, userKeymap) {
|
|
331
|
+
const keymapDef = $ctx(Object.fromEntries(Object.entries(userKeymap).map(([key, { shortcuts, priority }]) => {
|
|
332
|
+
return [key, {
|
|
333
|
+
shortcuts,
|
|
334
|
+
priority
|
|
335
|
+
}];
|
|
336
|
+
})), `${name}Keymap`);
|
|
337
|
+
const shortcuts = $shortcut((ctx) => {
|
|
338
|
+
const keys = ctx.get(keymapDef.key);
|
|
339
|
+
const keymapTuple = Object.entries(userKeymap).flatMap(([key, { command }]) => {
|
|
340
|
+
const target = keys[key];
|
|
341
|
+
const targetKeys = [target.shortcuts].flat();
|
|
342
|
+
const priority = target.priority;
|
|
343
|
+
return targetKeys.map((targetKey) => [targetKey, {
|
|
344
|
+
key: targetKey,
|
|
345
|
+
onRun: command,
|
|
346
|
+
priority
|
|
347
|
+
}]);
|
|
348
|
+
});
|
|
349
|
+
return Object.fromEntries(keymapTuple);
|
|
350
|
+
});
|
|
351
|
+
const result = [keymapDef, shortcuts];
|
|
352
|
+
result.ctx = keymapDef;
|
|
353
|
+
result.shortcuts = shortcuts;
|
|
354
|
+
result.key = keymapDef.key;
|
|
355
|
+
result.keymap = shortcuts.keymap;
|
|
356
|
+
return result;
|
|
357
|
+
}
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region src/composable/composed/$attr.ts
|
|
360
|
+
var $nodeAttr = (name, value = () => ({})) => $ctx(value, `${name}Attr`);
|
|
361
|
+
var $markAttr = (name, value = () => ({})) => $ctx(value, `${name}Attr`);
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region src/composable/composed/$remark.ts
|
|
364
|
+
function $remark(id, remark, initialOptions) {
|
|
365
|
+
const options = $ctx(initialOptions ?? {}, id);
|
|
366
|
+
const plugin = (ctx) => async () => {
|
|
367
|
+
await ctx.wait(InitReady);
|
|
368
|
+
const remarkPlugin = {
|
|
369
|
+
plugin: remark(ctx),
|
|
370
|
+
options: ctx.get(options.key)
|
|
371
|
+
};
|
|
372
|
+
ctx.update(remarkPluginsCtx, (rp) => [...rp, remarkPlugin]);
|
|
373
|
+
return () => {
|
|
374
|
+
ctx.update(remarkPluginsCtx, (rp) => rp.filter((x) => x !== remarkPlugin));
|
|
375
|
+
};
|
|
376
|
+
};
|
|
377
|
+
const result = [options, plugin];
|
|
378
|
+
result.id = id;
|
|
379
|
+
result.plugin = plugin;
|
|
380
|
+
result.options = options;
|
|
381
|
+
return result;
|
|
382
|
+
}
|
|
383
|
+
//#endregion
|
|
384
|
+
//#region src/macro/call-command.ts
|
|
385
|
+
function callCommand(slice, payload) {
|
|
386
|
+
return (ctx) => {
|
|
387
|
+
return ctx.get(commandsCtx).call(slice, payload);
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
//#endregion
|
|
391
|
+
//#region src/macro/force-update.ts
|
|
392
|
+
function forceUpdate() {
|
|
393
|
+
return (ctx) => {
|
|
394
|
+
const view = ctx.get(editorViewCtx);
|
|
395
|
+
const { tr } = view.state;
|
|
396
|
+
const nextTr = Object.assign(Object.create(tr), tr).setTime(Date.now());
|
|
397
|
+
return view.dispatch(nextTr);
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
//#endregion
|
|
401
|
+
//#region src/macro/get-html.ts
|
|
402
|
+
function getHTML() {
|
|
403
|
+
return (ctx) => {
|
|
404
|
+
const div = document.createElement("div");
|
|
405
|
+
const schema = ctx.get(schemaCtx);
|
|
406
|
+
const view = ctx.get(editorViewCtx);
|
|
407
|
+
const fragment = DOMSerializer.fromSchema(schema).serializeFragment(view.state.doc.content);
|
|
408
|
+
div.appendChild(fragment);
|
|
409
|
+
return div.innerHTML;
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
//#endregion
|
|
413
|
+
//#region src/macro/get-markdown.ts
|
|
414
|
+
function getMarkdown(range) {
|
|
415
|
+
return (ctx) => {
|
|
416
|
+
const view = ctx.get(editorViewCtx);
|
|
417
|
+
const schema = ctx.get(schemaCtx);
|
|
418
|
+
const serializer = ctx.get(serializerCtx);
|
|
419
|
+
if (!range) return serializer(view.state.doc);
|
|
420
|
+
const slice = view.state.doc.slice(range.from, range.to, true);
|
|
421
|
+
const doc = schema.topNodeType.createAndFill(null, slice.content);
|
|
422
|
+
if (!doc) {
|
|
423
|
+
console.error("No document found");
|
|
424
|
+
return "";
|
|
425
|
+
}
|
|
426
|
+
return serializer(doc);
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
//#endregion
|
|
430
|
+
//#region src/macro/insert.ts
|
|
431
|
+
function insert(markdown, inline = false) {
|
|
432
|
+
return (ctx) => {
|
|
433
|
+
const view = ctx.get(editorViewCtx);
|
|
434
|
+
const doc = ctx.get(parserCtx)(markdown);
|
|
435
|
+
if (!doc) return;
|
|
436
|
+
if (!inline) {
|
|
437
|
+
const contentSlice = view.state.selection.content();
|
|
438
|
+
return view.dispatch(view.state.tr.replaceSelection(new Slice(doc.content, contentSlice.openStart, contentSlice.openEnd)).scrollIntoView());
|
|
439
|
+
}
|
|
440
|
+
const schema = ctx.get(schemaCtx);
|
|
441
|
+
const dom = DOMSerializer.fromSchema(schema).serializeFragment(doc.content);
|
|
442
|
+
const slice = DOMParser.fromSchema(schema).parseSlice(dom);
|
|
443
|
+
const node = isTextOnlySlice(slice);
|
|
444
|
+
if (node) {
|
|
445
|
+
view.dispatch(view.state.tr.replaceSelectionWith(node, true));
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
view.dispatch(view.state.tr.replaceSelection(slice));
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
//#endregion
|
|
452
|
+
//#region src/macro/outline.ts
|
|
453
|
+
function outline() {
|
|
454
|
+
return (ctx) => {
|
|
455
|
+
const view = ctx.get(editorViewCtx);
|
|
456
|
+
const data = [];
|
|
457
|
+
view.state.doc.descendants((node) => {
|
|
458
|
+
if (node.type.name === "heading" && node.attrs.level) data.push({
|
|
459
|
+
text: node.textContent,
|
|
460
|
+
level: node.attrs.level,
|
|
461
|
+
id: node.attrs.id
|
|
462
|
+
});
|
|
463
|
+
});
|
|
464
|
+
return data;
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
//#endregion
|
|
468
|
+
//#region src/macro/replace-all.ts
|
|
469
|
+
function replaceAll(markdown, flush = false) {
|
|
470
|
+
return (ctx) => {
|
|
471
|
+
const view = ctx.get(editorViewCtx);
|
|
472
|
+
const doc = ctx.get(parserCtx)(markdown);
|
|
473
|
+
if (!doc) return;
|
|
474
|
+
if (!flush) {
|
|
475
|
+
const { state } = view;
|
|
476
|
+
return view.dispatch(state.tr.replace(0, state.doc.content.size, new Slice(doc.content, 0, 0)));
|
|
477
|
+
}
|
|
478
|
+
const schema = ctx.get(schemaCtx);
|
|
479
|
+
const newOptions = ctx.get(editorStateOptionsCtx)({
|
|
480
|
+
schema,
|
|
481
|
+
doc,
|
|
482
|
+
plugins: ctx.get(prosePluginsCtx)
|
|
483
|
+
});
|
|
484
|
+
const state = EditorState.create(newOptions);
|
|
485
|
+
view.updateState(state);
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
//#endregion
|
|
489
|
+
//#region src/macro/set-attr.ts
|
|
490
|
+
function setAttr(pos, update) {
|
|
491
|
+
return (ctx) => {
|
|
492
|
+
const view = ctx.get(editorViewCtx);
|
|
493
|
+
const { tr } = view.state;
|
|
494
|
+
const node = tr.doc.nodeAt(pos);
|
|
495
|
+
if (!node) return;
|
|
496
|
+
const nextAttr = update(node.attrs);
|
|
497
|
+
return view.dispatch(tr.setNodeMarkup(pos, void 0, nextAttr));
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
//#endregion
|
|
501
|
+
//#region src/macro/markdown-to-slice.ts
|
|
502
|
+
function markdownToSlice(markdown) {
|
|
503
|
+
return (ctx) => {
|
|
504
|
+
const doc = ctx.get(parserCtx)(markdown);
|
|
505
|
+
const schema = ctx.get(schemaCtx);
|
|
506
|
+
const dom = DOMSerializer.fromSchema(schema).serializeFragment(doc.content);
|
|
507
|
+
return DOMParser.fromSchema(schema).parseSlice(dom);
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
//#endregion
|
|
511
|
+
//#region src/macro/insert-pos.ts
|
|
512
|
+
function insertPos(markdown, pos, inline = false) {
|
|
513
|
+
return (ctx) => {
|
|
514
|
+
const slice = markdownToSlice(markdown)(ctx);
|
|
515
|
+
const view = ctx.get(editorViewCtx);
|
|
516
|
+
const toPos = view.state.doc.resolve(pos);
|
|
517
|
+
const min = 0;
|
|
518
|
+
const max = view.state.doc.content.size;
|
|
519
|
+
const resolved = inline ? toPos.pos : toPos.after(toPos.depth - 1);
|
|
520
|
+
const to = Math.min(Math.max(resolved, min), max);
|
|
521
|
+
view.dispatch(view.state.tr.replace(resolved, to, slice));
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
//#endregion
|
|
525
|
+
//#region src/macro/replace-range.ts
|
|
526
|
+
function replaceRange(markdown, range) {
|
|
527
|
+
return (ctx) => {
|
|
528
|
+
const view = ctx.get(editorViewCtx);
|
|
529
|
+
const slice = markdownToSlice(markdown)(ctx);
|
|
530
|
+
view.dispatch(view.state.tr.replace(range.from, range.to, slice));
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
//#endregion
|
|
534
|
+
//#region src/pipe.ts
|
|
535
|
+
var pipe = (...funcs) => {
|
|
536
|
+
const length = funcs.length;
|
|
537
|
+
let index = length;
|
|
538
|
+
while (index--) if (typeof funcs[index] !== "function") throw new TypeError("Expected a function");
|
|
539
|
+
return (...args) => {
|
|
540
|
+
let index = 0;
|
|
541
|
+
let result = length ? funcs[index](...args) : args[0];
|
|
542
|
+
while (++index < length) result = funcs[index](result);
|
|
543
|
+
return result;
|
|
544
|
+
};
|
|
545
|
+
};
|
|
546
|
+
//#endregion
|
|
547
|
+
export { $command, $commandAsync, $ctx, $inputRule, $inputRuleAsync, $mark, $markAsync, $markAttr, $markSchema, $node, $nodeAsync, $nodeAttr, $nodeSchema, $pasteRule, $pasteRuleAsync, $prose, $proseAsync, $remark, $shortcut, $shortcutAsync, $useKeymap, $view, $viewAsync, addTimer, callCommand, forceUpdate, getHTML, getMarkdown, insert, insertPos, markdownToSlice, nanoid, outline, pipe, replaceAll, replaceRange, setAttr };
|
|
548
|
+
|
|
549
|
+
//# sourceMappingURL=index.js.map
|