mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 17:30:09 +02:00
[Editor] Avoid calling setTimeout when editing an existing freetext
The code was added in order to guess if an editor has been moved but it's simpler to just set a variable when it's dragged or moved with the keyboard. This way we remove a bit of asynchronicity.
This commit is contained in:
parent
5adad89eb3
commit
3b93fdea26
2 changed files with 16 additions and 22 deletions
|
@ -62,6 +62,8 @@ class AnnotationEditor {
|
||||||
|
|
||||||
#hasBeenClicked = false;
|
#hasBeenClicked = false;
|
||||||
|
|
||||||
|
#initialPosition = null;
|
||||||
|
|
||||||
#isEditing = false;
|
#isEditing = false;
|
||||||
|
|
||||||
#isInEditMode = false;
|
#isInEditMode = false;
|
||||||
|
@ -445,6 +447,8 @@ class AnnotationEditor {
|
||||||
* @param {number} y - y-translation in screen coordinates.
|
* @param {number} y - y-translation in screen coordinates.
|
||||||
*/
|
*/
|
||||||
translate(x, y) {
|
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);
|
this.#translate(this.parentDimensions, x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -455,11 +459,13 @@ class AnnotationEditor {
|
||||||
* @param {number} y - y-translation in page coordinates.
|
* @param {number} y - y-translation in page coordinates.
|
||||||
*/
|
*/
|
||||||
translateInPage(x, y) {
|
translateInPage(x, y) {
|
||||||
|
this.#initialPosition ||= [this.x, this.y];
|
||||||
this.#translate(this.pageDimensions, x, y);
|
this.#translate(this.pageDimensions, x, y);
|
||||||
this.div.scrollIntoView({ block: "nearest" });
|
this.div.scrollIntoView({ block: "nearest" });
|
||||||
}
|
}
|
||||||
|
|
||||||
drag(tx, ty) {
|
drag(tx, ty) {
|
||||||
|
this.#initialPosition ||= [this.x, this.y];
|
||||||
const [parentWidth, parentHeight] = this.parentDimensions;
|
const [parentWidth, parentHeight] = this.parentDimensions;
|
||||||
this.x += tx / parentWidth;
|
this.x += tx / parentWidth;
|
||||||
this.y += ty / parentHeight;
|
this.y += ty / parentHeight;
|
||||||
|
@ -492,6 +498,14 @@ class AnnotationEditor {
|
||||||
this.div.scrollIntoView({ block: "nearest" });
|
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.
|
* Get the translation to take into account the editor border.
|
||||||
* The CSS engine positions the element by taking the border into account so
|
* The CSS engine positions the element by taking the border into account so
|
||||||
|
|
|
@ -357,7 +357,6 @@ class FreeTextEditor extends AnnotationEditor {
|
||||||
/** @inheritdoc */
|
/** @inheritdoc */
|
||||||
onceAdded() {
|
onceAdded() {
|
||||||
if (this.width) {
|
if (this.width) {
|
||||||
this.#cheatInitialRect();
|
|
||||||
// The editor was created in using ctrl+c.
|
// The editor was created in using ctrl+c.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -844,35 +843,16 @@ class FreeTextEditor extends AnnotationEditor {
|
||||||
}
|
}
|
||||||
|
|
||||||
#hasElementChanged(serialized) {
|
#hasElementChanged(serialized) {
|
||||||
const { value, fontSize, color, rect, pageIndex } = this.#initialData;
|
const { value, fontSize, color, pageIndex } = this.#initialData;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
this._hasBeenMoved ||
|
||||||
serialized.value !== value ||
|
serialized.value !== value ||
|
||||||
serialized.fontSize !== fontSize ||
|
serialized.fontSize !== fontSize ||
|
||||||
serialized.rect.some((x, i) => Math.abs(x - rect[i]) >= 1) ||
|
|
||||||
serialized.color.some((c, i) => c !== color[i]) ||
|
serialized.color.some((c, i) => c !== color[i]) ||
|
||||||
serialized.pageIndex !== pageIndex
|
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 };
|
export { FreeTextEditor };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue