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

Sign up to get free protection for your applications and to get access to all the features.
package/index.js CHANGED
@@ -101,52 +101,60 @@
101
101
 
102
102
  function Key(self) {
103
103
  var $ = this;
104
- var queue = {};
105
- $.command = function (v) {
106
- if (isString(v)) {
107
- return v === $.toString();
108
- }
109
- var command = $.keys[$.toString()];
110
- return isSet(command) ? command : false;
111
- };
112
104
  $.commands = {};
113
- $.fire = function (command) {
114
- var self = $.self || $,
115
- value,
116
- exist;
117
- if (isFunction(command)) {
118
- value = command.call(self);
119
- exist = true;
120
- } else if (isString(command) && (command = $.commands[command])) {
121
- value = command.call(self);
122
- exist = true;
123
- } else if (isArray(command)) {
124
- var data = command[1] || [];
125
- if (command = $.commands[command[0]]) {
126
- value = command.apply(self, data);
127
- exist = true;
128
- }
129
- }
130
- return exist ? isSet(value) ? value : true : null;
131
- };
132
105
  $.key = null;
133
106
  $.keys = {};
134
- $.pull = function (key) {
135
- $.key = null;
136
- if (!isSet(key)) {
137
- return queue = {}, $;
138
- }
139
- return delete queue[key], $;
140
- };
141
- $.push = function (key) {
142
- return queue[$.key = key] = 1, $;
143
- };
144
- $.self = self;
145
- $.toString = function () {
146
- return toObjectKeys(queue).join('-');
147
- };
107
+ $.queue = {};
108
+ $.self = self || $;
148
109
  return $;
149
110
  }
111
+ var $$ = Key.prototype;
112
+ $$.command = function (v) {
113
+ var $ = this;
114
+ if (isString(v)) {
115
+ return v === $.toString();
116
+ }
117
+ var command = $.keys[$.toString()];
118
+ return isSet(command) ? command : false;
119
+ };
120
+ $$.fire = function (command) {
121
+ var $ = this;
122
+ var self = $.self || $,
123
+ value,
124
+ exist;
125
+ if (isFunction(command)) {
126
+ value = command.call(self);
127
+ exist = true;
128
+ } else if (isString(command) && (command = $.commands[command])) {
129
+ value = command.call(self);
130
+ exist = true;
131
+ } else if (isArray(command)) {
132
+ var data = command[1] || [];
133
+ if (command = $.commands[command[0]]) {
134
+ value = command.apply(self, data);
135
+ exist = true;
136
+ }
137
+ }
138
+ return exist ? isSet(value) ? value : true : null;
139
+ };
140
+ $$.pull = function (key) {
141
+ var $ = this;
142
+ $.key = null;
143
+ if (!isSet(key)) {
144
+ return $.queue = {}, $;
145
+ }
146
+ return delete $.queue[key], $;
147
+ };
148
+ $$.push = function (key) {
149
+ var $ = this;
150
+ return $.queue[$.key = key] = 1, $;
151
+ };
152
+ $$.toString = function () {
153
+ return toObjectKeys(this.queue).join('-');
154
+ };
155
+ Object.defineProperty(Key, 'name', {
156
+ value: 'Key'
157
+ });
150
158
  var debounce = function debounce(then, time) {
151
159
  var timer;
152
160
  return function () {
@@ -161,6 +169,9 @@
161
169
  var offEventDefault = function offEventDefault(e) {
162
170
  return e && e.preventDefault();
163
171
  };
172
+ var offEventPropagation = function offEventPropagation(e) {
173
+ return e && e.stopPropagation();
174
+ };
164
175
  var bounce = debounce(function (map) {
165
176
  return map.pull();
166
177
  }, 1000);
@@ -176,14 +187,17 @@
176
187
  }
177
188
 
178
189
  function onKeyDown(e) {
190
+ var $ = this;
179
191
  var command,
180
- map = this[id],
192
+ map = $[id],
181
193
  v;
182
194
  map.push(e.key); // Add current key to the queue
195
+ $._event = e;
183
196
  if (command = map.command()) {
184
197
  v = map.fire(command);
185
198
  if (false === v) {
186
199
  offEventDefault(e);
200
+ offEventPropagation(e);
187
201
  } else if (null === v) {
188
202
  console.warn('Unknown command: `' + command + '`');
189
203
  }
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(),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"}}));
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}));
package/index.mjs CHANGED
@@ -2,7 +2,7 @@ import Key from '@taufik-nurrohman/key';
2
2
  import {debounce} from '@taufik-nurrohman/tick';
3
3
  import {fromStates} from '@taufik-nurrohman/from';
4
4
  import {isFunction, isSet} from '@taufik-nurrohman/is';
5
- import {offEventDefault} from '@taufik-nurrohman/event';
5
+ import {offEventDefault, offEventPropagation} from '@taufik-nurrohman/event';
6
6
 
7
7
  const bounce = debounce(map => map.pull(), 1000);
8
8
  const name = 'TextEditor.Key';
@@ -18,12 +18,15 @@ function onInput(e) {
18
18
  }
19
19
 
20
20
  function onKeyDown(e) {
21
- let command, map = this[id], v;
21
+ let $ = this;
22
+ let command, map = $[id], v;
22
23
  map.push(e.key); // Add current key to the queue
24
+ $._event = e;
23
25
  if (command = map.command()) {
24
26
  v = map.fire(command);
25
27
  if (false === v) {
26
28
  offEventDefault(e);
29
+ offEventPropagation(e);
27
30
  } else if (null === v) {
28
31
  console.warn('Unknown command: `' + command + '`');
29
32
  }
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.4"
49
+ "version": "1.0.6"
50
50
  }