@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
122 lines (121 loc) • 6.39 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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) { _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; }
/**
* 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
*/
import { SetAttrsStep } from '@atlaskit/adf-schema/steps';
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { isReplaceDocOperation } from '@atlaskit/editor-common/utils/document';
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
import { akEditorDefaultLayoutWidth, akEditorFullWidthLayoutWidth, akEditorMaxWidthLayoutWidth, akEditorWideLayoutWidth } from '@atlaskit/editor-shared-styles';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { TABLE_MAX_WIDTH, TABLE_FULL_WIDTH } from './table-resizing/utils/consts';
import { ALIGN_START } from './utils/alignment';
export var pluginKey = new PluginKey('tableWidthPlugin');
var createPlugin = function createPlugin(dispatch, dispatchAnalyticsEvent, fullWidthEnabled, maxWidthEnabled, isTableScalingEnabled, isTableAlignmentEnabled, isCommentEditor) {
return new 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 = 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 = akEditorMaxWidthLayoutWidth;
} else if (fullWidthEnabled) {
tableWidthCal = akEditorFullWidthLayoutWidth;
} else {
switch (layout) {
case 'wide':
tableWidthCal = akEditorWideLayoutWidth;
break;
case 'full-width':
tableWidthCal = 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 = akEditorDefaultLayoutWidth;
break;
}
}
var _node$attrs = node.attrs,
_width = _node$attrs.width,
rest = _objectWithoutProperties(_node$attrs, _excluded);
if (tableWidthCal) {
tr.step(new 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 !== (expValEquals('editor_tinymce_full_width_mode', 'isEnabled', true) || expValEquals('confluence_max_width_content_appearance', 'isEnabled', true) ? TABLE_MAX_WIDTH : TABLE_FULL_WIDTH)) {
tr.setNodeAttribute(pos, 'width', expValEquals('editor_tinymce_full_width_mode', 'isEnabled', true) || expValEquals('confluence_max_width_content_appearance', 'isEnabled', true) ? TABLE_MAX_WIDTH : TABLE_FULL_WIDTH);
}
if (shouldPatchTableAlignment) {
tr.setNodeAttribute(pos, 'layout', ALIGN_START);
}
}
});
});
});
}
return tr;
}
});
};
export { createPlugin };