From 6ab101a84b71407036c5490807811ab6181f8101 Mon Sep 17 00:00:00 2001 From: Yuri Mikhin Date: Wed, 27 Sep 2023 11:30:28 +0300 Subject: [PATCH] Improve modularity. --- .../colors/handle-opaque-background.ts | 20 ------- .../colors/handle-transparent-background.ts | 20 ------- .../build-general-selection-payload.ts | 4 +- .../selection/process-node-for-selection.ts | 24 -------- .../process-node-from-multiple-selection.ts | 59 +++++++++++++++++++ 5 files changed, 61 insertions(+), 66 deletions(-) delete mode 100644 src/api/services/colors/handle-opaque-background.ts delete mode 100644 src/api/services/colors/handle-transparent-background.ts delete mode 100644 src/api/services/selection/process-node-for-selection.ts create mode 100644 src/api/services/selection/process-node-from-multiple-selection.ts diff --git a/src/api/services/colors/handle-opaque-background.ts b/src/api/services/colors/handle-opaque-background.ts deleted file mode 100644 index 4ebe6af..0000000 --- a/src/api/services/colors/handle-opaque-background.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { buildColorsPair } from '~api/services/colors/build-colors-pair.ts'; -import { getComputedNodeFill } from '~api/services/colors/get-computed-node-fill.ts'; -import { type ColorPair } from '~api/types.ts'; -import { type FigmaNode } from '~types/figma.ts'; -import { getFirstVisibleNodeFill } from '~utils/figma/get-first-visible-node-fill.ts'; -import { notEmpty } from '~utils/not-empty.ts'; - -export const handleOpaqueBackground = ( - selectedNode: FigmaNode, - firstIntersectingNode: FigmaNode -): ColorPair | null => { - const bgFill = getFirstVisibleNodeFill(firstIntersectingNode.fills); - - const fgFill = getComputedNodeFill(selectedNode); - - if (notEmpty(fgFill) && notEmpty(bgFill)) - return buildColorsPair(selectedNode.id, fgFill, bgFill); - - return null; -}; diff --git a/src/api/services/colors/handle-transparent-background.ts b/src/api/services/colors/handle-transparent-background.ts deleted file mode 100644 index 81d3858..0000000 --- a/src/api/services/colors/handle-transparent-background.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { buildColorsPair } from '~api/services/colors/build-colors-pair.ts'; -import { getComputedNodeFill } from '~api/services/colors/get-computed-node-fill.ts'; -import { blendLayersColors } from '~api/services/figma/blend/blend-layers-colors.ts'; -import { type ColorPair } from '~api/types.ts'; -import { type FigmaNode } from '~types/figma.ts'; -import { notEmpty } from '~utils/not-empty.ts'; - -export const handleTransparentBackground = ( - selectedNode: FigmaNode, - intersectingNodes: FigmaNode[] -): ColorPair | null => { - const blendedBgColor = blendLayersColors(intersectingNodes); - - const fgFill = getComputedNodeFill(selectedNode); - - if (notEmpty(fgFill) && notEmpty(blendedBgColor)) - return buildColorsPair(selectedNode.id, fgFill, blendedBgColor); - - return null; -}; diff --git a/src/api/services/payload/build-general-selection-payload.ts b/src/api/services/payload/build-general-selection-payload.ts index 2ed1a20..eeeab29 100644 --- a/src/api/services/payload/build-general-selection-payload.ts +++ b/src/api/services/payload/build-general-selection-payload.ts @@ -1,4 +1,4 @@ -import { processNodeForSelection } from '~api/services/selection/process-node-for-selection.ts'; +import { processNodeFromMultipleSelection } from '~api/services/selection/process-node-from-multiple-selection.ts'; import { type SelectionChangeMessage } from '~types/messages.ts'; import { notEmpty } from '~utils/not-empty.ts'; @@ -6,7 +6,7 @@ export const buildGeneralSelectionPayload = ( selection: readonly SceneNode[] ): SelectionChangeMessage => { const selectedNodePairs = selection - .map((node) => processNodeForSelection(node)) + .map((fg) => processNodeFromMultipleSelection(fg)) .filter(notEmpty); return { diff --git a/src/api/services/selection/process-node-for-selection.ts b/src/api/services/selection/process-node-for-selection.ts deleted file mode 100644 index 2188d2c..0000000 --- a/src/api/services/selection/process-node-for-selection.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { handleOpaqueBackground } from '~api/services/colors/handle-opaque-background.ts'; -import { handleTransparentBackground } from '~api/services/colors/handle-transparent-background.ts'; -import { getIntersectingNodesWithPage } from '~api/services/figma/intersections/get-intersecting-nodes-with-page.ts'; -import { createFigmaNode } from '~api/services/figma/nodes/create-figma-node.ts'; -import { isLayerHasTransparency } from '~api/services/figma/visibility/is-layer-has-transparency.ts'; -import { type ColorPair } from '~api/types.ts'; -import { notEmpty } from '~utils/not-empty.ts'; - -export const processNodeForSelection = (node: SceneNode): ColorPair | null => { - const figmaNode = createFigmaNode(node); - const intersectingNodes = getIntersectingNodesWithPage(node); - - if (intersectingNodes.length === 0) return null; - - const [firstIntersectingNode] = intersectingNodes; - - if (!notEmpty(firstIntersectingNode)) return null; - - if (isLayerHasTransparency(firstIntersectingNode)) { - return handleTransparentBackground(figmaNode, intersectingNodes); - } else { - return handleOpaqueBackground(figmaNode, firstIntersectingNode); - } -}; diff --git a/src/api/services/selection/process-node-from-multiple-selection.ts b/src/api/services/selection/process-node-from-multiple-selection.ts new file mode 100644 index 0000000..ca9a1df --- /dev/null +++ b/src/api/services/selection/process-node-from-multiple-selection.ts @@ -0,0 +1,59 @@ +import { buildColorsPair } from '~api/services/colors/build-colors-pair.ts'; +import { getComputedNodeFill } from '~api/services/colors/get-computed-node-fill.ts'; +import { blendLayersColors } from '~api/services/figma/blend/blend-layers-colors.ts'; +import { getIntersectingNodesWithPage } from '~api/services/figma/intersections/get-intersecting-nodes-with-page.ts'; +import { createFigmaNode } from '~api/services/figma/nodes/create-figma-node.ts'; +import { isLayerHasTransparency } from '~api/services/figma/visibility/is-layer-has-transparency.ts'; +import { type ColorPair } from '~api/types.ts'; +import { type FigmaNode, type FigmaPaint } from '~types/figma.ts'; +import { getFirstVisibleNodeFill } from '~utils/figma/get-first-visible-node-fill.ts'; +import { notEmpty } from '~utils/not-empty.ts'; + +export const processNodeFromMultipleSelection = ( + fg: SceneNode +): ColorPair | null => { + const fgNode = createFigmaNode(fg); + const intersectingNodes = getIntersectingNodesWithPage(fg); + + if (intersectingNodes.length === 0) return null; + + const [firstIntersectingNode] = intersectingNodes; + + if (!notEmpty(firstIntersectingNode)) return null; + + const fgFill = getComputedNodeFill(fgNode); + + if (!notEmpty(fgFill)) return null; + + if (isLayerHasTransparency(firstIntersectingNode)) { + return handleTransparentBackground(fgNode.id, fgFill, intersectingNodes); + } else { + return handleOpaqueBackground(fgNode.id, fgFill, firstIntersectingNode); + } +}; + +export const handleOpaqueBackground = ( + id: string, + fgFill: FigmaPaint, + firstIntersectingNode: FigmaNode +): ColorPair | null => { + const bgFill = getFirstVisibleNodeFill(firstIntersectingNode.fills); + + if (notEmpty(fgFill) && notEmpty(bgFill)) + return buildColorsPair(id, fgFill, bgFill); + + return null; +}; + +export const handleTransparentBackground = ( + id: string, + fgFill: FigmaPaint, + intersectingNodes: FigmaNode[] +): ColorPair | null => { + const blendedBgColor = blendLayersColors(intersectingNodes); + + if (notEmpty(fgFill) && notEmpty(blendedBgColor)) + return buildColorsPair(id, fgFill, blendedBgColor); + + return null; +};