fregante / text-field-edit

Insert text in a <textarea> and <input> (supports Firefox and Undo)
https://npm.im/text-field-edit
MIT License
73 stars 3 forks source link

`execCommand` is asynchronous? #21

Open fregante opened 1 year ago

fregante commented 1 year ago

See https://github.com/refined-github/refined-github/issues/6348#issuecomment-1522208858

Maybe it's only Chromium. I wonder if reading field.value immediately after forces a synchronous update.

fregante commented 7 months ago

.focus() might be asynchronous:

https://stackoverflow.com/questions/6139107/programmatically-select-text-in-a-contenteditable-html-element#comment22508602_6150060

twschiller commented 7 months ago

FWIW, Chromium's tests for insertText look synchronous: https://chromium.googlesource.com/chromium/blink.git/+/refs/heads/main/LayoutTests/editing/execCommand/insert-text-not-inheriting-block-properties.html

Presumably, an async focus would look something like (not sure what you're want "refocus" handling to be):

async focus(element, {maxWait}) {
  if (document.activeElement === element) return;
  const deferred = pDefer();
  element.addListener("onfocus", () => {
     assert document.activeElement === element;
     deferred.resolve();
  }, {once: true});
  element.focus();
  return deferred.promise;
}
fregante commented 7 months ago

It's kind of unfortunate because the module now is synchronous, but with that I'd have to make every call async. But it looks about right