fix alicecss speed, root element, misc refactors

This commit is contained in:
CoolElectronics 2024-01-23 22:29:50 -05:00
parent 4b1656c493
commit ea5268c871
No known key found for this signature in database
GPG key ID: F63593D168636C50
4 changed files with 36 additions and 16 deletions

View file

@ -1,3 +1,4 @@
examples/ examples/
node_modules node_modules
a.html index.html
a.js

11
AliceJS.d.ts vendored
View file

@ -22,3 +22,14 @@ declare function handle<T>(references: AliceJSReferenceSink<T>, callback: (value
declare function css(strings: TemplateStringsArray, ...values: any): string; declare function css(strings: TemplateStringsArray, ...values: any): string;
declare var styled: { new: typeof css }; declare var styled: { new: typeof css };
type DLCSS = string;
interface Element {
$: DLComponent<any>
}
type DLComponent<T> = {
css: DLCSS,
root: Element,
} & T;

View file

@ -130,7 +130,7 @@ export function useValue(references) {
// Actual JSX factory. Responsible for creating the HTML elements and all of the *reactive* syntactic sugar // Actual JSX factory. Responsible for creating the HTML elements and all of the *reactive* syntactic sugar
export function h(type, props, ...children) { export function h(type, props, ...children) {
if (typeof type === "function") { if (typeof type === "function") {
let newthis = stateful({}); let newthis = stateful(Object.create(type.prototype));
for (const name in props) { for (const name in props) {
const references = props[name]; const references = props[name];
@ -163,12 +163,17 @@ export function h(type, props, ...children) {
delete props[name]; delete props[name];
} }
} }
Object.assign(newthis, props);
let slot = []; let slot = [];
for (const child of children) { for (const child of children) {
JSXAddChild(child, slot.push.bind(slot)); JSXAddChild(child, slot.push.bind(slot));
} }
return type.apply(newthis, [props, slot]); let elm = type.apply(newthis, [slot]);
elm.$ = newthis;
newthis.root = elm;
return elm;
} }
@ -359,7 +364,13 @@ function JSXAddChild(child, cb) {
appended.forEach(n => n.remove()); appended.forEach(n => n.remove());
appended = JSXAddChild(val, cb); appended = JSXAddChild(val, cb);
} else if (appended.length > 0) { } else if (appended.length > 0) {
appended[0].replaceWith((appended = JSXAddChild(val, cb))[0]); let old = appended[0];
appended = JSXAddChild(val, cb);
if (appended[0]) {
old.replaceWith(appended[0])
} else {
old.remove();
}
} else { } else {
appended = JSXAddChild(val, cb); appended = JSXAddChild(val, cb);
} }
@ -421,14 +432,15 @@ function JSXAddAttributes(elm, name, prop) {
elm.setAttribute(name, prop); elm.setAttribute(name, prop);
} }
const virtualDoc = document.implementation.createHTMLDocument("");
const virtualStyleElement = document.createElement("style");
virtualDoc.body.appendChild(virtualStyleElement);
function parse_css(uid, css) { function parse_css(uid, css) {
let cssParsed = ""; let cssParsed = "";
const virtualDoc = document.implementation.createHTMLDocument("");
const virtualStyleElement = document.createElement("style");
virtualStyleElement.textContent = css; virtualStyleElement.textContent = css;
virtualDoc.body.appendChild(virtualStyleElement);
//@ts-ignore //@ts-ignore
for (const rule of virtualStyleElement.sheet.cssRules) { for (const rule of virtualStyleElement.sheet.cssRules) {

View file

@ -1,6 +1,6 @@
{ {
"name": "@mercuryworkshop/alicejs", "name": "@mercuryworkshop/alicejs",
"version": "1.1.3", "version": "1.3.1",
"description": "A utilitarian HTML rendering library", "description": "A utilitarian HTML rendering library",
"scripts": { "scripts": {
"build": "esbuild --minify --bundle AliceJS.js --outfile=index.js && tsc", "build": "esbuild --minify --bundle AliceJS.js --outfile=index.js && tsc",
@ -10,13 +10,9 @@
"author": "MercuryWorkshop", "author": "MercuryWorkshop",
"repository": "https://github.com/MercuryWorkshop/AliceJS", "repository": "https://github.com/MercuryWorkshop/AliceJS",
"license": "MIT", "license": "MIT",
"exports": {
".": {
"browser": "./index.js", "browser": "./index.js",
"node": "./index.js", "node": "./index.js",
"types":"./AliceJS.d.ts" "types":"./AliceJS.d.ts",
}
},
"devDependencies": { "devDependencies": {
"esbuild": "^0.19.11", "esbuild": "^0.19.11",
"typescript": "^5.3.3" "typescript": "^5.3.3"