Add public setUniqueIdGenerator for custom ID generation#7
Add public setUniqueIdGenerator for custom ID generation#7kaznaan merged 10 commits intoluma/v4.4.xfrom
Conversation
Allows apps to override the unique ID generator used across tldraw, enabling alignment of ID generation between shapes and artifacts. Already re-exported via @tldraw/editor's `export * from '@tldraw/utils'`. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Include @tldraw/utils in the tarball packaging so that custom exports like setUniqueIdGenerator are available in downstream apps. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
PR SummaryMedium Risk Overview Updates the utils public surface (API report + Written by Cursor Bugbot for commit 544f5ec. This will update automatically on new commits. Configure here. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix prepared fixes for both issues found in the latest run.
- ✅ Fixed:
restoreUniqueIdsilently discards custom generator set via public API- I introduced a persistent configured generator baseline and changed
restoreUniqueIdto restore to that baseline instead of always restoring tonanoid.
- I introduced a persistent configured generator baseline and changed
- ✅ Fixed: New
setUniqueIdGeneratorduplicates existingmockUniqueIdimplementation- I removed the duplicated assignment logic by introducing a shared internal setter used by both
setUniqueIdGeneratorandmockUniqueId.
- I removed the duplicated assignment logic by introducing a shared internal setter used by both
Or push these changes by commenting:
@cursor push 4883ac7764
Preview (4883ac7764)
diff --git a/packages/utils/src/lib/id.ts b/packages/utils/src/lib/id.ts
--- a/packages/utils/src/lib/id.ts
+++ b/packages/utils/src/lib/id.ts
@@ -54,8 +54,13 @@
return id
}
-let impl = nanoid
+let configuredImpl = nanoid
+let impl = configuredImpl
+function setImpl(fn: (size?: number) => string) {
+ impl = fn
+}
+
/**
* Override the unique ID generator with a custom implementation.
*
@@ -72,7 +77,8 @@
* @public
*/
export function setUniqueIdGenerator(fn: (size?: number) => string) {
- impl = fn
+ configuredImpl = fn
+ setImpl(fn)
}
/**
@@ -95,13 +101,13 @@
* @internal
*/
export function mockUniqueId(fn: (size?: number) => string) {
- impl = fn
+ setImpl(fn)
}
/**
- * Restore the original unique ID generator after mocking.
+ * Restore the configured unique ID generator after mocking.
*
- * Resets the ID generation function back to the original nanoid implementation.
+ * Resets the ID generation function back to the currently configured implementation.
* This should be called after testing to restore normal ID generation behavior.
*
* @example
@@ -116,7 +122,7 @@
* @internal
*/
export function restoreUniqueId() {
- impl = nanoid
+ setImpl(configuredImpl)
}
/**| */ | ||
| export function setUniqueIdGenerator(fn: (size?: number) => string) { | ||
| impl = fn | ||
| } |
There was a problem hiding this comment.
restoreUniqueId silently discards custom generator set via public API
Medium Severity
setUniqueIdGenerator sets impl to a custom function, but restoreUniqueId always resets impl back to the hard-coded nanoid, not to whatever was set via setUniqueIdGenerator. If a downstream app configures a custom generator and any code later calls restoreUniqueId, the custom generator is silently lost. restoreUniqueId needs to be aware of the user-configured baseline to restore to the right implementation.



Summary
setUniqueIdGeneratoras a public export from@tldraw/utils, allowing apps to customize how IDs are generated across tldraw (shapes, pages, records)@tldraw/utilsto thepack-and-copy.shscript so the tarball is available for downstream apps@tldraw/editor'sexport * from '@tldraw/utils'Usage
Note for vespa-web
After merging, add this override to
frontend/vespa-web/package.json:🤖 Generated with Claude Code