Skip to content

Commit

Permalink
[IMP] mockContentHelper: use focus to change window.mockContentHelper
Browse files Browse the repository at this point in the history
This avoid having to rely on the obscure selectRange method.

Odoo task 2882521

Part-of: #3456
  • Loading branch information
rrahir committed Feb 6, 2024
1 parent f8e539f commit f61ba2f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/components/composer/composer/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ export class Composer extends Component<Props, SpreadsheetChildEnv> {
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();
Expand Down
5 changes: 3 additions & 2 deletions src/components/composer/content_editable_helper.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
6 changes: 2 additions & 4 deletions tests/components/__mocks__/content_editable_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

/**
Expand Down

0 comments on commit f61ba2f

Please sign in to comment.