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

[Editor] Avoid calling setTimeout when editing an existing freetext #17903

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/display/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class AnnotationEditor {

#hasBeenClicked = false;

#initialPosition = null;

#isEditing = false;

#isInEditMode = false;
Expand Down Expand Up @@ -445,6 +447,8 @@ class AnnotationEditor {
* @param {number} y - y-translation in screen coordinates.
*/
translate(x, y) {
// We don't change the initial position because the move here hasn't been
// done by the user.
this.#translate(this.parentDimensions, x, y);
}

Expand All @@ -455,11 +459,13 @@ class AnnotationEditor {
* @param {number} y - y-translation in page coordinates.
*/
translateInPage(x, y) {
this.#initialPosition ||= [this.x, this.y];
calixteman marked this conversation as resolved.
Show resolved Hide resolved
this.#translate(this.pageDimensions, x, y);
this.div.scrollIntoView({ block: "nearest" });
}

drag(tx, ty) {
this.#initialPosition ||= [this.x, this.y];
const [parentWidth, parentHeight] = this.parentDimensions;
this.x += tx / parentWidth;
this.y += ty / parentHeight;
Expand Down Expand Up @@ -492,6 +498,14 @@ class AnnotationEditor {
this.div.scrollIntoView({ block: "nearest" });
}

get _hasBeenMoved() {
return (
!!this.#initialPosition &&
(this.#initialPosition[0] !== this.x ||
this.#initialPosition[1] !== this.y)
);
}

/**
* Get the translation to take into account the editor border.
* The CSS engine positions the element by taking the border into account so
Expand Down
24 changes: 2 additions & 22 deletions src/display/editor/freetext.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ class FreeTextEditor extends AnnotationEditor {
/** @inheritdoc */
onceAdded() {
if (this.width) {
this.#cheatInitialRect();
// The editor was created in using ctrl+c.
return;
}
Expand Down Expand Up @@ -844,35 +843,16 @@ class FreeTextEditor extends AnnotationEditor {
}

#hasElementChanged(serialized) {
const { value, fontSize, color, rect, pageIndex } = this.#initialData;
const { value, fontSize, color, pageIndex } = this.#initialData;

return (
this._hasBeenMoved ||
serialized.value !== value ||
serialized.fontSize !== fontSize ||
serialized.rect.some((x, i) => Math.abs(x - rect[i]) >= 1) ||
serialized.color.some((c, i) => c !== color[i]) ||
serialized.pageIndex !== pageIndex
);
}

#cheatInitialRect(delayed = false) {
// The annotation has a rect but the editor has an other one.
// When we want to know if the annotation has changed (e.g. has been moved)
// we must compare the editor initial rect with the current one.
// So this method is a hack to have a way to compare the real rects.
if (!this.annotationElementId) {
return;
}

this.#setEditorDimensions();
if (!delayed && (this.width === 0 || this.height === 0)) {
setTimeout(() => this.#cheatInitialRect(/* delayed = */ true), 0);
return;
}

const padding = FreeTextEditor._internalPadding * this.parentScale;
this.#initialData.rect = this.getRect(padding, padding);
}
}

export { FreeTextEditor };