@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
85 lines (83 loc) • 4.29 kB
JavaScript
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 { akEditorTableNumberColumnWidth } from '@atlaskit/editor-shared-styles';
import { tableInsertColumnButtonOffset, tableInsertColumnButtonSize, tableToolbarSize } from '../consts';
var HORIZONTAL_ALIGN_COLUMN_BUTTON = -(tableInsertColumnButtonSize / 2);
var HORIZONTAL_ALIGN_NUMBERED_COLUMN_BUTTON = HORIZONTAL_ALIGN_COLUMN_BUTTON + akEditorTableNumberColumnWidth;
var VERTICAL_ALIGN_COLUMN_BUTTON = tableToolbarSize + tableInsertColumnButtonOffset;
var VERTICAL_ALIGN_COLUMN_BUTTON_DRAG = tableInsertColumnButtonOffset;
var HORIZONTAL_ALIGN_ROW_BUTTON = -(tableToolbarSize + tableInsertColumnButtonOffset + tableInsertColumnButtonSize);
var HORIZONTAL_ALIGN_ROW_BUTTON_DRAG = -18;
var VERTICAL_ALIGN_ROW_BUTTON = tableInsertColumnButtonSize / 2;
function getRowOptions(index, isDragAndDropEnabled) {
var defaultOptions = {
alignX: 'left',
alignY: 'bottom',
offset: [0, VERTICAL_ALIGN_ROW_BUTTON]
};
if (index === 0) {
defaultOptions = _objectSpread(_objectSpread({}, defaultOptions), {}, {
alignY: 'top',
// The offset is the inverse the original, because is align top nop bottom.
offset: [0, -VERTICAL_ALIGN_ROW_BUTTON]
});
}
return _objectSpread(_objectSpread({}, defaultOptions), {}, {
onPositionCalculated: function onPositionCalculated(position) {
return _objectSpread(_objectSpread({}, position), {}, {
// Left position should be always the offset (To place in the correct position even if the table has overflow).
left: isDragAndDropEnabled ? HORIZONTAL_ALIGN_ROW_BUTTON_DRAG : HORIZONTAL_ALIGN_ROW_BUTTON
});
}
});
}
function getColumnOptions(index, tableContainer, hasNumberedColumns, isDragAndDropEnabled) {
var options = {
alignX: 'end',
alignY: 'top',
offset: [HORIZONTAL_ALIGN_COLUMN_BUTTON, isDragAndDropEnabled ? VERTICAL_ALIGN_COLUMN_BUTTON_DRAG : VERTICAL_ALIGN_COLUMN_BUTTON],
// :: (position: PopupPosition) -> PopupPosition
// Limit the InsertButton position to the table container
// if the left position starts before it
// we should always set the InsertButton on the start,
// considering the offset from the first column
onPositionCalculated: function onPositionCalculated(position) {
var left = position.left;
if (!left) {
// If not left, lest skip expensive next calculations.
return position;
}
if (index === 0) {
return _objectSpread(_objectSpread({}, position), {}, {
left: hasNumberedColumns ? HORIZONTAL_ALIGN_NUMBERED_COLUMN_BUTTON : HORIZONTAL_ALIGN_COLUMN_BUTTON
});
}
// Check if current position is greater than the available container width
var rect = tableContainer ? tableContainer.getBoundingClientRect() : null;
return _objectSpread(_objectSpread({}, position), {}, {
left: rect && left > rect.width ? rect.width : left
});
}
};
// We need to change the popup position when
// the column index is zero
if (index === 0) {
return _objectSpread(_objectSpread({}, options), {}, {
alignX: 'left',
alignY: 'top'
});
}
return options;
}
function getPopupOptions(direction, index, hasNumberedColumns, isDragAndDropEnabled, tableContainer) {
switch (direction) {
case 'column':
return getColumnOptions(index, tableContainer, hasNumberedColumns, isDragAndDropEnabled);
case 'row':
return getRowOptions(index, isDragAndDropEnabled);
default:
return {};
}
}
export default getPopupOptions;