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

Sign up to get free protection for your applications and to get access to all the features.
package/index.js CHANGED
@@ -158,33 +158,26 @@
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 name = 'TextEditor.Key';
168
+ var id = '_Key';
176
169
 
177
170
  function onBlur(e) {
178
- this.Key.pull(); // Reset all key(s)
171
+ this[id].pull(); // Reset all key(s)
179
172
  }
180
173
 
181
174
  function onInput(e) {
182
- this.Key.pull(); // Reset all key(s)
175
+ onBlur.call(this);
183
176
  }
184
177
 
185
178
  function onKeyDown(e) {
186
179
  var command,
187
- map = this.Key,
180
+ map = this[id],
188
181
  v;
189
182
  map.push(e.key); // Add current key to the queue
190
183
  if (command = map.command()) {
@@ -199,47 +192,62 @@
199
192
  }
200
193
 
201
194
  function onKeyUp(e) {
202
- this.Key.pull(e.key); // Reset current key
195
+ this[id].pull(e.key); // Reset current key
203
196
  }
204
197
 
205
- function attach(self) {
198
+ function attach() {
206
199
  var $ = this;
200
+ var $$ = $.constructor.prototype;
207
201
  var map = new Key($);
208
- $.command = function (command, of) {
202
+ $.commands = fromStates($.commands = map.commands, $.state.commands || {});
203
+ $.keys = fromStates($.keys = map.keys, $.state.keys || {});
204
+ !isFunction($$.command) && ($$.command = function (command, of) {
205
+ var $ = this;
209
206
  return $.commands[command] = of, $;
210
- };
211
- $.commands = fromStates(map.commands, $.state.commands || {});
212
- $.k = function (join) {
213
- var key = map + "";
214
- if (!join || '-' === join) {
215
- return key;
207
+ });
208
+ !isFunction($$.k) && ($$.k = function (join) {
209
+ var $ = this,
210
+ key = $[id] + "",
211
+ keys;
212
+ if (isSet(join) && '-' !== join) {
213
+ keys = "" !== key ? key.split(/-(?!$)/) : [];
214
+ if (false !== join) {
215
+ return keys.join(join);
216
+ }
216
217
  }
217
- return key.split(/(?<!-)-/).join(join);
218
- };
219
- $.key = function (key, of) {
218
+ if (false === join) {
219
+ if ('-' === key) {
220
+ return [key];
221
+ }
222
+ return keys;
223
+ }
224
+ return key;
225
+ });
226
+ !isFunction($$.key) && ($$.key = function (key, of) {
227
+ var $ = this;
220
228
  return $.keys[key] = of, $;
221
- };
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;
229
+ });
230
+ $.on('blur', onBlur);
231
+ $.on('input', onInput);
232
+ $.on('key.down', onKeyDown);
233
+ $.on('key.up', onKeyUp);
234
+ return $[id] = map, $;
228
235
  }
229
236
 
230
- function detach(self) {
237
+ function detach() {
231
238
  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);
239
+ $[id].pull();
240
+ $.off('blur', onBlur);
241
+ $.off('input', onInput);
242
+ $.off('key.down', onKeyDown);
243
+ $.off('key.up', onKeyUp);
244
+ delete $[id];
245
+ return $;
239
246
  }
240
247
  var index_js = {
241
248
  attach: attach,
242
- detach: detach
249
+ detach: detach,
250
+ name: name
243
251
  };
244
252
  return index_js;
245
253
  }));
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,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 e(s[d])&&e(u[l][d])?s[d]=t({},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(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 s=e[1]||[];(e=u.commands[e[0]])&&(i=e.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=e,u.toString=function(){return f(i).join("-")},u}var s,l,a,d=function(n){return n&&n.preventDefault()},y=(s=function(n){return n.pull()},l=1e3,function(){var n=arguments,t=this;a&&clearTimeout(a),a=setTimeout((function(){return s.apply(t,n)}),l)}),m="_Key";function p(n){this[m].pull()}function h(n){p.call(this)}function k(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,e=n.constructor.prototype,o=new c(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",p),n.on("input",h),n.on("key.down",k),n.on("key.up",v),n[m]=o,n},detach:function(){var n=this;return n[m].pull(),n.off("blur",p),n.off("input",h),n.off("key.down",k),n.off("key.up",v),delete n[m],n},name:"TextEditor.Key"}}));
package/index.mjs CHANGED
@@ -1,20 +1,24 @@
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 {isFunction, 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 name = 'TextEditor.Key';
9
+
10
+ const id = '_Key';
7
11
 
8
12
  function onBlur(e) {
9
- this.Key.pull(); // Reset all key(s)
13
+ this[id].pull(); // Reset all key(s)
10
14
  }
11
15
 
12
16
  function onInput(e) {
13
- this.Key.pull(); // Reset all key(s)
17
+ onBlur.call(this);
14
18
  }
15
19
 
16
20
  function onKeyDown(e) {
17
- let command, map = this.Key, v;
21
+ let command, map = this[id], v;
18
22
  map.push(e.key); // Add current key to the queue
19
23
  if (command = map.command()) {
20
24
  v = map.fire(command);
@@ -28,39 +32,57 @@ function onKeyDown(e) {
28
32
  }
29
33
 
30
34
  function onKeyUp(e) {
31
- this.Key.pull(e.key); // Reset current key
35
+ this[id].pull(e.key); // Reset current key
32
36
  }
33
37
 
34
- function attach(self) {
35
- let $ = this;
36
- let map = new Key($);
37
- $.command = (command, of) => (($.commands[command] = of), $);
38
- $.commands = fromStates(map.commands, $.state.commands || {});
39
- $.k = join => {
40
- let key = map + "";
41
- if (!join || '-' === join) {
42
- return key;
38
+ function attach() {
39
+ const $ = this;
40
+ const $$ = $.constructor.prototype;
41
+ const map = new Key($);
42
+ $.commands = fromStates($.commands = map.commands, $.state.commands || {});
43
+ $.keys = fromStates($.keys = map.keys, $.state.keys || {});
44
+ !isFunction($$.command) && ($$.command = function (command, of) {
45
+ let $ = this;
46
+ return ($.commands[command] = of), $;
47
+ });
48
+ !isFunction($$.k) && ($$.k = function (join) {
49
+ let $ = this,
50
+ key = $[id] + "",
51
+ keys;
52
+ if (isSet(join) && '-' !== join) {
53
+ keys = "" !== key ? key.split(/-(?!$)/) : [];
54
+ if (false !== join) {
55
+ return keys.join(join);
56
+ }
57
+ }
58
+ if (false === join) {
59
+ if ('-' === key) {
60
+ return [key];
61
+ }
62
+ return keys;
43
63
  }
44
- return key.split(/(?<!-)-/).join(join);
45
- };
46
- $.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;
64
+ return key;
65
+ });
66
+ !isFunction($$.key) && ($$.key = function (key, of) {
67
+ let $ = this;
68
+ return ($.keys[key] = of), $;
69
+ });
70
+ $.on('blur', onBlur);
71
+ $.on('input', onInput);
72
+ $.on('key.down', onKeyDown);
73
+ $.on('key.up', onKeyUp);
74
+ return ($[id] = map), $;
53
75
  }
54
76
 
55
- function detach(self) {
77
+ function detach() {
56
78
  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);
79
+ $[id].pull();
80
+ $.off('blur', onBlur);
81
+ $.off('input', onInput);
82
+ $.off('key.down', onKeyDown);
83
+ $.off('key.up', onKeyUp);
84
+ delete $[id];
85
+ return $;
64
86
  }
65
87
 
66
- export default {attach, detach};
88
+ export default {attach, detach, name};
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.4"
49
50
  }