sanity-plugin-recurring-dates 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +186 -0
  3. package/dist/index.d.ts +29 -0
  4. package/dist/index.esm.js +9173 -0
  5. package/dist/index.esm.js.map +1 -0
  6. package/dist/index.js +9197 -0
  7. package/dist/index.js.map +1 -0
  8. package/package.json +93 -0
  9. package/sanity.json +8 -0
  10. package/src/components/CustomRule/CustomRule.tsx +230 -0
  11. package/src/components/CustomRule/Monthly.tsx +67 -0
  12. package/src/components/CustomRule/Weekly.tsx +62 -0
  13. package/src/components/CustomRule/index.tsx +1 -0
  14. package/src/components/DateInputs/CommonDateTimeInput.tsx +111 -0
  15. package/src/components/DateInputs/DateInput.tsx +81 -0
  16. package/src/components/DateInputs/DateTimeInput.tsx +122 -0
  17. package/src/components/DateInputs/base/DatePicker.tsx +34 -0
  18. package/src/components/DateInputs/base/DateTimeInput.tsx +104 -0
  19. package/src/components/DateInputs/base/LazyTextInput.tsx +77 -0
  20. package/src/components/DateInputs/base/calendar/Calendar.tsx +381 -0
  21. package/src/components/DateInputs/base/calendar/CalendarDay.tsx +47 -0
  22. package/src/components/DateInputs/base/calendar/CalendarMonth.tsx +52 -0
  23. package/src/components/DateInputs/base/calendar/YearInput.tsx +22 -0
  24. package/src/components/DateInputs/base/calendar/constants.ts +33 -0
  25. package/src/components/DateInputs/base/calendar/features.ts +4 -0
  26. package/src/components/DateInputs/base/calendar/utils.ts +34 -0
  27. package/src/components/DateInputs/index.ts +2 -0
  28. package/src/components/DateInputs/types.ts +4 -0
  29. package/src/components/DateInputs/utils.ts +4 -0
  30. package/src/components/RecurringDate.tsx +139 -0
  31. package/src/constants.ts +36 -0
  32. package/src/index.ts +2 -0
  33. package/src/plugin.tsx +19 -0
  34. package/src/schema/recurringDates.tsx +44 -0
  35. package/src/types.ts +27 -0
  36. package/src/utils.ts +16 -0
  37. package/v2-incompatible.js +11 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Tom Smith
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,186 @@
1
+ # sanity-plugin-recurring-dates
2
+
3
+ > This is a **Sanity Studio v3** plugin.
4
+
5
+ This is a plugin to add a custom input component to your Sanity Studio which allows you to specify recurring dates.
6
+
7
+ The plugin allows for easy selection of common recurrences, as well as configuring custom recurrences. It's also possible to provide an end to a recurrence by setting an end date or a count of how many times the recurrnce should happen.
8
+
9
+ ## Installation
10
+
11
+ ```sh
12
+ npm install sanity-plugin-recurring-dates
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Add it as a plugin in `sanity.config.ts` (or .js):
18
+
19
+ ```ts
20
+ import {defineConfig} from 'sanity'
21
+ import {recurringDates} from 'sanity-plugin-recurring-dates'
22
+
23
+ export default defineConfig({
24
+ //...
25
+ plugins: [recurringDates()],
26
+ })
27
+ ```
28
+
29
+ In your schema, you can then set a field to use the `recurringDates` field type:
30
+
31
+ ```ts
32
+ defineField({
33
+ name: 'date',
34
+ title: 'Date',
35
+ type: 'recurringDates',
36
+ })
37
+ ```
38
+
39
+ ## Configuring the plugin
40
+
41
+ You can configure the plugin on the plugin or field level. Plugin level configuration can be overridden on each field.
42
+
43
+ ### Plugin config
44
+
45
+ ```ts
46
+ export default defineConfig({
47
+ //...
48
+ plugins: [
49
+ recurringDates({
50
+ // An array of RRULE strings to use as default recurrences
51
+ defaultRecurrences: [
52
+ 'RRULE:FREQ=DAILY;INTERVAL=1',
53
+ 'RRULE:FREQ=WEEKLY;INTERVAL=1',
54
+ 'RRULE:FREQ=MONTHLY;INTERVAL=1',
55
+ 'RRULE:FREQ=YEARLY;INTERVAL=1',
56
+ ],
57
+
58
+ // Hides the end date field
59
+ hideEndDate: true, // defaults to false
60
+
61
+ // Hides the custom recurrence options
62
+ hideCustom: true, // defaults to false
63
+
64
+ // Configure the datepickers
65
+ // See https://www.sanity.io/docs/datetime-type#options
66
+ dateTimeOptions: {
67
+ dateFormat: 'DD/MM/yyyy',
68
+ timeFormat: 'HH:mm',
69
+ timeStep: 15,
70
+ },
71
+ }),
72
+ ],
73
+ })
74
+ ```
75
+
76
+ ### Field config
77
+
78
+ ```ts
79
+ defineField({
80
+ name: 'date',
81
+ title: 'Date',
82
+ type: 'recurringDates',
83
+ options: {
84
+ // An array of RRULE strings to use as default recurrences
85
+ defaultRecurrences: [
86
+ 'RRULE:FREQ=DAILY;INTERVAL=1',
87
+ 'RRULE:FREQ=WEEKLY;INTERVAL=1',
88
+ 'RRULE:FREQ=MONTHLY;INTERVAL=1',
89
+ 'RRULE:FREQ=YEARLY;INTERVAL=1',
90
+ ],
91
+
92
+ // Hides the end date field
93
+ hideEndDate: true, // defaults to false
94
+
95
+ // Hides the custom recurrence options
96
+ hideCustom: true, // defaults to false
97
+
98
+ // Configure the datepickers
99
+ // See https://www.sanity.io/docs/datetime-type#options
100
+ dateTimeOptions: {
101
+ dateFormat: 'DD/MM/yyyy',
102
+ timeFormat: 'HH:mm',
103
+ timeStep: 15,
104
+ },
105
+ },
106
+ })
107
+ ```
108
+
109
+ ## Content
110
+
111
+ Here is an example of the data stored on the field after configuring a recurring date:
112
+
113
+ ```json
114
+ {
115
+ "rrule": "RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=+1MO",
116
+ "startDate": "2023-08-01T12:47:00.000Z",
117
+ "endDate": "2023-08-01T15:15:00.000Z"
118
+ }
119
+ ```
120
+
121
+ As the plugin uses the standard Sanity `datetime` field type, the start and end dates are stored in UTC. The `rrule` string is in line with the recurrence rules defined in the [iCalendar RFC](https://tools.ietf.org/html/rfc5545), with rules generated by the [rrule.js](https://github.com/jakubroztocil/rrule) library.
122
+
123
+ ### Using the rules on a frontend
124
+
125
+ Whilst displaying the recurring dates on a frontend is beyond the scope of this plugin, there are a couple of considerations worth calling out.
126
+
127
+ As the start dates are stored in UTC, which is a "floating" time, you need to process these into the timezone you want to display the dates in. You'll also need to think about Daylight Saving Time (DST) in these timezones.
128
+
129
+ By using the `rrule` and `date-fns-tz` libraries, you can achieve this in a similar manner to the below:
130
+
131
+ ```ts
132
+ import {rrulestr} from 'rrule'
133
+ import {formatInTimeZone, getTimezoneOffset} from 'date-fns-tz'
134
+
135
+ // your desired timezone
136
+ const timezone = 'Europe/London'
137
+
138
+ // Pass your rrule and startDate to the `rrulestr` function
139
+ const upcomingRule = rrulestr(rrule, {
140
+ dtstart: startDate,
141
+ })
142
+
143
+ // Calculate the offset from UTC for your timezone at the start date
144
+ const initialOffset = getTimezoneOffset(timezone, new Date(startDate))
145
+
146
+ // Map through all the rules, returning an array of dates in the correct timezone
147
+ // with consideration for changes in timezone offset as a result of DST
148
+ const allDates = rule.all().map((date) => {
149
+ // Calculate the offset from UTC for your timezone at this recurrence
150
+ const dateOffset = getTimezoneOffset(timezone, date)
151
+
152
+ // Calculate the difference between the initial offset and this offset
153
+ const diff = initialOffset - dateOffset
154
+
155
+ // Adjust the date by the difference
156
+ date.setTime(date.getTime() + diff)
157
+
158
+ // Return a formatted date from the date needed, with the format we pass
159
+ // in the third parameter
160
+ return formatInTimeZone(
161
+ date.setTime(date.getTime() + diff),
162
+ timezone,
163
+ 'EEEE, MMMM d, yyyy h:mm aaaa',
164
+ )
165
+ })
166
+ ```
167
+
168
+ ## License
169
+
170
+ [MIT](LICENSE) © Tom Smith
171
+
172
+ ## Develop & test
173
+
174
+ This plugin uses [@sanity/plugin-kit](https://github.com/sanity-io/plugin-kit)
175
+ with default configuration for build & watch scripts.
176
+
177
+ See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
178
+ on how to run this plugin with hotreload in the studio.
179
+
180
+
181
+ ### Release new version
182
+
183
+ Run ["CI & Release" workflow](https://github.com/thebiggianthead/sanity-plugin-recurring-dates/actions/workflows/main.yml).
184
+ Make sure to select the main branch and check "Release new version".
185
+
186
+ Semantic release will only release on configured branches, so it is safe to run release on any branch.
@@ -0,0 +1,29 @@
1
+ import type {DatetimeOptions} from 'sanity'
2
+ import type {ObjectDefinition} from 'sanity'
3
+ import {Plugin as Plugin_2} from 'sanity'
4
+
5
+ export declare interface PluginConfig {
6
+ defaultRecurrences?: string[]
7
+ hideEndDate?: boolean
8
+ hideCustom?: boolean
9
+ dateTimeOptions?: DatetimeOptions
10
+ }
11
+
12
+ export declare type RecurringDateFieldOptions = Omit<ObjectDefinition, 'type' | 'fields'> & {
13
+ type: 'recurringDates'
14
+ options?: PluginConfig
15
+ }
16
+
17
+ export declare const recurringDates: Plugin_2<PluginConfig>
18
+
19
+ export declare type WithRequiredProperty<Type, Key extends keyof Type> = Type & {
20
+ [Property in Key]-?: Type[Property]
21
+ }
22
+
23
+ export {}
24
+
25
+ declare module 'sanity' {
26
+ interface IntrinsicDefinitions {
27
+ recurringDates: RecurringDateFieldOptions
28
+ }
29
+ }