import React, { useCallback, useEffect, useMemo, useState } from 'react' // import ModeEditHtmlIcon from 'material-ui-icons/ModeEdit' import FormatClearIcon from 'material-ui-icons/FormatClear' import SelectAllIcon from 'material-ui-icons/SelectAll' import AlignLeftIcon from 'material-ui-icons/FormatAlignLeft' import AlignRightIcon from 'material-ui-icons/FormatAlignRight' import AlignJustifyIcon from 'material-ui-icons/FormatAlignJustify' import AlignCenterIcon from 'material-ui-icons/FormatAlignCenter' import FormatBoldIcon from 'material-ui-icons/FormatBold' import FormatItalicIcon from 'material-ui-icons/FormatItalic' import FormatUnderlinedIcon from 'material-ui-icons/FormatUnderlined' import FormatStrikethroughIcon from 'material-ui-icons/FormatStrikethrough' import FormatListBulletedIcon from 'material-ui-icons/FormatListBulleted' import FormatListNumberedIcon from 'material-ui-icons/FormatListNumbered' import FormatIndentIncreaseIcon from 'material-ui-icons/FormatIndentIncrease' import FormatIndentDecreaseIcon from 'material-ui-icons/FormatIndentDecrease' import RedoIcon from 'material-ui-icons/Redo' import UndoIcon from 'material-ui-icons/Undo' import { TagEditorToolbarModalStyled } from './styles' import { ContentEditorToolbarButton, ContentEditorToolbarProps, ToolbarButtonProps, } from './interfaces' import Grid from 'material-ui/Grid' import { useCreateLinkButton } from './buttons/CreateLink' // import { useHTMLEditorModeButton } from './buttons/HTMLEditorMode' import { IconButton } from '@procraft/ui/dist/IconButton' // import { getReactFiber } from '../../../../../helpers/ReactFiber' export const ContentEditorToolbar: React.FC = ( props ) => { const { closestInSelection, contentEditableContainer, // object, // updateObject, activeSetter, // contentWrapper, } = props const [selection, setSelection] = useState(null) const [selectionType, setSelectionType] = useState< Selection['type'] | undefined >(undefined) selectionType const [ contentEditableContainerSelected, setContentEditableContainerSelected, ] = useState(false) const onSelectionChange = useCallback( (_event) => { const selection = document.getSelection() const contentEditableContainerSelected = (contentEditableContainer && selection?.containsNode(contentEditableContainer, true)) || false setContentEditableContainerSelected(contentEditableContainerSelected) setSelectionType(selection?.type) }, [contentEditableContainer] ) useEffect(() => { setSelection(global.document?.getSelection()) document.addEventListener('selectionchange', onSelectionChange) return () => { document.removeEventListener('selectionchange', onSelectionChange) } }, [onSelectionChange]) const onButtonClick = useCallback((event: MouseEvent) => { const currentTarget = event.currentTarget as HTMLButtonElement const name = currentTarget.name if (name) { const result = document.execCommand(name) if (!result) { console.error(new Error('Error execCommand')) } } else { console.error(new Error('name is empty')) } }, []) /** * Вставляем колонку */ const insertTableCell = useCallback( (tr: HTMLTableRowElement, index?: number) => { return !!tr.insertCell(index) }, [] ) /** * Вставляем новую строку в таблицу */ const insertTableRow = useCallback( (table: HTMLTableElement, index?: number, columnsCount = 1) => { const tr = table.insertRow(index) if (tr) { let i = 0 while (i < columnsCount) { insertTableCell(tr) i++ } return true } return false }, [insertTableCell] ) const hasSelection = contentEditableContainerSelected ? true : false const createLinkButton = useCreateLinkButton({ hasSelection, selection, }) // const HTMLEditorModeButton = useHTMLEditorModeButton({ // hasSelection, // selection, // object, // updateObject, // contentWrapper, // }) const renderToolbarButtons = useMemo(() => { const tableControls: ContentEditorToolbarButton[] = [ { key: 'insertTable', name: 'insertTable', title: 'Вставить таблицу', disabled: hasSelection ? false : true, onClick: () => document.execCommand( 'insertHTML', true, `
` ), icon: ( ), }, ] if (selection) { const table = closestInSelection('table') if (table) { const tr = closestInSelection('tr') table && tableControls.push({ key: 'insertRow', name: 'insertRow', title: 'Вставить строку', disabled: hasSelection ? false : true, onClick: () => { const index = tr && tr.parentElement ? Array.from(tr.parentElement.childNodes).indexOf(tr) : -1 return insertTableRow(table, index + 1, tr?.childElementCount) }, icon: ( ), }) tr && tableControls.push({ key: 'insertCell', name: 'insertCell', title: 'Вставить колонку', disabled: hasSelection ? false : true, onClick: () => { const cell = closestInSelection('td,th') const index = cell?.parentElement ? Array.from(cell.parentElement.childNodes).indexOf(cell) : -1 return insertTableCell(tr, index + 1) }, icon: ( ), }) } } const buttons: ToolbarButtonProps[] = [ { name: 'bold', title: 'Жирный текст', disabled: hasSelection ? false : true, icon: , }, { name: 'italic', title: 'Наклонный текст', disabled: hasSelection ? false : true, icon: , }, { name: 'underline', title: 'Подчеркнутый текст', disabled: hasSelection ? false : true, icon: , }, { name: 'strikeThrough', title: 'Перечеркнутый текст', disabled: hasSelection ? false : true, icon: , }, { name: 'justifyLeft', title: 'Выровнить влево', disabled: hasSelection ? false : true, icon: , }, { name: 'justifyCenter', title: 'Выровнить по центру', disabled: hasSelection ? false : true, icon: , }, { name: 'justifyRight', title: 'Выровнить вправо', disabled: hasSelection ? false : true, icon: , }, { name: 'justifyFull', title: 'Выровнить на всю ширину', disabled: hasSelection ? false : true, icon: , }, { name: 'insertUnorderedList', title: 'Ненумерованный список', disabled: hasSelection ? false : true, icon: , }, { name: 'insertOrderedList', title: 'Нумерованный список', disabled: hasSelection ? false : true, icon: , }, { name: 'outdent', title: 'Уменьшить отступ', disabled: hasSelection ? false : true, icon: , }, { name: 'indent', title: 'Увеличить отступ', disabled: hasSelection ? false : true, icon: , }, createLinkButton, // HTMLEditorModeButton, { name: 'selectAll', title: 'Выделить все', disabled: hasSelection ? false : true, icon: , }, { name: 'removeFormat', title: 'Удалить форматирование', disabled: hasSelection ? false : true, icon: , }, ] // .concat(tableControls) .concat([ { name: 'undo', title: 'Откатить действие (Ctrl+Z)', disabled: hasSelection ? false : true, icon: , }, { name: 'redo', title: 'Повторить действие (Ctrl+Y)', disabled: hasSelection ? false : true, icon: , }, ]) return buttons.map((n, index) => { const { disabled, name, icon, onClick, className, ...other } = n return ( {icon} ) }) }, [ closestInSelection, createLinkButton, hasSelection, insertTableCell, insertTableRow, onButtonClick, selection, ]) const closeHandler = useCallback(() => { activeSetter(false) }, [activeSetter]) return useMemo(() => { return ( {renderToolbarButtons} ) }, [renderToolbarButtons, closeHandler]) }