From 7195c4632f2df2e34c063cdc9ec32c5cd79ba79a Mon Sep 17 00:00:00 2001 From: Felix Feng Date: Thu, 26 Dec 2024 17:51:15 +0800 Subject: [PATCH] Add font-size-toolbar-button --- .../plate-ui/font-size-toolbar-butoon.tsx | 66 +++++++++++++++---- packages/font/src/lib/BaseFontSizePlugin.ts | 54 ++++++++++----- .../font/src/lib/utils/setChangedFontSize.ts | 4 +- 3 files changed, 93 insertions(+), 31 deletions(-) diff --git a/apps/www/src/registry/default/plate-ui/font-size-toolbar-butoon.tsx b/apps/www/src/registry/default/plate-ui/font-size-toolbar-butoon.tsx index 976ff18840..e4d40f18aa 100644 --- a/apps/www/src/registry/default/plate-ui/font-size-toolbar-butoon.tsx +++ b/apps/www/src/registry/default/plate-ui/font-size-toolbar-butoon.tsx @@ -1,6 +1,18 @@ 'use client'; -import { type TElement, getAboveNode, getMarks } from '@udecode/plate-common'; -import { useEditorSelector } from '@udecode/plate-common/react'; +import { useState } from 'react'; + +import { + type TElement, + getAboveNode, + getMarks, + setMarks, +} from '@udecode/plate-common'; +import { + focusEditor, + useEditorPlugin, + useEditorSelector, +} from '@udecode/plate-common/react'; +import { BaseFontSizePlugin } from '@udecode/plate-font'; import { FontSizePlugin } from '@udecode/plate-font/react'; import { HEADING_KEYS } from '@udecode/plate-heading'; import { Minus, Plus } from 'lucide-react'; @@ -13,9 +25,11 @@ const FONT_SIZE_MAP = { [HEADING_KEYS.h3]: '20px', }; -export const FontSizeToolbarButton = () => { - // const { api } = useEditorPlugin(FontSizePlugin); - // const setChangedFontSize = api.fontSize.setChangedFontSize; +export function FontSizeToolbarButton() { + const [inputValue, setInputValue] = useState(''); + const [isFocused, setIsFocused] = useState(false); + const { api, editor } = useEditorPlugin(BaseFontSizePlugin); + const setChangedFontSize = api.fontSize.setChangedFontSize; const cursorFontSize = useEditorSelector((editor) => { const marks = getMarks(editor) || {}; @@ -36,25 +50,51 @@ export const FontSizeToolbarButton = () => { return '16px'; }, []); + const handleInputChange = () => { + if (inputValue && inputValue !== cursorFontSize) { + setMarks(editor, { + [FontSizePlugin.key]: inputValue, + }); + } + + focusEditor(editor); + }; + + const displayValue = isFocused ? inputValue : cursorFontSize; + return (
- // setChangedFontSize({ fontSize: cursorFontSize, increase: false }) - // } + onClick={() => + setChangedFontSize({ fontSize: displayValue, increase: false }) + } onMouseDown={(e) => e.preventDefault()} > - {cursorFontSize as string} + { + setIsFocused(false); + handleInputChange(); + }} + onChange={(e) => setInputValue(e.target.value)} + onFocus={() => { + setIsFocused(true); + setInputValue(cursorFontSize); + }} + onKeyDown={(e) => e.key === 'Enter' && handleInputChange()} + type="text" + /> - // setChangedFontSize({ fontSize: cursorFontSize, increase: true }) - // } + onClick={() => + setChangedFontSize({ fontSize: displayValue, increase: true }) + } onMouseDown={(e) => e.preventDefault()} >
); -}; +} diff --git a/packages/font/src/lib/BaseFontSizePlugin.ts b/packages/font/src/lib/BaseFontSizePlugin.ts index b9ee69f873..e6b8c04feb 100644 --- a/packages/font/src/lib/BaseFontSizePlugin.ts +++ b/packages/font/src/lib/BaseFontSizePlugin.ts @@ -1,26 +1,48 @@ -import { createSlatePlugin } from '@udecode/plate-common'; +import { + type PluginConfig, + bindFirst, + createTSlatePlugin, +} from '@udecode/plate-common'; -export const BaseFontSizePlugin = createSlatePlugin({ +import { type getChangedFontSizeOptions, setChangedFontSize } from './utils'; + +export type BaseFontSizeConfig = PluginConfig< + 'fontSize', + FontSizeSelectors, + { + fontSize: { + setChangedFontSize: (options: getChangedFontSizeOptions) => void; + }; + } +>; + +type FontSizeSelectors = {}; + +export const BaseFontSizePlugin = createTSlatePlugin({ key: 'fontSize', inject: { nodeProps: { nodeKey: 'fontSize', }, }, -}).extend(({ type }) => ({ - parsers: { - html: { - deserializer: { - isLeaf: true, - parse: ({ element }) => ({ [type]: element.style.fontSize }), - rules: [ - { - validStyle: { - fontSize: '*', +}) + .extend(({ type }) => ({ + parsers: { + html: { + deserializer: { + isLeaf: true, + parse: ({ element }) => ({ [type]: element.style.fontSize }), + rules: [ + { + validStyle: { + fontSize: '*', + }, }, - }, - ], + ], + }, }, }, - }, -})); + })) + .extendApi(({ editor }) => ({ + setChangedFontSize: bindFirst(setChangedFontSize, editor), + })); diff --git a/packages/font/src/lib/utils/setChangedFontSize.ts b/packages/font/src/lib/utils/setChangedFontSize.ts index 380aa77517..299262c27e 100644 --- a/packages/font/src/lib/utils/setChangedFontSize.ts +++ b/packages/font/src/lib/utils/setChangedFontSize.ts @@ -1,6 +1,6 @@ import { type SlateEditor, setMarks } from '@udecode/plate-common'; -import { FontSizePlugin } from '../../react'; +import { BaseFontSizePlugin } from '../BaseFontSizePlugin'; export const setChangedFontSize = ( editor: SlateEditor, @@ -9,7 +9,7 @@ export const setChangedFontSize = ( const { fontSize, increase } = options; setMarks(editor, { - [FontSizePlugin.key]: getChangedFontSize({ fontSize, increase }), + [BaseFontSizePlugin.key]: getChangedFontSize({ fontSize, increase }), }); };