From fa6e949805948a2ced015fb2995ec30a259eaf4c Mon Sep 17 00:00:00 2001 From: matt Date: Sun, 24 Jul 2022 21:54:12 +0000 Subject: [PATCH] Generate hsl for EventType label --- src/collections/EventTypes.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/collections/EventTypes.ts b/src/collections/EventTypes.ts index 286df34..22f6535 100644 --- a/src/collections/EventTypes.ts +++ b/src/collections/EventTypes.ts @@ -5,6 +5,29 @@ const EventTypes: CollectionConfig = { access: { read: () => true, }, + hooks: { + afterRead: [ + ({ doc }) => { + const hashCode = (str) => { + let hash = 0; + + for (let i = 0; i < str.length; i++) + hash = str.charCodeAt(i) + ((hash << 5) - hash); + + + return hash; + }; + + const pickColour = (str) => { + return `hsl(${hashCode(str) % 360}, 100%, 50%)`; + }; + + doc.backgroundColour = pickColour(doc.name); + + return doc; + } + ] + }, admin: { useAsTitle: 'name' },