Skip to content

开源白板工具(SaaS),一体化白板,包含思维导图、流程图、自由画等。All in one open-source whiteboard tool with mind, flowchart, freehand and etc.

feat: add main menu (#14)

pubuzhixing824fc4a3693f6
diff --git a/packages/drawnix/src/components/dropdown-menu/DropdownMenu.tsx b/packages/drawnix/src/components/dropdown-menu/DropdownMenu.tsx
new file mode 100644
index 0000000..5052e0d
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/DropdownMenu.tsx
@@ -0,0 +1,44 @@
+// Credits to excalidraw
+import React from 'react';
+import DropdownMenuTrigger from './DropdownMenuTrigger';
+import DropdownMenuItem from './DropdownMenuItem';
+import MenuSeparator from './DropdownMenuSeparator';
+import DropdownMenuGroup from './DropdownMenuGroup';
+import DropdownMenuContent from './DropdownMenuContent';
+import DropdownMenuItemLink from './DropdownMenuItemLink';
+import DropdownMenuItemCustom from './DropdownMenuItemCustom';
+import {
+ getMenuContentComponent,
+ getMenuTriggerComponent,
+} from './dropdownMenuUtils';
+
+import './dropdown-menu.scss';
+
+const DropdownMenu = ({
+ children,
+ open,
+}: {
+ children?: React.ReactNode;
+ open: boolean;
+}) => {
+ const MenuTriggerComp = getMenuTriggerComponent(children);
+ const MenuContentComp = getMenuContentComponent(children);
+ return (
+ <>
+ {MenuTriggerComp}
+ {open && MenuContentComp}
+ </>
+ );
+};
+
+DropdownMenu.Trigger = DropdownMenuTrigger;
+DropdownMenu.Content = DropdownMenuContent;
+DropdownMenu.Item = DropdownMenuItem;
+DropdownMenu.ItemLink = DropdownMenuItemLink;
+DropdownMenu.ItemCustom = DropdownMenuItemCustom;
+DropdownMenu.Group = DropdownMenuGroup;
+DropdownMenu.Separator = MenuSeparator;
+
+export default DropdownMenu;
+
+DropdownMenu.displayName = 'DropdownMenu';
diff --git a/packages/drawnix/src/components/dropdown-menu/DropdownMenuContent.tsx b/packages/drawnix/src/components/dropdown-menu/DropdownMenuContent.tsx
new file mode 100644
index 0000000..f1c063b
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/DropdownMenuContent.tsx
@@ -0,0 +1,79 @@
+import { Island } from '../island';
+import React, { useEffect, useRef } from 'react';
+import { DropdownMenuContentPropsContext } from './common';
+import { useOutsideClick } from '../../hooks/use-outside-click';
+import { EVENT } from '../../constants';
+import { useStable } from '../../hooks/use-stable';
+import classNames from 'classnames';
+import { KEYS } from '../../keys';
+
+const MenuContent = ({
+ children,
+ onClickOutside,
+ className = '',
+ onSelect,
+ style,
+}: {
+ children?: React.ReactNode;
+ onClickOutside?: () => void;
+ className?: string;
+ /**
+ * Called when any menu item is selected (clicked on).
+ */
+ onSelect?: (event: Event) => void;
+ style?: React.CSSProperties;
+}) => {
+ const menuRef = useRef<HTMLDivElement>(null);
+
+ const callbacksRef = useStable({ onClickOutside });
+
+ useOutsideClick(menuRef, () => {
+ callbacksRef.onClickOutside?.();
+ });
+
+ useEffect(() => {
+ const onKeyDown = (event: KeyboardEvent) => {
+ if (event.key === KEYS.ESCAPE) {
+ event.stopImmediatePropagation();
+ callbacksRef.onClickOutside?.();
+ }
+ };
+
+ const option = {
+ // so that we can stop propagation of the event before it reaches
+ // event handlers that were bound before this one
+ capture: true,
+ };
+
+ document.addEventListener(EVENT.KEYDOWN, onKeyDown, option);
+ return () => {
+ document.removeEventListener(EVENT.KEYDOWN, onKeyDown, option);
+ };
+ }, [callbacksRef]);
+
+ const newClassName = classNames(`dropdown-menu ${className}`).trim();
+
+ return (
+ <DropdownMenuContentPropsContext.Provider value={{ onSelect }}>
+ <div
+ ref={menuRef}
+ className={newClassName}
+ style={style}
+ data-testid="dropdown-menu"
+ >
+ {
+ <Island
+ className="dropdown-menu-container"
+ padding={2}
+ style={{ zIndex: 2 }}
+ >
+ {children}
+ </Island>
+ }
+ </div>
+ </DropdownMenuContentPropsContext.Provider>
+ );
+};
+MenuContent.displayName = 'DropdownMenuContent';
+
+export default MenuContent;
diff --git a/packages/drawnix/src/components/dropdown-menu/DropdownMenuGroup.tsx b/packages/drawnix/src/components/dropdown-menu/DropdownMenuGroup.tsx
new file mode 100644
index 0000000..a96061e
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/DropdownMenuGroup.tsx
@@ -0,0 +1,23 @@
+import React from 'react';
+
+const MenuGroup = ({
+ children,
+ className = '',
+ style,
+ title,
+}: {
+ children: React.ReactNode;
+ className?: string;
+ style?: React.CSSProperties;
+ title?: string;
+}) => {
+ return (
+ <div className={`dropdown-menu-group ${className}`} style={style}>
+ {title && <p className="dropdown-menu-group-title">{title}</p>}
+ {children}
+ </div>
+ );
+};
+
+export default MenuGroup;
+MenuGroup.displayName = 'DropdownMenuGroup';
diff --git a/packages/drawnix/src/components/dropdown-menu/DropdownMenuItem.tsx b/packages/drawnix/src/components/dropdown-menu/DropdownMenuItem.tsx
new file mode 100644
index 0000000..6927843
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/DropdownMenuItem.tsx
@@ -0,0 +1,68 @@
+import React from 'react';
+import {
+ getDropdownMenuItemClassName,
+ useHandleDropdownMenuItemClick,
+} from './common';
+import MenuItemContent from './DropdownMenuItemContent';
+
+const DropdownMenuItem = ({
+ icon,
+ onSelect,
+ children,
+ shortcut,
+ className,
+ selected,
+ ...rest
+}: {
+ icon?: React.ReactNode;
+ onSelect: (event: Event) => void;
+ children: React.ReactNode;
+ shortcut?: string;
+ selected?: boolean;
+ className?: string;
+} & Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'onSelect'>) => {
+ const handleClick = useHandleDropdownMenuItemClick(rest.onClick, onSelect);
+
+ return (
+ <button
+ {...rest}
+ onClick={handleClick}
+ type="button"
+ className={getDropdownMenuItemClassName(className, selected)}
+ title={rest.title ?? rest['aria-label']}
+ >
+ <MenuItemContent icon={icon} shortcut={shortcut}>
+ {children}
+ </MenuItemContent>
+ </button>
+ );
+};
+DropdownMenuItem.displayName = 'DropdownMenuItem';
+
+export const DropDownMenuItemBadge = ({
+ children,
+}: {
+ children: React.ReactNode;
+}) => {
+ return (
+ <div
+ style={{
+ display: 'inline-flex',
+ marginLeft: 'auto',
+ padding: '2px 4px',
+ background: 'var(--color-promo)',
+ color: 'var(--color-surface-lowest)',
+ borderRadius: 6,
+ fontSize: 9,
+ fontFamily: 'Cascadia, monospace',
+ }}
+ >
+ {children}
+ </div>
+ );
+};
+DropDownMenuItemBadge.displayName = 'DropdownMenuItemBadge';
+
+DropdownMenuItem.Badge = DropDownMenuItemBadge;
+
+export default DropdownMenuItem;
diff --git a/packages/drawnix/src/components/dropdown-menu/DropdownMenuItemContent.tsx b/packages/drawnix/src/components/dropdown-menu/DropdownMenuItemContent.tsx
new file mode 100644
index 0000000..1c73538
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/DropdownMenuItemContent.tsx
@@ -0,0 +1,22 @@
+import React from 'react';
+
+const MenuItemContent = ({
+ icon,
+ shortcut,
+ children,
+}: {
+ icon?: React.ReactNode;
+ shortcut?: string;
+ children: React.ReactNode;
+}) => {
+ return (
+ <>
+ <div className="dropdown-menu-item__icon">{icon}</div>
+ <div className="dropdown-menu-item__text">{children}</div>
+ {shortcut && (
+ <div className="dropdown-menu-item__shortcut">{shortcut}</div>
+ )}
+ </>
+ );
+};
+export default MenuItemContent;
diff --git a/packages/drawnix/src/components/dropdown-menu/DropdownMenuItemContentRadio.tsx b/packages/drawnix/src/components/dropdown-menu/DropdownMenuItemContentRadio.tsx
new file mode 100644
index 0000000..124933b
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/DropdownMenuItemContentRadio.tsx
@@ -0,0 +1,48 @@
+import { RadioGroup } from '../radio-group';
+
+type Props<T> = {
+ value: T;
+ shortcut?: string;
+ choices: {
+ value: T;
+ label: React.ReactNode;
+ ariaLabel?: string;
+ }[];
+ onChange: (value: T) => void;
+ children: React.ReactNode;
+ name: string;
+};
+
+const DropdownMenuItemContentRadio = <T,>({
+ value,
+ shortcut,
+ onChange,
+ choices,
+ children,
+ name,
+}: Props<T>) => {
+ return (
+ <>
+ <div className="dropdown-menu-item-base dropdown-menu-item-bare">
+ <label className="dropdown-menu-item__text" htmlFor={name}>
+ {children}
+ </label>
+ <RadioGroup
+ name={name}
+ value={value}
+ onChange={onChange}
+ choices={choices}
+ />
+ </div>
+ {shortcut && (
+ <div className="dropdown-menu-item__shortcut dropdown-menu-item__shortcut--orphaned">
+ {shortcut}
+ </div>
+ )}
+ </>
+ );
+};
+
+DropdownMenuItemContentRadio.displayName = 'DropdownMenuItemContentRadio';
+
+export default DropdownMenuItemContentRadio;
diff --git a/packages/drawnix/src/components/dropdown-menu/DropdownMenuItemCustom.tsx b/packages/drawnix/src/components/dropdown-menu/DropdownMenuItemCustom.tsx
new file mode 100644
index 0000000..bcbec11
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/DropdownMenuItemCustom.tsx
@@ -0,0 +1,25 @@
+import React from 'react';
+
+const DropdownMenuItemCustom = ({
+ children,
+ className = '',
+ selected,
+ ...rest
+}: {
+ children: React.ReactNode;
+ className?: string;
+ selected?: boolean;
+} & React.HTMLAttributes<HTMLDivElement>) => {
+ return (
+ <div
+ {...rest}
+ className={`dropdown-menu-item-base dropdown-menu-item-custom ${className} ${
+ selected ? `dropdown-menu-item--selected` : ``
+ }`.trim()}
+ >
+ {children}
+ </div>
+ );
+};
+
+export default DropdownMenuItemCustom;
diff --git a/packages/drawnix/src/components/dropdown-menu/DropdownMenuItemLink.tsx b/packages/drawnix/src/components/dropdown-menu/DropdownMenuItemLink.tsx
new file mode 100644
index 0000000..e944f5d
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/DropdownMenuItemLink.tsx
@@ -0,0 +1,46 @@
+import MenuItemContent from './DropdownMenuItemContent';
+import React from 'react';
+import {
+ getDropdownMenuItemClassName,
+ useHandleDropdownMenuItemClick,
+} from './common';
+
+const DropdownMenuItemLink = ({
+ icon,
+ shortcut,
+ href,
+ children,
+ onSelect,
+ className = '',
+ selected,
+ ...rest
+}: {
+ href: string;
+ icon?: React.ReactNode;
+ children: React.ReactNode;
+ shortcut?: string;
+ className?: string;
+ selected?: boolean;
+ onSelect?: (event: Event) => void;
+} & React.AnchorHTMLAttributes<HTMLAnchorElement>) => {
+ const handleClick = useHandleDropdownMenuItemClick(rest.onClick, onSelect);
+
+ return (
+ <a
+ {...rest}
+ href={href}
+ target="_blank"
+ rel="noreferrer"
+ className={getDropdownMenuItemClassName(className, selected)}
+ title={rest.title ?? rest['aria-label']}
+ onClick={handleClick}
+ >
+ <MenuItemContent icon={icon} shortcut={shortcut}>
+ {children}
+ </MenuItemContent>
+ </a>
+ );
+};
+
+export default DropdownMenuItemLink;
+DropdownMenuItemLink.displayName = 'DropdownMenuItemLink';
diff --git a/packages/drawnix/src/components/dropdown-menu/DropdownMenuSeparator.tsx b/packages/drawnix/src/components/dropdown-menu/DropdownMenuSeparator.tsx
new file mode 100644
index 0000000..788e5ed
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/DropdownMenuSeparator.tsx
@@ -0,0 +1,12 @@
+const MenuSeparator = () => (
+ <div
+ style={{
+ height: '1px',
+ backgroundColor: 'var(--color-gray-10)',
+ margin: '.5rem 0',
+ }}
+ />
+);
+
+export default MenuSeparator;
+MenuSeparator.displayName = 'DropdownMenuSeparator';
diff --git a/packages/drawnix/src/components/dropdown-menu/DropdownMenuTrigger.tsx b/packages/drawnix/src/components/dropdown-menu/DropdownMenuTrigger.tsx
new file mode 100644
index 0000000..1a8423b
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/DropdownMenuTrigger.tsx
@@ -0,0 +1,34 @@
+import classNames from 'classnames';
+
+const MenuTrigger = ({
+ className = '',
+ children,
+ onToggle,
+ title,
+ ...rest
+}: {
+ className?: string;
+ children: React.ReactNode;
+ onToggle: () => void;
+ title?: string;
+} & Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, 'onSelect'>) => {
+ return (
+ <button
+ data-prevent-outside-click
+ className={classNames(
+ `dropdown-menu-button ${className}`,
+ 'zen-mode-transition'
+ ).trim()}
+ onClick={onToggle}
+ type="button"
+ data-testid="dropdown-menu-button"
+ title={title}
+ {...rest}
+ >
+ {children}
+ </button>
+ );
+};
+
+export default MenuTrigger;
+MenuTrigger.displayName = 'DropdownMenuTrigger';
diff --git a/packages/drawnix/src/components/dropdown-menu/common.ts b/packages/drawnix/src/components/dropdown-menu/common.ts
new file mode 100644
index 0000000..c595845
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/common.ts
@@ -0,0 +1,36 @@
+import React, { useContext } from "react";
+import { EVENT } from "../../constants";
+import { composeEventHandlers } from "../../utils";
+
+export const DropdownMenuContentPropsContext = React.createContext<{
+ onSelect?: (event: Event) => void;
+}>({});
+
+export const getDropdownMenuItemClassName = (
+ className = "",
+ selected = false,
+) => {
+ return `dropdown-menu-item dropdown-menu-item-base ${className} ${
+ selected ? "dropdown-menu-item--selected" : ""
+ }`.trim();
+};
+
+export const useHandleDropdownMenuItemClick = (
+ origOnClick:
+ | React.MouseEventHandler<HTMLAnchorElement | HTMLButtonElement>
+ | undefined,
+ onSelect: ((event: Event) => void) | undefined,
+) => {
+ const DropdownMenuContentProps = useContext(DropdownMenuContentPropsContext);
+
+ return composeEventHandlers(origOnClick, (event) => {
+ const itemSelectEvent = new CustomEvent(EVENT.MENU_ITEM_SELECT, {
+ bubbles: true,
+ cancelable: true,
+ });
+ onSelect?.(itemSelectEvent);
+ if (!itemSelectEvent.defaultPrevented) {
+ DropdownMenuContentProps.onSelect?.(itemSelectEvent);
+ }
+ });
+};
diff --git a/packages/drawnix/src/components/dropdown-menu/dropdown-menu.scss b/packages/drawnix/src/components/dropdown-menu/dropdown-menu.scss
new file mode 100644
index 0000000..daa035b
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/dropdown-menu.scss
@@ -0,0 +1,173 @@
+@import "../../styles/variables.module.scss";
+
+.drawnix {
+ .dropdown-menu {
+ position: absolute;
+ top: 100%;
+ margin-top: 0.25rem;
+
+ &--mobile {
+ left: 0;
+ width: 100%;
+ row-gap: 0.75rem;
+
+ .dropdown-menu-container {
+ padding: 8px 8px;
+ box-sizing: border-box;
+ box-shadow: var(--shadow-island);
+ border-radius: var(--border-radius-lg);
+ position: relative;
+ transition: box-shadow 0.5s ease-in-out;
+
+ &.zen-mode {
+ box-shadow: none;
+ }
+ }
+ }
+
+ .dropdown-menu-container {
+ background-color: var(--island-bg-color);
+ max-height: calc(100vh - 150px);
+ overflow-y: auto;
+ --gap: 2;
+ }
+
+ .dropdown-menu-item-base {
+ display: flex;
+ padding: 0 0.625rem;
+ column-gap: 0.625rem;
+ font-size: 0.875rem;
+ color: var(--color-gray-90);
+ width: 100%;
+ box-sizing: border-box;
+ font-weight: normal;
+ font-family: inherit;
+ }
+
+ .dropdown-menu-item {
+ background-color: transparent;
+ border: 1px solid transparent;
+ align-items: center;
+ height: 2rem;
+ cursor: pointer;
+ border-radius: var(--border-radius-md);
+
+ @media screen and (min-width: 1921px) {
+ height: 2.25rem;
+ }
+
+ &--selected {
+ background: var(--color-primary-light);
+ --icon-fill-color: var(--color-primary-darker);
+ }
+
+ &__text {
+ display: flex;
+ align-items: center;
+ width: 100%;
+ text-overflow: ellipsis;
+ overflow: hidden;
+ white-space: nowrap;
+ gap: 0.75rem;
+ }
+
+ &__shortcut {
+ margin-inline-start: auto;
+ opacity: 0.5;
+
+ &--orphaned {
+ text-align: right;
+ font-size: 0.875rem;
+ padding: 0 0.625rem;
+ }
+ }
+
+ &:hover {
+ background-color: var(--color-surface-primary-container);
+ text-decoration: none;
+ }
+
+ &:active {
+ background-color: var(--color-surface-primary-container);
+ border-color: var(--color-brand-active);
+ }
+
+ svg {
+ width: 1rem;
+ height: 1rem;
+ display: block;
+ }
+ }
+
+ .dropdown-menu-item-bare {
+ align-items: center;
+ height: 2rem;
+ justify-content: space-between;
+
+ @media screen and (min-width: 1921px) {
+ height: 2.25rem;
+ }
+
+ svg {
+ width: 1rem;
+ height: 1rem;
+ display: block;
+ }
+ }
+
+ .dropdown-menu-item-custom {
+ margin-top: 0.5rem;
+ }
+
+ .dropdown-menu-group-title {
+ font-size: 14px;
+ text-align: left;
+ margin: 10px 0;
+ font-weight: 500;
+ }
+ }
+
+ .dropdown-menu-button {
+ @include outlineButtonStyles;
+ width: var(--lg-button-size);
+ height: var(--lg-button-size);
+
+ --background: var(--color-surface-mid);
+
+ background-color: var(--background);
+
+ @at-root .drawnix.theme--dark#{&} {
+ --background: var(--color-surface-high);
+ &:hover {
+ --background: #363541;
+ }
+ }
+
+ &:hover {
+ --background: var(--color-surface-primary-container);
+ background-color: var(--background);
+ text-decoration: none;
+ }
+
+ &:active {
+ border-color: var(--color-primary);
+ }
+
+ svg {
+ width: var(--lg-icon-size);
+ height: var(--lg-icon-size);
+ }
+
+ &--mobile {
+ border: none;
+ margin: 0;
+ padding: 0;
+ width: var(--default-button-size);
+ height: var(--default-button-size);
+ }
+ }
+
+ .main-menu-trigger {
+ @include filledButtonOnCanvas;
+ }
+}
diff --git a/packages/drawnix/src/components/dropdown-menu/dropdownMenuUtils.ts b/packages/drawnix/src/components/dropdown-menu/dropdownMenuUtils.ts
new file mode 100644
index 0000000..bd44e2d
--- /dev/null
+++ b/packages/drawnix/src/components/dropdown-menu/dropdownMenuUtils.ts
@@ -0,0 +1,35 @@
+import React from 'react';
+
+export const getMenuTriggerComponent = (children: React.ReactNode) => {
+ const comp = React.Children.toArray(children).find(
+ (child) =>
+ React.isValidElement(child) &&
+ typeof child.type !== 'string' &&
+ //@ts-ignore
+ child?.type.displayName &&
+ //@ts-ignore
+ child.type.displayName === 'DropdownMenuTrigger'
+ );
+ if (!comp) {
+ return null;
+ }
+ //@ts-ignore
+ return comp;
+};
+
+export const getMenuContentComponent = (children: React.ReactNode) => {
+ const comp = React.Children.toArray(children).find(
+ (child) =>
+ React.isValidElement(child) &&
+ typeof child.type !== 'string' &&
+ //@ts-ignore
+ child?.type.displayName &&
+ //@ts-ignore
+ child.type.displayName === 'DropdownMenuContent'
+ );
+ if (!comp) {
+ return null;
+ }
+ //@ts-ignore
+ return comp;
+};
diff --git a/packages/drawnix/src/components/icons.tsx b/packages/drawnix/src/components/icons.tsx
index e1dc889..32bf16b 100644
--- a/packages/drawnix/src/components/icons.tsx
+++ b/packages/drawnix/src/components/icons.tsx
@@ -134,3 +134,55 @@ export const CurveArrowIcon = createIcon(
</g>
</svg>
);
+
+export const MenuIcon = createIcon(
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ strokeWidth="2"
+ >
+ <g strokeWidth="1.5">
+ <path stroke="none" d="M0 0h24v24H0z"></path>
+ <line x1="4" y1="6" x2="20" y2="6"></line>
+ <line x1="4" y1="12" x2="20" y2="12"></line>
+ <line x1="4" y1="18" x2="20" y2="18"></line>
+ </g>
+ </svg>
+);
+
+export const GithubIcon = createIcon(
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
+ <path
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ fill="none"
+ d="M7.5 15.833c-3.583 1.167-3.583-2.083-5-2.5m10 4.167v-2.917c0-.833.083-1.166-.417-1.666 2.334-.25 4.584-1.167 4.584-5a3.833 3.833 0 0 0-1.084-2.667 3.5 3.5 0 0 0-.083-2.667s-.917-.25-2.917 1.084a10.25 10.25 0 0 0-5.166 0C5.417 2.333 4.5 2.583 4.5 2.583a3.5 3.5 0 0 0-.083 2.667 3.833 3.833 0 0 0-1.084 2.667c0 3.833 2.25 4.75 4.584 5-.5.5-.5 1-.417 1.666V17.5"
+ strokeWidth="1.25"
+ ></path>
+ </svg>
+);
+
+export const ExportImageIcon = createIcon(
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
+ <g
+ strokeWidth="1.25"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ fill="none"
+ >
+ <path stroke="none" d="M0 0h24v24H0z"></path>
+ <path d="M15 8h.01"></path>
+ <path d="M12 20h-5a3 3 0 0 1 -3 -3v-10a3 3 0 0 1 3 -3h10a3 3 0 0 1 3 3v5"></path>
+ <path d="M4 15l4 -4c.928 -.893 2.072 -.893 3 0l4 4"></path>
+ <path d="M14 14l1 -1c.617 -.593 1.328 -.793 2.009 -.598"></path>
+ <path d="M19 16v6"></path>
+ <path d="M22 19l-3 3l-3 -3"></path>
+ </g>
+ </svg>
+);
diff --git a/packages/drawnix/src/components/main-menu/DefaultItems.scss b/packages/drawnix/src/components/main-menu/DefaultItems.scss
new file mode 100644
index 0000000..f2ff66b
--- /dev/null
+++ b/packages/drawnix/src/components/main-menu/DefaultItems.scss
@@ -0,0 +1,21 @@
+.drawnix {
+ .ActiveFile {
+ .ActiveFile__fileName {
+ display: flex;
+ align-items: center;
+
+ span {
+ text-overflow: ellipsis;
+ overflow: hidden;
+ white-space: nowrap;
+ width: 9.3em;
+ }
+
+ svg {
+ width: 1.15em;
+ margin-inline-end: 0.3em;
+ transform: scaleY(0.9);
+ }
+ }
+ }
+}
diff --git a/packages/drawnix/src/components/main-menu/DefaultItems.tsx b/packages/drawnix/src/components/main-menu/DefaultItems.tsx
new file mode 100644
index 0000000..e8c7c6b
--- /dev/null
+++ b/packages/drawnix/src/components/main-menu/DefaultItems.tsx
@@ -0,0 +1,51 @@
+import { ExportImageIcon, GithubIcon } from '../icons';
+import DropdownMenuItem from '../dropdown-menu/DropdownMenuItem';
+import DropdownMenuItemLink from '../dropdown-menu/DropdownMenuItemLink';
+
+import './DefaultItems.scss';
+import { useBoard } from '@plait/react-board';
+import { getSelectedElements } from '@plait/core';
+import { base64ToBlob, boardToImage, download } from '../../utils';
+
+export const SaveAsImage = () => {
+ const board = useBoard();
+
+ return (
+ <DropdownMenuItem
+ icon={ExportImageIcon}
+ data-testid="image-export-button"
+ onSelect={() => {
+ const selectedElements = getSelectedElements(board);
+ boardToImage(board, {
+ elements: selectedElements.length > 0 ? selectedElements : undefined,
+ }).then((image) => {
+ if (image) {
+ const pngImage = base64ToBlob(image);
+ const imageName = `drawnix-${new Date().getTime()}.png`;
+ download(pngImage, imageName);
+ }
+ });
+ }}
+ shortcut={`Cmd+Shift+E`}
+ aria-label={''}
+ >
+ {'导出图片'}
+ </DropdownMenuItem>
+ );
+};
+SaveAsImage.displayName = 'SaveAsImage';
+
+export const Socials = () => {
+ return (
+ <>
+ <DropdownMenuItemLink
+ icon={GithubIcon}
+ href="https://github.com/plait-board/drawnix"
+ aria-label="GitHub"
+ >
+ GitHub
+ </DropdownMenuItemLink>
+ </>
+ );
+};
+Socials.displayName = 'Socials';
diff --git a/packages/drawnix/src/components/main-menu/main-menu.tsx b/packages/drawnix/src/components/main-menu/main-menu.tsx
new file mode 100644
index 0000000..4691646
--- /dev/null
+++ b/packages/drawnix/src/components/main-menu/main-menu.tsx
@@ -0,0 +1,51 @@
+import React, { useState } from 'react';
+import classNames from 'classnames';
+import { Island } from '../island';
+import { useBoard } from '@plait/react-board';
+import { PlaitBoard } from '@plait/core';
+import DropdownMenu from '../dropdown-menu/DropdownMenu';
+import { MenuIcon } from '../icons';
+import { composeEventHandlers } from '../../utils';
+import { SaveAsImage, Socials } from './DefaultItems';
+
+type MainMenuProps = {
+ children?: React.ReactNode;
+ /**
+ * Called when any menu item is selected (clicked on).
+ */
+ onSelect?: (event: Event) => void;
+};
+
+export const MainMenu: React.FC<MainMenuProps> = ({ onSelect, children }) => {
+ const board = useBoard();
+ const [mainMenuOpen, setMainMenuOpen] = useState(false);
+ const container = PlaitBoard.getBoardContainer(board);
+
+ return (
+ <div className={classNames('main-menu')}>
+ <DropdownMenu open={mainMenuOpen}>
+ <DropdownMenu.Trigger
+ onToggle={() => {
+ setMainMenuOpen(!mainMenuOpen);
+ }}
+ data-testid="main-menu-trigger"
+ className="main-menu-trigger"
+ >
+ {MenuIcon}
+ </DropdownMenu.Trigger>
+ <DropdownMenu.Content
+ onClickOutside={() => {
+ setMainMenuOpen(false);
+ }}
+ onSelect={composeEventHandlers(onSelect, () => {
+ setMainMenuOpen(false);
+ })}
+ >
+ <SaveAsImage></SaveAsImage>
+ <DropdownMenu.Separator />
+ <Socials />
+ </DropdownMenu.Content>
+ </DropdownMenu>
+ </div>
+ );
+};
diff --git a/packages/drawnix/src/components/radio-group.scss b/packages/drawnix/src/components/radio-group.scss
new file mode 100644
index 0000000..b939af4
--- /dev/null
+++ b/packages/drawnix/src/components/radio-group.scss
@@ -0,0 +1,91 @@
+@import '../styles/variables.module.scss';
+
+.drawnix {
+ --RadioGroup-background: var(--island-bg-color);
+ --RadioGroup-border: var(--color-surface-high);
+
+ --RadioGroup-choice-color-off: var(--color-primary);
+ --RadioGroup-choice-color-off-hover: var(--color-brand-hover);
+ --RadioGroup-choice-background-off: var(--island-bg-color);
+ --RadioGroup-choice-background-off-active: var(--color-surface-high);
+
+ --RadioGroup-choice-color-on: var(--color-surface-lowest);
+ --RadioGroup-choice-background-on: var(--color-primary);
+ --RadioGroup-choice-background-on-hover: var(--color-brand-hover);
+ --RadioGroup-choice-background-on-active: var(--color-brand-active);
+
+ .RadioGroup {
+ box-sizing: border-box;
+ display: flex;
+ flex-direction: row;
+ align-items: flex-start;
+
+ padding: 3px;
+ border-radius: 10px;
+
+ background: var(--RadioGroup-background);
+ border: 1px solid var(--RadioGroup-border);
+
+ &__choice {
+ position: relative;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ width: 32px;
+ height: 24px;
+
+ color: var(--RadioGroup-choice-color-off);
+ background: var(--RadioGroup-choice-background-off);
+
+ border-radius: 8px;
+
+ font-family: "Assistant";
+ font-style: normal;
+ font-weight: 600;
+ font-size: 0.75rem;
+ line-height: 100%;
+ user-select: none;
+ letter-spacing: 0.4px;
+
+ transition: all 75ms ease-out;
+
+ &:hover {
+ color: var(--RadioGroup-choice-color-off-hover);
+ }
+
+ &:active {
+ background: var(--RadioGroup-choice-background-off-active);
+ }
+
+ &.active {
+ color: var(--RadioGroup-choice-color-on);
+ background: var(--RadioGroup-choice-background-on);
+
+ &:hover {
+ background: var(--RadioGroup-choice-background-on-hover);
+ }
+
+ &:active {
+ background: var(--RadioGroup-choice-background-on-active);
+ }
+ }
+
+ & input {
+ z-index: 1;
+ position: absolute;
+ width: 100%;
+ height: 100%;
+ margin: 0;
+ padding: 0;
+
+ border-radius: 8px;
+
+ -webkit-appearance: none;
+ -moz-appearance: none;
+ appearance: none;
+
+ cursor: pointer;
+ }
+ }
+ }
+}
diff --git a/packages/drawnix/src/components/radio-group.tsx b/packages/drawnix/src/components/radio-group.tsx
new file mode 100644
index 0000000..55e94e5
--- /dev/null
+++ b/packages/drawnix/src/components/radio-group.tsx
@@ -0,0 +1,45 @@
+import classNames from 'classnames';
+import './radio-group.scss';
+
+export type RadioGroupChoice<T> = {
+ value: T;
+ label: React.ReactNode;
+ ariaLabel?: string;
+};
+
+export type RadioGroupProps<T> = {
+ choices: RadioGroupChoice<T>[];
+ value: T;
+ onChange: (value: T) => void;
+ name: string;
+};
+
+export const RadioGroup = function <T>({
+ onChange,
+ value,
+ choices,
+ name,
+}: RadioGroupProps<T>) {
+ return (
+ <div className="RadioGroup">
+ {choices.map((choice) => (
+ <div
+ className={classNames('RadioGroup__choice', {
+ active: choice.value === value,
+ })}
+ key={String(choice.value)}
+ title={choice.ariaLabel}
+ >
+ <input
+ name={name}
+ type="radio"
+ checked={choice.value === value}
+ onChange={() => onChange(choice.value)}
+ aria-label={choice.ariaLabel}
+ />
+ {choice.label}
+ </div>
+ ))}
+ </div>
+ );
+};
diff --git a/packages/drawnix/src/components/toolbar/draw-toolbar.tsx b/packages/drawnix/src/components/toolbar/draw-toolbar.tsx
index 41b411a..942e0eb 100644
--- a/packages/drawnix/src/components/toolbar/draw-toolbar.tsx
+++ b/packages/drawnix/src/components/toolbar/draw-toolbar.tsx
@@ -132,7 +132,6 @@ export const DrawToolbar: React.FC<DrawToolbarProps> = ({ setPointer }) => {
>
<Popover.Trigger asChild>
<ToolButton
- className={classNames('Shape', { fillable: false })}
type="icon"
visible={true}
selected={
@@ -177,7 +176,6 @@ export const DrawToolbar: React.FC<DrawToolbarProps> = ({ setPointer }) => {
>
<Popover.Trigger asChild>
<ToolButton
- className={classNames('Shape', { fillable: false })}
type="icon"
visible={true}
selected={arrowOpen || isArrowLinePointer(board)}
@@ -210,7 +208,6 @@ export const DrawToolbar: React.FC<DrawToolbarProps> = ({ setPointer }) => {
return (
<ToolButton
key={index}
- className={classNames('Shape', { fillable: false })}
type="radio"
icon={button.icon}
checked={isChecked(button)}
diff --git a/packages/drawnix/src/constants.ts b/packages/drawnix/src/constants.ts
new file mode 100644
index 0000000..623cf79
--- /dev/null
+++ b/packages/drawnix/src/constants.ts
@@ -0,0 +1,31 @@
+export enum EVENT {
+ COPY = "copy",
+ PASTE = "paste",
+ CUT = "cut",
+ KEYDOWN = "keydown",
+ KEYUP = "keyup",
+ MOUSE_MOVE = "mousemove",
+ RESIZE = "resize",
+ UNLOAD = "unload",
+ FOCUS = "focus",
+ BLUR = "blur",
+ DRAG_OVER = "dragover",
+ DROP = "drop",
+ GESTURE_END = "gestureend",
+ BEFORE_UNLOAD = "beforeunload",
+ GESTURE_START = "gesturestart",
+ GESTURE_CHANGE = "gesturechange",
+ POINTER_MOVE = "pointermove",
+ POINTER_DOWN = "pointerdown",
+ POINTER_UP = "pointerup",
+ STATE_CHANGE = "statechange",
+ WHEEL = "wheel",
+ TOUCH_START = "touchstart",
+ TOUCH_END = "touchend",
+ HASHCHANGE = "hashchange",
+ VISIBILITY_CHANGE = "visibilitychange",
+ SCROLL = "scroll",
+ MENU_ITEM_SELECT = "menu.itemSelect",
+ MESSAGE = "message",
+ FULLSCREENCHANGE = "fullscreenchange",
+ }
\ No newline at end of file
diff --git a/packages/drawnix/src/drawnix.tsx b/packages/drawnix/src/drawnix.tsx
index d096b9c..68516fc 100644
--- a/packages/drawnix/src/drawnix.tsx
+++ b/packages/drawnix/src/drawnix.tsx
@@ -17,6 +17,7 @@ import { withCommonPlugin } from './plugins/with-common';
import { DrawToolbar } from './components/toolbar/draw-toolbar';
import './styles/index.scss';
+import { MainMenu } from './components/main-menu/main-menu';
export type DrawnixProps = {
value: PlaitElement[];
@@ -62,6 +63,7 @@ export const Drawnix: React.FC<DrawnixProps> = ({ value, onChange }) => {
}}
>
<Board></Board>
+ <MainMenu></MainMenu>
<DrawToolbar
setPointer={(pointer: DrawnixPointerType) => {
setAppState({ pointer });
diff --git a/packages/drawnix/src/hooks/use-outside-click.ts b/packages/drawnix/src/hooks/use-outside-click.ts
new file mode 100644
index 0000000..2c79143
--- /dev/null
+++ b/packages/drawnix/src/hooks/use-outside-click.ts
@@ -0,0 +1,86 @@
+import { useEffect } from 'react';
+import { EVENT } from '../constants';
+
+export function useOutsideClick<T extends HTMLElement>(
+ ref: React.RefObject<T>,
+ /** if performance is of concern, memoize the callback */
+ callback: (event: Event) => void,
+ /**
+ * Optional callback which is called on every click.
+ *
+ * Should return `true` if click should be considered as inside the container,
+ * and `false` if it falls outside and should call the `callback`.
+ *
+ * Returning `true` overrides the default behavior and `callback` won't be
+ * called.
+ *
+ * Returning `undefined` will fallback to the default behavior.
+ */
+ isInside?: (
+ event: Event & { target: HTMLElement },
+ /** the element of the passed ref */
+ container: T
+ ) => boolean | undefined
+) {
+ useEffect(() => {
+ function onOutsideClick(event: Event) {
+ const _event = event as Event & { target: T };
+
+ if (!ref.current) {
+ return;
+ }
+
+ const isInsideOverride = isInside?.(_event, ref.current);
+
+ if (isInsideOverride === true) {
+ return;
+ } else if (isInsideOverride === false) {
+ return callback(_event);
+ }
+
+ // clicked element is in the descenendant of the target container
+ if (
+ ref.current.contains(_event.target) ||
+ // target is detached from DOM (happens when the element is removed
+ // on a pointerup event fired *before* this handler's pointerup is
+ // dispatched)
+ !document.documentElement.contains(_event.target)
+ ) {
+ return;
+ }
+
+ const isClickOnRadixPortal =
+ _event.target.closest('[data-radix-portal]') ||
+ // when radix popup is in "modal" mode, it disables pointer events on
+ // the `body` element, so the target element is going to be the `html`
+ // (note: this won't work if we selectively re-enable pointer events on
+ // specific elements as we do with navbar or excalidraw UI elements)
+ (_event.target === document.documentElement &&
+ document.body.style.pointerEvents === 'none');
+
+ // if clicking on radix portal, assume it's a popup that
+ // should be considered as part of the UI. Obviously this is a terrible
+ // hack you can end up click on radix popups that outside the tree,
+ // but it works for most cases and the downside is minimal for now
+ if (isClickOnRadixPortal) {
+ return;
+ }
+
+ // clicking on a container that ignores outside clicks
+ if (_event.target.closest('[data-prevent-outside-click]')) {
+ return;
+ }
+
+ callback(_event);
+ }
+
+ // note: don't use `click` because it often reports incorrect `event.target`
+ document.addEventListener(EVENT.POINTER_DOWN, onOutsideClick);
+ document.addEventListener(EVENT.TOUCH_START, onOutsideClick);
+
+ return () => {
+ document.removeEventListener(EVENT.POINTER_DOWN, onOutsideClick);
+ document.removeEventListener(EVENT.TOUCH_START, onOutsideClick);
+ };
+ }, [ref, callback, isInside]);
+}
diff --git a/packages/drawnix/src/hooks/use-stable.ts b/packages/drawnix/src/hooks/use-stable.ts
new file mode 100644
index 0000000..708a3f2
--- /dev/null
+++ b/packages/drawnix/src/hooks/use-stable.ts
@@ -0,0 +1,7 @@
+import { useRef } from 'react';
+
+export const useStable = <T extends Record<string, any>>(value: T) => {
+ const ref = useRef<T>(value);
+ Object.assign(ref.current, value);
+ return ref.current;
+};
diff --git a/packages/drawnix/src/keys.ts b/packages/drawnix/src/keys.ts
new file mode 100644
index 0000000..9784537
--- /dev/null
+++ b/packages/drawnix/src/keys.ts
@@ -0,0 +1,84 @@
+import { IS_APPLE, IS_IOS } from '@plait/core';
+
+export const CODES = {
+ EQUAL: 'Equal',
+ MINUS: 'Minus',
+ NUM_ADD: 'NumpadAdd',
+ NUM_SUBTRACT: 'NumpadSubtract',
+ NUM_ZERO: 'Numpad0',
+ BRACKET_RIGHT: 'BracketRight',
+ BRACKET_LEFT: 'BracketLeft',
+ ONE: 'Digit1',
+ TWO: 'Digit2',
+ THREE: 'Digit3',
+ NINE: 'Digit9',
+ QUOTE: 'Quote',
+ ZERO: 'Digit0',
+ SLASH: 'Slash',
+ C: 'KeyC',
+ D: 'KeyD',
+ H: 'KeyH',
+ V: 'KeyV',
+ Z: 'KeyZ',
+ R: 'KeyR',
+ S: 'KeyS',
+} as const;
+
+export const KEYS = {
+ ARROW_DOWN: 'ArrowDown',
+ ARROW_LEFT: 'ArrowLeft',
+ ARROW_RIGHT: 'ArrowRight',
+ ARROW_UP: 'ArrowUp',
+ PAGE_UP: 'PageUp',
+ PAGE_DOWN: 'PageDown',
+ BACKSPACE: 'Backspace',
+ ALT: 'Alt',
+ CTRL_OR_CMD: IS_IOS || IS_APPLE ? 'metaKey' : 'ctrlKey',
+ DELETE: 'Delete',
+ ENTER: 'Enter',
+ ESCAPE: 'Escape',
+ QUESTION_MARK: '?',
+ SPACE: ' ',
+ TAB: 'Tab',
+ CHEVRON_LEFT: '<',
+ CHEVRON_RIGHT: '>',
+ PERIOD: '.',
+ COMMA: ',',
+ SUBTRACT: '-',
+ SLASH: '/',
+
+ A: 'a',
+ C: 'c',
+ D: 'd',
+ E: 'e',
+ F: 'f',
+ G: 'g',
+ H: 'h',
+ I: 'i',
+ L: 'l',
+ O: 'o',
+ P: 'p',
+ Q: 'q',
+ R: 'r',
+ S: 's',
+ T: 't',
+ V: 'v',
+ X: 'x',
+ Y: 'y',
+ Z: 'z',
+ K: 'k',
+ W: 'w',
+
+ 0: '0',
+ 1: '1',
+ 2: '2',
+ 3: '3',
+ 4: '4',
+ 5: '5',
+ 6: '6',
+ 7: '7',
+ 8: '8',
+ 9: '9',
+} as const;
+
+export type Key = keyof typeof KEYS;
diff --git a/packages/drawnix/src/styles/index.scss b/packages/drawnix/src/styles/index.scss
index 4f2c422..03a7173 100644
--- a/packages/drawnix/src/styles/index.scss
+++ b/packages/drawnix/src/styles/index.scss
@@ -11,4 +11,24 @@
left: 50%;
transform: translateX(-50%);
}
+ .main-menu {
+ position: absolute;
+ top: 40px;
+ left: 36px;
+ }
+
+ .drawnix-link, a {
+ font-weight: 500;
+ text-decoration: none;
+ color: var(--link-color);
+ user-select: none;
+ cursor: pointer;
+
+ &:hover {
+ text-decoration: underline;
+ }
+ &:active {
+ text-decoration: none;
+ }
+ }
}
\ No newline at end of file
diff --git a/packages/drawnix/src/styles/theme.scss b/packages/drawnix/src/styles/theme.scss
index 9668113..2c39a8a 100644
--- a/packages/drawnix/src/styles/theme.scss
+++ b/packages/drawnix/src/styles/theme.scss
@@ -58,7 +58,6 @@
--border-radius-sm: 0.25rem;
--border-radius-md: 0.375rem;
--border-radius-lg: 0.5rem;
-
--color-surface-high: hsl(220, 100%, 97%);
--color-surface-mid: hsl(220 25% 96%);
--color-surface-low: hsl(220 25% 94%);
@@ -70,5 +69,6 @@
--color-brand-active: #6698ff;
--color-border-outline: #767680;
--color-border-outline-variant: #c5c5d0;
- --color-surface-primary-container: rgba(102, 152, 255, .1);
+
+ --default-border-color: var(--color-surface-high);
}
diff --git a/packages/drawnix/src/styles/variables.module.scss b/packages/drawnix/src/styles/variables.module.scss
index c80e457..6cac617 100644
--- a/packages/drawnix/src/styles/variables.module.scss
+++ b/packages/drawnix/src/styles/variables.module.scss
@@ -63,6 +63,76 @@
}
}
+@mixin outlineButtonStyles {
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ padding: 0.625rem;
+ width: var(--button-width, var(--default-button-size));
+ height: var(--button-height, var(--default-button-size));
+ box-sizing: border-box;
+ border-width: 1px;
+ border-style: solid;
+ border-color: var(--button-border, var(--default-border-color));
+ border-radius: var(--border-radius-lg);
+ cursor: pointer;
+ background-color: var(--button-bg, var(--island-bg-color));
+ color: var(--icon-fill-color);
+ font-family: var(--ui-font);
+
+ svg {
+ width: var(--button-width, var(--lg-icon-size));
+ height: var(--button-height, var(--lg-icon-size));
+ }
+
+ &:hover {
+ background-color: var(--button-hover-bg, var(--island-bg-color));
+ border-color: var(
+ --button-hover-border,
+ var(--button-border, var(--default-border-color))
+ );
+ color: var(--button-hover-color, var(--button-color, var(--text-primary-color, inherit)))
+ }
+
+ &:active {
+ background-color: var(--button-active-bg, var(--island-bg-color));
+ border-color: var(--button-active-border, var(--color-primary-darkest));
+ }
+
+ &.active {
+ background-color: var(
+ --button-selected-bg,
+ var(--color-surface-primary-container)
+ );
+ border-color: var(
+ --button-selected-border,
+ var(--color-surface-primary-container)
+ );
+
+ &:hover {
+ background-color: var(
+ --button-selected-hover-bg,
+ var(--color-surface-primary-container)
+ );
+ }
+
+ svg {
+ color: var(--button-color, var(--color-on-primary-container));
+ }
+ }
+}
+
+@mixin filledButtonOnCanvas {
+ border: none;
+ box-shadow: 0 0 0 1px var(--color-surface-lowest);
+ background-color: var(--color-surface-low);
+
+ &:active {
+ box-shadow: 0 0 0 1px var(--color-brand-active);
+ }
+}
+
+
$theme-filter: "invert(93%) hue-rotate(180deg)";
$right-sidebar-width: "302px";
diff --git a/packages/drawnix/src/utils.ts b/packages/drawnix/src/utils.ts
index 27ebf13..ee9230f 100644
--- a/packages/drawnix/src/utils.ts
+++ b/packages/drawnix/src/utils.ts
@@ -1,13 +1,72 @@
-import type { ResolutionType } from "./utility-types";
+import { PlaitBoard, toImage, ToImageOptions } from '@plait/core';
+import type { ResolutionType } from './utility-types';
export const isPromiseLike = (
- value: any,
+ value: any
): value is Promise<ResolutionType<typeof value>> => {
return (
!!value &&
- typeof value === "object" &&
- "then" in value &&
- "catch" in value &&
- "finally" in value
+ typeof value === 'object' &&
+ 'then' in value &&
+ 'catch' in value &&
+ 'finally' in value
);
-};
\ No newline at end of file
+};
+
+// taken from Radix UI
+// https://github.com/radix-ui/primitives/blob/main/packages/core/primitive/src/primitive.tsx
+export const composeEventHandlers = <E>(
+ originalEventHandler?: (event: E) => void,
+ ourEventHandler?: (event: E) => void,
+ { checkForDefaultPrevented = true } = {}
+) => {
+ return function handleEvent(event: E) {
+ originalEventHandler?.(event);
+
+ if (
+ !checkForDefaultPrevented ||
+ !(event as unknown as Event)?.defaultPrevented
+ ) {
+ return ourEventHandler?.(event);
+ }
+ };
+};
+
+export const base64ToBlob = (base64: string) => {
+ let arr = base64.split(','),
+ fileType = arr[0].match(/:(.*?);/)![1],
+ bstr = atob(arr[1]),
+ l = bstr.length,
+ u8Arr = new Uint8Array(l);
+
+ while (l--) {
+ u8Arr[l] = bstr.charCodeAt(l);
+ }
+ return new Blob([u8Arr], {
+ type: fileType,
+ });
+};
+
+export const boardToImage = (
+ board: PlaitBoard,
+ options: ToImageOptions = {}
+) => {
+ return toImage(board, {
+ fillStyle: 'transparent',
+ inlineStyleClassNames: '.extend,.emojis,.text',
+ padding: 20,
+ ratio: 4,
+ ...options,
+ });
+};
+
+export function download(blob: Blob | MediaSource, filename: string) {
+ const a = document.createElement('a');
+ const url = window.URL.createObjectURL(blob);
+ a.href = url;
+ a.download = filename;
+ document.body.append(a);
+ a.click();
+ window.URL.revokeObjectURL(url);
+ a.remove();
+}