@taufik-nurrohman/text-editor.key 1.0.6 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  [Text Editor](https://github.com/taufik-nurrohman/text-editor) » Key
2
2
  ==========================================================================
3
3
 
4
- Key extension for [Text Editor](https://github.com/taufik-nurrohman/text-editor). This extension provides a feature to
5
- easily interact with the keyboard keys.
4
+ This extension provides a feature to easily interact with the keyboard keys.
6
5
 
7
6
  ![index.js](https://img.shields.io/github/size/taufik-nurrohman/text-editor.key/index.js?branch=main&color=%23f1e05a&label=index.js&labelColor=%231f2328&style=flat-square)
8
7
  ![index.min.js](https://img.shields.io/github/size/taufik-nurrohman/text-editor.key/index.min.js?branch=main&color=%23f1e05a&label=index.min.js&labelColor=%231f2328&style=flat-square)
package/index.js CHANGED
@@ -60,7 +60,7 @@
60
60
  var hasValue = function hasValue(x, data) {
61
61
  return -1 !== data.indexOf(x);
62
62
  };
63
- var fromStates = function fromStates() {
63
+ var _fromStates = function fromStates() {
64
64
  for (var _len = arguments.length, lot = new Array(_len), _key = 0; _key < _len; _key++) {
65
65
  lot[_key] = arguments[_key];
66
66
  }
@@ -82,7 +82,7 @@
82
82
  }
83
83
  // Merge object recursive
84
84
  } else if (isObject(out[k]) && isObject(lot[i][k])) {
85
- out[k] = fromStates({
85
+ out[k] = _fromStates({
86
86
  /* Clone! */ }, out[k], lot[i][k]);
87
87
  // Replace value
88
88
  } else {
@@ -176,20 +176,31 @@
176
176
  return map.pull();
177
177
  }, 1000);
178
178
  var name = 'TextEditor.Key';
179
- var id = '_Key';
179
+ var references = new WeakMap();
180
+
181
+ function getReference(key) {
182
+ return references.get(key) || null;
183
+ }
184
+
185
+ function letReference(key) {
186
+ return references.delete(key);
187
+ }
180
188
 
181
189
  function onBlur(e) {
182
- this[id].pull(); // Reset all key(s)
190
+ var $ = this,
191
+ map = getReference($);
192
+ $._event = e;
193
+ map.pull(); // Reset all key(s)
183
194
  }
184
195
 
185
196
  function onInput(e) {
186
- onBlur.call(this);
197
+ onBlur.call(this, e);
187
198
  }
188
199
 
189
200
  function onKeyDown(e) {
190
201
  var $ = this;
191
202
  var command,
192
- map = $[id],
203
+ map = getReference($),
193
204
  v;
194
205
  map.push(e.key); // Add current key to the queue
195
206
  $._event = e;
@@ -206,22 +217,30 @@
206
217
  }
207
218
 
208
219
  function onKeyUp(e) {
209
- this[id].pull(e.key); // Reset current key
220
+ var $ = this,
221
+ map = getReference($);
222
+ $._event = e;
223
+ map.pull(e.key); // Reset current key
224
+ }
225
+
226
+ function setReference(key, value) {
227
+ return references.set(key, value);
210
228
  }
211
229
 
212
230
  function attach() {
213
231
  var $ = this;
214
232
  var $$ = $.constructor.prototype;
215
233
  var map = new Key($);
216
- $.commands = fromStates($.commands = map.commands, $.state.commands || {});
217
- $.keys = fromStates($.keys = map.keys, $.state.keys || {});
234
+ $.commands = _fromStates($.commands = map.commands, $.state.commands || {});
235
+ $.keys = _fromStates($.keys = map.keys, $.state.keys || {});
218
236
  !isFunction($$.command) && ($$.command = function (command, of) {
219
237
  var $ = this;
220
238
  return $.commands[command] = of, $;
221
239
  });
222
240
  !isFunction($$.k) && ($$.k = function (join) {
223
241
  var $ = this,
224
- key = $[id] + "",
242
+ map = getReference($),
243
+ key = map + "",
225
244
  keys;
226
245
  if (isSet(join) && '-' !== join) {
227
246
  keys = "" !== key ? key.split(/-(?!$)/) : [];
@@ -245,18 +264,18 @@
245
264
  $.on('input', onInput);
246
265
  $.on('key.down', onKeyDown);
247
266
  $.on('key.up', onKeyUp);
248
- return $[id] = map, $;
267
+ return setReference($, map), $;
249
268
  }
250
269
 
251
270
  function detach() {
252
- var $ = this;
253
- $[id].pull();
271
+ var $ = this,
272
+ map = getReference($);
273
+ map.pull();
254
274
  $.off('blur', onBlur);
255
275
  $.off('input', onInput);
256
276
  $.off('key.down', onKeyDown);
257
277
  $.off('key.up', onKeyUp);
258
- delete $[id];
259
- return $;
278
+ return letReference($), $;
260
279
  }
261
280
  var index_js = {
262
281
  attach: attach,
package/index.min.js CHANGED
@@ -23,4 +23,4 @@
23
23
  * SOFTWARE.
24
24
  *
25
25
  */
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,s=u.shift(),a=0,l=i(u);a<l;++a)for(var y in u[a])if(r(s[y]))if(n(s[y])&&n(u[a][y])){s[y]=[].concat(s[y]);for(var d=0,p=i(u[a][y]);d<p;++d)c=u[a][y][d],-1===s[y].indexOf(c)&&s[y].push(u[a][y][d])}else e(s[y])&&e(u[a][y])?s[y]=t({},s[y],u[a][y]):s[y]=u[a][y];else s[y]=u[a][y];return s},i=function(n){return n.length};function f(n){var t=this;return t.commands={},t.key=null,t.keys={},t.queue={},t.self=n||t,t}var c=f.prototype;c.command=function(n){var t=this;if(o(n))return n===t.toString();var e=t.keys[t.toString()];return!!r(e)&&e},c.fire=function(e){var u,i,f=this,c=f.self||f;if(t(e))u=e.call(c),i=!0;else if(o(e)&&(e=f.commands[e]))u=e.call(c),i=!0;else if(n(e)){var s=e[1]||[];(e=f.commands[e[0]])&&(u=e.apply(c,s),i=!0)}return i?!r(u)||u:null},c.pull=function(n){var t=this;return t.key=null,r(n)?(delete t.queue[n],t):(t.queue={},t)},c.push=function(n){var t=this;return t.queue[t.key=n]=1,t},c.toString=function(){return(n=this.queue,Object.keys(n)).join("-");var n},Object.defineProperty(f,"name",{value:"Key"});var s,a,l,y=function(n){return n&&n.preventDefault()},d=function(n){return n&&n.stopPropagation()},p=(s=function(n){return n.pull()},a=1e3,function(){var n=arguments,t=this;l&&clearTimeout(l),l=setTimeout((function(){return s.apply(t,n)}),a)}),m="_Key";function h(n){this[m].pull()}function v(n){h.call(this)}function k(n){var t,e,r=this[m];r.push(n.key),this._event=n,(t=r.command())&&(!1===(e=r.fire(t))?(y(n),d(n)):null===e&&console.warn("Unknown command: `"+t+"`")),p(r)}function b(n){this[m].pull(n.key)}var g={attach:function(){var n=this,e=n.constructor.prototype,o=new f(n);return n.commands=u(n.commands=o.commands,n.state.commands||{}),n.keys=u(n.keys=o.keys,n.state.keys||{}),!t(e.command)&&(e.command=function(n,t){return this.commands[n]=t,this}),!t(e.k)&&(e.k=function(n){var t,e=this[m]+"";return r(n)&&"-"!==n&&(t=""!==e?e.split(/-(?!$)/):[],!1!==n)?t.join(n):!1===n?"-"===e?[e]:t:e}),!t(e.key)&&(e.key=function(n,t){return this.keys[n]=t,this}),n.on("blur",h),n.on("input",v),n.on("key.down",k),n.on("key.up",b),n[m]=o,n},detach:function(){var n=this;return n[m].pull(),n.off("blur",h),n.off("input",v),n.off("key.down",k),n.off("key.up",b),delete n[m],n},name:"TextEditor.Key"};return g}));
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(){for(var t=arguments.length,o=Array(t),f=0;f<t;f++)o[f]=arguments[f];for(var c,s=o.shift(),a=0,l=i(o);a<l;++a)for(var y in o[a])if(r(s[y]))if(n(s[y])&&n(o[a][y])){s[y]=[].concat(s[y]);for(var d=0,p=i(o[a][y]);d<p;++d)c=o[a][y][d],-1===s[y].indexOf(c)&&s[y].push(o[a][y][d])}else e(s[y])&&e(o[a][y])?s[y]=u({},s[y],o[a][y]):s[y]=o[a][y];else s[y]=o[a][y];return s},i=function(n){return n.length};function f(n){var t=this;return t.commands={},t.key=null,t.keys={},t.queue={},t.self=n||t,t}var c=f.prototype;c.command=function(n){var t=this;if(o(n))return n===t.toString();var e=t.keys[t.toString()];return!!r(e)&&e},c.fire=function(e){var u,i,f=this,c=f.self||f;if(t(e))u=e.call(c),i=!0;else if(o(e)&&(e=f.commands[e]))u=e.call(c),i=!0;else if(n(e)){var s=e[1]||[];(e=f.commands[e[0]])&&(u=e.apply(c,s),i=!0)}return i?!r(u)||u:null},c.pull=function(n){var t=this;return t.key=null,r(n)?(delete t.queue[n],t):(t.queue={},t)},c.push=function(n){var t=this;return t.queue[t.key=n]=1,t},c.toString=function(){return(n=this.queue,Object.keys(n)).join("-");var n},Object.defineProperty(f,"name",{value:"Key"});var s,a,l,y=(s=function(n){return n.pull()},a=1e3,function(){var n=arguments,t=this;l&&clearTimeout(l),l=setTimeout((function(){return s.apply(t,n)}),a)}),d=new WeakMap;function p(n){return d.get(n)||null}function m(n){var t=p(this);this._event=n,t.pull()}function h(n){m.call(this,n)}function v(n){var t,e,r=p(this);r.push(n.key),this._event=n,(t=r.command())&&(!1===(e=r.fire(t))?(function(n){n&&n.preventDefault()}(n),function(n){n&&n.stopPropagation()}(n)):null===e&&console.warn("Unknown command: `"+t+"`")),y(r)}function k(n){var t=p(this);this._event=n,t.pull(n.key)}var g={attach:function(){var n,e,o=this,i=o.constructor.prototype,c=new f(o);return o.commands=u(o.commands=c.commands,o.state.commands||{}),o.keys=u(o.keys=c.keys,o.state.keys||{}),!t(i.command)&&(i.command=function(n,t){return this.commands[n]=t,this}),!t(i.k)&&(i.k=function(n){var t,e=p(this)+"";return r(n)&&"-"!==n&&(t=""!==e?e.split(/-(?!$)/):[],!1!==n)?t.join(n):!1===n?"-"===e?[e]:t:e}),!t(i.key)&&(i.key=function(n,t){return this.keys[n]=t,this}),o.on("blur",m),o.on("input",h),o.on("key.down",v),o.on("key.up",k),n=o,e=c,d.set(n,e),o},detach:function(){var n,t=this;return p(t).pull(),t.off("blur",m),t.off("input",h),t.off("key.down",v),t.off("key.up",k),n=t,d.delete(n),t},name:"TextEditor.Key"};return g}));
package/index.mjs CHANGED
@@ -6,20 +6,30 @@ import {offEventDefault, offEventPropagation} from '@taufik-nurrohman/event';
6
6
 
7
7
  const bounce = debounce(map => map.pull(), 1000);
8
8
  const name = 'TextEditor.Key';
9
+ const references = new WeakMap;
9
10
 
10
- const id = '_Key';
11
+ function getReference(key) {
12
+ return references.get(key) || null;
13
+ }
14
+
15
+ function letReference(key) {
16
+ return references.delete(key);
17
+ }
11
18
 
12
19
  function onBlur(e) {
13
- this[id].pull(); // Reset all key(s)
20
+ let $ = this,
21
+ map = getReference($);
22
+ $._event = e;
23
+ map.pull(); // Reset all key(s)
14
24
  }
15
25
 
16
26
  function onInput(e) {
17
- onBlur.call(this);
27
+ onBlur.call(this, e);
18
28
  }
19
29
 
20
30
  function onKeyDown(e) {
21
31
  let $ = this;
22
- let command, map = $[id], v;
32
+ let command, map = getReference($), v;
23
33
  map.push(e.key); // Add current key to the queue
24
34
  $._event = e;
25
35
  if (command = map.command()) {
@@ -35,7 +45,14 @@ function onKeyDown(e) {
35
45
  }
36
46
 
37
47
  function onKeyUp(e) {
38
- this[id].pull(e.key); // Reset current key
48
+ let $ = this,
49
+ map = getReference($);
50
+ $._event = e;
51
+ map.pull(e.key); // Reset current key
52
+ }
53
+
54
+ function setReference(key, value) {
55
+ return references.set(key, value);
39
56
  }
40
57
 
41
58
  function attach() {
@@ -50,8 +67,8 @@ function attach() {
50
67
  });
51
68
  !isFunction($$.k) && ($$.k = function (join) {
52
69
  let $ = this,
53
- key = $[id] + "",
54
- keys;
70
+ map = getReference($),
71
+ key = map + "", keys;
55
72
  if (isSet(join) && '-' !== join) {
56
73
  keys = "" !== key ? key.split(/-(?!$)/) : [];
57
74
  if (false !== join) {
@@ -74,18 +91,18 @@ function attach() {
74
91
  $.on('input', onInput);
75
92
  $.on('key.down', onKeyDown);
76
93
  $.on('key.up', onKeyUp);
77
- return ($[id] = map), $;
94
+ return setReference($, map), $;
78
95
  }
79
96
 
80
97
  function detach() {
81
- let $ = this;
82
- $[id].pull();
98
+ let $ = this,
99
+ map = getReference($);
100
+ map.pull();
83
101
  $.off('blur', onBlur);
84
102
  $.off('input', onInput);
85
103
  $.off('key.down', onKeyDown);
86
104
  $.off('key.up', onKeyUp);
87
- delete $[id];
88
- return $;
105
+ return letReference($), $;
89
106
  }
90
107
 
91
108
  export default {attach, detach, name};
package/package.json CHANGED
@@ -46,5 +46,5 @@
46
46
  "scripts": {
47
47
  "pack": "pack --clean=false --from=.factory --js-format=umd --js-name=TextEditor.Key --js-top='%(js.license)' --mjs=true --to=."
48
48
  },
49
- "version": "1.0.6"
49
+ "version": "1.0.8"
50
50
  }