sebbo2002 / ical-generator

ical-generator is a small piece of code which generates ical calendar files
MIT License
749 stars 162 forks source link

Proposal to add color property to ical.EventData #153

Closed christopherread closed 5 years ago

christopherread commented 5 years ago

Hello, if I understand the specification correctly, iCalendar supports a COLOR property for events: https://tools.ietf.org/html/rfc7986#section-5.9

What do you think?

Reviewing your _generate() function, this doesn't look difficult to add, so I would be happy to submit a PR if you will accept it.

sebbo2002 commented 5 years ago

I think it's a very custom feature which is required by very few people. As I want ical-generator to be a small library which does not claim to be the right tool for every usecase, I would like to prefer seeing others demanding this feature before merging it. Feel free to fork this repo to fulfil your needs if you can't wait.

marplx commented 5 years ago

I'd really like this feature, too. :)

I mean, let him add this property, if he wants to. It's optional and doesn't break anything.

marplx commented 5 years ago

I added the color field manually with string replace just to learn that Google Calendar and Calendar for Mac are ignoring the color fields. 😢

newtang commented 5 years ago

Maybe instead of a specific color property, there could be a generic custom property, that takes an object with key-value pairs? Then we could allow some fields like color, without expressly supporting them.

sebbo2002 commented 5 years ago

I added the color field manually with string replace just to learn that Google Calendar and Calendar for Mac are ignoring the color fields. 😢

So I think this feature is obsolet? Does anyone still need this? //cc @christopherread @marfnk

Maybe instead of a specific color property, there could be a generic custom property, that takes an object with key-value pairs? Then we could allow some fields like color, without expressly supporting them.

We've been through this before. At that time we had rejected it, because there are some fields with different requirements (e.g. escaping). Then you would have to do it all yourself again. And then the sense gets lost somehow.

I'd close the issue until somebody called in with a "I still need this." Then open it again with pleasure.

KrisLau commented 2 years ago

I would love this feature! In my app users can subscribe to multiple calendars and I would love to set a kind of global color for each of the calendars so that it's easier to differentiate between them

DieserMerlin commented 2 years ago

I'd really appreaciate the implementation as it wouldn't really increase the complexity or bundle size by a noteworthy amount but add a feature that has become part of the specification and is not really an edge case.

A possible workaround atm is a helper class like this:

import ical, {ICalCalendar, ICalEvent, ICalEventData} from 'ical-generator';

export class ICalEventWithColor extends ICalEvent {
  private _color?: string;

  constructor(data: ICalEventData & {color?: string}, cal: ICalCalendar) {
    super(data, cal);
    this.color(data.color);
  }

  public color(color?: string | null): this {
    this._color = color || undefined;
    return this;
  }

  public override toString(): string {
    const out = super.toString().split('\n');
    if (this._color) out.splice(out.length - 2, 0, 'COLOR:' + this._color);
    return out.join('\n');
  }
}

Now you can add an event with a color:

const cal = ical();
cal.createEvent(
  new ICalEventWithColor({start: new Date(), color: 'turquoise'}, cal)
);
console.log(cal.toString());

Output:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//sebbo.net//ical-generator//EN
BEGIN:VEVENT
UID:1ca2fc5b-0c8d-4e36-8c12-9819b9eab0f9
SEQUENCE:0
DTSTAMP:20220831T122909Z
DTSTART:20220831T122909Z
SUMMARY:
COLOR:turquoise
END:VEVENT
END:VCALENDAR

Would be even more cleanly with an enum containing all possible CSS3 color values, but should do just fine.

KrisLau commented 2 years ago

@sebbo2002 Forgot to tag in my previous comment hahah

sebbo2002 commented 2 years ago

@KrisLau @DieserMerlin Do we now know which clients understand the color property at all? As far as I remember, I only didn't implement this feature because I didn't know of any client that even pays attention to the color property.

KrisLau commented 2 years ago

@sebbo2002 Hmm i think you're right from what I can tell searching online, I can't find clients that support this. I also tested with an ics file on google calendar and a mac calendar that it ignores the color property. I assumed it was possible because of one of the apps I use but it seems like they use something else probably (they use webcal instead of ics so i can't check how). Sorry to waste your time and thanks for the quick response!

sebbo2002 commented 2 years ago

Okay. So if someone knows a client that has a significant distribution and has implemented this feature, I'm definitely willing to implement the color feature. But as long as there is no client for it, I honestly don't see the need for it. This only creates open issues again, because the feature doesn't work as the user imagines it.

Therefore: If someone finds a client, please open the ticket again. Thank you very much.