2023-06-12 13:15:23 +02:00
|
|
|
import FocusTrap from 'focus-trap-react';
|
|
|
|
import {
|
|
|
|
Badge,
|
|
|
|
Box,
|
|
|
|
config,
|
|
|
|
Icon,
|
|
|
|
IconButton,
|
|
|
|
Icons,
|
|
|
|
IconSrc,
|
|
|
|
Line,
|
|
|
|
Menu,
|
|
|
|
PopOut,
|
2023-06-16 03:09:09 +02:00
|
|
|
Scroll,
|
2023-06-12 13:15:23 +02:00
|
|
|
Text,
|
|
|
|
Tooltip,
|
|
|
|
TooltipProvider,
|
|
|
|
toRem,
|
|
|
|
} from 'folds';
|
|
|
|
import React, { ReactNode, useState } from 'react';
|
|
|
|
import { ReactEditor, useSlate } from 'slate-react';
|
2023-06-16 03:09:09 +02:00
|
|
|
import {
|
|
|
|
isAnyMarkActive,
|
|
|
|
isBlockActive,
|
|
|
|
isMarkActive,
|
|
|
|
removeAllMark,
|
|
|
|
toggleBlock,
|
|
|
|
toggleMark,
|
|
|
|
} from './common';
|
2023-06-12 13:15:23 +02:00
|
|
|
import * as css from './Editor.css';
|
|
|
|
import { BlockType, MarkType } from './Elements';
|
|
|
|
import { HeadingLevel } from './slate';
|
|
|
|
import { isMacOS } from '../../utils/user-agent';
|
|
|
|
import { KeySymbol } from '../../utils/key-symbol';
|
|
|
|
|
|
|
|
function BtnTooltip({ text, shortCode }: { text: string; shortCode?: string }) {
|
|
|
|
return (
|
|
|
|
<Tooltip style={{ padding: config.space.S300 }}>
|
|
|
|
<Box gap="200" direction="Column" alignItems="Center">
|
|
|
|
<Text align="Center">{text}</Text>
|
|
|
|
{shortCode && (
|
|
|
|
<Badge as="kbd" radii="300" size="500">
|
|
|
|
<Text size="T200" align="Center">
|
|
|
|
{shortCode}
|
|
|
|
</Text>
|
|
|
|
</Badge>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
type MarkButtonProps = { format: MarkType; icon: IconSrc; tooltip: ReactNode };
|
|
|
|
export function MarkButton({ format, icon, tooltip }: MarkButtonProps) {
|
|
|
|
const editor = useSlate();
|
2023-06-16 03:09:09 +02:00
|
|
|
const disableInline = isBlockActive(editor, BlockType.CodeBlock);
|
|
|
|
|
|
|
|
if (disableInline) {
|
|
|
|
removeAllMark(editor);
|
|
|
|
}
|
2023-06-12 13:15:23 +02:00
|
|
|
|
|
|
|
const handleClick = () => {
|
|
|
|
toggleMark(editor, format);
|
|
|
|
ReactEditor.focus(editor);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TooltipProvider tooltip={tooltip} delay={500}>
|
|
|
|
{(triggerRef) => (
|
|
|
|
<IconButton
|
|
|
|
ref={triggerRef}
|
|
|
|
variant="SurfaceVariant"
|
|
|
|
onClick={handleClick}
|
|
|
|
aria-pressed={isMarkActive(editor, format)}
|
2023-06-16 03:09:09 +02:00
|
|
|
size="400"
|
2023-06-12 13:15:23 +02:00
|
|
|
radii="300"
|
2023-06-16 03:09:09 +02:00
|
|
|
disabled={disableInline}
|
2023-06-12 13:15:23 +02:00
|
|
|
>
|
2023-06-16 03:09:09 +02:00
|
|
|
<Icon size="200" src={icon} />
|
2023-06-12 13:15:23 +02:00
|
|
|
</IconButton>
|
|
|
|
)}
|
|
|
|
</TooltipProvider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockButtonProps = {
|
|
|
|
format: BlockType;
|
|
|
|
icon: IconSrc;
|
|
|
|
tooltip: ReactNode;
|
|
|
|
};
|
|
|
|
export function BlockButton({ format, icon, tooltip }: BlockButtonProps) {
|
|
|
|
const editor = useSlate();
|
|
|
|
|
|
|
|
const handleClick = () => {
|
|
|
|
toggleBlock(editor, format, { level: 1 });
|
|
|
|
ReactEditor.focus(editor);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TooltipProvider tooltip={tooltip} delay={500}>
|
|
|
|
{(triggerRef) => (
|
|
|
|
<IconButton
|
|
|
|
ref={triggerRef}
|
|
|
|
variant="SurfaceVariant"
|
|
|
|
onClick={handleClick}
|
|
|
|
aria-pressed={isBlockActive(editor, format)}
|
2023-06-16 03:09:09 +02:00
|
|
|
size="400"
|
2023-06-12 13:15:23 +02:00
|
|
|
radii="300"
|
|
|
|
>
|
2023-06-16 03:09:09 +02:00
|
|
|
<Icon size="200" src={icon} />
|
2023-06-12 13:15:23 +02:00
|
|
|
</IconButton>
|
|
|
|
)}
|
|
|
|
</TooltipProvider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function HeadingBlockButton() {
|
|
|
|
const editor = useSlate();
|
|
|
|
const [level, setLevel] = useState<HeadingLevel>(1);
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const isActive = isBlockActive(editor, BlockType.Heading);
|
|
|
|
|
|
|
|
const handleMenuSelect = (selectedLevel: HeadingLevel) => {
|
|
|
|
setOpen(false);
|
|
|
|
setLevel(selectedLevel);
|
|
|
|
toggleBlock(editor, BlockType.Heading, { level: selectedLevel });
|
|
|
|
ReactEditor.focus(editor);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PopOut
|
|
|
|
open={open}
|
2023-06-16 03:09:09 +02:00
|
|
|
offset={5}
|
2023-06-12 13:15:23 +02:00
|
|
|
align="Start"
|
|
|
|
position="Top"
|
|
|
|
content={
|
|
|
|
<FocusTrap
|
|
|
|
focusTrapOptions={{
|
|
|
|
initialFocus: false,
|
|
|
|
onDeactivate: () => setOpen(false),
|
|
|
|
clickOutsideDeactivates: true,
|
|
|
|
isKeyForward: (evt: KeyboardEvent) =>
|
|
|
|
evt.key === 'ArrowDown' || evt.key === 'ArrowRight',
|
|
|
|
isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp' || evt.key === 'ArrowLeft',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Menu style={{ padding: config.space.S100 }}>
|
|
|
|
<Box gap="100">
|
2023-06-16 03:09:09 +02:00
|
|
|
<IconButton onClick={() => handleMenuSelect(1)} size="400" radii="300">
|
|
|
|
<Icon size="200" src={Icons.Heading1} />
|
2023-06-12 13:15:23 +02:00
|
|
|
</IconButton>
|
2023-06-16 03:09:09 +02:00
|
|
|
<IconButton onClick={() => handleMenuSelect(2)} size="400" radii="300">
|
|
|
|
<Icon size="200" src={Icons.Heading2} />
|
2023-06-12 13:15:23 +02:00
|
|
|
</IconButton>
|
2023-06-16 03:09:09 +02:00
|
|
|
<IconButton onClick={() => handleMenuSelect(3)} size="400" radii="300">
|
|
|
|
<Icon size="200" src={Icons.Heading3} />
|
2023-06-12 13:15:23 +02:00
|
|
|
</IconButton>
|
|
|
|
</Box>
|
|
|
|
</Menu>
|
|
|
|
</FocusTrap>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{(ref) => (
|
|
|
|
<IconButton
|
|
|
|
style={{ width: 'unset' }}
|
|
|
|
ref={ref}
|
|
|
|
variant="SurfaceVariant"
|
|
|
|
onClick={() => (isActive ? toggleBlock(editor, BlockType.Heading) : setOpen(!open))}
|
|
|
|
aria-pressed={isActive}
|
2023-06-16 03:09:09 +02:00
|
|
|
size="400"
|
2023-06-12 13:15:23 +02:00
|
|
|
radii="300"
|
|
|
|
>
|
2023-06-16 03:09:09 +02:00
|
|
|
<Icon size="200" src={Icons[`Heading${level}`]} />
|
|
|
|
<Icon size="200" src={isActive ? Icons.Cross : Icons.ChevronBottom} />
|
2023-06-12 13:15:23 +02:00
|
|
|
</IconButton>
|
|
|
|
)}
|
|
|
|
</PopOut>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-16 03:09:09 +02:00
|
|
|
type ExitFormattingProps = { tooltip: ReactNode };
|
|
|
|
export function ExitFormatting({ tooltip }: ExitFormattingProps) {
|
|
|
|
const editor = useSlate();
|
|
|
|
|
|
|
|
const handleClick = () => {
|
|
|
|
if (isAnyMarkActive(editor)) {
|
|
|
|
removeAllMark(editor);
|
|
|
|
} else if (!isBlockActive(editor, BlockType.Paragraph)) {
|
|
|
|
toggleBlock(editor, BlockType.Paragraph);
|
|
|
|
}
|
|
|
|
ReactEditor.focus(editor);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TooltipProvider tooltip={tooltip} delay={500}>
|
|
|
|
{(triggerRef) => (
|
|
|
|
<IconButton
|
|
|
|
ref={triggerRef}
|
|
|
|
variant="SurfaceVariant"
|
|
|
|
onClick={handleClick}
|
|
|
|
size="400"
|
|
|
|
radii="300"
|
|
|
|
>
|
|
|
|
<Text size="B400">{`Exit ${KeySymbol.Hyper}`}</Text>
|
|
|
|
</IconButton>
|
|
|
|
)}
|
|
|
|
</TooltipProvider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-12 13:15:23 +02:00
|
|
|
export function Toolbar() {
|
|
|
|
const editor = useSlate();
|
|
|
|
const modKey = isMacOS() ? KeySymbol.Command : 'Ctrl';
|
|
|
|
|
2023-06-16 03:09:09 +02:00
|
|
|
const canEscape = isAnyMarkActive(editor) || !isBlockActive(editor, BlockType.Paragraph);
|
|
|
|
|
2023-06-12 13:15:23 +02:00
|
|
|
return (
|
2023-06-16 03:09:09 +02:00
|
|
|
<Box className={css.EditorToolbarBase}>
|
|
|
|
<Scroll direction="Horizontal" size="0">
|
|
|
|
<Box className={css.EditorToolbar} alignItems="Center" gap="300">
|
|
|
|
<>
|
|
|
|
<Box shrink="No" gap="100">
|
|
|
|
<MarkButton
|
|
|
|
format={MarkType.Bold}
|
|
|
|
icon={Icons.Bold}
|
|
|
|
tooltip={<BtnTooltip text="Bold" shortCode={`${modKey} + B`} />}
|
|
|
|
/>
|
|
|
|
<MarkButton
|
|
|
|
format={MarkType.Italic}
|
|
|
|
icon={Icons.Italic}
|
|
|
|
tooltip={<BtnTooltip text="Italic" shortCode={`${modKey} + I`} />}
|
|
|
|
/>
|
|
|
|
<MarkButton
|
|
|
|
format={MarkType.Underline}
|
|
|
|
icon={Icons.Underline}
|
|
|
|
tooltip={<BtnTooltip text="Underline" shortCode={`${modKey} + U`} />}
|
|
|
|
/>
|
|
|
|
<MarkButton
|
|
|
|
format={MarkType.StrikeThrough}
|
|
|
|
icon={Icons.Strike}
|
|
|
|
tooltip={
|
|
|
|
<BtnTooltip
|
|
|
|
text="Strike Through"
|
|
|
|
shortCode={`${modKey} + ${KeySymbol.Shift} + U`}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<MarkButton
|
|
|
|
format={MarkType.Code}
|
|
|
|
icon={Icons.Code}
|
|
|
|
tooltip={<BtnTooltip text="Inline Code" shortCode={`${modKey} + [`} />}
|
|
|
|
/>
|
|
|
|
<MarkButton
|
|
|
|
format={MarkType.Spoiler}
|
|
|
|
icon={Icons.EyeBlind}
|
|
|
|
tooltip={<BtnTooltip text="Spoiler" shortCode={`${modKey} + H`} />}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
<Line variant="SurfaceVariant" direction="Vertical" style={{ height: toRem(12) }} />
|
|
|
|
</>
|
|
|
|
<Box shrink="No" gap="100">
|
|
|
|
<BlockButton
|
|
|
|
format={BlockType.BlockQuote}
|
|
|
|
icon={Icons.BlockQuote}
|
|
|
|
tooltip={
|
|
|
|
<BtnTooltip text="Block Quote" shortCode={`${modKey} + ${KeySymbol.Shift} + '`} />
|
|
|
|
}
|
2023-06-12 13:15:23 +02:00
|
|
|
/>
|
2023-06-16 03:09:09 +02:00
|
|
|
<BlockButton
|
|
|
|
format={BlockType.CodeBlock}
|
|
|
|
icon={Icons.BlockCode}
|
|
|
|
tooltip={
|
|
|
|
<BtnTooltip text="Block Code" shortCode={`${modKey} + ${KeySymbol.Shift} + ;`} />
|
|
|
|
}
|
2023-06-12 13:15:23 +02:00
|
|
|
/>
|
2023-06-16 03:09:09 +02:00
|
|
|
<BlockButton
|
|
|
|
format={BlockType.OrderedList}
|
|
|
|
icon={Icons.OrderList}
|
|
|
|
tooltip={
|
|
|
|
<BtnTooltip text="Ordered List" shortCode={`${modKey} + ${KeySymbol.Shift} + 7`} />
|
|
|
|
}
|
2023-06-12 13:15:23 +02:00
|
|
|
/>
|
2023-06-16 03:09:09 +02:00
|
|
|
<BlockButton
|
|
|
|
format={BlockType.UnorderedList}
|
|
|
|
icon={Icons.UnorderList}
|
2023-06-12 13:15:23 +02:00
|
|
|
tooltip={
|
|
|
|
<BtnTooltip
|
2023-06-16 03:09:09 +02:00
|
|
|
text="Unordered List"
|
|
|
|
shortCode={`${modKey} + ${KeySymbol.Shift} + 8`}
|
2023-06-12 13:15:23 +02:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
2023-06-16 03:09:09 +02:00
|
|
|
<HeadingBlockButton />
|
2023-06-12 13:15:23 +02:00
|
|
|
</Box>
|
2023-06-16 03:09:09 +02:00
|
|
|
{canEscape && (
|
|
|
|
<>
|
|
|
|
<Line variant="SurfaceVariant" direction="Vertical" style={{ height: toRem(12) }} />
|
|
|
|
<Box shrink="No" gap="100">
|
|
|
|
<ExitFormatting
|
|
|
|
tooltip={<BtnTooltip text="Exit Formatting" shortCode={`${modKey} + E`} />}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</Scroll>
|
2023-06-12 13:15:23 +02:00
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|