|
| 1 | +import type { Mark } from "prosemirror-model"; |
| 2 | +import type { |
| 3 | + EditorView, |
| 4 | + MarkView, |
| 5 | + ViewMutationRecord, |
| 6 | +} from "prosemirror-view"; |
| 7 | + |
| 8 | +import type { BlockNoteEditor } from "@blocknote/core"; |
| 9 | +import type { |
| 10 | + CoreMarkViewSpec, |
| 11 | + CoreMarkViewUserOptions, |
| 12 | + MarkViewDOMSpec, |
| 13 | +} from "./CoreMarkViewOptions.js"; |
| 14 | + |
| 15 | +/* eslint-disable curly */ |
| 16 | + |
| 17 | +export class CoreMarkView<ComponentType> implements MarkView { |
| 18 | + dom: HTMLElement; |
| 19 | + contentDOM: HTMLElement | undefined; |
| 20 | + mark: Mark; |
| 21 | + view: EditorView; |
| 22 | + inline: boolean; |
| 23 | + options: CoreMarkViewUserOptions<ComponentType>; |
| 24 | + editor: BlockNoteEditor<any, any, any>; |
| 25 | + |
| 26 | + #createElement(as?: MarkViewDOMSpec) { |
| 27 | + const { inline, mark } = this; |
| 28 | + return as == null |
| 29 | + ? document.createElement(inline ? "span" : "div") |
| 30 | + : as instanceof HTMLElement |
| 31 | + ? as |
| 32 | + : as instanceof Function |
| 33 | + ? as(mark) |
| 34 | + : document.createElement(as); |
| 35 | + } |
| 36 | + |
| 37 | + createDOM(as?: MarkViewDOMSpec) { |
| 38 | + return this.#createElement(as); |
| 39 | + } |
| 40 | + |
| 41 | + createContentDOM(as?: MarkViewDOMSpec) { |
| 42 | + return this.#createElement(as); |
| 43 | + } |
| 44 | + |
| 45 | + constructor({ |
| 46 | + mark, |
| 47 | + view, |
| 48 | + inline, |
| 49 | + options, |
| 50 | + editor, // BlockNote specific |
| 51 | + }: CoreMarkViewSpec<ComponentType>) { |
| 52 | + this.mark = mark; |
| 53 | + this.view = view; |
| 54 | + this.inline = inline; |
| 55 | + this.options = options; |
| 56 | + this.editor = editor; |
| 57 | + |
| 58 | + this.dom = this.createDOM(options.as); |
| 59 | + this.contentDOM = options.contentAs |
| 60 | + ? this.createContentDOM(options.contentAs) |
| 61 | + : undefined; |
| 62 | + this.dom.setAttribute("data-mark-view-root", "true"); |
| 63 | + if (this.contentDOM) { |
| 64 | + this.contentDOM.setAttribute("data-mark-view-content", "true"); |
| 65 | + this.contentDOM.style.whiteSpace = "inherit"; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + get component() { |
| 70 | + return this.options.component; |
| 71 | + } |
| 72 | + |
| 73 | + shouldIgnoreMutation: (mutation: ViewMutationRecord) => boolean = ( |
| 74 | + mutation |
| 75 | + ) => { |
| 76 | + if (!this.dom || !this.contentDOM) return true; |
| 77 | + |
| 78 | + if (mutation.type === "selection") return false; |
| 79 | + |
| 80 | + if (this.contentDOM === mutation.target && mutation.type === "attributes") |
| 81 | + return true; |
| 82 | + |
| 83 | + if (this.contentDOM.contains(mutation.target)) return false; |
| 84 | + |
| 85 | + return true; |
| 86 | + }; |
| 87 | + |
| 88 | + ignoreMutation: (mutation: ViewMutationRecord) => boolean = (mutation) => { |
| 89 | + if (!this.dom || !this.contentDOM) return true; |
| 90 | + |
| 91 | + let result; |
| 92 | + |
| 93 | + const userIgnoreMutation = this.options.ignoreMutation; |
| 94 | + |
| 95 | + if (userIgnoreMutation) result = userIgnoreMutation(mutation); |
| 96 | + |
| 97 | + if (typeof result !== "boolean") |
| 98 | + result = this.shouldIgnoreMutation(mutation); |
| 99 | + |
| 100 | + return result; |
| 101 | + }; |
| 102 | + |
| 103 | + public destroy() { |
| 104 | + this.options.destroy?.(); |
| 105 | + this.dom.remove(); |
| 106 | + this.contentDOM?.remove(); |
| 107 | + } |
| 108 | +} |
0 commit comments