Skip to content

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

editor: saving a color now updates the color list

Abdullah Atta808270d2ef0b
diff --git a/packages/editor/src/components/popup-presenter/index.tsx b/packages/editor/src/components/popup-presenter/index.tsx
index e6bd52b64..37fd8ec6d 100644
--- a/packages/editor/src/components/popup-presenter/index.tsx
+++ b/packages/editor/src/components/popup-presenter/index.tsx
@@ -28,7 +28,7 @@ import {
useToolbarStore
} from "../../toolbar/stores/toolbar-store";
import React from "react";
-import { EditorContext, usePopupRenderer } from "./popuprenderer";
+import { usePopupRenderer } from "./popuprenderer";
import { ResponsivePresenter, ResponsivePresenterProps } from "../responsive";
import { ThemeProvider } from "../theme-provider";
@@ -237,10 +237,9 @@ export function PopupPresenter(props: PropsWithChildren<PopupPresenterProps>) {
export type PopupWrapperProps = UsePopupHandlerOptions & {
autoCloseOnUnmount?: boolean;
position: PositionOptions;
- renderPopup: (closePopup: () => void) => React.ReactNode;
} & Partial<Omit<PopupPresenterProps, "onClose">>;
-export function PopupWrapper(props: PopupWrapperProps) {
- const { id, position, renderPopup, autoCloseOnUnmount, ...presenterProps } =
+export function PopupWrapper(props: PropsWithChildren<PopupWrapperProps>) {
+ const { id, position, children, autoCloseOnUnmount, ...presenterProps } =
props;
const PopupRenderer = usePopupRenderer();
const { closePopup, isPopupOpen } = usePopupHandler(props);
@@ -252,58 +251,27 @@ export function PopupWrapper(props: PopupWrapperProps) {
};
}, [autoCloseOnUnmount, id, PopupRenderer]);
- useEffect(() => {
- if (PopupRenderer && isPopupOpen && !PopupRenderer.isOpen(id)) {
- PopupRenderer.openPopup(id, function Popup({ id }) {
- const isPopupOpen = useToolbarStore(
- (store) => !!store.openedPopups[id]
- );
-
- useEffect(() => {
- if (!isPopupOpen) {
- PopupRenderer.closePopup(id);
- }
- }, [id, isPopupOpen]);
-
- return (
- <PopupPresenter
- key={id}
- onClose={() => closePopup(id)}
- position={position}
- blocking
- focusOnRender
- {...presenterProps}
- isOpen={isPopupOpen}
- >
- <Box
- sx={{
- boxShadow: "menu",
- borderRadius: "default",
- overflow: "hidden"
- // width,
- }}
- >
- <EditorContext.Consumer>
- {() => {
- return renderPopup(() => PopupRenderer.closePopup(id));
- }}
- </EditorContext.Consumer>
- </Box>
- </PopupPresenter>
- );
- });
- }
- }, [
- PopupRenderer,
- isPopupOpen,
- closePopup,
- id,
- position,
- presenterProps,
- renderPopup
- ]);
-
- return null;
+ return (
+ <PopupPresenter
+ key={id}
+ onClose={() => closePopup(id)}
+ position={position}
+ blocking
+ focusOnRender
+ {...presenterProps}
+ isOpen={isPopupOpen}
+ >
+ <Box
+ sx={{
+ boxShadow: "menu",
+ borderRadius: "default",
+ overflow: "hidden"
+ }}
+ >
+ {children}
+ </Box>
+ </PopupPresenter>
+ );
}
type UsePopupHandlerOptions = {
diff --git a/packages/editor/src/toolbar/components/more-tools.tsx b/packages/editor/src/toolbar/components/more-tools.tsx
index b2c2c8b59..167e1ddb9 100644
--- a/packages/editor/src/toolbar/components/more-tools.tsx
+++ b/packages/editor/src/toolbar/components/more-tools.tsx
@@ -63,25 +63,24 @@ export function MoreTools(props: MoreToolsProps) {
autoCloseOnUnmount={autoCloseOnUnmount}
focusOnRender={false}
blocking={false}
- renderPopup={() => (
- <ToolbarGroup
- tools={tools}
- editor={editor}
- sx={{
- flex: 1,
- // this is intentionally set to a fixed value
- // because we want the same padding on mobile
- // and web.
- p: "5px",
- boxShadow: "menu",
- bg: "background",
- borderRadius: "default",
- overflowX: "auto",
- maxWidth: "95vw"
- }}
- />
- )}
- />
+ >
+ <ToolbarGroup
+ tools={tools}
+ editor={editor}
+ sx={{
+ flex: 1,
+ // this is intentionally set to a fixed value
+ // because we want the same padding on mobile
+ // and web.
+ p: "5px",
+ boxShadow: "menu",
+ bg: "background",
+ borderRadius: "default",
+ overflowX: "auto",
+ maxWidth: "95vw"
+ }}
+ />
+ </PopupWrapper>
</>
);
}
diff --git a/packages/editor/src/toolbar/components/split-button.tsx b/packages/editor/src/toolbar/components/split-button.tsx
index 72d2b0c54..8153b8795 100644
--- a/packages/editor/src/toolbar/components/split-button.tsx
+++ b/packages/editor/src/toolbar/components/split-button.tsx
@@ -78,6 +78,7 @@ function _SplitButton(props: PropsWithChildren<SplitButtonProps>) {
export const SplitButton = React.memo(_SplitButton, (prev, next) => {
return (
prev.toggled === next.toggled &&
- JSON.stringify(prev.sx) === JSON.stringify(next.sx)
+ JSON.stringify(prev.sx) === JSON.stringify(next.sx) &&
+ prev.children === next.children
);
});
diff --git a/packages/editor/src/toolbar/icons.ts b/packages/editor/src/toolbar/icons.ts
index 4d4c40c55..42418067e 100644
--- a/packages/editor/src/toolbar/icons.ts
+++ b/packages/editor/src/toolbar/icons.ts
@@ -105,7 +105,9 @@ import {
mdiResizeBottomRight,
mdiImageOutline,
mdiProgressDownload,
- mdiProgressAlert
+ mdiProgressAlert,
+ mdiContentSaveOutline,
+ mdiClipboardOutline
} from "@mdi/js";
export const Icons = {
@@ -146,6 +148,8 @@ export const Icons = {
imageFailed: mdiProgressAlert,
imageSettings: mdiImageEditOutline,
colorClear: mdiInvertColorsOff,
+ save: mdiContentSaveOutline,
+ copy: mdiClipboardOutline,
check: mdiCheck,
checkbox: mdiCheckboxMarkedOutline,
loading: mdiLoading,
diff --git a/packages/editor/src/toolbar/popups/color-picker.tsx b/packages/editor/src/toolbar/popups/color-picker.tsx
index d234a15ff..c6209113a 100644
--- a/packages/editor/src/toolbar/popups/color-picker.tsx
+++ b/packages/editor/src/toolbar/popups/color-picker.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 { Box, Flex, Text } from "@theme-ui/components";
+import { ButtonProps, Flex } from "@theme-ui/components";
import { Input } from "@theme-ui/components";
import { Icon } from "../components/icon";
import { Icons } from "../icons";
@@ -26,7 +26,9 @@ import tinycolor from "tinycolor2";
import { HexColorPicker } from "react-colorful";
import { Button } from "../../components/button";
import { debounce } from "../../utils/debounce";
-import { config } from "../../utils/config";
+import { Popup } from "../components/popup";
+import { SchemeColors } from "@notesnook/theme/dist/theme/colorscheme";
+
export const DEFAULT_COLORS = [
"#e91e63",
"#9c27b0",
@@ -45,6 +47,7 @@ export const DEFAULT_COLORS = [
];
type ColorPickerProps = {
+ colors?: string[];
color?: string;
onClear: () => void;
expanded?: boolean;
@@ -53,8 +56,7 @@ type ColorPickerProps = {
title?: string;
onSave?: (color: string) => void;
cacheKey?: string;
- colors?: string[];
- onColorDelete?: (color: string) => void;
+ onDelete?: (color: string) => void;
};
const PALETTE_SIZE = [35, 35, 25];
export function ColorPicker(props: ColorPickerProps) {
@@ -66,17 +68,17 @@ export function ColorPicker(props: ColorPickerProps) {
onClose,
expanded,
onSave,
- colors,
- onColorDelete
+ colors = [],
+ onDelete
} = props;
- console.log("color-picker", colors);
const ref = useRef<HTMLDivElement>(null);
const [isPickerOpen, setIsPickerOpen] = useState(expanded || false);
const [currentColor, setCurrentColor] = useState<string>(
tinycolor(color || colors?.[0]).toHexString()
);
- const [trashMode, setTrashMode] = useState(false);
+ const [deleteMode, setDeleteMode] = useState(false);
const tColor = tinycolor(currentColor);
+ const allColors = deleteMode ? colors : [...DEFAULT_COLORS, ...colors];
useEffect(() => {
if (!ref.current) return;
@@ -92,202 +94,190 @@ export function ColorPicker(props: ColorPickerProps) {
);
return (
- <Flex
- ref={ref}
- tabIndex={-1}
- sx={{
- bg: "background",
- flexDirection: "column",
- ".react-colorful": {
- width: "auto",
- height: 150
- },
- ".react-colorful__saturation": {
- borderRadius: ["default", 0]
- },
- width: ["calc(100vw - 20px)", 250]
- // width: ["auto", 250],
- }}
- >
- {onClose && (
- <Box
- sx={{
- display: ["none", "flex"],
- justifyContent: "space-between",
- p: 2,
- pb: isPickerOpen ? 2 : 0,
- //pb: 0,
- alignItems: "center"
- }}
- onClick={onClose}
- >
- <Text variant={"title"}>{title}</Text>
- <Button variant={"icon"} sx={{ p: 0 }}>
- <Icon path={Icons.close} size="big" />
- </Button>
- </Box>
- )}
-
- {isPickerOpen ? (
- <>
- <HexColorPicker
- onChange={(color) => {
- setCurrentColor(color);
- onColorChange(color);
- }}
- onTouchEnd={() => onChange(currentColor)}
- onMouseUp={() => onChange(currentColor)}
- />
- <Flex sx={{ flexDirection: "row" }}>
- <Input
- variant={"clean"}
- placeholder="#000000"
- spellCheck={false}
- sx={{
- my: 2,
- p: 0,
- borderRadius: 0,
- fontSize: ["title", "title", "body"],
- color: "fontTertiary",
- textAlign: "center",
- letterSpacing: 1.5
- }}
- value={currentColor.toUpperCase()}
- maxLength={7}
- onChange={(e) => {
- const { value } = e.target;
- if (!value) return;
- if (tinycolor(value, { format: "hex" }).isValid()) {
- setCurrentColor(value);
- onChange(value);
- }
+ <Popup title={title} onClose={onClose}>
+ <Flex
+ ref={ref}
+ tabIndex={-1}
+ sx={{
+ bg: "background",
+ flexDirection: "column",
+ ".react-colorful": {
+ width: "auto",
+ height: 150
+ },
+ ".react-colorful__saturation": {
+ borderRadius: ["default", 0]
+ },
+ width: ["calc(100vw - 20px)", 250]
+ // width: ["auto", 250],
+ }}
+ >
+ {isPickerOpen ? (
+ <>
+ <HexColorPicker
+ color={currentColor}
+ onChange={(color) => {
+ setCurrentColor(color);
+ onColorChange(color);
}}
+ onTouchEnd={() => onChange(currentColor)}
+ onMouseUp={() => onChange(currentColor)}
/>
- <Button
+ <Flex
sx={{
- width: "50%",
- flexShrink: 0,
- p: 0,
- fontSize: ["title", "title", "body"],
- fontWeight: 800,
- color: `${currentColor}`,
- bg: "transparent",
- textAlign: "center"
+ flexDirection: "row",
+ alignItems: "center",
+ justifyContent: "center"
}}
- onClick={() => onSave?.(currentColor)}
>
- Save Color
- </Button>
- </Flex>
- </>
- ) : null}
- <Flex>
- <Flex
- className="hide-scrollbar"
- sx={{
- flex: 1,
- p: 1,
- overflowX: ["auto", "hidden"],
- flexWrap: ["nowrap", "wrap"]
- }}
- >
- <Button
- variant={"icon"}
+ <Input
+ variant={"clean"}
+ placeholder="#000000"
+ spellCheck={false}
+ sx={{
+ my: 2,
+ p: 0,
+ borderRadius: 0,
+ fontSize: ["title", "title", "body"],
+ color: "fontTertiary",
+ width: [75, 75, 65],
+ letterSpacing: 1.5,
+ textAlign: "center"
+ }}
+ value={currentColor.toUpperCase()}
+ maxLength={7}
+ onChange={(e) => {
+ const { value } = e.target;
+ if (!value) return;
+ if (tinycolor(value, { format: "hex" }).isValid()) {
+ setCurrentColor(value);
+ onChange(value);
+ }
+ }}
+ />
+ {onSave && (
+ <PaletteButton
+ sx={{
+ boxShadow: "none"
+ }}
+ disabled={allColors.includes(currentColor)}
+ icon={Icons.save}
+ iconSize={18}
+ onClick={() => onSave(currentColor)}
+ title="Save color"
+ />
+ )}
+ </Flex>
+ </>
+ ) : null}
+ <Flex>
+ <Flex
+ className="hide-scrollbar"
sx={{
- flexShrink: 0,
- width: PALETTE_SIZE,
- height: PALETTE_SIZE,
- borderRadius: 50,
- boxShadow: "menu",
- p: 0,
- ml: [2, 2, 1]
+ flex: 1,
+ p: 1,
+ overflowX: ["auto", "hidden"],
+ flexWrap: ["nowrap", "wrap"],
+ maxHeight: ["auto", 100],
+ overflowY: ["hidden", "auto"]
}}
- onClick={onClear}
>
- <Icon path={Icons.colorClear} color="text" size={15} />
- </Button>
- <Button
- variant={"secondary"}
- sx={{
- flexShrink: 0,
- bg: currentColor,
- width: PALETTE_SIZE,
- height: PALETTE_SIZE,
- borderRadius: 50,
- // boxShadow: "menu",
- p: 0,
- ml: [2, 2, 1],
- ":hover": {
- bg: currentColor
+ {!deleteMode && (
+ <PaletteButton
+ icon={Icons.colorClear}
+ iconColor="text"
+ onClick={onClear}
+ title="Clear color"
+ iconSize={15}
+ />
+ )}
+ <PaletteButton
+ icon={Icons.delete}
+ iconColor={deleteMode ? "error" : "icon"}
+ bg={deleteMode ? "errorBg" : undefined}
+ onClick={() => setDeleteMode((s) => !s)}
+ title={
+ deleteMode
+ ? "Disable delete mode"
+ : "Enable delete mode for deleting custom colors"
}
- }}
- onClick={() => setIsPickerOpen((s) => !s)}
- >
- <Icon
- path={Icons.palette}
- color={tColor.isDark() ? "static" : "icon"}
- size={18}
+ iconSize={18}
/>
- </Button>
- <Button
- variant={"icon"}
- sx={{
- flexShrink: 0,
- width: PALETTE_SIZE,
- height: PALETTE_SIZE,
- bg: trashMode ? "primary" : "transparent",
- borderRadius: 50,
- boxShadow: "menu",
- p: 0,
- ml: [2, 2, 1],
- ":hover": {
- bg: trashMode ? "primary" : "transparent"
- }
- }}
- onClick={() => setTrashMode(!trashMode)}
- >
- <Icon
- path={Icons.delete}
- color={trashMode ? "accent" : "text"}
- size={20}
- />
- </Button>
- {[...DEFAULT_COLORS, ...(colors || [])].map((colorItem) => (
- <Button
- key={colorItem}
- sx={{
- flex: "0 0 auto",
- bg: colorItem,
- width: PALETTE_SIZE,
- height: PALETTE_SIZE,
- ml: [2, 1, 1],
- mb: [0, 1, 1],
- borderRadius: 50,
- ":hover": {
- bg: colorItem
- }
- }}
- onClick={() => {
- if (trashMode) {
- onColorDelete?.(colorItem);
- return;
+ {!deleteMode && (
+ <PaletteButton
+ icon={Icons.palette}
+ iconColor={tColor.isDark() ? "static" : "icon"}
+ onClick={() => setIsPickerOpen((s) => !s)}
+ title="Choose custom color"
+ iconSize={18}
+ bg={currentColor}
+ />
+ )}
+ {allColors.map((colorItem) => (
+ <PaletteButton
+ key={colorItem}
+ title={deleteMode ? "Click to delete this color" : colorItem}
+ bg={colorItem}
+ iconSize={15}
+ iconColor={"static"}
+ icon={
+ deleteMode && colors.includes(colorItem)
+ ? Icons.close
+ : undefined
}
- setCurrentColor(colorItem);
- onChange(colorItem);
- }}
- />
- ))}
+ onClick={() => {
+ if (deleteMode) {
+ onDelete?.(colorItem);
+ return;
+ }
+ setCurrentColor(colorItem);
+ onChange(colorItem);
+ }}
+ sx={{ mb: [0, 1, 1] }}
+ />
+ ))}
+ </Flex>
+ {onClose && (
+ <Button
+ variant={"icon"}
+ sx={{ display: ["block", "none"], px: 2 }}
+ onClick={onClose}
+ >
+ <Icon path={Icons.close} size="big" />
+ </Button>
+ )}
</Flex>
- {onClose && (
- <Button
- variant={"icon"}
- sx={{ display: ["block", "none"], px: 2 }}
- onClick={onClose}
- >
- <Icon path={Icons.close} size="big" />
- </Button>
- )}
</Flex>
- </Flex>
+ </Popup>
+ );
+}
+
+type PaletteButtonProps = ButtonProps & {
+ icon?: string;
+ iconColor?: keyof SchemeColors;
+ iconSize?: number;
+ bg?: string;
+};
+function PaletteButton(props: PaletteButtonProps) {
+ const { sx, iconColor, icon, iconSize = 20, bg, ...restProps } = props;
+ return (
+ <Button
+ variant={"icon"}
+ sx={{
+ flexShrink: 0,
+ width: PALETTE_SIZE,
+ height: PALETTE_SIZE,
+ borderRadius: 50,
+ boxShadow: "menu",
+ p: 0,
+ ml: [2, 2, 1],
+ bg,
+ ":hover": { bg },
+ ...sx
+ }}
+ {...restProps}
+ >
+ {icon && <Icon path={icon} color={iconColor} size={iconSize} />}
+ </Button>
);
}
diff --git a/packages/editor/src/toolbar/tools/colors.tsx b/packages/editor/src/toolbar/tools/colors.tsx
index 8dd1a9e7c..5a3a62b93 100644
--- a/packages/editor/src/toolbar/tools/colors.tsx
+++ b/packages/editor/src/toolbar/tools/colors.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 { useCallback, useEffect, useState } from "react";
+import { useEffect, useState } from "react";
import tinycolor from "tinycolor2";
import { PopupWrapper } from "../../components/popup-presenter";
import { config } from "../../utils/config";
@@ -46,12 +46,13 @@ export function ColorTool(props: ColorToolProps) {
const tColor = tinycolor(activeColor);
const isBottom = useToolbarLocation() === "bottom";
const [isOpen, setIsOpen] = useState<boolean>(false);
- const [colors, setColors] = useState<string[]>([]);
- console.log("colors 50", colors);
+ const [colors, setColors] = useState(
+ config.get<string[]>(`custom_${cacheKey}`, []) || []
+ );
useEffect(() => {
- setColors(config.get<string[]>(`custom_${cacheKey}`, []) || []);
- }, []);
+ config.set(`custom_${cacheKey}`, colors);
+ }, [cacheKey, colors]);
return (
<SplitButton
@@ -81,42 +82,32 @@ export function ColorTool(props: ColorToolProps) {
}}
focusOnRender={false}
blocking={false}
- renderPopup={(close) => (
- <ColorPicker
- color={activeColor}
- colors={colors}
- onColorDelete={(color) => {
- if (!DEFAULT_COLORS.includes(color)) {
- setColors((colors) => {
- const newColors = colors.filter((item) => item !== color);
- config.set(`custom_${cacheKey}`, newColors);
- return newColors;
- });
- }
- }}
- onClear={() => {
- onColorChange();
- config.set(cacheKey, null);
- }}
- onSave={(currentColor) => {
- if (!activeColor) return;
- if (colors?.includes(currentColor)) return;
- if (DEFAULT_COLORS.includes(currentColor)) return;
- setColors((colors) => {
- config.set(`custom_${cacheKey}`, [...colors, currentColor]);
- return [...colors, currentColor];
- });
- }}
- cacheKey={`custom_${cacheKey}`}
- onChange={(color) => {
- onColorChange(color);
- config.set(cacheKey, color);
- }}
- onClose={close}
- title={title}
- />
- )}
- />
+ >
+ <ColorPicker
+ color={activeColor}
+ colors={colors}
+ onDelete={(color) => {
+ if (DEFAULT_COLORS.includes(color)) return;
+ setColors((colors) => colors.filter((item) => item !== color));
+ }}
+ onClear={() => {
+ onColorChange();
+ config.set(cacheKey, null);
+ }}
+ onSave={(color) => {
+ setColors((colors) =>
+ colors.includes(color) ? colors : [...colors, color]
+ );
+ }}
+ cacheKey={`custom_${cacheKey}`}
+ onChange={(color) => {
+ onColorChange(color);
+ config.set(cacheKey, color);
+ }}
+ onClose={() => setIsOpen(false)}
+ title={title}
+ />
+ </PopupWrapper>
</SplitButton>
);
}
diff --git a/packages/editor/src/toolbar/tools/lists.tsx b/packages/editor/src/toolbar/tools/lists.tsx
index b96a5d50b..c1a75f9a6 100644
--- a/packages/editor/src/toolbar/tools/lists.tsx
+++ b/packages/editor/src/toolbar/tools/lists.tsx
@@ -75,40 +75,39 @@ function _ListTool<TListStyleTypes extends string>(
yOffset: 10
}}
onClosed={() => setIsOpen(false)}
- renderPopup={() => (
- <Box
- sx={{
- bg: "background",
- display: "grid",
- gridTemplateColumns: "1fr 1fr 1fr",
- p: 1
- }}
- >
- {subTypes.map((item) => (
- <Button
- key={item.title}
- variant={"menuitem"}
- sx={{ width: 80 }}
- onClick={() => {
- let chain = editor.current?.chain().focus();
- if (!chain || !editor.current) return;
+ >
+ <Box
+ sx={{
+ bg: "background",
+ display: "grid",
+ gridTemplateColumns: "1fr 1fr 1fr",
+ p: 1
+ }}
+ >
+ {subTypes.map((item) => (
+ <Button
+ key={item.title}
+ variant={"menuitem"}
+ sx={{ width: 80 }}
+ onClick={() => {
+ let chain = editor.current?.chain().focus();
+ if (!chain || !editor.current) return;
- if (!isListActive(editor.current)) {
- if (type === "bulletList") chain = chain.toggleBulletList();
- else chain = chain.toggleOrderedList();
- }
+ if (!isListActive(editor.current)) {
+ if (type === "bulletList") chain = chain.toggleBulletList();
+ else chain = chain.toggleOrderedList();
+ }
- return chain
- .updateAttributes(type, { listType: item.type })
- .run();
- }}
- >
- <ListThumbnail listStyleType={item.type} />
- </Button>
- ))}
- </Box>
- )}
- />
+ return chain
+ .updateAttributes(type, { listType: item.type })
+ .run();
+ }}
+ >
+ <ListThumbnail listStyleType={item.type} />
+ </Button>
+ ))}
+ </Box>
+ </PopupWrapper>
</SplitButton>
);
}