From 8453788b84758105d40bb5e5ad258dbc14598cc0 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Sat, 17 Dec 2022 19:55:03 +0800 Subject: [PATCH] close #204 --- .../src/tiptap/core/extensions/font-family.ts | 60 +++++++++++++++++++ .../tiptap/core/menus/fontfamily/index.tsx | 48 +++++++++++++++ .../collaboration/collaboration/menubar.tsx | 2 + .../src/tiptap/editor/collaboration/kit.ts | 4 +- 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 packages/client/src/tiptap/core/extensions/font-family.ts create mode 100644 packages/client/src/tiptap/core/menus/fontfamily/index.tsx diff --git a/packages/client/src/tiptap/core/extensions/font-family.ts b/packages/client/src/tiptap/core/extensions/font-family.ts new file mode 100644 index 0000000..848f3ae --- /dev/null +++ b/packages/client/src/tiptap/core/extensions/font-family.ts @@ -0,0 +1,60 @@ +import '@tiptap/extension-text-style'; + +import { Extension } from '@tiptap/core'; + +declare module '@tiptap/core' { + interface Commands { + fontFamily: { + setFontFamily: (fontFamily: string) => ReturnType; + unsetFontFamily: () => ReturnType; + }; + } +} + +export const FontFamily = Extension.create({ + name: 'fontFamily', + + addOptions() { + return { + types: ['textStyle'], + }; + }, + + addGlobalAttributes() { + return [ + { + types: this.options.types, + attributes: { + fontFamily: { + default: null, + parseHTML: (element) => element.style.fontFamily.replace(/['"]+/g, ''), + renderHTML: (attributes) => { + if (!attributes.fontFamily) { + return {}; + } + + return { + style: `font-family: ${attributes.fontFamily}`, + }; + }, + }, + }, + }, + ]; + }, + + addCommands() { + return { + setFontFamily: + (fontFamily) => + ({ chain }) => { + return chain().setMark('textStyle', { fontFamily }).run(); + }, + unsetFontFamily: + () => + ({ chain }) => { + return chain().setMark('textStyle', { fontFamily: null }).removeEmptyTextStyle().run(); + }, + }; + }, +}); diff --git a/packages/client/src/tiptap/core/menus/fontfamily/index.tsx b/packages/client/src/tiptap/core/menus/fontfamily/index.tsx new file mode 100644 index 0000000..1d84302 --- /dev/null +++ b/packages/client/src/tiptap/core/menus/fontfamily/index.tsx @@ -0,0 +1,48 @@ +import { Select } from '@douyinfe/semi-ui'; +import React, { useCallback } from 'react'; +import { Editor } from 'tiptap/core'; +import { Title } from 'tiptap/core/extensions/title'; +import { useActive } from 'tiptap/core/hooks/use-active'; +import { useAttributes } from 'tiptap/core/hooks/use-attributes'; + +export const Fonts = [ + 'Arial', + 'Arial Black', + 'Georgia', + 'Impact', + 'Tahoma', + 'Times New Roman', + 'Verdana', + 'Courier New', + 'Lucida Console', + 'Monaco', + 'monospace', +]; + +export const FontFamily: React.FC<{ editor: Editor }> = ({ editor }) => { + const isTitleActive = useActive(editor, Title.name); + const currentFontFamily = useAttributes(editor, 'textStyle', { fontFamily: '' }, (attrs) => attrs.fontFamily); + + const toggle = useCallback( + (val) => { + editor.chain().focus().setFontFamily(val).run(); + }, + [editor] + ); + + return ( + + ); +}; diff --git a/packages/client/src/tiptap/editor/collaboration/collaboration/menubar.tsx b/packages/client/src/tiptap/editor/collaboration/collaboration/menubar.tsx index a72310d..a4b3e13 100644 --- a/packages/client/src/tiptap/editor/collaboration/collaboration/menubar.tsx +++ b/packages/client/src/tiptap/editor/collaboration/collaboration/menubar.tsx @@ -19,6 +19,7 @@ import { DocumentReference } from 'tiptap/core/menus/document-reference'; import { Emoji } from 'tiptap/core/menus/emoji'; import { Excalidraw } from 'tiptap/core/menus/excalidraw'; import { Flow } from 'tiptap/core/menus/flow'; +import { FontFamily } from 'tiptap/core/menus/fontfamily'; import { FontSize } from 'tiptap/core/menus/fontsize'; import { Heading } from 'tiptap/core/menus/heading'; import { HorizontalRule } from 'tiptap/core/menus/horizontal-rule'; @@ -68,6 +69,7 @@ const _MenuBar: React.FC<{ editor: Editor }> = ({ editor }) => { + diff --git a/packages/client/src/tiptap/editor/collaboration/kit.ts b/packages/client/src/tiptap/editor/collaboration/kit.ts index 6a4d64b..7181533 100644 --- a/packages/client/src/tiptap/editor/collaboration/kit.ts +++ b/packages/client/src/tiptap/editor/collaboration/kit.ts @@ -26,6 +26,7 @@ import { EventEmitter } from 'tiptap/core/extensions/event-emitter'; import { Excalidraw } from 'tiptap/core/extensions/excalidraw'; import { Flow } from 'tiptap/core/extensions/flow'; import { Focus } from 'tiptap/core/extensions/focus'; +import { FontFamily } from 'tiptap/core/extensions/font-family'; import { FontSize } from 'tiptap/core/extensions/font-size'; import { Gapcursor } from 'tiptap/core/extensions/gapcursor'; import { HardBreak } from 'tiptap/core/extensions/hard-break'; @@ -48,7 +49,6 @@ import { Paste } from 'tiptap/core/extensions/paste'; import { Placeholder } from 'tiptap/core/extensions/placeholder'; import { QuickInsert } from 'tiptap/core/extensions/quick-insert'; import { SearchNReplace } from 'tiptap/core/extensions/search'; -import { SelectionExtension } from 'tiptap/core/extensions/selection'; import { Status } from 'tiptap/core/extensions/status'; import { Strike } from 'tiptap/core/extensions/strike'; import { Subscript } from 'tiptap/core/extensions/subscript'; @@ -124,6 +124,7 @@ export const CollaborationKit = [ Excalidraw, EventEmitter, Focus, + FontFamily, FontSize, Gapcursor, HardBreak, @@ -137,7 +138,6 @@ export const CollaborationKit = [ ListItem, Loading, OrderedList, - // SelectionExtension, Strike, Subscript, Superscript,