From a02fd2c695ac2cd07c8025f58a31a010f2a8004b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=98=8E=E5=8D=8E?= <51693922+WangMingHua111@users.noreply.github.com> Date: Wed, 3 Aug 2022 14:01:12 +0800 Subject: [PATCH] =?UTF-8?q?fix(editor):=20id=E5=8F=AF=E8=83=BD=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=20(#221)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 提升id位数,降低id重复几率 --- packages/editor/src/services/props.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/services/props.ts b/packages/editor/src/services/props.ts index 5cf9e114..c022769b 100644 --- a/packages/editor/src/services/props.ts +++ b/packages/editor/src/services/props.ts @@ -17,7 +17,7 @@ */ import { reactive } from 'vue'; -import { cloneDeep, mergeWith, random } from 'lodash-es'; +import { cloneDeep, mergeWith } from 'lodash-es'; import type { FormConfig } from '@tmagic/form'; import type { Id, MComponent, MNode, MPage } from '@tmagic/schema'; @@ -121,12 +121,25 @@ class Props extends BaseService { return { id, ...defaultPropsValue, - ...mergeWith(cloneDeep(this.state.propsValueMap[type] || {}), data), + ...mergeWith({}, cloneDeep(this.state.propsValueMap[type] || {}), data), }; } + /** + * 生成指定位数的GUID,无【-】格式 + * @param digit 位数,默认值8 + * @returns + */ + guid(digit = 8): string { + return 'x'.repeat(digit).replace(/[xy]/g, (c) => { + const r = (Math.random() * 16) | 0; + const v = c == 'x' ? r : (r & 0x3) | 0x8; + return v.toString(16); + }); + } + public async createId(type: string | number): Promise { - return `${type}_${random(10000, false)}`; + return `${type}_${this.guid()}`; } /**