Skip to content

Commit 193c6cb

Browse files
authored
Merge pull request #7 from lumalabs/theop/uniqueId-v2
Add public setUniqueIdGenerator for custom ID generation
2 parents 2157860 + 544f5ec commit 193c6cb

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

packages/utils/api-report.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ export function setInLocalStorage(key: string, value: string): void;
463463
// @internal
464464
export function setInSessionStorage(key: string, value: string): void;
465465

466+
// @public
467+
export function setUniqueIdGenerator(fn: (size?: number) => string): void;
468+
466469
// @internal
467470
export function sleep(ms: number): Promise<void>;
468471

packages/utils/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export { ExecutionQueue } from './lib/ExecutionQueue'
3333
export { FileHelpers } from './lib/file'
3434
export { noop, omitFromStackTrace } from './lib/function'
3535
export { getHashForBuffer, getHashForObject, getHashForString, lns } from './lib/hash'
36-
export { mockUniqueId, restoreUniqueId, uniqueId } from './lib/id'
36+
export { mockUniqueId, restoreUniqueId, setUniqueIdGenerator, uniqueId } from './lib/id'
3737
export { getFirstFromIterable } from './lib/iterable'
3838
export type { JsonArray, JsonObject, JsonPrimitive, JsonValue } from './lib/json-value'
3939
export {

packages/utils/src/lib/id.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ function nanoid(size = 21) {
5555
}
5656

5757
let impl = nanoid
58+
59+
/**
60+
* Override the unique ID generator with a custom implementation.
61+
*
62+
* This allows you to customize how IDs are generated across tldraw,
63+
* affecting all ID generation including shapes, pages, and other records.
64+
*
65+
* @param fn - A function that returns a string ID. Takes an optional size parameter.
66+
* @example
67+
* ```ts
68+
* import { setUniqueIdGenerator } from '@tldraw/editor'
69+
*
70+
* setUniqueIdGenerator(() => `id-${Date.now()}-${Math.random()}`)
71+
* ```
72+
* @public
73+
*/
74+
export function setUniqueIdGenerator(fn: (size?: number) => string) {
75+
impl = fn
76+
}
77+
5878
/**
5979
* Mock the unique ID generator with a custom implementation for testing.
6080
*

scripts/pack-and-copy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -e
33

44
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
55
DEST="$HOME/work/core/shared/js/tldraw"
6-
PACKAGES=(tldraw editor)
6+
PACKAGES=(utils tldraw editor)
77

88
mkdir -p "$DEST"
99

0 commit comments

Comments
 (0)