]> BookStack Code Mirror - bookstack/blob - resources/js/editor/schema-nodes.js
Got alignment buttons barely working for paragraphs
[bookstack] / resources / js / editor / schema-nodes.js
1 import {orderedList, bulletList, listItem} from "prosemirror-schema-list";
2
3 const alignAttrFromDomNode = node => {
4     if (node.classList.contains('align-right')) {
5         return 'right';
6     }
7     if (node.classList.contains('align-left')) {
8         return 'left';
9     }
10     if (node.classList.contains('align-center')) {
11         return 'center';
12     }
13     if (node.classList.contains('align-justify')) {
14         return 'justify';
15     }
16     return null;
17 };
18
19 const doc = {
20     content: "block+",
21 };
22
23 const paragraph = {
24     content: "inline*",
25     group: "block",
26     parseDOM: [
27         {
28             tag: "p",
29             getAttrs(node) {
30                 return {
31                     align: alignAttrFromDomNode(node),
32                 };
33             }
34         }
35     ],
36     attrs: {
37         align: {
38             default: null,
39         }
40     },
41     toDOM(node) {
42         const attrs = {};
43         if (node.attrs.align === 'right') {
44             attrs['class'] = 'align-right';
45         }
46         if (node.attrs.align === 'left') {
47             attrs['class'] = 'align-left';
48         }
49         return ["p", attrs, 0];
50     }
51 };
52
53 const blockquote = {
54     content: "block+",
55     group: "block",
56     defining: true,
57     parseDOM: [{tag: "blockquote"}],
58     align: {
59         default: null,
60     },
61     toDOM() {
62         return ["blockquote", 0];
63     }
64 };
65
66 const horizontal_rule = {
67     group: "block",
68     parseDOM: [{tag: "hr"}],
69     toDOM() {
70         return ["hr"];
71     }
72 };
73
74 const heading = {
75     attrs: {level: {default: 1}, align: {default: null}},
76     content: "inline*",
77     group: "block",
78     defining: true,
79     parseDOM: [{tag: "h1", attrs: {level: 1}},
80         {tag: "h2", attrs: {level: 2}},
81         {tag: "h3", attrs: {level: 3}},
82         {tag: "h4", attrs: {level: 4}},
83         {tag: "h5", attrs: {level: 5}},
84         {tag: "h6", attrs: {level: 6}}],
85     toDOM(node) {
86         return ["h" + node.attrs.level, 0]
87     }
88 };
89
90 const code_block = {
91     content: "text*",
92     marks: "",
93     group: "block",
94     code: true,
95     defining: true,
96     parseDOM: [{tag: "pre", preserveWhitespace: "full"}],
97     toDOM() {
98         return ["pre", ["code", 0]];
99     }
100 };
101
102 const text = {
103     group: "inline"
104 };
105
106 const image = {
107     inline: true,
108     attrs: {
109         src: {},
110         alt: {default: null},
111         title: {default: null}
112     },
113     group: "inline",
114     draggable: true,
115     parseDOM: [{
116         tag: "img[src]", getAttrs: function getAttrs(dom) {
117             return {
118                 src: dom.getAttribute("src"),
119                 title: dom.getAttribute("title"),
120                 alt: dom.getAttribute("alt")
121             }
122         }
123     }],
124     toDOM: function toDOM(node) {
125         const ref = node.attrs;
126         const src = ref.src;
127         const alt = ref.alt;
128         const title = ref.title;
129         return ["img", {src: src, alt: alt, title: title}]
130     }
131 };
132
133 const hard_break = {
134     inline: true,
135     group: "inline",
136     selectable: false,
137     parseDOM: [{tag: "br"}],
138     toDOM() {
139         return ["br"];
140     }
141 };
142
143 const callout = {
144     attrs: {
145         type: {default: 'info'},
146         align: {default: null},
147     },
148     content: "inline*",
149     group: "block",
150     defining: true,
151     parseDOM: [
152         {tag: 'p.callout.info', attrs: {type: 'info'}, priority: 75,},
153         {tag: 'p.callout.success', attrs: {type: 'success'}, priority: 75,},
154         {tag: 'p.callout.danger', attrs: {type: 'danger'}, priority: 75,},
155         {tag: 'p.callout.warning', attrs: {type: 'warning'}, priority: 75,},
156         {tag: 'p.callout', attrs: {type: 'info'}, priority: 75},
157     ],
158     toDOM(node) {
159         const type = node.attrs.type || 'info';
160         return ['p', {class: 'callout ' + type}, 0];
161     }
162 };
163
164 const ordered_list = Object.assign({}, orderedList, {content: "list_item+", group: "block"});
165 const bullet_list = Object.assign({}, bulletList, {content: "list_item+", group: "block"});
166 const list_item = Object.assign({}, listItem, {content: 'paragraph block*'});
167
168 const nodes = {
169     doc,
170     paragraph,
171     blockquote,
172     horizontal_rule,
173     heading,
174     code_block,
175     text,
176     image,
177     hard_break,
178     callout,
179     ordered_list,
180     bullet_list,
181     list_item,
182 };
183
184 export default nodes;