onejs-core 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/.gitattributes +2 -0
  2. package/.github/workflows/jsr.yml +19 -0
  3. package/.prettierrc +5 -0
  4. package/3rdparty/preact/LICENSE +21 -0
  5. package/3rdparty/preact/clone-element.ts +45 -0
  6. package/3rdparty/preact/compat/Children.ts +21 -0
  7. package/3rdparty/preact/compat/forwardRef.ts +49 -0
  8. package/3rdparty/preact/compat/index.ts +3 -0
  9. package/3rdparty/preact/compat/memo.ts +34 -0
  10. package/3rdparty/preact/compat/util.ts +38 -0
  11. package/3rdparty/preact/component.ts +235 -0
  12. package/3rdparty/preact/constants.ts +3 -0
  13. package/3rdparty/preact/create-context.ts +71 -0
  14. package/3rdparty/preact/create-element.ts +98 -0
  15. package/3rdparty/preact/diff/catch-error.ts +40 -0
  16. package/3rdparty/preact/diff/children.ts +355 -0
  17. package/3rdparty/preact/diff/index.ts +563 -0
  18. package/3rdparty/preact/diff/props.ts +174 -0
  19. package/3rdparty/preact/hooks/index.ts +536 -0
  20. package/3rdparty/preact/hooks/internal.d.ts +85 -0
  21. package/3rdparty/preact/hooks.d.ts +145 -0
  22. package/3rdparty/preact/index.ts +13 -0
  23. package/3rdparty/preact/internal.d.ts +155 -0
  24. package/3rdparty/preact/jsx-runtime/index.ts +80 -0
  25. package/3rdparty/preact/jsx.d.ts +1008 -0
  26. package/3rdparty/preact/options.ts +16 -0
  27. package/3rdparty/preact/preact.d.ts +317 -0
  28. package/3rdparty/preact/render.ts +76 -0
  29. package/3rdparty/preact/signals/index.ts +443 -0
  30. package/3rdparty/preact/signals/internal.d.ts +36 -0
  31. package/3rdparty/preact/signals-core/index.ts +663 -0
  32. package/3rdparty/preact/style.d.ts +205 -0
  33. package/3rdparty/preact/util.ts +29 -0
  34. package/@DO_NOT_CHANGE.txt +3 -0
  35. package/README.md +33 -0
  36. package/definitions/app.d.ts +52048 -0
  37. package/definitions/augments.d.ts +16 -0
  38. package/definitions/globals.d.ts +34 -0
  39. package/definitions/index.d.ts +9 -0
  40. package/definitions/jsx.d.ts +517 -0
  41. package/definitions/modules.d.ts +29 -0
  42. package/definitions/onejs.d.ts +164 -0
  43. package/definitions/preact.jsx.d.ts +7 -0
  44. package/definitions/proto-overrides.d.ts +13 -0
  45. package/definitions/puerts.d.ts +31 -0
  46. package/definitions/unity-engine.d.ts +23 -0
  47. package/hooks/eventful.ts +56 -0
  48. package/import-transform.mjs +42 -0
  49. package/index.ts +44 -0
  50. package/jsr.json +10 -0
  51. package/onejs-tw-config.cjs +188 -0
  52. package/package.json +9 -0
  53. package/preloads/inject.ts +44 -0
  54. package/styling/index.tsx +80 -0
  55. package/styling/utils/generateAlphabeticName.ts +21 -0
  56. package/styling/utils/generateComponentId.ts +6 -0
  57. package/styling/utils/hash.ts +46 -0
  58. package/switch.cjs +185 -0
  59. package/uss-transform-plugin.cjs +83 -0
  60. package/utils/color-palettes.ts +3 -0
  61. package/utils/color-parser.ts +249 -0
  62. package/utils/float-parser.ts +31 -0
  63. package/utils/index.ts +12 -0
