Skip to content

Commit

Permalink
package[ding-flow]: 调整id生成
Browse files Browse the repository at this point in the history
  • Loading branch information
miyuesc committed Jul 31, 2024
1 parent beb9ce6 commit c94059e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@arco-design/web-vue": "^2.56.0",
"@micro-zoe/micro-app": "1.0.0-rc.5",
"echarts": "^5.5.0",
"ids": "^1.0.5",
"lucide-vue-next": "^0.417.0",
"pinia": "^2.1.7",
"react": "^18.3.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ding-flow/src/utils/element-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function createNode<T extends BaseNodeType>(
bo?: Record<string, unknown>,
): BaseNode {
const base: BaseNode = {
id: `${type}-${ids()}`,
id: ids(type),
type,
name: name || DEFAULT_NAME_MAP[type] || type,
$prev: undefined,
Expand Down
27 changes: 19 additions & 8 deletions packages/ding-flow/src/utils/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
export function ids() {
import Ids from 'ids'

const idsCreator = new Ids()

export function ids(prefix: string, simple: boolean = true) {
if (simple) {
return idsCreator.nextPrefixed(prefix)
}

if (typeof crypto.randomUUID === 'function') {
return crypto.randomUUID()
return `${prefix}-${crypto.randomUUID()}`
}

if (typeof crypto.getRandomValues === 'function' && Uint8Array) {
return (([1e7] as any as number) + -1e3 + -4e3 + -8e3 + -1e11).toString().replace(/[018]/g, c => ((Number(c).valueOf()) ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> (Number(c).valueOf()) / 4).toString(16))
return `${prefix}-${(([1e7] as any as number) + -1e3 + -4e3 + -8e3 + -1e11).toString().replace(/[018]/g, c => (Number(c).valueOf() ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (Number(c).valueOf() / 4)))).toString(16))}`
}

return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0
const v = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
return `${prefix}-${'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
/[xy]/g,
(c) => {
const r = (Math.random() * 16) | 0
const v = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
},
)}`
}
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c94059e

Please sign in to comment.