UNPKG

@atlaskit/editor-plugin-list

Version:

List plugin for @atlaskit/editor-core

198 lines (196 loc) 10.1 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.pluginKey = exports.getDecorations = exports.createPlugin = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _selection = require("@atlaskit/editor-common/selection"); var _styles = require("@atlaskit/editor-common/styles"); var _utils = require("@atlaskit/editor-common/utils"); var _state2 = require("@atlaskit/editor-prosemirror/state"); var _utils2 = require("@atlaskit/editor-prosemirror/utils"); var _view = require("@atlaskit/editor-prosemirror/view"); var _expValEqualsNoExposure = require("@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure"); var _transforms = require("./transforms"); var _selection2 = require("./utils/selection"); function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } var listPluginKey = new _state2.PluginKey('listPlugin'); var pluginKey = exports.pluginKey = listPluginKey; var initialState = { bulletListActive: false, bulletListDisabled: false, orderedListActive: false, orderedListDisabled: false, decorationSet: _view.DecorationSet.empty }; var getDecorations = exports.getDecorations = function getDecorations(doc, _state, _featureFlags) { var decorations = []; // this stack keeps track of each (nested) list to calculate the indentation level var processedListsStack = []; doc.nodesBetween(0, doc.content.size, function (node, currentNodeStartPos) { if (processedListsStack.length > 0) { var isOutsideLastList = true; while (isOutsideLastList && processedListsStack.length > 0) { var lastList = processedListsStack[processedListsStack.length - 1]; var 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 ((0, _utils.isListNode)(node)) { processedListsStack.push({ node: node, startPos: currentNodeStartPos }); var from = currentNodeStartPos; var to = currentNodeStartPos + node.nodeSize; var depth = processedListsStack.length; decorations.push(_view.Decoration.node(from, to, { 'data-indent-level': "".concat(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 var digitsSize = (0, _utils.getItemCounterDigitsSize)({ itemsCount: node === null || node === void 0 ? void 0 : node.childCount, order: node === null || node === void 0 || (_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.order }); if (digitsSize && digitsSize > 1) { decorations.push(_view.Decoration.node(from, to, { style: (0, _styles.getOrderedListInlineStyles)(digitsSize, 'string') })); } } } }); return _view.DecorationSet.empty.add(doc, decorations); }; var getListState = function getListState(doc, selection) { var _doc$type$schema$node = doc.type.schema.nodes, bulletList = _doc$type$schema$node.bulletList, orderedList = _doc$type$schema$node.orderedList, taskList = _doc$type$schema$node.taskList; var listParent = (0, _utils2.findParentNodeOfType)([bulletList, orderedList, taskList])(selection); var bulletListActive = !!listParent && listParent.node.type === bulletList; var orderedListActive = !!listParent && listParent.node.type === orderedList; var bulletListDisabled = !(bulletListActive || orderedListActive || (0, _selection2.isWrappingPossible)(bulletList, selection)); var orderedListDisabled = !(bulletListActive || orderedListActive || (0, _selection2.isWrappingPossible)(orderedList, selection)); return { bulletListActive: bulletListActive, bulletListDisabled: bulletListDisabled, orderedListActive: orderedListActive, orderedListDisabled: orderedListDisabled }; }; var handleDocChanged = function handleDocChanged(featureFlags) { return function (tr, pluginState, editorState) { var nextPluginState = handleSelectionChanged(tr, pluginState); var decorationSet = getDecorations(tr.doc, editorState, featureFlags); return _objectSpread(_objectSpread({}, nextPluginState), {}, { decorationSet: decorationSet }); }; }; var handleSelectionChanged = function handleSelectionChanged(tr, pluginState) { var _getListState = getListState(tr.doc, tr.selection), bulletListActive = _getListState.bulletListActive, orderedListActive = _getListState.orderedListActive, bulletListDisabled = _getListState.bulletListDisabled, orderedListDisabled = _getListState.orderedListDisabled; if (bulletListActive !== pluginState.bulletListActive || orderedListActive !== pluginState.orderedListActive || bulletListDisabled !== pluginState.bulletListDisabled || orderedListDisabled !== pluginState.orderedListDisabled) { var nextPluginState = _objectSpread(_objectSpread({}, pluginState), {}, { bulletListActive: bulletListActive, orderedListActive: orderedListActive, bulletListDisabled: bulletListDisabled, orderedListDisabled: orderedListDisabled }); return nextPluginState; } return pluginState; }; var reducer = function reducer() { return function (state) { return state; }; }; var createInitialState = function createInitialState(featureFlags, api) { return function (state) { var isToolbarAIFCEnabled = Boolean(api === null || api === void 0 ? void 0 : api.toolbar); return _objectSpread(_objectSpread({}, isToolbarAIFCEnabled ? getListState(state.doc, state.selection) : initialState), {}, { decorationSet: getDecorations(state.doc, state, featureFlags) }); }; }; var createPlugin = exports.createPlugin = function createPlugin(eventDispatch, featureFlags, api) { var _pluginFactory = (0, _utils.pluginFactory)(listPluginKey, reducer(), { onDocChanged: handleDocChanged(featureFlags), onSelectionChanged: handleSelectionChanged }), getPluginState = _pluginFactory.getPluginState, createPluginState = _pluginFactory.createPluginState; return new _safePlugin.SafePlugin({ state: createPluginState(eventDispatch, createInitialState(featureFlags, api)), key: listPluginKey, appendTransaction: function appendTransaction(transactions, _oldState, newState) { if (!(0, _expValEqualsNoExposure.expValEqualsNoExposure)('platform_editor_flexible_list_schema', 'isEnabled', true)) { return null; } if (!transactions.some(function (t) { return t.docChanged; })) { return null; } // Efficiently scans only affected list nodes — exits early if none are found. var tr = (0, _transforms.applyListNormalisationFixes)({ tr: newState.tr, transactions: transactions, doc: newState.doc, schema: newState.schema }); if (tr.docChanged) { return tr; } return null; }, props: { decorations: function decorations(state) { var _getPluginState = getPluginState(state), decorationSet = _getPluginState.decorationSet; return decorationSet; }, handleClick: function handleClick(view, pos, event) { var state = view.state; // 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; var nodeAtPos = state.tr.doc.nodeAt(pos); var _view$state$schema$no = view.state.schema.nodes, listItem = _view$state$schema$no.listItem, codeBlock = _view$state$schema$no.codeBlock; if ((nodeAtPos === null || nodeAtPos === void 0 ? void 0 : nodeAtPos.type) === listItem && (nodeAtPos === null || nodeAtPos === void 0 || (_nodeAtPos$firstChild = nodeAtPos.firstChild) === null || _nodeAtPos$firstChild === void 0 ? void 0 : _nodeAtPos$firstChild.type) === codeBlock) { var _document; var bufferPx = 50; var isCodeBlockNextToListMarker = Boolean( // eslint-disable-next-line @atlaskit/platform/no-direct-document-usage (_document = document) === null || _document === void 0 || (_document = _document.elementFromPoint(event.clientX + (_styles.listItemCounterPadding + bufferPx), event.clientY)) === null || _document === void 0 ? void 0 : _document.closest(".".concat(_styles.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 (0, _selection.setGapCursorSelection)(view, pos + 1, _selection.Side.LEFT); return true; } } } return false; } } }); };