@rme-sdk/extension-codemirror6 1.1.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/dist/_tsup-dts-rollup.d.cts +179 -0
- package/dist/_tsup-dts-rollup.d.ts +179 -0
- package/dist/rme-sdk-extension-codemirror6.cjs +551 -0
- package/dist/rme-sdk-extension-codemirror6.d.cts +3 -0
- package/dist/rme-sdk-extension-codemirror6.d.ts +3 -0
- package/dist/rme-sdk-extension-codemirror6.js +545 -0
- package/package.json +61 -0
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
12
|
+
var __typeError = (msg) => {
|
|
13
|
+
throw TypeError(msg);
|
|
14
|
+
};
|
|
15
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
16
|
+
var __spreadValues = (a, b) => {
|
|
17
|
+
for (var prop in b || (b = {}))
|
|
18
|
+
if (__hasOwnProp.call(b, prop))
|
|
19
|
+
__defNormalProp(a, prop, b[prop]);
|
|
20
|
+
if (__getOwnPropSymbols)
|
|
21
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
22
|
+
if (__propIsEnum.call(b, prop))
|
|
23
|
+
__defNormalProp(a, prop, b[prop]);
|
|
24
|
+
}
|
|
25
|
+
return a;
|
|
26
|
+
};
|
|
27
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
28
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
29
|
+
var __export = (target, all) => {
|
|
30
|
+
for (var name in all)
|
|
31
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
32
|
+
};
|
|
33
|
+
var __copyProps = (to, from, except, desc) => {
|
|
34
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
35
|
+
for (let key of __getOwnPropNames(from))
|
|
36
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
37
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
38
|
+
}
|
|
39
|
+
return to;
|
|
40
|
+
};
|
|
41
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
42
|
+
var __decoratorStart = (base) => {
|
|
43
|
+
var _a2;
|
|
44
|
+
return [, , , __create((_a2 = base == null ? void 0 : base[__knownSymbol("metadata")]) != null ? _a2 : null)];
|
|
45
|
+
};
|
|
46
|
+
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
|
|
47
|
+
var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
|
|
48
|
+
var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
|
|
49
|
+
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
|
|
50
|
+
var __runInitializers = (array, flags, self, value) => {
|
|
51
|
+
for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
|
|
52
|
+
return value;
|
|
53
|
+
};
|
|
54
|
+
var __decorateElement = (array, flags, name, decorators, target, extra) => {
|
|
55
|
+
var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
|
|
56
|
+
var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
|
|
57
|
+
var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
|
|
58
|
+
var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
|
|
59
|
+
return __privateGet(this, extra);
|
|
60
|
+
}, set [name](x) {
|
|
61
|
+
return __privateSet(this, extra, x);
|
|
62
|
+
} }, name));
|
|
63
|
+
k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
|
|
64
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
65
|
+
ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
|
|
66
|
+
if (k) {
|
|
67
|
+
ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
|
|
68
|
+
if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
|
|
69
|
+
if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
|
|
70
|
+
}
|
|
71
|
+
it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
|
|
72
|
+
if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
|
|
73
|
+
else if (typeof it !== "object" || it === null) __typeError("Object expected");
|
|
74
|
+
else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
|
|
75
|
+
}
|
|
76
|
+
return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
|
|
77
|
+
};
|
|
78
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
79
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
80
|
+
var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
|
|
81
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
82
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
83
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
84
|
+
|
|
85
|
+
// src/index.ts
|
|
86
|
+
var src_exports = {};
|
|
87
|
+
__export(src_exports, {
|
|
88
|
+
CodeMirrorExtension: () => CodeMirrorExtension2
|
|
89
|
+
});
|
|
90
|
+
module.exports = __toCommonJS(src_exports);
|
|
91
|
+
|
|
92
|
+
// src/codemirror-extension.ts
|
|
93
|
+
var import_core2 = require("@rme-sdk/core");
|
|
94
|
+
var import_state4 = require("@rme-sdk/pm/state");
|
|
95
|
+
|
|
96
|
+
// src/codemirror-node-view.ts
|
|
97
|
+
var import_state = require("@codemirror/state");
|
|
98
|
+
var import_view = require("@codemirror/view");
|
|
99
|
+
var import_core = require("@rme-sdk/core");
|
|
100
|
+
var import_commands = require("@rme-sdk/pm/commands");
|
|
101
|
+
var import_state2 = require("@rme-sdk/pm/state");
|
|
102
|
+
var CodeMirror6NodeView = class {
|
|
103
|
+
constructor({
|
|
104
|
+
node,
|
|
105
|
+
view,
|
|
106
|
+
getPos,
|
|
107
|
+
extensions,
|
|
108
|
+
loadLanguage,
|
|
109
|
+
toggleName
|
|
110
|
+
}) {
|
|
111
|
+
__publicField(this, "dom");
|
|
112
|
+
__publicField(this, "node");
|
|
113
|
+
__publicField(this, "view");
|
|
114
|
+
__publicField(this, "getPos");
|
|
115
|
+
__publicField(this, "schema");
|
|
116
|
+
__publicField(this, "cm");
|
|
117
|
+
__publicField(this, "updating", false);
|
|
118
|
+
__publicField(this, "loadLanguage");
|
|
119
|
+
__publicField(this, "languageConf");
|
|
120
|
+
__publicField(this, "languageName");
|
|
121
|
+
__publicField(this, "toggleName");
|
|
122
|
+
this.node = node;
|
|
123
|
+
this.view = view;
|
|
124
|
+
this.getPos = getPos;
|
|
125
|
+
this.schema = node.type.schema;
|
|
126
|
+
this.loadLanguage = loadLanguage;
|
|
127
|
+
this.languageConf = new import_state.Compartment();
|
|
128
|
+
this.languageName = "";
|
|
129
|
+
this.toggleName = toggleName;
|
|
130
|
+
const changeFilter = import_state.EditorState.changeFilter.of((tr) => {
|
|
131
|
+
if (!tr.docChanged && !this.updating) {
|
|
132
|
+
this.forwardSelection();
|
|
133
|
+
}
|
|
134
|
+
return true;
|
|
135
|
+
});
|
|
136
|
+
const startState = import_state.EditorState.create({
|
|
137
|
+
doc: this.node.textContent,
|
|
138
|
+
extensions: [
|
|
139
|
+
import_view.keymap.of(this.codeMirrorKeymap()),
|
|
140
|
+
changeFilter,
|
|
141
|
+
this.languageConf.of([]),
|
|
142
|
+
...extensions != null ? extensions : []
|
|
143
|
+
]
|
|
144
|
+
});
|
|
145
|
+
this.cm = new import_view.EditorView({
|
|
146
|
+
state: startState,
|
|
147
|
+
dispatch: this.valueChanged.bind(this)
|
|
148
|
+
});
|
|
149
|
+
this.dom = this.cm.dom;
|
|
150
|
+
this.updateLanguage();
|
|
151
|
+
}
|
|
152
|
+
update(node) {
|
|
153
|
+
if (node.type !== this.node.type) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
this.node = node;
|
|
157
|
+
this.updateLanguage();
|
|
158
|
+
const change = computeChange(this.cm.state.doc.toString(), node.textContent);
|
|
159
|
+
if (change) {
|
|
160
|
+
this.updating = true;
|
|
161
|
+
this.cm.dispatch({
|
|
162
|
+
changes: { from: change.from, to: change.to, insert: change.text }
|
|
163
|
+
});
|
|
164
|
+
this.updating = false;
|
|
165
|
+
}
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
updateLanguage() {
|
|
169
|
+
const languageName = this.node.attrs.language;
|
|
170
|
+
if (languageName === this.languageName) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const language = this.loadLanguage(languageName);
|
|
174
|
+
if (!language) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if ((0, import_core.isPromise)(language)) {
|
|
178
|
+
language.then((lang) => {
|
|
179
|
+
this.setLanguage(lang);
|
|
180
|
+
this.languageName = languageName;
|
|
181
|
+
});
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
this.setLanguage(language);
|
|
185
|
+
this.languageName = languageName;
|
|
186
|
+
}
|
|
187
|
+
setLanguage(language) {
|
|
188
|
+
this.cm.dispatch({
|
|
189
|
+
effects: this.languageConf.reconfigure(language)
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Synchronize the selections from ProseMirror to CodeMirrror
|
|
194
|
+
*/
|
|
195
|
+
setSelection(anchor, head) {
|
|
196
|
+
this.cm.focus();
|
|
197
|
+
this.updating = true;
|
|
198
|
+
this.cm.dispatch({ selection: { anchor, head } });
|
|
199
|
+
this.updating = false;
|
|
200
|
+
}
|
|
201
|
+
selectNode() {
|
|
202
|
+
this.focus();
|
|
203
|
+
}
|
|
204
|
+
focus() {
|
|
205
|
+
this.cm.focus();
|
|
206
|
+
this.forwardSelection();
|
|
207
|
+
}
|
|
208
|
+
stopEvent() {
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
destroy() {
|
|
212
|
+
this.cm.destroy();
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* When the code editor is focused, we can keep the selection of the outer
|
|
216
|
+
* editor synchronized with the inner one, so that any commands executed on
|
|
217
|
+
* the outer editor see an accurate selection.
|
|
218
|
+
*/
|
|
219
|
+
forwardSelection() {
|
|
220
|
+
if (!this.cm.hasFocus) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const state = this.view.state;
|
|
224
|
+
const selection = this.asProseMirrorSelection(state.doc);
|
|
225
|
+
if (!selection.eq(state.selection)) {
|
|
226
|
+
this.view.dispatch(state.tr.setSelection(selection));
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* This helper function translates from a CodeMirror selection to a
|
|
231
|
+
* ProseMirror selection.
|
|
232
|
+
*/
|
|
233
|
+
asProseMirrorSelection(doc) {
|
|
234
|
+
const start = this.getPos() + 1;
|
|
235
|
+
const { anchor, head } = this.cm.state.selection.main;
|
|
236
|
+
return import_state2.TextSelection.between(doc.resolve(anchor + start), doc.resolve(head + start));
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* A somewhat tricky aspect of nesting editor like this is handling cursor
|
|
240
|
+
* motion across the edges of the inner editor. This node view will have to
|
|
241
|
+
* take care of allowing the user to move the selection out of the code
|
|
242
|
+
* editor. For that purpose, it binds the arrow keys to handlers that check if
|
|
243
|
+
* further motion would ‘escape’ the editor, and if so, return the selection
|
|
244
|
+
* and focus to the outer editor.
|
|
245
|
+
*
|
|
246
|
+
* The keymap also binds ctrl-enter, which, in ProseMirror's base keymap,
|
|
247
|
+
* creates a new paragraph after a code block.
|
|
248
|
+
*/
|
|
249
|
+
codeMirrorKeymap() {
|
|
250
|
+
return [
|
|
251
|
+
{
|
|
252
|
+
key: "ArrowUp",
|
|
253
|
+
run: this.maybeEscape("line", -1)
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
key: "ArrowLeft",
|
|
257
|
+
run: this.maybeEscape("char", -1)
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
key: "ArrowDown",
|
|
261
|
+
run: this.maybeEscape("line", 1)
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
key: "ArrowRight",
|
|
265
|
+
run: this.maybeEscape("char", 1)
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
key: "Ctrl-Enter",
|
|
269
|
+
run: () => {
|
|
270
|
+
if ((0, import_commands.exitCode)(this.view.state, this.view.dispatch)) {
|
|
271
|
+
this.view.focus();
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
{
|
|
278
|
+
key: "Backspace",
|
|
279
|
+
run: () => {
|
|
280
|
+
const ranges = this.cm.state.selection.ranges;
|
|
281
|
+
if (ranges.length > 1) {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
const selection = ranges[0];
|
|
285
|
+
if (selection && (!selection.empty || selection.anchor > 0)) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
if (this.cm.state.doc.lines >= 2) {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
const state = this.view.state;
|
|
292
|
+
const toggleNode = (0, import_core.assertGet)(state.schema.nodes, this.toggleName);
|
|
293
|
+
const pos = this.getPos();
|
|
294
|
+
const tr = (0, import_core.replaceNodeAtPosition)({
|
|
295
|
+
pos,
|
|
296
|
+
tr: state.tr,
|
|
297
|
+
content: toggleNode.createChecked({}, this.node.content)
|
|
298
|
+
});
|
|
299
|
+
tr.setSelection(import_state2.TextSelection.near(tr.doc.resolve(pos)));
|
|
300
|
+
this.view.dispatch(tr);
|
|
301
|
+
this.view.focus();
|
|
302
|
+
return true;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
];
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* When the actual content of the code editor is changed, the event handler
|
|
309
|
+
* registered in the node view's constructor calls this method. It'll compare
|
|
310
|
+
* the code block node's current value to the value in the editor, and
|
|
311
|
+
* dispatch a transaction if there is a difference.
|
|
312
|
+
*/
|
|
313
|
+
valueChanged(tr) {
|
|
314
|
+
this.cm.update([tr]);
|
|
315
|
+
if (!tr.docChanged || this.updating) {
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
const change = computeChange(this.node.textContent, tr.state.doc.toString());
|
|
319
|
+
if (change) {
|
|
320
|
+
const start = this.getPos() + 1;
|
|
321
|
+
const tr2 = this.view.state.tr.replaceWith(
|
|
322
|
+
start + change.from,
|
|
323
|
+
start + change.to,
|
|
324
|
+
change.text ? this.schema.text(change.text) : []
|
|
325
|
+
);
|
|
326
|
+
this.view.dispatch(tr2);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
maybeEscape(unit, dir) {
|
|
330
|
+
return (view) => {
|
|
331
|
+
const { state } = view;
|
|
332
|
+
if (state.selection.ranges.some((range) => !range.empty)) {
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
const anchor = state.selection.main.anchor;
|
|
336
|
+
const line = state.doc.lineAt(anchor);
|
|
337
|
+
const lineOffset = anchor - line.from;
|
|
338
|
+
if (line.number !== (dir < 0 ? 1 : state.doc.lines) || unit === "char" && lineOffset !== (dir < 0 ? 0 : line.length)) {
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
const targetPos = this.getPos() + (dir < 0 ? 0 : this.node.nodeSize);
|
|
342
|
+
const selection = import_state2.Selection.near(this.view.state.doc.resolve(targetPos), dir);
|
|
343
|
+
this.view.dispatch(this.view.state.tr.setSelection(selection).scrollIntoView());
|
|
344
|
+
this.view.focus();
|
|
345
|
+
return true;
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
function computeChange(oldVal, newVal) {
|
|
350
|
+
if (oldVal === newVal) {
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
let start = 0;
|
|
354
|
+
let oldEnd = oldVal.length;
|
|
355
|
+
let newEnd = newVal.length;
|
|
356
|
+
while (start < oldEnd && oldVal.charCodeAt(start) === newVal.charCodeAt(start)) {
|
|
357
|
+
++start;
|
|
358
|
+
}
|
|
359
|
+
while (oldEnd > start && newEnd > start && oldVal.charCodeAt(oldEnd - 1) === newVal.charCodeAt(newEnd - 1)) {
|
|
360
|
+
oldEnd--;
|
|
361
|
+
newEnd--;
|
|
362
|
+
}
|
|
363
|
+
return { from: start, to: oldEnd, text: newVal.slice(start, newEnd) };
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// src/codemirror-utils.ts
|
|
367
|
+
var import_state3 = require("@rme-sdk/pm/state");
|
|
368
|
+
function arrowHandler(dir) {
|
|
369
|
+
return ({ dispatch, view, tr }) => {
|
|
370
|
+
if (!view) {
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
if (!(tr.selection.empty && view.endOfTextblock(dir))) {
|
|
374
|
+
return false;
|
|
375
|
+
}
|
|
376
|
+
const side = dir === "left" || dir === "up" ? -1 : 1;
|
|
377
|
+
const $head = tr.selection.$head;
|
|
378
|
+
const nextPos = import_state3.Selection.near(tr.doc.resolve(side > 0 ? $head.after() : $head.before()), side);
|
|
379
|
+
if (nextPos.$head && nextPos.$head.parent.type.name === "codeMirror") {
|
|
380
|
+
dispatch == null ? void 0 : dispatch(tr.setSelection(nextPos));
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
return false;
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// src/codemirror-extension.ts
|
|
388
|
+
var _updateCodeMirror_dec, _createCodeMirror_dec, _enterKey_dec, _a, _CodeMirrorExtension_decorators, _init;
|
|
389
|
+
_CodeMirrorExtension_decorators = [(0, import_core2.extension)({
|
|
390
|
+
defaultOptions: {
|
|
391
|
+
extensions: null,
|
|
392
|
+
languages: null,
|
|
393
|
+
toggleName: "paragraph"
|
|
394
|
+
}
|
|
395
|
+
})];
|
|
396
|
+
var CodeMirrorExtension2 = class extends (_a = import_core2.NodeExtension, _enterKey_dec = [(0, import_core2.keyBinding)({ shortcut: "Enter" })], _createCodeMirror_dec = [(0, import_core2.command)()], _updateCodeMirror_dec = [(0, import_core2.command)()], _a) {
|
|
397
|
+
constructor() {
|
|
398
|
+
super(...arguments);
|
|
399
|
+
__runInitializers(_init, 5, this);
|
|
400
|
+
__publicField(this, "languageMap", null);
|
|
401
|
+
}
|
|
402
|
+
get name() {
|
|
403
|
+
return "codeMirror";
|
|
404
|
+
}
|
|
405
|
+
createNodeSpec(extra, override) {
|
|
406
|
+
var _a2;
|
|
407
|
+
return __spreadProps(__spreadValues({
|
|
408
|
+
group: "block",
|
|
409
|
+
content: "text*",
|
|
410
|
+
marks: "",
|
|
411
|
+
defining: true
|
|
412
|
+
}, override), {
|
|
413
|
+
code: true,
|
|
414
|
+
attrs: __spreadProps(__spreadValues({}, extra.defaults()), {
|
|
415
|
+
language: { default: "" }
|
|
416
|
+
}),
|
|
417
|
+
parseDOM: [
|
|
418
|
+
{
|
|
419
|
+
tag: "pre",
|
|
420
|
+
getAttrs: (node) => (0, import_core2.isElementDomNode)(node) ? extra.parse(node) : false
|
|
421
|
+
},
|
|
422
|
+
...(_a2 = override.parseDOM) != null ? _a2 : []
|
|
423
|
+
],
|
|
424
|
+
toDOM() {
|
|
425
|
+
return ["pre", ["code", 0]];
|
|
426
|
+
},
|
|
427
|
+
isolating: true
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
createNodeViews() {
|
|
431
|
+
return (node, view, getPos) => new CodeMirror6NodeView({
|
|
432
|
+
node,
|
|
433
|
+
view,
|
|
434
|
+
getPos,
|
|
435
|
+
extensions: this.options.extensions,
|
|
436
|
+
loadLanguage: this.loadLanguage.bind(this),
|
|
437
|
+
toggleName: this.options.toggleName
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
createKeymap() {
|
|
441
|
+
return {
|
|
442
|
+
ArrowLeft: arrowHandler("left"),
|
|
443
|
+
ArrowRight: arrowHandler("right"),
|
|
444
|
+
ArrowUp: arrowHandler("up"),
|
|
445
|
+
ArrowDown: arrowHandler("down")
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Create an input rule that listens converts the code fence into a code block
|
|
450
|
+
* when typing triple back tick followed by a space.
|
|
451
|
+
*/
|
|
452
|
+
createInputRules() {
|
|
453
|
+
const regexp = /^```(\S+) $/;
|
|
454
|
+
const getAttributes = (match) => {
|
|
455
|
+
var _a2;
|
|
456
|
+
const language = (_a2 = match[1]) != null ? _a2 : "";
|
|
457
|
+
return { language };
|
|
458
|
+
};
|
|
459
|
+
return [
|
|
460
|
+
(0, import_core2.nodeInputRule)({
|
|
461
|
+
regexp,
|
|
462
|
+
type: this.type,
|
|
463
|
+
beforeDispatch: ({ tr, start }) => {
|
|
464
|
+
const $pos = tr.doc.resolve(start);
|
|
465
|
+
tr.setSelection(import_state4.TextSelection.near($pos));
|
|
466
|
+
},
|
|
467
|
+
getAttributes
|
|
468
|
+
})
|
|
469
|
+
];
|
|
470
|
+
}
|
|
471
|
+
enterKey({ dispatch, tr }) {
|
|
472
|
+
var _a2;
|
|
473
|
+
if (!((0, import_core2.isTextSelection)(tr.selection) && tr.selection.empty)) {
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
const { nodeBefore, parent } = tr.selection.$anchor;
|
|
477
|
+
if (!(nodeBefore == null ? void 0 : nodeBefore.isText) || !parent.type.isTextblock) {
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
const regex = /^```(\S*)?$/;
|
|
481
|
+
const { text, nodeSize } = nodeBefore;
|
|
482
|
+
const { textContent } = parent;
|
|
483
|
+
if (!text) {
|
|
484
|
+
return false;
|
|
485
|
+
}
|
|
486
|
+
const matchesNodeBefore = text.match(regex);
|
|
487
|
+
const matchesParent = textContent.match(regex);
|
|
488
|
+
if (!matchesNodeBefore || !matchesParent) {
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
const language = (_a2 = matchesNodeBefore[1]) != null ? _a2 : "";
|
|
492
|
+
const pos = tr.selection.$from.before();
|
|
493
|
+
const end = pos + nodeSize + 1;
|
|
494
|
+
tr.replaceWith(pos, end, this.type.create({ language }));
|
|
495
|
+
tr.setSelection(import_state4.TextSelection.near(tr.doc.resolve(pos + 1)));
|
|
496
|
+
if (dispatch) {
|
|
497
|
+
dispatch(tr);
|
|
498
|
+
}
|
|
499
|
+
return true;
|
|
500
|
+
}
|
|
501
|
+
getLanguageMap() {
|
|
502
|
+
var _a2;
|
|
503
|
+
if (!this.languageMap) {
|
|
504
|
+
this.languageMap = {};
|
|
505
|
+
for (const language of (_a2 = this.options.languages) != null ? _a2 : []) {
|
|
506
|
+
for (const alias of language.alias) {
|
|
507
|
+
this.languageMap[alias] = language;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return this.languageMap;
|
|
512
|
+
}
|
|
513
|
+
loadLanguage(languageName) {
|
|
514
|
+
if (typeof languageName !== "string") {
|
|
515
|
+
return void 0;
|
|
516
|
+
}
|
|
517
|
+
const languageMap = this.getLanguageMap();
|
|
518
|
+
const language = languageMap[languageName.toLowerCase()];
|
|
519
|
+
if (!language) {
|
|
520
|
+
return void 0;
|
|
521
|
+
}
|
|
522
|
+
return language.support || language.load();
|
|
523
|
+
}
|
|
524
|
+
createCodeMirror(attributes) {
|
|
525
|
+
return (0, import_core2.setBlockType)(this.type, attributes);
|
|
526
|
+
}
|
|
527
|
+
updateCodeMirror(attributes) {
|
|
528
|
+
const type = this.type;
|
|
529
|
+
return ({ state, dispatch, tr }) => {
|
|
530
|
+
const parent = (0, import_core2.findParentNodeOfType)({ types: type, selection: state.selection });
|
|
531
|
+
if (!parent || (0, import_core2.isEqual)(attributes, parent.node.attrs)) {
|
|
532
|
+
return false;
|
|
533
|
+
}
|
|
534
|
+
tr.setNodeMarkup(parent.pos, type, __spreadValues(__spreadValues({}, parent.node.attrs), attributes));
|
|
535
|
+
if (dispatch) {
|
|
536
|
+
dispatch(tr);
|
|
537
|
+
}
|
|
538
|
+
return true;
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
_init = __decoratorStart(_a);
|
|
543
|
+
__decorateElement(_init, 1, "enterKey", _enterKey_dec, CodeMirrorExtension2);
|
|
544
|
+
__decorateElement(_init, 1, "createCodeMirror", _createCodeMirror_dec, CodeMirrorExtension2);
|
|
545
|
+
__decorateElement(_init, 1, "updateCodeMirror", _updateCodeMirror_dec, CodeMirrorExtension2);
|
|
546
|
+
CodeMirrorExtension2 = __decorateElement(_init, 0, "CodeMirrorExtension", _CodeMirrorExtension_decorators, CodeMirrorExtension2);
|
|
547
|
+
__runInitializers(_init, 1, CodeMirrorExtension2);
|
|
548
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
549
|
+
0 && (module.exports = {
|
|
550
|
+
CodeMirrorExtension
|
|
551
|
+
});
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { CodeMirrorExtension_alias_1 as CodeMirrorExtension } from './_tsup-dts-rollup';
|
|
2
|
+
export { CodeMirrorExtensionAttributes_alias_1 as CodeMirrorExtensionAttributes } from './_tsup-dts-rollup';
|
|
3
|
+
export { CodeMirrorExtensionOptions_alias_1 as CodeMirrorExtensionOptions } from './_tsup-dts-rollup';
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { CodeMirrorExtension_alias_1 as CodeMirrorExtension } from './_tsup-dts-rollup';
|
|
2
|
+
export { CodeMirrorExtensionAttributes_alias_1 as CodeMirrorExtensionAttributes } from './_tsup-dts-rollup';
|
|
3
|
+
export { CodeMirrorExtensionOptions_alias_1 as CodeMirrorExtensionOptions } from './_tsup-dts-rollup';
|