ultisuite-client/lib/drive/docs-table-styles.ts
R3D347HR4Y 303b2b1074
Some checks are pending
E2E / Playwright e2e (push) Waiting to run
wow
2026-06-11 01:22:40 +02:00

79 lines
3.1 KiB
TypeScript

import type { DocsTableBorder } from "@/lib/drive/docs-table-types"
const BORDER_STYLE_MAP: Record<string, string> = {
single: "solid",
dashed: "dashed",
dotted: "dotted",
double: "double",
dotDash: "dashed",
dotDotDash: "dotted",
none: "none",
nil: "none",
}
export function docsTableBorderToCss(border: DocsTableBorder | string | null | undefined): string | null {
if (!border) return null
if (typeof border === "string") return border.trim() || null
const style = BORDER_STYLE_MAP[border.style ?? ""] ?? "solid"
if (style === "none") return "none"
const sizePt = border.size ? Math.max(0.25, border.size / 8) : 0.75
const color = border.color && border.color !== "auto" ? border.color : "#000000"
return `${sizePt}pt ${style} ${color}`
}
export function buildDocsTableCellStyle(attrs: Record<string, unknown>): string | undefined {
const parts: string[] = []
const backgroundColor =
typeof attrs.backgroundColor === "string" && attrs.backgroundColor ? attrs.backgroundColor : null
if (backgroundColor) parts.push(`background-color: ${backgroundColor}`)
const verticalAlign =
attrs.verticalAlign === "top" || attrs.verticalAlign === "middle" || attrs.verticalAlign === "bottom"
? attrs.verticalAlign
: null
if (verticalAlign) parts.push(`vertical-align: ${verticalAlign}`)
const borderTop = docsTableBorderToCss(attrs.borderTop as DocsTableBorder | string | null)
const borderRight = docsTableBorderToCss(attrs.borderRight as DocsTableBorder | string | null)
const borderBottom = docsTableBorderToCss(attrs.borderBottom as DocsTableBorder | string | null)
const borderLeft = docsTableBorderToCss(attrs.borderLeft as DocsTableBorder | string | null)
if (borderTop) parts.push(`border-top: ${borderTop}`)
if (borderRight) parts.push(`border-right: ${borderRight}`)
if (borderBottom) parts.push(`border-bottom: ${borderBottom}`)
if (borderLeft) parts.push(`border-left: ${borderLeft}`)
return parts.length ? parts.join("; ") : undefined
}
export function buildDocsTableStyle(attrs: Record<string, unknown>): string | undefined {
const parts: string[] = []
const alignment =
attrs.alignment === "left" || attrs.alignment === "center" || attrs.alignment === "right"
? attrs.alignment
: null
if (alignment === "center") parts.push("margin-left: auto", "margin-right: auto")
else if (alignment === "right") parts.push("margin-left: auto", "margin-right: 0")
else if (alignment === "left") parts.push("margin-left: 0", "margin-right: auto")
if (attrs.layout === "fixed") parts.push("table-layout: fixed")
const cellSpacing = typeof attrs.cellSpacing === "number" ? attrs.cellSpacing : null
if (cellSpacing && cellSpacing > 0) {
const px = Math.round((cellSpacing / 20) * 96 / 72)
parts.push(`border-spacing: ${px}px`)
}
return parts.length ? parts.join("; ") : undefined
}
export function buildDocsTableRowStyle(attrs: Record<string, unknown>): string | undefined {
const rowHeight = typeof attrs.rowHeight === "string" ? attrs.rowHeight : null
if (!rowHeight) return undefined
return `height: ${rowHeight}`
}