@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
79 lines (78 loc) • 5.16 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.insertTableFromQuickInsert = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _analytics = require("@atlaskit/editor-common/analytics");
var _nesting = require("@atlaskit/editor-common/nesting");
var _utils = require("@atlaskit/editor-prosemirror/utils");
var _fg = require("@atlaskit/platform-feature-flags/fg");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _expValEqualsNoExposure = require("@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure");
var _tableSizeSelector = require("../../pm-plugins/table-size-selector");
var _create = require("../../pm-plugins/utils/create");
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) { (0, _defineProperty2.default)(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; }
var insertTableFromQuickInsert = exports.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(_tableSizeSelector.pluginKey, {
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' && (0, _expValEqualsNoExposure.expValEqualsNoExposure)('cc-markdown-mode', 'isEnabled', true) && (0, _fg.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 = (0, _create.createTableWithWidth)(tableNodeProps)(state.schema);
var tr = state.tr;
if ((0, _utils.hasParentNodeOfType)(state.schema.nodes.table)(state.selection) && options.tableOptions.allowNestedTables) {
if ((0, _nesting.getParentOfTypeCount)(state.schema.nodes.table)(state.selection.$from) > 1) {
var positionAfterTopTable = (0, _nesting.getPositionAfterTopParentNodeOfType)(state.schema.nodes.table)(state.selection.$from);
tr = (0, _utils.safeInsert)(tableNode, positionAfterTopTable)(tr);
tr.scrollIntoView();
} else {
tableNode = (0, _create.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: _analytics.ACTION.INSERTED,
actionSubject: _analytics.ACTION_SUBJECT.DOCUMENT,
actionSubjectId: _analytics.ACTION_SUBJECT_ID.TABLE,
attributes: _objectSpread({
inputMethod: inputMethod,
localId: tableNode.attrs.localId
}, (0, _expValEquals.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: _analytics.EVENT_TYPE.TRACK
})(tr);
return tr;
};