1 import schema from "./schema";
2 import {DOMParser, DOMSerializer} from "prosemirror-model";
8 export function htmlToDoc(html) {
9 const renderDoc = document.implementation.createHTMLDocument();
10 renderDoc.body.innerHTML = html;
11 return DOMParser.fromSchema(schema).parse(renderDoc.body);
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;
26 * @param {PmEditorState} state
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;
37 * @param {Object} object
40 export function nullifyEmptyValues(object) {
42 for (const [key, value] of Object.entries(object)) {
43 clean[key] = (value === "") ? null : value;
49 * @param {PmEditorState} state
50 * @param {PmMarkType} markType
52 * @return {{from: Number, to: Number}}
54 export function markRangeAtPosition(state, markType, pos) {
55 const $pos = state.doc.resolve(pos);
57 const { parent, parentOffset } = $pos;
58 const start = parent.childAfter(parentOffset);
59 if (!start.node) return {from: -1, to: -1};
61 const mark = start.node.marks.find((mark) => mark.type === markType);
62 if (!mark) return {from: -1, to: -1};
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)) {
70 startPos -= parent.child(startIndex).nodeSize;
72 while (endIndex < parent.childCount && mark.isInSet(parent.child(endIndex).marks)) {
73 endPos += parent.child(endIndex).nodeSize;
76 return { from: startPos, to: endPos };
80 * @class KeyedMultiStack
81 * Holds many stacks, seperated via a key, with a simple
82 * interface to pop and push values to the stacks.
84 export class KeyedMultiStack {
92 * @return {undefined|*}
95 if (Array.isArray(this.stack[key])) {
96 return this.stack[key].pop();
102 * @param {String} key
106 if (this.stack[key] === undefined) {
107 this.stack[key] = [];
110 this.stack[key].push(value);