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

useCapture for block tool keydown event? #1386

Closed
tommiv opened this issue Oct 16, 2020 · 2 comments · Fixed by #1563
Closed

useCapture for block tool keydown event? #1386

tommiv opened this issue Oct 16, 2020 · 2 comments · Fixed by #1563

Comments

@tommiv
Copy link

tommiv commented Oct 16, 2020

Question
The Changelog of this project states that in v2.18 the following change was made:

Improvements - Disabled useCapture flag for a block keydown handling. That will allow plugins to override keydown and stop event propagation, for example, to make own Tab behavior.

However, in v2.19 (I migrated straight from v2.17 to v2.19) it seems that this is not true. I made a custom block tool for code listings which includes a textarea element. I've attached a simple handler to it like this:

render() {
    ...
    this.nodeListing = document.createElement('textarea');
    this.nodeListing.addEventListener('keydown', this.onListingKeydown);
}

...

  onListingKeydown(e: KeyboardEvent) {
    if (e.code !== 'Tab' && e.code !== 'Escape') {
      return;
    }

    e.stopPropagation();
    e.preventDefault();
    console.log(e);
  }

This code half works: preventDefault works well but stopPropagation doesn't.
So I used the "Event Listeners" tab of Chrome's debugger and I figured out that editor.js attached keydown event listeners to every .ce-block element and useCapture set to true. The listener callback link points to editor.js:2 (minified version) but after beautification it seems that Chrome was trying to show me this:

So, where is the truth? Was that feature of v2.18 rolled back in v2.19, or I misunderstood what changelog says or it's a bug in implementation?

@hata6502
Copy link
Contributor

hata6502 commented Feb 3, 2021

test code

/src/components/tools/paragraph/src/index.js

  drawView() {
    let div = document.createElement('DIV');

    div.classList.add(this._CSS.wrapper, this._CSS.block);
    div.contentEditable = false;
    div.dataset.placeholder = this.api.i18n.t(this._placeholder);

    if (!this.readOnly) {
      div.contentEditable = true;
      div.addEventListener('keyup', this.onKeyUp);
    }

    div.addEventListener('keydown', (event) => {
      event.preventDefault();
      event.stopPropagation();

      console.log('paragraph test');
    });

    return div;
  }

@hata6502
Copy link
Contributor

hata6502 commented Feb 3, 2021

Really, BlockManager uses capture flag.

src/components/modules/blockManager.ts

  private bindBlockEvents(block: Block): void {
    const { BlockEvents } = this.Editor;

    this.readOnlyMutableListeners.on(block.holder, 'keydown', (event: KeyboardEvent) => {
      BlockEvents.keydown(event);
    }, true);

v2.18.0, capture flag is false: https://github.com/codex-team/editor.js/pull/1181/files#diff-73cbc05ddd84b0686ee89460cb98dfc39099c8472677e743192f301e26c2347cR678

But, v2.19.0, capture flag is true: 7877570#diff-73cbc05ddd84b0686ee89460cb98dfc39099c8472677e743192f301e26c2347cR710

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

Successfully merging a pull request may close this issue.

3 participants