raw icon indicating copy to clipboard operation
raw copied to clipboard

Bug in Firefox when dynamically populating the field

Open arokanto opened this issue 5 years ago • 0 comments

There's a weird bug on Firefox (v81) where movement keys jump to next/previous block and not next/previous character. This happens only if you dynamically load content to Editorjs and one of the blocks is a raw block.

Steps to reproduce:

  1. Load the Editorjs with this code:
<script src="https://cdn.jsdelivr.net/npm/@editorjs/editorjs@latest"></script>
<script src="https://cdn.jsdelivr.net/npm/@editorjs/raw@latest"></script>

<div id="editorjs"></div>

<script>
  const editor = new EditorJS({
    tools: {
      raw: RawTool
    },
    data: {
      blocks : [
        {
          type : "paragraph",
          data : {
            text : "Lorem ipsum"
          }
        },
        {
          type : "raw",
          data : {
            html : "Whatevs"
          }
        }
      ]
    }
  })
</script>
  1. Click the end of the Whatevs text with latest Firefox and press arrow left on keyboard. The cursor moves to previous block instead of moving to between letters v and s.

If I create a new Editorjs without prepopulating data and recreate those fields, this does not happen and moving the cursor using arrow keys in raw textarea works as it should.

This bug is more than mere annoyance, because it happens also when using backspace to delete characters. The cursor jumps to previous block and consecutive key presses remove characters from there instead of the ones the user intended to remove.

I'm not sure if this is a bug in Editorjs or this plugin, but you can prevent this behaviour by adding the following to this plugin's drawView() function:

textarea.addEventListener('keydown', function (event) {
  const blockKeys = ['ArrowRight', 'ArrowLeft', 'Backspace'];

  if (blockKeys.includes(event.key)) {
    event.stopPropagation();
  }
});

But this might have some unwanted side effects. There are some open bugs in Bugzilla related to Firefox's textarea that do not exist on other browsers, so the easier route might be using <div contenteditable="true"> instead of <textarea>, like in paragraph plugin.

arokanto avatar Sep 11 '20 11:09 arokanto