@atlaskit/editor-plugin-local-id
Version:
LocalId plugin for @atlaskit/editor-core
31 lines (29 loc) • 1.1 kB
JavaScript
import { uuid } from '@atlaskit/adf-schema';
/**
* Global Set to track currently generated and existing short UUIDs in the document.
* Used to prevent duplicate short IDs when using crypto.randomUUID().
*/
export var generatedShortUUIDs = new Set();
/**
* Generates a short UUID and checks for duplicates against both
* generated UUIDs and existing UUIDs in the document.
* Retries up to 10 times if a duplicate is found.
* Falls back to full UUID if max retries exceeded.
*/
export var generateShortUUID = function generateShortUUID() {
var maxRetries = 10;
for (var attempt = 0; attempt < maxRetries; attempt++) {
try {
// eslint-disable-next-line @atlassian/perf-linting/no-expensive-split-replace -- Ignored via go/ees017 (to be fixed)
var shortUUID = crypto.randomUUID().split('-')[4];
if (!generatedShortUUIDs.has(shortUUID)) {
generatedShortUUIDs.add(shortUUID);
return shortUUID;
}
} catch (_unused) {
break;
}
}
// Fallback to full UUID if short UUID generation fails or max retries exceeded
return uuid.generate();
};