mobiledoc-kit icon indicating copy to clipboard operation
mobiledoc-kit copied to clipboard

ensure `onTextInput` handlers can toggle markup

Open bantic opened this issue 9 years ago • 3 comments

See #457 — the onTextInput callback is run before mobiledoc's selection-polling finishes, so attempts to postEditor.toggleMarkup inside the handler get ignored a moment later when the selection-polling reads the existing markup at the cursor position.

Two possibilities to fix this are:

  • ensure the callback is run after the selection settles (async)
  • change text-insertion code to not reread the active markups after insertion (it already knows what markup exists at the cursor position)

In the meantime, the best is to use requestAnimationFrame(() => editor.toggleMarkup('...')).

bantic avatar Sep 01 '16 14:09 bantic

Is this issue related to async operations not working in onTextInput#run e.g. https://ember-twiddle.com/bdad2c72148711f00dbebed4adf1ad27?openFiles=templates.components.template-tags-input.hbs%2Ctemplates.components.template-tag.hbs

knownasilya avatar Apr 05 '17 14:04 knownasilya

@knownasilya Your twiddle there is something different (I think). editor.insertText(...) is a no-op when the editor doesn't have an active cursor (that code is here) — the hasCursor check looks to see if the window has a selection within the editor's surface area (see here).

The issue in your twiddle isn't due to async, but rather the fact that the window's selection is gone after the user clicks on one of the items in the popup. One thing you could do instead is to keep a reference to the current cursor position before the editor loses focus, and then restore that range before inserting the text. Here's an updated fiddle that does so: https://ember-twiddle.com/f6d6d04fcb21f06e5cdae1677e7023ce?openFiles=components.template-tags-input.js%2Ctemplates.components.template-tag.hbs

Perhaps it makes some sense to hold on to the last-known cursor position in an editor and attempt to insert text at that point, rather than no-op-ing altogether in editor.insertText...

bantic avatar Apr 06 '17 20:04 bantic

Related: #639

sdhull avatar Oct 01 '18 18:10 sdhull