import { css, cx } from '@emotion/css'; import { Editor, Node, Range, Transforms } from '@src/components/slate-packages/slate'; import { f } from '@src/resource/string'; import { getCache } from '@src/utils/cacheUtils'; import { SEL_CELLS } from '@src/utils/weak-maps'; import { Dropdown, Tooltip } from 'antd'; import $ from 'jquery'; import _ from 'lodash'; import React, { useCallback, useEffect, useState } from 'react'; import { TripdocsSdkContext } from '../../../../Docs'; import { ReactEditor } from '../../../slate-packages/slate-react'; import { IconBtn, IconButton } from '../../plugins/Components'; import { ELTYPE } from '../../plugins/config'; import { insertEditLink } from '../../plugins/EditLink'; import { colorChoice, dropdownMenuColor, FormatButton, getColorMark, MarkButton } from '../../plugins/HoveringToolbar'; import { isSameLineSelection } from '../../plugins/pluginsUtils/selectionUtils'; import { getEditorEventEmitter } from '../table/selection'; import { AlignSelectButton, BlockIconButton, CardMenuButton, FileButton, FontLetterSelectButton, LineHeightSelectButton, SizeSelectButton, StaticToolbarMoreMenuButton, TabButton, TocButton, ToolbarArrowButton, ToolbarButton, TypeSelectButton, VersionButton, } from './buttons'; const noOpHandler = e => { e.preventDefault(); e.stopPropagation(); return false; }; export const isTableUnbordered = (editor: ReactEditor) => { if (!editor) return false; const { selection } = editor; const [table] = Editor.nodes(editor, { match: (n: any) => n.type === 'table' }); if (table && selection) { return table[0]['unbordered']; } return false; }; export const StaticToolbar = ({ editor, editorSelection, docWidth, modalState, currentColor, setCurrentColor, anchorTrigger, setValue, isShowAnchor, setIsShowAnchor, }: any) => { const { isReadOnly, docId } = React.useContext(TripdocsSdkContext); const { elementPath, elementType } = editorSelection; const isInElectron: boolean = getCache(editor?.docId, 'options')?.isInElectron; const [isTitle, setIsTitle] = useState(elementType === 'heading-one' && elementPath[0] === 0); const [visible, setVisible] = useState({ fontColorVisible: false, bgColorVisible: false, cellBgColorVisible: false, }); const sel = editor && editor.selection; const [docSelection, setDocSelection] = useState(null); const [isTable, setIsTable] = useState({ hasSelCells: SEL_CELLS.get(editor)?.length > 0, cursorInTable: elementPath.length > 4, both: SEL_CELLS.get(editor)?.length > 0 || elementPath.length > 4, }); const selectionHandler = useCallback( _.debounce(() => { editor?.selection && setDocSelection(editor.selection); }, 50), [editor] ); useEffect(() => { document.addEventListener('selectionchange', selectionHandler); return () => { document.removeEventListener('selectionchange', selectionHandler); }; }, [editor]); useEffect(() => { console.log('DOC_SEL', docSelection, docSelection?.focus?.path[0] === 0); if (docSelection?.focus?.path[0] === 0 || !docSelection) { $(`#editorContainer-${docId} .header-static-toolbar`).addClass('disabled-static-toolbar'); } else { $(`#editorContainer-${docId} .header-static-toolbar`).removeClass('disabled-static-toolbar'); } const elPath = editor?.selection?.anchor?.path; setIsTable({ hasSelCells: SEL_CELLS.get(editor)?.length > 0, cursorInTable: elPath?.length > 4, both: SEL_CELLS.get(editor)?.length > 0 || elPath?.length > 4, }); return () => {}; }, [docSelection]); useEffect(() => { setIsTitle(elementType === 'heading-one' && elementPath[0] === 0); }, [elementPath, elementType]); const cellBgColorClick = () => { const selectedCells = SEL_CELLS.get(editor); if (selectedCells?.length > 0) { for (let cell of selectedCells) { Transforms.setNodes(editor, { cellBackgroundColor: currentColor.cellBgColor } as Partial, { at: cell[1] }); } } else { editor.selection && ReactEditor.hasRange(editor, editor.selection) && editor.selection.focus.path.length === 6 && Transforms.setNodes(editor, { cellBackgroundColor: currentColor.cellBgColor } as Partial, { at: editor.selection.focus.path.slice(0, 4), }); } }; const bgColorClick = () => { colorChoice(editor, 'backgroundColor', currentColor.bgColor); }; const fontColorClick = () => { colorChoice(editor, 'fontColor', currentColor.fontColor); }; const resetClick = () => { const selectedCells = SEL_CELLS.get(editor); if (selectedCells?.length > 0) { for (let cell of selectedCells) { Transforms.setNodes(editor, { cellBackgroundColor: null } as Partial, { at: cell[1] }); } } else { editor?.selection && ReactEditor.hasRange(editor, editor.selection) && editor.selection.focus.path.length === 6 && Transforms.setNodes(editor, { cellBackgroundColor: null } as Partial, { at: editor.selection.focus.path.slice(0, 4) }); } }; const [shouldShrink, setShouldShrink] = useState(false); useEffect(() => { const SHRINK_THRESHOLD = { LARGE: 1200, MIDDLE: 811 }; const contentWrapListener = rect => { if (rect.width <= SHRINK_THRESHOLD.LARGE) { setShouldShrink(true); } else if (rect.width > SHRINK_THRESHOLD.LARGE) { setShouldShrink(false); } }; getEditorEventEmitter(docId).on('resizeContentWrap', contentWrapListener, docId); return () => { getEditorEventEmitter(docId).off('resizeContentWrap', contentWrapListener, docId); }; }, [docId]); const en = 'en'; return (
{ e.preventDefault(); }} > {} } callback={() => { editor.undo(); }} style={{ cursor: editor && editor.history.undos.length === 0 ? 'not-allowed' : null, opacity: editor && editor.history.undos.length === 0 ? '0.4' : null, }} /> } callback={() => { editor.redo(); }} style={{ cursor: editor && editor.history.redos.length === 0 ? 'not-allowed' : null, opacity: editor && editor.history.redos.length === 0 ? '0.4' : null, }} /> } /> } /> <> } /> } /> } /> } /> } /> { e.preventDefault(); fontColorClick(); }} > setCurrentColor({ ...currentColor, fontColor: flag }), getColorMark(editor, 'fontColor') )} trigger={['click']} visible={visible.fontColorVisible} onVisibleChange={flag => setVisible({ ...visible, fontColorVisible: flag })} >
} /> { e.preventDefault(); bgColorClick(); }} > setCurrentColor({ ...currentColor, bgColor: flag }), getColorMark(editor, 'backgroundColor') )} trigger={['click']} visible={visible.bgColorVisible} onVisibleChange={flag => setVisible({ ...visible, bgColorVisible: flag })} > } /> {} {isTable.both && ( <> {} { e.preventDefault(); cellBgColorClick(); }} > setCurrentColor({ ...currentColor, cellBgColor: flag }), getColorMark(editor, 'cellBackgroundColor') )} visible={visible.cellBgColorVisible} onVisibleChange={flag => setVisible({ ...visible, cellBgColorVisible: flag })} trigger={['click']} > } /> { e.preventDefault(); const sel = editor.selection; if (sel && sel.focus.path.length > 3) { const [tableNode, tablePath]: any = Editor.node(editor, [sel.focus.path[0], 1]); console.log(sel, tableNode, tablePath); if (tableNode.unbordered) { Transforms.setNodes(editor, { unbordered: null } as Partial, { at: tablePath }); } else { Transforms.setNodes(editor, { unbordered: true } as Partial, { at: tablePath }); } } }} > } /> {} )} {} {shouldShrink ? ( ) : ( <> } isDisabled={isTable.hasSelCells} /> } isDisabled={isTable.hasSelCells} /> } isDisabled={isTable.hasSelCells} /> } isDisabled={isTable.hasSelCells} /> } isDisabled={ (editor && editor.selection && Range.isRange(editor.selection) && ReactEditor.hasRange(editor, editor.selection) && !isSameLineSelection(editor.selection)) || isTable.hasSelCells } onMouseDown={e => { e.preventDefault(); if ( editor && editor.selection && Range.isRange(editor.selection) && ReactEditor.hasRange(editor, editor.selection) && !isSameLineSelection(editor.selection) ) { return; } insertEditLink(editor, '', sel); return; }} /> } editor={editor} />} /> } editor={editor} />} /> {} {} {!isInElectron && ( } editor={editor} setValue={setValue} />} /> )} } editor={editor} setIsShowAnchor={setIsShowAnchor} isShowAnchor={isShowAnchor} /> } /> )} ); };