Skip to content

Commit b832e9b

Browse files
committed
feat: 删除drawModeHandler, 改为调用element的render
1 parent d9529a3 commit b832e9b

File tree

6 files changed

+9
-169
lines changed

6 files changed

+9
-169
lines changed

packages/board-core/src/plugins/draw/index.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { initContextAttrs } from "@e-board/board-utils";
22
import { eBoardContainer } from "../../common/IocContainer";
33
import { IModelService, IModeService, IEventService } from "../../services";
4-
import { IConfigService, IModel } from "../../services";
5-
import { IRenderService } from "../../services/renderService/type";
4+
import { IConfigService } from "../../services";
65
import { ITransformService } from "../../services/transformService/type";
76
import { IBoard, IPluginInitParams } from "../../types";
87
import { IPlugin } from "../type";
@@ -13,7 +12,6 @@ class DrawPlugin implements IPlugin {
1312
private disposeList: (() => void)[] = [];
1413
private configService = eBoardContainer.get<IConfigService>(IConfigService);
1514
private modelService = eBoardContainer.get<IModelService>(IModelService);
16-
private renderService = eBoardContainer.get<IRenderService>(IRenderService);
1715
private transformService = eBoardContainer.get<ITransformService>(ITransformService);
1816

1917

@@ -90,7 +88,6 @@ class DrawPlugin implements IPlugin {
9088
public init({ board }: IPluginInitParams) {
9189
this.board = board;
9290
this.initDrawMode();
93-
this.registerLineDrawHandler();
9491

9592
}
9693

@@ -110,33 +107,6 @@ class DrawPlugin implements IPlugin {
110107
});
111108
}
112109

113-
private registerLineDrawHandler() {
114-
this.renderService.registerDrawModelHandler("line", this.drawLineModelHandler);
115-
}
116-
117-
private drawLineModelHandler = (model: IModel, ctx?: CanvasRenderingContext2D, useWorldCoords = false) => {
118-
const context = this.board.getCtx();
119-
if (!context) return;
120-
context.save()
121-
const toScreenPoint = useWorldCoords
122-
? (point: { x: number; y: number }) => point
123-
: (point: { x: number; y: number }) => this.transformPoint(point);
124-
model.points?.forEach((point, index) => {
125-
const transformedPoint = toScreenPoint(point);
126-
if (index === 0) {
127-
context.moveTo(transformedPoint.x, transformedPoint.y);
128-
} else if (index < 2) {
129-
context.lineTo(transformedPoint.x, transformedPoint.y);
130-
} else {
131-
const p1 = toScreenPoint(model.points![index - 1]);
132-
const p2 = toScreenPoint(point);
133-
const midPointX = (p1.x + p2.x) / 2;
134-
const midPointY = (p1.y + p2.y) / 2;
135-
context.quadraticCurveTo(p1.x, p1.y, midPointX, midPointY);
136-
}
137-
});
138-
context.restore()
139-
};
140110

