From 6e86cac61313e4f730fcfdc20c5cc20f6bbef86f Mon Sep 17 00:00:00 2001 From: walker Date: Wed, 1 Nov 2023 14:46:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E9=98=85=E9=80=89=E9=A1=B9=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0createOnNotFound=E5=B1=9E=E6=80=A7=20=E5=9B=BE?= =?UTF-8?q?=E5=BD=A2=E7=8A=B6=E6=80=81=E6=8E=A5=E5=8F=A3=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?remove=E5=B1=9E=E6=80=A7=20=E8=B0=83=E6=95=B4=E5=9B=BE=E5=BD=A2?= =?UTF-8?q?app=E6=8E=A5=E6=94=B6=E8=AE=BE=E5=A4=87=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E7=9A=84=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/jlgraphic/app/JlGraphicApp.ts | 27 ++++++++++++++++++-------- src/jlgraphic/core/JlGraphic.ts | 1 + src/jlgraphic/message/MessageBroker.ts | 11 ++++++++++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/jlgraphic/app/JlGraphicApp.ts b/src/jlgraphic/app/JlGraphicApp.ts index ea1dfc7..013b62a 100644 --- a/src/jlgraphic/app/JlGraphicApp.ts +++ b/src/jlgraphic/app/JlGraphicApp.ts @@ -475,7 +475,8 @@ export interface IGraphicScene extends EventEmitter { */ handleGraphicStates( graphicStates: GraphicState[], - queryer?: GraphicQuery + queryer?: GraphicQuery, + createOnNotFound?: boolean ): void; /** * 根据类型获取图形模板 @@ -1092,7 +1093,8 @@ abstract class GraphicSceneBase */ handleGraphicStates( graphicStates: GraphicState[], - queryer?: GraphicQuery + queryer?: GraphicQuery, + createOnNotFound?: boolean ): void { graphicStates.forEach((state) => { let g: JlGraphic | undefined; @@ -1103,12 +1105,21 @@ abstract class GraphicSceneBase } try { if (!g) { - const template = this.getGraphicTemplatesByType(state.graphicType); - const g = template.new(); - g.loadState(state); - this.addGraphics(g); - } else if (g.updateStates(state)) { - g.repaint(); + // 未找到图形对象 + if (createOnNotFound) { + const template = this.getGraphicTemplatesByType(state.graphicType); + const g = template.new(); + g.loadState(state); + this.addGraphics(g); + } + } else { + // 找到 + if (state.remove) { + this.deleteGraphics(g); + g.destroy({ children: true }); + } else if (g.updateStates(state)) { + g.repaint(); + } } } catch (err) { console.error('图形状态处理异常', g, state, err); diff --git a/src/jlgraphic/core/JlGraphic.ts b/src/jlgraphic/core/JlGraphic.ts index 595bf59..a872c28 100644 --- a/src/jlgraphic/core/JlGraphic.ts +++ b/src/jlgraphic/core/JlGraphic.ts @@ -502,6 +502,7 @@ export interface GraphicData { export interface GraphicState { get code(): string; // 业务标识 get graphicType(): string; // 图形类型 + remove?: boolean; // 是否移除图形 /** * 克隆消息 */ diff --git a/src/jlgraphic/message/MessageBroker.ts b/src/jlgraphic/message/MessageBroker.ts index 0d2c2b1..08261b8 100644 --- a/src/jlgraphic/message/MessageBroker.ts +++ b/src/jlgraphic/message/MessageBroker.ts @@ -146,6 +146,7 @@ export type GraphicStateMessageConvert = ( message: Uint8Array ) => GraphicState[]; +// 根据状态查询图形对象查询器 export type GraphicQuery = ( state: GraphicState, store: GraphicQueryStore @@ -168,6 +169,10 @@ export interface AppStateSubscription { * 根据状态查询图形对象,默认为根据code和type查询图形对象 */ graphicQueryer?: GraphicQuery; + /** + * 当未根据状态找到图形对象时是否创建新对象 + */ + createOnNotFound?: boolean; /** * 订阅消息处理 */ @@ -196,7 +201,11 @@ class AppMessageHandler implements IMessageHandler { const sub = this.sub; if (sub.messageConverter) { const graphicStates = sub.messageConverter(data); - this.app.handleGraphicStates(graphicStates, sub.graphicQueryer); + this.app.handleGraphicStates( + graphicStates, + sub.graphicQueryer, + sub.createOnNotFound + ); } else if (sub.messageHandle) { sub.messageHandle(data); }