All files / webview-app/src/components/Canvas fixedZoneLayout.ts

40.84% Statements 29/71
14.28% Branches 6/42
5.88% Functions 1/17
42.64% Lines 29/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198                                                                                                                              5x   5x 5x   5x 5x     5x       5x             5x       5x     5x     5x       5x       5x       5x           5x         5x   5x       5x 5x                 5x     5x 5x               5x 5x               5x 5x                             5x 5x                             5x                        
import {
  FIXED_ZONE_GAP,
  FIXED_ZONE_PADDING,
  FIXED_ZONE_SECTION_GAP,
  MENU_BAR_MIN_HEIGHT,
  TOOL_BAR_MIN_SIDE_WIDTH,
  TOOL_BAR_MIN_THICKNESS,
} from "@/components/Canvas/constants";
import {
  collectDescendantIds,
  getStackExtent,
  normalizeToolBarPosition,
  type ToolBarEdge,
} from "@/components/Canvas/fixedZoneHelpers";
import type { CanvasComponent as CanvasComponentModel } from "@/types/canvas";
 
export interface MenuBarLayoutItem {
  menuBar: CanvasComponentModel;
  top: number;
  height: number;
}
 
export interface HorizontalToolBarLayoutItem {
  toolBar: CanvasComponentModel;
  thickness: number;
  top?: number;
  bottom?: number;
}
 
export interface VerticalToolBarLayoutItem {
  toolBar: CanvasComponentModel;
  top: number;
  thickness: number;
  width: number;
}
 
interface FixedZoneLayout {
  componentsById: Map<string, CanvasComponentModel>;
  floatingComponents: CanvasComponentModel[];
  menuBarLayout: MenuBarLayoutItem[];
  northToolBarLayout: HorizontalToolBarLayoutItem[];
  southToolBarLayout: HorizontalToolBarLayoutItem[];
  westToolBarLayout: VerticalToolBarLayoutItem[];
  eastToolBarLayout: VerticalToolBarLayoutItem[];
  sideTopInset: number;
  sideBottomInset: number;
}
 
export function getComponentLabel(component: CanvasComponentModel, fallback: string): string {
  const text = component.text.trim();
  if (text.length > 0) {
    return text;
  }
 
  const variableName = component.variableName.trim();
  if (variableName.length > 0) {
    return variableName;
  }
 
  return fallback;
}
 
export function buildFixedZoneLayout(components: CanvasComponentModel[]): FixedZoneLayout {
  const componentsById = new Map(components.map((component) => [component.id, component]));
 
  const menuBars = components.filter((component) => component.type === "MenuBar");
  const toolBarComponents = components.filter((component) => component.type === "ToolBar");
 
  const fixedComponentIds = new Set<string>();
  for (const menuBar of menuBars) {
    collectDescendantIds(menuBar, componentsById, components, fixedComponentIds);
  }
  for (const toolBar of toolBarComponents) {
    collectDescendantIds(toolBar, componentsById, components, fixedComponentIds);
  }
 
  const toolBarsByEdge: Record<ToolBarEdge, CanvasComponentModel[]> = {
    north: [],
    south: [],
    east: [],
    west: [],
  };
 
  for (const toolBar of toolBarComponents) {
    toolBarsByEdge[normalizeToolBarPosition(toolBar.position)].push(toolBar);
  }
 
  const menuBarHeights = menuBars.map((menuBar) =>
    Math.max(MENU_BAR_MIN_HEIGHT, Math.round(menuBar.height || MENU_BAR_MIN_HEIGHT)),
  );
  const northToolBarSizes = toolBarsByEdge.north.map((toolBar) =>
    Math.max(TOOL_BAR_MIN_THICKNESS, Math.round(toolBar.height || TOOL_BAR_MIN_THICKNESS)),
  );
  const southToolBarSizes = toolBarsByEdge.south.map((toolBar) =>
    Math.max(TOOL_BAR_MIN_THICKNESS, Math.round(toolBar.height || TOOL_BAR_MIN_THICKNESS)),
  );
 
  const menuBarStackHeight = getStackExtent(
    menuBars,
    (_, index) => menuBarHeights[index] ?? MENU_BAR_MIN_HEIGHT,
  );
  const northToolBarStackHeight = getStackExtent(
    toolBarsByEdge.north,
    (_, index) => northToolBarSizes[index] ?? TOOL_BAR_MIN_THICKNESS,
  );
  const southToolBarStackHeight = getStackExtent(
    toolBarsByEdge.south,
    (_, index) => southToolBarSizes[index] ?? TOOL_BAR_MIN_THICKNESS,
  );
 
  const topFixedHeight =
    menuBarStackHeight +
    northToolBarStackHeight +
    (menuBarStackHeight > 0 && northToolBarStackHeight > 0 ? FIXED_ZONE_SECTION_GAP : 0);
 
  const sideTopInset =
    FIXED_ZONE_PADDING + topFixedHeight + (topFixedHeight > 0 ? FIXED_ZONE_SECTION_GAP : 0);
  const sideBottomInset =
    FIXED_ZONE_PADDING +
    southToolBarStackHeight +
    (southToolBarStackHeight > 0 ? FIXED_ZONE_SECTION_GAP : 0);
 
  let menuTopOffset = FIXED_ZONE_PADDING;
  const menuBarLayout = menuBars.map((menuBar, index) => {
    const height = menuBarHeights[index] ?? MENU_BAR_MIN_HEIGHT;
    const top = menuTopOffset;
    menuTopOffset += height + FIXED_ZONE_GAP;
 
    return { menuBar, top, height };
  });
 
  const northToolBarsStart =
    FIXED_ZONE_PADDING +
    menuBarStackHeight +
    (menuBarStackHeight > 0 && toolBarsByEdge.north.length > 0 ? FIXED_ZONE_SECTION_GAP : 0);
  let northTopOffset = northToolBarsStart;
  const northToolBarLayout = toolBarsByEdge.north.map((toolBar, index) => {
    const thickness = northToolBarSizes[index] ?? TOOL_BAR_MIN_THICKNESS;
    const top = northTopOffset;
    northTopOffset += thickness + FIXED_ZONE_GAP;
 
    return { toolBar, top, thickness };
  });
 
  let southBottomOffset = FIXED_ZONE_PADDING;
  const southToolBarLayout = toolBarsByEdge.south.map((toolBar, index) => {
    const thickness = southToolBarSizes[index] ?? TOOL_BAR_MIN_THICKNESS;
    const bottom = southBottomOffset;
    southBottomOffset += thickness + FIXED_ZONE_GAP;
 
    return { toolBar, bottom, thickness };
  });
 
  let westTopOffset = sideTopInset;
  const westToolBarLayout = toolBarsByEdge.west.map((toolBar) => {
    const thickness = Math.max(
      TOOL_BAR_MIN_THICKNESS,
      Math.round(toolBar.height || TOOL_BAR_MIN_THICKNESS),
    );
    const width = Math.max(
      TOOL_BAR_MIN_SIDE_WIDTH,
      Math.round(toolBar.width || TOOL_BAR_MIN_SIDE_WIDTH),
    );
    const top = westTopOffset;
    westTopOffset += thickness + FIXED_ZONE_GAP;
 
    return { toolBar, top, thickness, width };
  });
 
  let eastTopOffset = sideTopInset;
  const eastToolBarLayout = toolBarsByEdge.east.map((toolBar) => {
    const thickness = Math.max(
      TOOL_BAR_MIN_THICKNESS,
      Math.round(toolBar.height || TOOL_BAR_MIN_THICKNESS),
    );
    const width = Math.max(
      TOOL_BAR_MIN_SIDE_WIDTH,
      Math.round(toolBar.width || TOOL_BAR_MIN_SIDE_WIDTH),
    );
    const top = eastTopOffset;
    eastTopOffset += thickness + FIXED_ZONE_GAP;
 
    return { toolBar, top, thickness, width };
  });
 
  return {
    componentsById,
    floatingComponents: components.filter((component) => !fixedComponentIds.has(component.id)),
    menuBarLayout,
    northToolBarLayout,
    southToolBarLayout,
    westToolBarLayout,
    eastToolBarLayout,
    sideTopInset,
    sideBottomInset,
  };
}