Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

KeyBindingUtil: add usesMacOSHeuristics method #869

Conversation

williamboman
Copy link
Contributor

@williamboman williamboman commented Dec 13, 2016

Summary

Add a public method, usesMacOSHeuristics, to the KeyBindingUtil module. This makes it possible to determine how draft-js will interpret certain keyboard events and thus alter the UI accordingly, e.g.;

screenshot 2016-12-13 at 17 58 33

I currently achieve this by doing (🙈);

const COMMAND_MODIFIER_CHAR = (navigator && navigator.userAgent.indexOf('Mac') >= 0)
    ? '\u2318' // Mac command key character
    : 'Ctrl'

@hellendag
Copy link

I'm curious about the context of your needs for COMMAND_MODIFIER_CHAR. Can you provide a snippet?

I would normally expect hasCommandModifier to solve the needs of determining whether a key event matches this use case.

@williamboman
Copy link
Contributor Author

@hellendag I need to be able to determine which key will be used as "command modifier" before any key event takes place.

In the example I posted in the OP, I check for KeyBindingUtil.hasCommandModifier(e) && e.keyCode === 13 and submit the form. I also want to display this shortcut in the interface (see screenshot in OP). As the command modifier, as of now, differs between macOS and other OSes I want to display the correct character in the UI. Hence COMMAND_MODIFIER_CHAR.

@thibaudcolas
Copy link
Contributor

thibaudcolas commented Jan 16, 2017

I've had two use cases for access to isOSX:

  • Implementing shortcuts that rely on combinations of ctrl/cmd and alt modifiers, like the Google Docs shortcuts for heading levels (eg. alt + cmd + 1)
  • Displaying the relevant combination of keys in the UI, like @williamboman.

I was able to access it in my implementation by abusing the implementation of isOptionKeyCommand. This is dirty, and will break whenever the implementation is changed to coerce the result to boolean, but it works in 0.9.1.

Edit: tested in versions 0.10.0 to 0.10.4.

const isOSX = isOptionKeyCommand({ altKey: 'test' }) === 'test';

@thibaudcolas
Copy link
Contributor

👋 Is there anything I can do to help get this in? As far as I can see this is still the only way to display correct keyboard shortcuts in editors.

Here is another example to complement @williamboman's:

keyboard-shortcut

It's available at https://springload.github.io/draftail/ if that helps.

@niveditc
Copy link
Contributor

niveditc commented Sep 8, 2018

@williamboman, @thibaudcolas - I think this is fine to add in, but I want to first make sure that someone still has a need for this additional API method. Please comment here if adding this will help your project & I can then merge it in - thanks! :)

@williamboman williamboman force-pushed the feat/KeyBindingUtil-macOSHeuristics branch from d5c81ea to 73530ad Compare September 8, 2018 21:00
@williamboman williamboman force-pushed the feat/KeyBindingUtil-macOSHeuristics branch from 73530ad to 74216fa Compare September 8, 2018 21:05
@williamboman
Copy link
Contributor Author

williamboman commented Sep 8, 2018

@williamboman, @thibaudcolas - I think this is fine to add in, but I want to first make sure that someone still has a need for this additional API method. Please comment here if adding this will help your project & I can then merge it in - thanks! :)

Won't make much difference for me rn, I don't use draft-js much anymore. If I ever pick it up again I'm sure I'd have use for this - so it would not surprise me if more people has use for this. I've rebased on top of latest master.

Copy link
Contributor

@niveditc niveditc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

Copy link

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

niveditc is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@thibaudcolas
Copy link
Contributor

Nice! I'll definitely be using this in my editor as soon as it is released. Same use case as before – displaying the right shortcut depending on the user's OS. See https://github.com/springload/draftail/blob/d7fcb93c09204467a76fbc8abe4f7073cc052bc9/lib/api/behavior.js#L23-L25.

jdecked pushed a commit to twitter-forks/draft-js that referenced this pull request Oct 9, 2019
Summary:
**Summary**

Add a public method, `usesMacOSHeuristics`, to the `KeyBindingUtil` module. This makes it possible to determine how draft-js will interpret certain keyboard events and thus alter the UI accordingly, e.g.;

<img width="499" alt="screenshot 2016-12-13 at 17 58 33" src="https://cloud.githubusercontent.com/assets/6705160/21150013/dee64566-c15d-11e6-9639-75a39d21d079.png">

I currently achieve this by doing (🙈);

```js
const COMMAND_MODIFIER_CHAR = (navigator && navigator.userAgent.indexOf('Mac') >= 0)
    ? '\u2318' // Mac command key character
    : 'Ctrl'
```
Pull Request resolved: facebookarchive#869

Differential Revision: D9734962

fbshipit-source-id: ed3f3d0a87df51c708fd666fe85bc6acba1ecfee
alicayan008 pushed a commit to alicayan008/draft-js that referenced this pull request Jul 4, 2023
Summary:
**Summary**

Add a public method, `usesMacOSHeuristics`, to the `KeyBindingUtil` module. This makes it possible to determine how draft-js will interpret certain keyboard events and thus alter the UI accordingly, e.g.;

<img width="499" alt="screenshot 2016-12-13 at 17 58 33" src="https://cloud.githubusercontent.com/assets/6705160/21150013/dee64566-c15d-11e6-9639-75a39d21d079.png">

I currently achieve this by doing (🙈);

```js
const COMMAND_MODIFIER_CHAR = (navigator && navigator.userAgent.indexOf('Mac') >= 0)
    ? '\u2318' // Mac command key character
    : 'Ctrl'
```
Pull Request resolved: facebookarchive/draft-js#869

Differential Revision: D9734962

fbshipit-source-id: ed3f3d0a87df51c708fd666fe85bc6acba1ecfee
aforismesen added a commit to aforismesen/draft-js that referenced this pull request Jul 12, 2024
Summary:
**Summary**

Add a public method, `usesMacOSHeuristics`, to the `KeyBindingUtil` module. This makes it possible to determine how draft-js will interpret certain keyboard events and thus alter the UI accordingly, e.g.;

<img width="499" alt="screenshot 2016-12-13 at 17 58 33" src="https://cloud.githubusercontent.com/assets/6705160/21150013/dee64566-c15d-11e6-9639-75a39d21d079.png">

I currently achieve this by doing (🙈);

```js
const COMMAND_MODIFIER_CHAR = (navigator && navigator.userAgent.indexOf('Mac') >= 0)
    ? '\u2318' // Mac command key character
    : 'Ctrl'
```
Pull Request resolved: facebookarchive/draft-js#869

Differential Revision: D9734962

fbshipit-source-id: ed3f3d0a87df51c708fd666fe85bc6acba1ecfee
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants