Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: improve toolbar popup controls

Abdullah Attabcffdd0f91c5
diff --git a/packages/editor/src/components/popup-presenter/index.tsx b/packages/editor/src/components/popup-presenter/index.tsx
index 7bd722f95..b82b3d936 100644
--- a/packages/editor/src/components/popup-presenter/index.tsx
+++ b/packages/editor/src/components/popup-presenter/index.tsx
@@ -18,7 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { useEffect, PropsWithChildren } from "react";
-import { Box } from "@theme-ui/components";
import {
PositionOptions,
PopupPresenterProps,
@@ -40,7 +39,7 @@ import { ResponsivePresenter, ResponsivePresenterProps } from "../responsive";
export type PopupWrapperProps = UsePopupHandlerOptions & {
autoCloseOnUnmount?: boolean;
position: PositionOptions;
-} & Partial<Omit<PopupPresenterProps, "onClose">>;
+} & Partial<Omit<PopupPresenterProps, "onClose" | "isOpen">>;
export function PopupWrapper(props: PropsWithChildren<PopupWrapperProps>) {
const { id, position, children, autoCloseOnUnmount, ...presenterProps } =
props;
@@ -67,15 +66,7 @@ export function PopupWrapper(props: PropsWithChildren<PopupWrapperProps>) {
{...presenterProps}
isOpen={isPopupOpen}
>
- <Box
- sx={{
- boxShadow: "menu",
- borderRadius: "default",
- overflow: "hidden"
- }}
- >
- {children}
- </Box>
+ {children}
</PopupPresenter>
);
}
@@ -83,28 +74,21 @@ export function PopupWrapper(props: PropsWithChildren<PopupWrapperProps>) {
type UsePopupHandlerOptions = {
id: string;
group: string;
- isOpen: boolean;
onClosed?: () => void;
};
export function usePopupHandler(options: UsePopupHandlerOptions) {
- const { isOpen, id, onClosed, group } = options;
+ const { id, onClosed, group } = options;
const openedPopups = useToolbarStore((store) => store.openedPopups);
- const openPopup = useToolbarStore((store) => store.openPopup);
const closePopup = useToolbarStore((store) => store.closePopup);
const closePopupGroup = useToolbarStore((store) => store.closePopupGroup);
const isPopupOpen = typeof openedPopups[id] === "object";
const isPopupDefined = typeof openedPopups[id] !== "undefined";
- useEffect(() => {
- if (isOpen) openPopup({ id, group });
- else closePopup(id);
- }, [isOpen, closePopup, openPopup, id, group]);
-
useEffect(() => {
// we don't want to close the popup just when it is about to open.
if (!isPopupOpen && isPopupDefined) onClosed?.();
- }, [isPopupOpen, isPopupDefined]);
+ }, [isPopupOpen, isPopupDefined, onClosed]);
useEffect(() => {
// if another popup in the same group is open, close it.
diff --git a/packages/editor/src/extensions/attachment/component.tsx b/packages/editor/src/extensions/attachment/component.tsx
index 17c9e1728..0da00f668 100644
--- a/packages/editor/src/extensions/attachment/component.tsx
+++ b/packages/editor/src/extensions/attachment/component.tsx
@@ -88,6 +88,7 @@ export function AttachmentComponent(props: ReactNodeViewProps<FileAttachment>) {
{selected && !isDragging && (
<ToolbarGroup
editor={editor}
+ groupId="attachmentTools"
tools={
editor.isEditable
? [
diff --git a/packages/editor/src/extensions/embed/component.tsx b/packages/editor/src/extensions/embed/component.tsx
index 9f8a520c7..c06ff094c 100644
--- a/packages/editor/src/extensions/embed/component.tsx
+++ b/packages/editor/src/extensions/embed/component.tsx
@@ -95,6 +95,7 @@ export function EmbedComponent(
>
<ToolbarGroup
editor={editor}
+ groupId="embedTools"
tools={[
"embedAlignLeft",
"embedAlignCenter",
diff --git a/packages/editor/src/extensions/image/component.tsx b/packages/editor/src/extensions/image/component.tsx
index 63e6d3b07..52ed460ae 100644
--- a/packages/editor/src/extensions/image/component.tsx
+++ b/packages/editor/src/extensions/image/component.tsx
@@ -141,6 +141,7 @@ export function ImageComponent(
>
<ToolbarGroup
editor={editor}
+ groupId="imageTools"
tools={
isReadonly
? [
diff --git a/packages/editor/src/extensions/web-clip/component.tsx b/packages/editor/src/extensions/web-clip/component.tsx
index b15f42107..faf74e74e 100644
--- a/packages/editor/src/extensions/web-clip/component.tsx
+++ b/packages/editor/src/extensions/web-clip/component.tsx
@@ -158,6 +158,7 @@ export function WebClipComponent(props: ReactNodeViewProps<WebClipAttributes>) {
>
<ToolbarGroup
editor={editor}
+ groupId="webclipTools"
tools={[
"webclipFullScreen",
"webclipOpenExternal",
diff --git a/packages/editor/src/hooks/use-editor.ts b/packages/editor/src/hooks/use-editor.ts
index 72596a53f..a1d1ba879 100644
--- a/packages/editor/src/hooks/use-editor.ts
+++ b/packages/editor/src/hooks/use-editor.ts
@@ -26,6 +26,7 @@ import {
import { DependencyList, useEffect, useMemo, useRef, useState } from "react";
import { Editor } from "../types";
import { EditorState } from "@tiptap/pm/state";
+import { useToolbarStore } from "../toolbar/stores/toolbar-store";
function useForceUpdate() {
const [, setValue] = useState(0);
@@ -121,9 +122,15 @@ export const useEditor = (
}
}
+ function onClick() {
+ useToolbarStore.getState().closeAllPopups();
+ }
+
editor.view.dom.addEventListener("dragenter", onDragEnter);
+ editor.view.dom.addEventListener("click", onClick);
return () => {
editor.view.dom.removeEventListener("dragenter", onDragEnter);
+ editor.view.dom.removeEventListener("click", onClick);
};
}, [editor.view.dom, editor.view.dragging]);
diff --git a/packages/editor/src/toolbar/components/dropdown.tsx b/packages/editor/src/toolbar/components/dropdown.tsx
index daa332316..1cd57855e 100644
--- a/packages/editor/src/toolbar/components/dropdown.tsx
+++ b/packages/editor/src/toolbar/components/dropdown.tsx
@@ -17,14 +17,17 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { useRef, useState, useLayoutEffect } from "react";
+import { useRef } from "react";
import { Text } from "@theme-ui/components";
import { Icon, MenuItem, MenuPresenter } from "@notesnook/ui";
import { Icons } from "../icons";
-import { useIsMobile, useToolbarLocation } from "../stores/toolbar-store";
+import {
+ useIsMobile,
+ usePopupManager,
+ useToolbarLocation
+} from "../stores/toolbar-store";
import { getToolbarElement } from "../utils/dom";
import { Button } from "../../components/button";
-import { usePopupHandler } from "../../components/popup-presenter";
type DropdownProps = {
id: string;
@@ -39,19 +42,10 @@ export function Dropdown(props: DropdownProps) {
const { id, group, items, selectedItem, buttonRef, menuWidth, disabled } =
props;
const internalRef = useRef<HTMLButtonElement>();
- const [isOpen, setIsOpen] = useState(false);
const toolbarLocation = useToolbarLocation();
const isMobile = useIsMobile();
const isBottom = toolbarLocation === "bottom";
-
- useLayoutEffect(() => () => setIsOpen(false), []);
-
- const { isPopupOpen } = usePopupHandler({
- group,
- id,
- isOpen,
- onClosed: () => setIsOpen(false)
- });
+ const { isOpen, toggle, close } = usePopupManager({ group, id });
return (
<>
@@ -64,7 +58,7 @@ export function Dropdown(props: DropdownProps) {
sx={{
p: 1,
m: 0,
- bg: isPopupOpen ? "hover" : "transparent",
+ bg: isOpen ? "hover" : "transparent",
height: "100%",
flexShrink: 0,
display: "flex",
@@ -79,7 +73,7 @@ export function Dropdown(props: DropdownProps) {
}
}}
disabled={disabled}
- onClick={() => setIsOpen((s) => !s)}
+ onClick={toggle}
onMouseDown={(e) => e.preventDefault()}
>
{typeof selectedItem === "string" ? (
@@ -87,7 +81,7 @@ export function Dropdown(props: DropdownProps) {
sx={{
fontSize: "subBody",
mr: 1,
- color: isPopupOpen ? "accent" : "paragraph",
+ color: isOpen ? "accent" : "paragraph",
flexShrink: 0
}}
>
@@ -99,22 +93,22 @@ export function Dropdown(props: DropdownProps) {
<Icon
path={
isBottom
- ? isPopupOpen
+ ? isOpen
? Icons.chevronDown
: Icons.chevronUp
- : isPopupOpen
+ : isOpen
? Icons.chevronUp
: Icons.chevronDown
}
- color={isPopupOpen ? "accent" : "paragraph"}
+ color={isOpen ? "accent" : "paragraph"}
size={"small"}
/>
</Button>
<MenuPresenter
- isOpen={isPopupOpen}
+ isOpen={isOpen}
items={items}
- onClose={() => setIsOpen(false)}
+ onClose={close}
position={{
target: isBottom
? getToolbarElement()
diff --git a/packages/editor/src/toolbar/components/more-tools.tsx b/packages/editor/src/toolbar/components/more-tools.tsx
index f5e6e2e74..fb9af21b7 100644
--- a/packages/editor/src/toolbar/components/more-tools.tsx
+++ b/packages/editor/src/toolbar/components/more-tools.tsx
@@ -17,15 +17,14 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { useLayoutEffect, useRef, useState } from "react";
+import { useEffect, useRef } from "react";
import { PopupWrapper } from "../../components/popup-presenter";
import { ToolButton } from "../components/tool-button";
-import { useToolbarLocation } from "../stores/toolbar-store";
+import { usePopupManager, useToolbarLocation } from "../stores/toolbar-store";
import { ToolProps } from "../types";
import { getToolbarElement } from "../utils/dom";
import { ToolId } from "../tools";
import { ToolbarGroup } from "./toolbar-group";
-import { useCallback } from "react";
type MoreToolsProps = ToolProps & {
popupId: string;
@@ -35,13 +34,22 @@ type MoreToolsProps = ToolProps & {
group?: string;
};
export function MoreTools(props: MoreToolsProps) {
- const { popupId, editor, tools, autoCloseOnUnmount, autoOpen, group } = props;
+ const { popupId, editor, tools, autoCloseOnUnmount, autoOpen } = props;
const toolbarLocation = useToolbarLocation();
const isBottom = toolbarLocation === "bottom";
+ const group = isBottom ? "mobile" : "toolbarGroup";
const buttonRef = useRef<HTMLButtonElement>(null);
- const [isOpen, setIsOpen] = useState(autoOpen || false);
- const onClosed = useCallback(() => setIsOpen(false), [setIsOpen]);
- useLayoutEffect(() => () => setIsOpen(false), []);
+ const { open, toggle, isOpen, isPinned, togglePinned } = usePopupManager({
+ id: popupId,
+ group
+ });
+
+ useEffect(() => {
+ if (autoOpen) {
+ open();
+ togglePinned();
+ }
+ }, [autoOpen]);
return (
<>
@@ -49,14 +57,12 @@ export function MoreTools(props: MoreToolsProps) {
{...props}
toggled={isOpen}
buttonRef={buttonRef}
- onClick={() => setIsOpen((s) => !s)}
+ onClick={toggle}
/>
<PopupWrapper
- isOpen={isOpen}
scope="editorToolbar"
- group={group || "toolbarGroup"}
+ group={group}
id={popupId}
- onClosed={onClosed}
position={{
isTargetAbsolute: true,
target: isBottom ? getToolbarElement() : buttonRef.current || "mouse",
@@ -67,19 +73,37 @@ export function MoreTools(props: MoreToolsProps) {
autoCloseOnUnmount={autoCloseOnUnmount}
focusOnRender={false}
blocking={false}
+ sx={{
+ display: "flex",
+ boxShadow: "menu",
+ bg: "background",
+ gap: [0, 0, "small"],
+ p: ["4px", "4px", "small"],
+ flex: 1,
+ borderRadius: "default",
+ overflowX: "auto",
+ maxWidth: "95vw"
+ }}
>
<ToolbarGroup
tools={tools}
editor={editor}
+ groupId={popupId}
sx={{
- flex: 1,
- boxShadow: "menu",
- bg: "background",
- borderRadius: "default",
- overflowX: "auto",
- maxWidth: "95vw"
+ p: 0,
+ borderRight: autoOpen ? "none" : "1px solid var(--border)",
+ pr: autoOpen ? 0 : ["4px", "4px", "small"],
+ mr: autoOpen ? 0 : ["4px", "4px", "small"]
}}
/>
+ {autoOpen ? null : (
+ <ToolButton
+ toggled={isPinned}
+ onClick={togglePinned}
+ icon="pin"
+ variant="small"
+ />
+ )}
</PopupWrapper>
</>
);
diff --git a/packages/editor/src/toolbar/components/popup.tsx b/packages/editor/src/toolbar/components/popup.tsx
index c7491d263..42e312a88 100644
--- a/packages/editor/src/toolbar/components/popup.tsx
+++ b/packages/editor/src/toolbar/components/popup.tsx
@@ -33,11 +33,13 @@ type Action = {
export type PopupProps = {
title?: string;
onClose?: () => void;
+ onPin?: () => void;
+ isPinned?: boolean;
action?: Action;
};
export function Popup(props: PropsWithChildren<PopupProps>) {
- const { title, onClose, action, children } = props;
+ const { title, onClose, onPin, isPinned, action, children } = props;
return (
<EmotionThemeProvider scope="editorToolbar">
@@ -63,13 +65,30 @@ export function Popup(props: PropsWithChildren<PopupProps>) {
}}
>
<Text variant={"title"}>{title}</Text>
- <Button
- variant={"secondary"}
- sx={{ p: 0, bg: "transparent" }}
- onClick={onClose}
- >
- <Icon path={Icons.close} size={"big"} />
- </Button>
+ <Flex sx={{ alignItems: "center", gap: 1 }}>
+ {onPin ? (
+ <Button
+ variant={"secondary"}
+ sx={{ p: 0, bg: "transparent" }}
+ onClick={onPin}
+ >
+ <Icon
+ path={Icons.pin}
+ size={"medium"}
+ color={isPinned ? "accent" : "icon"}
+ />
+ </Button>
+ ) : null}
+ {onClose ? (
+ <Button
+ variant={"secondary"}
+ sx={{ p: 0, bg: "transparent" }}
+ onClick={onClose}
+ >
+ <Icon path={Icons.close} size={"big"} />
+ </Button>
+ ) : null}
+ </Flex>
</Flex>
)}
{children}
diff --git a/packages/editor/src/toolbar/components/split-button.tsx b/packages/editor/src/toolbar/components/split-button.tsx
index a58ac6136..9bd3852ae 100644
--- a/packages/editor/src/toolbar/components/split-button.tsx
+++ b/packages/editor/src/toolbar/components/split-button.tsx
@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { PropsWithChildren, useRef } from "react";
+import { PropsWithChildren } from "react";
import { Flex } from "@theme-ui/components";
import { ToolButton, ToolButtonProps } from "./tool-button";
import { useIsMobile, useToolbarLocation } from "../stores/toolbar-store";
@@ -27,16 +27,16 @@ export type SplitButtonProps = ToolButtonProps & {
onOpen: () => void;
};
function _SplitButton(props: PropsWithChildren<SplitButtonProps>) {
- const { children, onOpen, sx, toggled, ...toolButtonProps } = props;
+ const { children, onOpen, sx, toggled, buttonRef, ...toolButtonProps } =
+ props;
- const ref = useRef<HTMLDivElement>(null);
const toolbarLocation = useToolbarLocation();
const isMobile = useIsMobile();
return (
<>
<Flex
- ref={ref}
+ ref={buttonRef}
sx={{
flexShrink: 0,
alignItems: "stretch",
@@ -66,6 +66,7 @@ function _SplitButton(props: PropsWithChildren<SplitButtonProps>) {
}
export const SplitButton = React.memo(_SplitButton, (prev, next) => {
return (
+ prev.buttonRef === next.buttonRef &&
prev.toggled === next.toggled &&
JSON.stringify(prev.sx) === JSON.stringify(next.sx) &&
prev.children === next.children
diff --git a/packages/editor/src/toolbar/components/tool-button.tsx b/packages/editor/src/toolbar/components/tool-button.tsx
index 9bd954b84..2a575e711 100644
--- a/packages/editor/src/toolbar/components/tool-button.tsx
+++ b/packages/editor/src/toolbar/components/tool-button.tsx
@@ -94,6 +94,7 @@ export const ToolButton = React.memo(
prev.toggled === next.toggled &&
prev.icon === next.icon &&
prev.disabled === next.disabled &&
+ prev.onClick === next.onClick &&
JSON.stringify(prev.sx) === JSON.stringify(next.sx)
);
}
diff --git a/packages/editor/src/toolbar/components/toolbar-group.tsx b/packages/editor/src/toolbar/components/toolbar-group.tsx
index d9209834c..96da90db7 100644
--- a/packages/editor/src/toolbar/components/toolbar-group.tsx
+++ b/packages/editor/src/toolbar/components/toolbar-group.tsx
@@ -29,9 +29,10 @@ export type ToolbarGroupProps = FlexProps & {
editor: Editor;
variant?: ToolButtonVariant;
force?: boolean;
+ groupId: string;
};
export function ToolbarGroup(props: ToolbarGroupProps) {
- const { tools, editor, force, sx, ...flexProps } = props;
+ const { tools, editor, force, sx, groupId, ...flexProps } = props;
return (
<Flex
@@ -48,6 +49,7 @@ export function ToolbarGroup(props: ToolbarGroupProps) {
if (Array.isArray(toolId)) {
return (
<MoreTools
+ parentGroup={groupId}
key={"more-tools"}
title="More"
icon="more"
@@ -61,6 +63,7 @@ export function ToolbarGroup(props: ToolbarGroupProps) {
const toolDefinition = getToolDefinition(toolId);
return (
<Component
+ parentGroup={groupId}
key={toolDefinition.title}
editor={editor}
force={force}
diff --git a/packages/editor/src/toolbar/floating-menus/hover-popup/link.tsx b/packages/editor/src/toolbar/floating-menus/hover-popup/link.tsx
index 2ff865b37..66a3b1529 100644
--- a/packages/editor/src/toolbar/floating-menus/hover-popup/link.tsx
+++ b/packages/editor/src/toolbar/floating-menus/hover-popup/link.tsx
@@ -20,7 +20,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { ToolbarGroup } from "../../components/toolbar-group";
import { HoverPopupProps } from ".";
import { useHoverPopupContext } from "./context";
-
+import { ToolbarGroupDefinition } from "../../types";
+
+const LINK_TOOLS: ToolbarGroupDefinition = [
+ "openLink",
+ "editLink",
+ "removeLink",
+ "copyLink"
+];
function LinkHoverPopup(props: HoverPopupProps) {
const { editor } = props;
const { selectedNode } = useHoverPopupContext();
@@ -36,7 +43,8 @@ function LinkHoverPopup(props: HoverPopupProps) {
return (
<ToolbarGroup
force
- tools={["openLink", "editLink", "removeLink", "copyLink"]}
+ tools={LINK_TOOLS}
+ groupId={"linkHoverTools"}
editor={editor}
sx={{
bg: "background",
diff --git a/packages/editor/src/toolbar/icons.ts b/packages/editor/src/toolbar/icons.ts
index 12bed5407..3269dbfab 100644
--- a/packages/editor/src/toolbar/icons.ts
+++ b/packages/editor/src/toolbar/icons.ts
@@ -122,7 +122,8 @@ import {
mdiCheckboxMultipleMarked,
mdiFormatFloatLeft,
mdiMessageOutline,
- mdiVectorLink
+ mdiVectorLink,
+ mdiPinOutline
} from "@mdi/js";
export const Icons = {
@@ -254,6 +255,7 @@ export const Icons = {
selectAllUnchecked: mdiCheckboxMultipleBlankOutline,
selectAllChecked: mdiCheckboxMultipleMarked,
checkList: mdiCheckboxMarkedOutline,
+ pin: mdiPinOutline,
none: ""
};
diff --git a/packages/editor/src/toolbar/popups/color-picker.tsx b/packages/editor/src/toolbar/popups/color-picker.tsx
index 9f31a0310..ede55d27b 100644
--- a/packages/editor/src/toolbar/popups/color-picker.tsx
+++ b/packages/editor/src/toolbar/popups/color-picker.tsx
@@ -39,6 +39,8 @@ type ColorPickerProps = {
expanded?: boolean;
onChange: (color: string) => void;
onClose?: () => void;
+ isPinned?: boolean;
+ onPin?: () => void;
title?: string;
onSave?: (color: string) => void;
cacheKey?: string;
@@ -52,6 +54,8 @@ export function ColorPicker(props: ColorPickerProps) {
onChange,
title,
onClose,
+ isPinned,
+ onPin,
expanded,
onSave,
colors = [],
@@ -83,12 +87,14 @@ export function ColorPicker(props: ColorPickerProps) {
);
return (
- <Popup title={title} onClose={onClose}>
+ <Popup title={title} onClose={onClose} isPinned={isPinned} onPin={onPin}>
<Flex
ref={ref}
tabIndex={-1}
sx={{
bg: "background",
+ boxShadow: ["menu", "none"],
+ borderRadius: ["default", "none"],
flexDirection: "column",
".react-colorful": {
width: "auto",
diff --git a/packages/editor/src/toolbar/stores/toolbar-store.ts b/packages/editor/src/toolbar/stores/toolbar-store.ts
index d3c4b3895..40aacf3e2 100644
--- a/packages/editor/src/toolbar/stores/toolbar-store.ts
+++ b/packages/editor/src/toolbar/stores/toolbar-store.ts
@@ -19,10 +19,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { create } from "zustand";
import { DownloadOptions } from "../../utils/downloader";
+import { useCallback } from "react";
export type ToolbarLocation = "top" | "bottom";
-export type PopupRef = { id: string; group: string };
+export type PopupRef = {
+ id: string;
+ group: string;
+ pinned?: boolean;
+ parent?: string;
+};
interface ToolbarState {
downloadOptions?: DownloadOptions;
setDownloadOptions: (options?: DownloadOptions) => void;
@@ -69,13 +75,27 @@ export const useToolbarStore = create<ToolbarState>((set, get) => ({
set((state) => {
for (const key in state.openedPopups) {
const ref = state.openedPopups[key];
- if (ref && ref.group === group && !excluded.includes(ref.id)) {
+ if (
+ ref &&
+ ref.group === group &&
+ !excluded.includes(ref.id) &&
+ !ref.pinned &&
+ !isChildPinned(state.openedPopups, ref.id)
+ ) {
state.openedPopups[key] = undefined;
}
}
return state;
}),
- closeAllPopups: () => set({ openedPopups: {} }),
+ closeAllPopups: () =>
+ set((state) => {
+ for (const key in state.openedPopups) {
+ const ref = state.openedPopups[key];
+ if (ref && !ref.pinned && !isChildPinned(state.openedPopups, ref.id))
+ state.openedPopups[key] = undefined;
+ }
+ return state;
+ }),
fontFamily: "sans-serif",
setFontFamily: (fontFamily) => set({ fontFamily }),
fontSize: 16,
@@ -89,3 +109,107 @@ export function useToolbarLocation() {
export function useIsMobile() {
return useToolbarStore((store) => store.isMobile);
}
+
+export function usePopupManager(options: {
+ id: string;
+ group: string;
+ parent?: string;
+}) {
+ const { id, parent } = options;
+ const openedPopups = useToolbarStore((store) => store.openedPopups);
+ const openPopup = useToolbarStore((store) => store.openPopup);
+ const closePopup = useToolbarStore((store) => store.closePopup);
+ const closePopupGroup = useToolbarStore((store) => store.closePopupGroup);
+ const isMobile = useIsMobile();
+ const isOpen = typeof openedPopups[id] === "object";
+ const isPinned =
+ typeof openedPopups[id] === "object" &&
+ (!!openedPopups[id]?.pinned || isChildPinned(openedPopups, id));
+ const group = isMobile ? "mobile" : options.group;
+
+ const open = useCallback(() => {
+ closePopupGroup(group, [id, parent || ""]);
+ openPopup({
+ id,
+ group,
+ parent,
+ pinned: isParentPinned(useToolbarStore.getState().openedPopups, parent)
+ });
+ }, [openPopup, closePopupGroup, group, id, parent]);
+
+ const close = useCallback(() => {
+ closePopup(id);
+ useToolbarStore.setState((state) => {
+ toggleChildState(
+ state.openedPopups,
+ (ref) => {
+ state.openedPopups[ref.id] = undefined;
+ },
+ id
+ );
+ return state;
+ });
+ }, [closePopup, id]);
+
+ const toggle = useCallback(() => {
+ if (isOpen) close();
+ else open();
+ }, [isOpen, close, open]);
+
+ const togglePinned = useCallback(() => {
+ useToolbarStore.setState((state) => {
+ toggleChildState(
+ state.openedPopups,
+ (ref) => (ref.pinned = !isPinned),
+ id
+ );
+ return state;
+ });
+ if (isPinned) openPopup({ group, id, parent, pinned: false });
+ else openPopup({ group, id, parent, pinned: true });
+ }, [isPinned, openPopup, id, group, parent]);
+
+ return { isOpen, open, close, toggle, isPinned, togglePinned };
+}
+
+function toggleChildState(
+ popups: Record<string, PopupRef | undefined | false>,
+ action: (ref: PopupRef) => void,
+ parent?: string
+) {
+ if (!parent) return;
+ for (const key in popups) {
+ const ref = popups[key];
+ if (ref && ref.parent === parent) {
+ action(ref);
+ toggleChildState(popups, action, ref.id);
+ }
+ }
+}
+
+function isParentPinned(
+ popups: Record<string, PopupRef | undefined | false>,
+ parent?: string
+) {
+ if (!parent) return;
+ for (const key in popups) {
+ const ref = popups[key];
+ if (ref && ref.id === parent && ref.pinned) {
+ return true;
+ }
+ }
+ return false;
+}
+
+function isChildPinned(
+ popups: Record<string, PopupRef | undefined | false>,
+ id: string
+) {
+ for (const key in popups) {
+ const ref = popups[key];
+ if (ref && ref.parent === id && ref.pinned) {
+ return true;
+ }
+ }
+ return false;
+}
diff --git a/packages/editor/src/toolbar/toolbar.tsx b/packages/editor/src/toolbar/toolbar.tsx
index 79b81d18f..18743cfd7 100644
--- a/packages/editor/src/toolbar/toolbar.tsx
+++ b/packages/editor/src/toolbar/toolbar.tsx
@@ -106,6 +106,7 @@ export function Toolbar(props: ToolbarProps) {
key={tools.join("")}
tools={tools}
editor={editor}
+ groupId={tools.join("")}
sx={{
borderRight: "1px solid var(--separator)",
":last-of-type": { borderRight: "none" },
diff --git a/packages/editor/src/toolbar/tools/colors.tsx b/packages/editor/src/toolbar/tools/colors.tsx
index e76082447..da947f3fe 100644
--- a/packages/editor/src/toolbar/tools/colors.tsx
+++ b/packages/editor/src/toolbar/tools/colors.tsx
@@ -17,23 +17,73 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { useEffect, useMemo, useState } from "react";
+import { useEffect, useMemo, useRef, useState } from "react";
import tinycolor from "tinycolor2";
import { PopupWrapper } from "../../components/popup-presenter";
import { config } from "../../utils/config";
import { SplitButton } from "../components/split-button";
import { ColorPicker } from "../popups/color-picker";
-import { useToolbarLocation } from "../stores/toolbar-store";
+import { usePopupManager, useToolbarLocation } from "../stores/toolbar-store";
import { ToolProps } from "../types";
-import { getToolbarElement } from "../utils/dom";
+import { getEditorToolbarPopup } from "../utils/dom";
import { PositionOptions } from "@notesnook/ui";
+type ColorType = "background" | "text" | "border";
type ColorToolProps = ToolProps & {
onColorChange: (color?: string) => void;
activeColor: string;
title: string;
cacheKey: string;
- defaultColors: string[];
+ type: ColorType;
+};
+
+const DEFAULT_COLORS: Record<ColorType, string[]> = {
+ background: [
+ "#b0bec5", // Neutralized pink
+ "#a1887f", // Neutralized purple
+ "#9575cd", // Neutralized deep purple
+ "#7986cb", // Neutralized indigo
+ "#64b5f6", // Neutralized blue
+ "#4fc3f7", // Neutralized light blue
+ "#4dd0e1", // Neutralized cyan
+ "#4db6ac", // Neutralized teal
+ "#81c784", // Neutralized green
+ "#aed581", // Neutralized light green
+ "#dce775", // Neutralized lime
+ "#fff176", // Neutralized yellow
+ "#ffd54f", // Neutralized amber
+ "#e57373" // Neutralized red
+ ],
+ text: [
+ "#e91e63",
+ "#9c27b0",
+ "#673ab7",
+ "#3f51b5",
+ "#2196f3",
+ "#03a9f4",
+ "#00bcd4",
+ "#009688",
+ "#4caf50",
+ "#8bc34a",
+ "#cddc39",
+ "#ffc107",
+ "#f44336"
+ ],
+ border: [
+ "#ef5350", // Crimson Red
+ "#ab47bc", // Lavender Purple
+ "#42a5f5", // Sky Blue
+ "#26c6da", // Aqua Green
+ "#ffca28", // Amber Yellow
+ "#ffa726", // Tangerine
+ "#66bb6a", // Spring Green
+ "#ec407a", // Rose Pink
+ "#ff7043", // Coral Orange
+ "#dce775", // Bright Lime
+ "#5c6bc0", // Soft Indigo
+ "#00bcd4", // Cyan
+ "#80cbc4"
+ ] // Bright Mint
};
export function ColorTool(props: ColorToolProps) {
@@ -42,25 +92,33 @@ export function ColorTool(props: ColorToolProps) {
activeColor: _activeColor,
title,
cacheKey,
- defaultColors,
+ type,
+ parentGroup,
...toolProps
} = props;
+ const buttonRef = useRef<HTMLButtonElement>(null);
+ const defaultColors = DEFAULT_COLORS[type];
const activeColor = _activeColor || config.get(cacheKey);
const tColor = tinycolor(activeColor);
const isBottom = useToolbarLocation() === "bottom";
- const [isOpen, setIsOpen] = useState<boolean>(false);
+ const { toggle, isOpen, close, isPinned, togglePinned } = usePopupManager({
+ id: props.icon,
+ group: "color",
+ parent: parentGroup
+ });
const [colors, setColors] = useState(
config.get<string[]>(`custom_${cacheKey}`, []) || []
);
- const position: PositionOptions = useMemo(() => {
- return {
+ const position: PositionOptions = useMemo(
+ () => ({
isTargetAbsolute: true,
- target: isBottom ? getToolbarElement() : "mouse",
- align: isBottom ? "center" : "end",
+ target: isBottom ? getEditorToolbarPopup() : buttonRef.current || "mouse",
+ align: isBottom ? "center" : "start",
location: isBottom ? "top" : "below",
yOffset: 10
- };
- }, [isBottom]);
+ }),
+ [isBottom, isOpen]
+ );
useEffect(() => {
config.set(`custom_${cacheKey}`, colors);
@@ -69,6 +127,7 @@ export function ColorTool(props: ColorToolProps) {
return (
<SplitButton
{...toolProps}
+ buttonRef={buttonRef}
iconColor={activeColor && tColor.isDark() ? "white" : "icon"}
sx={{
mr: 0,
@@ -77,12 +136,11 @@ export function ColorTool(props: ColorToolProps) {
bg: activeColor ? tColor.darken(5).toRgbString() : "transparent"
}
}}
- onOpen={() => setIsOpen((s) => !s)}
+ onOpen={toggle}
toggled={isOpen}
onClick={() => onColorChange(activeColor)}
>
<PopupWrapper
- isOpen={isOpen}
id={props.icon}
group={"color"}
position={position}
@@ -118,9 +176,9 @@ export function ColorTool(props: ColorToolProps) {
config.set(cacheKey, color);
}
}}
- onClose={() => {
- setIsOpen(false);
- }}
+ onClose={close}
+ isPinned={isPinned}
+ onPin={togglePinned}
title={title}
/>
</PopupWrapper>
@@ -136,22 +194,7 @@ export function Highlight(props: ToolProps) {
cacheKey="highlight"
activeColor={editor.getAttributes("textStyle").backgroundColor}
title={"Background color"}
- defaultColors={[
- "#b0bec5", // Neutralized pink
- "#a1887f", // Neutralized purple
- "#9575cd", // Neutralized deep purple
- "#7986cb", // Neutralized indigo
- "#64b5f6", // Neutralized blue
- "#4fc3f7", // Neutralized light blue
- "#4dd0e1", // Neutralized cyan
- "#4db6ac", // Neutralized teal
- "#81c784", // Neutralized green
- "#aed581", // Neutralized light green
- "#dce775", // Neutralized lime
- "#fff176", // Neutralized yellow
- "#ffd54f", // Neutralized amber
- "#e57373" // Neutralized red
- ]}
+ type="background"
onColorChange={(color) =>
color
? editor.chain().setHighlight(color).run()
@@ -169,21 +212,7 @@ export function TextColor(props: ToolProps) {
cacheKey={"textColor"}
activeColor={editor.getAttributes("textStyle").color}
title="Text color"
- defaultColors={[
- "#e91e63",
- "#9c27b0",
- "#673ab7",
- "#3f51b5",
- "#2196f3",
- "#03a9f4",
- "#00bcd4",
- "#009688",
- "#4caf50",
- "#8bc34a",
- "#cddc39",
- "#ffc107",
- "#f44336"
- ]}
+ type="text"
onColorChange={(color) =>
color
? editor.chain().setColor(color).run()
diff --git a/packages/editor/src/toolbar/tools/lists.tsx b/packages/editor/src/toolbar/tools/lists.tsx
index 2bf89b087..2bdaa154c 100644
--- a/packages/editor/src/toolbar/tools/lists.tsx
+++ b/packages/editor/src/toolbar/tools/lists.tsx
@@ -20,12 +20,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { ToolProps } from "../types";
import { Box, Button, Flex } from "@theme-ui/components";
import { IconNames } from "../icons";
-import { useCallback, useRef, useState, useLayoutEffect } from "react";
+import { useCallback, useRef } from "react";
import { SplitButton } from "../components/split-button";
-import { useToolbarLocation } from "../stores/toolbar-store";
+import { usePopupManager, useToolbarLocation } from "../stores/toolbar-store";
import { getToolbarElement } from "../utils/dom";
import { PopupWrapper } from "../../components/popup-presenter";
-import React from "react";
import { ToolButton } from "../components/tool-button";
import { findListItemType, isListActive } from "../../utils/prosemirror";
@@ -43,16 +42,18 @@ type ListToolProps<TListStyleTypes> = ToolProps & {
subTypes: ListSubType<TListStyleTypes>[];
};
-function _ListTool<TListStyleTypes extends string>(
+function ListTool<TListStyleTypes extends string>(
props: ListToolProps<TListStyleTypes>
) {
- const { editor, onClick, subTypes, type, ...toolProps } = props;
+ const { editor, onClick, subTypes, type, parentGroup, ...toolProps } = props;
const toolbarLocation = useToolbarLocation();
const isBottom = toolbarLocation === "bottom";
- const [isOpen, setIsOpen] = useState(false);
const buttonRef = useRef<HTMLButtonElement>(null);
-
- useLayoutEffect(() => () => setIsOpen(false), []);
+ const { toggle, isOpen, close } = usePopupManager({
+ id: toolProps.title,
+ group: "lists",
+ parent: parentGroup
+ });
return (
<SplitButton
@@ -61,14 +62,14 @@ function _ListTool<TListStyleTypes extends string>(
onClick={onClick}
toggled={isOpen}
sx={{ mr: 0 }}
- onOpen={() => setIsOpen((s) => !s)}
+ onOpen={toggle}
>
<PopupWrapper
- isOpen={isOpen}
group="lists"
id={toolProps.title}
blocking={false}
focusOnRender={false}
+ autoCloseOnUnmount
position={{
isTargetAbsolute: true,
target: isBottom ? getToolbarElement() : buttonRef.current || "mouse",
@@ -76,14 +77,14 @@ function _ListTool<TListStyleTypes extends string>(
location: isBottom ? "top" : "below",
yOffset: 10
}}
- onClosed={() => setIsOpen(false)}
>
<Box
sx={{
bg: "background",
+ boxShadow: "menu",
+ borderRadius: "default",
display: "grid",
gridTemplateColumns: "1fr 1fr 1fr",
- gap: 1,
p: 1
}}
>
@@ -91,7 +92,7 @@ function _ListTool<TListStyleTypes extends string>(
<Button
key={item.title}
variant={"menuitem"}
- sx={{ width: 80, p: 0 }}
+ sx={{ width: 80, p: 0, borderRadius: "default" }}
onClick={() => {
let chain = editor.chain().focus();
if (!chain || !editor) return;
@@ -100,7 +101,7 @@ function _ListTool<TListStyleTypes extends string>(
if (type === "bulletList") chain = chain.toggleBulletList();
else chain = chain.toggleOrderedList();
}
- setIsOpen(false);
+ close();
return chain
.updateAttributes(type, { listType: item.type })
.run();
@@ -115,10 +116,6 @@ function _ListTool<TListStyleTypes extends string>(
);
}
-const ListTool = React.memo(_ListTool, (prev, next) => {
- return prev.isActive === next.isActive;
-});
-
export function NumberedList(props: ToolProps) {
const { editor } = props;
@@ -230,11 +227,11 @@ function ListThumbnail(props: ListThumbnailProps) {
sx={{
flexDirection: "column",
flex: 1,
- p: 0,
listStyleType,
- ml: 4,
- mr: 1,
- my: 1,
+ m: 0,
+ ml: "10px",
+ p: "5px",
+ pl: "7px",
gap: `7px`
}}
onMouseDown={(e) => {
diff --git a/packages/editor/src/toolbar/tools/table.tsx b/packages/editor/src/toolbar/tools/table.tsx
index 3603e1e57..5612f198c 100644
--- a/packages/editor/src/toolbar/tools/table.tsx
+++ b/packages/editor/src/toolbar/tools/table.tsx
@@ -230,6 +230,7 @@ export function CellBackgroundColor(props: ToolProps) {
cacheKey="cellBackgroundColor"
activeColor={editor.getAttributes("tableCell").backgroundColor}
title={"Cell background color"}
+ type="background"
onColorChange={(color) =>
editor.chain().setCellAttribute("backgroundColor", color).run()
}
@@ -244,6 +245,7 @@ export function CellTextColor(props: ToolProps) {
<ColorTool
{...props}
cacheKey="cellTextColor"
+ type="text"
activeColor={editor.getAttributes("tableCell").color}
title={"Cell text color"}
onColorChange={(color) =>
@@ -261,6 +263,7 @@ export function CellBorderColor(props: ToolProps) {
{...props}
cacheKey="cellBorderColor"
activeColor={editor.getAttributes("tableCell").borderColor}
+ type="border"
title={"Cell border color"}
onColorChange={(color) =>
editor.chain().focus().setCellAttribute("borderColor", color).run()
diff --git a/packages/editor/src/toolbar/types.ts b/packages/editor/src/toolbar/types.ts
index 8bfbc9a83..f47685284 100644
--- a/packages/editor/src/toolbar/types.ts
+++ b/packages/editor/src/toolbar/types.ts
@@ -26,6 +26,7 @@ export type ToolProps = ToolDefinition & {
editor: Editor;
variant?: ToolButtonVariant;
force?: boolean;
+ parentGroup?: string;
};
export type ToolDefinition = {
diff --git a/packages/editor/src/toolbar/utils/dom.ts b/packages/editor/src/toolbar/utils/dom.ts
index 3a45ed5d7..2010fed38 100644
--- a/packages/editor/src/toolbar/utils/dom.ts
+++ b/packages/editor/src/toolbar/utils/dom.ts
@@ -33,6 +33,11 @@ export function getPopupContainer() {
);
}
+export function getEditorToolbarPopup() {
+ return (document.querySelector(".editor-mobile-toolbar-popup") ||
+ getToolbarElement()) as HTMLElement;
+}
+
export function getEditorContainer() {
return (document.querySelector(".active .editor") ||
getPopupContainer()) as HTMLElement;