@atlaskit/editor-plugin-layout
Version:
Layout plugin for @atlaskit/editor-core
226 lines (222 loc) • 10.5 kB
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { createSelectionClickHandler } from '@atlaskit/editor-common/selection';
import { filterCommand as filter } from '@atlaskit/editor-common/utils';
import { keydownHandler } from '@atlaskit/editor-prosemirror/keymap';
import { Fragment } from '@atlaskit/editor-prosemirror/model';
import { NodeSelection, Selection, TextSelection } from '@atlaskit/editor-prosemirror/state';
import { findParentNodeClosestToPos, findParentNodeOfType } from '@atlaskit/editor-prosemirror/utils';
import { Decoration, DecorationSet } from '@atlaskit/editor-prosemirror/view';
import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
import { fixColumnSizes, fixColumnStructure, getSelectedLayout } from './actions';
import { getColumnDividerDecorations } from './column-resize-divider';
import { EVEN_DISTRIBUTED_COL_WIDTHS } from './consts';
import { pluginKey } from './plugin-key';
import { pluginKey as layoutResizingPluginKey } from './resizing';
import { getMaybeLayoutSection } from './utils';
export const DEFAULT_LAYOUT = 'two_equal';
const isWholeSelectionInsideLayoutColumn = state => {
// Since findParentNodeOfType doesn't check if selection.to shares the parent, we do this check ourselves
const fromParent = findParentNodeOfType(state.schema.nodes.layoutColumn)(state.selection);
if (fromParent) {
const isToPosInsideSameLayoutColumn = state.selection.from < fromParent.pos + fromParent.node.nodeSize;
return isToPosInsideSameLayoutColumn;
}
return false;
};
const moveCursorToNextColumn = (state, dispatch) => {
const {
selection
} = state;
const {
schema: {
nodes: {
layoutColumn,
layoutSection
}
}
} = state;
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const section = findParentNodeOfType(layoutSection)(selection);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const column = findParentNodeOfType(layoutColumn)(selection);
if (column.node !== section.node.lastChild) {
const $nextColumn = state.doc.resolve(column.pos + column.node.nodeSize);
const shiftedSelection = TextSelection.findFrom($nextColumn, 1);
if (dispatch) {
dispatch(state.tr.setSelection(shiftedSelection));
}
}
return true;
};
const getNodeDecoration = (pos, node) => [Decoration.node(pos, pos + node.nodeSize, {
class: 'selected'
})];
const getInitialPluginState = (options, state) => {
const maybeLayoutSection = getMaybeLayoutSection(state);
const allowBreakout = options.allowBreakout || false;
const addSidebarLayouts = options.UNSAFE_addSidebarLayouts || false;
const allowSingleColumnLayout = options.UNSAFE_allowSingleColumnLayout || false;
const pos = maybeLayoutSection ? maybeLayoutSection.pos : null;
const selectedLayout = getSelectedLayout(maybeLayoutSection && maybeLayoutSection.node, DEFAULT_LAYOUT);
return {
pos,
allowBreakout,
addSidebarLayouts,
selectedLayout,
allowSingleColumnLayout,
isResizing: false
};
};
// To prevent a single-column layout,
// if a user attempts to delete a layout column and
// we will force remove the content instead.
// There are some edge cases where user can delete a layout column
// see packages/editor/editor-plugin-layout-tests/src/__tests__/unit/delete.ts
const handleDeleteLayoutColumn = (state, dispatch) => {
const sel = state.selection;
if (sel instanceof NodeSelection && sel.node.type.name === 'layoutColumn' && sel.$from.parent.type.name === 'layoutSection' && sel.$from.parent.childCount === 2 && dispatch && editorExperiment('advanced_layouts', true) && !editorExperiment('single_column_layouts', true)) {
var _sel$$from$parent$las, _sel$$from$parent$fir;
const tr = state.tr;
const layoutContentFragment = sel.$from.parentOffset === 0 ? Fragment.from((_sel$$from$parent$las = sel.$from.parent.lastChild) === null || _sel$$from$parent$las === void 0 ? void 0 : _sel$$from$parent$las.content) : Fragment.from((_sel$$from$parent$fir = sel.$from.parent.firstChild) === null || _sel$$from$parent$fir === void 0 ? void 0 : _sel$$from$parent$fir.content);
const parent = findParentNodeClosestToPos(sel.$from, node => {
return node.type.name === 'layoutSection';
});
if (parent) {
const layoutSectionPos = tr.mapping.map(parent.pos);
const layoutSectionNodeSize = parent.node.nodeSize;
dispatch(state.tr.replaceWith(layoutSectionPos, layoutSectionPos + layoutSectionNodeSize, layoutContentFragment));
return true;
}
return false;
}
return false;
};
export default (options => {
// Store a reference to the EditorView so widget decorations can dispatch transactions
let editorViewRef;
return new SafePlugin({
key: pluginKey,
view(view) {
editorViewRef = view;
return {
update(updatedView) {
editorViewRef = updatedView;
},
destroy() {
editorViewRef = undefined;
}
};
},
state: {
init: (_, state) => getInitialPluginState(options, state),
apply: (tr, pluginState, oldState, newState) => {
var _tr$getMeta, _pluginKey$getState;
const isResizing = editorExperiment('single_column_layouts', true) ? (_tr$getMeta = tr.getMeta('is-resizer-resizing')) !== null && _tr$getMeta !== void 0 ? _tr$getMeta : (_pluginKey$getState = pluginKey.getState(oldState)) === null || _pluginKey$getState === void 0 ? void 0 : _pluginKey$getState.isResizing : false;
if (tr.docChanged || tr.selectionSet) {
const maybeLayoutSection = getMaybeLayoutSection(newState);
const newPluginState = {
...pluginState,
pos: maybeLayoutSection ? maybeLayoutSection.pos : null,
isResizing,
selectedLayout: getSelectedLayout(maybeLayoutSection && maybeLayoutSection.node,
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
pluginState.selectedLayout)
};
return newPluginState;
}
return {
...pluginState,
isResizing
};
}
},
props: {
decorations(state) {
const layoutState = pluginKey.getState(state);
const isLayoutResizingPluginAvailable = layoutResizingPluginKey.get(state) !== undefined;
if (editorExperiment('advanced_layouts', true) && editorExperiment('platform_editor_layout_column_resize_handle', true) && isLayoutResizingPluginAvailable) {
const dividerDecorations = getColumnDividerDecorations(state, editorViewRef);
const selectedDecorations = layoutState.pos !== null ? getNodeDecoration(layoutState.pos, state.doc.nodeAt(layoutState.pos)) : [];
const allDecorations = [...selectedDecorations, ...dividerDecorations];
if (allDecorations.length > 0) {
return DecorationSet.create(state.doc, allDecorations);
}
return undefined;
}
if (layoutState.pos !== null) {
return DecorationSet.create(state.doc, getNodeDecoration(layoutState.pos, state.doc.nodeAt(layoutState.pos)));
}
return undefined;
},
handleKeyDown: keydownHandler({
Tab: filter(isWholeSelectionInsideLayoutColumn, moveCursorToNextColumn),
'Mod-Backspace': handleDeleteLayoutColumn,
'Mod-Delete': handleDeleteLayoutColumn,
Backspace: handleDeleteLayoutColumn,
Delete: handleDeleteLayoutColumn
}),
handleClickOn: createSelectionClickHandler(['layoutColumn'], target => target.hasAttribute('data-layout-section') || target.hasAttribute('data-layout-column'), {
useLongPressSelection: options.useLongPressSelection || false,
getNodeSelectionPos: (state, nodePos) => state.doc.resolve(nodePos).before()
})
},
appendTransaction: (transactions, _oldState, newState) => {
const changes = [];
transactions.forEach(prevTr => {
// remap change segments across the transaction set
changes.forEach(change => {
return {
from: prevTr.mapping.map(change.from),
to: prevTr.mapping.map(change.to),
slice: change.slice
};
});
// don't consider transactions that don't mutate
if (!prevTr.docChanged) {
return;
}
// Skip fixing column sizes for column resize drag transactions
if (editorExperiment('platform_editor_layout_column_resize_handle', true) && prevTr.getMeta('layoutColumnResize')) {
return;
}
const change = fixColumnSizes(prevTr, newState);
if (change) {
changes.push(change);
}
});
if (editorExperiment('advanced_layouts', true) && changes.length === 1) {
var _change$slice$content, _change$slice$content2;
const change = changes[0];
// When editorExperiment('single_column_layouts', true) is on
// delete can create a single column layout
// otherwise we replace the single column layout with its content
if (!editorExperiment('single_column_layouts', true) && change.slice.content.childCount === 1 && ((_change$slice$content = change.slice.content.firstChild) === null || _change$slice$content === void 0 ? void 0 : _change$slice$content.type.name) === 'layoutColumn' && ((_change$slice$content2 = change.slice.content.firstChild) === null || _change$slice$content2 === void 0 ? void 0 : _change$slice$content2.attrs.width) === EVEN_DISTRIBUTED_COL_WIDTHS[1]) {
const tr = newState.tr;
const {
content
} = change.slice.content.firstChild;
tr.replaceWith(change.from - 1, change.to, content);
return tr;
}
}
if (changes.length) {
let tr = newState.tr;
const selection = newState.selection.toJSON();
changes.forEach(change => {
tr.replaceRange(change.from, change.to, change.slice);
});
// selecting and deleting across columns in 3 col layouts can remove
// a layoutColumn so we fix the structure here
tr = fixColumnStructure(newState) || tr;
if (tr.docChanged) {
tr.setSelection(Selection.fromJSON(tr.doc, selection));
return tr;
}
}
return;
}
});
});