-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
259 lines (231 loc) · 8.78 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import Boolean, { defaults as booleanDefaults } from "./fields/boolean.ts";
import Code, { defaults as codeDefaults } from "./fields/code.ts";
import Color, { defaults as colorDefaults } from "./fields/color.ts";
import DateTime, { defaults as dateTimeDefaults } from "./fields/datetime.ts";
import Field from "./fields/field.ts";
import File, { defaults as fileDefaults } from "./fields/file.ts";
import Hidden, { defaults as hiddenDefaults } from "./fields/hidden.ts";
import Image, { defaults as imageDefaults } from "./fields/image.ts";
import List, { defaults as listDefaults } from "./fields/list.ts";
import Map, { defaults as mapDefaults } from "./fields/map.ts";
import Markdown, { defaults as markdownDefaults } from "./fields/markdown.ts";
import Number, { defaults as numberDefaults } from "./fields/number.ts";
import ObjectField, { defaults as objectDefaults } from "./fields/object.ts";
import Relation, { defaults as relationDefaults } from "./fields/relation.ts";
import Select, {
defaults as selectDefaults,
Option as SelectOption,
} from "./fields/select.ts";
import String, { defaults as stringDefaults } from "./fields/string.ts";
import Text, { defaults as textDefaults } from "./fields/text.ts";
import Files, { File as FileCollection } from "./collections/files.ts";
import Folder from "./collections/folder.ts";
export {
Boolean,
Code,
Color,
DateTime,
Field,
File,
Hidden,
Image,
List,
Map,
Markdown,
Number,
ObjectField as Object,
Relation,
Select,
String,
Text,
};
const factory = {
/** Configure individual default settings for every field */
defaults: {
boolean: booleanDefaults,
code: codeDefaults,
color: colorDefaults,
datetime: dateTimeDefaults,
file: fileDefaults,
hidden: hiddenDefaults,
image: imageDefaults,
list: listDefaults,
map: mapDefaults,
markdown: markdownDefaults,
number: numberDefaults,
object: objectDefaults,
relation: relationDefaults,
select: selectDefaults,
string: stringDefaults,
text: textDefaults,
},
/** Set the default `required` option for all fields */
set defaultRequired(required: boolean) {
for (const field of Object.values(this.defaults)) {
field.required(required);
}
},
/**
* A files collection contains one or more uniquely configured files.
* Unlike items in folder collections, which repeat the same configuration over all files in the folder, each item in a files collection has an explicitly set path, filename, and configuration.
* This can be useful for unique files with a custom set of fields, like a settings file or a custom landing page with a unique content structure.
* @see https://www.netlifycms.org/docs/collection-types/#file-collections
*/
files(label: string): Files {
return new Files(label);
},
/**
* Folder collections represent one or more files with the same format, fields, and configuration options, all stored within the same folder in the repository.
* You might use a folder collection for blog posts, product pages, author data files, etc.
* Unlike file collections, folder collections have the option to allow editors to create new items in the collection.
* @see https://www.netlifycms.org/docs/collection-types/#folder-collections
*/
folder(label: string, folder: string, fields: Field[] = []): Folder {
return new Folder(label).folder(folder).fields(fields);
},
/**
* The boolean widget translates a toggle switch input to a true/false value.
* @see https://www.netlifycms.org/docs/widgets/#boolean
*/
boolean(label: string): Boolean {
return new Boolean(label);
},
/**
* The code widget provides a code editor (powered by Codemirror) with optional syntax awareness.
* Can output the raw code value or an object with the selected language and the raw code value.
* @see https://www.netlifycms.org/docs/widgets/#code
*/
code(label: string): Code {
return new Code(label);
},
/**
* The color widget translates a color picker to a color string.
* @see https://www.netlifycms.org/docs/widgets/#color
*/
color(label: string): Color {
return new Color(label);
},
/**
* The datetime widget translates a datetime picker to a datetime string.
* @see https://www.netlifycms.org/docs/widgets/#datetime
*/
datetime(label: string): DateTime {
return new DateTime(label);
},
/**
* The file widget allows editors to upload a file or select an existing one from the media library.
* The path to the file will be saved to the field as a string.
* @see https://www.netlifycms.org/docs/widgets/#file
*/
file(label: string): File {
return new File(label);
},
/**
* Hidden widgets do not display in the UI.
* In folder collections that allow users to create new items, you will often want to set a default for hidden fields,
* so they will be set without requiring an input.
* @see https://www.netlifycms.org/docs/widgets/#hidden
*/
hidden(label: string, value?: unknown): Hidden {
const field = new Hidden(label);
if (value !== undefined) {
field.default(value);
}
return field;
},
/**
* The image widget allows editors to upload an image or select an existing one from the media library.
* The path to the image file will be saved to the field as a string.
* @see https://www.netlifycms.org/docs/widgets/#image
*/
image(label: string): Image {
return new Image(label);
},
/**
* The list widget allows you to create a repeatable item in the UI which saves as a list of widget values.
* Map a user-provided string with a comma delimiter into a list.
* You can choose any widget as a child of a list widget—even other lists.
* @see https://www.netlifycms.org/docs/widgets/#list
*/
list(label: string, fields?: Field | Field[]): List {
const field = new List(label);
if (fields) {
field.fields(fields);
}
return field;
},
/**
* The map widget allows you to edit spatial data using an interactive map. Spatial data for a single piece of geometry saves as a GeoJSON string in WGS84 projection.
* @see https://www.netlifycms.org/docs/widgets/#map
*/
map(label: string): Map {
return new Map(label);
},
/**
* The markdown widget provides a full fledged text editor allowing users to format text with features such as headings and blockquotes. Users can change their editing view with a handy toggle button.
* Please note: If you want to use your markdown editor to fill a markdown file contents after its frontmatter, you'll have to name the field body so the CMS recognizes it and saves the file accordingly.
* @see https://www.netlifycms.org/docs/widgets/#markdown
*/
markdown(label: string): Markdown {
return new Markdown(label);
},
/**
* The number widget uses an HTML number input, saving the value as a string, integer, or floating point number.
* @see https://www.netlifycms.org/docs/widgets/#number
*/
number(label: string): Number {
return new Number(label);
},
/**
* The object widget allows you to group multiple widgets together, nested under a single field.
* You can choose any widget as a child of an object widget—even other objects.
* @see https://www.netlifycms.org/docs/widgets/#object
*/
object(label: string, fields: Field[]): ObjectField {
return new ObjectField(label).fields(fields);
},
/**
* The relation widget allows you to reference items from another collection.
* It provides a search input with a list of entries from the collection you're referencing, and the list automatically updates with matched entries based on what you've typed.
* @see https://www.netlifycms.org/docs/widgets/#relation
*/
relation(
label: string,
target?: Folder | FileCollection,
id?: string,
searchFields?: string[],
): Relation {
const relation = new Relation(label);
if (target) {
if (!id) {
throw new Error("Relation target must have an id");
}
relation.target(target, id, searchFields);
}
return relation;
},
/**
* The select widget allows you to pick a string value from a dropdown menu.
* @see https://www.netlifycms.org/docs/widgets/#select
*/
select(label: string, options: SelectOption[] = []): Select {
return new Select(label).options(options);
},
/**
* The string widget translates a basic text input to a string value.
* For larger textarea inputs, use the text widget.
* @see https://www.netlifycms.org/docs/widgets/#string
*/
string(label: string): String {
return new String(label);
},
/**
* The text widget takes a multiline text field and saves it as a string.
* For shorter text inputs, use the string widget.
* @see https://www.netlifycms.org/docs/widgets/#text
*/
text(label: string): Text {
return new Text(label);
},
};
export default factory;