@atlaskit/editor-plugin-indentation
Version:
Indentation plugin for @atlaskit/editor-core
55 lines (54 loc) • 2.68 kB
JavaScript
import { INPUT_METHOD } from '@atlaskit/editor-common/analytics';
import { backspace, bindKeymapWithCommand, findShortcutByKeymap, indent, outdent } from '@atlaskit/editor-common/keymaps';
import { isTextSelection } from '@atlaskit/editor-common/utils';
import { keymap } from '@atlaskit/editor-prosemirror/keymap';
import { hasParentNodeOfType } from '@atlaskit/editor-prosemirror/utils';
import { getIndentCommand, getOutdentCommand } from '../editor-commands';
export function keymapPlugin(editorAnalyticsAPI) {
var list = {};
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
findShortcutByKeymap(indent), function (state, dispatch) {
var _state$schema$nodes = state.schema.nodes,
blockTaskItem = _state$schema$nodes.blockTaskItem,
paragraph = _state$schema$nodes.paragraph,
doc = _state$schema$nodes.doc;
if (blockTaskItem) {
var hasBlockTaskItem = false;
var hasParagraphAtDocLevel = false;
// Check if the selection contains a blockTaskItem
state.doc.nodesBetween(state.selection.from, state.selection.to, function (node, _pos, parent) {
if (node.type === blockTaskItem) {
hasBlockTaskItem = true;
return false; // stop iterating
}
if (node.type === paragraph && (parent === null || parent === void 0 ? void 0 : parent.type) === doc) {
hasParagraphAtDocLevel = true;
}
});
if (hasBlockTaskItem && !hasParagraphAtDocLevel) {
return false; // let `editor-plugin-tasks-and-decisions` handle indentation
}
}
return getIndentCommand(editorAnalyticsAPI)(INPUT_METHOD.KEYBOARD)(state, dispatch);
}, list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
findShortcutByKeymap(outdent), getOutdentCommand(editorAnalyticsAPI)(INPUT_METHOD.KEYBOARD), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
findShortcutByKeymap(backspace), function (state, dispatch) {
var selection = state.selection;
var blockTaskItem = state.schema.nodes.blockTaskItem;
// Let `editor-plugin-tasks-and-decisions` handle indentation inside blockTaskItems
if (isTextSelection(selection) && selection.$cursor && selection.$cursor.parentOffset === 0 && !hasParentNodeOfType([blockTaskItem])(selection)) {
return dispatch ? getOutdentCommand(editorAnalyticsAPI)(INPUT_METHOD.KEYBOARD)(state, dispatch) : false;
}
return false;
}, list);
return keymap(list);
}
export default keymapPlugin;