Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zbeyens committed Dec 28, 2024
1 parent 76c5701 commit d95cb13
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@udecode/plate-emoji": "41.0.0",
"@udecode/plate-excalidraw": "41.0.0",
"@udecode/plate-floating": "41.0.0",
"@udecode/plate-font": "41.0.0",
"@udecode/plate-font": "41.0.12",
"@udecode/plate-heading": "41.0.0",
"@udecode/plate-highlight": "41.0.0",
"@udecode/plate-horizontal-rule": "41.0.0",
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/components/plate-ui/fixed-toolbar-buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { ColorDropdownMenu } from './color-dropdown-menu';
import { CommentToolbarButton } from './comment-toolbar-button';
import { EmojiDropdownMenu } from './emoji-dropdown-menu';
import { ExportToolbarButton } from './export-toolbar-button';
import { FontSizeToolbarButton } from './font-size-toolbar-button';
import { RedoToolbarButton, UndoToolbarButton } from './history-toolbar-button';
import {
BulletedIndentListToolbarButton,
Expand Down Expand Up @@ -88,6 +89,7 @@ export function FixedToolbarButtons() {
<ToolbarGroup>
<InsertDropdownMenu />
<TurnIntoDropdownMenu />
<FontSizeToolbarButton />
</ToolbarGroup>

<ToolbarGroup>
Expand Down
150 changes: 150 additions & 0 deletions src/components/plate-ui/font-size-toolbar-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
'use client';
import { useState } from 'react';

import { cn } from '@udecode/cn';
import { type TElement, getAboveNode, getMarks } from '@udecode/plate-common';
import {
focusEditor,
useEditorPlugin,
useEditorSelector,
} from '@udecode/plate-common/react';
import { BaseFontSizePlugin, toUnitLess } from '@udecode/plate-font';
import { FontSizePlugin } from '@udecode/plate-font/react';
import { HEADING_KEYS } from '@udecode/plate-heading';
import { Minus, Plus } from 'lucide-react';

import { Popover, PopoverContent, PopoverTrigger } from './popover';
import { ToolbarButton } from './toolbar';

const DEFAULT_FONT_SIZE = '16';

const FONT_SIZE_MAP = {
[HEADING_KEYS.h1]: '36',
[HEADING_KEYS.h2]: '24',
[HEADING_KEYS.h3]: '20',
} as const;

const FONT_SIZES = [
'8',
'9',
'10',
'12',
'14',
'16',
'18',
'24',
'30',
'36',
'48',
'60',
'72',
'96',
] as const;

export function FontSizeToolbarButton() {
const [inputValue, setInputValue] = useState(DEFAULT_FONT_SIZE);
const [isFocused, setIsFocused] = useState(false);
const { api, editor } = useEditorPlugin(BaseFontSizePlugin);

const cursorFontSize = useEditorSelector((editor) => {
const marks = getMarks(editor);
const fontSize = marks?.[FontSizePlugin.key];

if (fontSize) {
return toUnitLess(fontSize as string);
}

const [node] =
getAboveNode<TElement>(editor, {
at: editor.selection?.focus,
}) || [];

return node?.type && node.type in FONT_SIZE_MAP
? FONT_SIZE_MAP[node.type as keyof typeof FONT_SIZE_MAP]
: DEFAULT_FONT_SIZE;
}, []);

const handleInputChange = () => {
const newSize = toUnitLess(inputValue);

if (Number.parseInt(newSize) < 1 || Number.parseInt(newSize) > 100) {
focusEditor(editor);

return;
}
if (newSize !== toUnitLess(cursorFontSize)) {
api.fontSize.setMark(`${newSize}px`);
}

focusEditor(editor);
};

const handleFontSizeChange = (delta: number) => {
const newSize = Number(displayValue) + delta;
api.fontSize.setMark(`${newSize}px`);
focusEditor(editor);
};

const displayValue = isFocused ? inputValue : cursorFontSize;

return (
<div className="flex h-7 items-center gap-1 rounded-md bg-muted/60 p-0 ">
<ToolbarButton onClick={() => handleFontSizeChange(-1)}>
<Minus />
</ToolbarButton>

<Popover open={isFocused} modal={false}>
<PopoverTrigger asChild>
<input
className={cn(
'h-full w-10 shrink-0 bg-transparent px-1 text-center text-sm hover:bg-muted'
)}
value={displayValue}
onBlur={() => {
setIsFocused(false);
handleInputChange();
}}
onChange={(e) => setInputValue(e.target.value)}
onFocus={() => {
setIsFocused(true);
setInputValue(toUnitLess(cursorFontSize));
}}
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault();
handleInputChange();
}
}}
data-plate-focus="true"
type="text"
/>
</PopoverTrigger>
<PopoverContent
className="w-10 px-px py-1"
onOpenAutoFocus={(e) => e.preventDefault()}
>
{FONT_SIZES.map((size) => (
<button
key={size}
className={cn(
'flex h-8 w-full items-center justify-center text-sm hover:bg-accent data-[highlighted=true]:bg-accent'
)}
onClick={() => {
api.fontSize.setMark(`${size}px`);
setIsFocused(false);
}}
data-highlighted={size === displayValue}
type="button"
>
{size}
</button>
))}
</PopoverContent>
</Popover>

<ToolbarButton onClick={() => handleFontSizeChange(1)}>
<Plus />
</ToolbarButton>
</div>
);
}
12 changes: 7 additions & 5 deletions src/components/plate-ui/indent-list-toolbar-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ export function NumberedIndentListToolbarButton() {
);

