UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

107 lines (105 loc) 3.61 kB
import { akEditorTableNumberColumnWidth } from '@atlaskit/editor-shared-styles'; import { tableInsertColumnButtonOffset, tableInsertColumnButtonSize } from '../consts'; const HORIZONTAL_ALIGN_COLUMN_BUTTON = -(tableInsertColumnButtonSize / 2); const HORIZONTAL_ALIGN_NUMBERED_COLUMN_BUTTON = HORIZONTAL_ALIGN_COLUMN_BUTTON + akEditorTableNumberColumnWidth; const VERTICAL_ALIGN_COLUMN_BUTTON_DRAG = tableInsertColumnButtonOffset; const HORIZONTAL_ALIGN_ROW_BUTTON_DRAG = -18; const VERTICAL_ALIGN_ROW_BUTTON = tableInsertColumnButtonSize / 2; function getRowOptions(index) { let defaultOptions = { alignX: 'left', alignY: 'bottom', offset: [0, VERTICAL_ALIGN_ROW_BUTTON] }; if (index === 0) { defaultOptions = { ...defaultOptions, alignY: 'top', // The offset is the inverse the original, because is align top nop bottom. offset: [0, -VERTICAL_ALIGN_ROW_BUTTON] }; } return { ...defaultOptions, onPositionCalculated(position) { return { ...position, // Left position should be always the offset (To place in the correct position even if the table has overflow). left: HORIZONTAL_ALIGN_ROW_BUTTON_DRAG }; } }; } function getColumnOptions(index, tableContainer, hasNumberedColumns, // Distance between the target cell top and table top, used when anchoring to a lower-row cell. verticalOffsetCorrection = 0) { const options = { alignX: 'end', alignY: 'top', offset: [HORIZONTAL_ALIGN_COLUMN_BUTTON, VERTICAL_ALIGN_COLUMN_BUTTON_DRAG], // :: (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(position) { // Move the popup upward whether the offset parent provides `top` or `bottom`. let verticalCorrection; if (verticalOffsetCorrection) { if (position.top !== undefined) { verticalCorrection = { top: position.top - verticalOffsetCorrection }; } else if (position.bottom !== undefined) { verticalCorrection = { bottom: position.bottom + verticalOffsetCorrection }; } } const { left } = position; if (!left) { return { ...position, ...verticalCorrection }; } if (index === 0) { return { ...position, ...verticalCorrection, left: hasNumberedColumns ? HORIZONTAL_ALIGN_NUMBERED_COLUMN_BUTTON : HORIZONTAL_ALIGN_COLUMN_BUTTON }; } // Check if current position is greater than the available container width const rect = tableContainer ? tableContainer.getBoundingClientRect() : null; return { ...position, ...verticalCorrection, 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 { ...options, alignX: 'left', alignY: 'top' }; } return options; } function getPopupOptions(direction, index, hasNumberedColumns, tableContainer, verticalOffsetCorrection = 0) { switch (direction) { case 'column': return getColumnOptions(index, tableContainer, hasNumberedColumns, verticalOffsetCorrection); case 'row': return getRowOptions(index); default: return {}; } } export default getPopupOptions;