@atlaskit/editor-plugin-layout
Version:
Layout plugin for @atlaskit/editor-core
64 lines (62 loc) • 2.64 kB
JavaScript
import { ignoreResizerMutations } from '@atlaskit/editor-common/resizer/BreakoutResizer';
import { DOMSerializer } from '@atlaskit/editor-prosemirror/model';
import { fg } from '@atlaskit/platform-feature-flags/fg';
import { isEmptyLayout } from './utils';
const toDOM = (node, isEmpty) => {
var _node$attrs$columnRul, _node$attrs$localId;
return ['div', {
class: 'layoutSectionView-content-wrap'
}, ['div', {
class: 'layout-section-container'
}, ['div', {
'data-layout-section': true,
// `?? ''` keeps unset attributes out of the DOM as `"null"`, which adf-schema's`parseDOM`
// reads back as a truthy string.
'data-column-rule-style': (_node$attrs$columnRul = node.attrs.columnRuleStyle) !== null && _node$attrs$columnRul !== void 0 ? _node$attrs$columnRul : '',
'data-empty-layout': String(isEmpty),
...(fg('platform_editor_adf_with_localid') && {
'data-local-id': (_node$attrs$localId = node.attrs.localId) !== null && _node$attrs$localId !== void 0 ? _node$attrs$localId : ''
})
}, 0]]];
};
/**
* Requires `platform_editor_vanilla_node_views_phase1`.
*
* Also used for SSR, where it needs no `NodeViewContentHole` /
* `data-ssr-content-dom-ref` handling — that exists only because the React
* portal overwrites `container.innerHTML`.
*/
export class LayoutSectionView {
constructor(node) {
const isEmpty = isEmptyLayout(node);
const {
dom,
contentDOM
} = DOMSerializer.renderSpec(document, toDOM(node, isEmpty));
this.dom = dom;
this.contentDOM = contentDOM;
this.node = node;
this.isEmpty = isEmpty;
}
update(node) {
if (node.attrs.columnRuleStyle !== this.node.attrs.columnRuleStyle) {
var _node$attrs$columnRul2;
this.contentDOM.setAttribute('data-column-rule-style', (_node$attrs$columnRul2 = node.attrs.columnRuleStyle) !== null && _node$attrs$columnRul2 !== void 0 ? _node$attrs$columnRul2 : '');
}
// Cache isEmpty so update() does not call isEmptyLayout on the previous node.
const isEmpty = isEmptyLayout(node);
if (isEmpty !== this.isEmpty) {
this.contentDOM.setAttribute('data-empty-layout', String(isEmpty));
this.isEmpty = isEmpty;
}
if (fg('platform_editor_adf_with_localid') && node.attrs.localId !== this.node.attrs.localId) {
var _node$attrs$localId2;
this.contentDOM.setAttribute('data-local-id', (_node$attrs$localId2 = node.attrs.localId) !== null && _node$attrs$localId2 !== void 0 ? _node$attrs$localId2 : '');
}
this.node = node;
return true;
}
ignoreMutation(mutation) {
return ignoreResizerMutations(mutation);
}
}