From ec70d68399c2baa651f8c846cc9f4ef759646572 Mon Sep 17 00:00:00 2001
From: Palanikannan1437 <73993394+Palanikannan1437@users.noreply.github.com>
Date: Fri, 15 Mar 2024 10:45:32 +0530
Subject: [PATCH 1/3] fix: clearing nodes to default node i.e paragraph before
converting it to other types of nodes
For more reference on what this does, please refer https://tiptap.dev/docs/editor/api/commands/clear-nodes
---
.../editor/core/src/lib/editor-commands.ts | 44 +++++++++----------
.../src/extensions/slash-commands.tsx | 5 ++-
.../src/ui/menus/bubble-menu/index.tsx | 17 ++++---
.../ui/menus/bubble-menu/link-selector.tsx | 2 +-
.../ui/menus/bubble-menu/node-selector.tsx | 2 +-
5 files changed, 39 insertions(+), 31 deletions(-)
diff --git a/packages/editor/core/src/lib/editor-commands.ts b/packages/editor/core/src/lib/editor-commands.ts
index 6524d1ff5..ae0bafefe 100644
--- a/packages/editor/core/src/lib/editor-commands.ts
+++ b/packages/editor/core/src/lib/editor-commands.ts
@@ -4,18 +4,18 @@ import { findTableAncestor } from "src/lib/utils";
import { UploadImage } from "src/types/upload-image";
export const toggleHeadingOne = (editor: Editor, range?: Range) => {
- if (range) editor.chain().focus().deleteRange(range).setNode("heading", { level: 1 }).run();
- else editor.chain().focus().toggleHeading({ level: 1 }).run();
+ if (range) editor.chain().focus().deleteRange(range).clearNodes().setNode("heading", { level: 1 }).run();
+ else editor.chain().focus().clearNodes().toggleHeading({ level: 1 }).run();
};
export const toggleHeadingTwo = (editor: Editor, range?: Range) => {
- if (range) editor.chain().focus().deleteRange(range).setNode("heading", { level: 2 }).run();
- else editor.chain().focus().toggleHeading({ level: 2 }).run();
+ if (range) editor.chain().focus().deleteRange(range).clearNodes().setNode("heading", { level: 2 }).run();
+ else editor.chain().focus().clearNodes().toggleHeading({ level: 2 }).run();
};
export const toggleHeadingThree = (editor: Editor, range?: Range) => {
- if (range) editor.chain().focus().deleteRange(range).setNode("heading", { level: 3 }).run();
- else editor.chain().focus().toggleHeading({ level: 3 }).run();
+ if (range) editor.chain().focus().deleteRange(range).clearNodes().setNode("heading", { level: 3 }).run();
+ else editor.chain().focus().clearNodes().toggleHeading({ level: 3 }).run();
};
export const toggleBold = (editor: Editor, range?: Range) => {
@@ -37,10 +37,10 @@ export const toggleCodeBlock = (editor: Editor, range?: Range) => {
// Check if code block is active then toggle code block
if (editor.isActive("codeBlock")) {
if (range) {
- editor.chain().focus().deleteRange(range).toggleCodeBlock().run();
+ editor.chain().focus().clearNodes().deleteRange(range).toggleCodeBlock().run();
return;
}
- editor.chain().focus().toggleCodeBlock().run();
+ editor.chain().focus().clearNodes().toggleCodeBlock().run();
return;
}
@@ -49,32 +49,32 @@ export const toggleCodeBlock = (editor: Editor, range?: Range) => {
if (isSelectionEmpty) {
if (range) {
- editor.chain().focus().deleteRange(range).toggleCodeBlock().run();
+ editor.chain().focus().clearNodes().deleteRange(range).toggleCodeBlock().run();
return;
}
- editor.chain().focus().toggleCodeBlock().run();
+ editor.chain().focus().clearNodes().toggleCodeBlock().run();
} else {
if (range) {
- editor.chain().focus().deleteRange(range).toggleCode().run();
+ editor.chain().focus().clearNodes().deleteRange(range).toggleCode().run();
return;
}
- editor.chain().focus().toggleCode().run();
+ editor.chain().focus().clearNodes().toggleCode().run();
}
};
export const toggleOrderedList = (editor: Editor, range?: Range) => {
- if (range) editor.chain().focus().deleteRange(range).toggleOrderedList().run();
- else editor.chain().focus().toggleOrderedList().run();
+ if (range) editor.chain().focus().deleteRange(range).clearNodes().toggleOrderedList().run();
+ else editor.chain().focus().clearNodes().toggleOrderedList().run();
};
export const toggleBulletList = (editor: Editor, range?: Range) => {
- if (range) editor.chain().focus().deleteRange(range).toggleBulletList().run();
- else editor.chain().focus().toggleBulletList().run();
+ if (range) editor.chain().focus().deleteRange(range).clearNodes().toggleBulletList().run();
+ else editor.chain().focus().clearNodes().toggleBulletList().run();
};
export const toggleTaskList = (editor: Editor, range?: Range) => {
- if (range) editor.chain().focus().deleteRange(range).toggleTaskList().run();
- else editor.chain().focus().toggleTaskList().run();
+ if (range) editor.chain().focus().deleteRange(range).clearNodes().toggleTaskList().run();
+ else editor.chain().focus().clearNodes().toggleTaskList().run();
};
export const toggleStrike = (editor: Editor, range?: Range) => {
@@ -83,8 +83,8 @@ export const toggleStrike = (editor: Editor, range?: Range) => {
};
export const toggleBlockquote = (editor: Editor, range?: Range) => {
- if (range) editor.chain().focus().deleteRange(range).toggleBlockquote().run();
- else editor.chain().focus().toggleBlockquote().run();
+ if (range) editor.chain().focus().deleteRange(range).clearNodes().toggleBlockquote().run();
+ else editor.chain().focus().clearNodes().toggleBlockquote().run();
};
export const insertTableCommand = (editor: Editor, range?: Range) => {
@@ -97,8 +97,8 @@ export const insertTableCommand = (editor: Editor, range?: Range) => {
}
}
}
- if (range) editor.chain().focus().deleteRange(range).insertTable({ rows: 3, cols: 3 }).run();
- else editor.chain().focus().insertTable({ rows: 3, cols: 3 }).run();
+ if (range) editor.chain().focus().deleteRange(range).clearNodes().insertTable({ rows: 3, cols: 3 }).run();
+ else editor.chain().focus().clearNodes().insertTable({ rows: 3, cols: 3 }).run();
};
export const unsetLinkEditor = (editor: Editor) => {
diff --git a/packages/editor/extensions/src/extensions/slash-commands.tsx b/packages/editor/extensions/src/extensions/slash-commands.tsx
index 88e257cef..f37d18c68 100644
--- a/packages/editor/extensions/src/extensions/slash-commands.tsx
+++ b/packages/editor/extensions/src/extensions/slash-commands.tsx
@@ -85,7 +85,10 @@ const getSuggestionItems =
searchTerms: ["p", "paragraph"],
icon: ,
command: ({ editor, range }: CommandProps) => {
- editor.chain().focus().deleteRange(range).toggleNode("paragraph", "paragraph").run();
+ if (range) {
+ editor.chain().focus().deleteRange(range).clearNodes().run();
+ }
+ editor.chain().focus().clearNodes().run();
},
},
{
diff --git a/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/index.tsx b/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/index.tsx
index f96e7293e..ef3329b6c 100644
--- a/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/index.tsx
+++ b/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/index.tsx
@@ -25,16 +25,20 @@ type EditorBubbleMenuProps = Omit;
export const EditorBubbleMenu: FC = (props: any) => {
const items: BubbleMenuItem[] = [
- BoldItem(props.editor),
- ItalicItem(props.editor),
- UnderLineItem(props.editor),
- StrikeThroughItem(props.editor),
+ ...(props.editor.isActive("code")
+ ? []
+ : [
+ BoldItem(props.editor),
+ ItalicItem(props.editor),
+ UnderLineItem(props.editor),
+ StrikeThroughItem(props.editor),
+ ]),
CodeItem(props.editor),
];
const bubbleMenuProps: EditorBubbleMenuProps = {
...props,
- shouldShow: ({ view, state, editor }) => {
+ shouldShow: ({ state, editor }) => {
const { selection } = state;
const { empty } = selection;
@@ -64,6 +68,7 @@ export const EditorBubbleMenu: FC = (props: any) => {
const [isLinkSelectorOpen, setIsLinkSelectorOpen] = useState(false);
const [isSelecting, setIsSelecting] = useState(false);
+
useEffect(() => {
function handleMouseDown() {
function handleMouseMove() {
@@ -109,7 +114,7 @@ export const EditorBubbleMenu: FC = (props: any) => {
/>
)}
{
setIsLinkSelectorOpen(!isLinkSelectorOpen);
diff --git a/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/link-selector.tsx b/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/link-selector.tsx
index 14448114d..e45cfb317 100644
--- a/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/link-selector.tsx
+++ b/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/link-selector.tsx
@@ -84,8 +84,8 @@ export const LinkSelector: FC = ({ editor, isOpen, setIsOpen
className="flex items-center rounded-sm p-1 text-custom-text-300 transition-all hover:bg-custom-background-90"
type="button"
onClick={(e) => {
- e.stopPropagation();
onLinkSubmit();
+ e.stopPropagation();
}}
>
diff --git a/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/node-selector.tsx b/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/node-selector.tsx
index 6bc996085..1bb8c38bd 100644
--- a/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/node-selector.tsx
+++ b/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/node-selector.tsx
@@ -26,7 +26,7 @@ export const NodeSelector: FC = ({ editor, isOpen, setIsOpen
{
name: "Text",
icon: TextIcon,
- command: () => editor.chain().focus().toggleNode("paragraph", "paragraph").run(),
+ command: () => editor.chain().focus().clearNodes().run(),
isActive: () => editor.isActive("paragraph") && !editor.isActive("bulletList") && !editor.isActive("orderedList"),
},
HeadingOneItem(editor),
From f7700b4536ee3bd2ee7a212336faa513b8214c25 Mon Sep 17 00:00:00 2001
From: Palanikannan1437 <73993394+Palanikannan1437@users.noreply.github.com>
Date: Fri, 15 Mar 2024 11:12:42 +0530
Subject: [PATCH 2/3] chore: clearNodes after delete in case of selections
being present
---
packages/editor/core/src/lib/editor-commands.ts | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/packages/editor/core/src/lib/editor-commands.ts b/packages/editor/core/src/lib/editor-commands.ts
index ae0bafefe..7c3e7f11e 100644
--- a/packages/editor/core/src/lib/editor-commands.ts
+++ b/packages/editor/core/src/lib/editor-commands.ts
@@ -37,7 +37,7 @@ export const toggleCodeBlock = (editor: Editor, range?: Range) => {
// Check if code block is active then toggle code block
if (editor.isActive("codeBlock")) {
if (range) {
- editor.chain().focus().clearNodes().deleteRange(range).toggleCodeBlock().run();
+ editor.chain().focus().deleteRange(range).clearNodes().toggleCodeBlock().run();
return;
}
editor.chain().focus().clearNodes().toggleCodeBlock().run();
@@ -49,13 +49,13 @@ export const toggleCodeBlock = (editor: Editor, range?: Range) => {
if (isSelectionEmpty) {
if (range) {
- editor.chain().focus().clearNodes().deleteRange(range).toggleCodeBlock().run();
+ editor.chain().focus().deleteRange(range).clearNodes().toggleCodeBlock().run();
return;
}
editor.chain().focus().clearNodes().toggleCodeBlock().run();
} else {
if (range) {
- editor.chain().focus().clearNodes().deleteRange(range).toggleCode().run();
+ editor.chain().focus().deleteRange(range).clearNodes().toggleCode().run();
return;
}
editor.chain().focus().clearNodes().toggleCode().run();
From adfa9c1b1aa36eb03f47928cacc25729a7ea43db Mon Sep 17 00:00:00 2001
From: Palanikannan1437 <73993394+Palanikannan1437@users.noreply.github.com>
Date: Fri, 15 Mar 2024 11:25:05 +0530
Subject: [PATCH 3/3] fix: hiding link selector in the bubble menu if inline
code block is selected
---
.../src/ui/menus/bubble-menu/index.tsx | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/index.tsx b/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/index.tsx
index ef3329b6c..2dbc86cec 100644
--- a/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/index.tsx
+++ b/packages/editor/rich-text-editor/src/ui/menus/bubble-menu/index.tsx
@@ -113,14 +113,16 @@ export const EditorBubbleMenu: FC = (props: any) => {
}}
/>
)}
- {
- setIsLinkSelectorOpen(!isLinkSelectorOpen);
- setIsNodeSelectorOpen(false);
- }}
- />
+ {!props.editor.isActive("code") && (
+ {
+ setIsLinkSelectorOpen(!isLinkSelectorOpen);
+ setIsNodeSelectorOpen(false);
+ }}
+ />
+ )}
{items.map((item) => (