UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

181 lines (177 loc) 11 kB
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, isTableScalingEnabled = false, isTableAlignmentEnabled = false, isFullWidthEnabled, pluginInjectionApi, getIntl, isTableFixedColumnWidthsOptionEnabled = false, shouldUseIncreasedScalingPercent, isCommentEditor, isChromelessEditor, isTableResizingEnabled) { var _pluginInjectionApi$a; const list = {}; const ariaNotifyPlugin = pluginInjectionApi === null || pluginInjectionApi === void 0 ? 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, isTableAlignmentEnabled, isFullWidthModeEnabled: !!isFullWidthEnabled, isCommentEditor: isCommentEditor, isChromelessEditor: isChromelessEditor, 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) { const 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 }), 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 }), 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 }), 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;