@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
74 lines • 3.95 kB
JavaScript
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 const insertTableFromQuickInsert = ({
api,
inputMethod,
insert,
isTableSelectorEnabled,
options,
state
}) => {
var _api$markdownMode, _api$table, _api$analytics, _tr$selection$$from$n;
if (isTableSelectorEnabled) {
const tr = insert('');
tr.setMeta(sizeSelectorPluginKey, {
isSelectorOpen: true
});
return tr;
}
const markdownState = api === null || api === void 0 ? 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 ? void 0 : (_api$markdownMode2 = api.markdownMode) === null || _api$markdownMode2 === void 0 ? void 0 : _api$markdownMode2.actions.insertSourceTable();
return state.tr;
}
const tableState = api === null || api === void 0 ? void 0 : (_api$table = api.table) === null || _api$table === void 0 ? void 0 : _api$table.sharedState.currentState();
const 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
};
let tableNode = createTableWithWidth(tableNodeProps)(state.schema);
let {
tr
} = state;
if (hasParentNodeOfType(state.schema.nodes.table)(state.selection) && options.tableOptions.allowNestedTables) {
if (getParentOfTypeCount(state.schema.nodes.table)(state.selection.$from) > 1) {
const positionAfterTopTable = getPositionAfterTopParentNodeOfType(state.schema.nodes.table)(state.selection.$from);
tr = safeInsert(tableNode, positionAfterTopTable)(tr);
tr.scrollIntoView();
} else {
tableNode = createTableWithWidth({
...tableNodeProps,
isNestedTable: true
})(state.schema);
tr = insert(tableNode);
}
} else {
tr = insert(tableNode);
}
api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions.attachAnalyticsEvent({
action: ACTION.INSERTED,
actionSubject: ACTION_SUBJECT.DOCUMENT,
actionSubjectId: ACTION_SUBJECT_ID.TABLE,
attributes: {
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;
};