From 2820a3d0940751cce7432c165f74f99656d4366f Mon Sep 17 00:00:00 2001 From: Delaney Gillilan Date: Tue, 10 Dec 2024 12:19:15 -0800 Subject: [PATCH 1/2] make modifiers spec compliant Fixes #345 --- CHANGELOG.md | 3 +- bundles/datastar-core.js | 4 +- bundles/datastar-core.js.map | 6 +- bundles/datastar.js | 16 +++--- bundles/datastar.js.map | 6 +- library/src/engine/engine.ts | 2 +- library/src/engine/nestedSignals.ts | 4 ++ .../src/plugins/official/dom/attributes/on.ts | 57 ++++--------------- library/src/utils/dom.ts | 17 +++++- sdk/dotnet/src/Consts.fs | 2 +- sdk/go/consts.go | 2 +- .../java/StarFederation/Datastar/Consts.java | 2 +- sdk/php/src/Consts.php | 2 +- site/routes_examples_active_search.templ | 2 +- site/routes_examples_inline_validation.templ | 8 ++- site/routes_examples_model_bindings.templ | 3 + site/routes_examples_signals_ifmissing.go | 9 ++- site/shared.templ | 4 +- site/smoketests/custom_events_test.go | 34 +++++------ site/static/md/examples/active_search.md | 2 +- site/static/md/examples/bind_keys.md | 12 ++-- site/static/md/examples/classes.md | 4 +- site/static/md/examples/custom_events.md | 2 +- .../md/examples/debounce_and_throttle.md | 12 ++-- site/static/md/examples/offline_sync.md | 8 +-- .../md/examples/replace_url_from_signals.md | 6 +- site/static/md/examples/scroll_into_view.md | 2 +- site/static/md/examples/session_storage.md | 6 +- site/static/md/examples/signals_changed.md | 8 +-- site/static/md/examples/signals_ifmissing.md | 12 ++-- site/static/md/reference/plugins_dom.md | 18 +++--- 31 files changed, 138 insertions(+), 137 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51f88aa58..bf325098d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,9 +25,10 @@ This should be the final round of API changes before v1.0.0 🚀 - Renamed the `data-store` attribute to `data-signals`. - Renamed the `data-bind` attribute to `data-attributes`. - Renamed the `data-model` attribute to `data-bind`. -- Changed the `data-*` attribute modifier delimiter from `.` to `:` (`data-on-keydown:debounce_100ms:throttle_lead="value"`). +- Changed the `data-*` attribute modifier delimiter from `.` to `_` for modifiers and from `_` to `.` for arguments. This is to be spec compliant while still parseable with new nested signal syntax #345 (`data-on-keydown_debounce.100ms_throttle.lead="value"`). - The the `get()`, `post()`, `put()`, and `delete()` plugins have been replaced by a single `sse()` plugin that accepts the method as an option (`sse(url, {method: 'post'})`), defaulting to `get`. - The `setAll()` and `toggleAll` plugins now accept a path prefix, instead of a regular expression. +- Nested signals no longer allow for `_` in the key. It causes a conflict with modifiers. ### Fixed diff --git a/bundles/datastar-core.js b/bundles/datastar-core.js index 19e485e91..18d340c3d 100644 --- a/bundles/datastar-core.js +++ b/bundles/datastar-core.js @@ -1,4 +1,4 @@ -"use strict";(()=>{var _e="computed",U={type:1,name:_e,keyReq:1,valReq:1,onLoad:({key:t,signals:e,genRX:n})=>{let s=n();e.setComputed(t,s)}};var W=t=>t.replace(/(?:^\w|[A-Z]|\b\w)/g,function(e,n){return n==0?e.toLowerCase():e.toUpperCase()}).replace(/\s+/g,""),J=t=>new Function(`return Object.assign({}, ${t})`)();var z={type:1,name:"signals",valReq:1,removeOnLoad:!0,onLoad:t=>{let{key:e,genRX:n,signals:s}=t;if(e!="")s.setValue(e,n()());else{let r=J(t.value);t.value=JSON.stringify(r),s.merge(n()())}}};var K={type:1,name:"star",keyReq:2,valReq:2,onLoad:()=>{alert("YOU ARE PROBABLY OVERCOMPLICATING IT")}};var H={name:"signalValue",type:0,fn:t=>{let e=/(?[\w0-9.]*)((\.value))/gm;return t.replaceAll(e,"ctx.signals.signal('$1').value")}};var X="datastar";var Y="0.21.0-beta2";var me={Morph:"morph",Inner:"inner",Outer:"outer",Prepend:"prepend",Append:"append",Before:"before",After:"after",UpsertAttributes:"upsertAttributes"},Le=me.Morph;function Z(t){if(t.id)return t.id;let e=0,n=r=>(e=(e<<5)-e+r,e&e),s=r=>r.split("").forEach(i=>n(i.charCodeAt(0)));for(;t.parentNode;){if(t.id){s(`${t.id}`);break}else if(t===t.ownerDocument.documentElement)s(t.tagName);else{for(let r=1,i=t;i.previousElementSibling;i=i.previousElementSibling,r++)n(r);t=t.parentNode}t=t.parentNode}return X+e}var ve="https://data-star.dev/errors";var u=(t,e)=>{let n=new Error;n.name=`error ${t}`;let s=`${ve}/${t}?${new URLSearchParams(e)}`;return n.message=`for more info see ${s}`,n};var ye=Symbol.for("preact-signals"),g=1,S=2,N=4,b=8,M=16,x=32;function B(){O++}function G(){if(O>1){O--;return}let t,e=!1;for(;T!==void 0;){let n=T;for(T=void 0,L++;n!==void 0;){let s=n._nextBatchedEffect;if(n._nextBatchedEffect=void 0,n._flags&=~S,!(n._flags&b)&&ee(n))try{n._callback()}catch(r){e||(t=r,e=!0)}n=s}}if(L=0,O--,e)throw u("BatchError, error",{error:t})}var a;var T,O=0,L=0,C=0;function Q(t){if(a===void 0)return;let e=t._node;if(e===void 0||e._target!==a)return e={_version:0,_source:t,_prevSource:a._sources,_nextSource:void 0,_target:a,_prevTarget:void 0,_nextTarget:void 0,_rollbackNode:e},a._sources!==void 0&&(a._sources._nextSource=e),a._sources=e,t._node=e,a._flags&x&&t._subscribe(e),e;if(e._version===-1)return e._version=0,e._nextSource!==void 0&&(e._nextSource._prevSource=e._prevSource,e._prevSource!==void 0&&(e._prevSource._nextSource=e._nextSource),e._prevSource=a._sources,e._nextSource=void 0,a._sources._nextSource=e,a._sources=e),e}function f(t){this._value=t,this._version=0,this._node=void 0,this._targets=void 0}f.prototype.brand=ye;f.prototype._refresh=function(){return!0};f.prototype._subscribe=function(t){this._targets!==t&&t._prevTarget===void 0&&(t._nextTarget=this._targets,this._targets!==void 0&&(this._targets._prevTarget=t),this._targets=t)};f.prototype._unsubscribe=function(t){if(this._targets!==void 0){let e=t._prevTarget,n=t._nextTarget;e!==void 0&&(e._nextTarget=n,t._prevTarget=void 0),n!==void 0&&(n._prevTarget=e,t._nextTarget=void 0),t===this._targets&&(this._targets=n)}};f.prototype.subscribe=function(t){return P(()=>{let e=this.value,n=a;a=void 0;try{t(e)}finally{a=n}})};f.prototype.valueOf=function(){return this.value};f.prototype.toString=function(){return this.value+""};f.prototype.toJSON=function(){return this.value};f.prototype.peek=function(){let t=a;a=void 0;try{return this.value}finally{a=t}};Object.defineProperty(f.prototype,"value",{get(){let t=Q(this);return t!==void 0&&(t._version=this._version),this._value},set(t){if(t!==this._value){if(L>100)throw u("SignalCycleDetected");this._value=t,this._version++,C++,B();try{for(let e=this._targets;e!==void 0;e=e._nextTarget)e._target._notify()}finally{G()}}}});function ee(t){for(let e=t._sources;e!==void 0;e=e._nextSource)if(e._source._version!==e._version||!e._source._refresh()||e._source._version!==e._version)return!0;return!1}function te(t){for(let e=t._sources;e!==void 0;e=e._nextSource){let n=e._source._node;if(n!==void 0&&(e._rollbackNode=n),e._source._node=e,e._version=-1,e._nextSource===void 0){t._sources=e;break}}}function ne(t){let e=t._sources,n;for(;e!==void 0;){let s=e._prevSource;e._version===-1?(e._source._unsubscribe(e),s!==void 0&&(s._nextSource=e._nextSource),e._nextSource!==void 0&&(e._nextSource._prevSource=s)):n=e,e._source._node=e._rollbackNode,e._rollbackNode!==void 0&&(e._rollbackNode=void 0),e=s}t._sources=n}function y(t){f.call(this,void 0),this._fn=t,this._sources=void 0,this._globalVersion=C-1,this._flags=N}y.prototype=new f;y.prototype._refresh=function(){if(this._flags&=~S,this._flags&g)return!1;if((this._flags&(N|x))===x||(this._flags&=~N,this._globalVersion===C))return!0;if(this._globalVersion=C,this._flags|=g,this._version>0&&!ee(this))return this._flags&=~g,!0;let t=a;try{te(this),a=this;let e=this._fn();(this._flags&M||this._value!==e||this._version===0)&&(this._value=e,this._flags&=~M,this._version++)}catch(e){this._value=e,this._flags|=M,this._version++}return a=t,ne(this),this._flags&=~g,!0};y.prototype._subscribe=function(t){if(this._targets===void 0){this._flags|=N|x;for(let e=this._sources;e!==void 0;e=e._nextSource)e._source._subscribe(e)}f.prototype._subscribe.call(this,t)};y.prototype._unsubscribe=function(t){if(this._targets!==void 0&&(f.prototype._unsubscribe.call(this,t),this._targets===void 0)){this._flags&=~x;for(let e=this._sources;e!==void 0;e=e._nextSource)e._source._unsubscribe(e)}};y.prototype._notify=function(){if(!(this._flags&S)){this._flags|=N|S;for(let t=this._targets;t!==void 0;t=t._nextTarget)t._target._notify()}};Object.defineProperty(y.prototype,"value",{get(){if(this._flags&g)throw u("SignalCycleDetected");let t=Q(this);if(this._refresh(),t!==void 0&&(t._version=this._version),this._flags&M)throw u("GetComputedError",{value:this._value});return this._value}});function se(t){return new y(t)}function re(t){let e=t._cleanup;if(t._cleanup=void 0,typeof e=="function"){B();let n=a;a=void 0;try{e()}catch(s){throw t._flags&=~g,t._flags|=b,$(t),u("CleanupEffectError",{error:s})}finally{a=n,G()}}}function $(t){for(let e=t._sources;e!==void 0;e=e._nextSource)e._source._unsubscribe(e);t._fn=void 0,t._sources=void 0,re(t)}function Se(t){if(a!==this)throw u("EndEffectError");ne(this),a=t,this._flags&=~g,this._flags&b&&$(this),G()}function w(t){this._fn=t,this._cleanup=void 0,this._sources=void 0,this._nextBatchedEffect=void 0,this._flags=x}w.prototype._callback=function(){let t=this._start();try{if(this._flags&b||this._fn===void 0)return;let e=this._fn();typeof e=="function"&&(this._cleanup=e)}finally{t()}};w.prototype._start=function(){if(this._flags&g)throw u("SignalCycleDetected");this._flags|=g,this._flags&=~b,re(this),te(this),B();let t=a;return a=this,Se.bind(this,t)};w.prototype._notify=function(){this._flags&S||(this._flags|=S,this._nextBatchedEffect=T,T=this)};w.prototype._dispose=function(){this._flags|=b,this._flags&g||$(this)};function P(t){let e=new w(t);try{e._callback()}catch(n){throw e._dispose(),u("EffectError",{error:n})}return e._dispose.bind(e)}function ie(t,e=!1){let n={};for(let s in t)if(t.hasOwnProperty(s)){let r=t[s];if(r instanceof f){if(e&&s.startsWith("_"))continue;n[s]=r.value}else n[s]=ie(r)}return n}function oe(t,e,n=!1){for(let s in e)if(e.hasOwnProperty(s)){let r=e[s];if(r instanceof Object&&!Array.isArray(r))t[s]||(t[s]={}),oe(t[s],r,n);else{if(n&&t[s])continue;t[s]=new f(r)}}}function ae(t,e){for(let n in t)if(t.hasOwnProperty(n)){let s=t[n];s instanceof f?e(n,s):ae(s,e)}}function xe(t,...e){let n={};for(let s of e){let r=s.split("."),i=t,o=n;for(let d=0;dn());this.setSignal(e,s)}value(e){return this.signal(e)?.value}setValue(e,n){let s=this.upsert(e,n);s.value=n}upsert(e,n){let s=e.split("."),r=this._signals;for(let d=0;d{let s;switch(n.type){case 0:this.macros.push(n);break;case 2:let r=n;this.watchers.push(r),s=r.onGlobalInit;break;case 3:this.actions[n.name]=n;break;case 1:let i=n;this.plugins.push(i),s=i.onGlobalInit;break;default:throw u("InvalidPluginType",{name:n.name,type:n.type})}if(s){let r=this;s({get signals(){return r._signals},effect:i=>P(i),actions:this.actions,apply:this.apply.bind(this),cleanup:this.cleanup.bind(this)})}}),this.apply(document.body)}cleanup(e){let n=this.removals.get(e);if(n){for(let s of n.set)s();this.removals.delete(e)}}apply(e){let n=new Set;this.plugins.forEach((s,r)=>{this.walkDownDOM(e,i=>{r||this.cleanup(i);for(let o in i.dataset){if(!o.startsWith(s.name))continue;let c=o.slice(s.name.length),[d,...p]=c.split(":"),h=d.length>0;h&&(d=d[0].toLowerCase()+d.slice(1));let V=`${i.dataset[o]}`||"",_=V,m=_.length>0,l=s.keyReq||0;if(h){if(l===2)throw u(s.name+"KeyNotAllowed")}else if(l===1)throw u(s.name+"KeyRequired");let E=s.valReq||0;if(m){if(E===2)throw u(s.name+"ValueNotAllowed")}else if(E===1)throw u(s.name+"ValueRequired");if(l===3||E===3){if(h&&m)throw u(s.name+"KeyAndValueProvided");if(!h&&!m)throw u(s.name+"KeyOrValueRequired")}i.id.length||(i.id=Z(i)),n.clear();let R=new Map;p.forEach(v=>{let[ge,...he]=v.split("_");R.set(W(ge),new Set(he))});let ue=[...s.macros?.pre||[],...this.macros,...s.macros?.post||[]];for(let v of ue)n.has(v)||(n.add(v),_=v.fn(_));let{actions:ce,apply:fe,cleanup:de}=this,pe=this,F;F={get signals(){return pe._signals},effect:v=>P(v),apply:fe.bind(this),cleanup:de.bind(this),actions:ce,genRX:()=>this.genRX(F,...s.argNames||[]),el:i,rawKey:o,rawValue:V,key:d,value:_,mods:R};let j=s.onLoad(F);j&&(this.removals.has(i)||this.removals.set(i,{id:i.id,set:new Set}),this.removals.get(i).set.add(j)),s?.removeOnLoad&&delete i.dataset[o]}})})}genRX(e,...n){let s=e.value.split(/;|\n/).map(l=>l.trim()).filter(l=>l!=""),r=s.length-1;s[r].startsWith("return")||(s[r]=`return (${s[r]});`);let o=s.join(` +"use strict";(()=>{var _e="computed",U={type:1,name:_e,keyReq:1,valReq:1,onLoad:({key:t,signals:e,genRX:n})=>{let s=n();e.setComputed(t,s)}};var W=t=>t.replace(/(?:^\w|[A-Z]|\b\w)/g,function(e,n){return n==0?e.toLowerCase():e.toUpperCase()}).replace(/\s+/g,""),J=t=>new Function(`return Object.assign({}, ${t})`)();var K={type:1,name:"signals",valReq:1,removeOnLoad:!0,onLoad:t=>{let{key:e,genRX:n,signals:s}=t;if(e!="")s.setValue(e,n()());else{let r=J(t.value);t.value=JSON.stringify(r),s.merge(n()())}}};var z={type:1,name:"star",keyReq:2,valReq:2,onLoad:()=>{alert("YOU ARE PROBABLY OVERCOMPLICATING IT")}};var H={name:"signalValue",type:0,fn:t=>{let e=/(?[\w0-9.]*)((\.value))/gm;return t.replaceAll(e,"ctx.signals.signal('$1').value")}};var X="datastar";var Y="0.21.0-beta2";var me={Morph:"morph",Inner:"inner",Outer:"outer",Prepend:"prepend",Append:"append",Before:"before",After:"after",UpsertAttributes:"upsertAttributes"},Le=me.Morph;function Z(t){if(t.id)return t.id;let e=0,n=r=>(e=(e<<5)-e+r,e&e),s=r=>r.split("").forEach(i=>n(i.charCodeAt(0)));for(;t.parentNode;){if(t.id){s(`${t.id}`);break}else if(t===t.ownerDocument.documentElement)s(t.tagName);else{for(let r=1,i=t;i.previousElementSibling;i=i.previousElementSibling,r++)n(r);t=t.parentNode}t=t.parentNode}return X+e}var ve="https://data-star.dev/errors";var u=(t,e)=>{let n=new Error;n.name=`error ${t}`;let s=`${ve}/${t}?${new URLSearchParams(e)}`;return n.message=`for more info see ${s}`,n};var ye=Symbol.for("preact-signals"),g=1,S=2,N=4,b=8,M=16,x=32;function B(){O++}function G(){if(O>1){O--;return}let t,e=!1;for(;T!==void 0;){let n=T;for(T=void 0,L++;n!==void 0;){let s=n._nextBatchedEffect;if(n._nextBatchedEffect=void 0,n._flags&=~S,!(n._flags&b)&&ee(n))try{n._callback()}catch(r){e||(t=r,e=!0)}n=s}}if(L=0,O--,e)throw u("BatchError, error",{error:t})}var a;var T,O=0,L=0,C=0;function Q(t){if(a===void 0)return;let e=t._node;if(e===void 0||e._target!==a)return e={_version:0,_source:t,_prevSource:a._sources,_nextSource:void 0,_target:a,_prevTarget:void 0,_nextTarget:void 0,_rollbackNode:e},a._sources!==void 0&&(a._sources._nextSource=e),a._sources=e,t._node=e,a._flags&x&&t._subscribe(e),e;if(e._version===-1)return e._version=0,e._nextSource!==void 0&&(e._nextSource._prevSource=e._prevSource,e._prevSource!==void 0&&(e._prevSource._nextSource=e._nextSource),e._prevSource=a._sources,e._nextSource=void 0,a._sources._nextSource=e,a._sources=e),e}function f(t){this._value=t,this._version=0,this._node=void 0,this._targets=void 0}f.prototype.brand=ye;f.prototype._refresh=function(){return!0};f.prototype._subscribe=function(t){this._targets!==t&&t._prevTarget===void 0&&(t._nextTarget=this._targets,this._targets!==void 0&&(this._targets._prevTarget=t),this._targets=t)};f.prototype._unsubscribe=function(t){if(this._targets!==void 0){let e=t._prevTarget,n=t._nextTarget;e!==void 0&&(e._nextTarget=n,t._prevTarget=void 0),n!==void 0&&(n._prevTarget=e,t._nextTarget=void 0),t===this._targets&&(this._targets=n)}};f.prototype.subscribe=function(t){return P(()=>{let e=this.value,n=a;a=void 0;try{t(e)}finally{a=n}})};f.prototype.valueOf=function(){return this.value};f.prototype.toString=function(){return this.value+""};f.prototype.toJSON=function(){return this.value};f.prototype.peek=function(){let t=a;a=void 0;try{return this.value}finally{a=t}};Object.defineProperty(f.prototype,"value",{get(){let t=Q(this);return t!==void 0&&(t._version=this._version),this._value},set(t){if(t!==this._value){if(L>100)throw u("SignalCycleDetected");this._value=t,this._version++,C++,B();try{for(let e=this._targets;e!==void 0;e=e._nextTarget)e._target._notify()}finally{G()}}}});function ee(t){for(let e=t._sources;e!==void 0;e=e._nextSource)if(e._source._version!==e._version||!e._source._refresh()||e._source._version!==e._version)return!0;return!1}function te(t){for(let e=t._sources;e!==void 0;e=e._nextSource){let n=e._source._node;if(n!==void 0&&(e._rollbackNode=n),e._source._node=e,e._version=-1,e._nextSource===void 0){t._sources=e;break}}}function ne(t){let e=t._sources,n;for(;e!==void 0;){let s=e._prevSource;e._version===-1?(e._source._unsubscribe(e),s!==void 0&&(s._nextSource=e._nextSource),e._nextSource!==void 0&&(e._nextSource._prevSource=s)):n=e,e._source._node=e._rollbackNode,e._rollbackNode!==void 0&&(e._rollbackNode=void 0),e=s}t._sources=n}function y(t){f.call(this,void 0),this._fn=t,this._sources=void 0,this._globalVersion=C-1,this._flags=N}y.prototype=new f;y.prototype._refresh=function(){if(this._flags&=~S,this._flags&g)return!1;if((this._flags&(N|x))===x||(this._flags&=~N,this._globalVersion===C))return!0;if(this._globalVersion=C,this._flags|=g,this._version>0&&!ee(this))return this._flags&=~g,!0;let t=a;try{te(this),a=this;let e=this._fn();(this._flags&M||this._value!==e||this._version===0)&&(this._value=e,this._flags&=~M,this._version++)}catch(e){this._value=e,this._flags|=M,this._version++}return a=t,ne(this),this._flags&=~g,!0};y.prototype._subscribe=function(t){if(this._targets===void 0){this._flags|=N|x;for(let e=this._sources;e!==void 0;e=e._nextSource)e._source._subscribe(e)}f.prototype._subscribe.call(this,t)};y.prototype._unsubscribe=function(t){if(this._targets!==void 0&&(f.prototype._unsubscribe.call(this,t),this._targets===void 0)){this._flags&=~x;for(let e=this._sources;e!==void 0;e=e._nextSource)e._source._unsubscribe(e)}};y.prototype._notify=function(){if(!(this._flags&S)){this._flags|=N|S;for(let t=this._targets;t!==void 0;t=t._nextTarget)t._target._notify()}};Object.defineProperty(y.prototype,"value",{get(){if(this._flags&g)throw u("SignalCycleDetected");let t=Q(this);if(this._refresh(),t!==void 0&&(t._version=this._version),this._flags&M)throw u("GetComputedError",{value:this._value});return this._value}});function se(t){return new y(t)}function re(t){let e=t._cleanup;if(t._cleanup=void 0,typeof e=="function"){B();let n=a;a=void 0;try{e()}catch(s){throw t._flags&=~g,t._flags|=b,$(t),u("CleanupEffectError",{error:s})}finally{a=n,G()}}}function $(t){for(let e=t._sources;e!==void 0;e=e._nextSource)e._source._unsubscribe(e);t._fn=void 0,t._sources=void 0,re(t)}function Se(t){if(a!==this)throw u("EndEffectError");ne(this),a=t,this._flags&=~g,this._flags&b&&$(this),G()}function w(t){this._fn=t,this._cleanup=void 0,this._sources=void 0,this._nextBatchedEffect=void 0,this._flags=x}w.prototype._callback=function(){let t=this._start();try{if(this._flags&b||this._fn===void 0)return;let e=this._fn();typeof e=="function"&&(this._cleanup=e)}finally{t()}};w.prototype._start=function(){if(this._flags&g)throw u("SignalCycleDetected");this._flags|=g,this._flags&=~b,re(this),te(this),B();let t=a;return a=this,Se.bind(this,t)};w.prototype._notify=function(){this._flags&S||(this._flags|=S,this._nextBatchedEffect=T,T=this)};w.prototype._dispose=function(){this._flags|=b,this._flags&g||$(this)};function P(t){let e=new w(t);try{e._callback()}catch(n){throw e._dispose(),u("EffectError",{error:n})}return e._dispose.bind(e)}function ie(t,e=!1){let n={};for(let s in t)if(t.hasOwnProperty(s)){let r=t[s];if(r instanceof f){if(e&&s.startsWith("_"))continue;n[s]=r.value}else n[s]=ie(r)}return n}function oe(t,e,n=!1){for(let s in e)if(e.hasOwnProperty(s)){if(s.match(/\_\_+/))throw u("InvalidSignalKey",{key:s});let r=e[s];if(r instanceof Object&&!Array.isArray(r))t[s]||(t[s]={}),oe(t[s],r,n);else{if(n&&t[s])continue;t[s]=new f(r)}}}function ae(t,e){for(let n in t)if(t.hasOwnProperty(n)){let s=t[n];s instanceof f?e(n,s):ae(s,e)}}function xe(t,...e){let n={};for(let s of e){let r=s.split("."),i=t,o=n;for(let d=0;dn());this.setSignal(e,s)}value(e){return this.signal(e)?.value}setValue(e,n){let s=this.upsert(e,n);s.value=n}upsert(e,n){let s=e.split("."),r=this._signals;for(let d=0;d{let s;switch(n.type){case 0:this.macros.push(n);break;case 2:let r=n;this.watchers.push(r),s=r.onGlobalInit;break;case 3:this.actions[n.name]=n;break;case 1:let i=n;this.plugins.push(i),s=i.onGlobalInit;break;default:throw u("InvalidPluginType",{name:n.name,type:n.type})}if(s){let r=this;s({get signals(){return r._signals},effect:i=>P(i),actions:this.actions,apply:this.apply.bind(this),cleanup:this.cleanup.bind(this)})}}),this.apply(document.body)}cleanup(e){let n=this.removals.get(e);if(n){for(let s of n.set)s();this.removals.delete(e)}}apply(e){let n=new Set;this.plugins.forEach((s,r)=>{this.walkDownDOM(e,i=>{r||this.cleanup(i);for(let o in i.dataset){if(!o.startsWith(s.name))continue;let c=o.slice(s.name.length),[d,...p]=c.split(/\_\_+/),h=d.length>0;h&&(d=d[0].toLowerCase()+d.slice(1));let V=`${i.dataset[o]}`||"",_=V,m=_.length>0,l=s.keyReq||0;if(h){if(l===2)throw u(s.name+"KeyNotAllowed")}else if(l===1)throw u(s.name+"KeyRequired");let E=s.valReq||0;if(m){if(E===2)throw u(s.name+"ValueNotAllowed")}else if(E===1)throw u(s.name+"ValueRequired");if(l===3||E===3){if(h&&m)throw u(s.name+"KeyAndValueProvided");if(!h&&!m)throw u(s.name+"KeyOrValueRequired")}i.id.length||(i.id=Z(i)),n.clear();let R=new Map;p.forEach(v=>{let[ge,...he]=v.split("_");R.set(W(ge),new Set(he))});let ue=[...s.macros?.pre||[],...this.macros,...s.macros?.post||[]];for(let v of ue)n.has(v)||(n.add(v),_=v.fn(_));let{actions:ce,apply:fe,cleanup:de}=this,pe=this,F;F={get signals(){return pe._signals},effect:v=>P(v),apply:fe.bind(this),cleanup:de.bind(this),actions:ce,genRX:()=>this.genRX(F,...s.argNames||[]),el:i,rawKey:o,rawValue:V,key:d,value:_,mods:R};let j=s.onLoad(F);j&&(this.removals.has(i)||this.removals.set(i,{id:i.id,set:new Set}),this.removals.get(i).set.add(j)),s?.removeOnLoad&&delete i.dataset[o]}})})}genRX(e,...n){let s=e.value.split(/;|\n/).map(l=>l.trim()).filter(l=>l!=""),r=s.length-1;s[r].startsWith("return")||(s[r]=`return (${s[r]});`);let o=s.join(` `),c=/(\w*)\(/gm,d=o.matchAll(c),p=new Set;for(let l of d)p.add(l[1]);let h=Object.keys(this.actions).filter(l=>p.has(l)),_=`${h.map(l=>`const ${l} = ctx.actions.${l}.fn;`).join(` -`)}return (()=> {${o}})()`,m=_.trim();h.forEach(l=>{m=m.replaceAll(l+"(",l+"(ctx,")});try{let l=n||[],E=new Function("ctx",...l,m);return(...R)=>E(e,...R)}catch(l){throw u("GeneratingExpressionFailed",{error:l,fnContent:_})}}walkDownDOM(e,n){if(!e||!(e instanceof HTMLElement||e instanceof SVGElement))return null;for(n(e),e=e.firstElementChild;e;)this.walkDownDOM(e,n),e=e.nextElementSibling}};var le=new k;le.load(K,H,z,U);var q=le;q.load();var Et=q;})(); +`)}return (()=> {${o}})()`,m=_.trim();h.forEach(l=>{m=m.replaceAll(l+"(",l+"(ctx,")});try{let l=n||[],E=new Function("ctx",...l,m);return(...R)=>E(e,...R)}catch(l){throw u("GeneratingExpressionFailed",{error:l,fnContent:_})}}walkDownDOM(e,n){if(!e||!(e instanceof HTMLElement||e instanceof SVGElement))return null;for(n(e),e=e.firstElementChild;e;)this.walkDownDOM(e,n),e=e.nextElementSibling}};var le=new k;le.load(z,H,K,U);var q=le;q.load();var Et=q;})(); //# sourceMappingURL=datastar-core.js.map diff --git a/bundles/datastar-core.js.map b/bundles/datastar-core.js.map index 2411931df..16d2a22bb 100644 --- a/bundles/datastar-core.js.map +++ b/bundles/datastar-core.js.map @@ -1,7 +1,7 @@ { "version": 3, "sources": ["../library/src/plugins/official/core/attributes/computed.ts", "../library/src/utils/text.ts", "../library/src/plugins/official/core/attributes/signals.ts", "../library/src/plugins/official/core/attributes/star.ts", "../library/src/plugins/official/core/macros/signals.ts", "../library/src/engine/consts.ts", "../library/src/utils/dom.ts", "../library/src/engine/errors.ts", "../library/src/vendored/preact-core.ts", "../library/src/engine/nestedSignals.ts", "../library/src/engine/engine.ts", "../library/src/engine/index.ts", "../library/src/bundles/datastar-core.ts"], - "sourcesContent": ["import {\n AttributePlugin,\n PluginType,\n Requirement,\n} from \"../../../../engine/types\";\n\nconst name = \"computed\";\nexport const Computed: AttributePlugin = {\n type: PluginType.Attribute,\n name,\n keyReq: Requirement.Must,\n valReq: Requirement.Must,\n onLoad: ({ key, signals, genRX }) => {\n const rx = genRX();\n signals.setComputed(key, rx);\n },\n};\n", "export const isBoolString = (str: string) => str.trim() === \"true\";\n\nexport const kebabize = (str: string) =>\n str.replace(\n /[A-Z]+(?![a-z])|[A-Z]/g,\n ($, ofs) => (ofs ? \"-\" : \"\") + $.toLowerCase(),\n );\n\nexport const camelize = (str: string) =>\n str.replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index == 0 ? word.toLowerCase() : word.toUpperCase();\n }).replace(/\\s+/g, \"\");\n\nexport const jsStrToObject = (raw: string) => {\n return (new Function(`return Object.assign({}, ${raw})`))();\n};\n", "import {\n AttributePlugin,\n NestedValues,\n PluginType,\n Requirement,\n} from \"../../../../engine/types\";\nimport { jsStrToObject } from \"../../../../utils/text\";\n\nexport const Signals: AttributePlugin = {\n type: PluginType.Attribute,\n name: \"signals\",\n valReq: Requirement.Must,\n removeOnLoad: true,\n onLoad: (ctx) => {\n const { key, genRX, signals } = ctx;\n if (key != \"\") {\n signals.setValue(key, genRX()());\n } else {\n const obj = jsStrToObject(ctx.value);\n ctx.value = JSON.stringify(obj);\n signals.merge(genRX()());\n }\n },\n};\n", "import {\n AttributePlugin,\n PluginType,\n Requirement,\n} from \"../../../../engine/types\";\n\nexport const Star: AttributePlugin = {\n type: PluginType.Attribute,\n name: \"star\",\n keyReq: Requirement.Denied,\n valReq: Requirement.Denied,\n onLoad: () => {\n alert(\"YOU ARE PROBABLY OVERCOMPLICATING IT\");\n },\n};\n", "import { MacroPlugin, PluginType } from \"../../../../engine/types\";\n\nexport const SignalValueMacro: MacroPlugin = {\n name: \"signalValue\",\n type: PluginType.Macro,\n fn: (original: string) => {\n const validJS = /(?[\\w0-9.]*)((\\.value))/gm;\n const sub = `ctx.signals.signal('$1').value`;\n return original.replaceAll(validJS, sub);\n },\n};\n", "// This is auto-generated by Datastar. DO NOT EDIT.\n\nexport const DATASTAR = \"datastar\";\nexport const DATASTAR_EVENT = \"datastar-event\";\nexport const DATASTAR_REQUEST = \"Datastar-Request\";\nexport const VERSION = \"0.21.0-beta2\";\n\n// #region Defaults\n\n// #region Default durations\n\n// The default duration for settling during fragment merges. Allows for CSS transitions to complete.\nexport const DefaultFragmentsSettleDurationMs = 300;\n// The default duration for retrying SSE on connection reset. This is part of the underlying retry mechanism of SSE.\nexport const DefaultSseRetryDurationMs = 1000;\n\n// #endregion\n\n\n// #region Default strings\n\n// The default attributes for