Skip to content

Commit

Permalink
add support for browser and switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
RobMayer committed Aug 9, 2023
1 parent bdff26d commit ba53f7c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jsx-in-ttpg",
"license": "UNLICENSE",
"version": "1.1.3",
"version": "1.1.4",
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts --no-splitting",
"clean": "rm -rf ./dist",
Expand Down
67 changes: 62 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import {
Player,
TextJustification,
VerticalAlignment,
UIElement,
ScreenUIElement,
WebBrowser,
WidgetSwitcher,
} from "@tabletop-playground/api";

type CanvasChild<T> = {
Expand All @@ -42,7 +42,7 @@ type BoxChild<T> = {
weight?: number;
};

export const useRef = <T extends Widget>(initial: T | null = null): RefHandle<T> => {
export const useRef = <T>(initial: T | null = null): RefHandle<T> => {
const ref = {
current: initial,
clear: () => {
Expand Down Expand Up @@ -71,8 +71,8 @@ export const render = (children: JSXNode): Widget => {
type ArrayOr<T> = T | T[];

export type JSXNode = JSX.Element;
export type RefHandle<T extends Widget> = { current: T | null; clear: () => void };
export type RefObject<T extends Widget> = { current: T | null };
export type RefHandle<T> = { current: T | null; clear: () => void };
export type RefObject<T> = { current: T | null };

type PossibleChildren = JSX.Element | ArrayOr<JSX.Element> | ArrayOr<BoxChild<JSX.Element>> | ArrayOr<CanvasChild<JSX.Element>> | TextNode;
export type TextNode = ArrayOr<string | undefined | null | boolean | number>;
Expand Down Expand Up @@ -115,6 +115,8 @@ export type JsxInTTPGElement<T extends Widget> =
T extends SelectionBox ? JSX.IntrinsicElements["select"] :
T extends Slider ? JSX.IntrinsicElements["slider"] :
T extends TextBox ? JSX.IntrinsicElements["input"] :
T extends WebBrowser ? JSX.IntrinsicElements["browser"] :
T extends WidgetSwitcher ? JSX.IntrinsicElements["switch"] :
never;

const ensureWidgets = (...children: PossibleChildren[]): Widget[] => {
Expand Down Expand Up @@ -252,6 +254,10 @@ const createElement = <const T extends keyof JSX.IntrinsicElements>(tag: T, attr
return sliderElement(attrs as JSX.IntrinsicElements["slider"]);
case "input":
return inputElement(attrs as JSX.IntrinsicElements["input"]);
case "browser":
return browserElement(attrs as JSX.IntrinsicElements["browser"]);
case "switch":
return switchElement(attrs as JSX.IntrinsicElements["switch"], ensureWidgets(...children));
}
};

Expand Down Expand Up @@ -626,6 +632,40 @@ const inputElement = (attrs: JSX.IntrinsicElements["input"]) => {
return element;
};

const browserElement = (attrs: JSX.IntrinsicElements["browser"]) => {
const element = new WebBrowser();
doCommon(element, attrs);
if (attrs.onChange) {
element.onURLChanged.add(attrs.onChange);
}
if (attrs.onLoadFinish) {
element.onLoadFinished.add(attrs.onLoadFinish);
}
if (attrs.onLoadStart) {
element.onLoadStarted.add(attrs.onLoadStart);
}
if (attrs.url) {
element.setURL(attrs.url);
}
return element;
};

const switchElement = (attrs: JSX.IntrinsicElements["switch"], children?: Widget[]) => {
const element = new WidgetSwitcher();
doCommon(element, attrs);
if (attrs.value) {
if (attrs.value instanceof Widget) {
element.setActiveWidget(attrs.value);
} else {
element.setActiveIndex(attrs.value);
}
}
if (children) {
children.forEach(element.addChild);
}
return element;
};

const INPUT_TYPES = {
string: 0,
float: 1,
Expand Down Expand Up @@ -932,6 +972,23 @@ declare global {
fontPackage?: string;
}
);
browser: {
ref?: { current: WebBrowser | null };
disabled?: boolean;
hidden?: boolean;
onLoadStart?: () => void;
onLoadFinish?: () => void;
onChange?: () => void;
url?: string;
children?: never;
};
switch: {
ref?: { current: WidgetSwitcher | null };
disabled?: boolean;
hidden?: boolean;
children?: ArrayOr<JSX.Element>;
value?: number | Widget;
};
}
}
}

0 comments on commit ba53f7c

Please sign in to comment.