sprae 2.8.4 → 2.10.0

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "sprae",
3
3
  "description": "DOM microhydration.",
4
- "version": "2.8.4",
4
+ "version": "2.10.0",
5
5
  "main": "src/index.js",
6
6
  "module": "src/index.js",
7
7
  "type": "module",
package/readme.md CHANGED
@@ -176,7 +176,7 @@ Add event listeners.
176
176
  * `.prevent`, `.stop` – prevent default or stop propagation.
177
177
  * `.window`, `.document`, `.outside`, `.self` – specify event target.
178
178
  * `.throttle-108`, `.debounce-108` – define throttling or postponing callback with (optional) timeout in ms.
179
- * `.ctrl`, `.shift`, `.alt`, `.meta`, `.cmd`, `.down`, `.up`, `.left`, `.right`, `.end`, `.home`, `.pagedown`, `.pageup`, `.enter`, `.plus`, `.minus`, `.star`, `.slash`, `.period`, `.equal`, `.underscore`, `.esc`, `.escape`, `.tab`, `.space`, `.backspace`, `.delete`, `.arrow`, `.alnum`, `.digit`, `.letter` – filter by [`event.key`](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values).
179
+ * `.ctrl`, `.shift`, `.alt`, `.meta`, `.arrow`, `.enter`, `.escape`, `.tab`, `.space`, `.backspace`, `.delete`, `.digit`, `.letter`, `.character` – filter by [`event.key`](https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values).
180
180
  * `.*` – any other modifier has no effect, but allows binding multiple handlers to same event (like jQuery event classes).
181
181
 
182
182
  #### `:data="values"`
package/sprae.js CHANGED
@@ -688,40 +688,35 @@ var directives_default = (el, expr, state, name) => {
688
688
  };
689
689
  var _stop = Symbol("stop");
