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(edgeless): auto-complete displays incorrect shape #6334

Merged
merged 3 commits into from
Feb 29, 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
CanvasElementType,
type IVec,
Overlay,
rotatePoints,
type RoughCanvas,
Vec,
} from '../../../../surface-block/index.js';
Expand All @@ -47,22 +46,21 @@ import {

class AutoCompleteOverlay extends Overlay {
linePoints: IVec[] = [];
shapePoints: IVec[] = [];
stroke = '';
renderShape: ((ctx: CanvasRenderingContext2D) => void) | null = null;

override render(ctx: CanvasRenderingContext2D, _rc: RoughCanvas) {
if (this.linePoints.length && this.shapePoints.length) {
if (this.linePoints.length && this.renderShape) {
ctx.setLineDash([2, 2]);
ctx.strokeStyle = this.stroke;
ctx.beginPath();
this.linePoints.forEach((p, index) => {
if (index === 0) ctx.moveTo(p[0], p[1]);
else ctx.lineTo(p[0], p[1]);
});
this.shapePoints.forEach((p, index) => {
if (index === 0) ctx.moveTo(p[0], p[1]);
else ctx.lineTo(p[0], p[1]);
});
ctx.closePath();
ctx.stroke();

this.renderShape(ctx);
ctx.stroke();
}
}
Expand Down Expand Up @@ -159,7 +157,7 @@ export class EdgelessAutoComplete extends WithDisposable(LitElement) {
_disposables.add(
this.edgeless.service.selection.slots.updated.on(() => {
this._autoCompleteOverlay.linePoints = [];
this._autoCompleteOverlay.shapePoints = [];
this._autoCompleteOverlay.renderShape = null;
})
);
_disposables.add(() => this.removeOverlay());
Expand Down Expand Up @@ -320,11 +318,9 @@ export class EdgelessAutoComplete extends WithDisposable(LitElement) {
this._autoCompleteOverlay.stroke =
this._surface.themeObserver.getVariableValue(current.strokeColor);
this._autoCompleteOverlay.linePoints = path;
this._autoCompleteOverlay.shapePoints = rotatePoints(
shapeMethods[targetType].points(bound),
bound.center,
current.rotate
);
this._autoCompleteOverlay.renderShape = ctx => {
shapeMethods[targetType].draw(ctx, { ...bound, rotate: current.rotate });
};
surface.refresh();
}

Expand Down
50 changes: 14 additions & 36 deletions packages/blocks/src/root-block/edgeless/utils/tool-overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { DisposableGroup, noop, Slot } from '@blocksuite/global/utils';

import type { CssVariableName } from '../../../_common/theme/css-variables.js';
import type { ShapeStyle } from '../../../surface-block/element-model/shape.js';
import { shapeMethods } from '../../../surface-block/element-model/shape.js';
import {
Bound,
type Options,
Overlay,
type RoughCanvas,
Expand All @@ -23,31 +25,6 @@ import {
SHAPE_OVERLAY_WIDTH,
} from '../utils/consts.js';

const drawRectangle = (ctx: CanvasRenderingContext2D, xywh: XYWH) => {
const [x, y, w, h] = xywh;
ctx.rect(x, y, w, h);
};

const drawTriangle = (ctx: CanvasRenderingContext2D, xywh: XYWH) => {
const [x, y, w, h] = xywh;
ctx.moveTo(x + w / 2, y);
ctx.lineTo(x, y + h);
ctx.lineTo(x + w, y + h);
};

const drawDiamond = (ctx: CanvasRenderingContext2D, xywh: XYWH) => {
const [x, y, w, h] = xywh;
ctx.lineTo(x + w / 2, y);
ctx.lineTo(x + w, y + h / 2);
ctx.lineTo(x + w / 2, y + h);
ctx.lineTo(x, y + h / 2);
};

const drawEllipse = (ctx: CanvasRenderingContext2D, xywh: XYWH) => {
const [x, y, w, h] = xywh;
ctx.ellipse(x + w / 2, y + h / 2, w / 2, h / 2, 0, 0, 2 * Math.PI);
};

const drawRoundedRect = (ctx: CanvasRenderingContext2D, xywh: XYWH) => {
const [x, y, w, h] = xywh;
const width = w;
Expand All @@ -74,18 +51,19 @@ const drawGeneralShape = (

ctx.beginPath();

const bound = Bound.fromXYWH(xywh);
switch (type) {
case 'rect':
drawRectangle(ctx, xywh);
shapeMethods.rect.draw(ctx, bound);
break;
case 'triangle':
drawTriangle(ctx, xywh);
shapeMethods.rect.draw(ctx, bound);
break;
case 'diamond':
drawDiamond(ctx, xywh);
shapeMethods.diamond.draw(ctx, bound);
break;
case 'ellipse':
drawEllipse(ctx, xywh);
shapeMethods.ellipse.draw(ctx, bound);
break;
case 'roundedRect':
drawRoundedRect(ctx, xywh);
Expand Down Expand Up @@ -191,13 +169,13 @@ export class RoundedRectShape extends Shape {
const y0 = y + r;
const y1 = y + h - r;
const path = `
M${x0},${y} L${x1},${y}
A${r},${r} 0 0 1 ${x1},${y0}
L${x1},${y1}
A${r},${r} 0 0 1 ${x1 - r},${y1}
L${x0 + r},${y1}
A${r},${r} 0 0 1 ${x0},${y1 - r}
L${x0},${y0}
M${x0},${y} L${x1},${y}
A${r},${r} 0 0 1 ${x1},${y0}
L${x1},${y1}
A${r},${r} 0 0 1 ${x1 - r},${y1}
L${x0 + r},${y1}
A${r},${r} 0 0 1 ${x0},${y1 - r}
L${x0},${y0}
A${r},${r} 0 0 1 ${x0 + r},${y}
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ export const diamond = {
[x + w / 2, y + h],
];
},
draw(ctx: CanvasRenderingContext2D, { x, y, w, h, rotate = 0 }: IBound) {
const cx = x + w / 2;
const cy = y + h / 2;

ctx.save();
ctx.translate(cx, cy);
ctx.rotate((rotate * Math.PI) / 180);
ctx.translate(-cx, -cy);

ctx.beginPath();
ctx.moveTo(x, y + h / 2);
ctx.lineTo(x + w / 2, y);
ctx.lineTo(x + w, y + h / 2);
ctx.lineTo(x + w / 2, y + h);
ctx.closePath();

ctx.restore();
},

hitTest(
this: ShapeElementModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ export const ellipse = {
[x + w / 2, y + h],
];
},
draw(ctx: CanvasRenderingContext2D, { x, y, w, h, rotate = 0 }: IBound) {
const cx = x + w / 2;
const cy = y + h / 2;

ctx.save();
ctx.translate(cx, cy);
ctx.rotate((rotate * Math.PI) / 180);
ctx.translate(-cx, -cy);

ctx.beginPath();
ctx.ellipse(cx, cy, w / 2, h / 2, 0, 0, 2 * Math.PI);

ctx.restore();
},
hitTest(
this: ShapeElementModel,
x: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export const rect = {
[x, y + h],
];
},
draw(ctx: CanvasRenderingContext2D, { x, y, w, h, rotate = 0 }: IBound) {
ctx.save();
ctx.translate(x + w / 2, y + h / 2);
ctx.rotate((rotate * Math.PI) / 180);
ctx.translate(-x - w / 2, -y - h / 2);
ctx.rect(x, y, w, h);
ctx.restore();
},
hitTest(
this: ShapeElementModel,
x: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ export const triangle = {
[x + w, y + h],
];
},
draw(ctx: CanvasRenderingContext2D, { x, y, w, h, rotate = 0 }: IBound) {
const cx = x + w / 2;
const cy = y + h / 2;

ctx.save();
ctx.translate(cx, cy);
ctx.rotate((rotate * Math.PI) / 180);
ctx.translate(-cx, -cy);

ctx.beginPath();
ctx.moveTo(x, y + h);
ctx.lineTo(x + w / 2, y);
ctx.lineTo(x + w, y + h);
ctx.closePath();

ctx.restore();
},
hitTest(
this: ShapeElementModel,
x: number,
Expand Down
Loading