]> BookStack Code Mirror - bookstack/blob - resources/js/editor/util.js
e1746a72517c0c599a3fcc338ac1c54436e068ee
[bookstack] / resources / js / editor / util.js
1 import schema from "./schema";
2 import {DOMParser, DOMSerializer} from "prosemirror-model";
3
4 /**
5  * @param {String} html
6  * @return {PmNode}
7  */
8 export function htmlToDoc(html) {
9     const renderDoc = document.implementation.createHTMLDocument();
10     renderDoc.body.innerHTML = html;
11     return DOMParser.fromSchema(schema).parse(renderDoc.body);
12 }
13
14 /**
15  * @param {PmNode} doc
16  * @return {string}
17  */
18 export function docToHtml(doc) {
19     const fragment = DOMSerializer.fromSchema(schema).serializeFragment(doc.content);
20     const renderDoc = document.implementation.createHTMLDocument();
21     renderDoc.body.appendChild(fragment);
22     return renderDoc.body.innerHTML;
23 }
24
25 /**
26  * @param {PmEditorState} state
27  * @return {String}
28  */
29 export function stateToHtml(state) {
30     const fragment = DOMSerializer.fromSchema(schema).serializeFragment(state.doc.content);
31     const renderDoc = document.implementation.createHTMLDocument();
32     renderDoc.body.appendChild(fragment);
33     return renderDoc.body.innerHTML;
34 }
35
36 /**
37  * @param {Object} object
38  * @return {{}}
39  */
40 export function nullifyEmptyValues(object) {
41     const clean = {};
42     for (const [key, value] of Object.entries(object)) {
43         clean[key] = (value === "") ? null : value;
44     }
45     return clean;
46 }
47
48 /**
49  * @param {PmEditorState} state
50  * @param {PmMarkType} markType
51  * @param {Number} pos
52  * @return {{from: Number, to: Number}}
53  */
54 export function markRangeAtPosition(state, markType, pos) {
55     const $pos = state.doc.resolve(pos);
56
57     const { parent, parentOffset } = $pos;
58     const start = parent.childAfter(parentOffset);
59     if (!start.node) return {from: -1, to: -1};
60
61     const mark = start.node.marks.find((mark) => mark.type === markType);
62     if (!mark) return {from: -1, to: -1};
63
64     let startIndex = $pos.index();
65     let startPos = $pos.start() + start.offset;
66     let endIndex = startIndex + 1;
67     let endPos = startPos + start.node.nodeSize;
68     while (startIndex > 0 && mark.isInSet(parent.child(startIndex - 1).marks)) {
69         startIndex -= 1;
70         startPos -= parent.child(startIndex).nodeSize;
71     }
72     while (endIndex < parent.childCount && mark.isInSet(parent.child(endIndex).marks)) {
73         endPos += parent.child(endIndex).nodeSize;
74         endIndex += 1;
75     }
76     return { from: startPos, to: endPos };
77 }
78
79 /**
80  * @class KeyedMultiStack
81  * Holds many stacks, seperated via a key, with a simple
82  * interface to pop and push values to the stacks.
83  */
84 export class KeyedMultiStack {
85
86     constructor() {
87         this.stack = {};
88     }
89
90     /**
91      * @param {String} key
92      * @return {undefined|*}
93      */
94     pop(key) {
95         if (Array.isArray(this.stack[key])) {
96             return this.stack[key].pop();
97         }
98         return undefined;
99     }
100
101     /**
102      * @param {String} key
103      * @param {*} value
104      */
105     push(key, value) {
106         if (this.stack[key] === undefined) {
107             this.stack[key] = [];
108         }
109
110         this.stack[key].push(value);
111     }
112 }