From f61ba2f88299fe9d9cf43e383d3b2b876a189532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rahir?= Date: Thu, 22 Dec 2022 10:02:01 +0000 Subject: [PATCH] [IMP] mockContentHelper: use focus to change window.mockContentHelper This avoid having to rely on the obscure selectRange method. Odoo task 2882521 Part-of: odoo/o-spreadsheet#3456 --- src/components/composer/composer/composer.ts | 1 + src/components/composer/content_editable_helper.ts | 5 +++-- tests/components/__mocks__/content_editable_helper.ts | 6 ++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/composer/composer/composer.ts b/src/components/composer/composer/composer.ts index e6179d5224..6203c60e26 100644 --- a/src/components/composer/composer/composer.ts +++ b/src/components/composer/composer/composer.ts @@ -422,6 +422,7 @@ export class Composer extends Component { this.shouldProcessInputEvents = false; if (this.props.focus !== "inactive") { + this.contentHelper.el.focus(); this.contentHelper.selectRange(0, 0); // move the cursor inside the composer at 0 0. } const content = this.getContent(); diff --git a/src/components/composer/content_editable_helper.ts b/src/components/composer/content_editable_helper.ts index ca69bd7af4..d5437514b1 100644 --- a/src/components/composer/content_editable_helper.ts +++ b/src/components/composer/content_editable_helper.ts @@ -1,9 +1,10 @@ import { HtmlContent } from "./composer/composer"; export class ContentEditableHelper { + // todo make el private and expose dedicated methods el: HTMLElement; - constructor(el: HTMLElement | null) { - this.el = el!; + constructor(el: HTMLElement) { + this.el = el; } updateEl(el: HTMLElement) { diff --git a/tests/components/__mocks__/content_editable_helper.ts b/tests/components/__mocks__/content_editable_helper.ts index cd3ef36194..dfe3748c66 100644 --- a/tests/components/__mocks__/content_editable_helper.ts +++ b/tests/components/__mocks__/content_editable_helper.ts @@ -27,10 +27,6 @@ export class ContentEditableHelper { this.colors = {}; } selectRange(start: number, end: number) { - // TODO: find a way not to depend on selectRange to gain focus and push mockContentHelper - this.el!.focus(); - // @ts-ignore - window.mockContentHelper = this; this.manualRange = true; // We cannot set the cursor position beyond the text of the editor if (!this.el || !this.el.textContent || start < 0 || end > this.el.textContent.length) return; @@ -100,6 +96,8 @@ export class ContentEditableHelper { private attachEventHandlers() { if (this.el === null) return; this.el.addEventListener("keydown", (ev: KeyboardEvent) => this.onKeyDown(this.el!, ev)); + // @ts-ignore + this.el.addEventListener("focus", (ev: FocusEvent) => (window.mockContentHelper = this)); } /**