import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { AlignBar, AlignmentGrid, InspectorSection, PaddingBox, type PaddingSide, PaintList, SizingControl } from "./ui/uicontext/components/canvas/Inspector.ts"import { Layout, Paint, Sizing } from "./ui/uicontext/components/canvas/Scene.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const styles = stylex.create({ panel: { inlineSize: "min(100%, 272px)", borderWidth: 1, borderStyle: "solid", borderColor: "light-dark(#ececef, #232327)", borderRadius: 12, overflow: "hidden" } })const Align = Schema.Literals(["start", "center", "end"])const Message = UiSchema.TaggedEnum({ PickedAlign: { x: Align, y: Align }, ChangedPadding: { side: Schema.Literals(["top", "right", "bottom", "left"]), value: Schema.Number }, PickedSizing: { axis: Schema.Literals(["w", "h"]), value: Sizing }, AddedFill: {}, RemovedFill: { id: Schema.String }, ToggledFill: { id: Schema.String, visible: Schema.Boolean }, OpenedFill: { id: Schema.NullOr(Schema.String) }, ColoredFill: { id: Schema.String, color: Schema.String, token: Schema.NullOr(Schema.String) }, FadedFill: { id: Schema.String, opacity: Schema.Number }, Aligned: {},})const sides: ReadonlyArray<PaddingSide> = ["top", "right", "bottom", "left"]export const CardPanel = Html.component({ name: "CardPanel", model: Schema.Struct({ layout: Layout, w: Sizing, h: Sizing, fills: Schema.Array(Paint), open: Schema.NullOr(Schema.String), next: Schema.Number }), message: Message,}).make({ init: () => ({ layout: { direction: "row" as const, gap: 12, padding: [16, 20, 16, 20] as const, alignX: "start" as const, alignY: "center" as const }, w: "fill" as const, h: "hug" as const, fills: [{ id: "f1", color: "#FFFFFF", token: "color/bg" }], open: null, next: 2, }), update: { PickedAlign: ($, { x, y }) => $.evolve({ layout: (l) => ({ ...l, alignX: x, alignY: y }) }), ChangedPadding: ($, { side, value }) => $.evolve({ layout: (l) => ({ ...l, padding: l.padding.map((p, i) => (i === sides.indexOf(side) ? Math.max(0, value) : p)) as unknown as typeof l.padding }) }), PickedSizing: ($, { axis, value }) => (axis === "w" ? $.evolve({ w: value }) : $.evolve({ h: value })), AddedFill: ($) => $.evolve({ fills: (f) => [...f, { id: `f${$.model.next}`, color: "#3D5AFE", opacity: 0.1 }], next: (n) => n + 1 }), RemovedFill: ($, { id }) => $.evolve({ fills: (f) => f.filter((p) => p.id !== id) }), ToggledFill: ($, { id, visible }) => $.evolve({ fills: (f) => f.map((p) => (p.id === id ? { ...p, visible } : p)) }), OpenedFill: ($, { id }) => $.evolve({ open: id }), ColoredFill: ($, { id, color, token }) => $.evolve({ fills: (f) => f.map((p) => (p.id === id ? { id, color, ...(token !== null && { token }), ...(p.opacity !== undefined && { opacity: p.opacity }) } : p)) }), FadedFill: ($, { id, opacity }) => $.evolve({ fills: (f) => f.map((p) => (p.id === id ? { ...p, opacity } : p)) }), Aligned: ($) => $.model, }, view: (h, m) => h.div.attrs(sx(styles.panel)).children( InspectorSection(h, { title: "Position", children: [AlignBar(h, { count: 1, onAlign: () => Message.Aligned(), onDistribute: () => Message.Aligned() })] }), InspectorSection(h, { title: "Auto layout", children: [ SizingControl(h, { axis: "w", value: m.w, allow: ["fixed", "hug", "fill"], onPick: (value) => Message.PickedSizing({ axis: "w", value }) }), SizingControl(h, { axis: "h", value: m.h, allow: ["fixed", "hug", "fill"], onPick: (value) => Message.PickedSizing({ axis: "h", value }) }), AlignmentGrid(h, { x: m.layout.alignX, y: m.layout.alignY, direction: m.layout.direction, onPick: (x, y) => Message.PickedAlign({ x, y }) }), PaddingBox(h, { key: "padding", value: m.layout.padding, onChange: (side, value) => Message.ChangedPadding({ side, value }), onShift: (side, by) => Message.ChangedPadding({ side, value: m.layout.padding[sides.indexOf(side)]! + by }), }), ], }), PaintList(h, { id: "fill", title: "Fill", paints: m.fills, open: m.open, tokens: [{ name: "color/bg", color: "#FFFFFF" }, { name: "color/accent", color: "#3D5AFE" }], onAdd: Message.AddedFill(), onRemove: (id) => Message.RemovedFill({ id }), onToggle: (id, visible) => Message.ToggledFill({ id, visible }), onOpen: (id) => Message.OpenedFill({ id }), onColor: (id, color) => Message.ColoredFill({ id, color, token: null }), onToken: (id, token, color) => Message.ColoredFill({ id, color, token }), onOpacity: (id, opacity) => Message.FadedFill({ id, opacity }), }), ),})