-
-
Notifications
You must be signed in to change notification settings - Fork 761
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3922 from udecode/toolbar/improve
Toolbar/improve
- Loading branch information
Showing
31 changed files
with
628 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@udecode/plate-font': patch | ||
--- | ||
|
||
Add api `api.fontSize.setMark`. | ||
Add utils `toUnitLess`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
apps/www/public/r/styles/default/font-size-toolbar-button.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"dependencies": [ | ||
"@udecode/plate-font" | ||
], | ||
"doc": { | ||
"description": "A toolbar control for adjusting font size.", | ||
"docs": [ | ||
{ | ||
"route": "/docs/font" | ||
} | ||
], | ||
"examples": [ | ||
"list-demo" | ||
] | ||
}, | ||
"files": [ | ||
{ | ||
"content": "'use client';\nimport { useState } from 'react';\n\nimport { cn } from '@udecode/cn';\nimport { type TElement, getAboveNode, getMarks } from '@udecode/plate-common';\nimport {\n focusEditor,\n useEditorPlugin,\n useEditorSelector,\n} from '@udecode/plate-common/react';\nimport { BaseFontSizePlugin, toUnitLess } from '@udecode/plate-font';\nimport { FontSizePlugin } from '@udecode/plate-font/react';\nimport { HEADING_KEYS } from '@udecode/plate-heading';\nimport { Minus, Plus } from 'lucide-react';\n\nimport { Popover, PopoverContent, PopoverTrigger } from './popover';\nimport { ToolbarButton } from './toolbar';\n\nconst DEFAULT_FONT_SIZE = '16';\n\nconst FONT_SIZE_MAP = {\n [HEADING_KEYS.h1]: '36',\n [HEADING_KEYS.h2]: '24',\n [HEADING_KEYS.h3]: '20',\n} as const;\n\nconst FONT_SIZES = [\n '8',\n '9',\n '10',\n '12',\n '14',\n '16',\n '18',\n '24',\n '30',\n '36',\n '48',\n '60',\n '72',\n '96',\n] as const;\n\nexport function FontSizeToolbarButton() {\n const [inputValue, setInputValue] = useState(DEFAULT_FONT_SIZE);\n const [isFocused, setIsFocused] = useState(false);\n const { api, editor } = useEditorPlugin(BaseFontSizePlugin);\n\n const cursorFontSize = useEditorSelector((editor) => {\n const marks = getMarks(editor);\n const fontSize = marks?.[FontSizePlugin.key];\n\n if (fontSize) {\n return toUnitLess(fontSize as string);\n }\n\n const [node] =\n getAboveNode<TElement>(editor, {\n at: editor.selection?.focus,\n }) || [];\n\n return node?.type && node.type in FONT_SIZE_MAP\n ? FONT_SIZE_MAP[node.type as keyof typeof FONT_SIZE_MAP]\n : DEFAULT_FONT_SIZE;\n }, []);\n\n const handleInputChange = () => {\n const newSize = toUnitLess(inputValue);\n\n if (Number.parseInt(newSize) < 1 || Number.parseInt(newSize) > 100) {\n focusEditor(editor);\n\n return;\n }\n if (newSize !== toUnitLess(cursorFontSize)) {\n api.fontSize.setMark(`${newSize}px`);\n }\n\n focusEditor(editor);\n };\n\n const handleFontSizeChange = (delta: number) => {\n const newSize = Number(displayValue) + delta;\n api.fontSize.setMark(`${newSize}px`);\n focusEditor(editor);\n };\n\n const displayValue = isFocused ? inputValue : cursorFontSize;\n\n return (\n <div className=\"flex h-7 items-center gap-1 rounded-md bg-muted/60 p-0 \">\n <ToolbarButton onClick={() => handleFontSizeChange(-1)}>\n <Minus />\n </ToolbarButton>\n\n <Popover open={isFocused} modal={false}>\n <PopoverTrigger asChild>\n <input\n className={cn(\n 'h-full w-10 shrink-0 bg-transparent px-1 text-center text-sm hover:bg-muted'\n )}\n value={displayValue}\n onBlur={() => {\n setIsFocused(false);\n handleInputChange();\n }}\n onChange={(e) => setInputValue(e.target.value)}\n onFocus={() => {\n setIsFocused(true);\n setInputValue(toUnitLess(cursorFontSize));\n }}\n onKeyDown={(e) => {\n if (e.key === 'Enter') {\n e.preventDefault();\n handleInputChange();\n }\n }}\n data-plate-focus=\"true\"\n type=\"text\"\n />\n </PopoverTrigger>\n <PopoverContent\n className=\"w-10 px-px py-1\"\n onOpenAutoFocus={(e) => e.preventDefault()}\n >\n {FONT_SIZES.map((size) => (\n <button\n key={size}\n className={cn(\n 'flex h-8 w-full items-center justify-center text-sm hover:bg-accent data-[highlighted=true]:bg-accent'\n )}\n onClick={() => {\n api.fontSize.setMark(`${size}px`);\n setIsFocused(false);\n }}\n data-highlighted={size === displayValue}\n type=\"button\"\n >\n {size}\n </button>\n ))}\n </PopoverContent>\n </Popover>\n\n <ToolbarButton onClick={() => handleFontSizeChange(1)}>\n <Plus />\n </ToolbarButton>\n </div>\n );\n}\n", | ||
"path": "plate-ui/font-size-toolbar-button.tsx", | ||
"target": "components/plate-ui/font-size-toolbar-button.tsx", | ||
"type": "registry:ui" | ||
} | ||
], | ||
"name": "font-size-toolbar-button", | ||
"registryDependencies": [ | ||
"toolbar" | ||
], | ||
"type": "registry:ui" | ||
} |
Oops, something went wrong.