]> BookStack Code Mirror - bookstack/blob - resources/js/editor/util.js
f94aa10fc93a908c69b04b13d014736e2a9bb546
[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  * @class KeyedMultiStack
27  * Holds many stacks, seperated via a key, with a simple
28  * interface to pop and push values to the stacks.
29  */
30 export class KeyedMultiStack {
31
32     constructor() {
33         this.stack = {};
34     }
35
36     /**
37      * @param {String} key
38      * @return {undefined|*}
39      */
40     pop(key) {
41         if (Array.isArray(this.stack[key])) {
42             return this.stack[key].pop();
43         }
44         return undefined;
45     }
46
47     /**
48      * @param {String} key
49      * @param {*} value
50      */
51     push(key, value) {
52         if (this.stack[key] === undefined) {
53             this.stack[key] = [];
54         }
55
56         this.stack[key].push(value);
57     }
58 }