Skip to content

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

fix: improve color picker for mobile

thecodrrd5efcd95fd6c
diff --git a/packages/editor/dist/components/popuppresenter/index.js b/packages/editor/dist/components/popuppresenter/index.js
index 06bcc3541..d27cb0bc0 100644
--- a/packages/editor/dist/components/popuppresenter/index.js
+++ b/packages/editor/dist/components/popuppresenter/index.js
@@ -28,33 +28,34 @@ import Modal from "react-modal";
import ReactDOM from "react-dom";
import { ThemeProvider } from "emotion-theming";
import { getPopupContainer, getToolbarElement } from "../../toolbar/utils/dom";
-import { useToolbarStore } from "../../toolbar/stores/toolbar-store";
+import { useIsMobile, useToolbarStore, } from "../../toolbar/stores/toolbar-store";
import { EditorContext, usePopupRenderer, } from "./popuprenderer";
import { ResponsivePresenter } from "../responsive";
function _PopupPresenter(props) {
var isOpen = props.isOpen, position = props.position, onClose = props.onClose, _a = props.blocking, blocking = _a === void 0 ? true : _a, _b = props.focusOnRender, focusOnRender = _b === void 0 ? true : _b, children = props.children;
+ var isMobile = useIsMobile();
var contentRef = useRef();
var observerRef = useRef();
- var repositionPopup = useCallback(function (position) {
+ var repositionPopup = useCallback(function () {
if (!contentRef.current || !position)
return;
var popup = contentRef.current;
var popupPosition = getPosition(popup, position);
popup.style.top = popupPosition.top + "px";
popup.style.left = popupPosition.left + "px";
- }, []);
+ }, [position]);
useEffect(function () {
- repositionPopup(position);
+ repositionPopup();
}, [position]);
useEffect(function () {
function onWindowResize() {
- repositionPopup(position);
+ repositionPopup();
}
window.addEventListener("resize", onWindowResize);
return function () {
window.removeEventListener("resize", onWindowResize);
};
- }, [position]);
+ }, []);
var attachMoveHandlers = useCallback(function () {
if (!contentRef.current || !isOpen)
return;
@@ -90,32 +91,37 @@ function _PopupPresenter(props) {
return;
var oldHeight = popup.offsetHeight;
observerRef.current = new ResizeObserver(function (e) {
- var _a = popup.getBoundingClientRect(), height = _a.height, y = _a.y;
- var delta = height - oldHeight;
- if (delta > 0) {
- // means the new size is bigger so we need to adjust the position
- // if required. We only do this in case the newly resized popup
- // is going out of the window.
- var windowHeight = document.body.clientHeight - 20;
- if (y + height > windowHeight) {
- popup.style.top = windowHeight - height + "px";
+ if (isMobile) {
+ repositionPopup();
+ }
+ else {
+ var _a = popup.getBoundingClientRect(), height = _a.height, y = _a.y;
+ var delta = height - oldHeight;
+ if (delta > 0) {
+ // means the new size is bigger so we need to adjust the position
+ // if required. We only do this in case the newly resized popup
+ // is going out of the window.
+ var windowHeight = document.body.clientHeight - 20;
+ if (y + height > windowHeight) {
+ popup.style.top = windowHeight - height + "px";
+ }
}
+ oldHeight = height;
}
- oldHeight = height;
});
observerRef.current.observe(popup, { box: "border-box" });
- }, []);
+ }, [isMobile]);
return (_jsx(Modal, __assign({ contentRef: function (ref) { return (contentRef.current = ref); }, className: "popup-presenter", role: "menu", isOpen: isOpen, appElement: document.body, shouldCloseOnEsc: true, shouldReturnFocusAfterClose: true, shouldCloseOnOverlayClick: true, shouldFocusAfterRender: focusOnRender, ariaHideApp: blocking, preventScroll: blocking, onRequestClose: onClose, portalClassName: "popup-presenter-portal", onAfterOpen: function (obj) {
if (!obj || !position)
return;
- repositionPopup(position);
+ repositionPopup();
handleResize();
attachMoveHandlers();
}, onAfterClose: function () { var _a; return (_a = observerRef.current) === null || _a === void 0 ? void 0 : _a.disconnect(); }, overlayElement: function (props, contentEl) {
return (_jsx(Box, __assign({}, props, {
//@ts-ignore
style: __assign(__assign({}, props.style), { position: !blocking ? "initial" : "fixed", zIndex: 1000, backgroundColor: !blocking ? "transparent" : "unset" }) }, { children: contentEl })));
- }, contentElement: function (props, children) { return (_jsx(Box, __assign({}, props, { style: {}, sx: {
+ }, contentElement: function (props, children) { return (_jsx(Box, __assign({}, props, { style: {}, onMouseDown: function (e) { return e.preventDefault(); }, sx: {
top: 0,
left: 0,
right: 0,
diff --git a/packages/editor/dist/toolbar/popups/colorpicker.js b/packages/editor/dist/toolbar/popups/colorpicker.js
index 07e7ccc5b..fc2a628dc 100644
--- a/packages/editor/dist/toolbar/popups/colorpicker.js
+++ b/packages/editor/dist/toolbar/popups/colorpicker.js
@@ -30,7 +30,7 @@ import { Box, Flex, Text } from "rebass";
import { Input } from "@rebass/forms";
import { Icon } from "../components/icon";
import { Icons } from "../icons";
-import { useState } from "react";
+import { useEffect, useRef, useState } from "react";
import tinycolor from "tinycolor2";
import { HexColorPicker } from "react-colorful";
import { Button } from "../../components/button";
@@ -53,10 +53,17 @@ export var DEFAULT_COLORS = [
var PALETTE_SIZE = [35, 35, 25];
export function ColorPicker(props) {
var _a = props.colors, colors = _a === void 0 ? DEFAULT_COLORS : _a, color = props.color, onClear = props.onClear, onChange = props.onChange, title = props.title, onClose = props.onClose, expanded = props.expanded;
+ var ref = useRef();
var _b = __read(useState(expanded || false), 2), isPickerOpen = _b[0], setIsPickerOpen = _b[1];
- var tColor = tinycolor(color || colors[0]);
- var _c = __read(useState(tColor.toHexString()), 2), currentColor = _c[0], setCurrentColor = _c[1];
- return (_jsxs(Flex, __assign({ sx: {
+ var _c = __read(useState(tinycolor(color).toHexString()), 2), currentColor = _c[0], setCurrentColor = _c[1];
+ var tColor = tinycolor(currentColor);
+ useEffect(function () {
+ if (!ref.current)
+ return;
+ if (isPickerOpen)
+ ref.current.focus({ preventScroll: true });
+ }, [isPickerOpen]);
+ return (_jsxs(Flex, __assign({ ref: ref, tabIndex: -1, sx: {
bg: "background",
flexDirection: "column",
".react-colorful": {
@@ -75,7 +82,7 @@ export function ColorPicker(props) {
pb: isPickerOpen ? 2 : 0,
//pb: 0,
alignItems: "center",
- }, onClick: onClose }, { children: [_jsx(Text, __assign({ variant: "title" }, { children: title })), _jsx(Button, __assign({ variant: "icon", sx: { p: 0 } }, { children: _jsx(Icon, { path: Icons.close, size: "big" }) }))] }))), isPickerOpen ? (_jsxs(_Fragment, { children: [_jsx(HexColorPicker, { color: currentColor, onChange: onChange }), _jsx(Input, { variant: "clean", placeholder: "#000000", spellCheck: false, sx: {
+ }, onClick: onClose }, { children: [_jsx(Text, __assign({ variant: "title" }, { children: title })), _jsx(Button, __assign({ variant: "icon", sx: { p: 0 } }, { children: _jsx(Icon, { path: Icons.close, size: "big" }) }))] }))), isPickerOpen ? (_jsxs(_Fragment, { children: [_jsx(HexColorPicker, { onChange: function (color) { return setCurrentColor(color); }, onTouchEnd: function () { return onChange(currentColor); }, onMouseUp: function () { return onChange(currentColor); } }), _jsx(Input, { variant: "clean", placeholder: "#000000", spellCheck: false, sx: {
my: 2,
p: 0,
borderRadius: 0,
@@ -87,7 +94,10 @@ export function ColorPicker(props) {
var value = e.target.value;
if (!value)
return;
- setCurrentColor(value);
+ if (tinycolor(value, { format: "hex" }).isValid()) {
+ setCurrentColor(value);
+ onChange(value);
+ }
} })] })) : null, _jsxs(Flex, { children: [_jsxs(Flex, __assign({ className: "hide-scrollbar", sx: {
flex: 1,
p: 1,
diff --git a/packages/editor/dist/utils/debounce.d.ts b/packages/editor/dist/utils/debounce.d.ts
new file mode 100644
index 000000000..aa5951c17
--- /dev/null
+++ b/packages/editor/dist/utils/debounce.d.ts
@@ -0,0 +1 @@
+export declare function debounce<F extends (...args: any) => any>(func: F, waitFor: number): (...args: Parameters<F>) => ReturnType<F>;
diff --git a/packages/editor/dist/utils/debounce.js b/packages/editor/dist/utils/debounce.js
new file mode 100644
index 000000000..a2e0f917e
--- /dev/null
+++ b/packages/editor/dist/utils/debounce.js
@@ -0,0 +1,38 @@
+var __read = (this && this.__read) || function (o, n) {
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
+ if (!m) return o;
+ var i = m.call(o), r, ar = [], e;
+ try {
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
+ }
+ catch (error) { e = { error: error }; }
+ finally {
+ try {
+ if (r && !r.done && (m = i["return"])) m.call(i);
+ }
+ finally { if (e) throw e.error; }
+ }
+ return ar;
+};
+var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
+ if (ar || !(i in from)) {
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
+ ar[i] = from[i];
+ }
+ }
+ return to.concat(ar || Array.prototype.slice.call(from));
+};
+export function debounce(func, waitFor) {
+ var timeout;
+ var debounced = function () {
+ var args = [];
+ for (var _i = 0; _i < arguments.length; _i++) {
+ args[_i] = arguments[_i];
+ }
+ if (timeout)
+ clearTimeout(timeout);
+ timeout = setTimeout(function () { return func.apply(void 0, __spreadArray([], __read(args), false)); }, waitFor);
+ };
+ return debounced;
+}
diff --git a/packages/editor/src/components/popup-presenter/index.tsx b/packages/editor/src/components/popup-presenter/index.tsx
index 254efe811..565449527 100644
--- a/packages/editor/src/components/popup-presenter/index.tsx
+++ b/packages/editor/src/components/popup-presenter/index.tsx
@@ -13,7 +13,10 @@ import { Editor } from "@tiptap/core";
import { ThemeProvider } from "emotion-theming";
import { getPopupContainer, getToolbarElement } from "../../toolbar/utils/dom";
import { Theme } from "@notesnook/theme";
-import { useToolbarStore } from "../../toolbar/stores/toolbar-store";
+import {
+ useIsMobile,
+ useToolbarStore,
+} from "../../toolbar/stores/toolbar-store";
import React from "react";
import {
EditorContext,
@@ -41,30 +44,31 @@ function _PopupPresenter(props: PropsWithChildren<PopupPresenterProps>) {
children,
} = props;
+ const isMobile = useIsMobile();
const contentRef = useRef<HTMLDivElement>();
const observerRef = useRef<ResizeObserver>();
- const repositionPopup = useCallback((position: PositionOptions) => {
+ const repositionPopup = useCallback(() => {
if (!contentRef.current || !position) return;
const popup = contentRef.current;
const popupPosition = getPosition(popup, position);
popup.style.top = popupPosition.top + "px";
popup.style.left = popupPosition.left + "px";
- }, []);
+ }, [position]);
useEffect(() => {
- repositionPopup(position);
+ repositionPopup();
}, [position]);
useEffect(() => {
function onWindowResize() {
- repositionPopup(position);
+ repositionPopup();
}
window.addEventListener("resize", onWindowResize);
return () => {
window.removeEventListener("resize", onWindowResize);
};
- }, [position]);
+ }, []);
const attachMoveHandlers = useCallback(() => {
if (!contentRef.current || !isOpen) return;
@@ -106,22 +110,26 @@ function _PopupPresenter(props: PropsWithChildren<PopupPresenterProps>) {
let oldHeight: number = popup.offsetHeight;
observerRef.current = new ResizeObserver((e) => {
- const { height, y } = popup.getBoundingClientRect();
- const delta = height - oldHeight;
- if (delta > 0) {
- // means the new size is bigger so we need to adjust the position
- // if required. We only do this in case the newly resized popup
- // is going out of the window.
+ if (isMobile) {
+ repositionPopup();
+ } else {
+ const { height, y } = popup.getBoundingClientRect();
+ const delta = height - oldHeight;
+ if (delta > 0) {
+ // means the new size is bigger so we need to adjust the position
+ // if required. We only do this in case the newly resized popup
+ // is going out of the window.
- const windowHeight = document.body.clientHeight - 20;
- if (y + height > windowHeight) {
- popup.style.top = windowHeight - height + "px";
+ const windowHeight = document.body.clientHeight - 20;
+ if (y + height > windowHeight) {
+ popup.style.top = windowHeight - height + "px";
+ }
}
+ oldHeight = height;
}
- oldHeight = height;
});
observerRef.current.observe(popup, { box: "border-box" });
- }, []);
+ }, [isMobile]);
return (
<Modal
@@ -140,7 +148,7 @@ function _PopupPresenter(props: PropsWithChildren<PopupPresenterProps>) {
portalClassName={"popup-presenter-portal"}
onAfterOpen={(obj) => {
if (!obj || !position) return;
- repositionPopup(position);
+ repositionPopup();
handleResize();
attachMoveHandlers();
@@ -176,6 +184,7 @@ function _PopupPresenter(props: PropsWithChildren<PopupPresenterProps>) {
<Box
{...props}
style={{}}
+ onMouseDown={(e) => e.preventDefault()}
sx={{
top: 0,
left: 0,
diff --git a/packages/editor/src/toolbar/popups/color-picker.tsx b/packages/editor/src/toolbar/popups/color-picker.tsx
index eb32ebc1a..fb67d2367 100644
--- a/packages/editor/src/toolbar/popups/color-picker.tsx
+++ b/packages/editor/src/toolbar/popups/color-picker.tsx
@@ -2,10 +2,11 @@ import { Box, Flex, Text } from "rebass";
import { Input } from "@rebass/forms";
import { Icon } from "../components/icon";
import { Icons } from "../icons";
-import { useState } from "react";
+import { useEffect, useRef, useState } from "react";
import tinycolor from "tinycolor2";
import { HexColorPicker } from "react-colorful";
import { Button } from "../../components/button";
+import { debounce } from "../../utils/debounce";
export const DEFAULT_COLORS = [
"#e91e63",
"#9c27b0",
@@ -43,14 +44,22 @@ export function ColorPicker(props: ColorPickerProps) {
onClose,
expanded,
} = props;
+ const ref = useRef<HTMLDivElement>();
const [isPickerOpen, setIsPickerOpen] = useState(expanded || false);
- const tColor = tinycolor(color || colors[0]);
const [currentColor, setCurrentColor] = useState<string>(
- tColor.toHexString()
+ tinycolor(color).toHexString()
);
+ const tColor = tinycolor(currentColor);
+
+ useEffect(() => {
+ if (!ref.current) return;
+ if (isPickerOpen) ref.current.focus({ preventScroll: true });
+ }, [isPickerOpen]);
return (
<Flex
+ ref={ref}
+ tabIndex={-1}
sx={{
bg: "background",
flexDirection: "column",
@@ -86,7 +95,11 @@ export function ColorPicker(props: ColorPickerProps) {
{isPickerOpen ? (
<>
- <HexColorPicker color={currentColor} onChange={onChange} />
+ <HexColorPicker
+ onChange={(color) => setCurrentColor(color)}
+ onTouchEnd={() => onChange(currentColor)}
+ onMouseUp={() => onChange(currentColor)}
+ />
<Input
variant={"clean"}
placeholder="#000000"
@@ -105,7 +118,10 @@ export function ColorPicker(props: ColorPickerProps) {
onChange={(e) => {
const { value } = e.target;
if (!value) return;
- setCurrentColor(value);
+ if (tinycolor(value, { format: "hex" }).isValid()) {
+ setCurrentColor(value);
+ onChange(value);
+ }
}}
/>
</>
diff --git a/packages/editor/src/utils/debounce.ts b/packages/editor/src/utils/debounce.ts
new file mode 100644
index 000000000..1b871f102
--- /dev/null
+++ b/packages/editor/src/utils/debounce.ts
@@ -0,0 +1,13 @@
+export function debounce<F extends (...args: any) => any>(
+ func: F,
+ waitFor: number
+) {
+ let timeout: NodeJS.Timeout | null;
+
+ const debounced = (...args: any) => {
+ if (timeout) clearTimeout(timeout);
+ timeout = setTimeout(() => func(...args), waitFor);
+ };
+
+ return debounced as (...args: Parameters<F>) => ReturnType<F>;
+}