1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-21 23:39:37 +01:00

Add on.disconnectedAsAbort(), pushRoot() to scope (and 🐛 pop)

This commit is contained in:
Jan Andrle 2023-11-17 14:48:11 +01:00
parent 5008b56ac8
commit 40780bd61f
Signed by: jaandrle
GPG Key ID: B3A25AED155AFFAB
11 changed files with 69 additions and 52 deletions

File diff suppressed because one or more lines are too long

25
dist/dde.js vendored

File diff suppressed because one or more lines are too long

View File

@ -139,6 +139,8 @@ export const scope: {
state: Scope[], state: Scope[],
/** Adds new child scope. All attributes are inherited by default. */ /** Adds new child scope. All attributes are inherited by default. */
push(scope: Partial<Scope>): ReturnType<Array<Scope>["push"]>, push(scope: Partial<Scope>): ReturnType<Array<Scope>["push"]>,
/** Adds root scope as a child of the current scope. */
pushRoot(): ReturnType<Array<Scope>["push"]>,
/** Removes last/current child scope. */ /** Removes last/current child scope. */
pop(): ReturnType<Array<Scope>["pop"]>, pop(): ReturnType<Array<Scope>["pop"]>,
}; };

File diff suppressed because one or more lines are too long

2
dist/esm.d.ts vendored
View File

@ -139,6 +139,8 @@ export const scope: {
state: Scope[], state: Scope[],
/** Adds new child scope. All attributes are inherited by default. */ /** Adds new child scope. All attributes are inherited by default. */
push(scope: Partial<Scope>): ReturnType<Array<Scope>["push"]>, push(scope: Partial<Scope>): ReturnType<Array<Scope>["push"]>,
/** Adds root scope as a child of the current scope. */
pushRoot(): ReturnType<Array<Scope>["push"]>,
/** Removes last/current child scope. */ /** Removes last/current child scope. */
pop(): ReturnType<Array<Scope>["pop"]>, pop(): ReturnType<Array<Scope>["pop"]>,
}; };

2
dist/esm.js vendored

File diff suppressed because one or more lines are too long

View File

