UNPKG

one

Version:

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

297 lines (294 loc) • 8.8 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var view_exports = {}; __export(view_exports, { WidgetUI: () => WidgetUI, encodeActivityView: () => encodeActivityView, encodeWidgetView: () => encodeWidgetView }); module.exports = __toCommonJS(view_exports); var import_react = require("react"); var WidgetUI = Object.freeze({ Text: "one-widget-text", VStack: "one-widget-vstack", HStack: "one-widget-hstack", ZStack: "one-widget-zstack", Spacer: "one-widget-spacer", Divider: "one-widget-divider", Image: "one-widget-image", Progress: "one-widget-progress", Gauge: "one-widget-gauge", Circle: "one-widget-circle", Rectangle: "one-widget-rectangle", RoundedRectangle: "one-widget-rounded-rectangle", Link: "one-widget-link" }); var hexColor = /^#[0-9a-fA-F]{6}$/; var styleNumberKeys = [ "fontSize", "padding", "borderRadius", "spacing", "width", "height", "opacity" ]; var styleStringKeys = [ "fontWeight", "fontDesign", "alignment" ]; function checkStyle(style, fill) { if (fill != null && (typeof fill !== "string" || !hexColor.test(fill))) { throw new Error("WidgetUI fill must be six-digit hex like #1685B1"); } if (style == null) return; if (typeof style !== "object" || Array.isArray(style)) { throw new Error("WidgetUI style must be an object"); } for (var _i = 0, _iter = ["color", "backgroundColor"]; _i < _iter.length; _i++) { var key = _iter[_i]; var value = style[key]; if (value != null && (typeof value !== "string" || !hexColor.test(value))) { throw new Error(`WidgetUI ${key} must be six-digit hex like #1685B1`); } } if (style.lineLimit != null && (typeof style.lineLimit !== "number" || !Number.isInteger(style.lineLimit))) { throw new Error("WidgetUI lineLimit must be an integer"); } var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0; try { for (var _iterator = styleNumberKeys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var key1 = _step.value; if (style[key1] != null && typeof style[key1] !== "number") { throw new Error(`WidgetUI ${key1} must be a number`); } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0; try { for (var _iterator1 = styleStringKeys[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) { var key2 = _step1.value; if (style[key2] != null && typeof style[key2] !== "string") { throw new Error(`WidgetUI ${key2} must be a string`); } } } catch (err) { _didIteratorError1 = true; _iteratorError1 = err; } finally { try { if (!_iteratorNormalCompletion1 && _iterator1.return != null) { _iterator1.return(); } } finally { if (_didIteratorError1) { throw _iteratorError1; } } } } function node(view) { if (view == null || typeof view === "boolean") return null; if (typeof view === "string" || typeof view === "number") { return { type: "text", text: String(view) }; } if (Array.isArray(view)) { return { type: "vstack", children: children(view) }; } if (!(0, import_react.isValidElement)(view)) { throw new Error("Widget JSX needs a WidgetUI root"); } if (view.type === import_react.Fragment) { return { type: "vstack", children: children(view.props.children) }; } if (typeof view.type === "function") { if (view.type.prototype && "isReactComponent" in view.type.prototype) { throw new Error("Widget JSX supports pure function components"); } return node(view.type(view.props)); } var type = typeof view.type === "string" && view.type.startsWith("one-widget-") ? view.type.slice("one-widget-".length) : void 0; if (!type || ![ "text", "vstack", "hstack", "zstack", "spacer", "divider", "image", "progress", "gauge", "circle", "rectangle", "rounded-rectangle", "link" ].includes(type)) { throw new Error("Widget JSX only supports WidgetUI primitives and pure components"); } var { children: content, style, ...props } = view.props; checkStyle(style, props.fill); if (props.cornerRadius != null && typeof props.cornerRadius !== "number") { throw new Error("WidgetUI cornerRadius must be a number"); } if (type === "text") { var parts = import_react.Children.toArray(content); if (parts.some(function(part) { return typeof part !== "string" && typeof part !== "number"; })) { throw new Error("WidgetUI.Text children must be text or numbers"); } return { type, text: parts.join(""), style }; } if (type === "image") { if (typeof props.systemName !== "string" || !props.systemName) { throw new Error("WidgetUI.Image needs a systemName"); } return { type, style, systemName: props.systemName }; } if (type === "progress" || type === "gauge") { var _props_total, _props_total1, _props_total2; if (!Number.isFinite(props.value) || !Number.isFinite((_props_total = props.total) !== null && _props_total !== void 0 ? _props_total : 1) || ((_props_total1 = props.total) !== null && _props_total1 !== void 0 ? _props_total1 : 1) <= 0) { throw new Error("WidgetUI progress values must be finite and total must be positive"); } return { type, style, value: props.value, total: (_props_total2 = props.total) !== null && _props_total2 !== void 0 ? _props_total2 : 1 }; } if (type === "circle" || type === "rectangle" || type === "rounded-rectangle") { return { type, style, fill: props.fill, cornerRadius: props.cornerRadius }; } if (type === "link") { var valid = false; try { valid = Boolean(new URL(props.url).protocol); } catch (unused) {} if (!valid) { throw new Error("WidgetUI.Link needs an absolute URL"); } return { type, style, url: props.url, children: children(content) }; } if (type === "spacer" || type === "divider") return { type, style }; return { type, style, children: children(content) }; } function children(content) { var nodes = []; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0; try { for (var _iterator = import_react.Children.toArray(content)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var child = _step.value; var next = node(child); if (next) nodes.push(next); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } return nodes; } function encodeWidgetView(view) { var root = node(view); if (!root) { throw new Error("Widget JSX needs a WidgetUI root"); } return JSON.stringify(root); } function encodeActivityView(view) { var lockScreen = node(view.lockScreen); if (!lockScreen) { throw new Error("Live Activity JSX needs a lockScreen layout"); } var encoded = JSON.stringify({ lockScreen, compactLeading: view.compactLeading == null ? null : node(view.compactLeading), compactTrailing: view.compactTrailing == null ? null : node(view.compactTrailing), minimal: view.minimal == null ? null : node(view.minimal), expandedLeading: view.expandedLeading == null ? null : node(view.expandedLeading), expandedTrailing: view.expandedTrailing == null ? null : node(view.expandedTrailing), expandedBottom: view.expandedBottom == null ? null : node(view.expandedBottom) }); if (new TextEncoder().encode(encoded).length > 3500) { throw new Error("Live Activity JSX must fit within 3500 UTF-8 bytes"); } return encoded; } //# sourceMappingURL=view.native.js.map