690
690
  var addListener = (el, evt, startFn) => {
691
- let evts = evt.split("..").map((e3) => e3.startsWith("on") ? e3.slice(2) : e3), opts = {}, conditions = [], cond;
692
- evts[0] = evts[0].replace(
693
- /\.(\w+)?-?([\w]+)?/g,
694
- (match, mod, param) => {
695
- (mod = mods[mod]) ? ([el, startFn, cond] = mod(el, startFn, opts, param), cond && conditions.push(cond), "") : "";
696
- return "";
697
- }
698
- );
699
- if (conditions.length) {
700
- let _cb = startFn;
701
- startFn = (e3) => {
702
- for (let cond2 of conditions)
703
- if (cond2(e3) === false)
691
+ let evts = evt.split("..").map((e3) => e3.startsWith("on") ? e3.slice(2) : e3), opts = { target: el, hooks: [], fn: startFn };
692
+ evts[0] = evts[0].replace(/\.(\w+)?-?([\w]+)?/g, (match, mod, param) => (mods[mod]?.(opts, param), ""));
693
+ let { target, hooks, fn, ...listenerOpts } = opts;
694
+ if (hooks.length) {
695
+ let _fn = fn;
696
+ fn = (e3) => {
697
+ for (let hook of hooks)
698
+ if (hook(e3) === false)
704
699
  return false;
705
- _cb(e3);
700
+ return _fn(e3);
706
701
  };
707
702
  }
708
703
  if (evts.length == 1)
709
- el.addEventListener(evts[0], startFn, opts);
704
+ target.addEventListener(evts[0], fn, listenerOpts);
710
705
  else {
711
- const nextEvt = (fn, cur = 0) => {
706
+ const nextEvt = (handler, cur = 0) => {
712
707
  let curListener = (e3) => {
713
- el.removeEventListener(evts[cur], curListener);
714
- if (typeof (fn = fn.call(el, e3)) !== "function")
715
- fn = () => {
708
+ target.removeEventListener(evts[cur], curListener);
709
+ if (typeof (handler = handler.call(target, e3)) !== "function")
710
+ handler = () => {
716
711
  };
717
712
  if (++cur < evts.length)
718
- nextEvt(fn, cur);
719
- else if (!startFn[_stop])
720
- nextEvt(startFn);
713
+ nextEvt(handler, cur);
714
+ else if (!fn[_stop])
715
+ nextEvt(fn);
721
716
  };
722
- el.addEventListener(evts[cur], curListener, opts);
717
+ target.addEventListener(evts[cur], curListener, listenerOpts);
723
718
  };
724
- nextEvt(startFn);
719
+ nextEvt(fn);
725
720
  }
726
721
  };
727
722
  var removeListener = (el, evt, fn) => {
@@ -730,118 +725,119 @@ var removeListener = (el, evt, fn) => {
730
725
  el.removeEventListener(evt, fn);
731
726
  };
732
727
  var mods = {
733
- throttle(el, cb, opts, limit) {
728
+ throttle(opts, limit) {
729
+ let { fn } = opts;
734
730
  limit = Number(limit) || 108;
735
731
  let pause, planned, block = () => {
736
732
  pause = true;
737
733
  setTimeout(() => {
738
734
  pause = false;
739
735
  if (planned)
740
- cb(e), planned = false, block();
736
+ return planned = false, block(), fn(e);
741
737
  });
742
738
  };
743
- return [el, (e3) => {
739
+ opts.fn = (e3) => {
744
740
  if (pause)
745
741
  return planned = true;
746
- cb(e3);
747
742
  block();
748
- }];
743
+ return fn(e3);
744
+ };
749
745
  },
750
- debounce(el, cb, opts, wait) {
746
+ debounce(opts, wait) {
747
+ let { fn } = opts;
751
748
  wait = Number(wait) || 108;
752
- let timeout, later = () => {
753
- timeout = null;
754
- cb(e);
755
- };
756
- return [el, (e3) => {
749
+ let timeout;
750
+ opts.fn = (e3) => {
757
751
  clearTimeout(timeout);
758
- timeout = setTimeout(later, wait);
759
- }];
752
+ timeout = setTimeout(() => {
753
+ timeout = null;
754
+ fn(e3);
755
+ }, wait);
756
+ };
760
757
  },
761
- window(el, cb) {
762
- return [window, cb];
758
+ window(opts) {
759
+ opts.target = window;
763
760
  },
764
- document(el, cb) {
765
- return [document, cb];
761
+ document(opts) {
762
+ opts.target = document;
766
763
  },
767
- outside(el, cb) {
768
- return [el, cb, (e3) => {
769
- if (el.contains(e3.target))
764
+ outside({ target, hooks }) {
765
+ hooks.push((e3) => {
766
+ if (target.contains(e3.target))
770
767
  return false;
771
768
  if (e3.target.isConnected === false)
772
769
  return false;
773
- if (el.offsetWidth < 1 && el.offsetHeight < 1)
770
+ if (target.offsetWidth < 1 && target.offsetHeight < 1)
774
771
  return false;
775
- }];
772
+ });
776
773
  },
777
- prevent(el, cb) {
778
- return [el, cb, (e3) => {
774
+ prevent({ hooks }) {
775
+ hooks.push((e3) => {
779
776
  e3.preventDefault();
780
- }];
777
+ });
781
778
  },
782
- stop(el, cb) {
783
- return [el, cb, (e3) => {
779
+ stop({ hooks }) {
780
+ hooks.push((e3) => {
784
781
  e3.stopPropagation();
785
- }];
782
+ });
786
783
  },
787
- self(el, cb) {
788
- return [el, cb, (e3) => {
789
- return e3.target === el;
790
- }];
784
+ self({ target, hooks }) {
785
+ hooks.push((e3) => {
786
+ return e3.target === target;
787
+ });
791
788
  },
792
- once(el, cb, opts) {
789
+ once(opts) {
793
790
  opts.once = true;
794
- return [el, cb];
795
791
  },
796
- passive(el, cb, opts) {
792
+ passive(opts) {
797
793
  opts.passive = true;
798
- return [el, cb];
799
794
  },
800
- capture(el, cb, opts) {
795
+ capture(opts) {
801
796
  opts.capture = true;
802
- return [el, cb];
797
+ },
798
+ ctrl({ hooks }) {
799
+ hooks.push((e3) => e3.key === "Control" || e3.key === "Ctrl");
800
+ },
801
+ shift({ hooks }) {
802
+ hooks.push((e3) => e3.key === "Shift");
803
+ },
804
+ alt({ hooks }) {
805
+ hooks.push((e3) => e3.key === "Alt");
806
+ },
807
+ meta({ hooks }) {
808
+ hooks.push((e3) => e3.key === "Meta");
809
+ },
810
+ arrow({ hooks }) {
811
+ hooks.push((e3) => e3.key.startsWith("Arrow"));
812
+ },
813
+ enter({ hooks }) {
814
+ hooks.push((e3) => e3.key === "Enter");
815
+ },
816
+ escape({ hooks }) {
817
+ hooks.push((e3) => e3.key.startsWith("Esc"));
818
+ },
819
+ tab({ hooks }) {
820
+ hooks.push((e3) => e3.key === "Tab");
821
+ },
822
+ space({ hooks }) {
823
+ hooks.push((e3) => e3.key === "Space" || e3.key === " ");
824
+ },
825
+ backspace({ hooks }) {
826
+ hooks.push((e3) => e3.key === "Backspace");
827
+ },
828
+ delete({ hooks }) {
829
+ hooks.push((e3) => e3.key === "Delete");
830
+ },
831
+ digit({ hooks }) {
832
+ hooks.push((e3) => /\d/.test(e3.key));
833
+ },
834
+ letter({ hooks }) {
835
+ hooks.push((e3) => /[a-zA-Z]/.test(e3.key));
836
+ },
837
+ character({ hooks }) {
838
+ hooks.push((e3) => /^\S$/.test(e3.key));
803
839
  }
804
840
  };
805
- var keys = {
806
- ctrl: "Control Ctrl",
807
- shift: "Shift",
808
- alt: "Alt",
809
- meta: "Meta",
810
- cmd: "Meta",
811
- down: "ArrowDown",
812
- up: "ArrowUp",
813
- left: "ArrowLeft",
814
- right: "ArrowRight",
815
- end: "End",
816
- home: "Home",
817
- pagedown: "PageDown",
818
- pageup: "PageUp",
819
- enter: "Enter",
820
- esc: "Escape",
821
- escape: "Escape",
822
- tab: "Tab",
823
- backspace: "Backspace",
824
- delete: "Delete",
825
- space: / /,
826
- plus: /\+/,
827
- minus: /\-/,
828
- star: /\*/,
829
- slash: /\//,
830
- period: /\./,
831
- equal: /\=/,
832
- underscore: /\_/,
833
- alnum: /\w/,
834
- letter: /[a-zA-Z]/,
835
- digit: /\d/
836
- };
837
- keys.arrow = keys.down + keys.up + keys.left + keys.right;
838
- for (let keyAttr in keys) {
839
- let keyName = keys[keyAttr];
840
- mods[keyAttr] = (el, cb, opts, extraKey) => [el, cb, (e3) => {
841
- if (!e3.key || (e3.key.length > 2 ? !keyName.includes?.(e3.key) : !keyName.test?.(e3.key)))
842
- return false;
843
- }];
844
- }
845
841
  var attr = (el, name, v2) => {
846
842
  if (v2 == null || v2 === false)
847
843
  el.removeAttribute(name);
package/sprae.min.js CHANGED
@@ -1 +1 @@
1
- function t(){throw new Error("Cycle detected")}function r(){if(o>1)o--;else{for(var t,e=!1;void 0!==n;){var r=n;for(n=void 0,s++;void 0!==r;){var i=r.o;if(r.o=void 0,r.f&=-3,!(8&r.f)&&f(r))try{r.c()}catch(r){e||(t=r,e=!0)}r=i}}if(s=0,o--,e)throw t}}var i=void 0,n=void 0,o=0,s=0,l=0;function a(t){if(void 0!==i){var e=t.n;if(void 0===e||e.t!==i)return i.s=e={i:0,S:t,p:void 0,n:i.s,t:i,e:void 0,x:void 0,r:e},t.n=e,32&i.f&&t.S(e),e;if(-1===e.i)return e.i=0,void 0!==e.p&&(e.p.n=e.n,void 0!==e.n&&(e.n.p=e.p),e.p=void 0,e.n=i.s,i.s.p=e,i.s=e),e}}function u(t){this.v=t,this.i=0,this.n=void 0,this.t=void 0}function f(t){for(var e=t.s;void 0!==e;e=e.n)if(e.S.i!==e.i||!e.S.h()||e.S.i!==e.i)return!0;return!1}function c(t){for(var e=t.s;void 0!==e;e=e.n){var r=e.S.n;void 0!==r&&(e.r=r),e.S.n=e,e.i=-1}}function h(t){for(var e=t.s,r=void 0;void 0!==e;){var i=e.n;-1===e.i?(e.S.U(e),e.n=void 0):(void 0!==r&&(r.p=e),e.p=void 0,e.n=r,r=e),e.S.n=e.r,void 0!==e.r&&(e.r=void 0),e=i}t.s=r}function v(t){u.call(this,void 0),this.x=t,this.s=void 0,this.g=l-1,this.f=4}function p(t){var e=t.u;if(t.u=void 0,"function"==typeof e){o++;var n=i;i=void 0;try{e()}catch(e){throw t.f&=-2,t.f|=8,d(t),e}finally{i=n,r()}}}function d(t){for(var e=t.s;void 0!==e;e=e.n)e.S.U(e);t.x=void 0,t.s=void 0,p(t)}function y(t){if(i!==this)throw new Error("Out-of-order effect");h(this),i=t,this.f&=-2,8&this.f&&d(this),r()}function b(t){this.x=t,this.u=void 0,this.s=void 0,this.o=void 0,this.f=32}function m(t){var e=new b(t);return e.c(),e.d.bind(e)}u.prototype.h=function(){return!0},u.prototype.S=function(t){this.t!==t&&void 0===t.e&&(t.x=this.t,void 0!==this.t&&(this.t.e=t),this.t=t)},u.prototype.U=function(t){var e=t.e,r=t.x;void 0!==e&&(e.x=r,t.e=void 0),void 0!==r&&(r.e=e,t.x=void 0),t===this.t&&(this.t=r)},u.prototype.subscribe=function(t){var e=this;return m((function(){var r=e.value,i=32&this.f;this.f&=-33;try{t(r)}finally{this.f|=i}}))},u.prototype.valueOf=function(){return this.value},u.prototype.toString=function(){return this.value+""},u.prototype.peek=function(){return this.v},Object.defineProperty(u.prototype,"value",{get:function(){var t=a(this);return void 0!==t&&(t.i=this.i),this.v},set:function(e){if(e!==this.v){s>100&&t(),this.v=e,this.i++,l++,o++;try{for(var i=this.t;void 0!==i;i=i.x)i.t.N()}finally{r()}}}}),(v.prototype=new u).h=function(){if(this.f&=-3,1&this.f)return!1;if(32==(36&this.f))return!0;if(this.f&=-5,this.g===l)return!0;if(this.g=l,this.f|=1,this.i>0&&!f(this))return this.f&=-2,!0;var t=i;try{c(this),i=this;var e=this.x();(16&this.f||this.v!==e||0===this.i)&&(this.v=e,this.f&=-17,this.i++)}catch(t){this.v=t,this.f|=16,this.i++}return i=t,h(this),this.f&=-2,!0},v.prototype.S=function(t){if(void 0===this.t){this.f|=36;for(var e=this.s;void 0!==e;e=e.n)e.S.S(e)}u.prototype.S.call(this,t)},v.prototype.U=function(t){if(u.prototype.U.call(this,t),void 0===this.t){this.f&=-33;for(var e=this.s;void 0!==e;e=e.n)e.S.U(e)}},v.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var t=this.t;void 0!==t;t=t.x)t.t.N()}},v.prototype.peek=function(){if(this.h()||t(),16&this.f)throw this.v;return this.v},Object.defineProperty(v.prototype,"value",{get:function(){1&this.f&&t();var e=a(this);if(this.h(),void 0!==e&&(e.i=this.i),16&this.f)throw this.v;return this.v}}),b.prototype.c=function(){var t=this.S();try{8&this.f||void 0===this.x||(this.u=this.x())}finally{t()}},b.prototype.S=function(){1&this.f&&t(),this.f|=1,this.f&=-9,p(this),c(this),o++;var e=i;return i=this,y.bind(this,e)},b.prototype.N=function(){2&this.f||(this.f|=2,this.o=n,n=this)},b.prototype.d=function(){this.f|=8,1&this.f||d(this)},Symbol.observable||=Symbol("observable");var g=new FinalizationRegistry((t=>t.call?.())),w=(t,e,r,i,n,o)=>{return t&&(s=(t[Symbol.observable]?.()||t).subscribe?.(e,r,i),o=s&&(()=>s.unsubscribe?.())||t.set&&t.call?.(n,e)||(t.then?.((t=>(!n&&e(t),i?.())),r)||(async o=>{try{for await(o of t){if(n)return;e(o)}i?.()}catch(t){r?.(t)}})())&&(t=>n=1),g.register(t,o),o);var s},A=t=>t&&t[S],S=Symbol("signal-struct");function x(t,e){if(A(t)&&!e)return t;if(O(t)){const n=Object.create(e||Object.getPrototypeOf(t)),o={},s=Object.getOwnPropertyDescriptors(t);for(let t in s){let e=s[t];if(e.get){let r=o[t]=new v(e.get.bind(n));Object.defineProperty(n,t,{get:()=>r.value,set:e.set?.bind(n),configurable:!1,enumerable:!0})}else{let s=e.value,l=(i=s)&&!!(i[Symbol.observable]||i[Symbol.asyncIterator]||i.call&&i.set||i.subscribe||i.then),a=o[t]=(r=s)&&r.peek?s:new u(l?void 0:O(s)?Object.seal(x(s)):Array.isArray(s)?x(s):s);l&&w(s,(t=>a.value=t)),Object.defineProperty(n,t,{get:()=>a.value,set(t){if(O(t)){if(O(a.value))try{return void Object.assign(a.value,t)}catch(t){}a.value=Object.seal(x(t))}else Array.isArray(t)?a.value=x(t):a.value=t},enumerable:!0,configurable:!1})}}return Object.defineProperty(n,S,{configurable:!1,enumerable:!1,value:!0}),n}var r,i;if(Array.isArray(t)&&!A(t[0]))for(let e=0;e<t.length;e++)t[e]=x(t[e]);return t}function O(t){return t&&t.constructor===Object}x.isStruct=A;var j=(t,e,r,i=null)=>{let n,o,s,l=0,a=r.length,u=e.length,{remove:f,same:c,insert:h,replace:v}=j;for(;l<a&&l<u&&c(e[l],r[l]);)l++;for(;l<a&&l<u&&c(r[a-1],e[u-1]);)i=r[(--u,--a)];if(l==u)for(;l<a;)h(i,r[l++],t);else{for(n=e[l];l<a;)s=r[l++],o=n?n.nextSibling:i,c(n,s)?n=o:l<a&&c(r[l],o)?(v(n,s,t),n=o):h(n,s,t);for(;!c(n,i);)o=n.nextSibling,f(n,t),n=o}return r};j.same=(t,e)=>t==e,j.replace=(t,e,r)=>r.replaceChild(e,t),j.insert=(t,e,r)=>r.insertBefore(e,t),j.remove=(t,e)=>e.removeChild(t);var N=j,E={},k={},P={},W=t=>null===t?k:void 0===t?P:"number"==typeof t||t instanceof Number?E[t]||(E[t]=new Number(t)):"string"==typeof t||t instanceof String?E[t]||(E[t]=new String(t)):"boolean"==typeof t||t instanceof Boolean?E[t]||(E[t]=new Boolean(t)):t,C={},T={};C.if=(t,e)=>{let r=document.createTextNode(""),i=[F(t,e,":if")],n=[t],o=t;for(;(o=t.nextElementSibling)&&o.hasAttribute(":else");)o.removeAttribute(":else"),(e=o.getAttribute(":if"))?(o.removeAttribute(":if"),o.remove(),n.push(o),i.push(F(t,e,":else :if"))):(o.remove(),n.push(o),i.push((()=>1)));return t.replaceWith(o=r),t=>{let e=i.findIndex((e=>e(t)));n[e]!=o&&((o[D]||o).replaceWith(o=n[e]||r),q(o,t))}},C.with=(t,e,r)=>{q(t,x(F(t,e,"with")(r),r))};var D=Symbol(":each");C.each=(t,e)=>{let r=function(t){let e=/,([^,\}\]]*)(?:,([^,\}\]]*))?$/,r=t.match(/([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/);if(!r)return;let i=r[2].trim(),n=r[1].replace(/^\s*\(|\)\s*$/g,"").trim(),o=n.match(e);return o?[n.replace(e,"").trim(),o[1].trim(),i]:[n,"",i]}(e);if(!r)return H(new Error,t,e);const i=t[D]=document.createTextNode("");t.replaceWith(i);const n=F(t,r[2],":each"),o=t.getAttribute(":key"),s=o?F(null,o):null;t.removeAttribute(":key");const l=new WeakMap,a=new WeakMap;let u=[];return o=>{let f=n(o);f?"number"==typeof f?f=Array.from({length:f},((t,e)=>[e,e+1])):Array.isArray(f)?f=f.map(((t,e)=>[e+1,t])):"object"==typeof f?f=Object.entries(f):H(Error("Bad list value"),t,e,":each"):f=[];let c=[],h=[];for(let[e,i]of f){let n,u,f=s?.({[r[0]]:i,[r[1]]:e});("string"==typeof(v=f)||"boolean"==typeof v||"number"==typeof v)&&(f=W(f)),null==f?n=t.cloneNode(!0):(n=a.get(f))||a.set(f,n=t.cloneNode(!0)),c.push(n),null!=f&&(u=l.get(f))?u[r[0]]=i:(u=x({[r[0]]:i,[r[1]]:e},o),null!=f&&l.set(f,u)),h.push(u)}var v;N(i.parentNode,u,c,i),u=c;for(let t=0;t<c.length;t++)q(c[t],h[t])}},T.ref=(t,e,r)=>{r[e]=t},T.id=(t,e)=>{let r=F(t,e,":id");return e=>{return i=r(e),t.id=i||0===i?i:"";var i}},T.class=(t,e)=>{let r=F(t,e,":class"),i=t.className;return e=>{let n=r(e);t.className=i+typeof n=="string"?n:(Array.isArray(n)?n:Object.entries(n).map((([t,e])=>e?t:""))).filter(Boolean).join(" ")}},T.style=(t,e)=>{let r=F(t,e,":style"),i=t.getAttribute("style")||"";return i.endsWith(";")||(i+="; "),e=>{let n=r(e);if("string"==typeof n)t.setAttribute("style",i+n);else for(let e in n)t.style[e]=n[e]}},T.text=(t,e)=>{let r=F(t,e,":text");return e=>{let i=r(e);t.textContent=null==i?"":i}},T.data=(t,e)=>{let r=F(t,e,":data");return e=>{let i=r(e);for(let e in i)t.dataset[e]=i[e]}},T.aria=(t,e)=>{let r=F(t,e,":aria");return e=>(e=>{for(let r in e)R(t,"aria-"+I(r),null==e[r]?null:e[r]+"")})(r(e))},T[""]=(t,e)=>{let r=F(t,e,":");if(r)return e=>{let i=r(e);for(let e in i)R(t,I(e),i[e])}},T.value=(t,e)=>{let r,i,n=F(t,e,":value"),o="text"===t.type||""===t.type?e=>t.setAttribute("value",t.value=null==e?"":e):"TEXTAREA"===t.tagName||"text"===t.type||""===t.type?e=>(r=t.selectionStart,i=t.selectionEnd,t.setAttribute("value",t.value=null==e?"":e),r&&t.setSelectionRange(r,i)):"checkbox"===t.type?e=>(t.value=e?"on":"",R(t,"checked",e)):"select-one"===t.type?e=>{for(let e in t.options)e.removeAttribute("selected");t.value=e,t.selectedOptions[0]?.setAttribute("selected","")}:e=>t.value=e;return t=>o(n(t))},T.on=(t,e)=>{let r=F(t,e,":on");return e=>{let i=r(e);for(let e in i)B(t,e,i[e]);return()=>{for(let e in i)L(t,e,i[e])}}};var U=(t,e,r,i)=>{let n=i.startsWith("on")&&i.slice(2),o=F(t,e,":"+i);if(o)return n?e=>{let r=o(e)||(()=>{});return B(t,n,r),()=>L(t,n,r)}:e=>R(t,i,o(e))},$=Symbol("stop"),B=(t,e,r)=>{let i,n=e.split("..").map((t=>t.startsWith("on")?t.slice(2):t)),o={},s=[];if(n[0]=n[0].replace(/\.(\w+)?-?([\w]+)?/g,((e,n,l)=>((n=M[n])&&([t,r,i]=n(t,r,o,l),i&&s.push(i)),""))),s.length){let t=r;r=e=>{for(let t of s)if(!1===t(e))return!1;t(e)}}if(1==n.length)t.addEventListener(n[0],r,o);else{const e=(i,s=0)=>{let l=o=>{t.removeEventListener(n[s],l),"function"!=typeof(i=i.call(t,o))&&(i=()=>{}),++s<n.length?e(i,s):r[$]||e(r)};t.addEventListener(n[s],l,o)};e(r)}},L=(t,e,r)=>{e.indexOf("..")>=0&&(r[$]=!0),t.removeEventListener(e,r)},M={throttle(t,r,i,n){n=Number(n)||108;let o,s,l=()=>{o=!0,setTimeout((()=>{o=!1,s&&(r(e),s=!1,l())}))};return[t,t=>{if(o)return s=!0;r(t),l()}]},debounce(t,r,i,n){n=Number(n)||108;let o,s=()=>{o=null,r(e)};return[t,t=>{clearTimeout(o),o=setTimeout(s,n)}]},window:(t,e)=>[window,e],document:(t,e)=>[document,e],outside:(t,e)=>[t,e,e=>!t.contains(e.target)&&!1!==e.target.isConnected&&!(t.offsetWidth<1&&t.offsetHeight<1)&&void 0],prevent:(t,e)=>[t,e,t=>{t.preventDefault()}],stop:(t,e)=>[t,e,t=>{t.stopPropagation()}],self:(t,e)=>[t,e,e=>e.target===t],once:(t,e,r)=>(r.once=!0,[t,e]),passive:(t,e,r)=>(r.passive=!0,[t,e]),capture:(t,e,r)=>(r.capture=!0,[t,e])},_={ctrl:"Control Ctrl",shift:"Shift",alt:"Alt",meta:"Meta",cmd:"Meta",down:"ArrowDown",up:"ArrowUp",left:"ArrowLeft",right:"ArrowRight",end:"End",home:"Home",pagedown:"PageDown",pageup:"PageUp",enter:"Enter",esc:"Escape",escape:"Escape",tab:"Tab",backspace:"Backspace",delete:"Delete",space:/ /,plus:/\+/,minus:/\-/,star:/\*/,slash:/\//,period:/\./,equal:/\=/,underscore:/\_/,alnum:/\w/,letter:/[a-zA-Z]/,digit:/\d/};_.arrow=_.down+_.up+_.left+_.right;for(let t in _){let e=_[t];M[t]=(t,r,i,n)=>[t,r,t=>{if(!t.key||(t.key.length>2?!e.includes?.(t.key):!e.test?.(t.key)))return!1}]}var R=(t,e,r)=>{null==r||!1===r?t.removeAttribute(e):t.setAttribute(e,!0===r?"":"number"==typeof r||"string"==typeof r?r:"")},z={};function F(t,e,r){let i=z[e];if(!i){let n=/^[\n\s]*if.*\(.*\)/.test(e)||/^(let|const)\s/.test(e)?`(() => { ${e} })()`:e;try{i=z[e]=new Function("__scope",`with (__scope) { return ${n} };`)}catch(i){return H(i,t,e,r)}}return n=>{let o;try{o=i.call(t,n)}catch(i){return H(i,t,e,r)}return o}}function H(t,e,r,i){Object.assign(t,{element:e,expression:r}),console.warn(`∴ ${t.message}\n\n${i}=${r?`"${r}"\n\n`:""}`,e),setTimeout((()=>{throw t}),0)}function I(t){return t.replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,(t=>"-"+t.toLowerCase()))}var Z=new WeakMap;function q(t,e){if(!t.children)return;if(Z.has(t)){let i=Z.get(t);return function(t){if(o>0)return t();o++;try{t()}finally{r()}}((()=>Object.assign(i,e))),i}const i=x(e||{}),n=[],s=(t,e=t.parentNode)=>{for(let r in C){let o=":"+r;if(t.hasAttribute?.(o)){let s=t.getAttribute(o);if(t.removeAttribute(o),n.push(C[r](t,s,i,r)),Z.has(t)||t.parentNode!==e)return!1}}if(t.attributes)for(let r=0;r<t.attributes.length;){let o=t.attributes[r];if(":"!==o.name[0]){r++;continue}t.removeAttribute(o.name);let s=o.value,l=o.name.slice(1).split(":");for(let r of l){let o=T[r]||U;if(n.push(o(t,s,i,r)),Z.has(t)||t.parentNode!==e)return!1}}for(let e,r=0;e=t.children[r];r++)!1===s(e,t)&&r--};s(t);for(let t of n)if(t){let e;m((()=>{"function"==typeof e&&e(),e=t(i)}))}return Object.seal(i),Z.set(t,i),i}var X=q;export{X as default};
1
+ function t(){throw new Error("Cycle detected")}function r(){if(n>1)n--;else{for(var t,e=!1;void 0!==o;){var r=o;for(o=void 0,s++;void 0!==r;){var i=r.o;if(r.o=void 0,r.f&=-3,!(8&r.f)&&f(r))try{r.c()}catch(r){e||(t=r,e=!0)}r=i}}if(s=0,n--,e)throw t}}var i=void 0,o=void 0,n=0,s=0,l=0;function a(t){if(void 0!==i){var e=t.n;if(void 0===e||e.t!==i)return i.s=e={i:0,S:t,p:void 0,n:i.s,t:i,e:void 0,x:void 0,r:e},t.n=e,32&i.f&&t.S(e),e;if(-1===e.i)return e.i=0,void 0!==e.p&&(e.p.n=e.n,void 0!==e.n&&(e.n.p=e.p),e.p=void 0,e.n=i.s,i.s.p=e,i.s=e),e}}function u(t){this.v=t,this.i=0,this.n=void 0,this.t=void 0}function f(t){for(var e=t.s;void 0!==e;e=e.n)if(e.S.i!==e.i||!e.S.h()||e.S.i!==e.i)return!0;return!1}function h(t){for(var e=t.s;void 0!==e;e=e.n){var r=e.S.n;void 0!==r&&(e.r=r),e.S.n=e,e.i=-1}}function c(t){for(var e=t.s,r=void 0;void 0!==e;){var i=e.n;-1===e.i?(e.S.U(e),e.n=void 0):(void 0!==r&&(r.p=e),e.p=void 0,e.n=r,r=e),e.S.n=e.r,void 0!==e.r&&(e.r=void 0),e=i}t.s=r}function v(t){u.call(this,void 0),this.x=t,this.s=void 0,this.g=l-1,this.f=4}function p(t){var e=t.u;if(t.u=void 0,"function"==typeof e){n++;var o=i;i=void 0;try{e()}catch(e){throw t.f&=-2,t.f|=8,d(t),e}finally{i=o,r()}}}function d(t){for(var e=t.s;void 0!==e;e=e.n)e.S.U(e);t.x=void 0,t.s=void 0,p(t)}function y(t){if(i!==this)throw new Error("Out-of-order effect");c(this),i=t,this.f&=-2,8&this.f&&d(this),r()}function b(t){this.x=t,this.u=void 0,this.s=void 0,this.o=void 0,this.f=32}function g(t){var e=new b(t);return e.c(),e.d.bind(e)}u.prototype.h=function(){return!0},u.prototype.S=function(t){this.t!==t&&void 0===t.e&&(t.x=this.t,void 0!==this.t&&(this.t.e=t),this.t=t)},u.prototype.U=function(t){var e=t.e,r=t.x;void 0!==e&&(e.x=r,t.e=void 0),void 0!==r&&(r.e=e,t.x=void 0),t===this.t&&(this.t=r)},u.prototype.subscribe=function(t){var e=this;return g((function(){var r=e.value,i=32&this.f;this.f&=-33;try{t(r)}finally{this.f|=i}}))},u.prototype.valueOf=function(){return this.value},u.prototype.toString=function(){return this.value+""},u.prototype.peek=function(){return this.v},Object.defineProperty(u.prototype,"value",{get:function(){var t=a(this);return void 0!==t&&(t.i=this.i),this.v},set:function(e){if(e!==this.v){s>100&&t(),this.v=e,this.i++,l++,n++;try{for(var i=this.t;void 0!==i;i=i.x)i.t.N()}finally{r()}}}}),(v.prototype=new u).h=function(){if(this.f&=-3,1&this.f)return!1;if(32==(36&this.f))return!0;if(this.f&=-5,this.g===l)return!0;if(this.g=l,this.f|=1,this.i>0&&!f(this))return this.f&=-2,!0;var t=i;try{h(this),i=this;var e=this.x();(16&this.f||this.v!==e||0===this.i)&&(this.v=e,this.f&=-17,this.i++)}catch(t){this.v=t,this.f|=16,this.i++}return i=t,c(this),this.f&=-2,!0},v.prototype.S=function(t){if(void 0===this.t){this.f|=36;for(var e=this.s;void 0!==e;e=e.n)e.S.S(e)}u.prototype.S.call(this,t)},v.prototype.U=function(t){if(u.prototype.U.call(this,t),void 0===this.t){this.f&=-33;for(var e=this.s;void 0!==e;e=e.n)e.S.U(e)}},v.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var t=this.t;void 0!==t;t=t.x)t.t.N()}},v.prototype.peek=function(){if(this.h()||t(),16&this.f)throw this.v;return this.v},Object.defineProperty(v.prototype,"value",{get:function(){1&this.f&&t();var e=a(this);if(this.h(),void 0!==e&&(e.i=this.i),16&this.f)throw this.v;return this.v}}),b.prototype.c=function(){var t=this.S();try{8&this.f||void 0===this.x||(this.u=this.x())}finally{t()}},b.prototype.S=function(){1&this.f&&t(),this.f|=1,this.f&=-9,p(this),h(this),n++;var e=i;return i=this,y.bind(this,e)},b.prototype.N=function(){2&this.f||(this.f|=2,this.o=o,o=this)},b.prototype.d=function(){this.f|=8,1&this.f||d(this)},Symbol.observable||=Symbol("observable");var m=new FinalizationRegistry((t=>t.call?.())),k=(t,e,r,i,o,n)=>{return t&&(s=(t[Symbol.observable]?.()||t).subscribe?.(e,r,i),n=s&&(()=>s.unsubscribe?.())||t.set&&t.call?.(o,e)||(t.then?.((t=>(!o&&e(t),i?.())),r)||(async n=>{try{for await(n of t){if(o)return;e(n)}i?.()}catch(t){r?.(t)}})())&&(t=>o=1),m.register(t,n),n);var s},S=t=>t&&t[w],w=Symbol("signal-struct");function A(t,e){if(S(t)&&!e)return t;if(x(t)){const o=Object.create(e||Object.getPrototypeOf(t)),n={},s=Object.getOwnPropertyDescriptors(t);for(let t in s){let e=s[t];if(e.get){let r=n[t]=new v(e.get.bind(o));Object.defineProperty(o,t,{get:()=>r.value,set:e.set?.bind(o),configurable:!1,enumerable:!0})}else{let s=e.value,l=(i=s)&&!!(i[Symbol.observable]||i[Symbol.asyncIterator]||i.call&&i.set||i.subscribe||i.then),a=n[t]=(r=s)&&r.peek?s:new u(l?void 0:x(s)?Object.seal(A(s)):Array.isArray(s)?A(s):s);l&&k(s,(t=>a.value=t)),Object.defineProperty(o,t,{get:()=>a.value,set(t){if(x(t)){if(x(a.value))try{return void Object.assign(a.value,t)}catch(t){}a.value=Object.seal(A(t))}else Array.isArray(t)?a.value=A(t):a.value=t},enumerable:!0,configurable:!1})}}return Object.defineProperty(o,w,{configurable:!1,enumerable:!1,value:!0}),o}var r,i;if(Array.isArray(t)&&!S(t[0]))for(let e=0;e<t.length;e++)t[e]=A(t[e]);return t}function x(t){return t&&t.constructor===Object}A.isStruct=S;var O=(t,e,r,i=null)=>{let o,n,s,l=0,a=r.length,u=e.length,{remove:f,same:h,insert:c,replace:v}=O;for(;l<a&&l<u&&h(e[l],r[l]);)l++;for(;l<a&&l<u&&h(r[a-1],e[u-1]);)i=r[(--u,--a)];if(l==u)for(;l<a;)c(i,r[l++],t);else{for(o=e[l];l<a;)s=r[l++],n=o?o.nextSibling:i,h(o,s)?o=n:l<a&&h(r[l],n)?(v(o,s,t),o=n):c(o,s,t);for(;!h(o,i);)n=o.nextSibling,f(o,t),o=n}return r};O.same=(t,e)=>t==e,O.replace=(t,e,r)=>r.replaceChild(e,t),O.insert=(t,e,r)=>r.insertBefore(e,t),O.remove=(t,e)=>e.removeChild(t);var j=O,N={},E={},W={},C=t=>null===t?E:void 0===t?W:"number"==typeof t||t instanceof Number?N[t]||(N[t]=new Number(t)):"string"==typeof t||t instanceof String?N[t]||(N[t]=new String(t)):"boolean"==typeof t||t instanceof Boolean?N[t]||(N[t]=new Boolean(t)):t,T={},$={};T.if=(t,e)=>{let r=document.createTextNode(""),i=[z(t,e,":if")],o=[t],n=t;for(;(n=t.nextElementSibling)&&n.hasAttribute(":else");)n.removeAttribute(":else"),(e=n.getAttribute(":if"))?(n.removeAttribute(":if"),n.remove(),o.push(n),i.push(z(t,e,":else :if"))):(n.remove(),o.push(n),i.push((()=>1)));return t.replaceWith(n=r),t=>{let e=i.findIndex((e=>e(t)));o[e]!=n&&((n[P]||n).replaceWith(n=o[e]||r),H(n,t))}},T.with=(t,e,r)=>{H(t,A(z(t,e,"with")(r),r))};var P=Symbol(":each");T.each=(t,e)=>{let r=function(t){let e=/,([^,\}\]]*)(?:,([^,\}\]]*))?$/,r=t.match(/([\s\S]*?)\s+(?:in|of)\s+([\s\S]*)/);if(!r)return;let i=r[2].trim(),o=r[1].replace(/^\s*\(|\)\s*$/g,"").trim(),n=o.match(e);return n?[o.replace(e,"").trim(),n[1].trim(),i]:[o,"",i]}(e);if(!r)return F(new Error,t,e);const i=t[P]=document.createTextNode("");t.replaceWith(i);const o=z(t,r[2],":each"),n=t.getAttribute(":key"),s=n?z(null,n):null;t.removeAttribute(":key");const l=new WeakMap,a=new WeakMap;let u=[];return n=>{let f=o(n);f?"number"==typeof f?f=Array.from({length:f},((t,e)=>[e,e+1])):Array.isArray(f)?f=f.map(((t,e)=>[e+1,t])):"object"==typeof f?f=Object.entries(f):F(Error("Bad list value"),t,e,":each"):f=[];let h=[],c=[];for(let[e,i]of f){let o,u,f=s?.({[r[0]]:i,[r[1]]:e});("string"==typeof(v=f)||"boolean"==typeof v||"number"==typeof v)&&(f=C(f)),null==f?o=t.cloneNode(!0):(o=a.get(f))||a.set(f,o=t.cloneNode(!0)),h.push(o),null!=f&&(u=l.get(f))?u[r[0]]=i:(u=A({[r[0]]:i,[r[1]]:e},n),null!=f&&l.set(f,u)),c.push(u)}var v;j(i.parentNode,u,h,i),u=h;for(let t=0;t<h.length;t++)H(h[t],c[t])}},$.ref=(t,e,r)=>{r[e]=t},$.id=(t,e)=>{let r=z(t,e,":id");return e=>{return i=r(e),t.id=i||0===i?i:"";var i}},$.class=(t,e)=>{let r=z(t,e,":class"),i=t.className;return e=>{let o=r(e);t.className=i+typeof o=="string"?o:(Array.isArray(o)?o:Object.entries(o).map((([t,e])=>e?t:""))).filter(Boolean).join(" ")}},$.style=(t,e)=>{let r=z(t,e,":style"),i=t.getAttribute("style")||"";return i.endsWith(";")||(i+="; "),e=>{let o=r(e);if("string"==typeof o)t.setAttribute("style",i+o);else for(let e in o)t.style[e]=o[e]}},$.text=(t,e)=>{let r=z(t,e,":text");return e=>{let i=r(e);t.textContent=null==i?"":i}},$.data=(t,e)=>{let r=z(t,e,":data");return e=>{let i=r(e);for(let e in i)t.dataset[e]=i[e]}},$.aria=(t,e)=>{let r=z(t,e,":aria");return e=>(e=>{for(let r in e)_(t,"aria-"+I(r),null==e[r]?null:e[r]+"")})(r(e))},$[""]=(t,e)=>{let r=z(t,e,":");if(r)return e=>{let i=r(e);for(let e in i)_(t,I(e),i[e])}},$.value=(t,e)=>{let r,i,o=z(t,e,":value"),n="text"===t.type||""===t.type?e=>t.setAttribute("value",t.value=null==e?"":e):"TEXTAREA"===t.tagName||"text"===t.type||""===t.type?e=>(r=t.selectionStart,i=t.selectionEnd,t.setAttribute("value",t.value=null==e?"":e),r&&t.setSelectionRange(r,i)):"checkbox"===t.type?e=>(t.value=e?"on":"",_(t,"checked",e)):"select-one"===t.type?e=>{for(let e in t.options)e.removeAttribute("selected");t.value=e,t.selectedOptions[0]?.setAttribute("selected","")}:e=>t.value=e;return t=>n(o(t))},$.on=(t,e)=>{let r=z(t,e,":on");return e=>{let i=r(e);for(let e in i)U(t,e,i[e]);return()=>{for(let e in i)L(t,e,i[e])}}};var B=(t,e,r,i)=>{let o=i.startsWith("on")&&i.slice(2),n=z(t,e,":"+i);if(n)return o?e=>{let r=n(e)||(()=>{});return U(t,o,r),()=>L(t,o,r)}:e=>_(t,i,n(e))},D=Symbol("stop"),U=(t,e,r)=>{let i=e.split("..").map((t=>t.startsWith("on")?t.slice(2):t)),o={target:t,hooks:[],fn:r};i[0]=i[0].replace(/\.(\w+)?-?([\w]+)?/g,((t,e,r)=>(M[e]?.(o,r),"")));let{target:n,hooks:s,fn:l,...a}=o;if(s.length){let t=l;l=e=>{for(let t of s)if(!1===t(e))return!1;return t(e)}}if(1==i.length)n.addEventListener(i[0],l,a);else{const t=(e,r=0)=>{let o=s=>{n.removeEventListener(i[r],o),"function"!=typeof(e=e.call(n,s))&&(e=()=>{}),++r<i.length?t(e,r):l[D]||t(l)};n.addEventListener(i[r],o,a)};t(l)}},L=(t,e,r)=>{e.indexOf("..")>=0&&(r[D]=!0),t.removeEventListener(e,r)},M={throttle(t,r){let{fn:i}=t;r=Number(r)||108;let o,n,s=()=>{o=!0,setTimeout((()=>{if(o=!1,n)return n=!1,s(),i(e)}))};t.fn=t=>o?n=!0:(s(),i(t))},debounce(t,e){let r,{fn:i}=t;e=Number(e)||108,t.fn=t=>{clearTimeout(r),r=setTimeout((()=>{r=null,i(t)}),e)}},window(t){t.target=window},document(t){t.target=document},outside({target:t,hooks:e}){e.push((e=>!t.contains(e.target)&&!1!==e.target.isConnected&&!(t.offsetWidth<1&&t.offsetHeight<1)&&void 0))},prevent({hooks:t}){t.push((t=>{t.preventDefault()}))},stop({hooks:t}){t.push((t=>{t.stopPropagation()}))},self({target:t,hooks:e}){e.push((e=>e.target===t))},once(t){t.once=!0},passive(t){t.passive=!0},capture(t){t.capture=!0},ctrl({hooks:t}){t.push((t=>"Control"===t.key||"Ctrl"===t.key))},shift({hooks:t}){t.push((t=>"Shift"===t.key))},alt({hooks:t}){t.push((t=>"Alt"===t.key))},meta({hooks:t}){t.push((t=>"Meta"===t.key))},arrow({hooks:t}){t.push((t=>t.key.startsWith("Arrow")))},enter({hooks:t}){t.push((t=>"Enter"===t.key))},escape({hooks:t}){t.push((t=>t.key.startsWith("Esc")))},tab({hooks:t}){t.push((t=>"Tab"===t.key))},space({hooks:t}){t.push((t=>"Space"===t.key||" "===t.key))},backspace({hooks:t}){t.push((t=>"Backspace"===t.key))},delete({hooks:t}){t.push((t=>"Delete"===t.key))},digit({hooks:t}){t.push((t=>/\d/.test(t.key)))},letter({hooks:t}){t.push((t=>/[a-zA-Z]/.test(t.key)))},character({hooks:t}){t.push((t=>/^\S$/.test(t.key)))}},_=(t,e,r)=>{null==r||!1===r?t.removeAttribute(e):t.setAttribute(e,!0===r?"":"number"==typeof r||"string"==typeof r?r:"")},R={};function z(t,e,r){let i=R[e];if(!i){let o=/^[\n\s]*if.*\(.*\)/.test(e)||/^(let|const)\s/.test(e)?`(() => { ${e} })()`:e;try{i=R[e]=new Function("__scope",`with (__scope) { return ${o} };`)}catch(i){return F(i,t,e,r)}}return o=>{let n;try{n=i.call(t,o)}catch(i){return F(i,t,e,r)}return n}}function F(t,e,r,i){Object.assign(t,{element:e,expression:r}),console.warn(`∴ ${t.message}\n\n${i}=${r?`"${r}"\n\n`:""}`,e),setTimeout((()=>{throw t}),0)}function I(t){return t.replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,(t=>"-"+t.toLowerCase()))}var Z=new WeakMap;function H(t,e){if(!t.children)return;if(Z.has(t)){let i=Z.get(t);return function(t){if(n>0)return t();n++;try{t()}finally{r()}}((()=>Object.assign(i,e))),i}const i=A(e||{}),o=[],s=(t,e=t.parentNode)=>{for(let r in T){let n=":"+r;if(t.hasAttribute?.(n)){let s=t.getAttribute(n);if(t.removeAttribute(n),o.push(T[r](t,s,i,r)),Z.has(t)||t.parentNode!==e)return!1}}if(t.attributes)for(let r=0;r<t.attributes.length;){let n=t.attributes[r];if(":"!==n.name[0]){r++;continue}t.removeAttribute(n.name);let s=n.value,l=n.name.slice(1).split(":");for(let r of l){let n=$[r]||B;if(o.push(n(t,s,i,r)),Z.has(t)||t.parentNode!==e)return!1}}for(let e,r=0;e=t.children[r];r++)!1===s(e,t)&&r--};s(t);for(let t of o)if(t){let e;g((()=>{"function"==typeof e&&e(),e=t(i)}))}return Object.seal(i),Z.set(t,i),i}var X=H;export{X as default};
package/src/directives.js CHANGED
@@ -266,34 +266,29 @@ const _stop = Symbol('stop')
266
266
  const addListener = (el, evt, startFn) => {
267
267
  // ona..onb
268
268
  let evts = evt.split('..').map(e => e.startsWith('on') ? e.slice(2) : e),
269
- opts = {}, conditions = [], cond
269
+ opts = {target: el, hooks: [], fn: startFn}
270
270
 
271
271
  // onevt.debounce-108
272
- evts[0] = evts[0].replace(
273
- /\.(\w+)?-?([\w]+)?/g,
274
- (match, mod, param) => {
275
- (mod=mods[mod]) ? ([el, startFn, cond] = mod(el, startFn, opts, param), cond && conditions.push(cond), '') : ''
276
- return ''
277
- }
278
- );
279
- // we collect conditions into a sigle callback to check before throttles
280
- if (conditions.length) {
281
- let _cb = startFn
282
- startFn = (e) => { for (let cond of conditions) if (cond(e) === false) return false; _cb(e) }
272
+ evts[0] = evts[0].replace(/\.(\w+)?-?([\w]+)?/g, (match, mod, param) => (mods[mod]?.(opts, param), ''));
273
+ // we collect condition hooks into a sigle callback to check before throttles
274
+ let {target, hooks, fn, ...listenerOpts} = opts
275
+ if (hooks.length) {
276
+ let _fn = fn
277
+ fn = (e) => { for (let hook of hooks) if (hook(e) === false) return false; return _fn(e) }
283
278
  }
284
279
 
285
- if (evts.length == 1) el.addEventListener(evts[0], startFn, opts);
280
+ if (evts.length == 1) target.addEventListener(evts[0], fn, listenerOpts);
286
281
  else {
287
- const nextEvt = (fn, cur=0) => {
282
+ const nextEvt = (handler, cur=0) => {
288
283
  let curListener = e => {
289
- el.removeEventListener(evts[cur], curListener)
290
- if (typeof (fn = fn.call(el,e)) !== 'function') fn = ()=>{}
291
- if (++cur < evts.length) nextEvt(fn, cur);
292
- else if (!startFn[_stop]) nextEvt(startFn); // update only if chain isn't stopped
284
+ target.removeEventListener(evts[cur], curListener)
285
+ if (typeof (handler = handler.call(target,e)) !== 'function') handler = ()=>{}
286
+ if (++cur < evts.length) nextEvt(handler, cur);
287
+ else if (!fn[_stop]) nextEvt(fn); // update only if chain isn't stopped
293
288
  }
294
- el.addEventListener(evts[cur], curListener, opts)
289
+ target.addEventListener(evts[cur], curListener, listenerOpts)
295
290
  }
296
- nextEvt(startFn)
291
+ nextEvt(fn)
297
292
  }
298
293
  }
299
294
  const removeListener = (el, evt, fn) => {
@@ -303,70 +298,66 @@ const removeListener = (el, evt, fn) => {
303
298
 
304
299
  // event modifiers
305
300
  const mods = {
306
- throttle(el, cb, opts, limit) {
301
+ throttle(opts, limit) {
302
+ let {fn} = opts
307
303
  limit = Number(limit) || 108
308
304
  let pause, planned, block = () => {
309
305
  pause = true
310
306
  setTimeout(() => {
311
307
  pause = false
312
308
  // if event happened during blocked time, it schedules call by the end
313
- if (planned) cb(e), planned = false, block();
309
+ if (planned) return (planned = false, block(), fn(e))
314
310
  })
315
311
  }
316
- return [el, e => {
312
+ opts.fn = e => {
317
313
  if (pause) return (planned = true)
318
- cb(e); block();
319
- }]
314
+ block();
315
+ return fn(e);
316
+ }
320
317
  },
321
318
 
322
- debounce(el, cb, opts, wait) {
319
+ debounce(opts, wait) {
320
+ let {fn} = opts
323
321
  wait = Number(wait) || 108;
324
- let timeout, later = () => { timeout = null; cb(e) }
325
- return [el, (e) => {
322
+ let timeout
323
+ opts.fn = (e) => {
326
324
  clearTimeout(timeout)
327
- timeout = setTimeout(later, wait)
328
- }]
325
+ timeout = setTimeout(() => {timeout = null; fn(e)}, wait)
326
+ }
329
327
  },
330
328
 
331
- window(el, cb) { return [window, cb] },
332
- document(el, cb) { return [document, cb] },
333
- outside(el, cb) {
334
- return [el, cb, (e) => {
335
- if (el.contains(e.target)) return false
329
+ window(opts) { opts.target = window },
330
+ document(opts) { opts.target = document },
331
+ outside({target, hooks}) {
332
+ hooks.push((e) => {
333
+ if (target.contains(e.target)) return false
336
334
  if (e.target.isConnected === false) return false
337
- if (el.offsetWidth < 1 && el.offsetHeight < 1) return false
338
- }]
335
+ if (target.offsetWidth < 1 && target.offsetHeight < 1) return false
336
+ })
339
337
  },
340
- prevent(el, cb) { return [el, cb, e => { e.preventDefault(); } ]},
341
- stop(el, cb) { return [el, cb, e => { e.stopPropagation(); } ]},
342
- self(el, cb) { return [el, cb, e => { return e.target === el } ]},
343
- once(el, cb, opts) { opts.once = true; return [el, cb] },
344
- passive(el, cb, opts) { opts.passive = true; return [el, cb] },
345
- capture(el, cb, opts) { opts.capture = true; return [el, cb] },
338
+ prevent({hooks}) { hooks.push(e => { e.preventDefault(); })},
339
+ stop({hooks}) { hooks.push(e => { e.stopPropagation(); })},
340
+ self({target, hooks}) { hooks.push(e => { return e.target === target })},
341
+ once(opts) { opts.once = true; },
342
+ passive(opts) { opts.passive = true; },
343
+ capture(opts) { opts.capture = true; },
344
+
345
+ // keyboard
346
+ ctrl({hooks}) { hooks.push(e => e.key === 'Control' || e.key === 'Ctrl')},
347
+ shift({hooks}) { hooks.push(e => e.key === 'Shift')},
348
+ alt({hooks}) { hooks.push(e => e.key === 'Alt')},
349
+ meta({hooks}) { hooks.push(e => e.key === 'Meta')},
350
+ arrow({hooks}) { hooks.push(e => e.key.startsWith('Arrow'))},
351
+ enter({hooks}) { hooks.push(e => e.key === 'Enter')},
352
+ escape({hooks}) { hooks.push(e => e.key.startsWith('Esc'))},
353
+ tab({hooks}) { hooks.push(e => e.key === 'Tab')},
354
+ space({hooks}) { hooks.push(e => e.key === 'Space' || e.key === ' ')},
355
+ backspace({hooks}) { hooks.push(e => e.key === 'Backspace') },
356
+ delete({hooks}) { hooks.push(e => e.key === 'Delete') },
357
+ digit({hooks}) { hooks.push(e => /\d/.test(e.key)) },
358
+ letter({hooks}) { hooks.push(e => /[a-zA-Z]/.test(e.key)) },
359
+ character({hooks}) { hooks.push(e => /^\S$/.test(e.key) ) },
346
360
  };
347
- let keys = {
348
- ctrl:'Control Ctrl',
349
- shift:'Shift',
350
- alt:'Alt',
351
- meta:'Meta',cmd:'Meta',
352
- down:'ArrowDown',up:'ArrowUp',left:'ArrowLeft',right:'ArrowRight',
353
- end:'End',home:'Home',pagedown:'PageDown',pageup:'PageUp',
354
- enter:'Enter',
355
- esc:'Escape',escape:'Escape',tab:'Tab',
356
- backspace:'Backspace', delete:'Delete',
357
- space:/ /,
358
- plus:/\+/,minus:/\-/,star:/\*/,slash:/\//,period:/\./,equal:/\=/,underscore:/\_/,
359
- alnum: /\w/,
360
- letter: /[a-zA-Z]/,
361
- digit: /\d/
362
- }
363
- keys.arrow = keys.down + keys.up + keys.left + keys.right
364
- for (let keyAttr in keys) {
365
- let keyName = keys[keyAttr]
366
- mods[keyAttr] = (el, cb, opts, extraKey) => [el, cb, e => {
367
- if (!e.key || (e.key.length > 2 ? !keyName.includes?.(e.key) : !keyName.test?.(e.key))) return false
368
- }]
369
- }
370
361
 
371
362
  // set attr
372
363
  const attr = (el, name, v) => {
package/test/test.js CHANGED
@@ -632,6 +632,15 @@ test('on: keys with prevent', e => {
632
632
  is(state.log,['x'])
633
633
  })
634
634
 
635
+ test('on: debounce', async e => {
636
+ let el = h`<x :onkeydown.debounce-1="e=>log.push(e.key)"></x>`
637
+ let state = sprae(el, {log:[]})
638
+ el.dispatchEvent(new window.KeyboardEvent('keydown', { key: 'x', bubbles: true }));
639
+ is(state.log, [])
640
+ await time(2)
641
+ is(state.log, ['x'])
642
+ })
643
+
635
644
  test('with: inline', () => {
636
645
  let el = h`<x :with="{foo:'bar'}"><y :text="foo + baz"></y></x>`
637
646
  let state = sprae(el, {baz: 'qux'})