@taufik-nurrohman/text-editor.key 1.0.11 → 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
- package/index.js +65 -33
- package/index.min.js +1 -1
- package/index.mjs +59 -34
- package/package.json +1 -1
package/index.js
CHANGED
@@ -211,19 +211,41 @@
|
|
211
211
|
onBlur.call(this, e);
|
212
212
|
}
|
213
213
|
|
214
|
-
function
|
215
|
-
var
|
214
|
+
function onKeyDownOrPutDown(e) {
|
215
|
+
var map = getReference(this),
|
216
216
|
command,
|
217
217
|
v,
|
218
|
+
data = e.data,
|
219
|
+
inputType = e.inputType,
|
218
220
|
key = e.key,
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
221
|
+
type = e.type;
|
222
|
+
if ('keydown' === type) {
|
223
|
+
// Make the `Alt`, `Control`, `Meta`, and `Shift` key(s) sticky (does not require the user to release all key(s) first to repeat or change the current key combination).
|
224
|
+
map[e.altKey ? 'push' : 'pull']('Alt');
|
225
|
+
map[e.ctrlKey ? 'push' : 'pull']('Control');
|
226
|
+
map[e.metaKey ? 'push' : 'pull']('Meta');
|
227
|
+
map[e.shiftKey ? 'push' : 'pull']('Shift');
|
228
|
+
// Add the actual key to the queue. Don’t worry, this will not mistakenly add a key that already exists in the queue.
|
229
|
+
key && map.push(key);
|
230
|
+
} else {
|
231
|
+
if ('deleteContentBackward' === inputType) {
|
232
|
+
map.pull().push('Backspace'); // Simulate `Backspace` key
|
233
|
+
} else if ('deleteContentForward' === inputType) {
|
234
|
+
map.pull().push('Delete'); // Simulate `Delete` key
|
235
|
+
} else if ('deleteWordBackward' === inputType) {
|
236
|
+
map.pull().push('Control').push('Backspace'); // Simulate `Control-Backspace` keys
|
237
|
+
} else if ('deleteWordForward' === inputType) {
|
238
|
+
map.pull().push('Control').push('Delete'); // Simulate `Control-Delete` keys
|
239
|
+
} else if ('insertLineBreak' === inputType) {
|
240
|
+
map.pull().push('Enter'); // Simulate `Enter` key
|
241
|
+
} else if ('insertText' === inputType && data) {
|
242
|
+
// One character at a time
|
243
|
+
map.toArray().forEach(function (key) {
|
244
|
+
return 1 === toCount(key) && map.pull(key);
|
245
|
+
});
|
246
|
+
map.push(data);
|
247
|
+
}
|
248
|
+
}
|
227
249
|
if (command = map.command()) {
|
228
250
|
v = map.fire(command);
|
229
251
|
if (false === v) {
|
@@ -236,24 +258,32 @@
|
|
236
258
|
bounce(map, e); // Reset all key(s) after 1 second idle.
|
237
259
|
}
|
238
260
|
|
239
|
-
function
|
240
|
-
var
|
261
|
+
function onKeyUpOrPutUp(e) {
|
262
|
+
var map = getReference(this),
|
263
|
+
data = e.data,
|
264
|
+
inputType = e.inputType,
|
241
265
|
key = e.key,
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
key
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
})
|
255
|
-
|
256
|
-
|
266
|
+
type = e.type;
|
267
|
+
if ('keyup' === type) {
|
268
|
+
map[e.altKey ? 'push' : 'pull']('Alt');
|
269
|
+
map[e.ctrlKey ? 'push' : 'pull']('Control');
|
270
|
+
map[e.metaKey ? 'push' : 'pull']('Meta');
|
271
|
+
map[e.shiftKey ? 'push' : 'pull']('Shift');
|
272
|
+
key && map.pull(key);
|
273
|
+
} else {
|
274
|
+
if ('deleteContentBackward' === inputType) {
|
275
|
+
map.pull('Backspace');
|
276
|
+
} else if ('deleteContentForward' === inputType) {
|
277
|
+
map.pull('Delete');
|
278
|
+
} else if ('deleteWordBackward' === inputType) {
|
279
|
+
map.pull('Control').pull('Backspace');
|
280
|
+
} else if ('deleteWordForward' === inputType) {
|
281
|
+
map.pull('Control').pull('Delete');
|
282
|
+
} else if ('insertLineBreak' === inputType) {
|
283
|
+
map.pull('Enter');
|
284
|
+
} else if ('insertText' === inputType && data) {
|
285
|
+
map.pull(data);
|
286
|
+
}
|
257
287
|
}
|
258
288
|
}
|
259
289
|
|
@@ -283,9 +313,10 @@
|
|
283
313
|
});
|
284
314
|
$.on('blur', onBlur);
|
285
315
|
$.on('focus', onFocus);
|
286
|
-
$.on('key.down',
|
287
|
-
$.on('key.up',
|
288
|
-
$.on('put.down',
|
316
|
+
$.on('key.down', onKeyDownOrPutDown);
|
317
|
+
$.on('key.up', onKeyUpOrPutUp);
|
318
|
+
$.on('put.down', onKeyDownOrPutDown);
|
319
|
+
$.on('put.up', onKeyUpOrPutUp);
|
289
320
|
return setReference($, map), $;
|
290
321
|
}
|
291
322
|
|
@@ -295,9 +326,10 @@
|
|
295
326
|
map.pull();
|
296
327
|
$.off('blur', onBlur);
|
297
328
|
$.off('focus', onFocus);
|
298
|
-
$.off('key.down',
|
299
|
-
$.off('key.up',
|
300
|
-
$.off('put.down',
|
329
|
+
$.off('key.down', onKeyDownOrPutDown);
|
330
|
+
$.off('key.up', onKeyUpOrPutUp);
|
331
|
+
$.off('put.down', onKeyDownOrPutDown);
|
332
|
+
$.off('put.up', onKeyUpOrPutUp);
|
301
333
|
return letReference($), $;
|
302
334
|
}
|
303
335
|
var index_js = {
|
package/index.min.js
CHANGED
@@ -23,4 +23,4 @@
|
|
23
23
|
* SOFTWARE.
|
24
24
|
*
|
25
25
|
*/
|
26
|
-
!function(t,
|
26
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):((t="undefined"!=typeof globalThis?globalThis:t||self).TextEditor=t.TextEditor||{},t.TextEditor.Key=e())}(this,(function(){"use strict";var t=function(t){return Array.isArray(t)},e=function(t){return"function"==typeof t},n=function(t,e){return void 0===e&&(e=!0),!(!t||"object"!=typeof t)&&(!e||function(t,e){return!(!t||"object"!=typeof t)&&r(e)&&r(t.constructor)&&e===t.constructor}(t,Object))},r=function(t){return function(t){return void 0!==t}(t)&&!function(t){return null===t}(t)},o=function(t){return"string"==typeof t};function u(t){var e=this;return e.commands={},e.key=null,e.keys={},e.self=t||e,e.set=new Set,e}var l=u.prototype;l.command=function(t){var e=this;if(o(t))return t===e.toString();var n=e.keys[e.toString()];return!!r(n)&&n},l.fire=function(n,u){var l,i,a=this,s=a.self||a;return u=u||[],e(n)||o(n)&&(n=a.commands[n])?(l=n.apply(s,u),i=!0):t(n)&&(t(n[1])&&n[1].forEach((function(t,e){return r(t)&&(u[e]=t)})),(n=a.commands[n[0]])&&(l=n.apply(s,u),i=!0)),i?!r(l)||l:null},l.pull=function(t){var e=this;return e.key=null,r(t)?(e.set.delete(t),e):(e.set=new Set,e)},l.push=function(t){var e=this;return e.set.add(e.key=t,1),e},l.toArray=function(){return Array.from(this.set)},l.toString=function(){return this.toArray().join("-")},Object.defineProperty(u,"name",{value:"Key"});var i,a,s,p=function(t){return t.length},f=function(){for(var e=arguments.length,o=Array(e),u=0;u<e;u++)o[u]=arguments[u];for(var l,i=o.shift(),a=0,s=p(o);a<s;++a)for(var c in o[a])if(r(i[c]))if(t(i[c])&&t(o[a][c])){i[c]=[].concat(i[c]);for(var d=0,y=p(o[a][c]);d<y;++d)l=o[a][c][d],-1===i[c].indexOf(l)&&i[c].push(o[a][c][d])}else n(i[c])&&n(o[a][c])?i[c]=f({},i[c],o[a][c]):i[c]=o[a][c];else i[c]=o[a][c];return i},c=(i=function(t,e){t.pull(),e.altKey&&t.push("Alt"),e.ctrlKey&&t.push("Control"),e.metaKey&&t.push("Meta"),e.shiftKey&&t.push("Shift")},a=1e3,function(){var t=arguments,e=this;s&&clearTimeout(s),s=setTimeout((function(){return i.apply(e,t)}),a)}),d=new WeakMap;function y(t){return d.get(t)||null}function h(t){y(this).pull()}function m(t){h.call(this,t)}function k(t){var e,n,r=y(this),o=t.data,u=t.inputType,l=t.key;"keydown"===t.type?(r[t.altKey?"push":"pull"]("Alt"),r[t.ctrlKey?"push":"pull"]("Control"),r[t.metaKey?"push":"pull"]("Meta"),r[t.shiftKey?"push":"pull"]("Shift"),l&&r.push(l)):"deleteContentBackward"===u?r.pull().push("Backspace"):"deleteContentForward"===u?r.pull().push("Delete"):"deleteWordBackward"===u?r.pull().push("Control").push("Backspace"):"deleteWordForward"===u?r.pull().push("Control").push("Delete"):"insertLineBreak"===u?r.pull().push("Enter"):"insertText"===u&&o&&(r.toArray().forEach((function(t){return 1===p(t)&&r.pull(t)})),r.push(o)),(e=r.command())&&(!1===(n=r.fire(e))?(function(t){t&&t.preventDefault()}(t),function(t){t&&t.stopPropagation()}(t)):null===n&&console.warn("Unknown command:",e)),c(r,t)}function v(t){var e=y(this),n=t.data,r=t.inputType,o=t.key;"keyup"===t.type?(e[t.altKey?"push":"pull"]("Alt"),e[t.ctrlKey?"push":"pull"]("Control"),e[t.metaKey?"push":"pull"]("Meta"),e[t.shiftKey?"push":"pull"]("Shift"),o&&e.pull(o)):"deleteContentBackward"===r?e.pull("Backspace"):"deleteContentForward"===r?e.pull("Delete"):"deleteWordBackward"===r?e.pull("Control").pull("Backspace"):"deleteWordForward"===r?e.pull("Control").pull("Delete"):"insertLineBreak"===r?e.pull("Enter"):"insertText"===r&&n&&e.pull(n)}var w={attach:function(){var t,n,r=this,o=r.constructor._,l=new u(r);return r.commands=f(r.commands=l.commands,r.state.commands||{}),r.keys=f(r.keys=l.keys,r.state.keys||{}),!e(o.command)&&(o.command=function(t,e){return this.commands[t]=e,this}),!e(o.k)&&(o.k=function(t){var e=y(this).toArray();return!1===t?e:e.join(t||"-")}),!e(o.key)&&(o.key=function(t,e){return this.keys[t]=e,this}),r.on("blur",h),r.on("focus",m),r.on("key.down",k),r.on("key.up",v),r.on("put.down",k),r.on("put.up",v),t=r,n=l,d.set(t,n),r},detach:function(){var t,e=this;return y(e).pull(),e.off("blur",h),e.off("focus",m),e.off("key.down",k),e.off("key.up",v),e.off("put.down",k),e.off("put.up",v),t=e,d.delete(t),e},name:"TextEditor.Key"};return w}));
|
package/index.mjs
CHANGED
@@ -37,17 +37,34 @@ function onFocus(e) {
|
|
37
37
|
onBlur.call(this, e);
|
38
38
|
}
|
39
39
|
|
40
|
-
function
|
41
|
-
let
|
42
|
-
key = e
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
40
|
+
function onKeyDownOrPutDown(e) {
|
41
|
+
let map = getReference(this), command, v,
|
42
|
+
{data, inputType, key, type} = e;
|
43
|
+
if ('keydown' === type) {
|
44
|
+
// Make the `Alt`, `Control`, `Meta`, and `Shift` key(s) sticky (does not require the user to release all key(s) first to repeat or change the current key combination).
|
45
|
+
map[e.altKey ? 'push' : 'pull']('Alt');
|
46
|
+
map[e.ctrlKey ? 'push' : 'pull']('Control');
|
47
|
+
map[e.metaKey ? 'push' : 'pull']('Meta');
|
48
|
+
map[e.shiftKey ? 'push' : 'pull']('Shift');
|
49
|
+
// Add the actual key to the queue. Don’t worry, this will not mistakenly add a key that already exists in the queue.
|
50
|
+
key && map.push(key);
|
51
|
+
} else {
|
52
|
+
if ('deleteContentBackward' === inputType) {
|
53
|
+
map.pull().push('Backspace'); // Simulate `Backspace` key
|
54
|
+
} else if ('deleteContentForward' === inputType) {
|
55
|
+
map.pull().push('Delete'); // Simulate `Delete` key
|
56
|
+
} else if ('deleteWordBackward' === inputType) {
|
57
|
+
map.pull().push('Control').push('Backspace'); // Simulate `Control-Backspace` keys
|
58
|
+
} else if ('deleteWordForward' === inputType) {
|
59
|
+
map.pull().push('Control').push('Delete'); // Simulate `Control-Delete` keys
|
60
|
+
} else if ('insertLineBreak' === inputType) {
|
61
|
+
map.pull().push('Enter'); // Simulate `Enter` key
|
62
|
+
} else if ('insertText' === inputType && data) {
|
63
|
+
// One character at a time
|
64
|
+
map.toArray().forEach(key => 1 === toCount(key) && map.pull(key));
|
65
|
+
map.push(data);
|
66
|
+
}
|
67
|
+
}
|
51
68
|
if (command = map.command()) {
|
52
69
|
v = map.fire(command);
|
53
70
|
if (false === v) {
|
@@ -60,23 +77,29 @@ function onKeyDown(e) {
|
|
60
77
|
bounce(map, e); // Reset all key(s) after 1 second idle.
|
61
78
|
}
|
62
79
|
|
63
|
-
function
|
64
|
-
let
|
65
|
-
key = e
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
+
function onKeyUpOrPutUp(e) {
|
81
|
+
let map = getReference(this),
|
82
|
+
{data, inputType, key, type} = e;
|
83
|
+
if ('keyup' === type) {
|
84
|
+
map[e.altKey ? 'push' : 'pull']('Alt');
|
85
|
+
map[e.ctrlKey ? 'push' : 'pull']('Control');
|
86
|
+
map[e.metaKey ? 'push' : 'pull']('Meta');
|
87
|
+
map[e.shiftKey ? 'push' : 'pull']('Shift');
|
88
|
+
key && map.pull(key);
|
89
|
+
} else {
|
90
|
+
if ('deleteContentBackward' === inputType) {
|
91
|
+
map.pull('Backspace');
|
92
|
+
} else if ('deleteContentForward' === inputType) {
|
93
|
+
map.pull('Delete');
|
94
|
+
} else if ('deleteWordBackward' === inputType) {
|
95
|
+
map.pull('Control').pull('Backspace');
|
96
|
+
} else if ('deleteWordForward' === inputType) {
|
97
|
+
map.pull('Control').pull('Delete');
|
98
|
+
} else if ('insertLineBreak' === inputType) {
|
99
|
+
map.pull('Enter');
|
100
|
+
} else if ('insertText' === inputType && data) {
|
101
|
+
map.pull(data);
|
102
|
+
}
|
80
103
|
}
|
81
104
|
}
|
82
105
|
|
@@ -106,9 +129,10 @@ function attach() {
|
|
106
129
|
});
|
107
130
|
$.on('blur', onBlur);
|
108
131
|
$.on('focus', onFocus);
|
109
|
-
$.on('key.down',
|
110
|
-
$.on('key.up',
|
111
|
-
$.on('put.down',
|
132
|
+
$.on('key.down', onKeyDownOrPutDown);
|
133
|
+
$.on('key.up', onKeyUpOrPutUp);
|
134
|
+
$.on('put.down', onKeyDownOrPutDown);
|
135
|
+
$.on('put.up', onKeyUpOrPutUp);
|
112
136
|
return setReference($, map), $;
|
113
137
|
}
|
114
138
|
|
@@ -118,9 +142,10 @@ function detach() {
|
|
118
142
|
map.pull();
|
119
143
|
$.off('blur', onBlur);
|
120
144
|
$.off('focus', onFocus);
|
121
|
-
$.off('key.down',
|
122
|
-
$.off('key.up',
|
123
|
-
$.off('put.down',
|
145
|
+
$.off('key.down', onKeyDownOrPutDown);
|
146
|
+
$.off('key.up', onKeyUpOrPutUp);
|
147
|
+
$.off('put.down', onKeyDownOrPutDown);
|
148
|
+
$.off('put.up', onKeyUpOrPutUp);
|
124
149
|
return letReference($), $;
|
125
150
|
}
|
126
151
|
|
package/package.json
CHANGED