/
index.js
151 lines (139 loc) · 3.82 KB
/
index.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
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
// textSchema{
import {Schema} from "prosemirror-model"
const textSchema = new Schema({
nodes: {
text: {},
doc: {content: "text*"}
}
})
// }
// noteSchema{
const noteSchema = new Schema({
nodes: {
text: {},
note: {
content: "text*",
toDOM() { return ["note", 0] },
parseDOM: [{tag: "note"}]
},
notegroup: {
content: "note+",
toDOM() { return ["notegroup", 0] },
parseDOM: [{tag: "notegroup"}]
},
doc: {
content: "(note | notegroup)+"
}
}
})
// }
// makeNoteGroup{
import {findWrapping} from "prosemirror-transform"
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 = 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
}
// }
// starSchema_1{
let starSchema = new Schema({
nodes: {
text: {
group: "inline",
},
star: {
inline: true,
group: "inline",
toDOM() { return ["star", "🟊"] },
parseDOM: [{tag: "star"}]
},
paragraph: {
group: "block",
content: "inline*",
toDOM() { return ["p", 0] },
parseDOM: [{tag: "p"}]
},
boring_paragraph: {
group: "block",
content: "text*",
marks: "",
toDOM() { return ["p", {class: "boring"}, 0] },
parseDOM: [{tag: "p.boring", priority: 60}]
},
doc: {
content: "block+"
}
},
// }
// starSchema_2{
marks: {
shouting: {
toDOM() { return ["shouting", 0] },
parseDOM: [{tag: "shouting"}]
},
link: {
attrs: {href: {}},
toDOM(node) { return ["a", {href: node.attrs.href}, 0] },
parseDOM: [{tag: "a", getAttrs(dom) { return {href: dom.href} }}],
inclusive: false
}
}
})
// }
// starKeymap{
import {toggleMark} from "prosemirror-commands"
import {keymap} from "prosemirror-keymap"
let starKeymap = keymap({
"Ctrl-b": toggleMark(starSchema.marks.shouting),
"Ctrl-q": toggleLink,
"Ctrl-Space": insertStar
})
// }
// toggleLink{
function toggleLink(state, dispatch) {
let {doc, selection} = state
if (selection.empty) return false
let attrs = null
if (!doc.rangeHasMark(selection.from, selection.to, starSchema.marks.link)) {
attrs = {href: prompt("Link to where?", "")}
if (!attrs.href) return false
}
return toggleMark(starSchema.marks.link, attrs)(state, dispatch)
}
// }
// insertStar{
function insertStar(state, dispatch) {
let type = starSchema.nodes.star
let {$from} = state.selection
if (!$from.parent.canReplaceWith($from.index(), $from.index(), type))
return false
dispatch(state.tr.replaceSelectionWith(type.create()))
return true
}
// }
import {DOMParser} from "prosemirror-model"
import {EditorState} from "prosemirror-state"
import {EditorView} from "prosemirror-view"
import {baseKeymap} from "prosemirror-commands"
import {history, undo, redo} from "prosemirror-history"
let histKeymap = keymap({"Mod-z": undo, "Mod-y": redo})
function start(place, content, schema, plugins = []) {
let doc = DOMParser.fromSchema(schema).parse(content)
return new EditorView(place, {
state: EditorState.create({
doc,
plugins: plugins.concat([histKeymap, keymap(baseKeymap), history()])
})
})
}
function id(str) { return document.getElementById(str) }
start({mount: id("text-editor")}, id("text-content"), textSchema)
start(id("note-editor"), id("note-content"), noteSchema, [keymap({"Ctrl-Space": makeNoteGroup})])
start(id("star-editor"), id("star-content"), starSchema, [starKeymap])