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

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
- Text Editor Key
2
- ================
1
+ [Text Editor](https://github.com/taufik-nurrohman/text-editor) » Key
2
+ ==========================================================================
3
3
 
4
- > Key extension for [Text Editor](https://github.com/taufik-nurrohman/text-editor).
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.
5
6
 
6
- Provides a feature to easily interact with the keyboard keys.
7
+ ![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
+ ![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
@@ -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,39 +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 || {});
212
- $.k = function () {
213
- return map + "";
203
+ $.commands = fromStates($.commands = map.commands, $.state.commands || {});
204
+ $.k = function (join) {
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
+ }
212
+ }
213
+ if (false === join) {
214
+ if ('-' === key) {
215
+ return [key];
216
+ }
217
+ return keys;
218
+ }
219
+ return key;
214
220
  };
215
221
  $.key = function (key, of) {
216
222
  return $.keys[key] = of, $;
217
223
  };
218
- $.keys = fromStates(map.keys, $.state.keys || {});
219
- onEvent('blur', self, onBlur);
220
- onEvent('input', self, onInput);
221
- onEvent('keydown', self, onKeyDown);
222
- onEvent('keyup', self, onKeyUp);
223
- 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 $;
224
231
  }
225
232
 
226
- function detach(self) {
233
+ function detach() {
227
234
  var $ = this;
228
- delete $.commands;
229
- delete $.keys;
230
- delete self.Key;
231
- offEvent('blur', self, onBlur);
232
- offEvent('input', self, onInput);
233
- offEvent('keydown', self, onKeyDown);
234
- 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 $;
235
242
  }
236
243
  var index_js = {
237
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)},u=function(n){return"string"==typeof n},o=function e(){for(var u=arguments.length,o=Array(u),f=0;f<u;f++)o[f]=arguments[f];for(var c,s=o.shift(),l=0,a=i(o);l<a;++l)for(var d in o[l])if(r(s[d]))if(n(s[d])&&n(o[l][d])){s[d]=[].concat(s[d]);for(var y=0,m=i(o[l][d]);y<m;++y)c=o[l][d][y],-1===s[d].indexOf(c)&&s[d].push(o[l][d][y])}else t(s[d])&&t(o[l][d])?s[d]=e({},s[d],o[l][d]):s[d]=o[l][d];else s[d]=o[l][d];return s},i=function(n){return n.length},f=function(n){return Object.keys(n)};function c(t){var o=this,i={};return o.command=function(n){if(u(n))return n===o.toString();var e=o.keys[o.toString()];return!!r(e)&&e},o.commands={},o.fire=function(t){var i,f,c=o.self||o;if(e(t))i=t.call(c),f=!0;else if(u(t)&&(t=o.commands[t]))i=t.call(c),f=!0;else if(n(t)){var s=t[1]||[];(t=o.commands[t[0]])&&(i=t.apply(c,s),f=!0)}return f?!r(i)||i:null},o.key=null,o.keys={},o.pull=function(n){return o.key=null,r(n)?(delete i[n],o):(i={},o)},o.push=function(n){return i[o.key=n]=1,o},o.self=t,o.toString=function(){return f(i).join("-")},o}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 h(n){this.Key.pull()}function k(n){this.Key.pull()}function v(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=o(t.commands,e.state.commands||{}),e.k=function(){return t+""},e.key=function(n,t){return e.keys[n]=t,e},e.keys=o(t.keys,e.state.keys||{}),m("blur",n,h),m("input",n,k),m("keydown",n,v),m("keyup",n,b),n.Key=t},detach:function(n){delete this.commands,delete this.keys,delete n.Key,d("blur",n,h),d("input",n,k),d("keydown",n,v),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,33 +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 || {});
39
- $.k = () => map + "";
40
+ $.commands = fromStates($.commands = map.commands, $.state.commands || {});
41
+ $.k = join => {
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
+ }
49
+ }
50
+ if (false === join) {
51
+ if ('-' === key) {
52
+ return [key];
53
+ }
54
+ return keys;
55
+ }
56
+ return key;
57
+ };
40
58
  $.key = (key, of) => (($.keys[key] = of), $);
41
- $.keys = fromStates(map.keys, $.state.keys || {});
42
- onEvent('blur', self, onBlur);
43
- onEvent('input', self, onInput);
44
- onEvent('keydown', self, onKeyDown);
45
- onEvent('keyup', self, onKeyUp);
46
- 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 $;
47
66
  }
48
67
 
49
- function detach(self) {
68
+ function detach() {
50
69
  let $ = this;
51
- delete $.commands;
52
- delete $.keys;
53
- delete self.Key;
54
- offEvent('blur', self, onBlur);
55
- offEvent('input', self, onInput);
56
- offEvent('keydown', self, onKeyDown);
57
- 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 $;
58
77
  }
59
78
 
60
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.0"
49
+ "version": "1.0.3"
49
50
  }