@peter.naydenov/cuts 1.4.3 → 1.4.5
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/Changelog.md +11 -0
- package/dist/cuts.cjs +1 -1
- package/dist/cuts.esm.mjs +1 -1
- package/dist/cuts.umd.js +1 -1
- package/package.json +12 -26
- package/src/findInstructions.js +9 -0
- package/src/main.js +8 -9
- package/src/methods/jump.js +8 -0
- package/src/methods/jumpBack.js +8 -0
- package/src/methods/jumpsReset.js +5 -0
- package/src/methods/setScenes.js +15 -15
- package/src/setInstruction.js +7 -0
- package/test-vitest/01-findPosition.test.js +108 -0
- package/tsconfig.json +4 -1
- package/types/findInstructions.d.ts +10 -1
- package/types/findInstructions.d.ts.map +1 -1
- package/types/main.d.ts +9 -9
- package/types/main.d.ts.map +1 -1
- package/types/methods/jump.d.ts +2 -2
- package/types/methods/jump.d.ts.map +1 -1
- package/types/methods/jumpBack.d.ts +2 -2
- package/types/methods/jumpBack.d.ts.map +1 -1
- package/types/methods/jumpsReset.d.ts.map +1 -1
- package/types/methods/setScenes.d.ts +45 -11
- package/types/methods/setScenes.d.ts.map +1 -1
- package/types/setInstruction.d.ts +1 -1
- package/types/setInstruction.d.ts.map +1 -1
- package/vitest.config.js +12 -0
package/Changelog.md
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
# Release History
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
## 1.4.5 ( 2025-10-20 )
|
|
5
|
+
- [x] Dependency update. @peter.naydenov/shortcuts - v.3.5.1
|
|
6
|
+
- [x] Cuts has event '@app-error'. Shortcut errors will trigger this event;
|
|
7
|
+
- [x] Type improvements;
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## 1.4.4 ( 2025-10-10)
|
|
12
|
+
- [x] Dependency update. @peter.naydenov/shortcuts - v.3.3.1 ( fixing problem with 'key:specialCharacters' events);
|
|
13
|
+
|
|
14
|
+
|
|
4
15
|
|
|
5
16
|
## 1.4.3 ( 2025-09-30)
|
|
6
17
|
- [x] Dependency update. ask-for-promise@3.0.2;
|
package/dist/cuts.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("@peter.naydenov/shortcuts"),n=require("ask-for-promise"),
|
|
1
|
+
"use strict";var e=require("@peter.naydenov/shortcuts"),n=require("ask-for-promise"),r=require("@peter.naydenov/log");function*t(e,n,r,t){let s=null;if(null===e)return yield*t.map(e=>[e,"show"]),void(yield[r,"show"]);if(!t||0===t.length)return n?(yield[e,"hide"],n.reverse(),yield*n.map(e=>[e,"hide"]),void(yield[r,"show"])):(yield[e,"hide"],void(yield[r,"show"]));if(t.includes(e)){for(let n=t.indexOf(e)+1;n<t.length;n++)yield[t[n],"show"];return void(yield[r,"show"])}if(!n)return yield[e,"hide"],void(yield*t.map(e=>[e,"show"]));if(t.every((e,r)=>n[r]===t[r]&&(s=r,!0)),null==s)return yield[e,"hide"],n.reverse(),yield*n.map(e=>[e,"hide"]),yield*t.map(e=>[e,"show"]),void(yield[r,"show"]);yield[e,"hide"];for(let e=n.length;e>s+1;e--)yield[n[e-1],"hide"];for(let e=s+1;e<t.length;e++)yield[t[e],"show"];yield[r,"show"]}function s(e){return function(n,...r){const{askForPromise:t,deps:s}=e;return function(){const e=t();return n({task:e,dependencies:s()},...r),e.promise}}}module.exports=function(o={logLevel:0}){const c=e.shortcuts({errorEventName:"@app-error"}),i=o.logLevel||0,u=r({level:i}),d={currentScene:null,currentParents:null,sceneNames:new Set,scenes:{},opened:!1,jumpStack:[]},l={},a={shortcutMngr:c,findInstructions:t,askForPromise:n,setInstruction:s({askForPromise:n,deps:c.getDependencies}),log:u};return l.hide=function(e,n){return function(r=1){const{askForPromise:t,shortcutMngr:s,setInstruction:o}=e,{currentScene:c,currentParents:i,scenes:u}=n,d=t(),l=[],a=i.length>0;let p;return s.pause(),l.push(o(u[c].hide)),a||(n.currentScene=null),a&&"*"===r&&[...i].reverse().forEach(e=>{p=n.currentParents.pop(),n.currentScene=p,l.push(o(u[e].hide))}),a&&!["*",1].includes(r)&&i.slice("-"+(r-1)).reverse().map(e=>{p=n.currentParents.pop(),n.currentScene=p,l.push(o(u[e].hide))}),(r=t.sequence(l)).onComplete(()=>{s.changeContext(n.currentScene),d.done()}),d.promise}}(a,d),l.listShortcuts=function(e,n){return function(e){const{scenes:r,sceneNames:t}=n;if(!t.has(e))return null;const{show:s,hide:o,parents:c,beforeUnload:i,afterLoad:u,...d}=r[e];return Object.keys(d)}}(0,d),l.setScenes=function(e,n){return function(r){const{shortcutMngr:t}=e;r.forEach(e=>{if(null==e)return;const{name:r,scene:s}=e;if(null==s)return void console.warn(`Scene ${r} is not defined`);s.parents||(s.parents=[]),n.scenes[r]=s,n.sceneNames.add(r);const{show:o,hide:c,parents:i,...u}=s,d={};d[r]=u,t.load(d)})}}(a,d),l.show=function(e,n){return function({scene:r,options:t={ssr:!1}},...s){const{shortcutMngr:o,askForPromise:c,log:i,findInstructions:u,setInstruction:d}=e,l=c(),a=c(),{opened:p,scenes:h,sceneNames:f,currentPage:m}=n;if(m){const e=h[m].beforeHide;"function"==typeof e&&e({done:a.done,dependencies:o.getDependencies()})}else a.done(!0);return a.onComplete(e=>{if(!e)return l.done(),l.promise;if(!f.has(r)&&i)return i({message:`Scene ${r} is not available.`,level:1,type:"error"}),l.done(),l.promise;if(!p&&t.ssr)return n.opened=!0,n.currentPage=r,o.changeContext(r),l.done(),l.promise;const{show:a,parents:m=[]}=h[r];if("*"===m[0])return a().then(()=>l.done()),n.currentParents.push(n.currentPage),n.currentPage=r,l.promise;function g([e,r]){if("show"===r)n.currentScene&&n.currentParents.push(n.currentScene),n.currentScene=e;else{let e=n.currentParents.pop();n.currentScene=e}return h[e][r]}m.forEach(e=>f.has(e)),n.currentParents||(n.currentParents=[]);const y=u(n.currentScene,n.currentParents,r,m),S=[];for(let e of y)[e].map(g).map(e=>S.push(d(e),...s));c.sequence(S).onComplete(()=>{n.opened=!0,o.changeContext(r),"function"==typeof h[r].afterShow&&h[r].afterShow({dependencies:o.getDependencies(),done:()=>{}}),l.done()})}),l.promise}}(a,d),l.jump=function(e,n){return function({scene:r},...t){return e.jumpStack.push(e.currentScene),n({scene:r},...t)}}(d,l.show),l.jumpBack=function(e,n){return function({hops:r}={hops:1},...t){for(let n=0;n<r-1;n++)e.jumpStack.pop();if(0===e.jumpStack.length)return;const s=e.jumpStack.pop();return n({scene:s},...t)}}(d,l.show),l.jumpsReset=function(e){return function(){e.jumpStack=[]}}(d),l.loadPlugins=async function(e){const n=e.map(e=>`plugin${e}`),r=await import("@peter.naydenov/shortcuts");return n.map(e=>r[e])},l.setDependencies=e=>c.setDependencies(e),l.getDependencies=()=>c.getDependencies(),l.setNote=n=>e.shortcuts.setNote(n),l.listScenes=()=>[...d.sceneNames],l.enablePlugin=(e,n)=>c.enablePlugin(e,n),l.disablePlugin=e=>c.disablePlugin(e),l.emit=(e,...n)=>c.emit(e,...n),l};
|
package/dist/cuts.esm.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{shortcuts as e}from"@peter.naydenov/shortcuts";import n from"ask-for-promise";import
|
|
1
|
+
import{shortcuts as e}from"@peter.naydenov/shortcuts";import n from"ask-for-promise";import r from"@peter.naydenov/log";function*t(e,n,r,t){let s=null;if(null===e)return yield*t.map(e=>[e,"show"]),void(yield[r,"show"]);if(!t||0===t.length)return n?(yield[e,"hide"],n.reverse(),yield*n.map(e=>[e,"hide"]),void(yield[r,"show"])):(yield[e,"hide"],void(yield[r,"show"]));if(t.includes(e)){for(let n=t.indexOf(e)+1;n<t.length;n++)yield[t[n],"show"];return void(yield[r,"show"])}if(!n)return yield[e,"hide"],void(yield*t.map(e=>[e,"show"]));if(t.every((e,r)=>n[r]===t[r]&&(s=r,!0)),null==s)return yield[e,"hide"],n.reverse(),yield*n.map(e=>[e,"hide"]),yield*t.map(e=>[e,"show"]),void(yield[r,"show"]);yield[e,"hide"];for(let e=n.length;e>s+1;e--)yield[n[e-1],"hide"];for(let e=s+1;e<t.length;e++)yield[t[e],"show"];yield[r,"show"]}function s(e){return function(n,...r){const{askForPromise:t,deps:s}=e;return function(){const e=t();return n({task:e,dependencies:s()},...r),e.promise}}}function o(o={logLevel:0}){const c=e({errorEventName:"@app-error"}),i=o.logLevel||0,u=r({level:i}),d={currentScene:null,currentParents:null,sceneNames:new Set,scenes:{},opened:!1,jumpStack:[]},l={},a={shortcutMngr:c,findInstructions:t,askForPromise:n,setInstruction:s({askForPromise:n,deps:c.getDependencies}),log:u};return l.hide=function(e,n){return function(r=1){const{askForPromise:t,shortcutMngr:s,setInstruction:o}=e,{currentScene:c,currentParents:i,scenes:u}=n,d=t(),l=[],a=i.length>0;let p;return s.pause(),l.push(o(u[c].hide)),a||(n.currentScene=null),a&&"*"===r&&[...i].reverse().forEach(e=>{p=n.currentParents.pop(),n.currentScene=p,l.push(o(u[e].hide))}),a&&!["*",1].includes(r)&&i.slice("-"+(r-1)).reverse().map(e=>{p=n.currentParents.pop(),n.currentScene=p,l.push(o(u[e].hide))}),(r=t.sequence(l)).onComplete(()=>{s.changeContext(n.currentScene),d.done()}),d.promise}}(a,d),l.listShortcuts=function(e,n){return function(e){const{scenes:r,sceneNames:t}=n;if(!t.has(e))return null;const{show:s,hide:o,parents:c,beforeUnload:i,afterLoad:u,...d}=r[e];return Object.keys(d)}}(0,d),l.setScenes=function(e,n){return function(r){const{shortcutMngr:t}=e;r.forEach(e=>{if(null==e)return;const{name:r,scene:s}=e;if(null==s)return void console.warn(`Scene ${r} is not defined`);s.parents||(s.parents=[]),n.scenes[r]=s,n.sceneNames.add(r);const{show:o,hide:c,parents:i,...u}=s,d={};d[r]=u,t.load(d)})}}(a,d),l.show=function(e,n){return function({scene:r,options:t={ssr:!1}},...s){const{shortcutMngr:o,askForPromise:c,log:i,findInstructions:u,setInstruction:d}=e,l=c(),a=c(),{opened:p,scenes:h,sceneNames:f,currentPage:m}=n;if(m){const e=h[m].beforeHide;"function"==typeof e&&e({done:a.done,dependencies:o.getDependencies()})}else a.done(!0);return a.onComplete(e=>{if(!e)return l.done(),l.promise;if(!f.has(r)&&i)return i({message:`Scene ${r} is not available.`,level:1,type:"error"}),l.done(),l.promise;if(!p&&t.ssr)return n.opened=!0,n.currentPage=r,o.changeContext(r),l.done(),l.promise;const{show:a,parents:m=[]}=h[r];if("*"===m[0])return a().then(()=>l.done()),n.currentParents.push(n.currentPage),n.currentPage=r,l.promise;function g([e,r]){if("show"===r)n.currentScene&&n.currentParents.push(n.currentScene),n.currentScene=e;else{let e=n.currentParents.pop();n.currentScene=e}return h[e][r]}m.forEach(e=>f.has(e)),n.currentParents||(n.currentParents=[]);const y=u(n.currentScene,n.currentParents,r,m),S=[];for(let e of y)[e].map(g).map(e=>S.push(d(e),...s));c.sequence(S).onComplete(()=>{n.opened=!0,o.changeContext(r),"function"==typeof h[r].afterShow&&h[r].afterShow({dependencies:o.getDependencies(),done:()=>{}}),l.done()})}),l.promise}}(a,d),l.jump=function(e,n){return function({scene:r},...t){return e.jumpStack.push(e.currentScene),n({scene:r},...t)}}(d,l.show),l.jumpBack=function(e,n){return function({hops:r}={hops:1},...t){for(let n=0;n<r-1;n++)e.jumpStack.pop();if(0===e.jumpStack.length)return;const s=e.jumpStack.pop();return n({scene:s},...t)}}(d,l.show),l.jumpsReset=function(e){return function(){e.jumpStack=[]}}(d),l.loadPlugins=async function(e){const n=e.map(e=>`plugin${e}`),r=await import("@peter.naydenov/shortcuts");return n.map(e=>r[e])},l.setDependencies=e=>c.setDependencies(e),l.getDependencies=()=>c.getDependencies(),l.setNote=n=>e.setNote(n),l.listScenes=()=>[...d.sceneNames],l.enablePlugin=(e,n)=>c.enablePlugin(e,n),l.disablePlugin=e=>c.disablePlugin(e),l.emit=(e,...n)=>c.emit(e,...n),l}export{o as default};
|
package/dist/cuts.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).cuts=t()}(this,function(){"use strict";var e={_normalizeWithPlugins:function(e,t){return function(e){const n=t.shortcuts;Object.keys(n).forEach(t=>{Object.entries(n[t]).forEach(([o,r])=>{const s=e(o);s!==o&&(delete n[t][o],n[t][s]=r)})})}},_readShortcutWithPlugins:function(e,t){return function(n){const{inAPI:o}=e,r=n.split(":")[0],s=o._systemAction(r,"none");let i=n;return-1!==s&&(i=t.plugins[s].shortcutName(n)),i}},_systemAction:function(e,t){return function(e,n,o=null){return t.plugins.findIndex(t=>t.getPrefix()===e&&(t[n]&&t[n](o),!0))}},changeContext:function(e,t){const{shortcuts:n,currentContext:o}=t,{ev:r}=e;return function(e=!1){const s=o.name;if(!e)return r.reset(),void(o.name=null);s!==e&&(n[e]?(n[s]&&r.reset(),o.name=e,t.plugins.forEach(t=>t.contextChange(e)),Object.entries(n[e]).forEach(([e,t])=>{t.forEach(t=>r.on(e,t))}),r.on("*",(...e)=>{t.exposeShortcut&&t.exposeShortcut(...e)})):r.emit("@shortcuts-error",`Context '${e}' does not exist`))}},listShortcuts:function(e,t){const n=t.shortcuts;return function(e=null){if(null!=e){let t=n[e];return null==t?null:Object.entries(t).map(([e,t])=>e)}return Object.keys(n).map(e=>{let t={};return t.context=e,t.shortcuts=Object.entries(n[e]).map(([e,t])=>e),t})}},load:function(e,t){const{shortcuts:n,plugins:o}=t,{API:{changeContext:r,getContext:s}}=e;return function(e){const t=s(),i=o.map(e=>e.getPrefix().toUpperCase());let c=!1;Object.entries(e).forEach(([e,r])=>{e===t&&(c=!0),n[e]={},Object.entries(r).forEach(([t,r])=>{let s=t,c=t.toUpperCase().trim(),u=i.map((e,t)=>c.startsWith(e)?t:null).filter(e=>null!==e);if(u.length){let e=u[0];s=o[e].shortcutName(t)}r instanceof Function&&(r=[r]),n[e][s]=r})}),c&&(r(),r(t))}},unload:function(e,t){const{currentContext:n,shortcuts:o}=t,{ev:r}=e;return function(e){n.name!==e?o[e]?delete o[e]:r.emit("shortcuts-error",`Context '${e}' does not exist`):r.emit("shortcuts-error",`Context '${e}' can't be removed during is current active context. Change the context first`)}}};function t(e){const t=e.toUpperCase(),n=/KEY\s*\:/i.test(t),o=t.indexOf(":");return n?`KEY:${t.slice(o+1).split(",").map(e=>e.trim()).map(e=>e.split("+").map(e=>e.trim()).sort().join("+")).join(",")}`:e}function n(e,t){let{shiftKey:n,altKey:o,ctrlKey:r}=e,s=e.code.replace("Key","").replace("Digit",""),i=[];return r&&i.push("CTRL"),n&&i.push("SHIFT"),o&&i.push("ALT"),t.hasOwnProperty(s)?i.push(t[s].toUpperCase()):["ControlLeft","ControlRight","ShiftLeft","ShiftRight","AltLeft","AltRight","Meta"].includes(s)||i.push(s.toUpperCase()),i.sort()}function o(e,t){let n=0;const{regex:o}=e,{listenOptions:r,currentContext:{name:s},shortcuts:i}=t;return null==s?0:(Object.entries(i[s]).forEach(([e,t])=>{if(!o.test(e))return;n++;let s=e.slice(4).split(",").length;r.maxSequence<s&&(r.maxSequence=s)}),n)}function r(){return{ArrowLeft:"LEFT",ArrowUp:"UP",ArrowRight:"RIGHT",ArrowDown:"DOWN",Enter:"ENTER",NumpadEnter:"ENTER",Escape:"ESC",Backspace:"BACKSPACE",Space:"SPACE",Tab:"TAB",Backquote:"`",BracketLeft:"[",BracketRight:"]",Equal:"=",Slash:"/",Backslash:"\\",IntlBackslash:"`",F1:"F1",F2:"F2",F3:"F3",F4:"F4",F5:"F5",F6:"F6",F7:"F7",F8:"F8",F9:"F9",F10:"F10",F11:"F11",F12:"F12"}}function s(e,t,n){const{listenOptions:{clickTarget:o}}=t;let r=n;return r===document||r===document.body?null:r.dataset[o]||"A"===r.nodeName?r:s(e,t,r.parentNode)}function i(e){const t=e.toUpperCase(),n=/CLICK\s*\:/i.test(t),o=["LEFT","MIDDLE","RIGHT"],r=["ALT","SHIFT","CTRL"];let s=null,i=[],c=0,u=t.indexOf(":");return n?(t.slice(u+1).trim().split("-").map(e=>e.trim()).forEach(e=>{o.includes(e)?s=e:r.includes(e)?i.push(e):isNaN(e)||(c=e)}),`CLICK:${s}-${c}${i.length>0?"-":""}${i.sort().join("-")}`):e}function c(e,t){let{shiftKey:n,altKey:o,ctrlKey:r,key:s,button:i}=e,c=`CLICK:${["LEFT","MIDDLE","RIGHT"][i]}-${t}`,u=[];return r&&u.push("CTRL"),n&&u.push("SHIFT"),o&&u.push("ALT"),u.length>0?`${c}${u.length>0?"-":""}${u.sort().join("-")}`:`${c}`}function u(e,t){let n=0;const{regex:o}=e,{listenOptions:r,currentContext:{name:s},shortcuts:i}=t;return null==s?0:(Object.entries(i[s]).forEach(([e,t])=>{if(!o.test(e))return;n++;let[,s]=e.slice(6).split("-");r.maxClicks<s&&(r.maxClicks=s)}),n)}function l(e){const t=e.toUpperCase(),n=/FORM\s*\:/i.test(t),o=t.indexOf(":");return n?`FORM:${t.slice(o+1).trim()}`:e}function a(e,t){const{regex:n,_defaults:o,ev:r}=e,{currentContext:{name:s},shortcuts:i,callbacks:c}=t;let u=[],l=[],a=[];if(null==s)return!1;if(Object.entries(i[s]).forEach(([e,t])=>{n.test(e)&&("FORM:WATCH"===e&&(u=t),"FORM:DEFINE"===e&&(l=t),"FORM:ACTION"===e&&(a=t))}),0===a.length)return!1;let f=new Set;0===l.length&&(l=[o.define]),0===u.length&&(u=[o.watch]);let p=u.map(e=>e()).reduce((e,t)=>(e.push(t),e),[]);return t.watchList=document.querySelectorAll(p),t.watchList.forEach(e=>f.add(l[0](e))),t.typeFn=l[0]?l[0]:o.define,a.forEach(e=>e instanceof Function?e()instanceof Array?void e().forEach(({fn:e,type:n,timing:o,wait:s=0})=>{if(f.has(n)&&e instanceof Function){let t=`${n}/${o}`;const s=c.hasOwnProperty(t);s?c[t].push(e):c[t]=[e],s||r.on(t,(e,t)=>{t.forEach(t=>{t instanceof Function&&t(e)})})}"instant"===o&&(t.wait[`${n}`]=s)}):(console.warn("Warning: The 'form:action' function should RETURN an array."),!1):(console.warn("Warning: The 'form:action' should be a function."),!1)),Object.keys(t.callbacks).length>0}const f={watch:()=>"input, select, textarea, button, a",define:e=>"checkbox"===e.type||"radio"===e.type?"checkbox":"button"==e.type||"submit"==e.type?"button":"input"};function p(t={}){const n=function(){let e={"*":[]},t={},n=new Set,o=!1,r="";return{on:function(t,n){e[t]||(e[t]=[]),e[t].push(n)},once:function(e,n){"*"!==e&&(t[e]||(t[e]=[]),t[e].push(n))},off:function(n,o){if(o)return e[n]&&(e[n]=e[n].filter(e=>e!==o)),t[n]&&(t[n]=t[n].filter(e=>e!==o)),e[n]&&0===e[n].length&&delete e[n],void(t[n]&&0===t[n].length&&delete e[n]);t[n]&&delete t[n],e[n]&&delete e[n]},reset:function(){e={"*":[]},t={},n=new Set},emit:function(){const[s,...i]=arguments;function c(t){let o=!1;"*"!==t&&(n.has(t)||(e[t].every(e=>{const t=e(...i);return"string"!=typeof t||"STOP"!==t.toUpperCase()||(o=!0,!1)}),o||e["*"].forEach(e=>e(s,...i))))}if(o&&(console.log(`${r} Event "${s}" was triggered.`),i.length>0&&(console.log("Arguments:"),console.log(...i),console.log("^----"))),"*"!==s){if(t[s]){if(n.has(s))return;t[s].forEach(e=>e(...i)),delete t[s]}e[s]&&c(s)}else Object.keys(e).forEach(e=>c(e))},stop:function(o){if("*"===o){const o=Object.keys(e),r=Object.keys(t);return void(n=new Set([...r,...o]))}n.add(o)},start:function(e){"*"!==e?n.delete(e):n.clear()},debug:function(e,t){o=!!e,t&&"string"==typeof t&&(r=t)}}}(),o={},r={},s={currentContext:{name:null,note:null},shortcuts:{},plugins:[],exposeShortcut:!(!t.onShortcut||"function"!=typeof t.onShortcut)&&t.onShortcut},i={ev:n,inAPI:o,API:r,extra:{}};return r.enablePlugin=(e,t={})=>{const n=e.name;if(-1===o._systemAction(n,"none")){let n;n=e(i,s,t),s.plugins.push(n)}},r.disablePlugin=e=>{const t=o._systemAction(e,"destroy");-1!==t&&(s.plugins=s.plugins.filter((e,n)=>n!==t))},r.mutePlugin=e=>o._systemAction(e,"mute"),r.unmutePlugin=e=>o._systemAction(e,"unmute"),r.getContext=()=>s.currentContext.name,r.getNote=()=>s.currentContext.note,r.setNote=(e=null)=>{"string"!=typeof e&&null!=e||(s.currentContext.note=e)},r.pause=(e="*")=>{let t=o._readShortcutWithPlugins(e);n.stop(t)},r.resume=(e="*")=>{const t=o._readShortcutWithPlugins(e);n.start(t)},r.emit=(e,...t)=>n.emit(o._readShortcutWithPlugins(e),...t),r.listContexts=()=>Object.keys(s.shortcuts),r.setDependencies=e=>i.extra={...i.extra,...e},r.getDependencies=()=>i.extra,Object.entries(e).forEach(([e,t])=>{e.startsWith("_")?o[e]=t(i,s):r[e]=t(i,s)}),r}var m=Object.freeze({__proto__:null,pluginClick:function(e,t,n){let{currentContext:o,shortcuts:r}=t,{inAPI:l}=e,a={ev:e.ev,_findTarget:s,_readClickEvent:c,mainDependencies:e,regex:/CLICK\s*\:/i},f={currentContext:o,shortcuts:r,listenOptions:{mouseWait:n.mouseWait?n.mouseWait:320,maxClicks:1,clickTarget:n.clickTarget?n.clickTarget:"click"}};l._normalizeWithPlugins(i);let p=function(e,t){const{ev:n,_findTarget:o,_readClickEvent:r,mainDependencies:s}=e,{listenOptions:i,currentContext:c}=t,{mouseWait:u}=i;let l=null,a=null,f=null,p=null,m=0;function d(){const e=r(a,m),t={target:l,targetProps:l?l.getBoundingClientRect():null,x:a.clientX,y:a.clientY,context:c.name,note:c.note,event:a,dependencies:s.extra,type:"click"};n.emit(e,t),f=null,p=null,l=null,a=null,m=0}function h(n){let r=i.maxClicks;return clearTimeout(f),p?(clearTimeout(p),void(p=setTimeout(()=>p=null,u))):(l=o(e,t,n.target),l&&l.dataset.hasOwnProperty("quickClick")&&(r=1),l&&"A"===l.tagName&&(r=1),a=n,m++,m>=r?(d(),void(r>1&&(p=setTimeout(()=>p=null,u)))):void(f=setTimeout(d,u)))}function g(n){let r=i.maxClicks;return clearTimeout(f),p?(clearTimeout(p),void(p=setTimeout(()=>p=null,u))):(l=o(e,t,n.target),l&&l.dataset.hasOwnProperty("quickClick")&&(r=1),l&&"A"===l.tagName&&(r=1),a=n,m++,m>=r?(d(),void(r>1&&(p=setTimeout(()=>p=null,u)))):void(f=setTimeout(d,u)))}return{start:function(){t.active||(window.addEventListener("contextmenu",g),document.addEventListener("click",h),t.active=!0)},stop:function(){t.active&&(window.removeEventListener("contextmenu",g),document.removeEventListener("click",h),t.active=!1)}}}(a,f),m=u(a,f);m>0&&p.start();let d={getPrefix:()=>"click",shortcutName:e=>i(e),contextChange:()=>{m=u(a,f),m<1&&p.stop(),m>0&&p.start()},mute:()=>p.stop(),unmute:()=>p.start(),destroy:()=>{p.stop(),f=null,d=null}};return Object.freeze(d),d},pluginForm:function(e,t,n){let{currentContext:o,shortcuts:r}=t,{inAPI:s}=e,i={ev:e.ev,mainDependencies:e,regex:/FORM\s*\:/i,_defaults:f},c={currentContext:o,shortcuts:r,callbacks:{},typeFn:"",watchList:[],wait:{}};s._normalizeWithPlugins(l);let u=function(e,t){const{ev:n}=e;let o=null;function r(e,t,n,o){return{target:n.target,context:t.currentContext.name,note:t.currentContext.note,event:n,dependencies:e.mainDependencies.extra,type:o}}function s(o){const{callbacks:s,typeFn:i}=t,c=i(o.target),u=r(e,t,o,c),l=`${c}/in`;null!=s[l]&&n.emit(l,u,s[l])}function i(o){const{callbacks:s,typeFn:i}=t,c=r(e,t,o,"form-out"),u=`${i(o.target)}/out`;null!=s[u]&&n.emit(u,c,s[u])}function c(s){const{callbacks:i,typeFn:c}=t,u=r(e,t,s,"form-instant"),l=c(s.target),a=t.wait[`${l}`],f=`${l}/instant`;null!=i[f]&&(0!==a?(clearTimeout(o),o=setTimeout(()=>n.emit(f,u,i[f]),a)):n.emit(f,u,i[f]))}return{start:function(){t.active||(document.addEventListener("focusin",s),document.addEventListener("focusout",i),document.addEventListener("input",c),t.active=!0)},stop:function(){t.active&&(document.removeEventListener("focusin",s),document.removeEventListener("focusout",i),document.removeEventListener("input",c),t.active=!1)}}}(i,c);o.name&&a(i,c)&&u.start();let p={getPrefix:()=>"form",shortcutName:e=>l(e),contextChange:()=>{c.callbacks={},c.typeFn="",c.watchList=[],c.wait={},a(i,c)?u.start():u.stop()},mute:()=>u.stop(),unmute:()=>u.start(),destroy:()=>{u.stop(),c=null,p=null}};return Object.freeze(p),p},pluginKey:function(e,s,i={}){let{currentContext:c,shortcuts:u,exposeShortcut:l}=s,{inAPI:a}=e,f={ev:e.ev,_specialChars:r,_readKeyEvent:n,mainDependencies:e,regex:/KEY\s*\:/i},p={currentContext:c,shortcuts:u,active:!1,listenOptions:{keyWait:i.keyWait?i.keyWait:480,maxSequence:1,keyIgnore:null},streamKeys:!(!i.streamKeys||"function"!=typeof i.streamKeys)&&i.streamKeys};a._normalizeWithPlugins(t);let m=function(e,t){const{ev:n,_specialChars:o,_readKeyEvent:r,mainDependencies:s}=e,{currentContext:i,streamKeys:c,listenOptions:u}=t,{keyWait:l}=u;let a=[],f=null,p=!0,m=!1;const d=()=>p=!1,h=()=>p=!0,g=()=>m=!0,y=()=>!1===p;function v(){let e=a.map(e=>[e.join("+")]);const t={wait:d,end:h,ignore:g,isWaiting:y,note:i.note,context:i.name,dependencies:s.extra,type:"key"};if(!p){let o=e.at(-1);n.emit(o,t),m&&(e=e.slice(0,-1),m=!1)}if(p){const o=`KEY:${e.join(",")}`;n.emit(o,t),a=[],f=null}}function x(t){if(clearTimeout(f),o.hasOwnProperty(t.code))return a.push(r(t,o)),c&&c({key:t.key,context:i.name,note:i.note,dependencies:e.extra}),u.keyIgnore?(clearTimeout(u.keyIgnore),void(u.keyIgnore=setTimeout(()=>u.keyIgnore=null,l))):p&&a.length===u.maxSequence?(v(),void(u.keyIgnore=setTimeout(()=>u.keyIgnore=null,l))):void(p?f=setTimeout(v,l):v())}function k(t){if(!o.hasOwnProperty(t.code)){if(clearTimeout(f),c&&c({key:t.key,context:i.name,note:i.note,dependencies:e.extra}),u.keyIgnore)return clearTimeout(u.keyIgnore),void(u.keyIgnore=setTimeout(()=>u.keyIgnore=null,l));if(a.push(r(t,o)),p&&a.length===u.maxSequence)return v(),void(u.keyIgnore=setTimeout(()=>u.keyIgnore=null,l));p?f=setTimeout(v,l):v()}}return{start:function(){t.active||(document.addEventListener("keydown",x),document.addEventListener("keypress",k),t.active=!0)},stop:function(){t.active&&(document.removeEventListener("keydown",x),document.removeEventListener("keypress",k),t.active=!1)}}}(f,p),d=o(f,p);d>0&&m.start();let h={getPrefix:()=>"key",shortcutName:e=>t(e),contextChange:e=>{d=o(f,p),d<1&&m.stop(),d>0&&m.start()},mute:()=>m.stop(),unmute:()=>m.start(),destroy:()=>{m.stop(),p=null,h=null}};return Object.freeze(h),h},shortcuts:p});function d(e){return e?function(e){let t=e.map(e=>h()),n=t.map(e=>e.promise);t.promises=t;let o=g(Promise.all(n));function r(e){let t="pending";return e.then(()=>t="fulfilled").catch(()=>t="rejected"),t}function s(n,...o){t.forEach((t,s)=>n({value:e[s],done:t.done,cancel:t.cancel,timeout:t.timeout,state:r(t.promise)},...o))}const i={promise:Promise.all(n),promises:t,done:e=>{t.forEach(t=>t.done(e))},cancel:e=>{t.forEach(t=>t.cancel(e))},each:s,onComplete:o,timeout:()=>{}};return i.timeout=y(!0,i),i}(e):h()}function h(){let e,t;const n=new Promise((n,o)=>{e=n,t=o}),o={promise:n,promises:null,done:e,cancel:t,each:()=>{},onComplete:g(n),timeout:()=>{}};return o.timeout=y(!1,o),o.each=(n,...r)=>{n({value:null,done:e,cancel:t,timeout:o.timeout},...r)},o}function g(e){return function(t,n=null){null===n?e.then(e=>t(e)):e.then(e=>t(e),e=>n(e))}}function y(e,t){let n;return n=e?Promise.all(t.promises.map(e=>e.promise)):t.promise,function(e,o){let r,s=new Promise((t,s)=>{r=setTimeout(()=>{t(o),Promise.resolve(n)},e)});return n.then(()=>clearTimeout(r)),t.onComplete=g(Promise.race([n,s])),t}}function v({message:e,level:t,type:n,logLevel:o}){if(0===o)return null;if(o<t)return null;const r=`[Debug]: ${e}`;switch(n){case"warn":console.warn(r);break;case"error":console.error(r);break;default:console.log(r)}return r}function x(e={},t=v){return function({message:n,type:o,level:r,...s}){const{level:i,type:c,defaultMessageLevel:u}=Object.assign({},{level:1e3,type:"log",defaultMessageLevel:1},e);return o||(o=c),r||(r=u),t({message:n,type:o,level:r,logLevel:i,...s})}}function*k(e,t,n,o){let r=null;if(null===e)return yield*o.map(e=>[e,"show"]),void(yield[n,"show"]);if(!o||0===o.length)return t?(yield[e,"hide"],t.reverse(),yield*t.map(e=>[e,"hide"]),void(yield[n,"show"])):(yield[e,"hide"],void(yield[n,"show"]));if(o.includes(e)){for(let t=o.indexOf(e)+1;t<o.length;t++)yield[o[t],"show"];return void(yield[n,"show"])}if(!t)return yield[e,"hide"],void(yield*o.map(e=>[e,"show"]));if(o.every((e,n)=>t[n]===o[n]&&(r=n,!0)),null==r)return yield[e,"hide"],t.reverse(),yield*t.map(e=>[e,"hide"]),yield*o.map(e=>[e,"show"]),void(yield[n,"show"]);yield[e,"hide"];for(let e=t.length;e>r+1;e--)yield[t[e-1],"hide"];for(let e=r+1;e<o.length;e++)yield[o[e],"show"];yield[n,"show"]}function C(e){return function(t,...n){const{askForPromise:o,deps:r}=e;return function(){const e=o();return t({task:e,dependencies:r()},...n),e.promise}}}return d.sequence=function(e,...t){const n=d(),o=[];const r=function*(e){for(const t of e)yield t}(e);return function e(t,...s){t.done?n.done(o):t.value(...s).then(t=>{o.push(t),e(r.next(),...s,t)})}(r.next(),...t),n},d.all=function(e,...t){const n=d(),o=[],r=e.map((e,n)=>"function"==typeof e?e(...t).then(e=>o[n]=e):e.then(e=>o[n]=e));return Promise.all(r).then(()=>n.done(o)),n},function(e={logLevel:0}){const t=p(),n=x({level:e.logLevel||0}),o={currentScene:null,currentParents:null,sceneNames:new Set,scenes:{},opened:!1,jumpStack:[]},r={},s={shortcutMngr:t,findInstructions:k,askForPromise:d,setInstruction:C({askForPromise:d,deps:t.getDependencies}),log:n};return r.hide=function(e,t){return function(n=1){const{askForPromise:o,shortcutMngr:r,setInstruction:s}=e,{currentScene:i,currentParents:c,scenes:u}=t,l=o(),a=[],f=c.length>0;let p;return r.pause(),a.push(s(u[i].hide)),f||(t.currentScene=null),f&&"*"===n&&[...c].reverse().forEach(e=>{p=t.currentParents.pop(),t.currentScene=p,a.push(s(u[e].hide))}),f&&!["*",1].includes(n)&&c.slice("-"+(n-1)).reverse().map(e=>{p=t.currentParents.pop(),t.currentScene=p,a.push(s(u[e].hide))}),(n=o.sequence(a)).onComplete(()=>{r.changeContext(t.currentScene),l.done()}),l.promise}}(s,o),r.listShortcuts=function(e,t){return function(e){const{scenes:n,sceneNames:o}=t;if(!o.has(e))return null;const{show:r,hide:s,parents:i,beforeUnload:c,afterLoad:u,...l}=n[e];return Object.keys(l)}}(0,o),r.setScenes=function(e,t){return function(n){const{shortcutMngr:o}=e;n.forEach(e=>{if(null==e)return;const{name:n,scene:r}=e;if(null==r)return void console.warn(`Scene ${n} is not defined`);r.parents||(r.parents=[]),t.scenes[n]=r,t.sceneNames.add(n);const{show:s,hide:i,parents:c,...u}=r,l={};l[n]=u,o.load(l)})}}(s,o),r.show=function(e,t){return function({scene:n,options:o={ssr:!1}},...r){const{shortcutMngr:s,askForPromise:i,log:c,findInstructions:u,setInstruction:l}=e,a=i(),f=i(),{opened:p,scenes:m,sceneNames:d,currentPage:h}=t;if(h){const e=m[h].beforeHide;"function"==typeof e&&e({done:f.done,dependencies:s.getDependencies()})}else f.done(!0);return f.onComplete(e=>{if(!e)return a.done(),a.promise;if(!d.has(n)&&c)return c({message:`Scene ${n} is not available.`,level:1,type:"error"}),a.done(),a.promise;if(!p&&o.ssr)return t.opened=!0,t.currentPage=n,s.changeContext(n),a.done(),a.promise;const{show:f,parents:h=[]}=m[n];if("*"===h[0])return f().then(()=>a.done()),t.currentParents.push(t.currentPage),t.currentPage=n,a.promise;function g([e,n]){if("show"===n)t.currentScene&&t.currentParents.push(t.currentScene),t.currentScene=e;else{let e=t.currentParents.pop();t.currentScene=e}return m[e][n]}h.forEach(e=>d.has(e)),t.currentParents||(t.currentParents=[]);const y=u(t.currentScene,t.currentParents,n,h),v=[];for(let e of y)[e].map(g).map(e=>v.push(l(e),...r));i.sequence(v).onComplete(()=>{t.opened=!0,s.changeContext(n),"function"==typeof m[n].afterShow&&m[n].afterShow({dependencies:s.getDependencies(),done:()=>{}}),a.done()})}),a.promise}}(s,o),r.jump=function(e,t){return function({scene:n},...o){return e.jumpStack.push(e.currentScene),t({scene:n},...o)}}(o,r.show),r.jumpBack=function(e,t){return function({hops:n}={hops:1},...o){for(let t=0;t<n-1;t++)e.jumpStack.pop();if(0===e.jumpStack.length)return;const r=e.jumpStack.pop();return t({scene:r},...o)}}(o,r.show),r.jumpsReset=function(e){return function(){e.jumpStack=[]}}(o),r.loadPlugins=async function(e){const t=e.map(e=>`plugin${e}`),n=await Promise.resolve().then(function(){return m});return t.map(e=>n[e])},r.setDependencies=e=>t.setDependencies(e),r.getDependencies=()=>t.getDependencies(),r.setNote=e=>p.setNote(e),r.listScenes=()=>[...o.sceneNames],r.enablePlugin=(e,n)=>t.enablePlugin(e,n),r.disablePlugin=e=>t.disablePlugin(e),r.emit=(e,...n)=>t.emit(e,...n),r}});
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).cuts=t()}(this,function(){"use strict";var e={_normalizeWithPlugins:function(e,t){return function(e){const n=t.shortcuts;Object.keys(n).forEach(t=>{Object.entries(n[t]).forEach(([o,r])=>{const s=e(o);s!==o&&(delete n[t][o],n[t][s]=r)})})}},_readShortcutWithPlugins:function(e,t){return function(n){const{inAPI:o}=e,r=n.split(":")[0].toLowerCase().trim(),s=o._systemAction(r,"none");let i=n;return-1!==s&&(i=t.plugins[s].shortcutName(n)),i}},_systemAction:function(e,t){return function(e,n,o=null){return t.plugins.findIndex(t=>t.getPrefix()===e&&(t[n]&&t[n](o),!0))}},changeContext:function(e,t){const{shortcuts:n,currentContext:o,ERROR_EVENT_NAME:r}=t,{ev:s}=e;return function(e=!1){const i=o.name;if(!e)return s.reset(),void(o.name=null);i!==e&&(n[e]?(n[i]&&s.reset(),o.name=e,t.plugins.forEach(t=>t.contextChange(e)),Object.entries(n[e]).forEach(([e,t])=>{t.forEach(t=>s.on(e,t))}),s.on("*",(...e)=>{t.exposeShortcut&&t.exposeShortcut(...e)})):s.emit(r,`Context '${e}' does not exist`))}},listShortcuts:function(e,t){const n=t.shortcuts;return function(e=null){if(null!=e){let t=n[e];return null==t?null:Object.entries(t).map(([e,t])=>e)}return Object.keys(n).map(e=>{let t={};return t.context=e,t.shortcuts=Object.entries(n[e]).map(([e,t])=>e),t})}},load:function(e,t){const{shortcuts:n,plugins:o}=t,{API:{changeContext:r,getContext:s}}=e;return function(e){const t=s(),i=o.map(e=>e.getPrefix().toUpperCase());let c=!1;Object.entries(e).forEach(([e,r])=>{e===t&&(c=!0),n[e]={},Object.entries(r).forEach(([t,r])=>{let s=t,c=t.toUpperCase().trim(),u=i.map((e,t)=>c.startsWith(e)?t:null).filter(e=>null!==e);if(u.length){let e=u[0];s=o[e].shortcutName(t)}r instanceof Function&&(r=[r]),n[e][s]=r})}),c&&(r(),r(t))}},unload:function(e,t){const{currentContext:n,shortcuts:o,ERROR_EVENT_NAME:r}=t,{ev:s}=e;return function(e){n.name!==e?o[e]?delete o[e]:s.emit(r,`Context '${e}' does not exist`):s.emit(r,`Context '${e}' can't be removed during is current active context. Change the context first`)}}};function t(e){const t=e.toUpperCase(),n=/KEY\s*\:/i.test(t),o=t.indexOf(":");return n?`KEY:${t.slice(o+1).split(",").map(e=>e.trim()).map(e=>e.split("+").map(e=>e.trim()).sort().join("+")).join(",")}`:e}function n(e,t){let{shiftKey:n,altKey:o,ctrlKey:r}=e,s=t(),i=e.code.replace("Key","").replace("Digit",""),c=[];return r&&c.push("CTRL"),n&&c.push("SHIFT"),o&&c.push("ALT"),s.hasOwnProperty(i)?c.push(s[i].toUpperCase()):["ControlLeft","ControlRight","ShiftLeft","ShiftRight","AltLeft","AltRight","Meta"].includes(i)||c.push(i.toUpperCase()),c.sort()}function o(e,t){let n=0;const{regex:o}=e,{listenOptions:r,currentContext:{name:s},shortcuts:i}=t;return null==s?0:(Object.entries(i[s]).forEach(([e,t])=>{if(!o.test(e))return;n++;let s=e.slice(4).split(",").length;r.maxSequence<s&&(r.maxSequence=s)}),n)}function r(){return{ArrowLeft:"LEFT",ArrowUp:"UP",ArrowRight:"RIGHT",ArrowDown:"DOWN",Enter:"ENTER",NumpadEnter:"ENTER",Escape:"ESC",Backspace:"BACKSPACE",Space:"SPACE",Tab:"TAB",Backquote:"`",BracketLeft:"[",BracketRight:"]",Equal:"=",Slash:"/",Backslash:"\\",IntlBackslash:"`",F1:"F1",F2:"F2",F3:"F3",F4:"F4",F5:"F5",F6:"F6",F7:"F7",F8:"F8",F9:"F9",F10:"F10",F11:"F11",F12:"F12"}}function s(e,t,n){const{listenOptions:{clickTarget:o}}=t;let r=n;return r===document||r===document.body?null:r.dataset[o]||"A"===r.nodeName?r:s(e,t,r.parentNode)}function i(e){const t=e.toUpperCase(),n=/CLICK\s*\:/i.test(t),o=["LEFT","MIDDLE","RIGHT"],r=["ALT","SHIFT","CTRL"];let s=null,i=[],c=0,u=t.indexOf(":");return n?(t.slice(u+1).trim().split("-").map(e=>e.trim()).forEach(e=>{o.includes(e)?s=e:r.includes(e)?i.push(e):isNaN(e)||(c=e)}),`CLICK:${s}-${c}${i.length>0?"-":""}${i.sort().join("-")}`):e}function c(e,t){let{shiftKey:n,altKey:o,ctrlKey:r,key:s,button:i}=e,c=`CLICK:${["LEFT","MIDDLE","RIGHT"][i]}-${t}`,u=[];return r&&u.push("CTRL"),n&&u.push("SHIFT"),o&&u.push("ALT"),u.length>0?`${c}${u.length>0?"-":""}${u.sort().join("-")}`:`${c}`}function u(e,t){let n=0;const{regex:o}=e,{listenOptions:r,currentContext:{name:s},shortcuts:i}=t;return null==s?0:(Object.entries(i[s]).forEach(([e,t])=>{if(!o.test(e))return;n++;let[,s]=e.slice(6).split("-");r.maxClicks<s&&(r.maxClicks=s)}),n)}function l(e){const t=e.toUpperCase(),n=/FORM\s*\:/i.test(t),o=t.indexOf(":");return n?`FORM:${t.slice(o+1).trim()}`:e}function a(e,t){const{regex:n,_defaults:o,ev:r}=e,{currentContext:{name:s},shortcuts:i,callbacks:c}=t;let u=[],l=[],a=[];if(null==s)return!1;if(Object.entries(i[s]).forEach(([e,t])=>{n.test(e)&&("FORM:WATCH"===e&&(u=t),"FORM:DEFINE"===e&&(l=t),"FORM:ACTION"===e&&(a=t))}),0===a.length)return!1;let f=new Set;0===l.length&&(l=[o.define]),0===u.length&&(u=[o.watch]);let p=u.map(e=>e()).reduce((e,t)=>(e.push(t),e),[]);return t.watchList=document.querySelectorAll(p),t.watchList.forEach(e=>f.add(l[0](e))),t.typeFn=l[0]?l[0]:o.define,a.forEach(e=>e instanceof Function?e()instanceof Array?void e().forEach(({fn:e,type:n,timing:o,wait:s=0})=>{if(f.has(n)&&e instanceof Function){let t=`${n}/${o}`;const s=c.hasOwnProperty(t);s?c[t].push(e):c[t]=[e],s||r.on(t,(e,t)=>{t.forEach(t=>{t instanceof Function&&t(e)})})}"instant"===o&&(t.wait[`${n}`]=s)}):(console.warn("Warning: The 'form:action' function should RETURN an array."),!1):(console.warn("Warning: The 'form:action' should be a function."),!1)),Object.keys(t.callbacks).length>0}const f={watch:()=>"input, select, textarea, button, a",define:e=>"checkbox"===e.type||"radio"===e.type?"checkbox":"button"==e.type||"submit"==e.type?"button":"input"};function p(t={}){let n={},o={};const r=function(){let e={"*":[]},t={},n=new Set,o=!1,r="";return{on:function(t,n){e[t]||(e[t]=[]),e[t].push(n)},once:function(e,n){"*"!==e&&(t[e]||(t[e]=[]),t[e].push(n))},off:function(n,o){if(o)return e[n]&&(e[n]=e[n].filter(e=>e!==o)),t[n]&&(t[n]=t[n].filter(e=>e!==o)),e[n]&&0===e[n].length&&delete e[n],void(t[n]&&0===t[n].length&&delete e[n]);t[n]&&delete t[n],e[n]&&delete e[n]},reset:function(){e={"*":[]},t={},n=new Set},emit:function(){const[s,...i]=arguments;function c(t){let o=!1;"*"!==t&&(n.has(t)||(e[t].every(e=>{const t=e(...i);return"string"!=typeof t||"STOP"!==t.toUpperCase()||(o=!0,!1)}),o||e["*"].forEach(e=>e(s,...i))))}if(o&&(console.log(`${r} Event "${s}" was triggered.`),i.length>0&&(console.log("Arguments:"),console.log(...i),console.log("^----"))),"*"!==s){if(t[s]){if(n.has(s))return;t[s].forEach(e=>e(...i)),delete t[s]}e[s]&&c(s)}else Object.keys(e).forEach(e=>c(e))},stop:function(o){if("*"===o){const o=Object.keys(e),r=Object.keys(t);return void(n=new Set([...r,...o]))}n.add(o)},start:function(e){"*"!==e?n.delete(e):n.clear()},debug:function(e,t){o=!!e,t&&"string"==typeof t&&(r=t)}}}(),s={currentContext:{name:null,note:null},shortcuts:{},plugins:[],exposeShortcut:!(!t.onShortcut||"function"!=typeof t.onShortcut)&&t.onShortcut,ERROR_EVENT_NAME:t.errorEventName?t.errorEventName:"@shortcuts-error"};let i={ev:r,inAPI:n,API:o,extra:{}};return o.enablePlugin=(e,t={})=>{if("function"!=typeof e)return;let o=e(i,s,t);const r=o.getPrefix();-1===n._systemAction(r,"none")?s.plugins.push(o):o.destroy()},o.disablePlugin=e=>{const t=n._systemAction(e,"destroy");-1!==t&&(s.plugins=s.plugins.filter((e,n)=>n!==t))},o.mutePlugin=e=>n._systemAction(e,"mute"),o.unmutePlugin=e=>n._systemAction(e,"unmute"),o.listPlugins=()=>s.plugins.map(e=>e.getPrefix()),o.getContext=()=>s.currentContext.name,o.getNote=()=>s.currentContext.note,o.setNote=(e=null)=>{"string"!=typeof e&&null!=e||(s.currentContext.note=e)},o.pause=(e="*")=>{let t=n._readShortcutWithPlugins(e);r.stop(t)},o.resume=(e="*")=>{const t=n._readShortcutWithPlugins(e);r.start(t)},o.emit=(e,...t)=>r.emit(n._readShortcutWithPlugins(e),...t),o.listContexts=()=>Object.keys(s.shortcuts),o.setDependencies=e=>i.extra={...i.extra,...e},o.getDependencies=()=>i.extra,o.reset=function(){r.reset(),o.changeContext(),s.plugins.forEach(e=>e.destroy()),o.listContexts().map(e=>o.unload(e)),i.extra={},s.exposeShortcut=null},Object.entries(e).forEach(([e,t])=>{e.startsWith("_")?n[e]=t(i,s):o[e]=t(i,s)}),o}var m=Object.freeze({__proto__:null,pluginClick:function(e,t,n){let{currentContext:o,shortcuts:r}=t,{inAPI:l}=e,a={ev:e.ev,_findTarget:s,_readClickEvent:c,mainDependencies:e,regex:/CLICK\s*\:/i},f={currentContext:o,active:!1,shortcuts:r,listenOptions:{mouseWait:n.mouseWait?n.mouseWait:320,maxClicks:1,clickTarget:n.clickTarget?n.clickTarget:"click"}};l._normalizeWithPlugins(i);let p=function(e,t){const{ev:n,_findTarget:o,_readClickEvent:r,mainDependencies:s}=e,{listenOptions:i,currentContext:c}=t,{mouseWait:u}=i;let l=null,a=null,f=null,p=null,m=0;function d(){const e=r(a,m),t={target:l,targetProps:l?l.getBoundingClientRect():null,x:a.clientX,y:a.clientY,context:c.name,note:c.note,event:a,dependencies:s.extra,type:"click"};n.emit(e,t),f=null,p=null,l=null,a=null,m=0}function h(n){let r=i.maxClicks;return clearTimeout(f),p?(clearTimeout(p),void(p=setTimeout(()=>p=null,u))):(l=o(e,t,n.target),l&&l.dataset.hasOwnProperty("quickClick")&&(r=1),l&&"A"===l.tagName&&(r=1),a=n,m++,m>=r?(d(),void(r>1&&(p=setTimeout(()=>p=null,u)))):void(f=setTimeout(d,u)))}function g(n){let r=i.maxClicks;return clearTimeout(f),p?(clearTimeout(p),void(p=setTimeout(()=>p=null,u))):(l=o(e,t,n.target),l&&l.dataset.hasOwnProperty("quickClick")&&(r=1),l&&"A"===l.tagName&&(r=1),a=n,m++,m>=r?(d(),void(r>1&&(p=setTimeout(()=>p=null,u)))):void(f=setTimeout(d,u)))}return{start:function(){t.active||(window.addEventListener("contextmenu",g),document.addEventListener("click",h),t.active=!0)},stop:function(){t.active&&(window.removeEventListener("contextmenu",g),document.removeEventListener("click",h),t.active=!1,f&&(clearTimeout(f),f=null),p&&(clearTimeout(p),p=null),m=0)}}}(a,f),m=u(a,f);m>0&&p.start();let d={getPrefix:()=>"click",shortcutName:e=>i(e),contextChange:()=>{m=u(a,f),m<1&&p.stop(),m>0&&p.start()},mute:()=>{p.stop()},unmute:()=>p.start(),destroy:()=>p.stop()};return Object.freeze(d),d},pluginForm:function(e,t,n){let{currentContext:o,shortcuts:r}=t,{inAPI:s}=e,i={ev:e.ev,mainDependencies:e,regex:/FORM\s*\:/i,_defaults:f},c={currentContext:o,shortcuts:r,callbacks:{},typeFn:"",watchList:[],wait:{}};s._normalizeWithPlugins(l);let u=function(e,t){const{ev:n}=e;let o=null;function r(e,t,n,o){return{target:n.target,context:t.currentContext.name,note:t.currentContext.note,event:n,dependencies:e.mainDependencies.extra,type:o}}function s(o){const{callbacks:s,typeFn:i}=t,c=i(o.target),u=r(e,t,o,c),l=`${c}/in`;null!=s[l]&&n.emit(l,u,s[l])}function i(o){const{callbacks:s,typeFn:i}=t,c=r(e,t,o,"form-out"),u=`${i(o.target)}/out`;null!=s[u]&&n.emit(u,c,s[u])}function c(s){const{callbacks:i,typeFn:c}=t,u=r(e,t,s,"form-instant"),l=c(s.target),a=t.wait[`${l}`],f=`${l}/instant`;null!=i[f]&&(0!==a?(clearTimeout(o),o=setTimeout(()=>n.emit(f,u,i[f]),a)):n.emit(f,u,i[f]))}return{start:function(){t.active||(document.addEventListener("focusin",s),document.addEventListener("focusout",i),document.addEventListener("input",c),t.active=!0)},stop:function(){t.active&&(document.removeEventListener("focusin",s),document.removeEventListener("focusout",i),document.removeEventListener("input",c),t.active=!1)}}}(i,c);o.name&&a(i,c)&&u.start();let p={getPrefix:()=>"form",shortcutName:e=>l(e),contextChange:()=>{c.callbacks={},c.typeFn="",c.watchList=[],c.wait={},a(i,c)?u.start():u.stop()},mute:()=>u.stop(),unmute:()=>u.start(),destroy:()=>{u.stop()}};return Object.freeze(p),p},pluginKey:function(e,s,i={}){let{currentContext:c,shortcuts:u,exposeShortcut:l}=s,{inAPI:a}=e,f={ev:e.ev,_specialChars:r,_readKeyEvent:n,mainDependencies:e,regex:/KEY\s*\:/i},p={currentContext:c,shortcuts:u,active:!1,listenOptions:{keyWait:i.keyWait?i.keyWait:480,maxSequence:1,keyIgnore:null},streamKeys:!(!i.streamKeys||"function"!=typeof i.streamKeys)&&i.streamKeys};a._normalizeWithPlugins(t);let m=function(e,t){const{ev:n,_specialChars:o,_readKeyEvent:r,mainDependencies:s}=e,{currentContext:i,streamKeys:c,listenOptions:u}=t,{keyWait:l}=u;let a=[],f=null,p=!0,m=!1;const d=()=>p=!1,h=()=>p=!0,g=()=>m=!0,y=()=>!1===p;function v(){let e=a.map(e=>[e.join("+")]);const t={wait:d,end:h,ignore:g,isWaiting:y,note:i.note,context:i.name,dependencies:s.extra,type:"key"};if(!p){let o=e.at(-1);n.emit(o,t),m&&(e=e.slice(0,-1),m=!1)}if(p){const o=`KEY:${e.join(",")}`;n.emit(o,t),a=[],f=null}}function x(t){if(clearTimeout(f),o().hasOwnProperty(t.code))return a.push(r(t,o)),c&&c({key:t.key,context:i.name,note:i.note,dependencies:e.extra}),u.keyIgnore?(clearTimeout(u.keyIgnore),void(u.keyIgnore=setTimeout(()=>u.keyIgnore=null,l))):p&&a.length===u.maxSequence?(v(),void(u.keyIgnore=setTimeout(()=>u.keyIgnore=null,l))):void(p?f=setTimeout(v,l):v())}function k(t){if(!o().hasOwnProperty(t.code)){if(clearTimeout(f),c&&c({key:t.key,context:i.name,note:i.note,dependencies:e.extra}),u.keyIgnore)return clearTimeout(u.keyIgnore),void(u.keyIgnore=setTimeout(()=>u.keyIgnore=null,l));if(a.push(r(t,o)),p&&a.length===u.maxSequence)return v(),void(u.keyIgnore=setTimeout(()=>u.keyIgnore=null,l));p?f=setTimeout(v,l):v()}}return{start:function(){t.active||(document.addEventListener("keydown",x),document.addEventListener("keypress",k),t.active=!0)},stop:function(){t.active&&(document.removeEventListener("keydown",x),document.removeEventListener("keypress",k),t.active=!1,f&&(clearTimeout(f),f=null),u.keyIgnore&&(clearTimeout(u.keyIgnore),u.keyIgnore=null),a=[],p=!0,m=!1)}}}(f,p),d=o(f,p);d>0&&m.start();let h={getPrefix:()=>"key",shortcutName:e=>t(e),contextChange:e=>{d=o(f,p),d<1&&m.stop(),d>0&&m.start()},mute:()=>m.stop(),unmute:()=>m.start(),destroy:()=>m.stop()};return Object.freeze(h),h},shortcuts:p});function d(e){return e?function(e){let t=e.map(e=>h()),n=t.map(e=>e.promise);t.promises=t;let o=g(Promise.all(n));function r(e){let t="pending";return e.then(()=>t="fulfilled").catch(()=>t="rejected"),t}function s(n,...o){t.forEach((t,s)=>n({value:e[s],done:t.done,cancel:t.cancel,timeout:t.timeout,state:r(t.promise)},...o))}const i={promise:Promise.all(n),promises:t,done:e=>{t.forEach(t=>t.done(e))},cancel:e=>{t.forEach(t=>t.cancel(e))},each:s,onComplete:o,timeout:()=>{}};return i.timeout=y(!0,i),i}(e):h()}function h(){let e,t;const n=new Promise((n,o)=>{e=n,t=o}),o={promise:n,promises:null,done:e,cancel:t,each:()=>{},onComplete:g(n),timeout:()=>{}};return o.timeout=y(!1,o),o.each=(n,...r)=>{n({value:null,done:e,cancel:t,timeout:o.timeout},...r)},o}function g(e){return function(t,n=null){null===n?e.then(e=>t(e)):e.then(e=>t(e),e=>n(e))}}function y(e,t){let n;return n=e?Promise.all(t.promises.map(e=>e.promise)):t.promise,function(e,o){let r,s=new Promise((t,s)=>{r=setTimeout(()=>{t(o),Promise.resolve(n)},e)});return n.then(()=>clearTimeout(r)),t.onComplete=g(Promise.race([n,s])),t}}function v({message:e,level:t,type:n,logLevel:o}){if(0===o)return null;if(o<t)return null;const r=`[Debug]: ${e}`;switch(n){case"warn":console.warn(r);break;case"error":console.error(r);break;default:console.log(r)}return r}function x(e={},t=v){return function({message:n,type:o,level:r,...s}){const{level:i,type:c,defaultMessageLevel:u}=Object.assign({},{level:1e3,type:"log",defaultMessageLevel:1},e);return o||(o=c),r||(r=u),t({message:n,type:o,level:r,logLevel:i,...s})}}function*k(e,t,n,o){let r=null;if(null===e)return yield*o.map(e=>[e,"show"]),void(yield[n,"show"]);if(!o||0===o.length)return t?(yield[e,"hide"],t.reverse(),yield*t.map(e=>[e,"hide"]),void(yield[n,"show"])):(yield[e,"hide"],void(yield[n,"show"]));if(o.includes(e)){for(let t=o.indexOf(e)+1;t<o.length;t++)yield[o[t],"show"];return void(yield[n,"show"])}if(!t)return yield[e,"hide"],void(yield*o.map(e=>[e,"show"]));if(o.every((e,n)=>t[n]===o[n]&&(r=n,!0)),null==r)return yield[e,"hide"],t.reverse(),yield*t.map(e=>[e,"hide"]),yield*o.map(e=>[e,"show"]),void(yield[n,"show"]);yield[e,"hide"];for(let e=t.length;e>r+1;e--)yield[t[e-1],"hide"];for(let e=r+1;e<o.length;e++)yield[o[e],"show"];yield[n,"show"]}function C(e){return function(t,...n){const{askForPromise:o,deps:r}=e;return function(){const e=o();return t({task:e,dependencies:r()},...n),e.promise}}}return d.sequence=function(e,...t){const n=d(),o=[];const r=function*(e){for(const t of e)yield t}(e);return function e(t,...s){t.done?n.done(o):t.value(...s).then(t=>{o.push(t),e(r.next(),...s,t)})}(r.next(),...t),n},d.all=function(e,...t){const n=d(),o=[],r=e.map((e,n)=>"function"==typeof e?e(...t).then(e=>o[n]=e):e.then(e=>o[n]=e));return Promise.all(r).then(()=>n.done(o)),n},function(e={logLevel:0}){const t=p({errorEventName:"@app-error"}),n=x({level:e.logLevel||0}),o={currentScene:null,currentParents:null,sceneNames:new Set,scenes:{},opened:!1,jumpStack:[]},r={},s={shortcutMngr:t,findInstructions:k,askForPromise:d,setInstruction:C({askForPromise:d,deps:t.getDependencies}),log:n};return r.hide=function(e,t){return function(n=1){const{askForPromise:o,shortcutMngr:r,setInstruction:s}=e,{currentScene:i,currentParents:c,scenes:u}=t,l=o(),a=[],f=c.length>0;let p;return r.pause(),a.push(s(u[i].hide)),f||(t.currentScene=null),f&&"*"===n&&[...c].reverse().forEach(e=>{p=t.currentParents.pop(),t.currentScene=p,a.push(s(u[e].hide))}),f&&!["*",1].includes(n)&&c.slice("-"+(n-1)).reverse().map(e=>{p=t.currentParents.pop(),t.currentScene=p,a.push(s(u[e].hide))}),(n=o.sequence(a)).onComplete(()=>{r.changeContext(t.currentScene),l.done()}),l.promise}}(s,o),r.listShortcuts=function(e,t){return function(e){const{scenes:n,sceneNames:o}=t;if(!o.has(e))return null;const{show:r,hide:s,parents:i,beforeUnload:c,afterLoad:u,...l}=n[e];return Object.keys(l)}}(0,o),r.setScenes=function(e,t){return function(n){const{shortcutMngr:o}=e;n.forEach(e=>{if(null==e)return;const{name:n,scene:r}=e;if(null==r)return void console.warn(`Scene ${n} is not defined`);r.parents||(r.parents=[]),t.scenes[n]=r,t.sceneNames.add(n);const{show:s,hide:i,parents:c,...u}=r,l={};l[n]=u,o.load(l)})}}(s,o),r.show=function(e,t){return function({scene:n,options:o={ssr:!1}},...r){const{shortcutMngr:s,askForPromise:i,log:c,findInstructions:u,setInstruction:l}=e,a=i(),f=i(),{opened:p,scenes:m,sceneNames:d,currentPage:h}=t;if(h){const e=m[h].beforeHide;"function"==typeof e&&e({done:f.done,dependencies:s.getDependencies()})}else f.done(!0);return f.onComplete(e=>{if(!e)return a.done(),a.promise;if(!d.has(n)&&c)return c({message:`Scene ${n} is not available.`,level:1,type:"error"}),a.done(),a.promise;if(!p&&o.ssr)return t.opened=!0,t.currentPage=n,s.changeContext(n),a.done(),a.promise;const{show:f,parents:h=[]}=m[n];if("*"===h[0])return f().then(()=>a.done()),t.currentParents.push(t.currentPage),t.currentPage=n,a.promise;function g([e,n]){if("show"===n)t.currentScene&&t.currentParents.push(t.currentScene),t.currentScene=e;else{let e=t.currentParents.pop();t.currentScene=e}return m[e][n]}h.forEach(e=>d.has(e)),t.currentParents||(t.currentParents=[]);const y=u(t.currentScene,t.currentParents,n,h),v=[];for(let e of y)[e].map(g).map(e=>v.push(l(e),...r));i.sequence(v).onComplete(()=>{t.opened=!0,s.changeContext(n),"function"==typeof m[n].afterShow&&m[n].afterShow({dependencies:s.getDependencies(),done:()=>{}}),a.done()})}),a.promise}}(s,o),r.jump=function(e,t){return function({scene:n},...o){return e.jumpStack.push(e.currentScene),t({scene:n},...o)}}(o,r.show),r.jumpBack=function(e,t){return function({hops:n}={hops:1},...o){for(let t=0;t<n-1;t++)e.jumpStack.pop();if(0===e.jumpStack.length)return;const r=e.jumpStack.pop();return t({scene:r},...o)}}(o,r.show),r.jumpsReset=function(e){return function(){e.jumpStack=[]}}(o),r.loadPlugins=async function(e){const t=e.map(e=>`plugin${e}`),n=await Promise.resolve().then(function(){return m});return t.map(e=>n[e])},r.setDependencies=e=>t.setDependencies(e),r.getDependencies=()=>t.getDependencies(),r.setNote=e=>p.setNote(e),r.listScenes=()=>[...o.sceneNames],r.enablePlugin=(e,n)=>t.enablePlugin(e,n),r.disablePlugin=e=>t.disablePlugin(e),r.emit=(e,...n)=>t.emit(e,...n),r}});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peter.naydenov/cuts",
|
|
3
3
|
"description": "SPA scene manager",
|
|
4
|
-
"version": "1.4.
|
|
4
|
+
"version": "1.4.5",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Peter Naydenov",
|
|
7
7
|
"main": "./src/main.js",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"build": "rollup -c",
|
|
23
23
|
"postbuild": "tsc -p tsconfig.json",
|
|
24
24
|
"preview": "vite preview",
|
|
25
|
-
"test": "
|
|
26
|
-
"cover": "
|
|
25
|
+
"test": "vitest",
|
|
26
|
+
"cover": "vitest run --coverage",
|
|
27
27
|
"cypress": "cypress open --component --browser chrome"
|
|
28
28
|
},
|
|
29
29
|
"repository": {
|
|
@@ -32,38 +32,24 @@
|
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@peter.naydenov/visual-controller-for-vue3": "^2.1.6",
|
|
35
|
-
"@rollup/plugin-commonjs": "^28.0.
|
|
36
|
-
"@rollup/plugin-node-resolve": "^16.0.
|
|
35
|
+
"@rollup/plugin-commonjs": "^28.0.8",
|
|
36
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
37
37
|
"@rollup/plugin-terser": "^0.4.4",
|
|
38
|
-
"@types/node": "^24.
|
|
38
|
+
"@types/node": "^24.9.0",
|
|
39
39
|
"@vitejs/plugin-vue": "^6.0.1",
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"mocha": "^11.7.4",
|
|
44
|
-
"rollup": "^4.52.3",
|
|
40
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
41
|
+
"cypress": "^15.5.0",
|
|
42
|
+
"rollup": "^4.52.5",
|
|
45
43
|
"typescript": "^5.9.3",
|
|
46
|
-
"vite": "^7.1.
|
|
44
|
+
"vite": "^7.1.11",
|
|
45
|
+
"vitest": "^3.2.4",
|
|
47
46
|
"vue": "^3.5.22"
|
|
48
47
|
},
|
|
49
48
|
"dependencies": {
|
|
50
49
|
"@peter.naydenov/log": "^1.1.1",
|
|
51
|
-
"@peter.naydenov/shortcuts": "^3.
|
|
50
|
+
"@peter.naydenov/shortcuts": "^3.5.1",
|
|
52
51
|
"ask-for-promise": "^3.0.2"
|
|
53
52
|
},
|
|
54
|
-
"c8": {
|
|
55
|
-
"include": [
|
|
56
|
-
"src/**/*.js"
|
|
57
|
-
],
|
|
58
|
-
"exclude": [
|
|
59
|
-
"node_modules",
|
|
60
|
-
"test"
|
|
61
|
-
],
|
|
62
|
-
"reporter": [
|
|
63
|
-
"lcov",
|
|
64
|
-
"text-summary"
|
|
65
|
-
]
|
|
66
|
-
},
|
|
67
53
|
"keywords": [
|
|
68
54
|
"script",
|
|
69
55
|
"visual",
|
package/src/findInstructions.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @function findInstructions
|
|
6
|
+
* @description Generator that yields instructions for transitioning between scenes
|
|
7
|
+
* @param {string|null} currentName - current scene name
|
|
8
|
+
* @param {Array.<string>|null} currentParents - current scene parents
|
|
9
|
+
* @param {string} targetName - target scene name
|
|
10
|
+
* @param {Array.<string>} targetParents - target scene parents
|
|
11
|
+
* @yields {Array.<string, 'show'|'hide'>} - instruction array with scene name and action
|
|
12
|
+
*/
|
|
4
13
|
function* findInstructions ( currentName, currentParents, targetName, targetParents ) {
|
|
5
14
|
let backToName, backIndex=null;
|
|
6
15
|
|
package/src/main.js
CHANGED
|
@@ -36,7 +36,7 @@ import jumpsReset from './methods/jumpsReset.js'
|
|
|
36
36
|
|
|
37
37
|
function main ( cfg= {logLevel:0} ) {
|
|
38
38
|
const
|
|
39
|
-
shortcutMngr = shortcuts ()
|
|
39
|
+
shortcutMngr = shortcuts ({ errorEventName: '@app-error' })
|
|
40
40
|
, logLevel = cfg.logLevel || 0
|
|
41
41
|
, log = createLog ({ level:logLevel })
|
|
42
42
|
, state = {
|
|
@@ -136,14 +136,13 @@ function main ( cfg= {logLevel:0} ) {
|
|
|
136
136
|
API.disablePlugin = ( pluginName ) => shortcutMngr.disablePlugin ( pluginName )
|
|
137
137
|
|
|
138
138
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
*/
|
|
139
|
+
/**
|
|
140
|
+
* @function emit
|
|
141
|
+
* @description Emit an event
|
|
142
|
+
* @param {string} event - event name
|
|
143
|
+
* @param {...*} args - Extra data to pass to the listeners
|
|
144
|
+
* @returns void
|
|
145
|
+
*/
|
|
147
146
|
API.emit = ( event, ...args ) => shortcutMngr.emit ( event, ...args )
|
|
148
147
|
return API
|
|
149
148
|
} // main func.
|
package/src/methods/jump.js
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
function jump ( state, show ) {
|
|
2
|
+
/**
|
|
3
|
+
* @function jump
|
|
4
|
+
* @description Jump to a scene, saving the current scene on the jump stack for later return
|
|
5
|
+
* @param {Object} request - request object
|
|
6
|
+
* @param {string} request.scene - target scene name
|
|
7
|
+
* @param {Array} [args] - arguments to pass to the scene method
|
|
8
|
+
* @returns {Promise} - promise that resolves when the jump is completed
|
|
9
|
+
*/
|
|
2
10
|
function jump ( { scene }, ...args ) {
|
|
3
11
|
state.jumpStack.push ( state.currentScene )
|
|
4
12
|
return show ( { scene }, ...args )
|
package/src/methods/jumpBack.js
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
function jumpBack ( state, show ) {
|
|
2
|
+
/**
|
|
3
|
+
* @function jumpBack
|
|
4
|
+
* @description Jump back to a previous scene from the jump stack
|
|
5
|
+
* @param {Object} [options] - options object
|
|
6
|
+
* @param {number} [options.hops=1] - number of hops to jump back
|
|
7
|
+
* @param {Array} [args] - arguments to pass to the scene method
|
|
8
|
+
* @returns {Promise|undefined} - promise that resolves when the jump back is completed, or undefined if no scenes in stack
|
|
9
|
+
*/
|
|
2
10
|
function jumpBack ({hops}={hops:1}, ...args ) {
|
|
3
11
|
for ( let i = 0; i < hops-1; i++ ) {
|
|
4
12
|
state.jumpStack.pop ()
|
package/src/methods/setScenes.js
CHANGED
|
@@ -3,32 +3,32 @@
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @typedef {Object} SceneObject
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
10
|
-
* @
|
|
11
|
-
* @
|
|
6
|
+
* @property {function} show - load interface and prerequisites for the scene;
|
|
7
|
+
* @property {function():boolean} [afterShow] - run after scene is loaded. Optional. Returns true to continue loading, false to cancel;
|
|
8
|
+
* @property {function():void} hide - Reverse the settings from 'show';
|
|
9
|
+
* @property {function():void} [beforeHide] - Run before scene is hidden. Optional;
|
|
10
|
+
* @property {Array.<string>} [parents] - list of parent scene names.
|
|
11
|
+
* @property {*} ... - shortcut descriptions
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
/**
|
|
16
|
+
/**
|
|
17
17
|
* @typedef {Object} sceneDescription
|
|
18
|
-
* @
|
|
19
|
-
* @
|
|
18
|
+
* @property {string} name - name of the scene
|
|
19
|
+
* @property {SceneObject} scene - object should contain 'show' and 'hide' + shortcuts and action functions
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
function setScenes ( dependencies, state ) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
/**
|
|
27
|
+
* @function setScenes
|
|
28
|
+
* @param {sceneDescription[]} list - list of objects with name and scene. Scene should be a Scene model.
|
|
29
|
+
* @returns void
|
|
30
|
+
* @description Screen Description is an object with fields 'name' and 'scene'
|
|
31
|
+
*/
|
|
32
32
|
return function setScenes ( list ) {
|
|
33
33
|
const { shortcutMngr } = dependencies;
|
|
34
34
|
list.forEach ( item => {
|
package/src/setInstruction.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
function setInstruction ( dependencies ) {
|
|
4
|
+
/**
|
|
5
|
+
* @function setInstruction
|
|
6
|
+
* @description Wrap a scene function (show/hide) to return a promise-based instruction
|
|
7
|
+
* @param {function} fn - the scene function to wrap
|
|
8
|
+
* @param {...*} args - arguments to pass to the scene function
|
|
9
|
+
* @returns {function} - a function that returns a promise when executed
|
|
10
|
+
*/
|
|
4
11
|
return function setInstruction ( fn, ...args ) {
|
|
5
12
|
const { askForPromise, deps } = dependencies;
|
|
6
13
|
return function applyInstruction () {
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import findInstructions from "../src/findInstructions.js"
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
describe ( 'Find a Position', () => {
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
it ( 'Switch top level pages', () => {
|
|
11
|
+
let result = []
|
|
12
|
+
let g = findInstructions ( 'old', undefined, 'new', undefined )
|
|
13
|
+
|
|
14
|
+
for (const n of g) { result.push(n) }
|
|
15
|
+
|
|
16
|
+
expect(result.length).toBe ( 2 )
|
|
17
|
+
expect(result[0][0]).toBe ( 'old' )
|
|
18
|
+
expect(result[0][1]).toBe ( 'hide' )
|
|
19
|
+
|
|
20
|
+
expect(result[1][0]).toBe ( 'new' )
|
|
21
|
+
expect(result[1][1]).toBe ( 'show' )
|
|
22
|
+
}) // it switch top level pages
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
it ( 'Switch from child page to top level page', () => {
|
|
27
|
+
let result = []
|
|
28
|
+
let g = findInstructions ( 'old', ['one', 'two'], 'new', undefined )
|
|
29
|
+
|
|
30
|
+
for (const n of g) { result.push(n) }
|
|
31
|
+
|
|
32
|
+
expect(result[0]).toEqual(['old', 'hide'])
|
|
33
|
+
expect(result[1]).toEqual(['two', 'hide'])
|
|
34
|
+
expect(result[2]).toEqual(['one', 'hide'])
|
|
35
|
+
expect(result[3]).toEqual(['new', 'show'])
|
|
36
|
+
}) // it switch from child page to top level page
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
it ( 'Switch to child page', () => {
|
|
41
|
+
let result = []
|
|
42
|
+
let g = findInstructions ( 'old', ['bla'], 'new', ['old', 'two', 'three'])
|
|
43
|
+
for (const n of g) { result.push(n) }
|
|
44
|
+
|
|
45
|
+
expect(result).toHaveLength(3)
|
|
46
|
+
expect(result[0]).toEqual(['two', 'show'])
|
|
47
|
+
expect(result[1]).toEqual(['three', 'show'])
|
|
48
|
+
expect(result[2]).toEqual(['new', 'show'])
|
|
49
|
+
}) // it switch to child page
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
it ( 'From child page to other siblings page', () => {
|
|
54
|
+
let result = []
|
|
55
|
+
let g = findInstructions('old', ['two', 'three'], 'new', ['two', 'three'])
|
|
56
|
+
for (const n of g) { result.push(n) }
|
|
57
|
+
|
|
58
|
+
expect(result).toHaveLength ( 2 )
|
|
59
|
+
expect(result[0]).toEqual(['old', 'hide'])
|
|
60
|
+
expect(result[1]).toEqual(['new', 'show'])
|
|
61
|
+
}) // it from top level page to not own child page
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
it ( 'From top level page to not own child page', () => {
|
|
66
|
+
let result = []
|
|
67
|
+
let g = findInstructions('old', undefined, 'new', ['two', 'three'])
|
|
68
|
+
for (const n of g) { result.push(n) }
|
|
69
|
+
|
|
70
|
+
expect(result).toHaveLength(3)
|
|
71
|
+
expect(result[0]).toEqual(['old', 'hide'])
|
|
72
|
+
expect(result[1]).toEqual(['two', 'show'])
|
|
73
|
+
expect(result[2]).toEqual(['three', 'show'])
|
|
74
|
+
}) // it from top level page to not own child page
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
it ( 'Long chain of parents', () => {
|
|
79
|
+
let result = []
|
|
80
|
+
let g = findInstructions('old', ['one', 'branch'], 'new', ['one', 'two', 'three'])
|
|
81
|
+
for (const n of g) { result.push(n) }
|
|
82
|
+
|
|
83
|
+
expect(result).toHaveLength(5)
|
|
84
|
+
expect(result[0]).toEqual(['old', 'hide'])
|
|
85
|
+
expect(result[1]).toEqual(['branch', 'hide'])
|
|
86
|
+
expect(result[2]).toEqual(['two', 'show'])
|
|
87
|
+
expect(result[3]).toEqual(['three', 'show'])
|
|
88
|
+
expect(result[4]).toEqual(['new', 'show'])
|
|
89
|
+
}) // it long chain of parents
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
it ( 'Different branches', () => {
|
|
94
|
+
let result = []
|
|
95
|
+
let g = findInstructions('old', ['something', 'branch'], 'new', ['one', 'two', 'three'])
|
|
96
|
+
for (const n of g) { result.push(n) }
|
|
97
|
+
|
|
98
|
+
expect(result).toHaveLength(7)
|
|
99
|
+
expect(result[0]).toEqual(['old', 'hide'])
|
|
100
|
+
expect(result[1]).toEqual(['branch', 'hide'])
|
|
101
|
+
expect(result[2]).toEqual(['something', 'hide'])
|
|
102
|
+
expect(result[3]).toEqual(['one', 'show'])
|
|
103
|
+
expect(result[4]).toEqual(['two', 'show'])
|
|
104
|
+
expect(result[5]).toEqual(['three', 'show'])
|
|
105
|
+
expect(result[6]).toEqual(['new', 'show'])
|
|
106
|
+
}) // it long chain of parents
|
|
107
|
+
|
|
108
|
+
}) // describe
|
package/tsconfig.json
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
1
|
export default findInstructions;
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* @function findInstructions
|
|
4
|
+
* @description Generator that yields instructions for transitioning between scenes
|
|
5
|
+
* @param {string|null} currentName - current scene name
|
|
6
|
+
* @param {Array.<string>|null} currentParents - current scene parents
|
|
7
|
+
* @param {string} targetName - target scene name
|
|
8
|
+
* @param {Array.<string>} targetParents - target scene parents
|
|
9
|
+
* @yields {Array.<string, 'show'|'hide'>} - instruction array with scene name and action
|
|
10
|
+
*/
|
|
11
|
+
declare function findInstructions(currentName: string | null, currentParents: Array<string> | null, targetName: string, targetParents: Array<string>): Generator<string[], void, unknown>;
|
|
3
12
|
//# sourceMappingURL=findInstructions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findInstructions.d.ts","sourceRoot":"","sources":["../src/findInstructions.js"],"names":[],"mappings":";AAGA,
|
|
1
|
+
{"version":3,"file":"findInstructions.d.ts","sourceRoot":"","sources":["../src/findInstructions.js"],"names":[],"mappings":";AAGA;;;;;;;;GAQG;AACH,+CANW,MAAM,GAAC,IAAI,kBACX,KAAK,CAAE,MAAM,CAAC,GAAC,IAAI,cACnB,MAAM,iBACN,KAAK,CAAE,MAAM,CAAC,sCAoExB"}
|
package/types/main.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ declare function main(cfg?: {
|
|
|
4
4
|
}): {
|
|
5
5
|
hide: (endSteps?: number | string) => Promise<any>;
|
|
6
6
|
listShortcuts: (sceneName: string) => (Array<string> | null);
|
|
7
|
-
setScenes: (list:
|
|
7
|
+
setScenes: (list: import("./methods/setScenes.js").sceneDescription[]) => void;
|
|
8
8
|
show: ({ scene: requestedScene, options }: {
|
|
9
9
|
scene: string;
|
|
10
10
|
options?: {
|
|
@@ -12,19 +12,19 @@ declare function main(cfg?: {
|
|
|
12
12
|
};
|
|
13
13
|
}, ...args?: any[]) => Promise<any>;
|
|
14
14
|
jump: ({ scene }: {
|
|
15
|
-
scene:
|
|
16
|
-
}, ...args
|
|
15
|
+
scene: string;
|
|
16
|
+
}, ...args?: any[]) => Promise<any>;
|
|
17
17
|
jumpBack: ({ hops }?: {
|
|
18
|
-
hops
|
|
19
|
-
}, ...args
|
|
18
|
+
hops?: number;
|
|
19
|
+
}, ...args?: any[]) => Promise<any> | undefined;
|
|
20
20
|
jumpsReset: () => void;
|
|
21
21
|
loadPlugins(plugins: Array<"Key" | "Click" | "Form">): Promise<Function[]>;
|
|
22
|
-
setDependencies(deps: any):
|
|
22
|
+
setDependencies(deps: any): void;
|
|
23
23
|
getDependencies(): any;
|
|
24
24
|
setNote(note: string): any;
|
|
25
25
|
listScenes(): Array<string>;
|
|
26
|
-
enablePlugin(plugin: Function, options?: any):
|
|
27
|
-
disablePlugin(pluginName: string):
|
|
28
|
-
emit(event: string, ...args: any[]):
|
|
26
|
+
enablePlugin(plugin: Function, options?: any): void;
|
|
27
|
+
disablePlugin(pluginName: string): void;
|
|
28
|
+
emit(event: string, ...args: any[]): void;
|
|
29
29
|
};
|
|
30
30
|
//# sourceMappingURL=main.d.ts.map
|
package/types/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.js"],"names":[],"mappings":";AAoCA
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.js"],"names":[],"mappings":";AAoCA;;;;;;;;;;;;;;;;;;;yBAyCmB,KAAK,0BAAc,GACjB,OAAO,CAAE,UAAU,CAAC;0BAgBtB,GAAC;uBAQC,GAAC;kBAMH,MAAM;kBAQJ,KAAK,CAAE,MAAM,CAAC;6CAQhB,GAAC;8BAQD,MAAM;gBASL,MAAM,WACH,GAAC,EAAA;EAKvB"}
|
package/types/methods/jump.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jump.d.ts","sourceRoot":"","sources":["../../src/methods/jump.js"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"jump.d.ts","sourceRoot":"","sources":["../../src/methods/jump.js"],"names":[],"mappings":";AAAA,0DAKO;IAAwB,KAAK,EAArB,MAAM;CACd,mCAQN"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jumpBack.d.ts","sourceRoot":"","sources":["../../src/methods/jumpBack.js"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"jumpBack.d.ts","sourceRoot":"","sources":["../../src/methods/jumpBack.js"],"names":[],"mappings":";AAAA,8DAKO;IAAyB,IAAI,GAArB,MAAM;CACd,sBACU,eAAQ,SAAS,CAWjC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jumpsReset.d.ts","sourceRoot":"","sources":["../../src/methods/jumpsReset.js"],"names":[],"mappings":";AAAA,
|
|
1
|
+
{"version":3,"file":"jumpsReset.d.ts","sourceRoot":"","sources":["../../src/methods/jumpsReset.js"],"names":[],"mappings":";AAAA,+CAIiB,IAAI,CAKpB"}
|
|
@@ -1,19 +1,53 @@
|
|
|
1
1
|
export default setScenes;
|
|
2
|
-
export type SceneObject =
|
|
3
|
-
|
|
2
|
+
export type SceneObject = {
|
|
3
|
+
/**
|
|
4
|
+
* - load interface and prerequisites for the scene;
|
|
5
|
+
*/
|
|
6
|
+
show: Function;
|
|
7
|
+
/**
|
|
8
|
+
* - run after scene is loaded. Optional. Returns true to continue loading, false to cancel;
|
|
9
|
+
*/
|
|
10
|
+
afterShow?: () => boolean;
|
|
11
|
+
/**
|
|
12
|
+
* - Reverse the settings from 'show';
|
|
13
|
+
*/
|
|
14
|
+
hide: () => void;
|
|
15
|
+
/**
|
|
16
|
+
* - Run before scene is hidden. Optional;
|
|
17
|
+
*/
|
|
18
|
+
beforeHide?: () => void;
|
|
19
|
+
/**
|
|
20
|
+
* - list of parent scene names.
|
|
21
|
+
*/
|
|
22
|
+
parents?: Array<string>;
|
|
23
|
+
/**
|
|
24
|
+
* - shortcut descriptions
|
|
25
|
+
*/
|
|
26
|
+
"": any;
|
|
27
|
+
};
|
|
28
|
+
export type sceneDescription = {
|
|
29
|
+
/**
|
|
30
|
+
* - name of the scene
|
|
31
|
+
*/
|
|
32
|
+
name: string;
|
|
33
|
+
/**
|
|
34
|
+
* - object should contain 'show' and 'hide' + shortcuts and action functions
|
|
35
|
+
*/
|
|
36
|
+
scene: SceneObject;
|
|
37
|
+
};
|
|
4
38
|
/**
|
|
5
39
|
* @typedef {Object} SceneObject
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
10
|
-
* @
|
|
11
|
-
* @
|
|
40
|
+
* @property {function} show - load interface and prerequisites for the scene;
|
|
41
|
+
* @property {function():boolean} [afterShow] - run after scene is loaded. Optional. Returns true to continue loading, false to cancel;
|
|
42
|
+
* @property {function():void} hide - Reverse the settings from 'show';
|
|
43
|
+
* @property {function():void} [beforeHide] - Run before scene is hidden. Optional;
|
|
44
|
+
* @property {Array.<string>} [parents] - list of parent scene names.
|
|
45
|
+
* @property {*} ... - shortcut descriptions
|
|
12
46
|
*/
|
|
13
47
|
/**
|
|
14
48
|
* @typedef {Object} sceneDescription
|
|
15
|
-
* @
|
|
16
|
-
* @
|
|
49
|
+
* @property {string} name - name of the scene
|
|
50
|
+
* @property {SceneObject} scene - object should contain 'show' and 'hide' + shortcuts and action functions
|
|
17
51
|
*/
|
|
18
|
-
declare function setScenes(dependencies: any, state: any): (list:
|
|
52
|
+
declare function setScenes(dependencies: any, state: any): (list: sceneDescription[]) => void;
|
|
19
53
|
//# sourceMappingURL=setScenes.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setScenes.d.ts","sourceRoot":"","sources":["../../src/methods/setScenes.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"setScenes.d.ts","sourceRoot":"","sources":["../../src/methods/setScenes.js"],"names":[],"mappings":";;;;;;;;;gBAMc,MAAW,OAAO;;;;UAClB,MAAW,IAAI;;;;iBACf,MAAW,IAAI;;;;cACf,KAAK,CAAE,MAAM,CAAC;;;;QACd,GAAC;;;;;;UAOD,MAAM;;;;WACN,WAAW;;AAfzB;;;;;;;;GAQG;AAIH;;;;GAIG;AAKH,4DAOgC,MAJhB,gBAAgB,EAII,UAsBlC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setInstruction.d.ts","sourceRoot":"","sources":["../src/setInstruction.js"],"names":[],"mappings":";AAEA,
|
|
1
|
+
{"version":3,"file":"setInstruction.d.ts","sourceRoot":"","sources":["../src/setInstruction.js"],"names":[],"mappings":";AAEA,qDAQiC,YAAE,EAAE,GAAG,MAHtB,GAAC,EAGyB,cAO1C"}
|