-
Notifications
You must be signed in to change notification settings - Fork 0
/
prosemirror-k2k.js
80 lines (72 loc) · 2.6 KB
/
prosemirror-k2k.js
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
(function (prosemirrorModel, prosemirrorTransform, prosemirrorCommands, prosemirrorKeymap, prosemirrorState, prosemirrorView, prosemirrorHistory) {
'use strict';
// noteSchema{
const noteSchema = new prosemirrorModel.Schema({
nodes: {
text: {},
article: {
content: "(num | heading | akn_paragraph)+",
toDOM() { return ["article", 0] },
parseDOM: [{ tag: "article" }]
},
num: {
content: "bold",
toDOM() { return ["num", 0] },
parseDOM: [{ tag: "num" }]
},
bold: {
content: "text*",
toDOM() { return ["b", 0] },
parseDOM: [{ tag: "b" }]
},
heading: {
content: "text*",
toDOM() { return ["heading", 0] },
parseDOM: [{ tag: "heading" }]
},
akn_paragraph: {
content: "content",
toDOM() { return ["paragraph", 0] },
parseDOM: [{ tag: "paragraph" }]
},
content: {
content: "paragraph",
toDOM() { return ["content", 0] },
parseDOM: [{ tag: "content" }]
},
paragraph: {
content: "text*",
toDOM() { return ["p", 0] },
parseDOM: [{ tag: "p" }]
},
doc: {
content: "article+"
}
}
});
function makeNoteGroup(state, dispatch) {
// Get a range around the selected blocks
let range = state.selection.$from.blockRange(state.selection.$to);
// See if it is possible to wrap that range in a note group
let wrapping = prosemirrorTransform.findWrapping(range, noteSchema.nodes.notegroup);
// If not, the command doesn't apply
if (!wrapping) return false
// Otherwise, dispatch a transaction, using the `wrap` method to
// create the step that does the actual wrapping.
if (dispatch) dispatch(state.tr.wrap(range, wrapping).scrollIntoView());
return true
}
// }
let histKeymap = prosemirrorKeymap.keymap({ "Mod-z": prosemirrorHistory.undo, "Mod-y": prosemirrorHistory.redo });
function start(place, content, schema, plugins = []) {
let doc = prosemirrorModel.DOMParser.fromSchema(schema).parse(content);
return new prosemirrorView.EditorView(place, {
state: prosemirrorState.EditorState.create({
doc,
plugins: plugins.concat([histKeymap, prosemirrorKeymap.keymap(prosemirrorCommands.baseKeymap), prosemirrorHistory.history()])
})
})
}
function id(str) { return document.getElementById(str) }
start(id("k2k-editor"), id("k2k-content"), noteSchema, [prosemirrorKeymap.keymap({ "Ctrl-Space": makeNoteGroup })]);
})(PM.model, PM.transform, PM.commands, PM.keymap, PM.state, PM.view, PM.history);