UNPKG

@atlaskit/editor-plugin-toolbar-lists-indentation

Version:

Toolbar lists and indentation plugin for @atlaskit/editor-core

53 lines (51 loc) 2.58 kB
import { MAX_INDENTATION_LEVEL } from '@atlaskit/editor-common/indentation'; import { getListItemAttributes } from '@atlaskit/editor-common/lists'; import { hasParentNodeOfType } from '@atlaskit/editor-prosemirror/utils'; export function getIndentationButtonsState(editorState, allowHeadingAndParagraphIndentation, taskDecisionState, indentationState, isInsideListItem) { var _indentationState$isI; const state = { indentDisabled: true, outdentDisabled: true, node: null }; const { selection } = editorState; // Handle bullet and numbered lists seperately as they do // not use the indentation mark. // Check for lists before paragraphs and headings in case // the selection is in a list nested in a layout column. if (isInsideListItem !== null && isInsideListItem !== void 0 && isInsideListItem(editorState.tr)) { const { indentLevel, itemIndex } = getListItemAttributes(selection.$head); return { // List indent levels are zero indexed so we need to subtract 1 indentDisabled: itemIndex === 0 || indentLevel >= MAX_INDENTATION_LEVEL - 1, outdentDisabled: false, node: 'list' }; } // Handle tasks seperately as they do not use the indentation mark // and have different behaviour for outdent compared to lists if (taskDecisionState !== null && taskDecisionState !== void 0 && taskDecisionState.isInsideTask) { return { indentDisabled: taskDecisionState.indentDisabled, outdentDisabled: taskDecisionState.outdentDisabled, node: 'taskList' }; } const isTopLevelParagraphOrHeading = selection.$from.depth === 1; const isInLayoutNode = hasParentNodeOfType(editorState.schema.nodes.layoutColumn)(selection) && // depth of non-nested paragraphs and headings in layouts will always be 3 selection.$from.depth === 3; if (allowHeadingAndParagraphIndentation && ((_indentationState$isI = indentationState === null || indentationState === void 0 ? void 0 : indentationState.isIndentationAllowed) !== null && _indentationState$isI !== void 0 ? _indentationState$isI : false) && (isTopLevelParagraphOrHeading || isInLayoutNode) && (indentationState === null || indentationState === void 0 ? void 0 : indentationState.indentDisabled) !== undefined && (indentationState === null || indentationState === void 0 ? void 0 : indentationState.outdentDisabled) !== undefined) { return { indentDisabled: indentationState.indentDisabled, outdentDisabled: indentationState.outdentDisabled, node: 'paragraph_heading' }; } return state; }