From d0735313256d1d36123a8dbcd682bcfd83aa01b6 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Sun, 5 Jun 2022 00:17:09 +0800 Subject: [PATCH] tiptap: add defaultShowPicker for DocumentReference --- .../core/extensions/document-reference.ts | 17 +++++++++--- .../client/src/tiptap/core/menus/commands.tsx | 3 ++- .../core/menus/document-reference/bubble.tsx | 27 ++++++++++++++++++- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/packages/client/src/tiptap/core/extensions/document-reference.ts b/packages/client/src/tiptap/core/extensions/document-reference.ts index 2e00641a..6f666c62 100644 --- a/packages/client/src/tiptap/core/extensions/document-reference.ts +++ b/packages/client/src/tiptap/core/extensions/document-reference.ts @@ -3,10 +3,15 @@ import { ReactNodeViewRenderer } from '@tiptap/react'; import { DocumentReferenceWrapper } from 'tiptap/core/wrappers/document-reference'; import { getDatasetAttribute } from 'tiptap/prose-utils'; +type IDocumentReferenceAttrs = { + defaultShowPicker?: boolean; + createUser: string; +}; + declare module '@tiptap/core' { interface Commands { documentReference: { - setDocumentReference: () => ReturnType; + setDocumentReference: (arg?: IDocumentReferenceAttrs) => ReturnType; }; } } @@ -32,6 +37,12 @@ export const DocumentReference = Node.create({ default: '', parseHTML: getDatasetAttribute('title'), }, + defaultShowPicker: { + default: false, + }, + createUser: { + default: null, + }, }; }, @@ -58,11 +69,11 @@ export const DocumentReference = Node.create({ addCommands() { return { setDocumentReference: - () => + (attrs) => ({ commands }) => { return commands.insertContent({ type: this.name, - attrs: {}, + attrs, }); }, }; diff --git a/packages/client/src/tiptap/core/menus/commands.tsx b/packages/client/src/tiptap/core/menus/commands.tsx index f349e45a..f02684af 100644 --- a/packages/client/src/tiptap/core/menus/commands.tsx +++ b/packages/client/src/tiptap/core/menus/commands.tsx @@ -147,7 +147,8 @@ export const COMMANDS: ICommand[] = [ { icon: , label: '文档', - action: (editor) => editor.chain().focus().setDocumentReference().run(), + action: (editor, user) => + editor.chain().focus().setDocumentReference({ defaultShowPicker: true, createUser: user.id }).run(), }, { icon: , diff --git a/packages/client/src/tiptap/core/menus/document-reference/bubble.tsx b/packages/client/src/tiptap/core/menus/document-reference/bubble.tsx index a7954856..f8200e50 100644 --- a/packages/client/src/tiptap/core/menus/document-reference/bubble.tsx +++ b/packages/client/src/tiptap/core/menus/document-reference/bubble.tsx @@ -4,17 +4,33 @@ import { DataRender } from 'components/data-render'; import { Divider } from 'components/divider'; import { IconDocument } from 'components/icons'; import { Tooltip } from 'components/tooltip'; +import { useUser } from 'data/user'; import { useWikiTocs } from 'data/wiki'; +import { useToggle } from 'hooks/use-toggle'; import { useRouter } from 'next/router'; -import { useCallback } from 'react'; +import { useCallback, useEffect } from 'react'; import { BubbleMenu } from 'tiptap/core/bubble-menu'; import { DocumentReference } from 'tiptap/core/extensions/document-reference'; +import { useAttributes } from 'tiptap/core/hooks/use-attributes'; import { copyNode, deleteNode } from 'tiptap/prose-utils'; const { Text } = Typography; +type DocumentReferenceAttrs = { + defaultShowPicker: boolean; + createUser: string; +}; + export const DocumentReferenceBubbleMenu = ({ editor }) => { + const attrs = useAttributes(editor, DocumentReference.name, { + defaultShowPicker: false, + createUser: '', + }); + const { defaultShowPicker, createUser } = attrs; + const { user } = useUser(); const { pathname, query } = useRouter(); + const [visible, toggleVisible] = useToggle(false); + const wikiIdFromUrl = query?.wikiId; const isShare = pathname.includes('share'); const { data: tocs, loading, error } = useWikiTocs(isShare ? null : wikiIdFromUrl); @@ -36,6 +52,13 @@ export const DocumentReferenceBubbleMenu = ({ editor }) => { const copyMe = useCallback(() => copyNode(DocumentReference.name, editor), [editor]); const deleteMe = useCallback(() => deleteNode(DocumentReference.name, editor), [editor]); + useEffect(() => { + if (defaultShowPicker && user && createUser === user.id) { + toggleVisible(true); + editor.chain().updateAttributes(DocumentReference.name, { defaultShowPicker: false }).focus().run(); + } + }, [editor, defaultShowPicker, toggleVisible, createUser, user]); + return ( {