UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

31 lines 1.37 kB
import { ACTION_SUBJECT, EVENT_TYPE, TABLE_ACTION } from '@atlaskit/editor-common/analytics'; import { Fragment, Slice } from '@atlaskit/editor-prosemirror/model'; import { TextSelection } from '@atlaskit/editor-prosemirror/state'; import { findTable, isTableSelected } from '@atlaskit/editor-tables/utils'; import { getSelectedTableInfo } from '../utils/analytics'; export const replaceSelectedTable = (state, content, inputMethod, editorAnalyticsAPI) => { if (isTableSelected(state.selection)) { const table = findTable(state.selection); if (table) { const slice = typeof content === 'string' ? new Slice(Fragment.from(state.schema.text(content)), 0, 0) : content; const tr = state.tr.replace(table.pos, table.pos + table.node.nodeSize, slice); tr.setSelection(TextSelection.create(tr.doc, table.pos + slice.size + 1)); const { totalRowCount, totalColumnCount } = getSelectedTableInfo(state.selection); editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 ? void 0 : editorAnalyticsAPI.attachAnalyticsEvent({ action: TABLE_ACTION.REPLACED, actionSubject: ACTION_SUBJECT.TABLE, attributes: { totalColumnCount, totalRowCount, inputMethod }, eventType: EVENT_TYPE.TRACK })(tr); return tr; } } return state.tr; };