import { AlignCenterOutlined, AlignLeftOutlined, AlignRightOutlined, CheckOutlined, LineHeightOutlined, MoreOutlined, PlusSquareTwoTone, VerticalAlignBottomOutlined, VerticalAlignMiddleOutlined, VerticalAlignTopOutlined, } from '@ant-design/icons'; import storage from '@utils/storage'; import { Dropdown, message, Modal, Popconfirm, Select, Tooltip } from 'antd'; import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { Editor, Node, Range, Transforms } from '@src/components/slate-packages/slate'; import { isBlockActive, toggleBlock } from '../block'; import { COLOR_ACTIVE, COLOR_DEFAULT, COLOR_DISABLED, IconBtn, IconButton } from '../Components'; import { ELTYPE, HEADING_TYPES, TABBABLE_TYPES } from '../config'; import { alignToggle, isAlignActive, isVerticalAlignActive, setVerticalAlign } from '../HoveringToolbar'; import { decreaseIndent, increaseIndent } from '../indent'; import { insertOl } from '../OLULList/OlList'; import { min } from '../siderMenu'; import staticToolbarCardMenu from '../../../DropdownMenu/staticToolbarCardMenu'; import staticToolbarMoreMenu from '../../../DropdownMenu/staticToolbarMoreMenu'; import { opsTable, TableOps } from '../table/tableOperation'; import { css, cx } from '@emotion/css'; import { isLineHeightActive } from '../HoveringToolbar/lineheight'; import { getCache, recoverCacheDocContent } from '@src/utils/cacheUtils'; import { toSharedType, YjsEditor } from '@src/components/slate-packages/slate-yjs'; import { IS_RECOVERING_CONTENT } from '../ErrorHandle/weak-maps'; import sessStorage from '@src/utils/sessStorage'; import { SEL_CELLS } from '@src/utils/weak-maps'; import { getEditorEventEmitter } from '../table/selection'; import { createUUID } from '@src/utils/randomId'; import { f } from '@src/resource/string'; import { ReactEditor } from '@src/components/slate-packages/slate-react'; import { fontLetterByNum, fontSizeByNum } from '../HoveringToolbar/fontSize'; export const FileButton: React.FC = (props: any) => { const { title, icon, callback, style } = props; const [tooltipVisible, setTooltipVisible] = useState(false); return ( { setTooltipVisible(visible); }} >
{ e.preventDefault(); callback(); window.event.returnValue = false; setTooltipVisible(false); }} style={style} > {icon}
); }; interface ToolbarButtonProps { title: string; button: React.ReactElement; onMouseDown: (e?: React.MouseEvent) => void; isDisabled?: boolean; buttonClass?: string; } export const ToolbarButton: React.FC = (props: ToolbarButtonProps) => { const { title, button, onMouseDown: mouseDownFn, isDisabled = false, buttonClass } = props; const [tooltipVisible, setTooltipVisible] = useState(false); return ( { setTooltipVisible(visible); }} >
{ e.preventDefault(); setTooltipVisible(false); mouseDownFn && mouseDownFn(e); }} onMouseDownCapture={e => { if (isDisabled) { e.preventDefault(); e.stopPropagation(); return; } }} style={{ cursor: isDisabled ? 'not-allowed' : null, opacity: isDisabled ? '0.4' : null, }} > {button}
); }; export const ToolbarArrowButton: React.FC = (props: ToolbarButtonProps) => { const { button, isDisabled = false, buttonClass } = props; return (
{ if (isDisabled) { e.preventDefault(); e.stopPropagation(); return; } }} style={{ cursor: isDisabled ? 'not-allowed' : null, opacity: isDisabled ? '0.4' : null, }} > {button}
); }; export const VerticalAlignButton: React.FC = ({ valign, icon, editor, ...props }) => { const selection = editor && editor.selection; const active = editor && isVerticalAlignActive(editor, valign, selection); return ( { event.preventDefault(); setVerticalAlign(editor, valign, selection); }} > {icon} ); }; export const handleTableOps = (editor: ReactEditor, ops: TableOps) => { const selection = editor && editor.selection; if ( selection && ReactEditor.hasRange(editor, selection) && ((selection.anchor && min(selection.anchor.path[0], selection.focus.path[0]) !== 0) || (!selection.anchor && selection[0] !== 0)) ) { opsTable(editor, ops, selection); } else { if (selection && ReactEditor.hasRange(editor, selection) && selection.anchor && Range.start(selection).path[0] === Range.end(selection).path[0]) { if (selection.anchor.path[0] < selection.focus.path[0]) { let sel = { anchor: { path: [selection.anchor.path[0] + 1, selection.anchor.path[1]], offset: 0, }, focus: selection.focus, }; opsTable(editor, ops, sel); } else { let sel = { anchor: selection.anchor, focus: { path: [selection.focus.path[0] + 1, selection.focus.path[1]], offset: 0 }, }; opsTable(editor, ops, sel); } } else if (selection && ReactEditor.hasRange(editor, selection) && !selection.anchor) { opsTable(editor, ops, selection); } } }; export const TableOpsButton: React.FC = (props: any) => { const { ops, icon, editor, ...attributes } = props; return ( { event.preventDefault(); handleTableOps(editor, ops); }} > ); }; export const StaticToolbarMoreMenuButton: React.FC = ({ editor, editorSelection, docWidth, modalState, setValue, isShowAnchor, setIsShowAnchor, isDisabled, hasSelCells, ...props }) => { const [tooltipVisible, setTooltipVisible] = useState(false); const [cardMenuVisible, setCardMenuVisible] = useState(false); const selection = editor && editor.selection; const node = selection && ReactEditor.hasRange(editor, selection) && Node.get(editor, selection.focus.path.slice(0, -1)); return ( { !cardMenuVisible && setTooltipVisible(visible); }} >
{ e.preventDefault(); }} >
setCardMenuVisible(flag)} >
{ e.preventDefault(); setTooltipVisible(false); }} onClick={e => { e.preventDefault(); }} > { e.preventDefault(); }} />
); }; export const CardMenuButton: React.FC = ({ editor, editorSelection, docWidth, modalState, isDisabled, ...props }) => { const [tooltipVisible, setTooltipVisible] = useState(false); const [cardMenuVisible, setCardMenuVisible] = useState(false); return ( { !cardMenuVisible && setTooltipVisible(visible); }} > setCardMenuVisible(flag)} >
{ e.preventDefault(); setTooltipVisible(false); }} onMouseDownCapture={e => { if (isDisabled) { e.preventDefault(); e.stopPropagation(); return; } }} onClick={e => { e.preventDefault(); }} style={{ width: '64px', borderRadius: '4px', cursor: isDisabled ? 'not-allowed' : null, opacity: isDisabled ? '0.4' : null, }} > { e.preventDefault(); }} />
{f('insert')}
); }; export const FontLetterSelectButton: React.FC = ({ editor, anchorTrigger, ...props }) => { const [tooltipVisible, setTooltipVisible] = useState(false); const [selectVisible, setSelectVisible] = useState(false); const selection = editor && editor.selection; const node: any = selection && ReactEditor.hasRange(editor, selection) && ReactEditor.hasRange(editor, selection) && Node.get(editor, Range.start(selection).path); const { Option } = Select; const letter = [0, 1, 2, 3, 4, 5, 6]; let curFontLetter: any = (node && node.fontLetter) || 0; return useMemo( () => ( { !selectVisible && setTooltipVisible(visible); }} > ), [curFontLetter, tooltipVisible] ); }; export const SizeSelectButton: React.FC = ({ editor, anchorTrigger, ...props }) => { const [tooltipVisible, setTooltipVisible] = useState(false); const [selectVisible, setSelectVisible] = useState(false); const selection = editor && editor.selection; const node: any = selection && ReactEditor.hasRange(editor, selection) && ReactEditor.hasRange(editor, selection) && Node.get(editor, Range.start(selection).path); const { Option } = Select; const sizeArr = [12, 14, 16, 18, 20, 24, 28, 30, 32, 36, 40, 48, 56, 64, 72, 96, 120, 144]; let curSize: any = (node && node.fontSizeChange && node.fontSizeChange + 14) || 14; return useMemo( () => ( { !selectVisible && setTooltipVisible(visible); }} > ), [curSize, tooltipVisible] ); }; export const TypeSelectButton: React.FC = ({ editor, anchorTrigger, ...props }) => { const [tooltipVisible, setTooltipVisible] = useState(false); const [selectVisible, setSelectVisible] = useState(false); const selection = editor && editor.selection; const node: any = selection && ReactEditor.hasRange(editor, selection) && ReactEditor.hasRange(editor, selection) && Node.get(editor, Range.start(selection).path.slice(0, -1)); const { Option } = Select; const typeArr = [ ELTYPE.PARAGRAPH, ELTYPE.HEADING_ONE, ELTYPE.HEADING_TWO, ELTYPE.HEADING_THREE, ELTYPE.HEADING_FOUR, ELTYPE.HEADING_FIVE, ELTYPE.HEADING_SIX, ]; const mapping = { [ELTYPE.PARAGRAPH]: f('normal'), [ELTYPE.HEADING_ONE]: f('heading1'), [ELTYPE.HEADING_TWO]: f('heading2'), [ELTYPE.HEADING_THREE]: f('heading3'), [ELTYPE.HEADING_FOUR]: f('heading4'), [ELTYPE.HEADING_FIVE]: f('heading5'), [ELTYPE.HEADING_SIX]: f('heading6'), }; let curType: any = node && (node.type as string) ? (node.type as string) : ELTYPE.PARAGRAPH; if (node && node.oldType && (node.type == ELTYPE.ULLIST || node.type == ELTYPE.OLLIST || node.type == ELTYPE.TODO_LIST)) { curType = node.oldType; } const mappedValue = mapping[curType] ?? mapping[ELTYPE.PARAGRAPH]; return useMemo( () => ( { !selectVisible && setTooltipVisible(visible); }} > ), [curType, tooltipVisible] ); }; export const AlignSelectButton: React.FC = ({ editor, isVertical, ...props }) => { const [tooltipVisible, setTooltipVisible] = useState(false); const [selectVisible, setSelectVisible] = useState(false); const selection = editor && editor.selection; const node: any = selection && ReactEditor.hasRange(editor, selection) && Node.get(editor, selection.focus.path.slice(0, -1)); const { Option } = Select; const alignArr = ['align-left', 'align-center', 'align-right']; const vAlignArr: any = ['top', 'middle', 'bottom']; const mapping: any = { 'align-left': [f('alignLeft'), ], 'align-center': [f('alignCenter'), ], 'align-right': [f('alignRight'), ], top: [f('alignTop'), ], middle: [f('alignMiddle'), ], bottom: [f('alignBottom'), ], }; let curAlign: any; const defaultAlign = { vertical: vAlignArr.filter( (valign: any) => editor && editor.selection && ReactEditor.hasRange(editor, selection) && isVerticalAlignActive(editor, valign, selection) )[0], horizontal: alignArr.filter(align => editor && editor.selection && ReactEditor.hasRange(editor, selection) && isAlignActive(editor, align, selection))[0] || (node && node.type === ELTYPE.IMAGE ? 'align-center' : 'align-left'), }; curAlign = selection && ReactEditor.hasRange(editor, selection) && (isVertical ? defaultAlign.vertical : defaultAlign.horizontal); return ( { !selectVisible && setTooltipVisible(visible); }} > ); }; export const BlockIconButton: React.FC = (props: any) => { const { format, icon, svgIcon, editor, isDisabled, ...attributes } = props; const selection = editor && editor.selection; const active = editor && isBlockActive(editor, format, selection); let alignActive: any; if (format.startsWith('align')) { alignActive = editor && isAlignActive(editor, format, selection); } return (
{ event.preventDefault(); if (editor.selection) { const rowNode: any = editor.selection.anchor.path.length < 3 ? Node.get(editor, [editor.selection.anchor.path[0]]) : Node.get(editor, editor.selection.anchor.path.slice(0, -1)); if (format.startsWith('align')) { const fo = format.split('-')[1]; alignToggle(editor, fo, selection); return; } if (rowNode.type !== format && [ELTYPE.OLLIST, ELTYPE.ULLIST].includes(format)) { let id = format + '=' + createUUID(); const employee = storage.get('userInfo')?.employee; insertOl( editor, { type: format, tabLevel: 0, num: 1, id: id, oldType: rowNode.oldType || (HEADING_TYPES.includes(rowNode.type) ? rowNode.type : undefined), authCls: 'auth-' + employee, elId: createUUID(), }, { at: editor.selection } ); return; } if (selection && ReactEditor.hasRange(editor, selection) && selection[0] !== 0) { toggleBlock(editor, format, selection); } else { if (selection) { toggleBlock(editor, format, selection); } } } }} >
{svgIcon ? svgIcon : }
); }; export const TabButton: React.FC = (props: any) => { const { format, icon, editor, isDecrease, ...attributes } = props; const selection = editor && editor.selection; return ( { event.preventDefault(); const selCells = SEL_CELLS.get(editor); const hasSelCells = selCells && selCells.length > 0; const node = selection && ReactEditor.hasRange(editor, selection) && Node.get(editor, selection.focus.path.slice(0, -1)); if (isDecrease) { if (hasSelCells) { selCells.forEach((entry: any) => { const [, path] = entry; const node: any = Node.get(editor, path); const { children } = node; children.forEach((child: any, index: number) => { if ([...TABBABLE_TYPES].includes(child.type)) { decreaseIndent(editor, child, { anchor: { path: [...path, index, 0], offset: 0 }, focus: { path: [...path, index, 0], offset: 0 }, }); } }); }); } else { decreaseIndent(editor, node, selection); } } else { if (hasSelCells) { selCells.forEach((entry: any) => { const [, path] = entry; const node: any = Node.get(editor, path); const { children } = node; console.log('[increaseIndent]', children); children.forEach((child: any, index: number) => { if ([...TABBABLE_TYPES].includes(child.type)) { increaseIndent(editor, child, { anchor: { path: [...path, index, 0], offset: 0 }, focus: { path: [...path, index, 0], offset: 0 }, }); } }); }); } else { console.log('increaseIndent((((', editor, node, selection); increaseIndent(editor, node, selection); } } }} > {icon} ); }; export const LineHeightSelectButton: React.FC = ({ editor, ...props }) => { const [tooltipVisible, setTooltipVisible] = useState(false); const [selectVisible, setSelectVisible] = useState(false); const selection = editor && editor.selection; const node = selection && ReactEditor.hasRange(editor, selection) && Node.get(editor, selection.focus.path.slice(0, -1)); const { Option } = Select; const lineHeightArr: any = ['1.75', '1', '1.15', '1.5', '2', '2.5', '3']; let curLineHeight; curLineHeight = selection && ReactEditor.hasRange(editor, selection) && lineHeightArr.filter( (lineHeight: any) => editor && editor.selection && ReactEditor.hasRange(editor, selection) && isLineHeightActive(editor, lineHeight, selection) )[0]; const en = 'en'; return ( { !selectVisible && setTooltipVisible(visible); }} > ); }; export const TocButton: React.FC = (props: any) => { const { format, icon, editor, setIsShowAnchor, isShowAnchor, ...attributes } = props; const selection = editor && editor.selection; return ( { event.preventDefault(); setIsShowAnchor(prev => !prev); }} > {icon} ); }; export const CACHE_DOC_CONTENT: WeakMap = new WeakMap(); export const VersionButton: React.FC = (props: any) => { const { format, icon, editor, setValue, ...attributes } = props; const selection = editor && editor.selection; const node = selection && ReactEditor.hasRange(editor, selection) && Node.get(editor, selection.focus.path.slice(0, -1)); return ( { event.preventDefault(); showCacheDocContentModal(editor); const { docId } = editor; }} > {icon} ); }; const DocCacheItemContent = props => { const { docContentCacheArr, editor } = props; const [index, setIndex] = useState(0); const serialize = useCallback(nodes => { return nodes.map(n => Node.string(n)).join('\n'); }, []); useEffect(() => { CACHE_DOC_CONTENT.set(editor, docContentCacheArr?.[index]?.docContent); }, [index]); return (
{docContentCacheArr.map(({ docContent, at }, i) => { return (
{ e.preventDefault(); setIndex(i); }} >
保存时间 {': '} {at.replace(/:\d{3}$/, '')}
文档内容 {': '} {} {serialize(docContent)}
); })}
); }; export const showCacheDocContentModal = editor => { const { docId } = editor; const docContentCacheArr = sessStorage.get('tripdocs_sdk/docContentCacheArr_' + docId) || []; if (docContentCacheArr.length > 0) { const modal = Modal.info({ title: '文档历史版本', content: , maskClosable: true, closable: true, width: '70vw', onOk: () => { const cacheDocContent = CACHE_DOC_CONTENT.get(editor); IS_RECOVERING_CONTENT.set(editor, true); window.tripdocs.editorsMap[editor.docId].api.setContent(cacheDocContent); setTimeout(() => IS_RECOVERING_CONTENT.set(editor, false)); }, }); } else { message.destroy(); message.info('没有历史版本'); } };