UNPKG

@atlaskit/editor-plugin-local-id

Version:

LocalId plugin for @atlaskit/editor-core

36 lines (34 loc) 1.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generatedShortUUIDs = exports.generateShortUUID = void 0; var _adfSchema = require("@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(). */ var generatedShortUUIDs = exports.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. */ var generateShortUUID = exports.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 _adfSchema.uuid.generate(); };