UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

72 lines 4.84 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { getParentOfTypeCount, getPositionAfterTopParentNodeOfType } from '@atlaskit/editor-common/nesting'; import { hasParentNodeOfType, safeInsert } from '@atlaskit/editor-prosemirror/utils'; import { fg } from '@atlaskit/platform-feature-flags/fg'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { expValEqualsNoExposure } from '@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure'; import { pluginKey as sizeSelectorPluginKey } from '../../pm-plugins/table-size-selector'; import { createTableWithWidth } from '../../pm-plugins/utils/create'; export var insertTableFromQuickInsert = function insertTableFromQuickInsert(_ref) { var _api$markdownMode, _api$table, _api$analytics, _tr$selection$$from$n; var api = _ref.api, inputMethod = _ref.inputMethod, insert = _ref.insert, isTableSelectorEnabled = _ref.isTableSelectorEnabled, options = _ref.options, state = _ref.state; if (isTableSelectorEnabled) { var _tr = insert(''); _tr.setMeta(sizeSelectorPluginKey, { isSelectorOpen: true }); return _tr; } var markdownState = api === null || api === void 0 || (_api$markdownMode = api.markdownMode) === null || _api$markdownMode === void 0 ? void 0 : _api$markdownMode.sharedState.currentState(); if (markdownState !== null && markdownState !== void 0 && markdownState.isMarkdownMode && (markdownState === null || markdownState === void 0 ? void 0 : markdownState.view) === 'syntax' && expValEqualsNoExposure('cc-markdown-mode', 'isEnabled', true) && fg('platform_editor_markdown_compatible_toolbar')) { var _api$markdownMode2; api === null || api === void 0 || (_api$markdownMode2 = api.markdownMode) === null || _api$markdownMode2 === void 0 || _api$markdownMode2.actions.insertSourceTable(); return state.tr; } var tableState = api === null || api === void 0 || (_api$table = api.table) === null || _api$table === void 0 ? void 0 : _api$table.sharedState.currentState(); var tableNodeProps = { isTableScalingEnabled: options.isTableScalingEnabled, isTableAlignmentEnabled: options.tableOptions.allowTableAlignment, isFullWidthModeEnabled: tableState === null || tableState === void 0 ? void 0 : tableState.isFullWidthModeEnabled, isMaxWidthModeEnabled: tableState === null || tableState === void 0 ? void 0 : tableState.isMaxWidthModeEnabled, isCommentEditor: options.isCommentEditor, isChromelessEditor: options.isChromelessEditor, isTableResizingEnabled: options.tableOptions.allowTableResizing }; var tableNode = createTableWithWidth(tableNodeProps)(state.schema); var tr = state.tr; if (hasParentNodeOfType(state.schema.nodes.table)(state.selection) && options.tableOptions.allowNestedTables) { if (getParentOfTypeCount(state.schema.nodes.table)(state.selection.$from) > 1) { var positionAfterTopTable = getPositionAfterTopParentNodeOfType(state.schema.nodes.table)(state.selection.$from); tr = safeInsert(tableNode, positionAfterTopTable)(tr); tr.scrollIntoView(); } else { tableNode = createTableWithWidth(_objectSpread(_objectSpread({}, tableNodeProps), {}, { isNestedTable: true }))(state.schema); tr = insert(tableNode); } } else { tr = insert(tableNode); } api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 || _api$analytics.actions.attachAnalyticsEvent({ action: ACTION.INSERTED, actionSubject: ACTION_SUBJECT.DOCUMENT, actionSubjectId: ACTION_SUBJECT_ID.TABLE, attributes: _objectSpread({ inputMethod: inputMethod, localId: tableNode.attrs.localId }, expValEquals('platform_editor_nest_table_in_panel', 'isEnabled', true) ? { parentNode: (_tr$selection$$from$n = tr.selection.$from.node(-1)) === null || _tr$selection$$from$n === void 0 ? void 0 : _tr$selection$$from$n.type.name } : {}), eventType: EVENT_TYPE.TRACK })(tr); return tr; };