]> BookStack Code Mirror - bookstack/blob - resources/js/editor/util.js
Got underline working in editor
[bookstack] / resources / js / editor / util.js
1 import schema from "./schema";
2 import {DOMParser, DOMSerializer} from "prosemirror-model";
3
4
5 export function htmlToDoc(html) {
6     const renderDoc = document.implementation.createHTMLDocument();
7     renderDoc.body.innerHTML = html;
8     return DOMParser.fromSchema(schema).parse(renderDoc.body);
9 }
10
11 export function docToHtml(doc) {
12     const fragment = DOMSerializer.fromSchema(schema).serializeFragment(doc.content);
13     const renderDoc = document.implementation.createHTMLDocument();
14     renderDoc.body.appendChild(fragment);
15     return renderDoc.body.innerHTML;
16 }
17
18 /**
19  * @class KeyedMultiStack
20  * Holds many stacks, seperated via a key, with a simple
21  * interface to pop and push values to the stacks.
22  */
23 export class KeyedMultiStack {
24
25     constructor() {
26         this.stack = {};
27     }
28
29     /**
30      * @param {String} key
31      * @return {undefined|*}
32      */
33     pop(key) {
34         if (Array.isArray(this.stack[key])) {
35             return this.stack[key].pop();
36         }
37         return undefined;
38     }
39
40     /**
41      * @param {String} key
42      * @param {*} value
43      */
44     push(key, value) {
45         if (this.stack[key] === undefined) {
46             this.stack[key] = [];
47         }
48
49         this.stack[key].push(value);
50     }
51 }