diff --git a/packages/utils/api-report.api.md b/packages/utils/api-report.api.md index f39154e7bbd7..d96cc015fc99 100644 --- a/packages/utils/api-report.api.md +++ b/packages/utils/api-report.api.md @@ -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; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 9f82268992ba..0c8301deac75 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -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 { diff --git a/packages/utils/src/lib/id.ts b/packages/utils/src/lib/id.ts index c02306926638..76cdf1b75324 100644 --- a/packages/utils/src/lib/id.ts +++ b/packages/utils/src/lib/id.ts @@ -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 +} + /** * Mock the unique ID generator with a custom implementation for testing. * diff --git a/scripts/pack-and-copy.sh b/scripts/pack-and-copy.sh index c035744f7162..bdbda7352e68 100755 --- a/scripts/pack-and-copy.sh +++ b/scripts/pack-and-copy.sh @@ -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"