Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/utils/api-report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ export function setInLocalStorage(key: string, value: string): void;
// @internal
export function setInSessionStorage(key: string, value: string): void;

// @public
export function setUniqueIdGenerator(fn: (size?: number) => string): void;

// @internal
export function sleep(ms: number): Promise<void>;

Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export { ExecutionQueue } from './lib/ExecutionQueue'
export { FileHelpers } from './lib/file'
export { noop, omitFromStackTrace } from './lib/function'
export { getHashForBuffer, getHashForObject, getHashForString, lns } from './lib/hash'
export { mockUniqueId, restoreUniqueId, uniqueId } from './lib/id'
export { mockUniqueId, restoreUniqueId, setUniqueIdGenerator, uniqueId } from './lib/id'
export { getFirstFromIterable } from './lib/iterable'
export type { JsonArray, JsonObject, JsonPrimitive, JsonValue } from './lib/json-value'
export {
Expand Down
20 changes: 20 additions & 0 deletions packages/utils/src/lib/id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ function nanoid(size = 21) {
}

let impl = nanoid

/**
* Override the unique ID generator with a custom implementation.
*
* This allows you to customize how IDs are generated across tldraw,
* affecting all ID generation including shapes, pages, and other records.
*
* @param fn - A function that returns a string ID. Takes an optional size parameter.
* @example
* ```ts
* import { setUniqueIdGenerator } from '@tldraw/editor'
*
* setUniqueIdGenerator(() => `id-${Date.now()}-${Math.random()}`)
* ```
* @public
*/
export function setUniqueIdGenerator(fn: (size?: number) => string) {
impl = fn
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Additional Locations (1)

Fix in Cursor Fix in Web

Comment thread
cursor[bot] marked this conversation as resolved.

/**
* Mock the unique ID generator with a custom implementation for testing.
*
Expand Down
2 changes: 1 addition & 1 deletion scripts/pack-and-copy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -e

REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
DEST="$HOME/work/core/shared/js/tldraw"
PACKAGES=(tldraw editor)
PACKAGES=(utils tldraw editor)

mkdir -p "$DEST"

Expand Down
Loading