UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

126 lines (125 loc) 6.81 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.pluginKey = exports.createPlugin = void 0; var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _steps = require("@atlaskit/adf-schema/steps"); var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _document = require("@atlaskit/editor-common/utils/document"); var _state = require("@atlaskit/editor-prosemirror/state"); var _editorSharedStyles = require("@atlaskit/editor-shared-styles"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); var _consts = require("./table-resizing/utils/consts"); var _alignment = require("./utils/alignment"); var _excluded = ["width"]; 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; } /** * A plugin for handle table custom widths * Has login to scan the document, add width value to table's width attribute when necessary * Also holds resizing state to hide / show table controls */ var pluginKey = exports.pluginKey = new _state.PluginKey('tableWidthPlugin'); var createPlugin = exports.createPlugin = function createPlugin(dispatch, dispatchAnalyticsEvent, fullWidthEnabled, maxWidthEnabled, isTableScalingEnabled, isTableAlignmentEnabled, isCommentEditor) { return new _safePlugin.SafePlugin({ key: pluginKey, state: { init: function init() { return { resizing: false, tableLocalId: '', tableRef: null }; }, apply: function apply(tr, pluginState) { var meta = tr.getMeta(pluginKey); if (meta) { var keys = Object.keys(meta); var changed = keys.some(function (key) { return pluginState[key] !== meta[key]; }); if (changed) { var newState = _objectSpread(_objectSpread({}, pluginState), meta); dispatch(pluginKey, newState); return newState; } } return pluginState; } }, appendTransaction: function appendTransaction(transactions, oldState, newState) { // When document first load in Confluence, initially it is an empty document // and Collab service triggers a transaction to replace the empty document with the real document that should be rendered. // what we need to do is to add width attr to all tables in the real document // isReplaceDocumentOperation is checking if the transaction is the one that replace the empty document with the real document var isReplaceDocumentOperation = (0, _document.isReplaceDocOperation)(transactions, oldState); var referentialityTr = transactions.find(function (tr) { return tr.getMeta('referentialityTableInserted'); }); var shouldPatchTableWidth = fullWidthEnabled && isTableScalingEnabled; var shouldPatchTableAlignment = fullWidthEnabled && isTableAlignmentEnabled; if (!isReplaceDocumentOperation && (!shouldPatchTableWidth && !shouldPatchTableAlignment || !referentialityTr)) { return null; } var table = newState.schema.nodes.table; var tr = newState.tr; if (isReplaceDocumentOperation && !isCommentEditor) { newState.doc.forEach(function (node, offset) { if (node.type === table) { var width = node.attrs.width; var layout = node.attrs.layout; if (!width && layout) { var tableWidthCal; if (maxWidthEnabled) { tableWidthCal = _editorSharedStyles.akEditorMaxWidthLayoutWidth; } else if (fullWidthEnabled) { tableWidthCal = _editorSharedStyles.akEditorFullWidthLayoutWidth; } else { switch (layout) { case 'wide': tableWidthCal = _editorSharedStyles.akEditorWideLayoutWidth; break; case 'full-width': tableWidthCal = _editorSharedStyles.akEditorFullWidthLayoutWidth; break; // when in fix-width appearance, no need to assign value to table width attr // as when table is created, width attr is null by default, table rendered using layout attr default: tableWidthCal = _editorSharedStyles.akEditorDefaultLayoutWidth; break; } } var _node$attrs = node.attrs, _width = _node$attrs.width, rest = (0, _objectWithoutProperties2.default)(_node$attrs, _excluded); if (tableWidthCal) { tr.step(new _steps.SetAttrsStep(offset, _objectSpread({ width: tableWidthCal }, rest))); } } } }); } if (referentialityTr) { referentialityTr.steps.forEach(function (step) { step.getMap().forEach(function (_, __, newStart, newEnd) { newState.doc.nodesBetween(newStart, newEnd, function (node, pos) { if (node.type === table) { if (shouldPatchTableWidth && node.attrs.width !== ((0, _expValEquals.expValEquals)('editor_tinymce_full_width_mode', 'isEnabled', true) || (0, _expValEquals.expValEquals)('confluence_max_width_content_appearance', 'isEnabled', true) ? _consts.TABLE_MAX_WIDTH : _consts.TABLE_FULL_WIDTH)) { tr.setNodeAttribute(pos, 'width', (0, _expValEquals.expValEquals)('editor_tinymce_full_width_mode', 'isEnabled', true) || (0, _expValEquals.expValEquals)('confluence_max_width_content_appearance', 'isEnabled', true) ? _consts.TABLE_MAX_WIDTH : _consts.TABLE_FULL_WIDTH); } if (shouldPatchTableAlignment) { tr.setNodeAttribute(pos, 'layout', _alignment.ALIGN_START); } } }); }); }); } return tr; } }); };