From c6c4fe29dd76ddbd18cdf95b473889dc02f4c65a Mon Sep 17 00:00:00 2001 From: fan Date: Thu, 14 Dec 2023 10:34:54 +0800 Subject: [PATCH] =?UTF-8?q?query=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/jlgraphic/core/GraphicStore.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/jlgraphic/core/GraphicStore.ts b/src/jlgraphic/core/GraphicStore.ts index 902705b..c237d74 100644 --- a/src/jlgraphic/core/GraphicStore.ts +++ b/src/jlgraphic/core/GraphicStore.ts @@ -9,12 +9,12 @@ export interface GraphicQueryStore { /** * 根据id获取图形 */ - queryById(id: number): T; + queryById(id: number | string): T; /** * 根据id模糊查询图形 * @param id */ - queryByIdAmbiguous(id: number): JlGraphic[]; + queryByIdAmbiguous(id: number | string): JlGraphic[]; /** * 根据类型获取图形列表 @@ -79,15 +79,20 @@ export class GraphicStore implements GraphicQueryStore { getAllGraphics(): JlGraphic[] { return [...this.store.values()]; } - queryById(id: number): T { - const graphic = this.store.get(id) as T; - if (!graphic) throw Error(`未找到id为 [${id}] 的图形.`); - return this.store.get(id) as T; + queryById(id: number | string): T { + let nid = id; + if (typeof id === 'string') { + nid = parseInt(id); + } + const graphic = this.store.get(nid as number) as T; + if (!graphic) throw Error(`未找到id为 [${nid}] 的图形.`); + return this.store.get(nid as number) as T; } - queryByIdAmbiguous(id: number): JlGraphic[] { + queryByIdAmbiguous(id: number | string): JlGraphic[] { + const nid = id + ''; const list: JlGraphic[] = []; this.store.forEach((g) => { - if (Math.abs(g.id - id) <= 10) { + if ((g.id + '').search(nid) >= 0) { list.push(g); } });