esaQute/core.js

360 lines
10 KiB
JavaScript
Raw Permalink Normal View History

2023-01-21 22:30:07 +03:00
((window)=>{
function render(...entries)
2023-01-12 23:48:50 +03:00
{
2023-01-21 22:30:07 +03:00
let content = {};
let domContainer = document.createElement('div');
renderTreeQueQue(
domContainer,
entries,
content
);
return domContainer;
2023-01-12 23:48:50 +03:00
};
2023-01-21 22:30:07 +03:00
function renderTreeQueQue(domContainer, entries, content)
{
if(!content._renderPlanned)
{
requestAnimationFrame(()=>{
createTree(
domContainer,
entries,
content
)
});
content._renderPlanned = true;
}
2023-01-12 23:48:50 +03:00
};
2023-01-21 22:30:07 +03:00
function createTree(domContainer, entries, content)
{
let root = é(entries[0]);
RenderElement(domContainer, root);
}
/**
* @param {vnode} data
*/
function RenderElement(parent, data)
{
let elem;
2023-01-12 23:48:50 +03:00
if(
2023-01-21 22:30:07 +03:00
(typeof data == "boolean" && !data) ||
data == null ||
data === undefined
2023-01-12 23:48:50 +03:00
)
{
2023-01-21 22:30:07 +03:00
return document.createDocumentFragment()
};
if(data.type == é.type)
{
if(typeof data.companent == "string")
{
elem = document.createElement(data.companent);
for (const [name,value] of Object.entries(data.props))
{
if(["key","children"].includes(name))
{
continue;
}
if(name.startsWith('on'))
{
elem.addEventListener(name.slice(2),value)
}
else
{
elem.setAttribute(name, value)
}
};
}
else if(typeof data.companent == "function")
{
let companent = RenderCompanent(data);
2023-01-22 17:23:53 +03:00
elem = RenderCompanentScope(null, companent)
2023-01-21 22:30:07 +03:00
}
else if(typeof data.companent == "string")
{
elem = document.createTextNode(companent);
}
else if(!data.companent)
{
elem = document.createDocumentFragment()
};
if(data.props && data.props.children instanceof Array)
{
for (const companent of (data.props.children||[]))
{
if( typeof companent == "string" )
{
elem.append(document.createTextNode(companent));
}
else
{
2023-01-22 17:23:53 +03:00
RenderCompanentScope(elem, companent);
2023-01-21 22:30:07 +03:00
}
}
}
}else if(data.$){
elem = document.createElement(data.$ || "div");
for (const [name,value] of Object.entries(data)) {
switch(name)
{
case "$":
break;
case "in":
break;
default:
{
if(name.startsWith('on'))
{
elem.addEventListener(name.slice(2),value)
}
else
{
elem.setAttribute(name, value)
}
};
}
};
if(data.in)
{
if(data.in instanceof Array)
{
for (const companent of (data.in||[])) {
if(
typeof companent == "string"
)
{
elem.append(document.createTextNode(companent));
}
else
{
2023-01-22 17:23:53 +03:00
RenderCompanentScope(elem, companent)
2023-01-21 22:30:07 +03:00
}
}
}else{
if(
typeof data.in == "string"
)
{
elem.append(document.createTextNode(data.in));
}
else
{
2023-01-22 17:23:53 +03:00
RenderCompanentScope(elem, companent)
2023-01-21 22:30:07 +03:00
}
}
}
data.parent = parent;
data.dom = elem;
2023-01-12 23:48:50 +03:00
}
2023-01-21 22:30:07 +03:00
else if(data instanceof Array)
2023-01-12 23:48:50 +03:00
{
2023-01-21 22:30:07 +03:00
if(parent)
2023-01-12 23:48:50 +03:00
{
2023-01-21 22:30:07 +03:00
for (const vdata of data.flat(Infinity)) {
2023-01-22 17:23:53 +03:00
RenderCompanentScope(parent, vdata)
2023-01-12 23:48:50 +03:00
};
}
2023-01-21 22:30:07 +03:00
else
{
let d = document.createDocumentFragment();
for (const vdata of data.flat(Infinity)) {
2023-01-22 17:23:53 +03:00
RenderCompanentScope(d, vdata)
2023-01-21 22:30:07 +03:00
};
elem = d;
}
}
else if(typeof data == "string")
{
elem = document.createTextNode(data);
2023-01-12 23:48:50 +03:00
};
2023-01-21 22:30:07 +03:00
data._render = false;
if(parent)
{
parent.append(elem);
}
else
2023-01-12 23:48:50 +03:00
{
2023-01-21 22:30:07 +03:00
return elem;
}
}
2023-01-22 17:23:53 +03:00
function RenderCompanentScope(data)
2023-01-21 22:30:07 +03:00
{
useCompanent(data);
let result = data.companent(data.props);
useCompanent(undefined);
return result;
2023-01-22 17:23:53 +03:00
};
function clone(original)
{
if(Array.isArray(original))
{
let e = [];
for (const original of original) {
// Klonlanabilirlik
}
}
2023-01-21 22:30:07 +03:00
}
2023-01-22 17:23:53 +03:00
function checkSyncRender(data)
{
// İki kompanentin farkına bakılmalı ve yeniden çizilmeleri gerekiyor
}
function scheduleRender(data)
{
if(scheduleRender.planned)
{
scheduleRender.items.push(data);
return;
};
requestAnimationFrame(()=>{
scheduleRender.planned = false;
checkSyncRender(data)
});
scheduleRender.planned = true;
};
scheduleRender.planned = false;
scheduleRender.items = [];
2023-01-21 22:30:07 +03:00
function é(a,b)
{
if(a instanceof Array)
{
let t = b instanceof Array ? b : [];
for (const _a of a) {
if(_a instanceof Array)
2023-01-12 23:48:50 +03:00
{
2023-01-21 22:30:07 +03:00
é(_a, t);
2023-01-12 23:48:50 +03:00
}else{
2023-01-21 22:30:07 +03:00
t.push(é(_a));
2023-01-12 23:48:50 +03:00
}
};
2023-01-21 22:30:07 +03:00
t = t.flat(Infinity);
return é({
$: é.fragment,
in: t
})
2023-01-12 23:48:50 +03:00
}
2023-01-21 22:30:07 +03:00
if(a.type == é.type)
{
return a;
2023-01-12 23:48:50 +03:00
}
2023-01-21 22:30:07 +03:00
let t = vnode(), companent, key, props = {};
if(typeof a == "function")
2023-01-12 23:48:50 +03:00
{
2023-01-21 22:30:07 +03:00
t.companent = a;
t.key = b && ('key' in b) ? b.key : undefined;
t.props = b;
}else if(typeof a == "string"){
t.companent = null;
t.props = {
children: [a]
};
}else if('$' in a){
props = {};
for (const index of Object.keys(a)) {
switch(index)
{
case "$": companent = a[index]; break;
case "in":{
if(a[index] instanceof Array)
{
2023-01-22 17:23:53 +03:00
props.children = a[index].map((child) => {
let item = é(child);
item.parent = t;
return item;
});
2023-01-21 22:30:07 +03:00
}else{
2023-01-22 17:23:53 +03:00
let child = é(a[index]);
child.parent = t;
props.children = [child]
2023-01-21 22:30:07 +03:00
}
break
};
case "key": key = a[index]; break;
default:{
props[index] = a[index];
}
}
};
t.companent = companent;
t.key = key;
t.props = props;
2023-01-12 23:48:50 +03:00
}else{
2023-01-21 22:30:07 +03:00
throw new Error("Unknown companent type")
2023-01-12 23:48:50 +03:00
}
2023-01-21 22:30:07 +03:00
return t;
};
é.type = Symbol("élement");
é.fragment = Symbol("framént");
2023-01-12 23:48:50 +03:00
2023-01-21 22:30:07 +03:00
let currentCompanent;
let currentCompanentStateIndex = 0;
function useCompanent(vnode)
2023-01-12 23:48:50 +03:00
{
2023-01-21 22:30:07 +03:00
currentCompanent = vnode;
currentCompanentStateIndex = 0;
2023-01-12 23:48:50 +03:00
};
2023-01-21 22:30:07 +03:00
function useState(initial)
{
let companent = currentCompanent;
let id = currentCompanentStateIndex++;
let value;
if(!companent)
{
throw new Error("Cannot be used outside of components")
};
if(companent._state.length <= id)
{
if(typeof initial == "function")
2023-01-12 23:48:50 +03:00
{
2023-01-21 22:30:07 +03:00
value = initial();
}
else
2023-01-12 23:48:50 +03:00
{
2023-01-21 22:30:07 +03:00
value = initial;
}
companent._state[id] = value;
}else
{
value = companent._state[id];
};
return [
value,
newValue => {
if(typeof newValue == "function")
{
value = newValue();
}
if(!Object.is(newValue, value))
{
companent._state[id] = value;
2023-01-22 17:23:53 +03:00
companent._render = true;
scheduleRenderCompanent(companent);
2023-01-21 22:30:07 +03:00
}
}
]
};
é.useState = useState;
function vnode()
{
return {
type: é.type,
// Raw render requirements
parent: undefined,
key: undefined,
companent: undefined,
props:{},
// Companent requirements
ref: undefined,
dom: undefined,
_state: [],
_hooks: [],
_render: true,
context: undefined
2023-01-12 23:48:50 +03:00
}
2023-01-21 22:30:07 +03:00
};
window.é = é;
window.render = render;
})(window)