1 import {orderedList, bulletList, listItem} from "prosemirror-schema-list";
2 import {Fragment} from "prosemirror-model";
5 * @param {HTMLElement} node
6 * @return {string|null}
8 function getAlignAttrFromDomNode(node) {
9 const classList = node.classList;
10 const styles = node.style || {};
11 const alignments = ['right', 'left', 'center', 'justify'];
12 for (const alignment of alignments) {
13 if (classList.contains('align-' + alignment) || styles.textAlign === alignment) {
22 * @param {Object} attrs
25 function addAlignmentAttr(node, attrs) {
26 const positions = ['right', 'left', 'center', 'justify'];
27 for (const position of positions) {
28 if (node.attrs.align === position) {
29 return addClassToAttrs('align-' + position, attrs);
35 function getAttrsParserForAlignment(node) {
37 align: getAlignAttrFromDomNode(node),
42 * @param {String} className
43 * @param {Object} attrs
46 function addClassToAttrs(className, attrs) {
47 return Object.assign({}, attrs, {
48 class: attrs.class ? attrs.class + ' ' + className : className,
53 * @param {String[]} attrNames
54 * @return {function(Element): {}}
56 function domAttrsToAttrsParser(attrNames) {
57 return function (node) {
59 for (const attr of attrNames) {
60 attrs[attr] = node.hasAttribute(attr) ? node.getAttribute(attr) : null;
67 * @param {PmNode} node
68 * @param {String[]} attrNames
70 function extractAttrsForDom(node, attrNames) {
72 for (const attr of attrNames) {
73 if (node.attrs[attr]) {
74 domAttrs[attr] = node.attrs[attr];
90 getAttrs: getAttrsParserForAlignment,
99 return ["p", addAlignmentAttr(node, {}), 0];
107 parseDOM: [{tag: "blockquote", getAttrs: getAttrsParserForAlignment}],
114 return ["blockquote", addAlignmentAttr(node, {}), 0];
118 const horizontal_rule = {
120 parseDOM: [{tag: "hr"}],
127 const headingParseGetAttrs = (level) => {
128 return function (node) {
129 return {level, align: getAlignAttrFromDomNode(node)};
133 attrs: {level: {default: 1}, align: {default: null}},
138 {tag: "h1", getAttrs: headingParseGetAttrs(1)},
139 {tag: "h2", getAttrs: headingParseGetAttrs(2)},
140 {tag: "h3", getAttrs: headingParseGetAttrs(3)},
141 {tag: "h4", getAttrs: headingParseGetAttrs(4)},
142 {tag: "h5", getAttrs: headingParseGetAttrs(5)},
143 {tag: "h6", getAttrs: headingParseGetAttrs(6)},
146 return ["h" + node.attrs.level, addAlignmentAttr(node, {}), 0]
156 parseDOM: [{tag: "pre", preserveWhitespace: "full"}],
158 return ["pre", ["code", 0]];
170 alt: {default: null},
171 title: {default: null},
172 height: {default: null},
173 width: {default: null},
178 tag: "img[src]", getAttrs: function getAttrs(dom) {
180 src: dom.getAttribute("src"),
181 title: dom.getAttribute("title"),
182 alt: dom.getAttribute("alt"),
183 height: dom.getAttribute("height"),
184 width: dom.getAttribute("width"),
188 toDOM: function toDOM(node) {
189 const ref = node.attrs;
192 const title = ref.title;
193 const width = ref.width;
194 const height = ref.height;
195 return ["img", {src, alt, title, width, height}]
202 height: {default: null},
203 width: {default: null},
204 title: {default: null},
205 allow: {default: null},
206 sandbox: {default: null},
212 getAttrs: domAttrsToAttrsParser(["src", "width", "height", "title", "allow", "sandbox"]),
215 const attrs = extractAttrsForDom(node, ["src", "width", "height", "title", "allow", "sandbox"])
216 return ["iframe", attrs];
224 parseDOM: [{tag: "br"}],
231 const calloutParseGetAttrs = (type) => {
232 return function (node) {
233 return {type, align: getAlignAttrFromDomNode(node)};
238 type: {default: 'info'},
239 align: {default: null},
245 {tag: 'p.callout.info', getAttrs: calloutParseGetAttrs('info'), priority: 75},
246 {tag: 'p.callout.success', getAttrs: calloutParseGetAttrs('success'), priority: 75},
247 {tag: 'p.callout.danger', getAttrs: calloutParseGetAttrs('danger'), priority: 75},
248 {tag: 'p.callout.warning', getAttrs: calloutParseGetAttrs('warning'), priority: 75},
249 {tag: 'p.callout', getAttrs: calloutParseGetAttrs('info'), priority: 75},
252 const type = node.attrs.type || 'info';
253 return ['p', addAlignmentAttr(node, {class: 'callout ' + type}), 0];
257 const ordered_list = Object.assign({}, orderedList, {content: "list_item+", group: "block"});
258 const bullet_list = Object.assign({}, bulletList, {content: "list_item+", group: "block"});
259 const list_item = Object.assign({}, listItem, {content: 'paragraph block*'});
262 content: "table_row+",
264 style: {default: null},
269 parseDOM: [{tag: "table", getAttrs: domAttrsToAttrsParser(['style'])}],
271 return ["table", extractAttrsForDom(node, ['style']), ["tbody", 0]]
276 content: "(table_cell | table_header)*",
278 parseDOM: [{tag: "tr"}],
279 toDOM() { return ["tr", 0] }
283 colspan: {default: 1},
284 rowspan: {default: 1},
285 width: {default: null},
286 height: {default: null},
289 function getCellAttrs(dom) {
291 colspan: Number(dom.getAttribute("colspan") || 1),
292 rowspan: Number(dom.getAttribute("rowspan") || 1),
293 width: dom.style.width || null,
294 height: dom.style.height || null,
298 function setCellAttrs(node) {
302 if (node.attrs.colspan != 1) attrs.colspan = node.attrs.colspan;
303 if (node.attrs.rowspan != 1) attrs.rowspan = node.attrs.rowspan;
304 if (node.attrs.width) styles.push(`width: ${node.attrs.width}`);
305 if (node.attrs.height) styles.push(`height: ${node.attrs.height}`);
307 attrs.style = styles.join(';');
318 parseDOM: [{tag: "td", getAttrs: dom => getCellAttrs(dom)}],
319 toDOM(node) { return ["td", setCellAttrs(node), 0] }
322 const table_header = {
325 tableRole: "header_cell",
327 parseDOM: [{tag: "th", getAttrs: dom => getCellAttrs(dom)}],
328 toDOM(node) { return ["th", setCellAttrs(node), 0] }
333 content: "details_summary block*",
343 return ["details", 0];
347 const details_summary = {
351 tag: "details summary",
354 return ["summary", 0];
383 export default nodes;