From 58dae2ef695bb902c0b7935f36ac6b51f556e02f Mon Sep 17 00:00:00 2001 From: codetorso Date: Sat, 17 Aug 2024 05:50:16 +0530 Subject: [PATCH] popover ruining my day again --- .../web/components/editor/advanced-editor.tsx | 9 ++-- apps/web/components/editor/toc.tsx | 49 ++++++------------- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/apps/web/components/editor/advanced-editor.tsx b/apps/web/components/editor/advanced-editor.tsx index b6565c82..f08cd15a 100644 --- a/apps/web/components/editor/advanced-editor.tsx +++ b/apps/web/components/editor/advanced-editor.tsx @@ -45,6 +45,7 @@ type tContent = { isActive: boolean; itemIndex: number; isScrolledOver: boolean; + pos: number; }; const TailwindAdvancedEditor = memo( @@ -60,18 +61,20 @@ const TailwindAdvancedEditor = memo( TableOfContents.configure({ getIndex: getHierarchicalIndexes, onUpdate(content) { + console.log(content); setItems(content); }, }), ]; return ( -
+
handleCommandNavigation(event), @@ -103,7 +106,7 @@ const TailwindAdvancedEditor = memo( className="flex w-full items-center space-x-2 rounded-md px-2 py-1 text-left text-sm hover:bg-[#21303D] group aria-selected:bg-[#21303D]" key={item.title} > -
+
{item.icon}
diff --git a/apps/web/components/editor/toc.tsx b/apps/web/components/editor/toc.tsx index 1bca6a11..4f5a3abb 100644 --- a/apps/web/components/editor/toc.tsx +++ b/apps/web/components/editor/toc.tsx @@ -1,6 +1,6 @@ -import { TextSelection } from "@tiptap/pm/state"; import { useEditor } from "novel"; import { motion } from "framer-motion"; + type tContent = { id: string; textContent: string; @@ -8,6 +8,7 @@ type tContent = { isActive: boolean; itemIndex: number; isScrolledOver: boolean; + pos: number; }; export const ToCItem = ({ @@ -15,7 +16,7 @@ export const ToCItem = ({ onItemClick, }: { item: tContent; - onItemClick: (e: MouseEvent, id: string) => void; + onItemClick: (pos: number) => void; }) => { return ( ); }; export const ToC = ({ items }: { items: tContent[] }) => { - if (items.length === 0) { + if (items.length < 2) { return; } return ( -
+
{items.map((item, i) => ( @@ -50,7 +50,7 @@ export const ToC = ({ items }: { items: tContent[] }) => { initial={{ x: 15, opacity: 0 }} whileHover={{ x: 5, opacity: 1 }} transition={{ ease: "easeOut", duration: 0.15 }} - className="absolute top-0 right-0 space-y-3" + className="absolute top-0 right-0 space-y-3 min-w-72 max-w-72" > @@ -61,36 +61,15 @@ export const ToC = ({ items }: { items: tContent[] }) => { function Container({ items }: { items: tContent[] }) { const { editor } = useEditor(); - const onItemClick = (e, id) => { - e.preventDefault(); - + const onItemClick = (pos: number) => { + console.log(pos); if (editor) { - const element = editor.view.dom.querySelector(`[data-toc-id="${id}"`); - const pos = editor.view.posAtDOM(element, 0); - - // set focus - const tr = editor.view.state.tr; - - tr.setSelection(new TextSelection(tr.doc.resolve(pos))); - - editor.view.dispatch(tr); - - editor.view.focus(); - - if (history.pushState) { - // eslint-disable-line - history.pushState(null, null, `#${id}`); // eslint-disable-line - } - - window.scrollTo({ - top: element.getBoundingClientRect().top + window.scrollY, - behavior: "smooth", - }); + editor.commands.focus(pos ? pos : 1, { scrollIntoView: true }); } }; return ( -
+
{items.map((item, i) => ( ))}