UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

30 lines 1.52 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 var replaceSelectedTable = function replaceSelectedTable(state, content, inputMethod, editorAnalyticsAPI) { if (isTableSelected(state.selection)) { var table = findTable(state.selection); if (table) { var slice = typeof content === 'string' ? new Slice(Fragment.from(state.schema.text(content)), 0, 0) : content; var tr = state.tr.replace(table.pos, table.pos + table.node.nodeSize, slice); tr.setSelection(TextSelection.create(tr.doc, table.pos + slice.size + 1)); var _getSelectedTableInfo = getSelectedTableInfo(state.selection), totalRowCount = _getSelectedTableInfo.totalRowCount, totalColumnCount = _getSelectedTableInfo.totalColumnCount; editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({ action: TABLE_ACTION.REPLACED, actionSubject: ACTION_SUBJECT.TABLE, attributes: { totalColumnCount: totalColumnCount, totalRowCount: totalRowCount, inputMethod: inputMethod }, eventType: EVENT_TYPE.TRACK })(tr); return tr; } } return state.tr; };