-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24972 from Marklb/marklb/fix-angular-name-collisions
Angular: Add random attribute to bootstrapped selector
- Loading branch information
Showing
4 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
code/frameworks/angular/src/client/angular-beta/utils/StoryUID.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Count of stories for each storyId. | ||
*/ | ||
const storyCounts = new Map<string, number>(); | ||
|
||
/** | ||
* Increments the count for a storyId and returns the next UID. | ||
* | ||
* When a story is bootstrapped, the storyId is used as the element tag. That | ||
* becomes an issue when a story is rendered multiple times in the same docs | ||
* page. This function returns a UID that is appended to the storyId to make | ||
* it unique. | ||
* | ||
* @param storyId id of a story | ||
* @returns uid of a story | ||
*/ | ||
export const getNextStoryUID = (storyId: string): string => { | ||
if (!storyCounts.has(storyId)) { | ||
storyCounts.set(storyId, -1); | ||
} | ||
|
||
const count = storyCounts.get(storyId) + 1; | ||
storyCounts.set(storyId, count); | ||
return `${storyId}-${count}`; | ||
}; | ||
|
||
/** | ||
* Clears the storyId counts. | ||
* | ||
* Can be useful for testing, where you need predictable increments, without | ||
* reloading the global state. | ||
* | ||
* If onlyStoryId is provided, only that storyId is cleared. | ||
* | ||
* @param onlyStoryId id of a story | ||
*/ | ||
export const clearStoryUIDs = (onlyStoryId?: string): void => { | ||
if (onlyStoryId !== undefined && onlyStoryId !== null) { | ||
storyCounts.delete(onlyStoryId); | ||
} else { | ||
storyCounts.clear(); | ||
} | ||
}; |