@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
191 lines (187 loc) • 11.8 kB
JavaScript
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
import { addColumnAfter, addColumnAfterVO, addColumnBefore, addColumnBeforeVO, addRowAfter, addRowAfterVO, addRowBefore, addRowBeforeVO, backspace, bindKeymapWithCommand, decreaseMediaSize, deleteColumn, deleteRow, escape, focusToContextMenuTrigger, increaseMediaSize, moveColumnLeftOld, moveColumnLeft, moveColumnRightOld, moveColumnRight, moveDown, moveLeft, moveRight, moveRowDownOld, moveRowDown, moveRowUpOld, moveRowUp, moveUp, nextCell, previousCell, startColumnResizing, toggleTable } from '@atlaskit/editor-common/keymaps';
import { editorCommandToPMCommand } from '@atlaskit/editor-common/preset';
import { chainCommands } from '@atlaskit/editor-prosemirror/commands';
import { keymap } from '@atlaskit/editor-prosemirror/keymap';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { moveSourceWithAnalyticsViaShortcut } from '../pm-plugins/drag-and-drop/commands-with-analytics';
import { goToNextCell, moveCursorBackward, setFocusToCellMenu } from './commands';
import { activateNextResizeArea, initiateKeyboardColumnResizing, stopKeyboardColumnResizing } from './commands/column-resize';
import { addRowAroundSelection, changeColumnWidthByStepWithAnalytics, deleteSelectedRowsOrColumnsWithAnalyticsViaShortcut, deleteTableIfSelectedWithAnalytics, emptyMultipleCellsWithAnalytics } from './commands/commands-with-analytics';
import { goToNextCellVertical } from './commands/go-to-next-cell';
import { addColumnAfter as addColumnAfterCommand, addColumnBefore as addColumnBeforeCommand, insertTableWithNestingSupport } from './commands/insert';
export function keymapPlugin(getEditorContainerWidth, api, nodeViewPortalProviderAPI, editorAnalyticsAPI, dragAndDropEnabled) {
var _pluginInjectionApi$a;
var isTableScalingEnabled = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
var isTableAlignmentEnabled = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : false;
var isFullWidthEnabled = arguments.length > 7 ? arguments[7] : undefined;
var pluginInjectionApi = arguments.length > 8 ? arguments[8] : undefined;
var getIntl = arguments.length > 9 ? arguments[9] : undefined;
var isTableFixedColumnWidthsOptionEnabled = arguments.length > 10 && arguments[10] !== undefined ? arguments[10] : false;
var shouldUseIncreasedScalingPercent = arguments.length > 11 ? arguments[11] : undefined;
var isCommentEditor = arguments.length > 12 ? arguments[12] : undefined;
var isChromelessEditor = arguments.length > 13 ? arguments[13] : undefined;
var isTableResizingEnabled = arguments.length > 14 ? arguments[14] : undefined;
var list = {};
var ariaNotifyPlugin = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$a = pluginInjectionApi.accessibilityUtils) === null || _pluginInjectionApi$a === void 0 ? void 0 : _pluginInjectionApi$a.actions.ariaNotify;
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
nextCell.common, goToNextCell(editorAnalyticsAPI, ariaNotifyPlugin, getIntl)(1), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
previousCell.common, goToNextCell(editorAnalyticsAPI, ariaNotifyPlugin, getIntl)(-1), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
toggleTable.common, editorCommandToPMCommand(insertTableWithNestingSupport({
isTableScalingEnabled: isTableScalingEnabled,
isTableAlignmentEnabled: isTableAlignmentEnabled,
isFullWidthModeEnabled: !!isFullWidthEnabled,
isCommentEditor: isCommentEditor,
isChromelessEditor: isChromelessEditor,
isTableResizingEnabled: isTableResizingEnabled
}, api, {
action: ACTION.INSERTED,
actionSubject: ACTION_SUBJECT.DOCUMENT,
actionSubjectId: ACTION_SUBJECT_ID.TABLE,
attributes: {
inputMethod: INPUT_METHOD.SHORTCUT
},
eventType: EVENT_TYPE.TRACK
})), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
backspace.common, chainCommands(deleteTableIfSelectedWithAnalytics(editorAnalyticsAPI)(INPUT_METHOD.KEYBOARD), emptyMultipleCellsWithAnalytics(editorAnalyticsAPI)(INPUT_METHOD.KEYBOARD)), list);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
bindKeymapWithCommand(backspace.common, moveCursorBackward, list);
// Add row/column shortcuts
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
addRowBefore.common, addRowAroundSelection(editorAnalyticsAPI)('TOP'), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
addRowAfter.common, addRowAroundSelection(editorAnalyticsAPI)('BOTTOM'), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
addColumnBefore.common, addColumnBeforeCommand(api, isTableScalingEnabled, isTableFixedColumnWidthsOptionEnabled, shouldUseIncreasedScalingPercent), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
addColumnAfter.common, addColumnAfterCommand(api, isTableScalingEnabled, isTableFixedColumnWidthsOptionEnabled, shouldUseIncreasedScalingPercent), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
addRowBeforeVO.common, addRowAroundSelection(editorAnalyticsAPI)('TOP'), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
addRowAfterVO.common, addRowAroundSelection(editorAnalyticsAPI)('BOTTOM'), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
addColumnBeforeVO.common, addColumnBeforeCommand(api, isTableScalingEnabled, isTableFixedColumnWidthsOptionEnabled, shouldUseIncreasedScalingPercent), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
addColumnAfterVO.common, addColumnAfterCommand(api, isTableScalingEnabled, isTableFixedColumnWidthsOptionEnabled, shouldUseIncreasedScalingPercent), list);
if (dragAndDropEnabled && moveRowDown.common && moveRowUp.common && moveColumnLeft.common && moveColumnRight.common) {
var isNewKeyMapExperiment = expValEquals('editor-a11y-fy26-keyboard-move-row-column', 'isEnabled', true);
// Move row/column shortcuts
/**
* NOTE: If the keyboard shortcut for moving rows or columns is changed, we need to update the handleKeyDown function
* in packages/editor/editor-plugin-table/src/pm-plugins/drag-and-drop/plugin.ts
* to make sure the logic for holding the shortcut keys is valid
* See ticket ED-22154 https://product-fabric.atlassian.net/browse/ED-22154
*/
bindKeymapWithCommand(
// Ignored via go/ees005
isNewKeyMapExperiment ? moveRowDown.common :
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
moveRowDownOld.common, moveSourceWithAnalyticsViaShortcut(editorAnalyticsAPI, ariaNotifyPlugin, getIntl)('table-row', 1), list);
bindKeymapWithCommand(
// Ignored via go/ees005
isNewKeyMapExperiment ? moveRowUp.common :
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
moveRowUpOld.common, moveSourceWithAnalyticsViaShortcut(editorAnalyticsAPI, ariaNotifyPlugin, getIntl)('table-row', -1), list);
bindKeymapWithCommand(
// Ignored via go/ees005
isNewKeyMapExperiment ? moveColumnLeft.common :
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
moveColumnLeftOld.common, moveSourceWithAnalyticsViaShortcut(editorAnalyticsAPI, ariaNotifyPlugin, getIntl)('table-column', -1), list);
bindKeymapWithCommand(
// Ignored via go/ees005
isNewKeyMapExperiment ? moveColumnRight.common :
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
moveColumnRightOld.common, moveSourceWithAnalyticsViaShortcut(editorAnalyticsAPI, ariaNotifyPlugin, getIntl)('table-column', 1), list);
// Delete row/column shortcuts
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
deleteColumn.common, deleteSelectedRowsOrColumnsWithAnalyticsViaShortcut(editorAnalyticsAPI, api, isTableScalingEnabled, isTableFixedColumnWidthsOptionEnabled, shouldUseIncreasedScalingPercent), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
deleteRow.common, deleteSelectedRowsOrColumnsWithAnalyticsViaShortcut(editorAnalyticsAPI, api, isTableScalingEnabled, isTableFixedColumnWidthsOptionEnabled, shouldUseIncreasedScalingPercent), list);
}
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
startColumnResizing.common, initiateKeyboardColumnResizing({
ariaNotify: ariaNotifyPlugin,
getIntl: getIntl,
nodeViewPortalProviderAPI: nodeViewPortalProviderAPI
}), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
moveRight.common, activateNextResizeArea({
direction: 1,
ariaNotify: ariaNotifyPlugin,
getIntl: getIntl,
nodeViewPortalProviderAPI: nodeViewPortalProviderAPI
}), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
moveLeft.common, activateNextResizeArea({
direction: -1,
ariaNotify: ariaNotifyPlugin,
getIntl: getIntl,
nodeViewPortalProviderAPI: nodeViewPortalProviderAPI
}), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
moveDown.common, goToNextCellVertical(1), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
moveUp.common, goToNextCellVertical(-1), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
decreaseMediaSize.common, changeColumnWidthByStepWithAnalytics(editorAnalyticsAPI, api)(-10, getEditorContainerWidth, isTableScalingEnabled, isTableFixedColumnWidthsOptionEnabled, !!isCommentEditor, INPUT_METHOD.SHORTCUT, ariaNotifyPlugin, getIntl), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
increaseMediaSize.common, changeColumnWidthByStepWithAnalytics(editorAnalyticsAPI, api)(10, getEditorContainerWidth, isTableScalingEnabled, isTableFixedColumnWidthsOptionEnabled, !!isCommentEditor, INPUT_METHOD.SHORTCUT, ariaNotifyPlugin, getIntl), list);
bindKeymapWithCommand(
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
escape.common, stopKeyboardColumnResizing({
ariaNotify: ariaNotifyPlugin,
getIntl: getIntl
}), list);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
bindKeymapWithCommand(focusToContextMenuTrigger.common, setFocusToCellMenu(), list);
return keymap(list);
}
export default keymapPlugin;