return (
<ToolbarSplitButton pressed={openState.open} tooltip="Numbered List">
<ToolbarSplitButton pressed={openState.open}>
<ToolbarSplitButtonPrimary
className="data-[state=on]:bg-accent data-[state=on]:text-accent-foreground"
onClick={() => {
onClick={() =>
toggleIndentList(editor, {
listStyleType: ListStyleType.Decimal,
});
}}
})
}
data-state={pressed ? 'on' : 'off'}
tooltip="Numbered List"
>
<ListOrdered className="size-4" />
</ToolbarSplitButtonPrimary>
Expand Down Expand Up @@ -128,7 +129,7 @@ export function BulletedIndentListToolbarButton() {
);

return (
<ToolbarSplitButton pressed={openState.open} tooltip="Bulleted List">
<ToolbarSplitButton pressed={openState.open}>
<ToolbarSplitButtonPrimary
className="data-[state=on]:bg-accent data-[state=on]:text-accent-foreground"
onClick={() => {
Expand All @@ -137,6 +138,7 @@ export function BulletedIndentListToolbarButton() {
});
}}
data-state={pressed ? 'on' : 'off'}
tooltip="Bulleted List"
>
<List className="size-4" />
</ToolbarSplitButtonPrimary>
Expand Down
29 changes: 17 additions & 12 deletions src/components/plate-ui/insert-dropdown-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import { TocPlugin } from '@udecode/plate-heading/react';
import { HorizontalRulePlugin } from '@udecode/plate-horizontal-rule/react';
import { INDENT_LIST_KEYS, ListStyleType } from '@udecode/plate-indent-list';
import { LinkPlugin } from '@udecode/plate-link/react';
import {
EquationPlugin,
InlineEquationPlugin,
} from '@udecode/plate-math/react';
import { ImagePlugin, MediaEmbedPlugin } from '@udecode/plate-media/react';
import { TablePlugin } from '@udecode/plate-table/react';
import { TogglePlugin } from '@udecode/plate-toggle/react';
Expand All @@ -40,6 +44,7 @@ import {
PilcrowIcon,
PlusIcon,
QuoteIcon,
RadicalIcon,
SquareIcon,
TableIcon,
TableOfContentsIcon,
Expand Down Expand Up @@ -192,12 +197,12 @@ const groups: Group[] = [
label: '3 columns',
value: 'action_three_columns',
},
// {
// focusEditor: false,
// icon: <RadicalIcon />,
// label: 'Equation',
// value: EquationPlugin.key,
// },
{
focusEditor: false,
icon: <RadicalIcon />,
label: 'Equation',
value: EquationPlugin.key,
},
].map((item) => ({
...item,
onSelect: (editor, value) => {
Expand All @@ -219,12 +224,12 @@ const groups: Group[] = [
label: 'Date',
value: DatePlugin.key,
},
// {
// focusEditor: false,
// icon: <RadicalIcon />,
// label: 'Inline Equation',
// value: InlineEquationPlugin.key,
// },
{
focusEditor: false,
icon: <RadicalIcon />,
label: 'Inline Equation',
value: InlineEquationPlugin.key,
},
].map((item) => ({
...item,
onSelect: (editor, value) => {
Expand Down
3 changes: 1 addition & 2 deletions src/components/plate-ui/media-toolbar-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ export function MediaToolbarButton({
}
}}
pressed={openState.open}
tooltip={currentConfig.tooltip}
>
<ToolbarSplitButtonPrimary>
<ToolbarSplitButtonPrimary tooltip={currentConfig.tooltip}>
{currentConfig.icon}
</ToolbarSplitButtonPrimary>

Expand Down
46 changes: 24 additions & 22 deletions src/components/plate-ui/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,30 @@ export const ToolbarSplitButton = React.forwardRef<
);
});

export const ToolbarSplitButtonPrimary = React.forwardRef<
React.ElementRef<typeof ToolbarToggleItem>,
Omit<React.ComponentPropsWithoutRef<typeof ToolbarToggleItem>, 'value'>
>(({ children, className, size, variant, ...props }, ref) => {
return (
<span
ref={ref}
className={cn(
toolbarButtonVariants({
size,
variant,
}),
'rounded-r-none',
'group-data-[pressed=true]:bg-accent group-data-[pressed=true]:text-accent-foreground',
className
)}
{...props}
>
{children}
</span>
);
});
export const ToolbarSplitButtonPrimary = withTooltip(
React.forwardRef<
React.ElementRef<typeof ToolbarToggleItem>,
Omit<React.ComponentPropsWithoutRef<typeof ToolbarToggleItem>, 'value'>
>(({ children, className, size, variant, ...props }, ref) => {
return (
<span
ref={ref}
className={cn(
toolbarButtonVariants({
size,
variant,
}),
'rounded-r-none',
'group-data-[pressed=true]:bg-accent group-data-[pressed=true]:text-accent-foreground',
className
)}
{...props}
>
{children}
</span>
);
})
);

export const ToolbarSplitButtonSecondary = React.forwardRef<
HTMLButtonElement,
Expand Down

0 comments on commit d95cb13

Please sign in to comment.