@atlaskit/editor-plugin-list
Version:
List plugin for @atlaskit/editor-core
192 lines (191 loc) • 8.24 kB
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { setGapCursorSelection, Side } from '@atlaskit/editor-common/selection';
import { CodeBlockSharedCssClassName, getOrderedListInlineStyles, listItemCounterPadding } from '@atlaskit/editor-common/styles';
import { getItemCounterDigitsSize, isListNode, pluginFactory } from '@atlaskit/editor-common/utils';
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
import { findParentNodeOfType } from '@atlaskit/editor-prosemirror/utils';
import { Decoration, DecorationSet } from '@atlaskit/editor-prosemirror/view';
import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure';
import { applyListNormalisationFixes } from './transforms';
import { isWrappingPossible } from './utils/selection';
const listPluginKey = new PluginKey('listPlugin');
export const pluginKey = listPluginKey;
const initialState = {
bulletListActive: false,
bulletListDisabled: false,
orderedListActive: false,
orderedListDisabled: false,
decorationSet: DecorationSet.empty
};
export const getDecorations = (doc, _state, _featureFlags) => {
const decorations = [];
// this stack keeps track of each (nested) list to calculate the indentation level
const processedListsStack = [];
doc.nodesBetween(0, doc.content.size, (node, currentNodeStartPos) => {
if (processedListsStack.length > 0) {
let isOutsideLastList = true;
while (isOutsideLastList && processedListsStack.length > 0) {
const lastList = processedListsStack[processedListsStack.length - 1];
const lastListEndPos = lastList.startPos + lastList.node.nodeSize;
isOutsideLastList = currentNodeStartPos >= lastListEndPos;
// once we finish iterating over each innermost list, pop the stack to
// decrease the indent level attribute accordingly
if (isOutsideLastList) {
processedListsStack.pop();
}
}
}
if (isListNode(node)) {
processedListsStack.push({
node,
startPos: currentNodeStartPos
});
const from = currentNodeStartPos;
const to = currentNodeStartPos + node.nodeSize;
const depth = processedListsStack.length;
decorations.push(Decoration.node(from, to, {
'data-indent-level': `${depth}`
}));
if (node.type.name === 'orderedList') {
var _node$attrs;
// If a numbered list has item counters numbering >= 100, we'll need to add special
// spacing to account for the extra digit chars
const digitsSize = getItemCounterDigitsSize({
itemsCount: node === null || node === void 0 ? void 0 : node.childCount,
order: node === null || node === void 0 ? void 0 : (_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.order
});
if (digitsSize && digitsSize > 1) {
decorations.push(Decoration.node(from, to, {
style: getOrderedListInlineStyles(digitsSize, 'string')
}));
}
}
}
});
return DecorationSet.empty.add(doc, decorations);
};
const getListState = (doc, selection) => {
const {
bulletList,
orderedList,
taskList
} = doc.type.schema.nodes;
const listParent = findParentNodeOfType([bulletList, orderedList, taskList])(selection);
const bulletListActive = !!listParent && listParent.node.type === bulletList;
const orderedListActive = !!listParent && listParent.node.type === orderedList;
const bulletListDisabled = !(bulletListActive || orderedListActive || isWrappingPossible(bulletList, selection));
const orderedListDisabled = !(bulletListActive || orderedListActive || isWrappingPossible(orderedList, selection));
return {
bulletListActive,
bulletListDisabled,
orderedListActive,
orderedListDisabled
};
};
const handleDocChanged = featureFlags => (tr, pluginState, editorState) => {
const nextPluginState = handleSelectionChanged(tr, pluginState);
const decorationSet = getDecorations(tr.doc, editorState, featureFlags);
return {
...nextPluginState,
decorationSet
};
};
const handleSelectionChanged = (tr, pluginState) => {
const {
bulletListActive,
orderedListActive,
bulletListDisabled,
orderedListDisabled
} = getListState(tr.doc, tr.selection);
if (bulletListActive !== pluginState.bulletListActive || orderedListActive !== pluginState.orderedListActive || bulletListDisabled !== pluginState.bulletListDisabled || orderedListDisabled !== pluginState.orderedListDisabled) {
const nextPluginState = {
...pluginState,
bulletListActive,
orderedListActive,
bulletListDisabled,
orderedListDisabled
};
return nextPluginState;
}
return pluginState;
};
const reducer = () => state => {
return state;
};
const createInitialState = (featureFlags, api) => state => {
const isToolbarAIFCEnabled = Boolean(api === null || api === void 0 ? void 0 : api.toolbar);
return {
// When plugin is initialised, editor state is defined with selection
// hence returning the list state based on the selection to avoid list button in primary toolbar flickering during initial load
...(isToolbarAIFCEnabled ? getListState(state.doc, state.selection) : initialState),
decorationSet: getDecorations(state.doc, state, featureFlags)
};
};
export const createPlugin = (eventDispatch, featureFlags, api) => {
const {
getPluginState,
createPluginState
} = pluginFactory(listPluginKey, reducer(), {
onDocChanged: handleDocChanged(featureFlags),
onSelectionChanged: handleSelectionChanged
});
return new SafePlugin({
state: createPluginState(eventDispatch, createInitialState(featureFlags, api)),
key: listPluginKey,
appendTransaction(transactions, _oldState, newState) {
if (!expValEqualsNoExposure('platform_editor_flexible_list_schema', 'isEnabled', true)) {
return null;
}
if (!transactions.some(t => t.docChanged)) {
return null;
}
// Efficiently scans only affected list nodes — exits early if none are found.
const tr = applyListNormalisationFixes({
tr: newState.tr,
transactions,
doc: newState.doc,
schema: newState.schema
});
if (tr.docChanged) {
return tr;
}
return null;
},
props: {
decorations(state) {
const {
decorationSet
} = getPluginState(state);
return decorationSet;
},
handleClick: (view, pos, event) => {
const {
state
} = view;
// Ignored via go/ees005
// eslint-disable-next-line @atlaskit/editor/no-as-casting
if (['LI', 'UL'].includes((event === null || event === void 0 ? void 0 : event.target).tagName)) {
var _nodeAtPos$firstChild;
const nodeAtPos = state.tr.doc.nodeAt(pos);
const {
listItem,
codeBlock
} = view.state.schema.nodes;
if ((nodeAtPos === null || nodeAtPos === void 0 ? void 0 : nodeAtPos.type) === listItem && (nodeAtPos === null || nodeAtPos === void 0 ? void 0 : (_nodeAtPos$firstChild = nodeAtPos.firstChild) === null || _nodeAtPos$firstChild === void 0 ? void 0 : _nodeAtPos$firstChild.type) === codeBlock) {
var _document, _document$elementFrom;
const bufferPx = 50;
const isCodeBlockNextToListMarker = Boolean( // eslint-disable-next-line @atlaskit/platform/no-direct-document-usage
(_document = document) === null || _document === void 0 ? void 0 : (_document$elementFrom = _document.elementFromPoint(event.clientX + (listItemCounterPadding + bufferPx), event.clientY)) === null || _document$elementFrom === void 0 ? void 0 : _document$elementFrom.closest(`.${CodeBlockSharedCssClassName.CODEBLOCK_CONTAINER}`));
if (isCodeBlockNextToListMarker) {
// +1 needed to put cursor inside li
// otherwise gap cursor markup will be injected as immediate child of ul resulting in invalid html
setGapCursorSelection(view, pos + 1, Side.LEFT);
return true;
}
}
}
return false;
}
}
});
};