From 2147eae687b82d05bc43bb4605d9068f148bb92a Mon Sep 17 00:00:00 2001 From: undergroundwires Date: Sat, 7 Oct 2023 15:14:53 +0200 Subject: [PATCH] Add developer toolkit UI component The commit adds a new a UI component that's enabled in development mode. This component, initially, provides a button that wen clicked, logs all the script and category names to the console. It helps revising names used throughout the application. By having this component in a conditionally rendered component, it's excluded from the production builds. --- src/presentation/components/App.vue | 9 ++- .../components/DevToolkit/DevToolkit.vue | 71 +++++++++++++++++++ .../components/DevToolkit/DumpNames.ts | 35 +++++++++ src/presentation/components/TheHeader.vue | 1 + 4 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 src/presentation/components/DevToolkit/DevToolkit.vue create mode 100644 src/presentation/components/DevToolkit/DumpNames.ts diff --git a/src/presentation/components/App.vue b/src/presentation/components/App.vue index b8a87bcd..f659018a 100644 --- a/src/presentation/components/App.vue +++ b/src/presentation/components/App.vue @@ -7,11 +7,12 @@ + + + diff --git a/src/presentation/components/DevToolkit/DumpNames.ts b/src/presentation/components/DevToolkit/DumpNames.ts new file mode 100644 index 00000000..750e95b1 --- /dev/null +++ b/src/presentation/components/DevToolkit/DumpNames.ts @@ -0,0 +1,35 @@ +import { IApplication } from '@/domain/IApplication'; +import { ApplicationFactory } from '@/application/ApplicationFactory'; + +export async function dumpNames(): Promise { + const application = await ApplicationFactory.Current.getApp(); + const names = collectNames(application); + const output = names.join('\n'); + return output; +} + +function collectNames(application: IApplication): string[] { + const { collections } = application; + + const allNames = [ + ...collections.flatMap((collection) => collection.getAllCategories().map((c) => c.name)), + ...collections.flatMap((collection) => collection.getAllScripts().map((c) => c.name)), + ]; + + const uniqueNames = [...new Set(allNames)]; + + return shuffle(uniqueNames); +} + +/* + Shuffle an array of strings, returning a new array with elements in random order. + Uses the Fisher-Yates (or Durstenfeld) algorithm. +*/ +function shuffle(array: readonly string[]): string[] { + const shuffledArray = [...array]; + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]]; + } + return shuffledArray; +} diff --git a/src/presentation/components/TheHeader.vue b/src/presentation/components/TheHeader.vue index 07f863e0..7b9eaf77 100644 --- a/src/presentation/components/TheHeader.vue +++ b/src/presentation/components/TheHeader.vue @@ -22,6 +22,7 @@ export default defineComponent({ }; }, }); +