on master help sign in
Handle cut, paste, undo, and other changes that avoid onkeypress
1b098b9Evan Czaplicki
libraries/Native/Graphics/Input.js+31−4
@@ -222,9 +222,6 @@ Elm.Native.Graphics.Input.make = function(elm) {
222 field.style.backgroundColor = 'transparent';
222 field.style.backgroundColor = 'transparent';
223 field.style.pointerEvents = 'auto';
223 field.style.pointerEvents = 'auto';
224
224
225− field.elm_signal = model.signal;
226− field.elm_handler = model.handler;
227−
228 field.type = model.type;
225 field.type = model.type;
229 field.placeholder = JS.fromString(model.placeHolder);
226 field.placeholder = JS.fromString(model.placeHolder);
230 field.value = JS.fromString(model.content.string);
227 field.value = JS.fromString(model.content.string);
@@ -232,6 +229,10 @@ Elm.Native.Graphics.Input.make = function(elm) {
232 var direction = selection.direction.ctor === 'Forward' ? 'forward' : 'backward';
229 var direction = selection.direction.ctor === 'Forward' ? 'forward' : 'backward';
233 setRange(field, selection.start, selection.end, direction);
230 setRange(field, selection.start, selection.end, direction);
234
231
232+ field.elm_signal = model.signal;
233+ field.elm_handler = model.handler;
234+ field.elm_old_value = field.value;
235+
235 function keyUpdate(event) {
236 function keyUpdate(event) {
236 var curr = field.value;
237 var curr = field.value;
237 var next = (curr.slice(0, field.selectionStart) +
238 var next = (curr.slice(0, field.selectionStart) +
@@ -250,6 +251,29 @@ Elm.Native.Graphics.Input.make = function(elm) {
250 event.preventDefault();
251 event.preventDefault();
251 }
252 }
252
253
254+ function inputUpdate(event) {
255+ var curr = field.elm_old_value;
256+ var next = field.value;
257+ if (curr === next) {
258+ return;
259+ }
260+
261+ var direction = field.selectionDirection === 'forward' ? 'Forward' : 'Backward';
262+ var start = field.selectionStart;
263+ var end = field.selectionEnd;
264+ field.value = field.elm_old_value;
265+
266+ elm.notify(field.elm_signal.id, field.elm_handler({
267+ _:{},
268+ string: JS.toString(next),
269+ selection: {
270+ start: start,
271+ end: end,
272+ direction: { ctor: direction }
273+ },
274+ }));
275+ }
276+
253 function mouseUpdate(event) {
277 function mouseUpdate(event) {
254 var direction = field.selectionDirection === 'forward' ? 'Forward' : 'Backward';
278 var direction = field.selectionDirection === 'forward' ? 'Forward' : 'Backward';
255 elm.notify(field.elm_signal.id, field.elm_handler({
279 elm.notify(field.elm_signal.id, field.elm_handler({
@@ -271,6 +295,7 @@ Elm.Native.Graphics.Input.make = function(elm) {
271 elm.node.removeEventListener('mouseup', mouseup)
295 elm.node.removeEventListener('mouseup', mouseup)
272 }
296 }
273 field.addEventListener('keypress', keyUpdate);
297 field.addEventListener('keypress', keyUpdate);
298+ field.addEventListener('input', inputUpdate);
274 field.addEventListener('mousedown', mousedown);
299 field.addEventListener('mousedown', mousedown);
275
300
276 return field;
301 return field;
@@ -282,7 +307,9 @@ Elm.Native.Graphics.Input.make = function(elm) {
282
307
283 field.type = newModel.type;
308 field.type = newModel.type;
284 field.placeholder = JS.fromString(newModel.placeHolder);
309 field.placeholder = JS.fromString(newModel.placeHolder);
285− field.value = JS.fromString(newModel.content.string);
310+ var value = JS.fromString(newModel.content.string);
311+ field.value = value;
312+ field.elm_old_value = value;
286 var selection = newModel.content.selection;
313 var selection = newModel.content.selection;
287 var direction = selection.direction.ctor === 'Forward' ? 'forward' : 'backward';
314 var direction = selection.direction.ctor === 'Forward' ? 'forward' : 'backward';
288 setRange(field, selection.start, selection.end, direction);
315 setRange(field, selection.start, selection.end, direction);