A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
editor: extract evaluating click bounds for collapsible nodes into a util Signed-off-by: 01zulfi <[email protected]>
01zulfi8f25f9ba3e7a
diff --git a/packages/editor/src/extensions/callout/callout.ts b/packages/editor/src/extensions/callout/callout.tsindex 6eccfc01a..0d66be1f2 100644--- a/packages/editor/src/extensions/callout/callout.ts+++ b/packages/editor/src/extensions/callout/callout.ts@@ -16,13 +16,11 @@ GNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.*/-import { getParentAttributes } from "../../utils/prosemirror.js";import {- InputRule,- Node,- findParentNodeClosestToPos,- mergeAttributes-} from "@tiptap/core";+ getParentAttributes,+ isClickWithinBounds+} from "../../utils/prosemirror.js";+import { InputRule, Node, mergeAttributes } from "@tiptap/core";import { Paragraph } from "../paragraph/index.js";import { Heading } from "../heading/index.js";import { TextSelection } from "@tiptap/pm/state";@@ -231,37 +229,9 @@ export const Callout = Node.create({const pos = typeof getPos === "function" ? getPos() : 0;if (typeof pos !== "number") return;- const resolvedPos = editor.state.doc.resolve(pos);-- const { x, y, width } = e.target.getBoundingClientRect();-- const clientX =- e instanceof MouseEvent ? e.clientX : e.touches[0].clientX;-- const clientY =- e instanceof MouseEvent ? e.clientY : e.touches[0].clientY;-- const hitArea = { width: 40, height: 40 };-- const isRtl =- e.target.dir === "rtl" ||- findParentNodeClosestToPos(- resolvedPos,- (node) => !!node.attrs.textDirection- )?.node.attrs.textDirection === "rtl";- let xEnd = clientX <= x + width;- let xStart = clientX >= x + width - hitArea.width;-- const yStart = clientY >= y;- const yEnd = clientY <= y + hitArea.height;-- if (isRtl) {- xStart = clientX >= x;- xEnd = clientX <= x + hitArea.width;- }-- if (xStart && xEnd && yStart && yEnd) {+ const resolvedPos = editor.state.doc.resolve(pos);+ if (isClickWithinBounds(e, resolvedPos, "right")) {e.preventDefault();e.stopImmediatePropagation();diff --git a/packages/editor/src/extensions/heading/heading.ts b/packages/editor/src/extensions/heading/heading.tsindex 5915553e2..61062a99d 100644--- a/packages/editor/src/extensions/heading/heading.ts+++ b/packages/editor/src/extensions/heading/heading.ts@@ -25,6 +25,7 @@ import {import { Heading as TiptapHeading } from "@tiptap/extension-heading";import { Plugin, PluginKey } from "@tiptap/pm/state";import { Decoration, DecorationSet } from "@tiptap/pm/view";+import { isClickWithinBounds } from "../../utils/prosemirror";const HEADING_REGEX = /^(#{1,6})\s$/;export const Heading = TiptapHeading.extend({@@ -169,33 +170,7 @@ export const Heading = TiptapHeading.extend({);if (calloutAncestor) return;- const { x, y, right } = heading.getBoundingClientRect();-- const clientX =- e instanceof MouseEvent ? e.clientX : e.touches[0].clientX;- const clientY =- e instanceof MouseEvent ? e.clientY : e.touches[0].clientY;-- const hitArea = { width: 40, height: 40 };-- const isRtl =- heading.dir === "rtl" ||- findParentNodeClosestToPos(- resolvedPos,- (node) => !!node.attrs.textDirection- )?.node.attrs.textDirection === "rtl";-- let xStart = clientX >= x - hitArea.width;- let xEnd = clientX <= x;- const yStart = clientY >= y;- const yEnd = clientY <= y + hitArea.height;-- if (isRtl) {- xEnd = clientX <= x + hitArea.width;- xStart = clientX >= right;- }-- if (xStart && xEnd && yStart && yEnd) {+ if (isClickWithinBounds(e, resolvedPos, "left")) {e.preventDefault();e.stopImmediatePropagation();diff --git a/packages/editor/src/extensions/outline-list-item/outline-list-item.ts b/packages/editor/src/extensions/outline-list-item/outline-list-item.tsindex 86fe24405..e5e969857 100644--- a/packages/editor/src/extensions/outline-list-item/outline-list-item.ts+++ b/packages/editor/src/extensions/outline-list-item/outline-list-item.ts@@ -17,12 +17,11 @@ You should have received a copy of the GNU General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.*/+import { Node, mergeAttributes } from "@tiptap/core";import {- Node,- mergeAttributes,- findParentNodeClosestToPos-} from "@tiptap/core";-import { findParentNodeOfTypeClosestToPos } from "../../utils/prosemirror.js";+ findParentNodeOfTypeClosestToPos,+ isClickWithinBounds+} from "../../utils/prosemirror.js";import { OutlineList } from "../outline-list/outline-list.js";import { keybindings, tiptapKeys } from "@notesnook/common";@@ -129,36 +128,9 @@ export const OutlineListItem = Node.create<ListItemOptions>({const pos = typeof getPos === "function" ? getPos() : 0;if (typeof pos !== "number") return;- const resolvedPos = editor.state.doc.resolve(pos);-- const { x, y, right } = li.getBoundingClientRect();-- const clientX =- e instanceof MouseEvent ? e.clientX : e.touches[0].clientX;-- const clientY =- e instanceof MouseEvent ? e.clientY : e.touches[0].clientY;-- const hitArea = { width: 40, height: 40 };- const isRtl =- e.target.dir === "rtl" ||- findParentNodeClosestToPos(- resolvedPos,- (node) => !!node.attrs.textDirection- )?.node.attrs.textDirection === "rtl";-- let xStart = clientX >= x - hitArea.width;- let xEnd = clientX <= x;- const yStart = clientY >= y;- const yEnd = clientY <= y + hitArea.height;-- if (isRtl) {- xEnd = clientX <= right + hitArea.width;- xStart = clientX >= right;- }-- if (xStart && xEnd && yStart && yEnd) {+ const resolvedPos = editor.state.doc.resolve(pos);+ if (isClickWithinBounds(e, resolvedPos, "left")) {e.preventDefault();editor.commands.command(({ tr }) => {tr.setNodeAttribute(diff --git a/packages/editor/src/utils/prosemirror.ts b/packages/editor/src/utils/prosemirror.tsindex 34b36b3f6..ae6c1dfda 100644--- a/packages/editor/src/utils/prosemirror.ts+++ b/packages/editor/src/utils/prosemirror.ts@@ -345,3 +345,52 @@ export function getDeletedNodes(}return nodes;}++export function isClickWithinBounds(+ e: MouseEvent | TouchEvent,+ pos: ResolvedPos,+ hitPosition: "left" | "right",+ hitArea: { width: number; height: number } = { width: 40, height: 40 }+) {+ const { target } = e;+ if (!(target instanceof HTMLElement)) return false;++ const { x, y, right, width } = target.getBoundingClientRect();+ const clientX = e instanceof MouseEvent ? e.clientX : e.touches[0].clientX;+ const clientY = e instanceof MouseEvent ? e.clientY : e.touches[0].clientY;+ const isRtl =+ target.dir === "rtl" ||+ findParentNodeClosestToPos(pos, (node) => !!node.attrs.textDirection)?.node+ .attrs.textDirection === "rtl";++ switch (hitPosition) {+ case "left": {+ let xStart = clientX >= x - hitArea.width;+ let xEnd = clientX <= x;+ const yStart = clientY >= y;+ const yEnd = clientY <= y + hitArea.height;++ if (isRtl) {+ xEnd = clientX <= x + hitArea.width;+ xStart = clientX >= right;+ }++ return xStart && xEnd && yStart && yEnd;+ }+ case "right": {+ let xEnd = clientX <= x + width;+ let xStart = clientX >= x + width - hitArea.width;+ const yStart = clientY >= y;+ const yEnd = clientY <= y + hitArea.height;++ if (isRtl) {+ xStart = clientX >= x;+ xEnd = clientX <= x + hitArea.width;+ }++ return xStart && xEnd && yStart && yEnd;+ }+ default:+ return false;+ }+}