@taufik-nurrohman/text-editor.key 1.0.2 → 1.0.3

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/index.js CHANGED
@@ -158,33 +158,25 @@
158
158
  }, time);
159
159
  };
160
160
  };
161
- var offEvent = function offEvent(name, node, then) {
162
- node.removeEventListener(name, then);
163
- };
164
161
  var offEventDefault = function offEventDefault(e) {
165
162
  return e && e.preventDefault();
166
163
  };
167
- var onEvent = function onEvent(name, node, then, options) {
168
- if (options === void 0) {
169
- options = false;
170
- }
171
- node.addEventListener(name, then, options);
172
- };
173
164
  var bounce = debounce(function (map) {
174
165
  return map.pull();
175
166
  }, 1000);
167
+ var id = 'Key_' + Date.now();
176
168
 
177
169
  function onBlur(e) {
178
- this.Key.pull(); // Reset all key(s)
170
+ this[id].pull(); // Reset all key(s)
179
171
  }
180
172
 
181
173
  function onInput(e) {
182
- this.Key.pull(); // Reset all key(s)
174
+ onBlur.call(this);
183
175
  }
184
176
 
185
177
  function onKeyDown(e) {
186
178
  var command,
187
- map = this.Key,
179
+ map = this[id],
188
180
  v;
189
181
  map.push(e.key); // Add current key to the queue
190
182
  if (command = map.command()) {
@@ -199,43 +191,54 @@
199
191
  }
200
192
 
201
193
  function onKeyUp(e) {
202
- this.Key.pull(e.key); // Reset current key
194
+ this[id].pull(e.key); // Reset current key
203
195
  }
204
196
 
205
- function attach(self) {
197
+ function attach() {
206
198
  var $ = this;
207
199
  var map = new Key($);
208
200
  $.command = function (command, of) {
209
201
  return $.commands[command] = of, $;
210
202
  };
211
- $.commands = fromStates(map.commands, $.state.commands || {});
203
+ $.commands = fromStates($.commands = map.commands, $.state.commands || {});
212
204
  $.k = function (join) {
213
- var key = map + "";
214
- if (!join || '-' === join) {
215
- return key;
205
+ var key = map + "",
206
+ keys;
207
+ if (isSet(join) && '-' !== join) {
208
+ keys = "" !== key ? key.split(/(?<!-)-/) : [];
209
+ if (false !== join) {
210
+ return keys.join(join);
211
+ }
216
212
  }
217
- return key.split(/(?<!-)-/).join(join);
213
+ if (false === join) {
214
+ if ('-' === key) {
215
+ return [key];
216
+ }
217
+ return keys;
218
+ }
219
+ return key;
218
220
  };
219
221
  $.key = function (key, of) {
220
222
  return $.keys[key] = of, $;
221
223
  };
222
- $.keys = fromStates(map.keys, $.state.keys || {});
223
- onEvent('blur', self, onBlur);
224
- onEvent('input', self, onInput);
225
- onEvent('keydown', self, onKeyDown);
226
- onEvent('keyup', self, onKeyUp);
227
- self.Key = map;
224
+ $.keys = fromStates($.keys = map.keys, $.state.keys || {});
225
+ $.on('blur', onBlur);
226
+ $.on('input', onInput);
227
+ $.on('key.down', onKeyDown);
228
+ $.on('key.up', onKeyUp);
229
+ $[id] = map;
230
+ return $;
228
231
  }
229
232
 
230
- function detach(self) {
233
+ function detach() {
231
234
  var $ = this;
232
- delete $.commands;
233
- delete $.keys;
234
- delete self.Key;
235
- offEvent('blur', self, onBlur);
236
- offEvent('input', self, onInput);
237
- offEvent('keydown', self, onKeyDown);
238
- offEvent('keyup', self, onKeyUp);
235
+ $[id].pull();
236
+ $.off('blur', onBlur);
237
+ $.off('input', onInput);
238
+ $.off('key.down', onKeyDown);
239
+ $.off('key.up', onKeyUp);
240
+ delete $[id];
241
+ return $;
239
242
  }
240
243
  var index_js = {
241
244
  attach: attach,
package/index.min.js CHANGED
@@ -23,4 +23,4 @@
23
23
  * SOFTWARE.
24
24
  *
25
25
  */
26
- !function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):((n="undefined"!=typeof globalThis?globalThis:n||self).TextEditor=n.TextEditor||{},n.TextEditor.Key=e())}(this,(function(){"use strict";var n=function(n){return Array.isArray(n)},e=function(n){return"function"==typeof n},t=function(n,e){return void 0===e&&(e=!0),"object"==typeof n&&(!e||function(n,e){return n&&r(e)&&n instanceof e}(n,Object))},r=function(n){return function(n){return void 0!==n}(n)&&!function(n){return null===n}(n)},o=function(n){return"string"==typeof n},u=function e(){for(var o=arguments.length,u=Array(o),f=0;f<o;f++)u[f]=arguments[f];for(var c,s=u.shift(),l=0,a=i(u);l<a;++l)for(var d in u[l])if(r(s[d]))if(n(s[d])&&n(u[l][d])){s[d]=[].concat(s[d]);for(var y=0,m=i(u[l][d]);y<m;++y)c=u[l][d][y],-1===s[d].indexOf(c)&&s[d].push(u[l][d][y])}else t(s[d])&&t(u[l][d])?s[d]=e({},s[d],u[l][d]):s[d]=u[l][d];else s[d]=u[l][d];return s},i=function(n){return n.length},f=function(n){return Object.keys(n)};function c(t){var u=this,i={};return u.command=function(n){if(o(n))return n===u.toString();var e=u.keys[u.toString()];return!!r(e)&&e},u.commands={},u.fire=function(t){var i,f,c=u.self||u;if(e(t))i=t.call(c),f=!0;else if(o(t)&&(t=u.commands[t]))i=t.call(c),f=!0;else if(n(t)){var s=t[1]||[];(t=u.commands[t[0]])&&(i=t.apply(c,s),f=!0)}return f?!r(i)||i:null},u.key=null,u.keys={},u.pull=function(n){return u.key=null,r(n)?(delete i[n],u):(i={},u)},u.push=function(n){return i[u.key=n]=1,u},u.self=t,u.toString=function(){return f(i).join("-")},u}var s,l,a,d=function(n,e,t){e.removeEventListener(n,t)},y=function(n){return n&&n.preventDefault()},m=function(n,e,t,r){void 0===r&&(r=!1),e.addEventListener(n,t,r)},p=(s=function(n){return n.pull()},l=1e3,function(){var n=arguments,e=this;a&&clearTimeout(a),a=setTimeout((function(){return s.apply(e,n)}),l)});function v(n){this.Key.pull()}function h(n){this.Key.pull()}function k(n){var e,t,r=this.Key;r.push(n.key),(e=r.command())&&(!1===(t=r.fire(e))?y(n):null===t&&console.warn("Unknown command: `"+e+"`")),p(r)}function b(n){this.Key.pull(n.key)}return{attach:function(n){var e=this,t=new c(e);e.command=function(n,t){return e.commands[n]=t,e},e.commands=u(t.commands,e.state.commands||{}),e.k=function(n){var e=t+"";return n&&"-"!==n?e.split(/(?<!-)-/).join(n):e},e.key=function(n,t){return e.keys[n]=t,e},e.keys=u(t.keys,e.state.keys||{}),m("blur",n,v),m("input",n,h),m("keydown",n,k),m("keyup",n,b),n.Key=t},detach:function(n){delete this.commands,delete this.keys,delete n.Key,d("blur",n,v),d("input",n,h),d("keydown",n,k),d("keyup",n,b)}}}));
26
+ !function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):((n="undefined"!=typeof globalThis?globalThis:n||self).TextEditor=n.TextEditor||{},n.TextEditor.Key=t())}(this,(function(){"use strict";var n=function(n){return Array.isArray(n)},t=function(n){return"function"==typeof n},e=function(n,t){return void 0===t&&(t=!0),"object"==typeof n&&(!t||function(n,t){return n&&r(t)&&n instanceof t}(n,Object))},r=function(n){return function(n){return void 0!==n}(n)&&!function(n){return null===n}(n)},o=function(n){return"string"==typeof n},u=function t(){for(var o=arguments.length,u=Array(o),f=0;f<o;f++)u[f]=arguments[f];for(var c,l=u.shift(),s=0,a=i(u);s<a;++s)for(var d in u[s])if(r(l[d]))if(n(l[d])&&n(u[s][d])){l[d]=[].concat(l[d]);for(var y=0,m=i(u[s][d]);y<m;++y)c=u[s][d][y],-1===l[d].indexOf(c)&&l[d].push(u[s][d][y])}else e(l[d])&&e(u[s][d])?l[d]=t({},l[d],u[s][d]):l[d]=u[s][d];else l[d]=u[s][d];return l},i=function(n){return n.length},f=function(n){return Object.keys(n)};function c(e){var u=this,i={};return u.command=function(n){if(o(n))return n===u.toString();var t=u.keys[u.toString()];return!!r(t)&&t},u.commands={},u.fire=function(e){var i,f,c=u.self||u;if(t(e))i=e.call(c),f=!0;else if(o(e)&&(e=u.commands[e]))i=e.call(c),f=!0;else if(n(e)){var l=e[1]||[];(e=u.commands[e[0]])&&(i=e.apply(c,l),f=!0)}return f?!r(i)||i:null},u.key=null,u.keys={},u.pull=function(n){return u.key=null,r(n)?(delete i[n],u):(i={},u)},u.push=function(n){return i[u.key=n]=1,u},u.self=e,u.toString=function(){return f(i).join("-")},u}var l,s,a,d=function(n){return n&&n.preventDefault()},y=(l=function(n){return n.pull()},s=1e3,function(){var n=arguments,t=this;a&&clearTimeout(a),a=setTimeout((function(){return l.apply(t,n)}),s)}),m="Key_"+Date.now();function p(n){this[m].pull()}function k(n){p.call(this)}function h(n){var t,e,r=this[m];r.push(n.key),(t=r.command())&&(!1===(e=r.fire(t))?d(n):null===e&&console.warn("Unknown command: `"+t+"`")),y(r)}function v(n){this[m].pull(n.key)}return{attach:function(){var n=this,t=new c(n);return n.command=function(t,e){return n.commands[t]=e,n},n.commands=u(n.commands=t.commands,n.state.commands||{}),n.k=function(n){var e,o=t+"";return r(n)&&"-"!==n&&(e=""!==o?o.split(/(?<!-)-/):[],!1!==n)?e.join(n):!1===n?"-"===o?[o]:e:o},n.key=function(t,e){return n.keys[t]=e,n},n.keys=u(n.keys=t.keys,n.state.keys||{}),n.on("blur",p),n.on("input",k),n.on("key.down",h),n.on("key.up",v),n[m]=t,n},detach:function(){var n=this;return n[m].pull(),n.off("blur",p),n.off("input",k),n.off("key.down",h),n.off("key.up",v),delete n[m],n}}}));
package/index.mjs CHANGED
@@ -1,20 +1,22 @@
1
1
  import Key from '@taufik-nurrohman/key';
2
2
  import {debounce} from '@taufik-nurrohman/tick';
3
3
  import {fromStates} from '@taufik-nurrohman/from';
4
- import {onEvent, offEvent, offEventDefault} from '@taufik-nurrohman/event';
4
+ import {isSet} from '@taufik-nurrohman/is';
5
+ import {offEventDefault} from '@taufik-nurrohman/event';
5
6
 
6
7
  const bounce = debounce(map => map.pull(), 1000);
8
+ const id = 'Key_' + Date.now();
7
9
 
8
10
  function onBlur(e) {
9
- this.Key.pull(); // Reset all key(s)
11
+ this[id].pull(); // Reset all key(s)
10
12
  }
11
13
 
12
14
  function onInput(e) {
13
- this.Key.pull(); // Reset all key(s)
15
+ onBlur.call(this);
14
16
  }
15
17
 
16
18
  function onKeyDown(e) {
17
- let command, map = this.Key, v;
19
+ let command, map = this[id], v;
18
20
  map.push(e.key); // Add current key to the queue
19
21
  if (command = map.command()) {
20
22
  v = map.fire(command);
@@ -28,39 +30,50 @@ function onKeyDown(e) {
28
30
  }
29
31
 
30
32
  function onKeyUp(e) {
31
- this.Key.pull(e.key); // Reset current key
33
+ this[id].pull(e.key); // Reset current key
32
34
  }
33
35
 
34
- function attach(self) {
36
+ function attach() {
35
37
  let $ = this;
36
38
  let map = new Key($);
37
39
  $.command = (command, of) => (($.commands[command] = of), $);
38
- $.commands = fromStates(map.commands, $.state.commands || {});
40
+ $.commands = fromStates($.commands = map.commands, $.state.commands || {});
39
41
  $.k = join => {
40
- let key = map + "";
41
- if (!join || '-' === join) {
42
- return key;
42
+ let key = map + "",
43
+ keys;
44
+ if (isSet(join) && '-' !== join) {
45
+ keys = "" !== key ? key.split(/(?<!-)-/) : [];
46
+ if (false !== join) {
47
+ return keys.join(join);
48
+ }
43
49
  }
44
- return key.split(/(?<!-)-/).join(join);
50
+ if (false === join) {
51
+ if ('-' === key) {
52
+ return [key];
53
+ }
54
+ return keys;
55
+ }
56
+ return key;
45
57
  };
46
58
  $.key = (key, of) => (($.keys[key] = of), $);
47
- $.keys = fromStates(map.keys, $.state.keys || {});
48
- onEvent('blur', self, onBlur);
49
- onEvent('input', self, onInput);
50
- onEvent('keydown', self, onKeyDown);
51
- onEvent('keyup', self, onKeyUp);
52
- self.Key = map;
59
+ $.keys = fromStates($.keys = map.keys, $.state.keys || {});
60
+ $.on('blur', onBlur);
61
+ $.on('input', onInput);
62
+ $.on('key.down', onKeyDown);
63
+ $.on('key.up', onKeyUp);
64
+ $[id] = map;
65
+ return $;
53
66
  }
54
67
 
55
- function detach(self) {
68
+ function detach() {
56
69
  let $ = this;
57
- delete $.commands;
58
- delete $.keys;
59
- delete self.Key;
60
- offEvent('blur', self, onBlur);
61
- offEvent('input', self, onInput);
62
- offEvent('keydown', self, onKeyDown);
63
- offEvent('keyup', self, onKeyUp);
70
+ $[id].pull();
71
+ $.off('blur', onBlur);
72
+ $.off('input', onInput);
73
+ $.off('key.down', onKeyDown);
74
+ $.off('key.up', onKeyUp);
75
+ delete $[id];
76
+ return $;
64
77
  }
65
78
 
66
79
  export default {attach, detach};
package/package.json CHANGED
@@ -5,6 +5,7 @@
5
5
  "dependencies": {
6
6
  "@taufik-nurrohman/event": "*",
7
7
  "@taufik-nurrohman/from": "*",
8
+ "@taufik-nurrohman/is": "*",
8
9
  "@taufik-nurrohman/key": "*",
9
10
  "@taufik-nurrohman/text-editor": "*",
10
11
  "@taufik-nurrohman/tick": "*"
@@ -45,5 +46,5 @@
45
46
  "scripts": {
46
47
  "pack": "pack --clean=false --from=.factory --js-format=umd --js-name=TextEditor.Key --js-top='%(js.license)' --mjs=true --to=."
47
48
  },
48
- "version": "1.0.2"
49
+ "version": "1.0.3"
49
50
  }