onejs-core 3.0.3 → 3.0.4
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.
- package/.gitattributes +2 -2
- package/.prettierrc +5 -5
- package/.vscode/settings.json +5 -5
- package/LICENSE +20 -20
- package/README.md +102 -102
- package/definitions/Assemblies/OneJS.Runtime.Ext.d.ts +6 -6
- package/definitions/Assemblies/OneJS.Runtime.d.ts +826 -826
- package/definitions/Assemblies/OneJS.Samples.d.ts +20 -20
- package/definitions/Assemblies/Unity.Mathematics.d.ts +9221 -9221
- package/definitions/Assemblies/UnityEditor.CoreModule.d.ts +32614 -32614
- package/definitions/Assemblies/UnityEngine.AIModule.d.ts +998 -998
- package/definitions/Assemblies/UnityEngine.AnimationModule.d.ts +3308 -3308
- package/definitions/Assemblies/UnityEngine.AssetBundleModule.d.ts +337 -337
- package/definitions/Assemblies/UnityEngine.AudioModule.d.ts +1154 -1154
- package/definitions/Assemblies/UnityEngine.CoreModule.d.ts +29587 -29587
- package/definitions/Assemblies/UnityEngine.PhysicsModule.d.ts +3137 -3137
- package/definitions/Assemblies/UnityEngine.TerrainModule.d.ts +1270 -1270
- package/definitions/Assemblies/UnityEngine.UIElementsModule.d.ts +32718 -32718
- package/definitions/Assemblies/UnityEngine.UnityAnalyticsCommonModule.d.ts +274 -274
- package/definitions/Assemblies/index.d.ts +16 -16
- package/definitions/Assemblies/mscorlib.d.ts +19416 -19416
- package/definitions/augments.d.ts +66 -66
- package/definitions/globals.d.ts +53 -53
- package/definitions/index.d.ts +11 -11
- package/definitions/jsx.d.ts +570 -570
- package/definitions/modules.d.ts +32 -32
- package/definitions/onejs.d.ts +171 -171
- package/definitions/preact.jsx.d.ts +6 -6
- package/definitions/proto-overrides.d.ts +41 -41
- package/definitions/puerts.d.ts +30 -30
- package/definitions/unity-engine.d.ts +22 -22
- package/dist/csharp/index.d.ts +3 -0
- package/dist/csharp/index.js +3 -0
- package/dist/dom/document.d.ts +30 -0
- package/dist/dom/document.js +89 -0
- package/dist/dom/dom-style.d.ts +9 -0
- package/dist/dom/dom-style.js +27 -0
- package/dist/dom/dom.d.ts +83 -0
- package/dist/dom/dom.js +313 -0
- package/dist/dom/index.d.ts +4 -0
- package/dist/dom/index.js +4 -0
- package/dist/dom/selector.d.ts +0 -0
- package/dist/dom/selector.js +0 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +45 -0
- package/dist/math/index.d.ts +86 -0
- package/dist/math/index.js +361 -0
- package/dist/preloads/inject.d.ts +3 -0
- package/dist/preloads/inject.js +36 -0
- package/dist/styling/index.d.ts +10 -0
- package/dist/styling/index.js +28 -0
- package/dist/styling/utils/generateAlphabeticName.d.ts +1 -0
- package/dist/styling/utils/generateAlphabeticName.js +16 -0
- package/dist/styling/utils/generateComponentId.d.ts +1 -0
- package/dist/styling/utils/generateComponentId.js +5 -0
- package/dist/styling/utils/hash.d.ts +5 -0
- package/dist/styling/utils/hash.js +34 -0
- package/dist/utils/arrays.d.ts +1 -0
- package/dist/utils/arrays.js +10 -0
- package/dist/utils/color-palettes.d.ts +2 -0
- package/dist/utils/color-palettes.js +2 -0
- package/dist/utils/color-parser.d.ts +161 -0
- package/dist/utils/color-parser.js +241 -0
- package/dist/utils/float-parser.d.ts +7 -0
- package/dist/utils/float-parser.js +23 -0
- package/dist/utils/index.d.ts +12 -0
- package/dist/utils/index.js +15 -0
- package/dist/utils/responsive.d.ts +4 -0
- package/dist/utils/responsive.js +23 -0
- package/dist/utils/subscribe.d.ts +4 -0
- package/dist/utils/subscribe.js +10 -0
- package/dist/utils/system.d.ts +1 -0
- package/dist/utils/system.js +16 -0
- package/dist/utils/toJsArray.d.ts +1 -0
- package/dist/utils/toJsArray.js +10 -0
- package/dom/document.ts +115 -115
- package/dom/dom-style.ts +36 -36
- package/dom/dom.ts +376 -376
- package/dom/index.ts +3 -3
- package/index.ts +59 -59
- package/jsr.json +9 -9
- package/math/README.md +212 -212
- package/math/index.ts +487 -487
- package/package.json +33 -33
- package/preloads/inject.ts +43 -43
- package/scripts/esbuild/copy-assets.mjs +94 -94
- package/scripts/esbuild/decorator-fix.mjs +17 -17
- package/scripts/esbuild/import-transform.mjs +100 -100
- package/scripts/esbuild/index.mjs +3 -3
- package/scripts/esbuild/watch-output.mjs +38 -38
- package/scripts/postcss/cleanup-plugin.cjs +89 -89
- package/scripts/postcss/onejs-tw-config.cjs +252 -252
- package/scripts/postcss/optional-import-plugin.cjs +26 -26
- package/scripts/postcss/tailwind-logging-plugin.cjs +11 -11
- package/scripts/postcss/unwrap-is-plugin.cjs +16 -16
- package/scripts/postcss/uss-transform-plugin.cjs +92 -92
- package/scripts/switch.cjs +290 -290
- package/styling/index.tsx +32 -32
- package/styling/utils/generateAlphabeticName.ts +20 -20
- package/styling/utils/generateComponentId.ts +5 -5
- package/styling/utils/hash.ts +46 -46
- package/tsconfig.json +24 -24
- package/typings.d.ts +5 -5
- package/utils/arrays.ts +10 -10
- package/utils/color-palettes.ts +2 -2
- package/utils/color-parser.ts +248 -248
- package/utils/float-parser.ts +30 -30
- package/utils/index.ts +15 -15
- package/utils/subscribe.ts +16 -16
- package/utils/system.ts +16 -16
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/// <reference path="../../definitions/modules.d.ts" />
|
|
2
|
+
import { DomWrapper } from "./dom";
|
|
3
|
+
import { EventBase } from "UnityEngine/UIElements";
|
|
4
|
+
interface ElementCreationOptions {
|
|
5
|
+
is?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class DocumentWrapper {
|
|
8
|
+
#private;
|
|
9
|
+
get _doc(): CS.OneJS.Dom.Document;
|
|
10
|
+
/**
|
|
11
|
+
* The body/root element of the document. Will be null for Editor documents.
|
|
12
|
+
*/
|
|
13
|
+
get body(): DomWrapper | null;
|
|
14
|
+
constructor(doc: CS.OneJS.Dom.Document);
|
|
15
|
+
addRuntimeUSS(uss: string): void;
|
|
16
|
+
clearRuntimeStyleSheets(): void;
|
|
17
|
+
createElement(tagName: string, options?: ElementCreationOptions): DomWrapper;
|
|
18
|
+
createElementNS(ns: string, tagName: string, options?: ElementCreationOptions): DomWrapper;
|
|
19
|
+
createTextNode(text: string): DomWrapper;
|
|
20
|
+
createDocumentFragment(): DomWrapper;
|
|
21
|
+
getElementById(id: string): DomWrapper;
|
|
22
|
+
querySelectorAll(selector: string): DomWrapper[];
|
|
23
|
+
elementFromPoint(x: number, y: number): DomWrapper | null;
|
|
24
|
+
elementsFromPoint(x: number, y: number): DomWrapper[];
|
|
25
|
+
addEventListener(type: string, listener: (event: EventBase) => void, options?: boolean | {
|
|
26
|
+
once?: boolean;
|
|
27
|
+
}): void;
|
|
28
|
+
removeEventListener(type: string, listener: (event: EventBase) => void, useCapture?: boolean): void;
|
|
29
|
+
}
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Vector2 } from "UnityEngine";
|
|
2
|
+
import { DomWrapper } from "./dom";
|
|
3
|
+
export class DocumentWrapper {
|
|
4
|
+
get _doc() { return this.#doc; }
|
|
5
|
+
#doc;
|
|
6
|
+
#body;
|
|
7
|
+
/**
|
|
8
|
+
* The body/root element of the document. Will be null for Editor documents.
|
|
9
|
+
*/
|
|
10
|
+
get body() { return this.#body; }
|
|
11
|
+
constructor(doc) {
|
|
12
|
+
this.#doc = doc;
|
|
13
|
+
this.#body = doc.body ? new DomWrapper(doc.body) : null;
|
|
14
|
+
}
|
|
15
|
+
addRuntimeUSS(uss) {
|
|
16
|
+
this.#doc.addRuntimeUSS(uss);
|
|
17
|
+
}
|
|
18
|
+
clearRuntimeStyleSheets() {
|
|
19
|
+
this.#doc.clearRuntimeStyleSheets();
|
|
20
|
+
}
|
|
21
|
+
// MARK: Create
|
|
22
|
+
createElement(tagName, options) {
|
|
23
|
+
return new DomWrapper(this.#doc.createElement(tagName));
|
|
24
|
+
}
|
|
25
|
+
createElementNS(ns, tagName, options) {
|
|
26
|
+
tagName = typeof tagName === 'string' ? tagName : 'div';
|
|
27
|
+
return new DomWrapper(this.#doc.createElement(tagName));
|
|
28
|
+
}
|
|
29
|
+
createTextNode(text) {
|
|
30
|
+
return new DomWrapper(this.#doc.createTextNode(text));
|
|
31
|
+
}
|
|
32
|
+
createDocumentFragment() {
|
|
33
|
+
return this.createElement("div");
|
|
34
|
+
}
|
|
35
|
+
// MARK: Query
|
|
36
|
+
getElementById(id) {
|
|
37
|
+
return new DomWrapper(this.#doc.getElementById(id));
|
|
38
|
+
}
|
|
39
|
+
querySelectorAll(selector) {
|
|
40
|
+
let doms = this.#doc.querySelectorAll(selector);
|
|
41
|
+
let res = [];
|
|
42
|
+
for (let i = 0; i < doms.Length; i++) {
|
|
43
|
+
res.push(new DomWrapper(doms.get_Item(i)));
|
|
44
|
+
}
|
|
45
|
+
return res;
|
|
46
|
+
}
|
|
47
|
+
elementFromPoint(x, y) {
|
|
48
|
+
const root = this.body;
|
|
49
|
+
if (!root)
|
|
50
|
+
return null;
|
|
51
|
+
const hitTest = (node) => {
|
|
52
|
+
if (!node.ve.worldBound.Contains(new Vector2(x, y)))
|
|
53
|
+
return null;
|
|
54
|
+
// later siblings are painted on top → scan from back to front
|
|
55
|
+
for (let i = node.childNodes.length - 1; i >= 0; i--) {
|
|
56
|
+
const hit = hitTest(node.childNodes[i]);
|
|
57
|
+
if (hit)
|
|
58
|
+
return hit;
|
|
59
|
+
}
|
|
60
|
+
return node;
|
|
61
|
+
};
|
|
62
|
+
return hitTest(root);
|
|
63
|
+
}
|
|
64
|
+
elementsFromPoint(x, y) {
|
|
65
|
+
const root = this.body;
|
|
66
|
+
if (!root)
|
|
67
|
+
return [];
|
|
68
|
+
const hits = [];
|
|
69
|
+
const collect = (node) => {
|
|
70
|
+
if (!node.ve.worldBound.Contains(new Vector2(x, y)))
|
|
71
|
+
return;
|
|
72
|
+
// visit children first, from front to back (last child is top‑most)
|
|
73
|
+
for (let i = node.childNodes.length - 1; i >= 0; i--) {
|
|
74
|
+
collect(node.childNodes[i]);
|
|
75
|
+
}
|
|
76
|
+
// add the current node itself
|
|
77
|
+
hits.push(node);
|
|
78
|
+
};
|
|
79
|
+
collect(root);
|
|
80
|
+
return hits; // ordered front‑to‑back (top‑most first)
|
|
81
|
+
}
|
|
82
|
+
// MARK: Event
|
|
83
|
+
addEventListener(type, listener, options) {
|
|
84
|
+
this.body?.addEventListener(type, listener, options);
|
|
85
|
+
}
|
|
86
|
+
removeEventListener(type, listener, useCapture) {
|
|
87
|
+
this.body?.removeEventListener(type, listener, useCapture);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare class DomStyleWrapper implements CS.OneJS.Dom.DomStyle {
|
|
2
|
+
_domStyle: CS.OneJS.Dom.DomStyle;
|
|
3
|
+
constructor(domStyle: CS.OneJS.Dom.DomStyle);
|
|
4
|
+
getProperty(name: string): any;
|
|
5
|
+
setProperty(name: string, value: any): void;
|
|
6
|
+
}
|
|
7
|
+
export interface DomStyleWrapper extends CS.OneJS.Dom.DomStyle {
|
|
8
|
+
[key: string | symbol]: any;
|
|
9
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export class DomStyleWrapper {
|
|
2
|
+
_domStyle; // Making this public so that Proxy's setProperty can access it
|
|
3
|
+
constructor(domStyle) {
|
|
4
|
+
this._domStyle = domStyle;
|
|
5
|
+
return new Proxy(this, {
|
|
6
|
+
set(target, prop, value, receiver) {
|
|
7
|
+
if (typeof prop === 'string' && !(prop in target)) {
|
|
8
|
+
target.setProperty(prop, value);
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
return Reflect.set(target, prop, value, receiver);
|
|
12
|
+
},
|
|
13
|
+
get(target, prop, receiver) {
|
|
14
|
+
if (typeof prop === 'string' && !(prop in target)) {
|
|
15
|
+
return target.getProperty(prop);
|
|
16
|
+
}
|
|
17
|
+
return Reflect.get(target, prop, receiver);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
getProperty(name) {
|
|
22
|
+
return this._domStyle.getProperty(name);
|
|
23
|
+
}
|
|
24
|
+
setProperty(name, value) {
|
|
25
|
+
this._domStyle.setProperty(name, value);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/// <reference path="../../definitions/modules.d.ts" />
|
|
2
|
+
import { EventBase } from "UnityEngine/UIElements";
|
|
3
|
+
import { DomStyleWrapper } from "./dom-style";
|
|
4
|
+
export declare class DomWrapper {
|
|
5
|
+
get _dom(): CS.OneJS.Dom.Dom;
|
|
6
|
+
get ve(): CS.UnityEngine.UIElements.VisualElement;
|
|
7
|
+
get childNodes(): DomWrapper[];
|
|
8
|
+
get firstChild(): DomWrapper | null;
|
|
9
|
+
get parentNode(): DomWrapper | null;
|
|
10
|
+
get nextSibling(): DomWrapper | null;
|
|
11
|
+
get nodeType(): number;
|
|
12
|
+
get style(): DomStyleWrapper;
|
|
13
|
+
get Id(): string;
|
|
14
|
+
set Id(value: string);
|
|
15
|
+
get key(): string;
|
|
16
|
+
set key(value: string);
|
|
17
|
+
get value(): any;
|
|
18
|
+
get checked(): boolean;
|
|
19
|
+
get data(): any;
|
|
20
|
+
set data(value: any);
|
|
21
|
+
get className(): string;
|
|
22
|
+
set className(value: string);
|
|
23
|
+
get classList(): DomTokenList;
|
|
24
|
+
/**
|
|
25
|
+
* Not using private fields because of issues with the `#private;` line
|
|
26
|
+
* generated by tsc
|
|
27
|
+
*/
|
|
28
|
+
dom: CS.OneJS.Dom.Dom;
|
|
29
|
+
domStyleWrapper: DomStyleWrapper;
|
|
30
|
+
domTokenList: DomTokenList;
|
|
31
|
+
cachedChildNodes: DomWrapper[] | null;
|
|
32
|
+
boundListeners: WeakMap<WeakKey, any>;
|
|
33
|
+
constructor(dom: CS.OneJS.Dom.Dom);
|
|
34
|
+
appendChild(child: DomWrapper): DomWrapper | undefined;
|
|
35
|
+
removeChild(child: DomWrapper): void;
|
|
36
|
+
insertBefore(a: DomWrapper, b: DomWrapper): void;
|
|
37
|
+
insertAfter(a: DomWrapper, b: DomWrapper): void;
|
|
38
|
+
before(other: DomWrapper): void;
|
|
39
|
+
clearChildren(): void;
|
|
40
|
+
setAttribute(name: string, value: any): void;
|
|
41
|
+
removeAttribute(name: string): void;
|
|
42
|
+
append(child: DomWrapper): void;
|
|
43
|
+
cloneNode(deep?: boolean): DomWrapper;
|
|
44
|
+
remove(): void;
|
|
45
|
+
contains(child: DomWrapper): boolean;
|
|
46
|
+
focus(): void;
|
|
47
|
+
addEventListener(type: string, listener: (event: EventBase) => void, options?: boolean | {
|
|
48
|
+
once?: boolean;
|
|
49
|
+
}): void;
|
|
50
|
+
removeEventListener(type: string, listener: (event: EventBase) => void, useCapture?: boolean): void;
|
|
51
|
+
/**
|
|
52
|
+
* Returns all elements matching the specified selector.
|
|
53
|
+
* Supports basic selectors:
|
|
54
|
+
* - Tag names: 'div'
|
|
55
|
+
* - IDs: '#myId'
|
|
56
|
+
* - Classes: '.myClass'
|
|
57
|
+
* - Combinations: 'div.myClass#myId'
|
|
58
|
+
*/
|
|
59
|
+
querySelectorAll(selector: string): DomWrapper[];
|
|
60
|
+
/**
|
|
61
|
+
* Returns the first element matching the specified selector.
|
|
62
|
+
* Supports the same basic selectors as querySelectorAll.
|
|
63
|
+
*/
|
|
64
|
+
querySelector(selector: string): DomWrapper | null;
|
|
65
|
+
}
|
|
66
|
+
export declare function querySelectorAll(root: DomWrapper, selector: string): DomWrapper[];
|
|
67
|
+
export declare function querySelector(root: DomWrapper, selector: string): DomWrapper | null;
|
|
68
|
+
declare class DomTokenList {
|
|
69
|
+
dom: CS.OneJS.Dom.Dom;
|
|
70
|
+
constructor(dom: CS.OneJS.Dom.Dom);
|
|
71
|
+
_tokens(): string[];
|
|
72
|
+
_update(tokens: string[]): void;
|
|
73
|
+
add(...tokens: string[]): void;
|
|
74
|
+
remove(...tokens: string[]): void;
|
|
75
|
+
toggle(token: string, force?: boolean): boolean;
|
|
76
|
+
contains(token: string): boolean;
|
|
77
|
+
replace(oldToken: string, newToken: string): boolean;
|
|
78
|
+
toString(): string;
|
|
79
|
+
get length(): number;
|
|
80
|
+
item(index: number): string | null;
|
|
81
|
+
[Symbol.iterator](): Iterator<string>;
|
|
82
|
+
}
|
|
83
|
+
export {};
|
package/dist/dom/dom.js
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { DomStyleWrapper } from "./dom-style";
|
|
2
|
+
export class DomWrapper {
|
|
3
|
+
get _dom() { return this.dom; }
|
|
4
|
+
get ve() { return this.dom.ve; }
|
|
5
|
+
get childNodes() {
|
|
6
|
+
if (this.cachedChildNodes)
|
|
7
|
+
return this.cachedChildNodes;
|
|
8
|
+
this.cachedChildNodes = new Array(this.dom.childNodes.Length);
|
|
9
|
+
var i = this.dom.childNodes.Length;
|
|
10
|
+
while (i--) {
|
|
11
|
+
this.cachedChildNodes[i] = new DomWrapper(this.dom.childNodes.get_Item(i));
|
|
12
|
+
}
|
|
13
|
+
return this.cachedChildNodes;
|
|
14
|
+
}
|
|
15
|
+
get firstChild() {
|
|
16
|
+
return this.dom.firstChild ? new DomWrapper(this.dom.firstChild) : null;
|
|
17
|
+
}
|
|
18
|
+
get parentNode() {
|
|
19
|
+
return this.dom.parentNode ? new DomWrapper(this.dom.parentNode) : null;
|
|
20
|
+
}
|
|
21
|
+
get nextSibling() {
|
|
22
|
+
return this.dom.nextSibling ? new DomWrapper(this.dom.nextSibling) : null;
|
|
23
|
+
}
|
|
24
|
+
get nodeType() { return this.dom.nodeType; }
|
|
25
|
+
get style() { return this.domStyleWrapper; }
|
|
26
|
+
get Id() { return this.dom.Id; }
|
|
27
|
+
set Id(value) { this.dom.Id = value; }
|
|
28
|
+
get key() { return this.dom.key; }
|
|
29
|
+
set key(value) { this.dom.key = value; }
|
|
30
|
+
get value() { return this.dom.value; }
|
|
31
|
+
get checked() { return this.dom.checked; }
|
|
32
|
+
get data() { return this.dom.data; }
|
|
33
|
+
set data(value) { this.dom.data = value; }
|
|
34
|
+
get className() { return this.dom.className; }
|
|
35
|
+
set className(value) { this.dom.className = value; }
|
|
36
|
+
get classList() { return this.domTokenList; }
|
|
37
|
+
/**
|
|
38
|
+
* Not using private fields because of issues with the `#private;` line
|
|
39
|
+
* generated by tsc
|
|
40
|
+
*/
|
|
41
|
+
dom;
|
|
42
|
+
domStyleWrapper;
|
|
43
|
+
domTokenList;
|
|
44
|
+
cachedChildNodes = null;
|
|
45
|
+
boundListeners = new WeakMap();
|
|
46
|
+
constructor(dom) {
|
|
47
|
+
this.dom = dom;
|
|
48
|
+
this.domStyleWrapper = new DomStyleWrapper(dom.style);
|
|
49
|
+
this.domTokenList = new DomTokenList(dom);
|
|
50
|
+
}
|
|
51
|
+
// MARK: Manipulation
|
|
52
|
+
appendChild(child) {
|
|
53
|
+
if (!child)
|
|
54
|
+
return;
|
|
55
|
+
this.dom.appendChild(child.dom);
|
|
56
|
+
this.cachedChildNodes = null;
|
|
57
|
+
return child;
|
|
58
|
+
}
|
|
59
|
+
removeChild(child) {
|
|
60
|
+
if (!child)
|
|
61
|
+
return;
|
|
62
|
+
this.dom.removeChild(child.dom);
|
|
63
|
+
this.cachedChildNodes = null;
|
|
64
|
+
}
|
|
65
|
+
insertBefore(a, b) {
|
|
66
|
+
this.dom.insertBefore(a?._dom, b?._dom);
|
|
67
|
+
this.cachedChildNodes = null;
|
|
68
|
+
}
|
|
69
|
+
insertAfter(a, b) {
|
|
70
|
+
this.dom.insertAfter(a?._dom, b?._dom);
|
|
71
|
+
this.cachedChildNodes = null;
|
|
72
|
+
}
|
|
73
|
+
before(other) {
|
|
74
|
+
if (this.parentNode) {
|
|
75
|
+
this.parentNode.insertBefore(other, this);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
clearChildren() {
|
|
79
|
+
this.dom.clearChildren();
|
|
80
|
+
this.cachedChildNodes = null;
|
|
81
|
+
}
|
|
82
|
+
setAttribute(name, value) {
|
|
83
|
+
this.dom.setAttribute(name, value);
|
|
84
|
+
}
|
|
85
|
+
removeAttribute(name) {
|
|
86
|
+
this.dom.removeAttribute(name);
|
|
87
|
+
}
|
|
88
|
+
// MARK: Node.prototype
|
|
89
|
+
append(child) {
|
|
90
|
+
if (!child)
|
|
91
|
+
return;
|
|
92
|
+
this.dom.appendChild(child.dom);
|
|
93
|
+
this.cachedChildNodes = null;
|
|
94
|
+
}
|
|
95
|
+
cloneNode(deep = false) {
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
remove() {
|
|
99
|
+
if (this.parentNode) {
|
|
100
|
+
this.parentNode.removeChild(this);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// MARK: Misc
|
|
104
|
+
contains(child) {
|
|
105
|
+
if (!child)
|
|
106
|
+
return false;
|
|
107
|
+
return this.dom.contains(child._dom);
|
|
108
|
+
}
|
|
109
|
+
focus() {
|
|
110
|
+
this.dom.focus();
|
|
111
|
+
}
|
|
112
|
+
// MARK: Event
|
|
113
|
+
addEventListener(type, listener, options) {
|
|
114
|
+
let boundListener = this.boundListeners.get(listener);
|
|
115
|
+
if (!boundListener) {
|
|
116
|
+
boundListener = listener.bind(this);
|
|
117
|
+
this.boundListeners.set(listener, boundListener);
|
|
118
|
+
}
|
|
119
|
+
if (typeof options === 'object' && options.once) {
|
|
120
|
+
const onceWrapper = (event) => {
|
|
121
|
+
boundListener(event);
|
|
122
|
+
this.dom.removeEventListener(type, onceWrapper, false);
|
|
123
|
+
};
|
|
124
|
+
this.dom.addEventListener(type, onceWrapper, false);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
this.dom.addEventListener(type, boundListener, options ? true : false);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
removeEventListener(type, listener, useCapture) {
|
|
131
|
+
const boundListener = this.boundListeners.get(listener);
|
|
132
|
+
if (boundListener) {
|
|
133
|
+
this.dom.removeEventListener(type, boundListener, useCapture ? true : false);
|
|
134
|
+
this.boundListeners.delete(listener); // isn't strictly necessary for WeakMap, but still good practice
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Returns all elements matching the specified selector.
|
|
139
|
+
* Supports basic selectors:
|
|
140
|
+
* - Tag names: 'div'
|
|
141
|
+
* - IDs: '#myId'
|
|
142
|
+
* - Classes: '.myClass'
|
|
143
|
+
* - Combinations: 'div.myClass#myId'
|
|
144
|
+
*/
|
|
145
|
+
querySelectorAll(selector) {
|
|
146
|
+
const selectorInfo = parseSelector(selector);
|
|
147
|
+
const results = [];
|
|
148
|
+
function traverse(element) {
|
|
149
|
+
if (elementMatchesSelector(element, selectorInfo)) {
|
|
150
|
+
results.push(element);
|
|
151
|
+
}
|
|
152
|
+
for (const child of element.childNodes) {
|
|
153
|
+
traverse(child);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
traverse(this);
|
|
157
|
+
return results;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Returns the first element matching the specified selector.
|
|
161
|
+
* Supports the same basic selectors as querySelectorAll.
|
|
162
|
+
*/
|
|
163
|
+
querySelector(selector) {
|
|
164
|
+
const selectorInfo = parseSelector(selector);
|
|
165
|
+
function traverse(element) {
|
|
166
|
+
if (elementMatchesSelector(element, selectorInfo)) {
|
|
167
|
+
return element;
|
|
168
|
+
}
|
|
169
|
+
for (const child of element.childNodes) {
|
|
170
|
+
const match = traverse(child);
|
|
171
|
+
if (match) {
|
|
172
|
+
return match;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
return traverse(this);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function parseSelector(selector) {
|
|
181
|
+
const selectorInfo = {
|
|
182
|
+
classes: []
|
|
183
|
+
};
|
|
184
|
+
// Handle ID
|
|
185
|
+
const idMatch = selector.match(/#([^.#\s]+)/);
|
|
186
|
+
if (idMatch) {
|
|
187
|
+
selectorInfo.id = idMatch[1];
|
|
188
|
+
selector = selector.replace(idMatch[0], '');
|
|
189
|
+
}
|
|
190
|
+
// Handle classes
|
|
191
|
+
const classMatches = selector.match(/\.([^.#\s]+)/g);
|
|
192
|
+
if (classMatches) {
|
|
193
|
+
selectorInfo.classes = classMatches.map(c => c.substring(1));
|
|
194
|
+
selector = selector.replace(/\.[^.#\s]+/g, '');
|
|
195
|
+
}
|
|
196
|
+
// Handle tag name (what's left after removing id and classes)
|
|
197
|
+
const tagName = selector.trim();
|
|
198
|
+
if (tagName) {
|
|
199
|
+
selectorInfo.tag = tagName.toLowerCase();
|
|
200
|
+
}
|
|
201
|
+
return selectorInfo;
|
|
202
|
+
}
|
|
203
|
+
function elementMatchesSelector(element, selectorInfo) {
|
|
204
|
+
// Check tag name
|
|
205
|
+
if (selectorInfo.tag && element.ve.GetType().Name.toLowerCase() !== selectorInfo.tag) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
// Check ID
|
|
209
|
+
if (selectorInfo.id && element.Id !== selectorInfo.id) {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
// Check classes
|
|
213
|
+
if (selectorInfo.classes.length > 0) {
|
|
214
|
+
const elementClasses = element.className.split(' ').filter(c => c);
|
|
215
|
+
for (const className of selectorInfo.classes) {
|
|
216
|
+
if (!elementClasses.includes(className)) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
export function querySelectorAll(root, selector) {
|
|
224
|
+
const results = [];
|
|
225
|
+
const selectorInfo = parseSelector(selector);
|
|
226
|
+
function traverse(element) {
|
|
227
|
+
// Check if current element matches
|
|
228
|
+
if (elementMatchesSelector(element, selectorInfo)) {
|
|
229
|
+
results.push(element);
|
|
230
|
+
}
|
|
231
|
+
// Recursively check children
|
|
232
|
+
for (const child of element.childNodes) {
|
|
233
|
+
traverse(child);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
traverse(root);
|
|
237
|
+
return results;
|
|
238
|
+
}
|
|
239
|
+
export function querySelector(root, selector) {
|
|
240
|
+
const selectorInfo = parseSelector(selector);
|
|
241
|
+
function traverse(element) {
|
|
242
|
+
// Check if current element matches
|
|
243
|
+
if (elementMatchesSelector(element, selectorInfo)) {
|
|
244
|
+
return element;
|
|
245
|
+
}
|
|
246
|
+
// Recursively check children
|
|
247
|
+
for (const child of element.childNodes) {
|
|
248
|
+
const match = traverse(child);
|
|
249
|
+
if (match) {
|
|
250
|
+
return match;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
return traverse(root);
|
|
256
|
+
}
|
|
257
|
+
class DomTokenList {
|
|
258
|
+
dom;
|
|
259
|
+
constructor(dom) {
|
|
260
|
+
this.dom = dom;
|
|
261
|
+
}
|
|
262
|
+
_tokens() {
|
|
263
|
+
return this.dom.className.trim().split(/\s+/).filter(Boolean);
|
|
264
|
+
}
|
|
265
|
+
_update(tokens) {
|
|
266
|
+
this.dom.className = tokens.join(' ');
|
|
267
|
+
}
|
|
268
|
+
add(...tokens) {
|
|
269
|
+
const set = new Set(this._tokens());
|
|
270
|
+
tokens.forEach(t => t && set.add(t));
|
|
271
|
+
this._update(Array.from(set));
|
|
272
|
+
}
|
|
273
|
+
remove(...tokens) {
|
|
274
|
+
const set = new Set(this._tokens());
|
|
275
|
+
tokens.forEach(t => set.delete(t));
|
|
276
|
+
this._update(Array.from(set));
|
|
277
|
+
}
|
|
278
|
+
toggle(token, force) {
|
|
279
|
+
if (!token)
|
|
280
|
+
return false;
|
|
281
|
+
const has = this.contains(token);
|
|
282
|
+
if (force === true || (!has && force !== false)) {
|
|
283
|
+
this.add(token);
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
if (has && (force === false || force === undefined)) {
|
|
287
|
+
this.remove(token);
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
return has;
|
|
291
|
+
}
|
|
292
|
+
contains(token) {
|
|
293
|
+
return this._tokens().includes(token);
|
|
294
|
+
}
|
|
295
|
+
replace(oldToken, newToken) {
|
|
296
|
+
if (!this.contains(oldToken))
|
|
297
|
+
return false;
|
|
298
|
+
const tokens = this._tokens().map(t => (t === oldToken ? newToken : t));
|
|
299
|
+
this._update(tokens);
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
toString() {
|
|
303
|
+
return this.dom.className;
|
|
304
|
+
}
|
|
305
|
+
get length() { return this._tokens().length; }
|
|
306
|
+
item(index) {
|
|
307
|
+
const t = this._tokens();
|
|
308
|
+
return index >= 0 && index < t.length ? t[index] : null;
|
|
309
|
+
}
|
|
310
|
+
[Symbol.iterator]() {
|
|
311
|
+
return this._tokens()[Symbol.iterator]();
|
|
312
|
+
}
|
|
313
|
+
}
|
|
File without changes
|
|
File without changes
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/// <reference path="../definitions/index.d.ts" />
|
|
2
|
+
import { DocumentWrapper } from "./dom/document";
|
|
3
|
+
import { DomWrapper } from "./dom/dom";
|
|
4
|
+
/**
|
|
5
|
+
* OneJS's own h function. Use this to quickly create elements in jsx-like syntax
|
|
6
|
+
* @param type
|
|
7
|
+
* @param props
|
|
8
|
+
* @param children
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
export declare function h(type: any, props: any, ...children: any[]): any;
|
|
12
|
+
export { emo } from "./styling/index";
|
|
13
|
+
declare global {
|
|
14
|
+
interface Document extends DocumentWrapper {
|
|
15
|
+
}
|
|
16
|
+
interface Element extends DomWrapper {
|
|
17
|
+
}
|
|
18
|
+
const newCsArray: <T>(type: {
|
|
19
|
+
new (...args: any[]): T;
|
|
20
|
+
}, count: number) => CS.System.Array;
|
|
21
|
+
const toJsArray: <T>(csArr: CS.System.Array) => T[];
|
|
22
|
+
const toCsArray: <T>(jsArr: T[], type: {
|
|
23
|
+
new (...args: any[]): T;
|
|
24
|
+
}) => CS.System.Array;
|
|
25
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/// <reference path="./definitions/index.d.ts" />
|
|
2
|
+
import { DocumentWrapper } from "./dom/document";
|
|
3
|
+
/**
|
|
4
|
+
* OneJS's own h function. Use this to quickly create elements in jsx-like syntax
|
|
5
|
+
* @param type
|
|
6
|
+
* @param props
|
|
7
|
+
* @param children
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export function h(type, props, ...children) {
|
|
11
|
+
const element = typeof type === "string" ? document.createElement(type) : type;
|
|
12
|
+
// Assign properties to the element
|
|
13
|
+
for (const [key, value] of Object.entries(props || {})) {
|
|
14
|
+
if (key.startsWith("on") && typeof value === "function") {
|
|
15
|
+
element.addEventListener(key.substring(2).toLowerCase(), value);
|
|
16
|
+
}
|
|
17
|
+
else if (key === "style" && typeof value === "object") {
|
|
18
|
+
Object.assign(element.style, value);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
element.setAttribute(key, value);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// Append children
|
|
25
|
+
for (const child of children) {
|
|
26
|
+
if (typeof child === "string") {
|
|
27
|
+
element.appendChild(document.createTextNode(child));
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
element.appendChild(child);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return element;
|
|
34
|
+
}
|
|
35
|
+
export { emo } from "./styling/index";
|
|
36
|
+
if (typeof globalThis.___document != "undefined") {
|
|
37
|
+
// @ts-ignore
|
|
38
|
+
globalThis.onejsDocument = new DocumentWrapper(globalThis.___document);
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
if (!globalThis.ONEJS_WEBGL && !globalThis.document) {
|
|
41
|
+
// @ts-ignore
|
|
42
|
+
globalThis.document = globalThis.onejsDocument;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// puer.$extension(CS.UnityEngine.UIElements.VisualElement, CS.UnityEngine.UIElements.VisualElementExtensions)
|