@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 +36 -33
- package/index.min.js +1 -1
- package/index.mjs +38 -25
- package/package.json +2 -1
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.
|
|
170
|
+
this[id].pull(); // Reset all key(s)
|
|
179
171
|
}
|
|
180
172
|
|
|
181
173
|
function onInput(e) {
|
|
182
|
-
|
|
174
|
+
onBlur.call(this);
|
|
183
175
|
}
|
|
184
176
|
|
|
185
177
|
function onKeyDown(e) {
|
|
186
178
|
var command,
|
|
187
|
-
map = this
|
|
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.
|
|
194
|
+
this[id].pull(e.key); // Reset current key
|
|
203
195
|
}
|
|
204
196
|
|
|
205
|
-
function attach(
|
|
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
|
-
|
|
215
|
-
|
|
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
|
-
|
|
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
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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(
|
|
233
|
+
function detach() {
|
|
231
234
|
var $ = this;
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
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,
|
|
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 {
|
|
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.
|
|
11
|
+
this[id].pull(); // Reset all key(s)
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
function onInput(e) {
|
|
13
|
-
|
|
15
|
+
onBlur.call(this);
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
function onKeyDown(e) {
|
|
17
|
-
let command, map = this
|
|
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.
|
|
33
|
+
this[id].pull(e.key); // Reset current key
|
|
32
34
|
}
|
|
33
35
|
|
|
34
|
-
function attach(
|
|
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
|
-
|
|
42
|
-
|
|
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
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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(
|
|
68
|
+
function detach() {
|
|
56
69
|
let $ = this;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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.
|
|
49
|
+
"version": "1.0.3"
|
|
49
50
|
}
|