From f89c1051db5f023454d698c6ac9b19282bf4da67 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Mon, 25 Apr 2022 10:54:38 +0800 Subject: [PATCH] feat: add defaultShowPicker for katex --- .../client/src/tiptap/extensions/katex.ts | 10 ++++- .../client/src/tiptap/menus/insert/index.tsx | 2 +- .../client/src/tiptap/menus/quick-insert.tsx | 9 +++- .../tiptap/wrappers/katex/index.module.scss | 10 ++++- .../src/tiptap/wrappers/katex/index.tsx | 42 +++++++++++++++---- 5 files changed, 60 insertions(+), 13 deletions(-) diff --git a/packages/client/src/tiptap/extensions/katex.ts b/packages/client/src/tiptap/extensions/katex.ts index 20fc7b0..faefe7f 100644 --- a/packages/client/src/tiptap/extensions/katex.ts +++ b/packages/client/src/tiptap/extensions/katex.ts @@ -2,10 +2,15 @@ import { Node, mergeAttributes, nodeInputRule } from '@tiptap/core'; import { ReactNodeViewRenderer } from '@tiptap/react'; import { KatexWrapper } from '../wrappers/katex'; +type IKatexAttrs = { + text?: string; + defaultShowPicker?: boolean; +}; + declare module '@tiptap/core' { interface Commands { katex: { - setKatex: () => ReturnType; + setKatex: (arg?: IKatexAttrs) => ReturnType; }; } } @@ -35,6 +40,9 @@ export const Katex = Node.create({ return element.getAttribute('data-text'); }, }, + defaultShowPicker: { + default: false, + }, }; }, diff --git a/packages/client/src/tiptap/menus/insert/index.tsx b/packages/client/src/tiptap/menus/insert/index.tsx index 7179d21..782360a 100644 --- a/packages/client/src/tiptap/menus/insert/index.tsx +++ b/packages/client/src/tiptap/menus/insert/index.tsx @@ -79,7 +79,7 @@ export const Insert: React.FC<{ editor: Editor }> = ({ editor }) => { 思维导图 - editor.chain().focus().setKatex().run()}> + editor.chain().focus().setKatex({ defaultShowPicker: true }).run()}> 数学公式 diff --git a/packages/client/src/tiptap/menus/quick-insert.tsx b/packages/client/src/tiptap/menus/quick-insert.tsx index 4090308..c22c0b0 100644 --- a/packages/client/src/tiptap/menus/quick-insert.tsx +++ b/packages/client/src/tiptap/menus/quick-insert.tsx @@ -208,7 +208,14 @@ export const QUICK_INSERT_ITEMS = [ 数学公式 ), - command: (editor: Editor) => editor.chain().focus().setKatex().run(), + command: (editor: Editor) => + editor + .chain() + .focus() + .setKatex({ + defaultShowPicker: true, + }) + .run(), }, { diff --git a/packages/client/src/tiptap/wrappers/katex/index.module.scss b/packages/client/src/tiptap/wrappers/katex/index.module.scss index 929c93a..a2413e9 100644 --- a/packages/client/src/tiptap/wrappers/katex/index.module.scss +++ b/packages/client/src/tiptap/wrappers/katex/index.module.scss @@ -1,6 +1,12 @@ .wrap { display: inline-flex; justify-content: center; - padding: 0 8px; - transform: translateY(8px); + transform: translateY(1px); + + font-size: 12px; + line-height: 16px; + height: 20px; + padding: 2px 8px; + + cursor: pointer; } diff --git a/packages/client/src/tiptap/wrappers/katex/index.tsx b/packages/client/src/tiptap/wrappers/katex/index.tsx index 4f51775..746ebbc 100644 --- a/packages/client/src/tiptap/wrappers/katex/index.tsx +++ b/packages/client/src/tiptap/wrappers/katex/index.tsx @@ -1,16 +1,19 @@ import { NodeViewWrapper, NodeViewContent } from '@tiptap/react'; -import { useMemo } from 'react'; +import { useMemo, useCallback, useEffect, useRef } from 'react'; import cls from 'classnames'; import { Popover, TextArea, Typography, Space } from '@douyinfe/semi-ui'; import { IconHelpCircle } from '@douyinfe/semi-icons'; import katex from 'katex'; +import { useToggle } from 'hooks/use-toggle'; import styles from './index.module.scss'; const { Text } = Typography; export const KatexWrapper = ({ editor, node, updateAttributes }) => { const isEditable = editor.isEditable; - const { text } = node.attrs; + const { text, defaultShowPicker } = node.attrs; + const ref = useRef(); + const [visible, toggleVisible] = useToggle(false); const formatText = useMemo(() => { try { @@ -20,22 +23,46 @@ export const KatexWrapper = ({ editor, node, updateAttributes }) => { } }, [text]); - const content = text.trim() ? ( - - ) : ( - 点击输入公式 + const content = useMemo( + () => + text.trim() ? ( + + ) : ( + 点击输入公式 + ), + [text] ); + const onVisibleChange = useCallback( + (value) => { + toggleVisible(value); + if (defaultShowPicker) { + updateAttributes({ defaultShowPicker: false }); + } + }, + [defaultShowPicker, updateAttributes] + ); + + useEffect(() => { + if (defaultShowPicker) { + toggleVisible(true); + setTimeout(() => ref.current?.focus(), 100); + } + }, [defaultShowPicker]); + return ( - {isEditable ? (