UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

1 lines • 10.4 kB
{"version":3,"file":"view.js","names":[],"sources":["esm/platform/widgets/view.js"],"sourcesContent":["import {\n Children,\n Fragment,\n isValidElement\n} from \"react\";\nconst WidgetUI = Object.freeze({\n Text: \"one-widget-text\",\n VStack: \"one-widget-vstack\",\n HStack: \"one-widget-hstack\",\n ZStack: \"one-widget-zstack\",\n Spacer: \"one-widget-spacer\",\n Divider: \"one-widget-divider\",\n Image: \"one-widget-image\",\n Progress: \"one-widget-progress\",\n Gauge: \"one-widget-gauge\",\n Circle: \"one-widget-circle\",\n Rectangle: \"one-widget-rectangle\",\n RoundedRectangle: \"one-widget-rounded-rectangle\",\n Link: \"one-widget-link\"\n});\nconst hexColor = /^#[0-9a-fA-F]{6}$/;\nconst styleNumberKeys = [\n \"fontSize\",\n \"padding\",\n \"borderRadius\",\n \"spacing\",\n \"width\",\n \"height\",\n \"opacity\"\n];\nconst styleStringKeys = [\"fontWeight\", \"fontDesign\", \"alignment\"];\nfunction checkStyle(style, fill) {\n if (fill != null && (typeof fill !== \"string\" || !hexColor.test(fill))) {\n throw new Error(\"WidgetUI fill must be six-digit hex like #1685B1\");\n }\n if (style == null) return;\n if (typeof style !== \"object\" || Array.isArray(style)) {\n throw new Error(\"WidgetUI style must be an object\");\n }\n for (const key of [\"color\", \"backgroundColor\"]) {\n const value = style[key];\n if (value != null && (typeof value !== \"string\" || !hexColor.test(value))) {\n throw new Error(`WidgetUI ${key} must be six-digit hex like #1685B1`);\n }\n }\n if (style.lineLimit != null && (typeof style.lineLimit !== \"number\" || !Number.isInteger(style.lineLimit))) {\n throw new Error(\"WidgetUI lineLimit must be an integer\");\n }\n for (const key of styleNumberKeys) {\n if (style[key] != null && typeof style[key] !== \"number\") {\n throw new Error(`WidgetUI ${key} must be a number`);\n }\n }\n for (const key of styleStringKeys) {\n if (style[key] != null && typeof style[key] !== \"string\") {\n throw new Error(`WidgetUI ${key} must be a string`);\n }\n }\n}\nfunction node(view) {\n if (view == null || typeof view === \"boolean\") return null;\n if (typeof view === \"string\" || typeof view === \"number\") {\n return { type: \"text\", text: String(view) };\n }\n if (Array.isArray(view)) {\n return { type: \"vstack\", children: children(view) };\n }\n if (!isValidElement(view)) {\n throw new Error(\"Widget JSX needs a WidgetUI root\");\n }\n if (view.type === Fragment) {\n return { type: \"vstack\", children: children(view.props.children) };\n }\n if (typeof view.type === \"function\") {\n if (view.type.prototype && \"isReactComponent\" in view.type.prototype) {\n throw new Error(\"Widget JSX supports pure function components\");\n }\n return node(view.type(view.props));\n }\n const type = typeof view.type === \"string\" && view.type.startsWith(\"one-widget-\") ? view.type.slice(\"one-widget-\".length) : void 0;\n if (!type || ![\n \"text\",\n \"vstack\",\n \"hstack\",\n \"zstack\",\n \"spacer\",\n \"divider\",\n \"image\",\n \"progress\",\n \"gauge\",\n \"circle\",\n \"rectangle\",\n \"rounded-rectangle\",\n \"link\"\n ].includes(type)) {\n throw new Error(\"Widget JSX only supports WidgetUI primitives and pure components\");\n }\n const {\n children: content,\n style,\n ...props\n } = view.props;\n checkStyle(style, props.fill);\n if (props.cornerRadius != null && typeof props.cornerRadius !== \"number\") {\n throw new Error(\"WidgetUI cornerRadius must be a number\");\n }\n if (type === \"text\") {\n const parts = Children.toArray(content);\n if (parts.some((part) => typeof part !== \"string\" && typeof part !== \"number\")) {\n throw new Error(\"WidgetUI.Text children must be text or numbers\");\n }\n return { type, text: parts.join(\"\"), style };\n }\n if (type === \"image\") {\n if (typeof props.systemName !== \"string\" || !props.systemName) {\n throw new Error(\"WidgetUI.Image needs a systemName\");\n }\n return { type, style, systemName: props.systemName };\n }\n if (type === \"progress\" || type === \"gauge\") {\n if (!Number.isFinite(props.value) || !Number.isFinite(props.total ?? 1) || (props.total ?? 1) <= 0) {\n throw new Error(\n \"WidgetUI progress values must be finite and total must be positive\"\n );\n }\n return { type, style, value: props.value, total: props.total ?? 1 };\n }\n if (type === \"circle\" || type === \"rectangle\" || type === \"rounded-rectangle\") {\n return { type, style, fill: props.fill, cornerRadius: props.cornerRadius };\n }\n if (type === \"link\") {\n let valid = false;\n try {\n valid = Boolean(new URL(props.url).protocol);\n } catch {\n }\n if (!valid) {\n throw new Error(\"WidgetUI.Link needs an absolute URL\");\n }\n return { type, style, url: props.url, children: children(content) };\n }\n if (type === \"spacer\" || type === \"divider\") return { type, style };\n return { type, style, children: children(content) };\n}\nfunction children(content) {\n const nodes = [];\n for (const child of Children.toArray(content)) {\n const next = node(child);\n if (next) nodes.push(next);\n }\n return nodes;\n}\nfunction encodeWidgetView(view) {\n const root = node(view);\n if (!root) {\n throw new Error(\"Widget JSX needs a WidgetUI root\");\n }\n return JSON.stringify(root);\n}\nfunction encodeActivityView(view) {\n const lockScreen = node(view.lockScreen);\n if (!lockScreen) {\n throw new Error(\"Live Activity JSX needs a lockScreen layout\");\n }\n const encoded = JSON.stringify({\n lockScreen,\n compactLeading: view.compactLeading == null ? null : node(view.compactLeading),\n compactTrailing: view.compactTrailing == null ? null : node(view.compactTrailing),\n minimal: view.minimal == null ? null : node(view.minimal),\n expandedLeading: view.expandedLeading == null ? null : node(view.expandedLeading),\n expandedTrailing: view.expandedTrailing == null ? null : node(view.expandedTrailing),\n expandedBottom: view.expandedBottom == null ? null : node(view.expandedBottom)\n });\n if (new TextEncoder().encode(encoded).length > 3500) {\n throw new Error(\"Live Activity JSX must fit within 3500 UTF-8 bytes\");\n }\n return encoded;\n}\nexport {\n WidgetUI,\n encodeActivityView,\n encodeWidgetView\n};\n//# sourceMappingURL=view.js.map\n"],"mappings":";;;AAKA,MAAM,WAAW,OAAO,OAAO;CAC7B,MAAM;CACN,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,OAAO;CACP,UAAU;CACV,OAAO;CACP,QAAQ;CACR,WAAW;CACX,kBAAkB;CAClB,MAAM;AACR,CAAC;AACD,MAAM,WAAW;AACjB,MAAM,kBAAkB;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;A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