import React, { SyntheticEvent } from 'react'; import { InjectedIntlProps, injectIntl } from 'react-intl'; import { tableMarginTop } from '@atlaskit/editor-common'; import { akEditorTableNumberColumnWidth } from '@atlaskit/editor-shared-styles'; import Tooltip from '@atlaskit/tooltip'; import * as keymaps from '../../../../keymaps'; import { closestElement } from '../../../../utils/dom'; import { TableCssClassName as ClassName } from '../../types'; import tableMessages from '../messages'; import { tableToolbarSize } from '../consts'; export interface ButtonProps { type: 'row' | 'column'; tableRef: HTMLElement; onMouseDown: (event: SyntheticEvent) => void; hasStickyHeaders: boolean; } const getInsertLineHeight = ( tableRef: HTMLElement, hasStickyHeaders: boolean, ) => { // The line gets height 100% from the table, // but since we have an overflow on the left, // we should add an offset to make up for it. const LINE_OFFSET = 3; const ADDITIONAL_HEIGHT = hasStickyHeaders ? tableRef.getBoundingClientRect().top - tableMarginTop * 4 - LINE_OFFSET : tableToolbarSize + LINE_OFFSET; return tableRef.offsetHeight + ADDITIONAL_HEIGHT; }; const getToolbarSize = (tableRef: HTMLElement): number => { const parent = closestElement(tableRef, `.${ClassName.TABLE_CONTAINER}`); if (parent) { return parent.querySelector(`.${ClassName.NUMBERED_COLUMN}`) ? tableToolbarSize + akEditorTableNumberColumnWidth - 1 : tableToolbarSize; } return tableToolbarSize; }; const getInsertLineWidth = (tableRef: HTMLElement) => { // The line gets width 100% from the table, // but since we have an overflow on the left, // we should add an offset to make up for it. const LINE_OFFSET = 4; const { parentElement, offsetWidth } = tableRef; const parentOffsetWidth = parentElement!.offsetWidth; const { scrollLeft } = parentElement!; const diff = offsetWidth - parentOffsetWidth; const toolbarSize = getToolbarSize(tableRef); return ( Math.min( offsetWidth + toolbarSize, parentOffsetWidth + toolbarSize - Math.max(scrollLeft - diff, 0), ) + LINE_OFFSET ); }; const tooltipMessageByType = (type: string) => { return type === 'row' ? tableMessages.insertRow : tableMessages.insertColumn; }; const InsertButton = ({ onMouseDown, tableRef, type, intl: { formatMessage }, hasStickyHeaders, }: ButtonProps & InjectedIntlProps) => { const content = ( } position="top" > <>
); const floatingButtonClassName = type === 'column' ? ClassName.CONTROLS_FLOATING_BUTTON_COLUMN : ClassName.CONTROLS_FLOATING_BUTTON_ROW; return (
{content}
); }; export default injectIntl(InsertButton);