@atlaskit/editor-plugin-rule
Version:
Rule plugin for @atlaskit/editor-core
67 lines • 2.23 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import { bind } from 'bind-event-listener';
import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
var ruleNodeViewAttribute = 'data-rule-node-view';
export var RuleNodeView = /*#__PURE__*/function () {
function RuleNodeView(node, view) {
var _this = this;
_classCallCheck(this, RuleNodeView);
// 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", function () {
if (!_this.view.hasFocus()) {
_this.view.focus();
}
});
this.node = node;
this.view = view;
var 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);
}
return _createClass(RuleNodeView, [{
key: "update",
value: function update(node) {
if (node.type !== this.node.type) {
return false;
}
if (this.dom.firstChild && node.sameMarkup(this.node)) {
this.node = node;
return true;
}
var toDOM = node.type.spec.toDOM;
if (!toDOM) {
return false;
}
var _DOMSerializer$render = DOMSerializer.renderSpec(this.view.dom.ownerDocument, toDOM(node)),
dom = _DOMSerializer$render.dom;
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;
}
}, {
key: "ignoreMutation",
value: function ignoreMutation() {
return true;
}
}, {
key: "destroy",
value: function destroy() {
this.unbindMousedown();
}
}]);
}();