Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add editor hook to listen for entered text #367

Closed
bantic opened this issue Apr 20, 2016 · 3 comments · Fixed by #368
Closed

add editor hook to listen for entered text #367

bantic opened this issue Apr 20, 2016 · 3 comments · Fixed by #368

Comments

@bantic
Copy link
Collaborator

bantic commented Apr 20, 2016

In order to implement something like a system where typing "@" triggers an editor event that a consumer can listen to and then later replace with an atom (to represent that @-mention in some semantic/metadata-y sense).

The sort of flexible input configuration that Mousetrap uses seems like a good path.

This system should handle:

  • inserted text (if the user types "abc", or "@" e.g.) — callback should be invoked after the text is inserted
  • keydown or keyup with and without modifiers ("command+k") — callback should be invoked on key down/up event and cancel default behavior

In the callback the following information should be available:

  • cursor position (probably just read this from editor.range)
  • have access to the editor
  • be able to specify that other listeners for the same text/key-combo should fire (by default only the first listener would likely fire)
@bantic
Copy link
Collaborator Author

bantic commented Apr 21, 2016

The current hooks the editor exposes for reacting to use key- and text- related events are:

  • registerTextExpansion — these take required "text" and "trigger key" args (e.g.: text "*" and trigger <SPACE>). The callback (which is passed as the "run" key) is invoked when the trigger key is pressed after the "text" is entered at the start of the section.
  • registerKeyCommand — these take a required "str" arg that is similar to Mousetrap style (e.g. "META+K"). The callback is invoked when the key-combination keyDown event happens

@bantic
Copy link
Collaborator Author

bantic commented Apr 21, 2016

After thinking it over, there are two distinct (though related) concerns — reacting to text the user typed and a key-combination the user pressed. Specifying whether to trigger on key-up or -down only applies to key-combos, and options like atStart: true|false only make sense to text-insertions. In that light it makes sense to keep them as separate hooks.

To address the use case described in this issue I'm going to change the text expansion hooks in the editor to be more flexible. They can be updated to make the trigger optional, and to accept an atStart prop that determines whether they will fire any time or only when the text is at the start of the section.

E.g.:

// react to @-mentions
editor.registerTextExpansion({
  text: "@",
  // no explicit trigger. This expansion will trigger after user types "@" (but will leave the "@" text in the editor)
  atStart: false,  // any time the text is entered,
  run: callbackFn // this callback would likely delete the "@" text and replace it with a "mention" atom
});

// expand "*" at the start of a section into a list section (this is an existing built-in expansion)
editor.registerTextExpansion({
  text: "*",
  trigger: " ", // if the user types a " " after the "*", this expansion will be invoked — only the "*" text will be in the editor because the expansion will capture and cancel the keypress for the " " char
  atStart: true,
  run: callbackThatTogglesSectionToListItem
});

cc @rlivsey let me know if this makes sense to you

bantic added a commit that referenced this issue Apr 21, 2016
Deprecate `Editor#registerTextExpansion` in favor of `onTextInput`

Fixes #367
bantic added a commit that referenced this issue Apr 21, 2016
Deprecate `Editor#registerTextExpansion` in favor of `onTextInput`

Fixes #367
@rlivsey
Copy link
Collaborator

rlivsey commented Apr 25, 2016

@bantic looks great to me!

bantic added a commit that referenced this issue Apr 26, 2016
Deprecate `Editor#registerTextExpansion` in favor of `onTextInput`

Fixes #367
bantic added a commit that referenced this issue Apr 26, 2016
Deprecate `Editor#registerTextExpansion` in favor of `onTextInput`

Fixes #367
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants