Skip to content

Commit

Permalink
feat: Internal function to search a block
Browse files Browse the repository at this point in the history
  • Loading branch information
surajshetty3416 committed Feb 18, 2025
1 parent f9784ca commit 107d20f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
35 changes: 34 additions & 1 deletion frontend/src/components/BuilderCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
import LoadingIcon from "@/components/Icons/Loading.vue";
import { BreakpointConfig, CanvasHistory } from "@/types/Builder/BuilderCanvas";
import Block from "@/utils/block";
import { getBlockCopy, isCtrlOrCmd } from "@/utils/helpers";
import { getBlockCopy, getBlockObject, isCtrlOrCmd } from "@/utils/helpers";
import { useBlockEventHandlers } from "@/utils/useBlockEventHandlers";
import { useBlockSelection } from "@/utils/useBlockSelection";
import { useCanvasDropZone } from "@/utils/useCanvasDropZone";
Expand Down Expand Up @@ -213,6 +213,38 @@ const handleClick = (ev: MouseEvent) => {
}
};
function searchBlock(searchTerm: string, targetBlock: null | Block) {
// find nearest block to the search term
// convert block to string and search for the term
// if found, return the nearest block
// else return null
if (!targetBlock) {
targetBlock = getRootBlock();
}
console.log("searching block", targetBlock);
const blockObject = getBlockObject(targetBlock);
const children = blockObject.children || [];
delete blockObject.children;
let blockId = "";
if (JSON.stringify(blockObject).toLowerCase().includes(searchTerm.toLowerCase())) {
blockId = blockObject.blockId as string;
}
if (blockId) {
const block = findBlock(blockId);
if (block) {
console.log("found block", block);
return scrollBlockIntoView(block);
}
} else {
for (const child of children) {
return searchBlock(searchTerm, child);
}
}
return null;
}
watch(
() => block,
() => {
Expand Down Expand Up @@ -268,6 +300,7 @@ defineExpose({
removeBlock,
selectBlockRange,
resizingBlock,
searchBlock,
});
function selectBreakpoint(ev: MouseEvent, breakpoint: BreakpointConfig) {
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/utils/useCanvasUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export function useCanvasUtils(
) {
// wait for editor to render
await new Promise((resolve) => setTimeout(resolve, 100));
if (!selectedBlockIds.value.has(blockToFocus.blockId)) {
selectBlock(blockToFocus);
}
await nextTick();
if (
!canvasContainer.value ||
Expand All @@ -39,6 +42,7 @@ export function useCanvasUtils(
}
const container = canvasContainer.value as HTMLElement;
const containerRect = container.getBoundingClientRect();
await nextTick();
const selectedBlock = document.body.querySelector(
`.editor[data-block-id="${blockToFocus.blockId}"][selected=true]`,
) as HTMLElement;
Expand Down

0 comments on commit 107d20f

Please sign in to comment.