@ -10,8 +10,10 @@ export function fullNameComponent(){
const name= labels.map(_=> S("")); const name= labels.map(_=> S(""));
const full_name= S(()=> const full_name= S(()=>
name.map(l=> l()).filter(Boolean).join(" ") || "-"); name.map(l=> l()).filter(Boolean).join(" ") || "-");
scope.host(on.connected(()=> console.log(fullNameComponent))); scope.host(
scope.host(on.disconnected(()=> console.log(fullNameComponent))) on.connected(()=> console.log(fullNameComponent)),
on.disconnected(()=> console.log(fullNameComponent))
);
const elSVG= elNS("http://www.w3.org/2000/svg"); const elSVG= elNS("http://www.w3.org/2000/svg");
return el("div", { className }).append( return el("div", { className }).append(

View File

@ -23,17 +23,19 @@ export class CustomHTMLTestElement extends HTMLElement{
render({ test }){ render({ test }){
console.log(scope.state); console.log(scope.state);
scope.host(on.connected(()=> console.log(CustomHTMLTestElement))); scope.host(
scope.host(on.attributeChanged(e=> console.log(e))); on.connected(()=> console.log(CustomHTMLTestElement)),
scope.host(on.disconnected(()=> console.log(CustomHTMLTestElement))); on.attributeChanged(e=> console.log(e)),
on.disconnected(()=> console.log(CustomHTMLTestElement))
);
const name= S.attribute("name"); const name= S.attribute("name");
const preName= S.attribute("pre-name"); const preName= S.attribute("pre-name");
console.log({ name, test, preName}); console.log({ name, test, preName});
return el("p").append( return el("p").append(
el("#text", { textContent: name }), el("#text", name),
el("#text", { textContent: test }), el("#text", test),
el("#text", { textContent: preName }), el("#text", preName),
el("button", { type: "button", textContent: "pre-name", onclick: ()=> preName("Ahoj") }) el("button", { type: "button", textContent: "pre-name", onclick: ()=> preName("Ahoj") })
); );
} }
@ -48,7 +50,7 @@ lifecycleToEvents(CustomHTMLTestElement)
customElements.define(CustomHTMLTestElement.tagName, CustomHTMLTestElement); customElements.define(CustomHTMLTestElement.tagName, CustomHTMLTestElement);
function customElementRender(_this, render){ function customElementRender(_this, render){
scope.push({ scope: _this, host: (...a)=> a.length ? a[0](_this) : _this, custom_element: _this }); scope.push({ scope: _this, host: (...c)=> c.length ? c.forEach(c=> c(_this)) : _this, custom_element: _this });
const out= render(_this); const out= render(_this);
scope.pop(); scope.pop();
return out; return out;

2
index.d.ts vendored
View File

@ -145,6 +145,8 @@ export const scope: {
state: Scope[], state: Scope[],
/** Adds new child scope. All attributes are inherited by default. */ /** Adds new child scope. All attributes are inherited by default. */
push(scope: Partial<Scope>): ReturnType<Array<Scope>["push"]>, push(scope: Partial<Scope>): ReturnType<Array<Scope>["push"]>,
/** Adds root scope as a child of the current scope. */
pushRoot(): ReturnType<Array<Scope>["push"]>,
/** Removes last/current child scope. */ /** Removes last/current child scope. */
pop(): ReturnType<Array<Scope>["pop"]>, pop(): ReturnType<Array<Scope>["pop"]>,
}; };

View File

@ -19,11 +19,15 @@ export const scope= {
}, },
get state(){ return [ ...scopes ]; }, get state(){ return [ ...scopes ]; },
push(s= {}){ push(s= {}){ return scopes.push(Object.assign({}, this.current, { prevent: false }, s)); },
return scopes.push(Object.assign({}, this.current, { prevent: false }, s)); pushRoot(){ return scopes.push(scopes[0]); },
pop(){
if(scopes.length===1) return;
return scopes.pop();
}, },
pop(){ return scopes.pop(); },
}; };
function append(...els){ this.appendOrig(...els); return this; }
export function chainableAppend(el){ if(el.append===append) return el; el.appendOrig= el.append; el.append= append; return el; }
let namespace; let namespace;
export function createElement(tag, attributes, ...modifiers){ export function createElement(tag, attributes, ...modifiers){
/* jshint maxcomplexity: 15 */ /* jshint maxcomplexity: 15 */
@ -36,7 +40,7 @@ export function createElement(tag, attributes, ...modifiers){
switch(true){ switch(true){
case typeof tag==="function": { case typeof tag==="function": {
scoped= 1; scoped= 1;
scope.push({ scope: tag, host: c=> c ? (scoped===1 ? modifiers.unshift(c) : c(el_host), undefined) : el_host }); scope.push({ scope: tag, host: (...c)=> c.length ? (scoped===1 ? modifiers.unshift(...c) : c.forEach(c=> c(el_host)), undefined) : el_host });
el= tag(attributes || undefined); el= tag(attributes || undefined);
const is_fragment= el instanceof DocumentFragment; const is_fragment= el instanceof DocumentFragment;
if(el.nodeName==="#comment") break; if(el.nodeName==="#comment") break;
@ -72,11 +76,6 @@ createElement.mark= function(attrs, is_open= false){
if(!is_open) out.end= document.createComment("</dde:mark>"); if(!is_open) out.end= document.createComment("</dde:mark>");
return out; return out;
}; };
createElement.later= function(){
const el= createElement.mark({ type: "later" });
el.append= el.prepend= function(...els){ el.after(...els); return el; };
return el;
};
export { createElement as el }; export { createElement as el };
//const namespaceHelper= ns=> ns==="svg" ? "http://www.w3.org/2000/svg" : ns; //const namespaceHelper= ns=> ns==="svg" ? "http://www.w3.org/2000/svg" : ns;
@ -180,6 +179,3 @@ function attrArrToStr(attr){ return Array.isArray(attr) ? attr.filter(Boolean).j
function setRemove(obj, prop, key, val){ return obj[ (isUndef(val) ? "remove" : "set") + prop ](key, attrArrToStr(val)); } function setRemove(obj, prop, key, val){ return obj[ (isUndef(val) ? "remove" : "set") + prop ](key, attrArrToStr(val)); }
function setRemoveNS(obj, prop, key, val, ns= null){ return obj[ (isUndef(val) ? "remove" : "set") + prop + "NS" ](ns, key, attrArrToStr(val)); } function setRemoveNS(obj, prop, key, val, ns= null){ return obj[ (isUndef(val) ? "remove" : "set") + prop + "NS" ](ns, key, attrArrToStr(val)); }
function setDelete(obj, key, val){ Reflect.set(obj, key, val); if(!isUndef(val)) return; return Reflect.deleteProperty(obj, key); } function setDelete(obj, key, val){ Reflect.set(obj, key, val); if(!isUndef(val)) return; return Reflect.deleteProperty(obj, key); }
function append(...els){ this.appendOrig(...els); return this; }
function chainableAppend(el){ if(el.append===append) return el; el.appendOrig= el.append; el.append= append; return el; }

View File

@ -51,6 +51,15 @@ on.disconnected= function(listener, options){
return element; return element;
}; };
}; };
const store_abort= new WeakMap();
on.disconnectedAsAbort= function(host){
if(store_abort.has(host)) return store_abort.get(host);
const a= new AbortController();
store_abort.set(host, a);
host(on.disconnected(()=> a.abort()));
return a;
};
on.attributeChanged= function(listener, options){ on.attributeChanged= function(listener, options){
const name= "attributeChanged"; const name= "attributeChanged";
if(typeof options !== "object") if(typeof options !== "object")