-
Notifications
You must be signed in to change notification settings - Fork 131
/
index.js
88 lines (77 loc) · 2.65 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
// nodespec{
// The supported types of dinosaurs.
const dinos = ["brontosaurus", "stegosaurus", "triceratops",
"tyrannosaurus", "pterodactyl"]
const dinoNodeSpec = {
// Dinosaurs have one attribute, their type, which must be one of
// the types defined above.
// Brontosaurs are still the default dino.
attrs: {type: {default: "brontosaurus"}},
inline: true,
group: "inline",
draggable: true,
// These nodes are rendered as images with a `dino-type` attribute.
// There are pictures for all dino types under /img/dino/.
toDOM: node => ["img", {"dino-type": node.attrs.type,
src: "/img/dino/" + node.attrs.type + ".png",
title: node.attrs.type,
class: "dinosaur"}],
// When parsing, such an image, if its type matches one of the known
// types, is converted to a dino node.
parseDOM: [{
tag: "img[dino-type]",
getAttrs: dom => {
let type = dom.getAttribute("dino-type")
return dinos.indexOf(type) > -1 ? {type} : false
}
}]
}
// }
// schema{
import {Schema, DOMParser} from "prosemirror-model"
import {schema} from "prosemirror-schema-basic"
const dinoSchema = new Schema({
nodes: schema.spec.nodes.addBefore("image", "dino", dinoNodeSpec),
marks: schema.spec.marks
})
let content = document.querySelector("#content")
let startDoc = DOMParser.fromSchema(dinoSchema).parse(content)
// }
// command{
let dinoType = dinoSchema.nodes.dino
function insertDino(type) {
return function(state, dispatch) {
let {$from} = state.selection, index = $from.index()
if (!$from.parent.canReplaceWith(index, index, dinoType))
return false
if (dispatch)
dispatch(state.tr.replaceSelectionWith(dinoType.create({type})))
return true
}
}
// }
// menu{
import {MenuItem} from "prosemirror-menu"
import {buildMenuItems} from "prosemirror-example-setup"
// Ask example-setup to build its basic menu
let menu = buildMenuItems(dinoSchema)
// Add a dino-inserting item for each type of dino
dinos.forEach(name => menu.insertMenu.content.push(new MenuItem({
title: "Insert " + name,
label: name.charAt(0).toUpperCase() + name.slice(1),
enable(state) { return insertDino(name)(state) },
run: insertDino(name)
})))
// }
// editor{
import {EditorState} from "prosemirror-state"
import {EditorView} from "prosemirror-view"
import {exampleSetup} from "prosemirror-example-setup"
window.view = new EditorView(document.querySelector("#editor"), {
state: EditorState.create({
doc: startDoc,
// Pass exampleSetup our schema and the menu we created
plugins: exampleSetup({schema: dinoSchema, menuContent: menu.fullMenu})
})
})
// }