@atlaskit/editor-plugin-caption
Version:
Caption plugin for @atlaskit/editor-core
67 lines (65 loc) • 2.78 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
/**
* DOM spec for the caption node view.
*/
function toDOM(node) {
return ['figcaption', _objectSpread({
'data-caption': 'true',
'data-media-caption': 'true',
'data-testid': 'media-caption'
}, node.attrs.localId ? {
'data-local-id': node.attrs.localId
} : {}), ['div', {
class: 'captionView-content-wrap'
}, 0]];
}
function createCaptionDOM(node) {
return DOMSerializer.renderSpec(document, toDOM(node));
}
/**
* Vanilla (React-free) implementation of CaptionNodeView.
*
* Replaces SelectionBasedNodeView + CaptionComponent React render. The node view owns the DOM
* shape and nothing else: editability is inherited from the ProseMirror root rather than being
* set here, so disabling the editor cannot be overridden by a stale nested `contenteditable`.
*/
export var CaptionNodeView = /*#__PURE__*/function () {
function CaptionNodeView(node) {
_classCallCheck(this, CaptionNodeView);
this.node = node;
var _createCaptionDOM = createCaptionDOM(node),
dom = _createCaptionDOM.dom,
contentDOM = _createCaptionDOM.contentDOM;
this.dom = dom;
this.contentDOM = contentDOM;
}
return _createClass(CaptionNodeView, [{
key: "update",
value: function update(node, _decorations, _innerDecorations) {
if (node.type !== this.node.type) {
return false;
}
this.node = node;
return true;
}
}, {
key: "ignoreMutation",
value: function ignoreMutation(mutation) {
if (!this.contentDOM) {
return true;
}
return !this.contentDOM.contains(mutation.target) && mutation.type !== 'selection';
}
}]);
}();
/** Factory function for ProseMirror node view registration. */
export function captionNodeView() {
return function (node) {
return new CaptionNodeView(node);
};
}