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

Fix Shift or Ctrl Key cause the drawn line is selected with additional line and can't be removed #18496

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
89 changes: 55 additions & 34 deletions src/display/editor/ink.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@

static _editorType = AnnotationEditorType.INK;

#isDrawingStraightLine = false;

constructor(params) {
super({ ...params, name: "inkEditor" });
this.color = params.color || null;
Expand Down Expand Up @@ -393,6 +395,8 @@
this.opacity ??= InkEditor._defaultOpacity;
}
this.currentPath.push([x, y]);
this.#currentPath2D = new Path2D();
this.#currentPath2D.moveTo(x, y);
this.#hasSomethingToDraw = false;
this.#setStroke();

Expand All @@ -411,36 +415,35 @@
* @param {number} y
*/
#draw(x, y) {
const [lastX, lastY] = this.currentPath.at(-1);
if (this.currentPath.length > 1 && x === lastX && y === lastY) {
return;
}
const currentPath = this.currentPath;
let path2D = this.#currentPath2D;
currentPath.push([x, y]);
this.#hasSomethingToDraw = true;

Check failure on line 418 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Delete `⏎`
if (currentPath.length <= 2) {
path2D.moveTo(...currentPath[0]);
path2D.lineTo(x, y);
return;
}

if (currentPath.length === 3) {
this.#currentPath2D = path2D = new Path2D();
path2D.moveTo(...currentPath[0]);
if (this.#isDrawingStraightLine) {
const [startX, startY] = this.currentPath[0];
this.currentPath = [[startX, startY], [x, y]];

Check failure on line 421 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Replace `[startX,·startY],·[x,·y]` with `⏎········[startX,·startY],⏎········[x,·y],⏎······`
this.#currentPath2D = new Path2D();
this.#currentPath2D.moveTo(startX, startY);
this.#currentPath2D.lineTo(x, y);
} else {

Check failure on line 425 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Delete `·`
const [lastX, lastY] = this.currentPath.at(-1);
if (this.currentPath.length > 1 && x === lastX && y === lastY) {
return;
}
this.currentPath.push([x, y]);
if (this.currentPath.length > 2) {
this.#makeBezierCurve(
this.#currentPath2D,
...this.currentPath.at(-3),
...this.currentPath.at(-2),
x,
y
);
} else {
this.#currentPath2D.lineTo(x, y);
}
}

this.#makeBezierCurve(
path2D,
...currentPath.at(-3),
...currentPath.at(-2),
x,
y
);
}
this.#hasSomethingToDraw = true;
}

Check failure on line 444 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Insert `··`

#endPath() {

Check failure on line 446 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

'#endPath' is defined but never used
if (this.currentPath.length === 0) {
return;
}
Expand All @@ -460,8 +463,8 @@
y = Math.min(Math.max(y, 0), this.canvas.height);

this.#draw(x, y);
this.#endPath();

// this.#endPath();
// this.#isDrawingStraightLine = false;
// Interpolate the path entered by the user with some
// Bezier's curves in order to have a smoother path and
// to reduce the data size used to draw it in the PDF.
Expand All @@ -470,8 +473,8 @@
bezier = this.#generateBezierPoints();
} else {
// We have only one point finally.
const xy = [x, y];
bezier = [[xy, xy.slice(), xy.slice(), xy]];
const [x, y] = this.currentPath[0];

Check failure on line 476 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

'x' is already declared in the upper scope on line 459 column 16

Check failure on line 476 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

'y' is already declared in the upper scope on line 459 column 19
bezier = [[[x, y], [x, y], [x, y], [x, y]]];

Check failure on line 477 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Replace `[[x,·y],·[x,·y],·[x,·y],·[x,·y]]` with `⏎········[⏎··········[x,·y],⏎··········[x,·y],⏎··········[x,·y],⏎··········[x,·y],⏎········],⏎······`
}
const path2D = this.#currentPath2D;
const currentPath = this.currentPath;
Expand Down Expand Up @@ -501,6 +504,10 @@
};

this.addCommands({ cmd, undo, mustExec: true });
this.#isDrawingStraightLine = false;

Check failure on line 508 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Delete `····`
// Re-enable selection

Check failure on line 509 in src/display/editor/ink.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Delete `·`
setTimeout(() => this.parent.setEditingState(true), 0);
}

#drawPoints() {
Expand Down Expand Up @@ -557,8 +564,18 @@

#generateBezierPoints() {
const path = this.currentPath;
if (path.length <= 2) {
return [[path[0], path[0], path.at(-1), path.at(-1)]];

if (!path || path.length === 0) {
return [];
}

if (path.length === 1) {
// If we only have one point, duplicate it to create a tiny line
return [[path[0], path[0], path[0], path[0]]];
}

if (path.length === 2 && this.#isDrawingStraightLine) {
return [[path[0], path[0], path[1], path[1]]];
}

const bezierPoints = [];
Expand All @@ -581,6 +598,7 @@
[x0, y0] = [x3, y3];
}

if (path.length > 2) {
const [x1, y1] = path[i];
const [x2, y2] = path[i + 1];

Expand All @@ -589,6 +607,7 @@
const control2 = [x2 + (2 * (x1 - x2)) / 3, y2 + (2 * (y1 - y2)) / 3];

bezierPoints.push([[x0, y0], control1, control2, [x2, y2]]);
}
return bezierPoints;
}

Expand Down Expand Up @@ -664,16 +683,18 @@

// We want to draw on top of any other editors.
// Since it's the last child, there's no need to give it a higher z-index.
this.setInForeground();
// this.setInForeground();

event.preventDefault();
event.stopPropagation();

if (!this.div.contains(document.activeElement)) {
this.div.focus({
preventScroll: true /* See issue #17327 */,
});
}

this.#isDrawingStraightLine = event.shiftKey || event.ctrlKey;
this.parent.setEditingState(false);
this.#startDrawing(event.offsetX, event.offsetY);
}

Expand Down