Skip to content

Commit

Permalink
Improve modularity.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhin committed Sep 27, 2023
1 parent 6022737 commit 6ab101a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 66 deletions.
20 changes: 0 additions & 20 deletions src/api/services/colors/handle-opaque-background.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/api/services/colors/handle-transparent-background.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/api/services/payload/build-general-selection-payload.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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';

export const buildGeneralSelectionPayload = (
selection: readonly SceneNode[]
): SelectionChangeMessage => {
const selectedNodePairs = selection
.map((node) => processNodeForSelection(node))
.map((fg) => processNodeFromMultipleSelection(fg))
.filter(notEmpty);

return {
Expand Down
24 changes: 0 additions & 24 deletions src/api/services/selection/process-node-for-selection.ts

This file was deleted.

59 changes: 59 additions & 0 deletions src/api/services/selection/process-node-from-multiple-selection.ts
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 6ab101a

Please sign in to comment.