-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproseconfig.mjs
552 lines (532 loc) · 20 KB
/
proseconfig.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// A modified version of example-setup.js
// from the prosemirror-example-setup package; getting rid of images and
// headers but adding subscripts, superscripts, etc.
// Most code unchanged
import { keymap } from 'prosemirror-keymap';
import { undo, redo, history } from 'prosemirror-history';
import { toggleMark, wrapIn, chainCommands, exitCode, setBlockType, joinUp, joinDown, lift, selectParentNode, baseKeymap } from 'prosemirror-commands';
import { NodeSelection, Plugin } from 'prosemirror-state';
import { dropCursor } from 'prosemirror-dropcursor';
import { gapCursor } from 'prosemirror-gapcursor';
import { icons, wrapItem, blockTypeItem, MenuItem, Dropdown, DropdownSubmenu, joinUpItem, liftItem, selectParentNodeItem, undoItem, redoItem, menuBar } from 'prosemirror-menu';
import { wrapInList, splitListItem, liftListItem, sinkListItem } from 'prosemirror-schema-list';
import { undoInputRule, smartQuotes, ellipsis, emDash, inputRules, wrappingInputRule, textblockTypeInputRule } from 'prosemirror-inputrules';
const prefix = "ProseMirror-prompt";
function openPrompt(options) {
let wrapper = document.body.appendChild(document.createElement("div"));
wrapper.className = prefix;
let mouseOutside = (e) => { if (!wrapper.contains(e.target))
close(); };
setTimeout(() => window.addEventListener("mousedown", mouseOutside), 50);
let close = () => {
window.removeEventListener("mousedown", mouseOutside);
if (wrapper.parentNode)
wrapper.parentNode.removeChild(wrapper);
};
let domFields = [];
for (let name in options.fields)
domFields.push(options.fields[name].render());
let submitButton = document.createElement("button");
submitButton.type = "submit";
submitButton.className = prefix + "-submit";
submitButton.textContent = "OK";
let cancelButton = document.createElement("button");
cancelButton.type = "button";
cancelButton.className = prefix + "-cancel";
cancelButton.textContent = "Cancel";
cancelButton.addEventListener("click", close);
let form = wrapper.appendChild(document.createElement("form"));
if (options.title)
form.appendChild(document.createElement("h5")).textContent = options.title;
domFields.forEach(field => {
form.appendChild(document.createElement("div")).appendChild(field);
});
let buttons = form.appendChild(document.createElement("div"));
buttons.className = prefix + "-buttons";
buttons.appendChild(submitButton);
buttons.appendChild(document.createTextNode(" "));
buttons.appendChild(cancelButton);
let box = wrapper.getBoundingClientRect();
wrapper.style.top = ((window.innerHeight - box.height) / 2) + "px";
wrapper.style.left = ((window.innerWidth - box.width) / 2) + "px";
let submit = () => {
let params = getValues(options.fields, domFields);
if (params) {
close();
options.callback(params);
}
};
form.addEventListener("submit", e => {
e.preventDefault();
submit();
});
form.addEventListener("keydown", e => {
if (e.keyCode == 27) {
e.preventDefault();
close();
}
else if (e.keyCode == 13 && !(e.ctrlKey || e.metaKey || e.shiftKey)) {
e.preventDefault();
submit();
}
else if (e.keyCode == 9) {
window.setTimeout(() => {
if (!wrapper.contains(document.activeElement))
close();
}, 500);
}
});
let input = form.elements[0];
if (input)
input.focus();
}
function getValues(fields, domFields) {
let result = Object.create(null), i = 0;
for (let name in fields) {
let field = fields[name], dom = domFields[i++];
let value = field.read(dom), bad = field.validate(value);
if (bad) {
reportInvalid(dom, bad);
return null;
}
result[name] = field.clean(value);
}
return result;
}
function reportInvalid(dom, message) {
// FIXME this is awful and needs a lot more work
let parent = dom.parentNode;
let msg = parent.appendChild(document.createElement("div"));
msg.style.left = (dom.offsetLeft + dom.offsetWidth + 2) + "px";
msg.style.top = (dom.offsetTop - 5) + "px";
msg.className = "ProseMirror-invalid";
msg.textContent = message;
setTimeout(() => parent.removeChild(msg), 1500);
}
/**
The type of field that `openPrompt` expects to be passed to it.
*/
class Field {
/**
Create a field with the given options. Options support by all
field types are:
*/
constructor(
/**
@internal
*/
options) {
this.options = options;
}
/**
Read the field's value from its DOM node.
*/
read(dom) { return dom.value; }
/**
A field-type-specific validation function.
*/
validateType(value) { return null; }
/**
@internal
*/
validate(value) {
if (!value && this.options.required)
return "Required field";
return this.validateType(value) || (this.options.validate ? this.options.validate(value) : null);
}
clean(value) {
return this.options.clean ? this.options.clean(value) : value;
}
}
/**
A field class for single-line text fields.
*/
class TextField extends Field {
render() {
let input = document.createElement("input");
input.type = "text";
input.placeholder = this.options.label;
input.value = this.options.value || "";
input.autocomplete = "off";
return input;
}
}
// Helpers to create specific types of items
function canInsert(state, nodeType) {
let $from = state.selection.$from;
for (let d = $from.depth; d >= 0; d--) {
let index = $from.index(d);
if ($from.node(d).canReplaceWith(index, index, nodeType))
return true;
}
return false;
}
function insertImageItem(nodeType) {
return new MenuItem({
title: "Insert image",
label: "Image",
enable(state) { return canInsert(state, nodeType); },
run(state, _, view) {
let { from, to } = state.selection, attrs = null;
if (state.selection instanceof NodeSelection && state.selection.node.type == nodeType)
attrs = state.selection.node.attrs;
openPrompt({
title: "Insert image",
fields: {
src: new TextField({ label: "Location", required: true, value: attrs && attrs.src }),
title: new TextField({ label: "Title", value: attrs && attrs.title }),
alt: new TextField({ label: "Description",
value: attrs ? attrs.alt : state.doc.textBetween(from, to, " ") })
},
callback(attrs) {
view.dispatch(view.state.tr.replaceSelectionWith(nodeType.createAndFill(attrs)));
view.focus();
}
});
}
});
}
function cmdItem(cmd, options) {
let passedOptions = {
label: options.title,
run: cmd
};
for (let prop in options)
passedOptions[prop] = options[prop];
if (!options.enable && !options.select)
passedOptions[options.enable ? "enable" : "select"] = state => cmd(state);
return new MenuItem(passedOptions);
}
function markActive(state, type) {
let { from, $from, to, empty } = state.selection;
if (empty)
return !!type.isInSet(state.storedMarks || $from.marks());
else
return state.doc.rangeHasMark(from, to, type);
}
function markItem(markType, options) {
let passedOptions = {
active(state) { return markActive(state, markType); }
};
for (let prop in options)
passedOptions[prop] = options[prop];
return cmdItem(toggleMark(markType), passedOptions);
}
function linkItem(markType) {
return new MenuItem({
title: "Add or remove link",
//icon: icons.link,
render: gicon('link'),
active(state) { return markActive(state, markType); },
enable(state) { return !state.selection.empty; },
run(state, dispatch, view) {
if (markActive(state, markType)) {
toggleMark(markType)(state, dispatch);
return true;
}
openPrompt({
title: "Create a link",
fields: {
href: new TextField({
label: "Link target",
required: true
}),
title: new TextField({ label: "Tooltip" })
},
callback(attrs) {
toggleMark(markType, attrs)(view.state, view.dispatch);
view.focus();
}
});
}
});
}
function wrapListItem(nodeType, options) {
return cmdItem(wrapInList(nodeType, options.attrs), options);
}
/**
Given a schema, look for default mark and node types in it and
return an object with relevant menu items relating to those marks.
*/
function gicon(icostr) {
return (view) => {
const btn = document.createElement('span');
btn.innerHTML = '<span class="material-symbols-outlined" style="position: relative; cursor: pointer; bottom: -0.3rem;">' + icostr + '</span>';
return btn;
}
}
function buildMenuItems(schema) {
let r = {};
let mark;
if (mark = schema.marks.strong)
r.toggleStrong = markItem(mark, { title: "Toggle strong style", render: gicon('format_bold') });
if (mark = schema.marks.em)
r.toggleEm = markItem(mark, { title: "Toggle emphasis", render: gicon('format_italic') });
if (mark = schema.marks.underline) {
r.toggleUnderline = markItem(mark, { title: "Toggle underline", render: gicon('format_underlined')});
}
if (mark = schema.marks.superscript) {
r.toggleSuperscript = markItem(mark, { title: "Toggle superscript", render: gicon('superscript') });
}
if (mark = schema.marks.subscript) {
r.toggleSubscript = markItem(mark, { title: "Toggle subscript", render: gicon('subscript') });
}
if (mark = schema.marks.code)
r.toggleCode = markItem(mark, { title: "Toggle code font", icon: icons.code });
if (mark = schema.marks.link)
r.toggleLink = linkItem(mark);
let node;
if (node = schema.nodes.image)
r.insertImage = insertImageItem(node);
if (node = schema.nodes.bullet_list)
r.wrapBulletList = wrapListItem(node, {
title: "Wrap in bullet list",
render: gicon('format_list_bulleted')
//icon: icons.bulletList
});
if (node = schema.nodes.ordered_list)
r.wrapOrderedList = wrapListItem(node, {
title: "Wrap in ordered list",
render: gicon('format_list_numbered')
//icon: icons.orderedList
});
if (node = schema.nodes.blockquote)
r.wrapBlockQuote = wrapItem(node, {
title: "Wrap in block quote",
render: gicon('format_quote')
//icon: icons.blockquote
});
if (node = schema.nodes.paragraph)
r.makeParagraph = blockTypeItem(node, {
title: "Change to paragraph",
label: "Plain"
});
if (node = schema.nodes.code_block)
r.makeCodeBlock = blockTypeItem(node, {
title: "Change to code block",
label: "Code"
});
if (node = schema.nodes.heading)
for (let i = 1; i <= 10; i++)
r["makeHead" + i] = blockTypeItem(node, {
title: "Change to heading " + i,
label: "Level " + i,
attrs: { level: i }
});
if (node = schema.nodes.horizontal_rule) {
let hr = node;
r.insertHorizontalRule = new MenuItem({
title: "Insert horizontal rule",
label: "Horizontal rule",
enable(state) { return canInsert(state, hr); },
run(state, dispatch) { dispatch(state.tr.replaceSelectionWith(hr.create())); }
});
}
let cut = (arr) => arr.filter(x => x);
r.insertMenu = new Dropdown(cut([r.insertImage, r.insertHorizontalRule]), { label: "Insert" });
r.typeMenu = new Dropdown(cut([r.makeParagraph, r.makeCodeBlock, r.makeHead1 && new DropdownSubmenu(cut([
r.makeHead1, r.makeHead2, r.makeHead3, r.makeHead4, r.makeHead5, r.makeHead6
]), { label: "Heading" })]), { label: "Type..." });
r.inlineMenu = [cut([r.toggleStrong, r.toggleEm, r.toggleUnderline, r.toggleSuperscript, r.toggleSubscript, r.toggleLink])];
r.blockMenu = [cut([r.wrapBulletList, r.wrapOrderedList, r.wrapBlockQuote, joinUpItem])];
r.fullMenu = r.inlineMenu.concat(r.blockMenu, [[undoItem, redoItem]]);
return r;
}
const mac = typeof navigator != "undefined" ? /Mac|iP(hone|[oa]d)/.test(navigator.platform) : false;
/**
Inspect the given schema looking for marks and nodes from the
basic schema, and if found, add key bindings related to them.
This will add:
* **Mod-b** for toggling [strong](https://prosemirror.net/docs/ref/#schema-basic.StrongMark)
* **Mod-i** for toggling [emphasis](https://prosemirror.net/docs/ref/#schema-basic.EmMark)
* **Mod-`** for toggling [code font](https://prosemirror.net/docs/ref/#schema-basic.CodeMark)
* **Ctrl-Shift-0** for making the current textblock a paragraph
* **Ctrl-Shift-1** to **Ctrl-Shift-Digit6** for making the current
textblock a heading of the corresponding level
* **Ctrl-Shift-Backslash** to make the current textblock a code block
* **Ctrl-Shift-8** to wrap the selection in an ordered list
* **Ctrl-Shift-9** to wrap the selection in a bullet list
* **Ctrl->** to wrap the selection in a block quote
* **Enter** to split a non-empty textblock in a list item while at
the same time splitting the list item
* **Mod-Enter** to insert a hard break
* **Mod-_** to insert a horizontal rule
* **Backspace** to undo an input rule
* **Alt-ArrowUp** to `joinUp`
* **Alt-ArrowDown** to `joinDown`
* **Mod-BracketLeft** to `lift`
* **Escape** to `selectParentNode`
You can suppress or map these bindings by passing a `mapKeys`
argument, which maps key names (say `"Mod-B"` to either `false`, to
remove the binding, or a new key name string.
*/
function buildKeymap(schema, mapKeys) {
let keys = {}, type;
function bind(key, cmd) {
if (mapKeys) {
let mapped = mapKeys[key];
if (mapped === false)
return;
if (mapped)
key = mapped;
}
keys[key] = cmd;
}
bind("Mod-z", undo);
bind("Shift-Mod-z", redo);
bind("Backspace", undoInputRule);
if (!mac)
bind("Mod-y", redo);
bind("Alt-ArrowUp", joinUp);
bind("Alt-ArrowDown", joinDown);
bind("Mod-BracketLeft", lift);
bind("Escape", selectParentNode);
if (type = schema.marks.strong) {
bind("Mod-b", toggleMark(type));
bind("Mod-B", toggleMark(type));
}
if (type = schema.marks.em) {
bind("Mod-i", toggleMark(type));
bind("Mod-I", toggleMark(type));
}
if (type = schema.marks.underline) {
bind("Mod-u", toggleMark(type));
bind("Mod-U", toggleMark(type));
}
if (type = schema.marks.code)
bind("Mod-`", toggleMark(type));
if (type = schema.nodes.bullet_list)
bind("Shift-Ctrl-8", wrapInList(type));
if (type = schema.nodes.ordered_list)
bind("Shift-Ctrl-9", wrapInList(type));
if (type = schema.nodes.blockquote)
bind("Ctrl->", wrapIn(type));
if (type = schema.nodes.hard_break) {
let br = type, cmd = chainCommands(exitCode, (state, dispatch) => {
if (dispatch)
dispatch(state.tr.replaceSelectionWith(br.create()).scrollIntoView());
return true;
});
bind("Mod-Enter", cmd);
bind("Shift-Enter", cmd);
if (mac)
bind("Ctrl-Enter", cmd);
}
if (type = schema.nodes.list_item) {
bind("Enter", splitListItem(type));
bind("Mod-[", liftListItem(type));
bind("Mod-]", sinkListItem(type));
}
if (type = schema.nodes.paragraph)
bind("Shift-Ctrl-0", setBlockType(type));
if (type = schema.nodes.code_block)
bind("Shift-Ctrl-\\", setBlockType(type));
if (type = schema.nodes.heading)
for (let i = 1; i <= 6; i++)
bind("Shift-Ctrl-" + i, setBlockType(type, { level: i }));
if (type = schema.nodes.horizontal_rule) {
let hr = type;
bind("Mod-_", (state, dispatch) => {
if (dispatch)
dispatch(state.tr.replaceSelectionWith(hr.create()).scrollIntoView());
return true;
});
}
return keys;
}
/**
Given a blockquote node type, returns an input rule that turns `"> "`
at the start of a textblock into a blockquote.
*/
function blockQuoteRule(nodeType) {
return wrappingInputRule(/^\s*>\s$/, nodeType);
}
/**
Given a list node type, returns an input rule that turns a number
followed by a dot at the start of a textblock into an ordered list.
*/
function orderedListRule(nodeType) {
return wrappingInputRule(/^(\d+)\.\s$/, nodeType, match => ({ order: +match[1] }), (match, node) => node.childCount + node.attrs.order == +match[1]);
}
/**
Given a list node type, returns an input rule that turns a bullet
(dash, plush, or asterisk) at the start of a textblock into a
bullet list.
*/
function bulletListRule(nodeType) {
return wrappingInputRule(/^\s*([-+*])\s$/, nodeType);
}
/**
Given a code block node type, returns an input rule that turns a
textblock starting with three backticks into a code block.
*/
function codeBlockRule(nodeType) {
return textblockTypeInputRule(/^```$/, nodeType);
}
/**
Given a node type and a maximum level, creates an input rule that
turns up to that number of `#` characters followed by a space at
the start of a textblock into a heading whose level corresponds to
the number of `#` signs.
*/
function headingRule(nodeType, maxLevel) {
return textblockTypeInputRule(new RegExp("^(#{1," + maxLevel + "})\\s$"), nodeType, match => ({ level: match[1].length }));
}
/**
A set of input rules for creating the basic block quotes, lists,
code blocks, and heading.
*/
function buildInputRules(schema) {
let rules = smartQuotes.concat(ellipsis, emDash), type;
if (type = schema.nodes.blockquote)
rules.push(blockQuoteRule(type));
if (type = schema.nodes.ordered_list)
rules.push(orderedListRule(type));
if (type = schema.nodes.bullet_list)
rules.push(bulletListRule(type));
if (type = schema.nodes.code_block)
rules.push(codeBlockRule(type));
if (type = schema.nodes.heading)
rules.push(headingRule(type, 6));
return inputRules({ rules });
}
/**
Create an array of plugins pre-configured for the given schema.
The resulting array will include the following plugins:
* Input rules for smart quotes and creating the block types in the
schema using markdown conventions (say `"> "` to create a
blockquote)
* A keymap that defines keys to create and manipulate the nodes in the
schema
* A keymap binding the default keys provided by the
prosemirror-commands module
* The undo history plugin
* The drop cursor plugin
* The gap cursor plugin
* A custom plugin that adds a `menuContent` prop for the
prosemirror-menu wrapper, and a CSS class that enables the
additional styling defined in `style/style.css` in this package
Probably only useful for quickly setting up a passable
editor—you'll need more control over your settings in most
real-world situations.
*/
function proseConfig(options) {
let plugins = [
buildInputRules(options.schema),
keymap(buildKeymap(options.schema, options.mapKeys)),
keymap(baseKeymap),
dropCursor(),
gapCursor()
];
if (options.menuBar !== false)
plugins.push(menuBar({ floating: options.floatingMenu !== false,
content: options.menuContent || buildMenuItems(options.schema).fullMenu }));
if (options.history !== false)
plugins.push(history());
return plugins.concat(new Plugin({
props: {
attributes: { class: "ProseMirror-example-setup-style" }
}
}));
}
export { buildInputRules, buildKeymap, buildMenuItems, proseConfig };