Skip to content

Commit

Permalink
feat: find node in the document
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Apr 9, 2024
1 parent dc9dde5 commit e29630a
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 23 deletions.
15 changes: 15 additions & 0 deletions src/main/endpoints/highlightNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createEndpoint } from "../utils/createEndpoint";

export type HighlightNodeProps = {
key: string;
};

export const highlightNodeEndpoint = createEndpoint(
"HIGHLIGHT_NODE",
async ({ key }: HighlightNodeProps) => {
const node = figma.getNodeById(key);
if (node) {
figma.viewport.scrollAndZoomIntoView([node]);
}
}
);
2 changes: 2 additions & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
setPluginData,
} from "./utils/settingsTools";
import { DEFAULT_SIZE } from "@/ui/hooks/useWindowSize";
import { highlightNodeEndpoint } from "./endpoints/highlightNode";

const getAllPages = () => {
const document = figma.root;
Expand Down Expand Up @@ -91,6 +92,7 @@ export default async function () {
copyPageEndpoint.register();
updateNodesEndpoint.register();
setNodesDataEndpoint.register();
highlightNodeEndpoint.register();

const config = await getPluginData();

Expand Down
3 changes: 3 additions & 0 deletions src/main/utils/nodeTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ function shouldIncludeNode(
if (settings.ignorePrefix && node.name.startsWith(settings.ignorePrefix)) {
return false;
}
if (node.characters.trim().length === 0) {
return false;
}
return true;
}

Expand Down
12 changes: 12 additions & 0 deletions src/ui/hooks/useHighlightNodeMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {
HighlightNodeProps,
highlightNodeEndpoint,
} from "@/main/endpoints/highlightNode";
import { useMutation } from "react-query";

export const useHighlightNodeMutation = () => {
return useMutation<void, void, HighlightNodeProps>(
[highlightNodeEndpoint.name],
(data: HighlightNodeProps) => highlightNodeEndpoint.call(data)
);
};
11 changes: 11 additions & 0 deletions src/ui/icons/SvgIcons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,14 @@ export const AddCircle = (props: h.JSX.SVGAttributes<SVGSVGElement>) => {
</svg>
);
};

export const MyLocation = (props: h.JSX.SVGAttributes<SVGSVGElement>) => {
return (
<svg {...props} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48">
<path
fill="currentColor"
d="M24 16c-4.42 0-8 3.58-8 8s3.58 8 8 8 8-3.58 8-8-3.58-8-8-8zm17.88 6C40.96 13.66 34.34 7.04 26 6.12V2h-4v4.12C13.66 7.04 7.04 13.66 6.12 22H2v4h4.12c.92 8.34 7.54 14.96 15.88 15.88V46h4v-4.12c8.34-.92 14.96-7.54 15.88-15.88H46v-4h-4.12zM24 38c-7.73 0-14-6.27-14-14s6.27-14 14-14 14 6.27 14 14-6.27 14-14 14z"
/>
</svg>
);
};
15 changes: 15 additions & 0 deletions src/ui/views/Index/Index.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
height: 20px;
}

.highlightButton {
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
width: 20px;
height: 20px;
color: var(--figma-color-text-secondary);
}

.disabled {
color: var(--figma-color-text-disabled);
}
Expand All @@ -46,3 +56,8 @@
display: grid;
grid-template-rows: auto 1fr;
}

.actionsContainer {
display: flex;
gap: 4px;
}
2 changes: 2 additions & 0 deletions src/ui/views/Index/Index.css.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
declare const styles: {
readonly "actionsContainer": string;
readonly "connectButton": string;
readonly "container": string;
readonly "disabled": string;
readonly "highlightButton": string;
readonly "keyInput": string;
readonly "languageContainer": string;
readonly "nsSelect": string;
Expand Down
65 changes: 42 additions & 23 deletions src/ui/views/Index/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "@create-figma-plugin/ui";

import { NodeInfo } from "@/types";
import { Settings, InsertLink } from "@/ui/icons/SvgIcons";
import { Settings, InsertLink, MyLocation } from "@/ui/icons/SvgIcons";
import { useApiQuery } from "@/ui/client/useQueryApi";
import { getConflictingNodes } from "@/tools/getConflictingNodes";
import { FullPageLoading } from "@/ui/components/FullPageLoading/FullPageLoading";
Expand All @@ -29,6 +29,7 @@ import styles from "./Index.css";
import { KeyInput } from "./KeyInput";
import { useSelectedNodes } from "@/ui/hooks/useSelectedNodes";
import { useSetNodesDataMutation } from "@/ui/hooks/useSetNodesDataMutation";
import { useHighlightNodeMutation } from "@/ui/hooks/useHighlightNodeMutation";

export const Index = () => {
const selectionLoadable = useSelectedNodes();
Expand All @@ -50,6 +51,8 @@ export const Index = () => {
method: "get",
});

const highlightMutation = useHighlightNodeMutation();

const setNodesDataMutation = useSetNodesDataMutation();

const languages = languagesLoadable.data?._embedded?.languages;
Expand Down Expand Up @@ -99,6 +102,10 @@ export const Index = () => {
}
};

const handleHighlight = (node: NodeInfo) => {
highlightMutation.mutate({ key: node.id });
};

const handlePull = () => {
setRoute("pull", {
lang: language,
Expand Down Expand Up @@ -244,28 +251,40 @@ export const Index = () => {
}
actionCallback={(node) => {
return (
<div
data-cy="index_link_button"
role="button"
title={
!node.connected
? "Connect to existing key"
: "Edit key connection"
}
onClick={() => handleConnect(node)}
className={styles.connectButton}
>
{node.connected ? (
<InsertLink width={16} height={16} />
) : (
<InsertLink
width={16}
height={16}
style={{
color: "var(--figma-color-text-secondary)",
}}
/>
)}
<div className={styles.actionsContainer}>
<div
data-cy="index_highlight_button"
role="button"
title="Locate on the page"
onClick={() => handleHighlight(node)}
className={styles.highlightButton}
>
<MyLocation width={16} height={16} />
</div>

<div
data-cy="index_link_button"
role="button"
title={
!node.connected
? "Connect to existing key"
: "Edit key connection"
}
onClick={() => handleConnect(node)}
className={styles.connectButton}
>
{node.connected ? (
<InsertLink width={16} height={16} />
) : (
<InsertLink
width={16}
height={16}
style={{
color: "var(--figma-color-text-secondary)",
}}
/>
)}
</div>
</div>
);
}}
Expand Down

0 comments on commit e29630a

Please sign in to comment.