Skip to content

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

editor: fix editing link adds a new link right beside it

Abdullah Attacd0a6a8fcbb4
diff --git a/packages/editor/src/toolbar/components/toolbar-group.tsx b/packages/editor/src/toolbar/components/toolbar-group.tsx
index 58e12c998..d9209834c 100644
--- a/packages/editor/src/toolbar/components/toolbar-group.tsx
+++ b/packages/editor/src/toolbar/components/toolbar-group.tsx
@@ -23,17 +23,15 @@ import { Flex, FlexProps } from "@theme-ui/components";
import { Editor } from "../../types";
import { MoreTools } from "./more-tools";
import { getToolDefinition } from "../tool-definitions";
-import { NodeWithOffset } from "../../utils/prosemirror";
export type ToolbarGroupProps = FlexProps & {
tools: ToolbarGroupDefinition;
editor: Editor;
variant?: ToolButtonVariant;
force?: boolean;
- selectedNode?: NodeWithOffset;
};
export function ToolbarGroup(props: ToolbarGroupProps) {
- const { tools, editor, force, selectedNode, sx, ...flexProps } = props;
+ const { tools, editor, force, sx, ...flexProps } = props;
return (
<Flex
@@ -66,7 +64,6 @@ export function ToolbarGroup(props: ToolbarGroupProps) {
key={toolDefinition.title}
editor={editor}
force={force}
- selectedNode={selectedNode}
{...toolDefinition}
/>
);
diff --git a/packages/editor/src/toolbar/floating-menus/hover-popup/context.ts b/packages/editor/src/toolbar/floating-menus/hover-popup/context.ts
new file mode 100644
index 000000000..d785bf4f3
--- /dev/null
+++ b/packages/editor/src/toolbar/floating-menus/hover-popup/context.ts
@@ -0,0 +1,30 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+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 { NodeWithOffset } from "@/src/utils/prosemirror";
+import { createContext, useContext } from "react";
+
+const HoverPopupContext = createContext<{
+ selectedNode?: NodeWithOffset;
+ hide: () => void;
+}>({ hide: () => {} });
+export function useHoverPopupContext() {
+ return useContext(HoverPopupContext);
+}
+export const HoverPopupContextProvider = HoverPopupContext.Provider;
diff --git a/packages/editor/src/toolbar/floating-menus/hover-popup/index.tsx b/packages/editor/src/toolbar/floating-menus/hover-popup/index.tsx
index e03bb12f1..dbd405fa1 100644
--- a/packages/editor/src/toolbar/floating-menus/hover-popup/index.tsx
+++ b/packages/editor/src/toolbar/floating-menus/hover-popup/index.tsx
@@ -20,13 +20,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { useEffect, useRef } from "react";
import { showPopup } from "../../../components/popup-presenter";
import { Editor } from "../../../types";
-import { NodeWithOffset } from "../../../utils/prosemirror";
import { FloatingMenuProps } from "../types";
import { LinkHoverPopupHandler } from "./link";
+import { HoverPopupContextProvider } from "./context";
export type HoverPopupProps = {
editor: Editor;
- selectedNode: NodeWithOffset;
};
const handlers = [LinkHoverPopupHandler];
@@ -96,14 +95,18 @@ export function HoverPopupHandler(props: FloatingMenuProps) {
const hidePopup = showPopup({
popup: () => (
- <Popup
- editor={editor}
- selectedNode={{
- node,
- from: pos,
- to: pos + node.nodeSize
+ <HoverPopupContextProvider
+ value={{
+ selectedNode: {
+ node,
+ from: pos,
+ to: pos + node.nodeSize
+ },
+ hide: () => hidePopup()
}}
- />
+ >
+ <Popup editor={editor} />
+ </HoverPopupContextProvider>
),
blocking: false,
focusOnRender: false,
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 f357794b0..2ff865b37 100644
--- a/packages/editor/src/toolbar/floating-menus/hover-popup/link.tsx
+++ b/packages/editor/src/toolbar/floating-menus/hover-popup/link.tsx
@@ -19,10 +19,12 @@ 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";
function LinkHoverPopup(props: HoverPopupProps) {
- const { editor, selectedNode } = props;
- const { node } = selectedNode;
+ const { editor } = props;
+ const { selectedNode } = useHoverPopupContext();
+ const { node } = selectedNode || {};
if (
!node?.isText ||
@@ -36,7 +38,6 @@ function LinkHoverPopup(props: HoverPopupProps) {
force
tools={["openLink", "editLink", "removeLink", "copyLink"]}
editor={editor}
- selectedNode={selectedNode}
sx={{
bg: "background",
boxShadow: "menu",
diff --git a/packages/editor/src/toolbar/tools/link.tsx b/packages/editor/src/toolbar/tools/link.tsx
index 3ac63d9e2..e5cb389bf 100644
--- a/packages/editor/src/toolbar/tools/link.tsx
+++ b/packages/editor/src/toolbar/tools/link.tsx
@@ -30,6 +30,7 @@ import { Flex, Link } from "@theme-ui/components";
import { ImageNode } from "../../extensions/image";
import { Link as LinkNode } from "../../extensions/link";
import { getMarkAttributes } from "@tiptap/core";
+import { useHoverPopupContext } from "../floating-menus/hover-popup/context";
export function LinkSettings(props: ToolProps) {
const { editor } = props;
@@ -96,7 +97,8 @@ export function AddInternalLink(props: ToolProps) {
}
export function EditLink(props: ToolProps) {
- const { editor, selectedNode: _selectedNode } = props;
+ const { editor } = props;
+ const { selectedNode: _selectedNode, hide } = useHoverPopupContext();
const selectedNode = useRefValue(
_selectedNode || selectionToOffset(editor.state)
);
@@ -109,6 +111,7 @@ export function EditLink(props: ToolProps) {
<ToolButton
{...props}
onClick={async () => {
+ hide();
const link = await editor.storage.createInternalLink?.();
if (!link) return;
const { from, to } = editor.state.selection;
@@ -125,7 +128,12 @@ export function EditLink(props: ToolProps) {
<LinkTool
{...props}
isEditing
- onDone={(attributes) => editor.commands.setLink(attributes)}
+ onDone={(attributes) => {
+ if (selectedNode.current)
+ editor.chain().focus().setTextSelection(selectedNode.current).run();
+ editor.commands.setLink(attributes);
+ hide();
+ }}
onClick={() => {
if (!selectedNode.current) return;
@@ -146,7 +154,8 @@ export function EditLink(props: ToolProps) {
}
export function RemoveLink(props: ToolProps) {
- const { editor, selectedNode } = props;
+ const { editor } = props;
+ const { selectedNode, hide } = useHoverPopupContext();
if (!editor.isEditable) return null;
return (
<ToolButton
@@ -159,13 +168,15 @@ export function RemoveLink(props: ToolProps) {
to: selectedNode.to
});
editor.chain().focus().unsetLink().run();
+ hide();
}}
/>
);
}
export function OpenLink(props: ToolProps) {
- const { editor, selectedNode: _selectedNode } = props;
+ const { editor } = props;
+ const { selectedNode: _selectedNode, hide } = useHoverPopupContext();
const selectedNode = useRefValue(
_selectedNode || selectionToOffset(editor.state)
);
@@ -181,6 +192,7 @@ export function OpenLink(props: ToolProps) {
onClick={(e) => {
e.preventDefault();
editor.storage.openLink?.(href);
+ hide();
}}
target="_blank"
variant="body"
@@ -204,6 +216,7 @@ export function OpenLink(props: ToolProps) {
toggled={false}
onClick={() => {
editor.storage.openLink?.(href);
+ hide();
}}
/>
</Flex>
@@ -211,7 +224,8 @@ export function OpenLink(props: ToolProps) {
}
export function CopyLink(props: ToolProps) {
- const { editor, selectedNode: _selectedNode } = props;
+ const { editor } = props;
+ const { selectedNode: _selectedNode, hide } = useHoverPopupContext();
const selectedNode = useRefValue(
_selectedNode || selectionToOffset(editor.state)
);
@@ -231,6 +245,7 @@ export function CopyLink(props: ToolProps) {
selectedNode.current?.node?.textContent || link?.attrs.title
}</a>`
);
+ hide();
}}
/>
);
diff --git a/packages/editor/src/toolbar/types.ts b/packages/editor/src/toolbar/types.ts
index bb77f1102..8bfbc9a83 100644
--- a/packages/editor/src/toolbar/types.ts
+++ b/packages/editor/src/toolbar/types.ts
@@ -20,14 +20,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { Editor } from "../types";
import { IconNames } from "./icons";
import { ToolId } from "./tools";
-import { NodeWithOffset } from "../utils/prosemirror";
export type ToolButtonVariant = "small" | "normal";
export type ToolProps = ToolDefinition & {
editor: Editor;
variant?: ToolButtonVariant;
force?: boolean;
- selectedNode?: NodeWithOffset;
};
export type ToolDefinition = {