@atlaskit/editor-plugin-breakout
Version:
Breakout plugin for @atlaskit/editor-core
33 lines • 1.3 kB
JavaScript
import { transferCodeBlockWrappedValue } from '@atlaskit/editor-common/code-block';
import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
import { findSupportedNodeForBreakout } from '../pm-plugins/utils/find-breakout-node';
import { updateExpandedState } from '../pm-plugins/utils/single-player-expand';
export function removeBreakout(isLivePage) {
return (state, dispatch) => {
const node = findSupportedNodeForBreakout(state.selection);
if (!node) {
return false;
}
const marks = node.node.marks.filter(m => m.type.name !== 'breakout');
const tr = state.tr.setNodeMarkup(node.pos, node.node.type, node.node.attrs, marks);
if (node.node.type === state.schema.nodes.expand) {
updateExpandedState(tr, node, isLivePage);
} else if (node.node.type === state.schema.nodes.codeBlock) {
const newNode = tr.doc.nodeAt(node.pos);
const oldNode = node.node;
if (newNode) {
transferCodeBlockWrappedValue(oldNode, newNode);
}
}
tr.setMeta('scrollIntoView', false);
if (state.selection instanceof NodeSelection) {
if (state.selection.$anchor.pos === node.pos) {
tr.setSelection(NodeSelection.create(tr.doc, node.pos));
}
}
if (dispatch) {
dispatch(tr);
}
return true;
};
}