@atlaskit/editor-plugin-rule
Version:
Rule plugin for @atlaskit/editor-core
57 lines • 1.72 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import { bind } from 'bind-event-listener';
import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
const ruleNodeViewAttribute = 'data-rule-node-view';
export class RuleNodeView {
constructor(node, view) {
// when there is only 1 rule node on the page and it is not editable, clicking on it does not focus the editor and Cmd+C/ Ctrl+C shortcut will not work. this mouse down listener adds focus to the editor.
_defineProperty(this, "handleMousedown", () => {
if (!this.view.hasFocus()) {
this.view.focus();
}
});
this.node = node;
this.view = view;
const dom = this.view.dom.ownerDocument.createElement('div');
this.dom = dom;
dom.setAttribute(ruleNodeViewAttribute, '');
dom.setAttribute('contenteditable', 'inherit');
this.unbindMousedown = bind(dom, {
type: 'mousedown',
listener: this.handleMousedown
});
this.update(node);
}
update(node) {
if (node.type !== this.node.type) {
return false;
}
if (this.dom.firstChild && node.sameMarkup(this.node)) {
this.node = node;
return true;
}
const toDOM = node.type.spec.toDOM;
if (!toDOM) {
return false;
}
const {
dom
} = DOMSerializer.renderSpec(this.view.dom.ownerDocument, toDOM(node));
if (dom instanceof HTMLElement) {
dom.contentEditable = 'false';
}
if (this.dom.firstChild) {
this.dom.replaceChild(dom, this.dom.firstChild);
} else {
this.dom.appendChild(dom);
}
this.node = node;
return true;
}
ignoreMutation() {
return true;
}
destroy() {
this.unbindMousedown();
}
}