状态消息订阅添加图形对象查询属性,可以从外部控制查询方式,如果不提供,默认为根据code和type查询

This commit is contained in:
walker 2023-09-22 10:59:45 +08:00
parent 3e1bbc92bf
commit 22fc9f8d6b
3 changed files with 28 additions and 9 deletions

View File

@ -24,6 +24,7 @@ import {
WsMsgCli,
type AppStateSubscription,
type MessageCliOption,
GraphicQuery,
} from '../message';
import { OperationRecord } from '../operation/JlOperation';
import {
@ -472,7 +473,10 @@ export interface IGraphicScene extends EventEmitter<GraphicAppEvents> {
*
* @param graphicStates
*/
handleGraphicStates(graphicStates: GraphicState[]): void;
handleGraphicStates(
graphicStates: GraphicState[],
queryer?: GraphicQuery
): void;
/**
*
* @param type
@ -1083,12 +1087,17 @@ abstract class GraphicSceneBase
*
* @param graphicStates
*/
handleGraphicStates(graphicStates: GraphicState[]): void {
handleGraphicStates(
graphicStates: GraphicState[],
queryer?: GraphicQuery
): void {
graphicStates.forEach((state) => {
const g = this.queryStore.queryByCodeAndType(
state.code,
state.graphicType
);
let g: JlGraphic | undefined;
if (queryer) {
g = queryer(state, this.queryStore);
} else {
g = this.queryStore.queryByCodeAndType(state.code, state.graphicType);
}
try {
if (!g) {
const template = this.getGraphicTemplatesByType(state.graphicType);

View File

@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import EventEmitter from 'eventemitter3';
import { IGraphicScene } from '../app';
import { GraphicState } from '../core';
import { GraphicQueryStore, GraphicState, JlGraphic } from '../core';
import {
IMessageHandler,
IUnsubscriptor,
@ -146,6 +146,11 @@ export type GraphicStateMessageConvert = (
message: Uint8Array
) => GraphicState[];
export type GraphicQuery = (
state: GraphicState,
store: GraphicQueryStore
) => JlGraphic | undefined;
// 订阅消息处理器
export type SubscriptionMessageHandle = (message: Uint8Array) => void;
@ -159,6 +164,10 @@ export interface AppStateSubscription {
*
*/
messageConverter?: GraphicStateMessageConvert;
/**
* ,code和type查询图形对象
*/
graphicQueryer?: GraphicQuery;
/**
*
*/
@ -187,7 +196,7 @@ class AppMessageHandler implements IMessageHandler {
const sub = this.sub;
if (sub.messageConverter) {
const graphicStates = sub.messageConverter(data);
this.app.handleGraphicStates(graphicStates);
this.app.handleGraphicStates(graphicStates, sub.graphicQueryer);
} else if (sub.messageHandle) {
sub.messageHandle(data);
}

View File

@ -2,6 +2,7 @@ import { defineStore } from 'pinia';
import { destroyDrawApp, getDrawApp, initDrawApp } from 'src/examples/app';
import { DrawAssistant, GraphicData, IDrawApp, JlGraphic } from 'src/jlgraphic';
import { IJlCanvas } from 'src/jlgraphic/app/JlGraphicApp';
import { markRaw } from 'vue';
export const useDrawStore = defineStore('draw', {
state: () => ({
@ -81,7 +82,7 @@ export const useDrawStore = defineStore('draw', {
});
app.on('graphicselected', () => {
console.log('批量选中事件', app.selectedGraphics.length);
this.selectedGraphics = app.selectedGraphics;
this.selectedGraphics = markRaw(app.selectedGraphics);
});
this.selectedGraphics = [];
return app;