question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"extraKey" property on React component erases CodeMirror default key map behavior

See original GitHub issue

Current Behavior

When a user defines the extraKey property on the <ReactSimpleMDE /> component, it overwrites/erases the default key handler behavior for the entire component such that the editor stops reacting to events like Cmd-P (Preview) and F11 (Full screen editor), among others.

Questions

  1. Is this intended behavior? It seems from the CodeMirror documentation the extraKeys property is meant to add keymaps in addition to the default keymap. See documentation here:

    Can be used to specify extra key bindings for the editor, alongside the ones defined by keyMap.

  2. (Assuming the answer to #1 is “no”) Is this behavior that react-simplemde-editor is causing, or is it a downstream dependency (i.e. - CodeMirror or SimpleMDE)?

Workaround

Set the SimpleMDE getInstance to capture the ref, then add your own keymap on top of what the default codemirror behavior is:

// Handler
getInstance = (simplemde) => {
  simplemde.codemirror.addKeyMap({
    Up: (cm) => {
      cm.replaceSelection(" surprise. ");
    },
    Down: (cm) => {
      cm.replaceSelection(" surprise again! ");
    }
  });
}

// JSX
<SimpleMDE
  getMdeInstance={getInstance}
  {...otherProps}
/>

CodeMirror Repro

Here is the problem in action (also viewable here): https://codesandbox.io/s/compassionate-fire-db63m

And here is the problem fixed by my workaround: https://codesandbox.io/s/dark-mountain-51kuy

Here is a screenshot of the problematic behavior in the online sample referenced in the README

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
RIP21commented, Apr 2, 2021

I really need to spent few hours one day addressing those… Sorry, I’m building a startup as a CTO and Co-founder (basically doing all the shit in the world) now and I barely have time for myself let alone some library that I don’t use for last 3 years 😃 So, I cannot promise, but I’ll try do smth today (will be fun to read this 3 years after while this issue will be still open 😄) But I’ll try folks, I’ll try to. Maybe it’s time to rewrite it using hooks? Will see.

0reactions
RIP21commented, Apr 22, 2021

4.1.4 has this fixed. Just published.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CodeMirror 5 User Manual
The default is "default" , which is the only key map defined in codemirror.js itself. Extra key maps are found in the key...
Read more >
@uiw/react-codemirror - npm
Start using @uiw/react-codemirror in your project by running `npm i ... The default is "default", which is the only keymap defined in ...
Read more >
CodeMirror: User Manual
The default is "default" , which is the only key map defined in codemirror.js itself. Extra key maps are found in the key...
Read more >
CodeMirror: User Manual - Tricefy Help Center
CodeMirror is a code-editor component that can be embedded in Web pages. ... The default is "default" , which is the only keymap...
Read more >
Detect new line in codemirror, without overriding default ...
Returning CodeMirror.Pass from a key handler will cause the default behavior (or the next keyhandler) to be run after your handler returns.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found