Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

1.4 KB
Raw Blame
import type { Appearance } from './model';

export const resolveAppearance = (appearance: Appearance) => ({
  ...appearance,
  strokeWidth: appearance.strokeWidth ?? 2,
  strokeStyle: appearance.strokeStyle ?? 'solid',
  opacity: appearance.opacity ?? 1,
  cornerRadius: appearance.cornerRadius ?? 0,
});

export function validAppearance(value: unknown): value is Appearance {
  if (
    !value ||
    typeof value !== 'object' ||
    !('fill' in value) ||
    typeof value.fill !== 'string' ||
    !('stroke' in value) ||
    typeof value.stroke !== 'string'
  )
    return false;
  for (const key of ['strokeWidth', 'opacity', 'cornerRadius'] as const) {
    if (!(key in value)) continue;
    const number = value[key as keyof typeof value];
    if (number === undefined) continue;
    if (
      typeof number !== 'number' ||
      !Number.isFinite(number) ||
      number < 0 ||
      (key === 'opacity' && number > 1)
    )
      return false;
  }
  if (
    'strokeStyle' in value &&
    value.strokeStyle !== undefined &&
    !['solid', 'dashed', 'dotted'].includes(value.strokeStyle as string)
  )
    return false;
  return true;
}

export function strokeDasharray(appearance: Appearance): string | undefined {
  const { strokeWidth, strokeStyle } = resolveAppearance(appearance);
  if (strokeStyle === 'dashed') return `${strokeWidth * 4} ${strokeWidth * 3}`;
  if (strokeStyle === 'dotted') return `0 ${strokeWidth * 2.5}`;
  return undefined;
}