UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

64 lines (63 loc) 2.87 kB
import { ACTION_SUBJECT, EVENT_TYPE, TABLE_ACTION } from '@atlaskit/editor-common/analytics'; import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { countCellsInSlice, getTableElementMoveTypeBySlice, getTableSelectionType, isInsideFirstCellOfRowOrColumn } from '../commands/misc'; import { resetRowOrColumnMovedTransform, updateRowOrColumnMoved } from './commands'; import { createPluginState } from './plugin-factory'; import { pluginKey } from './plugin-key'; import { defaultState } from './types'; export var createPlugin = function createPlugin(dispatch, dispatchAnalyticsEvent) { return new SafePlugin({ key: pluginKey, state: createPluginState(dispatch, defaultState), appendTransaction: function appendTransaction(transactions, oldState, newState) { var tr = transactions.find(function (tr) { var _tr$getMeta; return (_tr$getMeta = tr.getMeta(pluginKey)) === null || _tr$getMeta === void 0 || (_tr$getMeta = _tr$getMeta.data) === null || _tr$getMeta === void 0 || (_tr$getMeta = _tr$getMeta.currentActions) === null || _tr$getMeta === void 0 ? void 0 : _tr$getMeta.includes('pasted'); }); if (tr) { var _tr$getMeta2; var newTr = newState.tr; dispatchAnalyticsEvent({ action: TABLE_ACTION.ROW_OR_COLUMN_MOVED, actionSubject: ACTION_SUBJECT.TABLE, actionSubjectId: null, eventType: EVENT_TYPE.TRACK, attributes: { type: (_tr$getMeta2 = tr.getMeta(pluginKey)) === null || _tr$getMeta2 === void 0 || (_tr$getMeta2 = _tr$getMeta2.data) === null || _tr$getMeta2 === void 0 ? void 0 : _tr$getMeta2.type } }); return resetRowOrColumnMovedTransform()(newTr); } return undefined; }, props: { handlePaste: function handlePaste(_ref, event, slice) { var state = _ref.state, dispatch = _ref.dispatch; var schema = state.schema; var type = getTableElementMoveTypeBySlice(slice, state); // if the selection wasn't in the first cell of a row or column, don't count it if (!type || !isInsideFirstCellOfRowOrColumn(state.selection, type)) { return; } var count = countCellsInSlice(slice, schema, type); updateRowOrColumnMoved({ numberOfCells: count, type: type }, 'pasted')(state, dispatch); }, transformCopied: function transformCopied(slice, _ref2) { var state = _ref2.state, dispatch = _ref2.dispatch; var schema = state.schema; var type = getTableSelectionType(state.selection); var count = countCellsInSlice(slice, schema, type); updateRowOrColumnMoved({ numberOfCells: count, type: type }, 'copyOrCut')(state, dispatch); return slice; } } }); };