@atlaskit/editor-plugin-caption
Version:
Caption plugin for @atlaskit/editor-core
58 lines (56 loc) • 1.57 kB
JavaScript
import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
/**
* DOM spec for the caption node view.
*/
function toDOM(node) {
return ['figcaption', {
'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 class CaptionNodeView {
constructor(node) {
this.node = node;
const {
dom,
contentDOM
} = createCaptionDOM(node);
this.dom = dom;
this.contentDOM = contentDOM;
}
update(node, _decorations, _innerDecorations) {
if (node.type !== this.node.type) {
return false;
}
this.node = node;
return true;
}
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 node => {
return new CaptionNodeView(node);
};
}