UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

92 lines (89 loc) 4.54 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _editorSharedStyles = require("@atlaskit/editor-shared-styles"); var _consts = require("../consts"); 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 HORIZONTAL_ALIGN_COLUMN_BUTTON = -(_consts.tableInsertColumnButtonSize / 2); var HORIZONTAL_ALIGN_NUMBERED_COLUMN_BUTTON = HORIZONTAL_ALIGN_COLUMN_BUTTON + _editorSharedStyles.akEditorTableNumberColumnWidth; var VERTICAL_ALIGN_COLUMN_BUTTON = _consts.tableToolbarSize + _consts.tableInsertColumnButtonOffset; var VERTICAL_ALIGN_COLUMN_BUTTON_DRAG = _consts.tableInsertColumnButtonOffset; var HORIZONTAL_ALIGN_ROW_BUTTON = -(_consts.tableToolbarSize + _consts.tableInsertColumnButtonOffset + _consts.tableInsertColumnButtonSize); var HORIZONTAL_ALIGN_ROW_BUTTON_DRAG = -18; var VERTICAL_ALIGN_ROW_BUTTON = _consts.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 {}; } } var _default = exports.default = getPopupOptions;