141111
private getCanvasPoint(clientX: number, clientY: number) {
142112
const canvas = this.board.getCanvas();
@@ -194,7 +164,6 @@ class DrawPlugin implements IPlugin {
194164

195165
public dispose() {
196166
this.disposeList.forEach(dispose => dispose());
197-
this.renderService.unregisterDrawModelHandler("line");
198167
}
199168
}
200169

packages/board-core/src/plugins/drawShape/index.ts

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,17 @@ import { initContextAttrs } from "@e-board/board-utils";
22
import { eBoardContainer } from "../../common/IocContainer";
33
import { IModelService, IModeService, IEventService } from "../../services";
44
import { IConfigService, IModel } from "../../services";
5-
import { IRenderService } from "../../services/renderService/type";
65
import { ITransformService } from "../../services/transformService/type";
76
import { IBoard, IPluginInitParams } from "../../types";
87
import { IPlugin } from "../type";
98

109
const CURRENT_MODE = "drawShape";
1110

12-
13-
type IShapeRectangle = {
14-
width: number;
15-
height: number;
16-
}
17-
1811
class DrawShapePlugin implements IPlugin {
1912
private board!: IBoard;
2013
private disposeList: (() => void)[] = [];
2114
private configService = eBoardContainer.get<IConfigService>(IConfigService);
2215
private modelService = eBoardContainer.get<IModelService>(IModelService);
23-
private renderService = eBoardContainer.get<IRenderService>(IRenderService);
2416
private transformService = eBoardContainer.get<ITransformService>(ITransformService);
2517
private lastPoint = { x: 0, y: 0 };
2618

@@ -94,7 +86,6 @@ class DrawShapePlugin implements IPlugin {
9486
public init({ board }: IPluginInitParams) {
9587
this.board = board;
9688
this.initDrawMode();
97-
this.registerShapeDrawHandler();
9889
}
9990

10091
private initDrawMode() {
@@ -113,44 +104,6 @@ class DrawShapePlugin implements IPlugin {
113104
});
114105
}
115106

116-
private registerShapeDrawHandler() {
117-
// 钜形
118-
this.renderService.registerDrawModelHandler("rectangle", this.drawRectangleModelHandler);
119-
}
120-
121-
private drawRectangleModelHandler = (
122-
model: IModel<IShapeRectangle>,
123-
_: any,
124-
isViewChanged: boolean = false
125-
) => {
126-
const context = this.board.getCtx();
127-
if (!context) return;
128-
const [point] = model.points!;
129-
if (isViewChanged) {
130-
context.rect(
131-
point.x,
132-
point.y,
133-
model.width,
134-
model.height
135-
)
136-
} else {
137-
const transformedPoint = this.transformPoint({ x: point.x, y: point.y });
138-
const zoom = this.transformService.getView().zoom;
139-
140-
// 绘制矩形
141-
context.rect(
142-
transformedPoint.x,
143-
transformedPoint.y,
144-
model.width * zoom,
145-
model.height * zoom
146-
);
147-
}
148-
149-
if (model.options?.fillStyle) {
150-
context.fillStyle = model.options.fillStyle;
151-
context.fill();
152-
}
153-
};
154107

155108
private getCanvasPoint(clientX: number, clientY: number) {
156109
const canvas = this.board.getCanvas();
@@ -208,7 +161,6 @@ class DrawShapePlugin implements IPlugin {
208161

209162
public dispose() {
210163
this.disposeList.forEach(dispose => dispose());
211-
this.renderService.unregisterDrawModelHandler("rectangle");
212164
}
213165
}
214166

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1 @@
1-
export interface ILine {
2-
id: string;
3-
points: { x: number; y: number }[];
4-
}
51

6-
export interface TextLayoutInfo {
7-
lines: string[];
8-
lineHeight: number;
9-
fontSize: number;
10-
fontFamily: string;
11-
textAlign: string;
12-
paddingTop: number;
13-
paddingLeft: number;
14-
dpr: number; // 设备像素比
15-
}
16-
17-
export interface IRectangleModel {
18-
width: number;
19-
height: number;
20-
text?: string;
21-
textLayout?: TextLayoutInfo;
22-
fillStyle?: string;
23-
}

packages/board-core/src/plugins/picture/index.ts

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { eBoardContainer } from "../../common/IocContainer";
22
import { IConfigService, IModelService } from "../../services";
3-
import { IRenderService } from "../../services/renderService/type";
43
import { ITransformService } from "../../services/transformService/type";
54
import { IBoard, IPluginInitParams } from "../../types";
65
import { IPlugin } from "../type";
@@ -17,7 +16,6 @@ class PicturePlugin implements IPlugin {
1716
private board!: IBoard;
1817
private disposeList: (() => void)[] = [];
1918
private modelService = eBoardContainer.get<IModelService>(IModelService);
20-
private renderService = eBoardContainer.get<IRenderService>(IRenderService);
2119
private transformService = eBoardContainer.get<ITransformService>(ITransformService);
2220
private configService = eBoardContainer.get<IConfigService>(IConfigService);
2321
private imageCache = new Map<string, HTMLImageElement>();
@@ -27,7 +25,6 @@ class PicturePlugin implements IPlugin {
2725

2826
public init({ board }: IPluginInitParams) {
2927
this.board = board;
30-
this.registerPictureDrawHandler();
3128
}
3229

3330
public insertImage = (
@@ -98,47 +95,8 @@ class PicturePlugin implements IPlugin {
9895
return this.transformService.transformPoint(point, inverse);
9996
}
10097

101-
private registerPictureDrawHandler() {
102-
this.renderService.registerDrawModelHandler("picture", this.drawPictureModelHandler);
103-
}
104-
105-
private drawPictureModelHandler = (model: PictureModel, _: any, useWorldCoords = false) => {
106-
const context = this.board.getCtx();
107-
if (!context || !model.imageData || !model.points) return;
108-
109-
context.save();
110-
111-
let img = this.imageCache.get(model.id);
112-
if (!img) {
113-
img = new Image();
114-
img.src = model.imageData;
115-
this.imageCache.set(model.id, img);
116-
}
117-
118-
if (img.complete) {
119-
const zoom = this.transformService.getView().zoom;
120-
const transformedPos = useWorldCoords ? model.points[0] : this.transformPoint(model.points[0]);
121-
const width = useWorldCoords ? (model.width || img.width) : (model.width || img.width) * zoom;
122-
const height = useWorldCoords ? (model.height || img.height) : (model.height || img.height) * zoom;
123-
124-
// 将图片中心对齐到指定位置,而不是左上角
125-
const drawX = transformedPos.x
126-
const drawY = transformedPos.y
127-
128-
context.drawImage(img, drawX, drawY, width, height);
129-
} else {
130-
img.onload = () => {
131-
this.renderService.reRender();
132-
};
133-
}
134-
135-
context.restore();
136-
};
137-
138-
13998
public dispose() {
14099
this.disposeList.forEach(dispose => dispose());
141-
this.renderService.unregisterDrawModelHandler("picture");
142100
this.imageCache.clear();
143101
}
144102
}

packages/board-core/src/services/renderService/index.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import { IModelService, ModelChangeType, BoundingBox, IModel } from '../modelSer
44
import { ITransformService } from "../transformService/type";
55

66

7-
import { IDrawModelHandler, IRenderService, Range, View } from "./type";
7+
import { IRenderService, Range, View } from "./type";
88
import { TileManager } from "./tileManager";
99
import { Emitter, initContextAttrs } from '@e-board/board-utils';
10+
import { IElementService } from "../elementService/type";
1011

1112

1213
class RenderService implements IRenderService {
1314
private board!: IBoard;
1415
private modelService = eBoardContainer.get<IModelService>(IModelService);
15-
private modelHandler = new Map<string, IDrawModelHandler>();
16+
private elementService = eBoardContainer.get<IElementService>(IElementService);
1617
private readonly _renderStart = new Emitter<void>();
1718
private readonly _renderEnd = new Emitter<void>();
1819
public onRenderStart = this._renderStart.event;
@@ -178,16 +179,7 @@ class RenderService implements IRenderService {
178179
}
179180

180181

181-
public registerDrawModelHandler(key: string, handler: IDrawModelHandler) {
182-
this.modelHandler.set(key, handler);
183-
}
184-
185-
public unregisterDrawModelHandler(key: string) {
186-
this.modelHandler.delete(key);
187-
}
188-
189182
public dispose(): void {
190-
this.modelHandler = new Map();
191183
this.disposeList.forEach(dispose => dispose());
192184
RenderService.transformService = null;
193185
}
@@ -244,8 +236,6 @@ class RenderService implements IRenderService {
244236
};
245237
}
246238

247-
248-
249239
if (!this.currentRanges) {
250240
context.clearRect(0, 0, canvas.width, canvas.height);
251241
if (isViewChange) {
@@ -263,7 +253,6 @@ class RenderService implements IRenderService {
263253
);
264254
}
265255

266-
267256
// 同时清空交互画布,避免重叠
268257
if (interactionCtx) {
269258
interactionCtx.clearRect(0, 0, interactionCtx.canvas.width, interactionCtx.canvas.height);
@@ -310,19 +299,16 @@ class RenderService implements IRenderService {
310299
continue;
311300
}
312301
}
313-
const handler = this.modelHandler.get(model.type);
314-
if (handler) {
302+
const comp = this.elementService.getElement(model.type);
303+
if (!comp) continue;
304+
const renderHandler = new comp.render(this.board);
305+
if (renderHandler) {
315306
context.beginPath();
316307
initContextAttrs(context, { zoom }, model.options);
317-
handler(model, context as any, !!useWorldCoords);
308+
renderHandler.render(model, context as any, !!useWorldCoords);
318309
context.stroke();
319310
}
320-
321-
322311
}
323-
324-
325-
326312
context.restore();
327313
this.currentRanges = null;
328314
this._renderEnd.fire();

packages/board-core/src/services/renderService/type.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ export type Range = {
99
maxY: number
1010
}
1111

12-
export interface IDrawModelHandler {
13-
(model: any, ctx?: CanvasRenderingContext2D, useWorldCoords?: boolean): void;
14-
}
1512
export interface View {
1613
x: number;
1714
y: number;

0 commit comments

Comments
 (0)