UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

68 lines (67 loc) 2.77 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 const createPlugin = (dispatch, dispatchAnalyticsEvent) => new SafePlugin({ key: pluginKey, state: createPluginState(dispatch, defaultState), appendTransaction: (transactions, oldState, newState) => { const tr = transactions.find(tr => { var _tr$getMeta, _tr$getMeta$data, _tr$getMeta$data$curr; return (_tr$getMeta = tr.getMeta(pluginKey)) === null || _tr$getMeta === void 0 ? void 0 : (_tr$getMeta$data = _tr$getMeta.data) === null || _tr$getMeta$data === void 0 ? void 0 : (_tr$getMeta$data$curr = _tr$getMeta$data.currentActions) === null || _tr$getMeta$data$curr === void 0 ? void 0 : _tr$getMeta$data$curr.includes('pasted'); }); if (tr) { var _tr$getMeta2, _tr$getMeta2$data; const 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 ? void 0 : (_tr$getMeta2$data = _tr$getMeta2.data) === null || _tr$getMeta2$data === void 0 ? void 0 : _tr$getMeta2$data.type } }); return resetRowOrColumnMovedTransform()(newTr); } return undefined; }, props: { handlePaste: ({ state, dispatch }, event, slice) => { const { schema } = state; const 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; } const count = countCellsInSlice(slice, schema, type); updateRowOrColumnMoved({ numberOfCells: count, type }, 'pasted')(state, dispatch); }, transformCopied: (slice, { state, dispatch }) => { const { schema } = state; const type = getTableSelectionType(state.selection); const count = countCellsInSlice(slice, schema, type); updateRowOrColumnMoved({ numberOfCells: count, type }, 'copyOrCut')(state, dispatch); return slice; } } });