Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: removed redundant codes from useBlockHighlight #63

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 19 additions & 44 deletions src/core/components/canvas/static/Canvas.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect } from "react";
import { useFrame } from "../../../frame";
import { useSelectedBlockIds, useSelectedStylingBlocks, useUpdateBlocksProps } from "../../../hooks";
import { useSelectedBlockIds, useSelectedStylingBlocks, useUpdateBlocksProps, useBlockHighlight } from "../../../hooks";
import { first, isEmpty, omit, throttle } from "lodash-es";
import { Quill } from "react-quill";
import { useAtom } from "jotai";
Expand Down Expand Up @@ -47,6 +47,7 @@ const useHandleCanvasDblClick = () => {
const INLINE_EDITABLE_BLOCKS = ["Heading", "Paragraph", "Text", "Link", "Span", "Button"];
const updateContent = useUpdateBlocksProps();
const [editingBlockId, setEditingBlockId] = useAtom(inlineEditingActiveAtom);
const { clearHighlight } = useBlockHighlight();

return (e) => {
if (editingBlockId) return;
Expand Down Expand Up @@ -74,10 +75,7 @@ const useHandleCanvasDblClick = () => {
newBlock.removeEventListener("blur", blurListener, true);
destroyQuill(quill);
setEditingBlockId("");
if (lastHighlighted) {
lastHighlighted.removeAttribute("data-highlighted");
lastHighlighted = null;
}
clearHighlight();
}
newBlock.addEventListener("blur", blurListener, true);

Expand All @@ -99,18 +97,15 @@ const useHandleCanvasClick = () => {
const [ids, setIds] = useSelectedBlockIds();
const [editingBlockId] = useAtom(inlineEditingActiveAtom);
const [treeRef] = useAtom(treeRefAtom);

const { clearHighlight } = useBlockHighlight();
return (e: any) => {
if (editingBlockId) return;
e.stopPropagation();
const chaiBlock: HTMLElement = getTargetedBlock(e.target);
if (chaiBlock?.getAttribute("data-block-id") && chaiBlock?.getAttribute("data-block-id") === "container") {
setIds([]);
setStyleBlockIds([]);
if (lastHighlighted) {
lastHighlighted.removeAttribute("data-highlighted");
lastHighlighted = null;
}
clearHighlight();
return;
}

Expand All @@ -133,59 +128,39 @@ const useHandleCanvasClick = () => {
setStyleBlockIds([]);
setIds(blockId === "canvas" ? [] : [blockId]);
}
if (lastHighlighted) {
lastHighlighted.removeAttribute("data-highlighted");
lastHighlighted = null;
}

clearHighlight();
};
};

let lastHighlighted: HTMLElement | null = null;

const handleMouseMove = throttle((e: any) => {
if (lastHighlighted) {
lastHighlighted.removeAttribute("data-highlighted");
}

const chaiBlock = getTargetedBlock(e.target);
if (chaiBlock) {
chaiBlock.setAttribute("data-highlighted", "true");
lastHighlighted = chaiBlock;
} else {
lastHighlighted = null;
}
}, 16);

const useHandleMouseMove = () => {
const [editingBlockId] = useAtom(inlineEditingActiveAtom);
return (e: any) => {
if (editingBlockId) return;
handleMouseMove(e);
};
};
const { highlightBlock } = useBlockHighlight();

const clearHighlight = () => {
if (lastHighlighted) {
lastHighlighted.removeAttribute("data-highlighted");
lastHighlighted = null;
}
return throttle((e: any) => {
if (editingBlockId) return;
const chaiBlock = getTargetedBlock(e.target);
if (chaiBlock) {
highlightBlock(chaiBlock.getAttribute("data-block-id"));
}
}, 16);
};

const useHandleMouseLeave = () => {
const { clearHighlight } = useBlockHighlight();
return clearHighlight;
};

export const Canvas = ({ children }: { children: React.ReactNode }) => {
const { document } = useFrame();
const [ids] = useSelectedBlockIds();
const [styleIds, setSelectedStylingBlocks] = useSelectedStylingBlocks();
const { clearHighlight } = useBlockHighlight();

// Add cleanup effect
useEffect(() => {
return () => {
clearHighlight();
};
}, []);
return clearHighlight;
}, [clearHighlight]);

useEffect(() => {
setTimeout(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/core/hooks/useBlockHighlight.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useAtom } from "jotai";
import { canvasIframeAtom } from "../atoms/ui";

//module-level variable to track the last highlighted block
let lastHighlighted: HTMLElement | null = null;

export const useBlockHighlight = () => {
Expand Down Expand Up @@ -29,5 +30,5 @@ export const useBlockHighlight = () => {
}
};

return { highlightBlock, clearHighlight };
return { highlightBlock, clearHighlight, lastHighlighted };
};
Loading