@@ -0,0 +1,164 @@
1
+ /**
2
+ * If this throws tsc error, you need to remove some lines from index.d.ts that's
3
+ * generated by puerts. The lines are:
4
+ *
5
+ * public get alignContent(): any;
6
+ * public set alignContent(value: any);
7
+ * public get alignItems(): any;
8
+ * public set alignItems(value: any);
9
+ * ...
10
+ * public get wordSpacing(): any;
11
+ * public set wordSpacing(value: any);
12
+ */
13
+
14
+ // Don't do any import here, just declare the types
15
+
16
+ type VisualElement = CS.UnityEngine.UIElements.VisualElement;
17
+ type float2 = CS.Unity.Mathematics.float2;
18
+ type float3 = CS.Unity.Mathematics.float3;
19
+ type float4 = CS.Unity.Mathematics.float4;
20
+
21
+
22
+ type StyleEnum<T> = CS.UnityEngine.UIElements.StyleEnum$1<T>;
23
+ type Align = CS.UnityEngine.UIElements.Align;
24
+ type StyleColor = CS.UnityEngine.UIElements.StyleColor;
25
+ type Color = CS.UnityEngine.Color;
26
+ type StyleBackground = CS.UnityEngine.UIElements.StyleBackground;
27
+ type Background = CS.UnityEngine.UIElements.Background;
28
+ type StyleBackgroundSize = CS.UnityEngine.UIElements.StyleBackgroundSize;
29
+ type BackgroundSize = CS.UnityEngine.UIElements.BackgroundSize;
30
+ type StyleBackgroundRepeat = CS.UnityEngine.UIElements.StyleBackgroundRepeat;
31
+ type BackgroundRepeat = CS.UnityEngine.UIElements.BackgroundRepeat;
32
+ type StyleBackgroundPosition = CS.UnityEngine.UIElements.StyleBackgroundPosition;
33
+ type BackgroundPosition = CS.UnityEngine.UIElements.BackgroundPosition;
34
+ type StyleFloat = CS.UnityEngine.UIElements.StyleFloat;
35
+ type StyleLength = CS.UnityEngine.UIElements.StyleLength;
36
+ type Length = CS.UnityEngine.UIElements.Length;
37
+ type StyleCursor = CS.UnityEngine.UIElements.StyleCursor;
38
+ type StyleFont = CS.UnityEngine.UIElements.StyleFont;
39
+ type StyleFontDefinition = CS.UnityEngine.UIElements.StyleFontDefinition;
40
+ type StyleTextShadow = CS.UnityEngine.UIElements.StyleTextShadow;
41
+ type StyleTransformOrigin = CS.UnityEngine.UIElements.StyleTransformOrigin;
42
+ type StyleList<T> = CS.UnityEngine.UIElements.StyleList$1<T>;
43
+ type StylePropertyName = CS.UnityEngine.UIElements.StylePropertyName;
44
+ type EasingFunction = CS.UnityEngine.UIElements.EasingFunction;
45
+ type StyleTranslate = CS.UnityEngine.UIElements.StyleTranslate;
46
+ type StyleScale = CS.UnityEngine.UIElements.StyleScale;
47
+ type StyleInt = CS.UnityEngine.UIElements.StyleInt;
48
+ type Cursor = CS.UnityEngine.UIElements.Cursor;
49
+ type DisplayStyle = CS.UnityEngine.UIElements.DisplayStyle;
50
+ type Overflow = CS.UnityEngine.UIElements.Overflow;
51
+ type Position = CS.UnityEngine.UIElements.Position;
52
+ type Rotate = CS.UnityEngine.UIElements.Rotate;
53
+ type Scale = CS.UnityEngine.UIElements.Scale;
54
+ type TimeValue = CS.UnityEngine.UIElements.TimeValue;
55
+ type StyleRotate = CS.UnityEngine.UIElements.StyleRotate;
56
+ type Wrap = CS.UnityEngine.UIElements.Wrap;
57
+ type Justify = CS.UnityEngine.UIElements.Justify;
58
+ type FlexDirection = CS.UnityEngine.UIElements.FlexDirection;
59
+ type TextOverflow = CS.UnityEngine.UIElements.TextOverflow;
60
+ type TextShadow = CS.UnityEngine.UIElements.TextShadow;
61
+ type TransformOrigin = CS.UnityEngine.UIElements.TransformOrigin;
62
+ type Time = CS.UnityEngine.Time;
63
+ type StyleFontStyle = CS.UnityEngine.FontStyle;
64
+ type Translate = CS.UnityEngine.UIElements.Translate;
65
+ type FontDefinition = CS.UnityEngine.UIElements.FontDefinition;
66
+ type ScaleMode = CS.UnityEngine.ScaleMode;
67
+ type OverflowClipBox = CS.UnityEngine.UIElements.OverflowClipBox;
68
+ type Visibility = CS.UnityEngine.UIElements.Visibility;
69
+ type WhiteSpace = CS.UnityEngine.UIElements.WhiteSpace;
70
+ type TextOverflowPosition = CS.UnityEngine.UIElements.TextOverflowPosition;
71
+
72
+ declare namespace CS.OneJS.Dom {
73
+
74
+ interface DomStyle {
75
+ alignContent: StyleEnum<Align> | Align | string | null | number;
76
+ alignItems: StyleEnum<Align> | Align | string | null | number;
77
+ alignSelf: StyleEnum<Align> | Align | string | null | number;
78
+ backgroundColor: StyleColor | Color | string | null | number[] | float4;
79
+ backgroundImage: StyleBackground | Background | string | null | Sprite | Texture;
80
+ backgroundSize: StyleBackgroundSize | BackgroundSize | string | null;
81
+ backgroundRepeat: StyleBackgroundRepeat | BackgroundRepeat | string | null;
82
+ backgroundPosition: StyleBackgroundPosition | BackgroundPosition | string | null;
83
+ backgroundPositionX: StyleBackgroundPosition | BackgroundPosition | string | null;
84
+ backgroundPositionY: StyleBackgroundPosition | BackgroundPosition | string | null;
85
+ borderColor: StyleColor | Color | string | null | Color[] | number[] | float4;
86
+ borderWidth: StyleFloat | number | string | null | number[];
87
+ borderRadius: StyleLength | Length | string | null | number | number[];
88
+ borderBottomColor: StyleColor | Color | string | null | number[] | float4;
89
+ borderTopColor: StyleColor | Color | string | null | number[] | float4;
90
+ borderLeftColor: StyleColor | Color | string | null | number[] | float4;
91
+ borderRightColor: StyleColor | Color | string | null | number[] | float4;
92
+ borderBottomWidth: StyleFloat | number | string | null;
93
+ borderTopWidth: StyleFloat | number | string | null;
94
+ borderLeftWidth: StyleFloat | number | string | null;
95
+ borderRightWidth: StyleFloat | number | string | null;
96
+ borderTopLeftRadius: StyleLength | Length | string | null | number;
97
+ borderTopRightRadius: StyleLength | Length | string | null | number;
98
+ borderBottomRightRadius: StyleLength | Length | string | null | number;
99
+ borderBottomLeftRadius: StyleLength | Length | string | null | number;
100
+ bottom: StyleLength | Length | string | null | number;
101
+ color: StyleColor | Color | string | null | number[] | float4;
102
+ cursor: StyleCursor | Cursor | null;
103
+ display: StyleEnum<DisplayStyle> | DisplayStyle | string | null | number;
104
+ flexBasis: StyleLength | Length | string | null | number;
105
+ flexDirection: StyleEnum<FlexDirection> | FlexDirection | string | null | number;
106
+ flexGrow: StyleFloat | number | string | null;
107
+ flexShrink: StyleFloat | number | string | null;
108
+ flexWrap: StyleEnum<Wrap> | Wrap | string | null | number;
109
+ fontSize: StyleLength | Length | string | null | number;
110
+ height: StyleLength | Length | string | null | number;
111
+ justifyContent: StyleEnum<Justify> | Justify | string | null | number;
112
+ left: StyleLength | Length | string | null | number;
113
+ letterSpacing: StyleLength | Length | string | null | number;
114
+ margin: StyleLength | Length | string | null | number[] | number;
115
+ marginBottom: StyleLength | Length | string | null | number;
116
+ marginLeft: StyleLength | Length | string | null | number;
117
+ marginRight: StyleLength | Length | string | null | number;
118
+ marginTop: StyleLength | Length | string | null | number;
119
+ maxHeight: StyleLength | Length | string | null | number;
120
+ maxWidth: StyleLength | Length | string | null | number;
121
+ minHeight: StyleLength | Length | string | null | number;
122
+ minWidth: StyleLength | Length | string | null | number;
123
+ opacity: StyleFloat | number | string | null | number;
124
+ overflow: StyleEnum<Overflow> | Overflow | string | null | number;
125
+ padding: StyleLength | Length | string | null | number[] | number;
126
+ paddingBottom: StyleLength | Length | string | null | number;
127
+ paddingLeft: StyleLength | Length | string | null | number;
128
+ paddingRight: StyleLength | Length | string | null | number;
129
+ paddingTop: StyleLength | Length | string | null | number;
130
+ position: StyleEnum<Position> | Position | string | null | number;
131
+ right: StyleLength | Length | string | null | number;
132
+ rotate: StyleRotate | Rotate | string | null | number;
133
+ scale: StyleScale | Scale | string | null | number | number[] | float2;
134
+ textOverflow: StyleEnum<TextOverflow> | TextOverflow | string | null | number;
135
+ textShadow: StyleTextShadow | TextShadow | string | null;
136
+ top: StyleLength | Length | string | null | number;
137
+ transformOrigin: StyleTransformOrigin | TransformOrigin | string | string[] | null | number[] | float2;
138
+ transitionDelay: StyleList<TimeValue> | TimeValue | string | null | string[] | number | number[];
139
+ transitionDuration: StyleList<TimeValue> | TimeValue | string | null | string[] | number | number[];
140
+ transitionProperty: StyleList<StylePropertyName> | StylePropertyName | string | null | string[];
141
+ transitionTimingFunction: StyleList<EasingFunction> | EasingFunction | string | null | string[];
142
+ translate: StyleTranslate | Translate | string | null | number[];
143
+ unityBackgroundImageTintColor: StyleColor | Color | string | null | number[] | float4;
144
+ unityBackgroundScaleMode: StyleEnum<ScaleMode> | ScaleMode | string | null | number;
145
+ unityFont: StyleFont | UnityEngine.Font | string | null;
146
+ unityFontDefinition: StyleFontDefinition | FontDefinition | string | null;
147
+ unityFontStyleAndWeight: StyleEnum<UnityEngine.FontStyle> | UnityEngine.FontStyle | string | null | number;
148
+ unityOverflowClipBox: StyleEnum<OverflowClipBox> | OverflowClipBox | string | null | number;
149
+ unityParagraphSpacing: StyleLength | Length | string | null | number;
150
+ unitySliceBottom: StyleInt | number | string | null;
151
+ unitySliceLeft: StyleInt | number | string | null;
152
+ unitySliceRight: StyleInt | number | string | null;
153
+ unitySliceTop: StyleInt | number | string | null;
154
+ unitySliceScale: StyleFloat | number | string | null;
155
+ unityTextAlign: StyleEnum<UnityEngine.TextAnchor> | UnityEngine.TextAnchor | string | null | number;
156
+ unityTextOutlineColor: StyleColor | Color | string | null | number[] | float4;
157
+ unityTextOutlineWidth: StyleFloat | number | string | null;
158
+ unityTextOverflowPosition: StyleEnum<TextOverflowPosition> | TextOverflowPosition | string | null | number;
159
+ visibility: StyleEnum<Visibility> | Visibility | string | null | number;
160
+ whiteSpace: StyleEnum<WhiteSpace> | WhiteSpace | string | null | number;
161
+ width: StyleLength | Length | string | null | number;
162
+ wordSpacing: StyleLength | Length | string | null | number;
163
+ }
164
+ }
@@ -0,0 +1,7 @@
1
+ declare module "preact/jsx" {
2
+ namespace JSXInternal {
3
+ interface IntrinsicElements extends JSX.IntrinsicElements {
4
+
5
+ }
6
+ }
7
+ }
@@ -0,0 +1,13 @@
1
+
2
+
3
+ declare namespace CS.UnityEngine {
4
+ interface GameObject {
5
+ GetComp<T>(type: { new(): T }): T
6
+ AddComp<T>(type: { new(): T }): T
7
+ }
8
+
9
+ interface Component {
10
+ GetComp<T>(type: { new(): T }): T
11
+ AddComp<T>(type: { new(): T }): T
12
+ }
13
+ }
@@ -0,0 +1,31 @@
1
+ declare enum __Puerts_CSharpEnum { }
2
+
3
+ declare namespace puer {
4
+ function $ref<T>(x?: T): CS.$Ref<T>;
5
+
6
+ function $unref<T>(x: CS.$Ref<T>): T;
7
+
8
+ function $set<T>(x: CS.$Ref<T>, val: T): void;
9
+
10
+ function $promise<T>(x: CS.$Task<T>): Promise<T>;
11
+
12
+ function $generic<T extends new (...args: any[]) => any>(genericType: T, ...genericArguments: (typeof __Puerts_CSharpEnum | (new (...args: any[]) => any))[]): T;
13
+
14
+ function $genericMethod(genericType: new (...args: any[]) => any, methodName: string, ...genericArguments: (typeof __Puerts_CSharpEnum | (new (...args: any[]) => any))[]): (...args: any[]) => any;
15
+
16
+ function $typeof(x: new (...args: any[]) => any): CS.System.Type;
17
+
18
+ function $extension(c: Function, e: Function): void;
19
+
20
+ function on(eventType: string, listener: Function, prepend?: boolean): void;
21
+
22
+ function off(eventType: string, listener: Function): void;
23
+
24
+ function emit(eventType: string, ...args: any[]): boolean;
25
+
26
+ function loadFile(name: string): { content: string, debugpath: string };
27
+
28
+ function evalScript(name: string): void;
29
+
30
+ function require(name: string): any;
31
+ }
@@ -0,0 +1,23 @@
1
+
2
+ interface TypeRef<T> {
3
+ runtimeType: Function;
4
+ }
5
+
6
+ declare namespace CS {
7
+ namespace UnityEngine {
8
+
9
+ }
10
+ namespace UnityEngine.UIElements {
11
+ interface IChangeEvent {
12
+ }
13
+
14
+ class ChangeEvent<T> extends UnityEngine.UIElements.EventBase$1<T> implements IChangeEvent, System.IDisposable {
15
+ static GetPooled<T>(): T
16
+ static GetPooled<T>(previousValue: any, newValue: any): ChangeEvent<T>
17
+ previousValue: T
18
+ newValue: T
19
+ constructor()
20
+ Dispose(): void
21
+ }
22
+ }
23
+ }
@@ -0,0 +1,56 @@
1
+ import { Dom } from "OneJS/Dom"
2
+ import { VisualElement } from "UnityEngine/UIElements"
3
+ import { MutableRef, StateUpdater, useCallback, useEffect, useState } from "preact/hooks"
4
+
5
+ /**
6
+ * A convenience hook that, like useState(), returns a stateful value and a function to update it.
7
+ * This one ties the value to a property on a C# object.
8
+ * It takes care of setting up and cleaning up the C# value changed event automatically.
9
+ * Refer here for more info: https://onejs.com/docs/uiworkflow#reducing-boilerplates
10
+ *
11
+ * @param obj The C# object containing the property to be observed
12
+ * @param propertyName The name of the property to be observed
13
+ * @param eventName The name of the event to be observed. If not specified, it defaults to "On{propertyName}Changed"
14
+ * @returns
15
+ */
16
+ export function useEventfulState<
17
+ T extends { [k in `On${K}Changed`]: OneJS.Event<(value: T[K]) => void> },
18
+ K extends string & keyof T
19
+ >(obj: T, propertyName: K): [T[K], StateUpdater<T[K]>]
20
+ export function useEventfulState<T, K extends string & keyof T, E extends OneJS.EventKeys<T>>(
21
+ obj: T,
22
+ propertyName: K,
23
+ eventName: E
24
+ ): [T[K], StateUpdater<T[K]>]
25
+ export function useEventfulState<
26
+ T extends { [k in keyof T]: k extends E ? OneJS.Event<(value: T[K]) => void> : any },
27
+ K extends string & keyof T,
28
+ E extends OneJS.EventKeys<T>
29
+ >(obj: T, propertyName: K, eventName?: E): [T[K], StateUpdater<T[K]>] {
30
+ // Guarantee the component is re-rendered on changed event,
31
+ // by ensuring that the state is always updated with a different identity
32
+ // to handle the case where the object property is an array
33
+ // and the changed event is raised with the same array instance.
34
+ const [state, setState] = useState({ value: obj?.[propertyName] })
35
+ const setValue = useCallback((value) => setState({ value }), [])
36
+
37
+ useEffect(() => {
38
+ if (obj == null) return
39
+
40
+ eventName ??= `On${propertyName}Changed` as E
41
+ setValue(obj[propertyName])
42
+ return onejs.subscribe(obj, eventName, setValue as OneJS.EventGenericType<T[E]>)
43
+ }, [obj])
44
+
45
+ const setValWrapper = useCallback(
46
+ (v: T[K]) => {
47
+ if (obj == null) return
48
+
49
+ obj[propertyName] = v
50
+ // setVal(v) // No need to set the state here in JS. The event handling stuff above will do.
51
+ },
52
+ [obj]
53
+ )
54
+
55
+ return [state.value as T[K], setValWrapper]
56
+ }
@@ -0,0 +1,42 @@
1
+ import * as fs from "fs";
2
+ import * as path from "path";
3
+
4
+ export const importTransformPlugin = {
5
+ name: "onejs-import-transform",
6
+ setup(build) {
7
+ // First pass: Mark C# imports as external
8
+ build.onResolve({ filter: /^(UnityEngine|OneJS)/ }, (args) => {
9
+ return { path: args.path, external: true };
10
+ });
11
+
12
+ // Second pass: Transform all JS and TSX files
13
+ build.onLoad({ filter: /\.(js|jsx|ts|tsx)$/ }, async (args) => {
14
+ let contents = await fs.promises.readFile(args.path, "utf8");
15
+
16
+ // Transform Unity imports
17
+ contents = contents.replace(
18
+ /import\s+(?:{([^}]+)})?\s*from\s*["'](UnityEngine[^"']*|OneJS[^"']*)["'];?/g,
19
+ (match, imports, moduleName) => {
20
+ moduleName = moduleName.replace(/\//g, ".");
21
+ if (imports) {
22
+ const importItems = imports.split(",").map((item) => item.trim());
23
+ return `const { ${importItems.join(", ")} } = CS.${moduleName};`;
24
+ } else {
25
+ const namespaceName = moduleName.split(".").pop();
26
+ return `const ${namespaceName} = CS.${moduleName};`;
27
+ }
28
+ }
29
+ );
30
+
31
+ // Transform any remaining require statements for Unity modules
32
+ contents = contents.replace(
33
+ /__require\(["'](UnityEngine[^"']*|OneJS[^"']*)["']\)/g,
34
+ (match, moduleName) => {
35
+ return `CS.${moduleName.replace(/\//g, ".")}`;
36
+ }
37
+ );
38
+
39
+ return { contents, loader: path.extname(args.path).slice(1) };
40
+ });
41
+ },
42
+ };
package/index.ts ADDED
@@ -0,0 +1,44 @@
1
+ /// <reference types="./definitions/app.d.ts" />
2
+ /// <reference types="./definitions/onejs.d.ts" />
3
+ /// <reference types="./definitions/jsx.d.ts" />
4
+ /// <reference types="./definitions/puerts.d.ts" />
5
+ /// <reference types="./definitions/unity-engine.d.ts" />
6
+ /// <reference types="./definitions/proto-overrides.d.ts" />
7
+
8
+ declare var document: CS.OneJS.Dom.Document
9
+
10
+ /**
11
+ * OneJS's own h function. Use this to quickly create elements in jsx-like syntax
12
+ * @param type
13
+ * @param props
14
+ * @param children
15
+ * @returns
16
+ */
17
+ export function h(type: any, props: any, ...children: any[]): any {
18
+ const element = typeof type === "string" ? document.createElement(type) : type;
19
+
20
+ // Assign properties to the element
21
+ for (const [key, value] of Object.entries(props || {})) {
22
+ if (key.startsWith("on") && typeof value === "function") {
23
+ element.addEventListener(key.substring(2).toLowerCase(), value);
24
+ } else if (key === "style" && typeof value === "object") {
25
+ Object.assign(element.style, value);
26
+ } else {
27
+ element.setAttribute(key, value);
28
+ }
29
+ }
30
+
31
+ // Append children
32
+ for (const child of children) {
33
+ if (typeof child === "string") {
34
+ element.appendChild(document.createTextNode(child));
35
+ } else {
36
+ element.appendChild(child);
37
+ }
38
+ }
39
+
40
+ return element;
41
+ }
42
+
43
+ export { emo } from "./styling/index"
44
+ export { useEventfulState } from "./hooks/eventful"
package/jsr.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@onejs/core",
3
+ "version": "0.1.20",
4
+ "exports": {
5
+ ".": "./index.ts",
6
+ "./utils": "./index.ts",
7
+ "./uss-transform-plugin.cjs": "./uss-transform-plugin.cjs",
8
+ "./onejs-tw-config.cjs": "./onejs-tw-config.cjs"
9
+ }
10
+ }
@@ -0,0 +1,188 @@
1
+ const defaultTheme = require("tailwindcss/defaultTheme")
2
+ const plugin = require("tailwindcss/plugin")
3
+
4
+ let theme = rem2px(defaultTheme)
5
+ theme.extend = {
6
+ transitionTimingFunction: {
7
+ DEFAULT: "ease-in-out",
8
+ "in-out": "ease-in-out",
9
+ },
10
+ }
11
+ exports.theme = theme
12
+
13
+ exports.paths = []
14
+
15
+ exports.plugins = [
16
+ plugin(function ({ addUtilities, matchUtilities, theme }) {
17
+ addUtilities({
18
+ ".bg-crop": {
19
+ "-unity-background-scale-mode": "scale-and-crop",
20
+ },
21
+ ".bg-fit": {
22
+ "-unity-background-scale-mode": "scale-to-fit",
23
+ },
24
+ ".bg-fill": {
25
+ "-unity-background-scale-mode": "scale-to-fill",
26
+ },
27
+ ".font-normal": {
28
+ "-unity-font-style": "normal",
29
+ },
30
+ ".bold": {
31
+ "-unity-font-style": "bold",
32
+ },
33
+ ".italic": {
34
+ "-unity-font-style": "italic",
35
+ },
36
+ ".bold-italic": {
37
+ "-unity-font-style": "bold-and-italic",
38
+ },
39
+ ".transition-none": { "transition-property": "none" },
40
+ ".transition-all": {
41
+ "transition-property": "all",
42
+ "transition-duration": "150ms",
43
+ },
44
+ ".transition": {
45
+ "transition-property": "color, background-color, border-color, opacity",
46
+ "transition-duration": "150ms",
47
+ },
48
+ ".transition-colors": {
49
+ "transition-property": "color, background-color, border-color",
50
+ "transition-duration": "150ms",
51
+ },
52
+ ".transition-opacity": {
53
+ "transition-property": "opacity",
54
+ "transition-duration": "150ms",
55
+ },
56
+ ".duration-0": { "transition-duration": "0s" },
57
+ ".duration-75": { "transition-duration": "75ms" },
58
+ ".duration-100": { "transition-duration": "100ms" },
59
+ ".duration-150": { "transition-duration": "150ms" },
60
+ ".duration-200": { "transition-duration": "200ms" },
61
+ ".duration-300": { "transition-duration": "300ms" },
62
+ ".duration-500": { "transition-duration": "500ms" },
63
+ ".duration-700": { "transition-duration": "700ms" },
64
+ ".duration-1000": { "transition-duration": "1000ms" },
65
+ ".scale-none": { scale: "none" },
66
+ ".rotate-none": { rotate: "none" },
67
+ })
68
+
69
+ matchUtilities({
70
+ fontdef: (value) => ({ "-unity-font-definition": `url("${value}")` }),
71
+ }, {})
72
+
73
+ matchUtilities(
74
+ {
75
+ text: (value) => ({ "-unity-text-align": value }),
76
+ },
77
+ {
78
+ values: {
79
+ left: "middle-left",
80
+ center: "middle-center",
81
+ right: "middle-right",
82
+ "upper-left": "upper-left",
83
+ "upper-center": "upper-center",
84
+ "upper-right": "upper-right",
85
+ "lower-left": "lower-left",
86
+ "lower-center": "lower-center",
87
+ "lower-right": "lower-right",
88
+ },
89
+ }
90
+ )
91
+
92
+ matchUtilities(
93
+ {
94
+ translate: (value) => ({ translate: value }),
95
+ "translate-x": (value) => ({ translate: `${value} 0` }),
96
+ "translate-y": (value) => ({ translate: `0 ${value}` }),
97
+ },
98
+ {
99
+ supportsNegativeValues: true,
100
+ values: theme("translate"),
101
+ }
102
+ )
103
+
104
+ matchUtilities(
105
+ {
106
+ scale: (value) => ({ scale: value }),
107
+ "scale-x": (value) => ({ scale: `${value} 1` }),
108
+ "scale-y": (value) => ({ scale: `1 ${value}` }),
109
+ },
110
+ {
111
+ supportsNegativeValues: true,
112
+ values: theme("scale"),
113
+ }
114
+ )
115
+
116
+ matchUtilities(
117
+ {
118
+ rotate: (value) => ({ rotate: value }),
119
+ },
120
+ {
121
+ supportsNegativeValues: true,
122
+ values: theme("rotate"),
123
+ }
124
+ )
125
+
126
+ matchUtilities(
127
+ {
128
+ inset: (value) => ({
129
+ top: value,
130
+ bottom: value,
131
+ left: value,
132
+ right: value,
133
+ }),
134
+ },
135
+ {
136
+ supportsNegativeValues: true,
137
+ values: theme("inset"),
138
+ }
139
+ )
140
+ }),
141
+ ]
142
+
143
+ exports.corePlugins = {
144
+ // USS cannot support dynamic custom properties within rgb()
145
+ // which is what is used by Tailwind for opacity scales
146
+ transitionProperty: false,
147
+ transitionDuration: false,
148
+ backdropOpacity: false,
149
+ backgroundOpacity: false,
150
+ borderOpacity: false,
151
+ divideOpacity: false,
152
+ ringOpacity: false,
153
+ textOpacity: false,
154
+ transform: false,
155
+ translate: false,
156
+ rotate: false,
157
+ scale: false,
158
+ fontWeight: false,
159
+ lineHeight: false,
160
+ }
161
+
162
+ /**
163
+ * Utilities
164
+ */
165
+
166
+ // rem is not supported in USS
167
+ function rem2px(input, fontSize = 16) {
168
+ if (input == null) {
169
+ return input
170
+ }
171
+ switch (typeof input) {
172
+ case "object":
173
+ if (Array.isArray(input)) {
174
+ return input.map((val) => rem2px(val, fontSize))
175
+ }
176
+ const ret = {}
177
+ for (const key in input) {
178
+ ret[key] = rem2px(input[key], fontSize)
179
+ }
180
+ return ret
181
+ case "string":
182
+ return input.replace(/(\d*\.?\d+)rem$/, (_, val) => `${parseFloat(val) * fontSize}px`)
183
+ case "function":
184
+ return eval(input.toString().replace(/(\d*\.?\d+)rem/g, (_, val) => `${parseFloat(val) * fontSize}px`))
185
+ default:
186
+ return input
187
+ }
188
+ }
package/package.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "onejs-core",
3
+ "description": "The JS part of OneJS, a Unity3D UI framework and Scripting Engine.",
4
+ "version": "0.3.5",
5
+ "dependencies": {
6
+ "css-flatten": "^2.0.0",
7
+ "css-simple-parser": "^3.0.0"
8
+ }
9
+ }
@@ -0,0 +1,44 @@
1
+
2
+
3
+ export function btoa(text: string): string {
4
+ // return CS.OneJS.CommonGlobals.btoa(text);
5
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
6
+ let str = text;
7
+ let output = '';
8
+
9
+ for (let i = 0; i < str.length; i += 3) {
10
+ let block = (str.charCodeAt(i) << 16) | ((i + 1 < str.length ? str.charCodeAt(i + 1) : 0) << 8) | (i + 2 < str.length ? str.charCodeAt(i + 2) : 0);
11
+ output += chars[(block >> 18) & 0x3F] +
12
+ chars[(block >> 12) & 0x3F] +
13
+ (i + 1 < str.length ? chars[(block >> 6) & 0x3F] : '=') +
14
+ (i + 2 < str.length ? chars[block & 0x3F] : '=');
15
+ }
16
+
17
+ return output;
18
+ }
19
+
20
+ // function uint8ArrayToString(uint8Array) {
21
+ // const chars: any = [];
22
+ // for (let i = 0; i < uint8Array.length; i++) {
23
+ // chars.push(String.fromCharCode(uint8Array[i]));
24
+ // }
25
+ // return chars.join('');
26
+ // }
27
+
28
+ export function atob(base64: string): string {
29
+ // return CS.OneJS.CommonGlobals.atob(base64);
30
+ // return CS.System.Text.Encoding.UTF8.GetString(CS.System.Convert.FromBase64String(base64))
31
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
32
+ let str = base64.replace(/=+$/, '');
33
+ let output = '';
34
+
35
+ if (str.length % 4 == 1) {
36
+ throw new Error('Invalid base64 string');
37
+ }
38
+
39
+ for (let bc = 0, bs = 0, buffer, idx = 0; buffer = str.charAt(idx++); ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer, bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0) {
40
+ buffer = chars.indexOf(buffer);
41
+ }
42
+
43
+ return output;
44
+ }