@plasmicpkgs/plasmic-cms 0.0.284 → 0.0.285

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.
@@ -0,0 +1,1531 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __objRest = (source, exclude) => {
21
+ var target = {};
22
+ for (var prop in source)
23
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
+ target[prop] = source[prop];
25
+ if (source != null && __getOwnPropSymbols)
26
+ for (var prop of __getOwnPropSymbols(source)) {
27
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
+ target[prop] = source[prop];
29
+ }
30
+ return target;
31
+ };
32
+ var __async = (__this, __arguments, generator) => {
33
+ return new Promise((resolve, reject) => {
34
+ var fulfilled = (value) => {
35
+ try {
36
+ step(generator.next(value));
37
+ } catch (e) {
38
+ reject(e);
39
+ }
40
+ };
41
+ var rejected = (value) => {
42
+ try {
43
+ step(generator.throw(value));
44
+ } catch (e) {
45
+ reject(e);
46
+ }
47
+ };
48
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
49
+ step((generator = generator.apply(__this, __arguments)).next());
50
+ });
51
+ };
52
+
53
+ // src/index.tsx
54
+ import registerComponent from "@plasmicapp/host/registerComponent";
55
+ import registerGlobalContext from "@plasmicapp/host/registerGlobalContext";
56
+
57
+ // src/components.tsx
58
+ import { repeatedElement, usePlasmicCanvasContext } from "@plasmicapp/host";
59
+ import { usePlasmicQueryData } from "@plasmicapp/query";
60
+ import dayjs from "dayjs";
61
+ import React2 from "react";
62
+
63
+ // src/api.ts
64
+ function queryParamsToApi(params) {
65
+ return {
66
+ where: params.where,
67
+ limit: params.limit,
68
+ offset: params.offset,
69
+ order: params.orderBy ? [
70
+ {
71
+ field: params.orderBy,
72
+ dir: params.desc ? "desc" : "asc"
73
+ }
74
+ ] : void 0,
75
+ fields: params.fields
76
+ };
77
+ }
78
+ var HttpError = class extends Error {
79
+ constructor(status, message) {
80
+ super(message);
81
+ this.status = status;
82
+ }
83
+ };
84
+ var API = class {
85
+ constructor(config) {
86
+ this.config = config;
87
+ }
88
+ get(_0) {
89
+ return __async(this, arguments, function* (endpoint, params = {}) {
90
+ var _a;
91
+ const url = new URL(
92
+ `${this.config.host}/api/v1/cms/databases/${this.config.databaseId}${endpoint}`
93
+ );
94
+ const fixedParams = Object.keys(params).reduce((newObj, key) => {
95
+ const value = params[key];
96
+ if (value != null) {
97
+ newObj[key] = value;
98
+ }
99
+ return newObj;
100
+ }, {});
101
+ url.search = new URLSearchParams(fixedParams).toString();
102
+ const response = yield fetch(url.toString(), {
103
+ method: "GET",
104
+ headers: {
105
+ accept: "*/*",
106
+ "x-plasmic-api-cms-tokens": `${this.config.databaseId}:${this.config.databaseToken}`
107
+ },
108
+ mode: "cors"
109
+ });
110
+ if (response.status !== 200) {
111
+ let message = yield response.text();
112
+ try {
113
+ const json = JSON.parse(message);
114
+ if ((_a = json.error) == null ? void 0 : _a.message) {
115
+ message = json.error.message;
116
+ }
117
+ } catch (e) {
118
+ }
119
+ throw new HttpError(response.status, message);
120
+ }
121
+ return yield response.json();
122
+ });
123
+ }
124
+ fetchTables() {
125
+ return __async(this, null, function* () {
126
+ try {
127
+ const response = yield this.get(``);
128
+ return response.tables;
129
+ } catch (e) {
130
+ console.error(e);
131
+ throw e;
132
+ }
133
+ });
134
+ }
135
+ useDraftForTable(table) {
136
+ var _a;
137
+ if (Array.isArray(this.config.useDraft)) {
138
+ return this.config.useDraft.includes(table);
139
+ } else {
140
+ return (_a = this.config.useDraft) != null ? _a : false;
141
+ }
142
+ }
143
+ query(_0) {
144
+ return __async(this, arguments, function* (table, params = {}) {
145
+ try {
146
+ const response = yield this.get(`/tables/${table}/query`, {
147
+ q: JSON.stringify(queryParamsToApi(params)),
148
+ draft: Number(this.useDraftForTable(table) || params.useDraft),
149
+ locale: this.config.locale
150
+ });
151
+ return response.rows;
152
+ } catch (e) {
153
+ console.error(e);
154
+ throw e;
155
+ }
156
+ });
157
+ }
158
+ count(_0) {
159
+ return __async(this, arguments, function* (table, params = {}) {
160
+ try {
161
+ const response = yield this.get(`/tables/${table}/count`, {
162
+ q: JSON.stringify(queryParamsToApi(params)),
163
+ draft: Number(this.useDraftForTable(table) || params.useDraft)
164
+ });
165
+ return response.count;
166
+ } catch (e) {
167
+ console.error(e);
168
+ throw e;
169
+ }
170
+ });
171
+ }
172
+ };
173
+ function mkApi(config) {
174
+ if (!config) {
175
+ throw new Error("Component must be wrapped in 'CMS Data Provider'.");
176
+ }
177
+ return new API(config);
178
+ }
179
+
180
+ // src/constants.ts
181
+ var DEFAULT_HOST = "https://data.plasmic.app";
182
+
183
+ // src/context.tsx
184
+ import {
185
+ DataProvider,
186
+ useDataEnv,
187
+ useSelector
188
+ } from "@plasmicapp/host";
189
+ import React from "react";
190
+ var contextPrefix = "plasmicCms";
191
+ var databaseContextKey = `${contextPrefix}Database`;
192
+ var tablesContextKey = `${contextPrefix}Tables`;
193
+ var tableSchemaContextKey = `${contextPrefix}TableSchema`;
194
+ var collectionResultSuffix = `Collection`;
195
+ var mkQueryContextKey = (table) => `${contextPrefix}${capitalizeFirst(table)}${collectionResultSuffix}`;
196
+ var itemContextSuffix = `Item`;
197
+ var countContextSuffix = `Count`;
198
+ var modeContextSuffix = `Mode`;
199
+ var mkRowContextKey = (table) => `${contextPrefix}${capitalizeFirst(table)}${itemContextSuffix}`;
200
+ var mkCountContextKey = (table) => `${contextPrefix}${capitalizeFirst(table)}${countContextSuffix}`;
201
+ var mkModeContextKey = (table) => `${contextPrefix}${capitalizeFirst(table)}${modeContextSuffix}`;
202
+ function capitalizeFirst(str) {
203
+ var _a;
204
+ return ((_a = str[0]) == null ? void 0 : _a.toUpperCase()) + str.slice(1);
205
+ }
206
+ function useDatabase() {
207
+ return useSelector(databaseContextKey);
208
+ }
209
+ function makeDatabaseCacheKey(config) {
210
+ if (!config) {
211
+ return null;
212
+ }
213
+ const _a = config, { databaseToken } = _a, rest = __objRest(_a, ["databaseToken"]);
214
+ return JSON.stringify(rest);
215
+ }
216
+ function DatabaseProvider({
217
+ config,
218
+ children
219
+ }) {
220
+ return /* @__PURE__ */ React.createElement(DataProvider, { name: databaseContextKey, data: config, hidden: true }, children);
221
+ }
222
+ function useTables() {
223
+ return useSelector(tablesContextKey);
224
+ }
225
+ function TablesProvider({
226
+ children,
227
+ tables
228
+ }) {
229
+ return /* @__PURE__ */ React.createElement(DataProvider, { name: tablesContextKey, data: tables, hidden: true }, children);
230
+ }
231
+ function TableSchemaProvider({
232
+ children,
233
+ table
234
+ }) {
235
+ var _a, _b;
236
+ const tables = useTables();
237
+ let schema;
238
+ if (tables && (tables == null ? void 0 : tables.length) > 0) {
239
+ if (!table) {
240
+ schema = (_a = tables[0]) == null ? void 0 : _a.schema;
241
+ } else {
242
+ schema = (_b = tables == null ? void 0 : tables.find((t) => (t == null ? void 0 : t.identifier) === table)) == null ? void 0 : _b.schema;
243
+ }
244
+ }
245
+ return /* @__PURE__ */ React.createElement(DataProvider, { name: tableSchemaContextKey, data: schema }, children);
246
+ }
247
+ function getClosestMatchingKeysBy(env, pred) {
248
+ return [...Object.keys(env).reverse()].filter((key) => pred(key));
249
+ }
250
+ function QueryResultProvider({
251
+ children,
252
+ table,
253
+ rows,
254
+ hidden
255
+ }) {
256
+ return /* @__PURE__ */ React.createElement(
257
+ DataProvider,
258
+ {
259
+ name: table ? mkModeContextKey(table) : void 0,
260
+ data: "rows",
261
+ hidden: true
262
+ },
263
+ /* @__PURE__ */ React.createElement(
264
+ DataProvider,
265
+ {
266
+ name: table ? mkQueryContextKey(table) : void 0,
267
+ data: rows,
268
+ hidden
269
+ },
270
+ children
271
+ )
272
+ );
273
+ }
274
+ function useTablesWithDataLoaded(mode) {
275
+ const env = useDataEnv();
276
+ const tables = useTables();
277
+ if (!env) {
278
+ return void 0;
279
+ }
280
+ if (!tables) {
281
+ return void 0;
282
+ }
283
+ const matchingKeys = getClosestMatchingKeysBy(env, (key) => {
284
+ if (mode === "rows") {
285
+ return key.endsWith(itemContextSuffix);
286
+ } else if (mode === "count") {
287
+ return key.endsWith(countContextSuffix);
288
+ } else {
289
+ return key.endsWith(itemContextSuffix) || key.endsWith(countContextSuffix);
290
+ }
291
+ });
292
+ return tables.filter(
293
+ (table) => matchingKeys.some((key) => {
294
+ if (mode === "rows") {
295
+ return mkRowContextKey(table.identifier) === key;
296
+ } else if (mode === "count") {
297
+ return mkCountContextKey(table.identifier) === key;
298
+ } else {
299
+ return mkRowContextKey(table.identifier) === key || mkCountContextKey(table.identifier) === key;
300
+ }
301
+ })
302
+ );
303
+ }
304
+ function deriveTableId(tables, table) {
305
+ if (!table && tables && tables.length > 0) {
306
+ table = tables[0].identifier;
307
+ }
308
+ return table;
309
+ }
310
+ function useRow(tables, table) {
311
+ const env = useDataEnv();
312
+ if (!env) {
313
+ return void 0;
314
+ }
315
+ table = deriveTableId(tables, table);
316
+ if (table) {
317
+ return {
318
+ table,
319
+ row: env[mkRowContextKey(table)]
320
+ };
321
+ }
322
+ return void 0;
323
+ }
324
+ function useCount(tables, table) {
325
+ const env = useDataEnv();
326
+ if (!env) {
327
+ return void 0;
328
+ }
329
+ table = deriveTableId(tables, table);
330
+ if (table) {
331
+ return {
332
+ table,
333
+ count: env[mkCountContextKey(table)]
334
+ };
335
+ }
336
+ return void 0;
337
+ }
338
+ function RowProvider({
339
+ children,
340
+ table,
341
+ row
342
+ }) {
343
+ return /* @__PURE__ */ React.createElement(DataProvider, { name: mkRowContextKey(table), data: row }, children);
344
+ }
345
+ function CountProvider({
346
+ children,
347
+ table,
348
+ count
349
+ }) {
350
+ return /* @__PURE__ */ React.createElement(
351
+ DataProvider,
352
+ {
353
+ name: table ? mkModeContextKey(table) : void 0,
354
+ data: "count",
355
+ hidden: true
356
+ },
357
+ /* @__PURE__ */ React.createElement(
358
+ DataProvider,
359
+ {
360
+ name: table ? mkCountContextKey(table) : void 0,
361
+ data: count
362
+ },
363
+ children
364
+ )
365
+ );
366
+ }
367
+
368
+ // src/util.ts
369
+ function mkTableOptions(tables) {
370
+ if (!tables) {
371
+ return [];
372
+ }
373
+ return tables.map((table) => ({
374
+ value: table.identifier,
375
+ label: table.name
376
+ }));
377
+ }
378
+ function mkFieldOptions(tables, tableIdentifier, types) {
379
+ if (!tables) {
380
+ return [];
381
+ }
382
+ const table = tables.find((t) => t.identifier === tableIdentifier);
383
+ if (!table) {
384
+ return [];
385
+ }
386
+ let fields = table.schema.fields;
387
+ if (types) {
388
+ fields = fields.filter((f) => types.includes(f.type));
389
+ }
390
+ const options = fields.map((f) => ({
391
+ value: f.identifier,
392
+ label: f.name || f.identifier
393
+ }));
394
+ if (!options.some((option) => option.value === "_id")) {
395
+ options.push({
396
+ label: "System-assigned ID",
397
+ value: "_id"
398
+ });
399
+ }
400
+ return options;
401
+ }
402
+
403
+ // src/components.tsx
404
+ var modulePath = "@plasmicpkgs/plasmic-cms";
405
+ var componentPrefix = "hostless-plasmic-cms";
406
+ function renderMaybeData(maybeData, renderFn, loaderProps, inEditor, loadingMessage, forceLoadingState) {
407
+ if ("error" in maybeData) {
408
+ const error = maybeData.error;
409
+ if (!inEditor) {
410
+ return /* @__PURE__ */ React2.createElement(React2.Fragment, null, loadingMessage != null ? loadingMessage : /* @__PURE__ */ React2.createElement("div", null, "Loading..."));
411
+ }
412
+ if (error && error instanceof HttpError && error.status === 404) {
413
+ if (loaderProps.hideIfNotFound) {
414
+ return null;
415
+ } else {
416
+ return /* @__PURE__ */ React2.createElement("div", null, "Error: Data not found");
417
+ }
418
+ } else {
419
+ return /* @__PURE__ */ React2.createElement("div", null, "Error: ", error == null ? void 0 : error.message);
420
+ }
421
+ }
422
+ if (!("data" in maybeData) || forceLoadingState) {
423
+ return /* @__PURE__ */ React2.createElement(React2.Fragment, null, loadingMessage != null ? loadingMessage : /* @__PURE__ */ React2.createElement("div", null, "Loading..."));
424
+ }
425
+ return renderFn(maybeData.data);
426
+ }
427
+ var cmsCredentialsProviderMeta = {
428
+ name: `${componentPrefix}-credentials-provider`,
429
+ displayName: "CMS Credentials Provider",
430
+ description: `
431
+ Find (or create) your CMS in the [dashboard](https://studio.plasmic.app), and go to its Settings view for the ID and token.
432
+
433
+ [See tutorial video](https://docs.plasmic.app/learn/plasmic-cms/).`,
434
+ importName: "CmsCredentialsProvider",
435
+ importPath: modulePath,
436
+ providesData: true,
437
+ props: {
438
+ host: {
439
+ type: "string",
440
+ displayName: "Studio URL",
441
+ description: `The default host for use in production is ${DEFAULT_HOST}.`,
442
+ defaultValue: DEFAULT_HOST,
443
+ defaultValueHint: DEFAULT_HOST,
444
+ advanced: true
445
+ },
446
+ databaseId: {
447
+ type: "string",
448
+ displayName: "CMS ID",
449
+ description: "The ID of the CMS (database) to use. (Can get on the CMS settings page)"
450
+ },
451
+ databaseToken: {
452
+ type: "string",
453
+ displayName: "CMS Public Token",
454
+ description: "The Public Token of the CMS (database) you are using. (Can get on the CMS settings page)"
455
+ },
456
+ locale: {
457
+ type: "string",
458
+ displayName: "Locale",
459
+ description: "The locale to use for localized values, leave empty for the default locale."
460
+ }
461
+ }
462
+ };
463
+ function CmsCredentialsProvider({
464
+ children,
465
+ databaseId,
466
+ databaseToken,
467
+ host,
468
+ locale,
469
+ useDraft
470
+ }) {
471
+ const config = {
472
+ databaseId,
473
+ databaseToken,
474
+ locale,
475
+ host: host || DEFAULT_HOST,
476
+ useDraft: useDraft != null ? useDraft : false
477
+ };
478
+ return /* @__PURE__ */ React2.createElement(DatabaseProvider, { config }, /* @__PURE__ */ React2.createElement(TablesFetcher, null, children));
479
+ }
480
+ function TablesFetcher({ children }) {
481
+ const databaseConfig = useDatabase();
482
+ const cacheKey = JSON.stringify({
483
+ component: "TablesFetcher",
484
+ databaseConfig: makeDatabaseCacheKey(databaseConfig)
485
+ });
486
+ const maybeData = usePlasmicQueryData(cacheKey, () => __async(this, null, function* () {
487
+ if (!isDatabaseConfigured(databaseConfig)) {
488
+ return [];
489
+ }
490
+ return yield mkApi(databaseConfig).fetchTables();
491
+ }));
492
+ const inEditor = !!usePlasmicCanvasContext();
493
+ return /* @__PURE__ */ React2.createElement(TablesProvider, { tables: maybeData.data }, inEditor && maybeData.error ? /* @__PURE__ */ React2.createElement("div", null, "CMS Error: ", maybeData.error.message) : children);
494
+ }
495
+ function isDatabaseConfigured(config) {
496
+ return (config == null ? void 0 : config.databaseId) && (config == null ? void 0 : config.databaseToken);
497
+ }
498
+ var cmsQueryRepeaterMeta = {
499
+ name: `${componentPrefix}-query-repeater`,
500
+ displayName: "CMS Data Fetcher",
501
+ description: "Fetches CMS data. Repeats `children` slot content for each row fetched. [See tutorial video](https://docs.plasmic.app/learn/plasmic-cms/).",
502
+ importName: "CmsQueryRepeater",
503
+ importPath: modulePath,
504
+ providesData: true,
505
+ defaultStyles: {
506
+ display: "flex",
507
+ width: "stretch",
508
+ maxWidth: "100%",
509
+ flexDirection: "column"
510
+ },
511
+ props: {
512
+ children: {
513
+ type: "slot",
514
+ isRepeated: true,
515
+ defaultValue: {
516
+ type: "vbox",
517
+ children: [
518
+ {
519
+ type: "component",
520
+ name: `${componentPrefix}-row-field`
521
+ }
522
+ ]
523
+ }
524
+ },
525
+ table: {
526
+ type: "choice",
527
+ displayName: "Model",
528
+ description: "CMS model (table) to query.",
529
+ options: (_, ctx) => mkTableOptions(ctx == null ? void 0 : ctx.tables),
530
+ defaultValueHint: (_, ctx) => ctx == null ? void 0 : ctx.table
531
+ },
532
+ useDraft: {
533
+ type: "boolean",
534
+ displayName: "Use drafts?",
535
+ description: "If set, also query unpublished content.",
536
+ defaultValue: false,
537
+ hidden: () => true
538
+ },
539
+ mode: {
540
+ type: "choice",
541
+ options: [
542
+ { label: "Rows", value: "rows" },
543
+ { label: "Count", value: "count" }
544
+ ],
545
+ defaultValueHint: "rows"
546
+ },
547
+ where: {
548
+ type: "object",
549
+ displayName: "Filter",
550
+ description: "Filter clause, as a JSON in Mongo query format. Should not be used together with Filter field and Filter value",
551
+ advanced: true
552
+ },
553
+ filterField: {
554
+ type: "choice",
555
+ displayName: "Filter field",
556
+ description: "Field (from model schema) to filter by",
557
+ options: ({ table }, ctx) => {
558
+ var _a;
559
+ return mkFieldOptions(ctx == null ? void 0 : ctx.tables, (_a = ctx == null ? void 0 : ctx.table) != null ? _a : table, [
560
+ "number" /* NUMBER */,
561
+ "boolean" /* BOOLEAN */,
562
+ "text" /* TEXT */,
563
+ "long-text" /* LONG_TEXT */,
564
+ "ref" /* REF */
565
+ ]);
566
+ }
567
+ },
568
+ filterValue: {
569
+ type: "string",
570
+ displayName: "Filter value",
571
+ description: "Value to filter by, should be of filter field type"
572
+ },
573
+ orderBy: {
574
+ type: "choice",
575
+ displayName: "Order by",
576
+ description: "Field to order by.",
577
+ options: (_, ctx) => mkFieldOptions(ctx == null ? void 0 : ctx.tables, ctx == null ? void 0 : ctx.table, [
578
+ "number" /* NUMBER */,
579
+ "boolean" /* BOOLEAN */,
580
+ "date-time" /* DATE_TIME */,
581
+ "long-text" /* LONG_TEXT */,
582
+ "text" /* TEXT */
583
+ ]),
584
+ hidden: (ps) => ps.mode === "count"
585
+ },
586
+ desc: {
587
+ type: "boolean",
588
+ displayName: "Sort descending?",
589
+ description: 'Sort descending by "Order by" field.',
590
+ defaultValue: false,
591
+ hidden: (ps) => ps.mode === "count"
592
+ },
593
+ limit: {
594
+ type: "number",
595
+ displayName: "Limit",
596
+ description: "Maximum number of entries to fetch (0 for unlimited).",
597
+ defaultValue: 0,
598
+ min: 0,
599
+ hidden: (ps) => ps.mode === "count"
600
+ },
601
+ offset: {
602
+ type: "number",
603
+ displayName: "Offset",
604
+ description: "Skips this number of rows in the result set; used in combination with limit to build pagination",
605
+ min: 0,
606
+ hidden: (ps) => ps.mode === "count"
607
+ },
608
+ fields: {
609
+ type: "choice",
610
+ multiSelect: true,
611
+ displayName: "Fields",
612
+ description: "Fields from the CMS model to include with each row; by default, all fields are included",
613
+ options: ({ table }, ctx) => {
614
+ var _a;
615
+ return mkFieldOptions(ctx == null ? void 0 : ctx.tables, (_a = ctx == null ? void 0 : ctx.table) != null ? _a : table);
616
+ },
617
+ hidden: (ps) => ps.mode === "count"
618
+ },
619
+ emptyMessage: {
620
+ type: "slot",
621
+ defaultValue: {
622
+ type: "text",
623
+ value: "No matching published entries found."
624
+ }
625
+ },
626
+ forceEmptyState: {
627
+ type: "boolean",
628
+ displayName: "Force empty state",
629
+ description: "If set, will render as if no matching entries were found.",
630
+ defaultValue: false
631
+ },
632
+ loadingMessage: {
633
+ type: "slot",
634
+ defaultValue: {
635
+ type: "text",
636
+ value: "Loading..."
637
+ }
638
+ },
639
+ forceLoadingState: {
640
+ type: "boolean",
641
+ displayName: "Force loading state",
642
+ description: "If set, will render as if it is waiting for the query to run.",
643
+ defaultValue: false
644
+ },
645
+ noLayout: {
646
+ type: "boolean",
647
+ displayName: "No layout",
648
+ description: "When set, CMS Data Loader will not layout its children; instead, the layout set on its parent element will be used. Useful if you want to set flex gap or control container tag type.",
649
+ defaultValue: false
650
+ },
651
+ noAutoRepeat: {
652
+ type: "boolean",
653
+ displayName: "No auto-repeat",
654
+ description: "Do not automatically repeat children for every entry.",
655
+ defaultValue: false,
656
+ hidden: (ps) => ps.mode === "count"
657
+ }
658
+ }
659
+ };
660
+ function CmsQueryRepeater({
661
+ table,
662
+ children,
663
+ setControlContextData,
664
+ mode,
665
+ where,
666
+ useDraft,
667
+ orderBy,
668
+ desc,
669
+ limit,
670
+ offset,
671
+ emptyMessage,
672
+ forceEmptyState,
673
+ loadingMessage,
674
+ forceLoadingState,
675
+ noLayout,
676
+ noAutoRepeat,
677
+ className,
678
+ filterField,
679
+ filterValue,
680
+ fields
681
+ }) {
682
+ const databaseConfig = useDatabase();
683
+ const tables = useTables();
684
+ if (filterField && filterValue) {
685
+ where = {
686
+ [filterField]: filterValue
687
+ };
688
+ }
689
+ const params = { where, useDraft, orderBy, desc, limit, offset, fields };
690
+ if (!table && tables && tables.length > 0) {
691
+ table = tables[0].identifier;
692
+ }
693
+ const cacheKey = JSON.stringify({
694
+ component: "CmsQueryLoader",
695
+ mode,
696
+ table,
697
+ databaseConfig: makeDatabaseCacheKey(databaseConfig),
698
+ params
699
+ });
700
+ if (tables) {
701
+ setControlContextData == null ? void 0 : setControlContextData({ tables, table });
702
+ }
703
+ const maybeData = usePlasmicQueryData(cacheKey, () => __async(this, null, function* () {
704
+ if (!isDatabaseConfigured(databaseConfig)) {
705
+ throw new Error(`You must specify a CMS ID and API key`);
706
+ }
707
+ if (!table) {
708
+ throw new Error(`You must select a model to query`);
709
+ } else if (tables && !tables.find((t) => t.identifier === table)) {
710
+ throw new Error(`There is no model called "${table}"`);
711
+ } else if (mode === "count") {
712
+ return mkApi(databaseConfig).count(table, params);
713
+ } else {
714
+ return mkApi(databaseConfig).query(table, params);
715
+ }
716
+ }));
717
+ const inEditor = !!usePlasmicCanvasContext();
718
+ if (mode === "count") {
719
+ const node = renderMaybeData(
720
+ maybeData,
721
+ () => children,
722
+ { hideIfNotFound: false },
723
+ inEditor,
724
+ loadingMessage,
725
+ forceLoadingState
726
+ );
727
+ return /* @__PURE__ */ React2.createElement(TableSchemaProvider, { table }, /* @__PURE__ */ React2.createElement(
728
+ CountProvider,
729
+ {
730
+ table,
731
+ count: typeof (maybeData == null ? void 0 : maybeData.data) === "number" ? maybeData.data : void 0
732
+ },
733
+ node
734
+ ));
735
+ } else {
736
+ const node = renderMaybeData(
737
+ maybeData,
738
+ (rows) => {
739
+ if (rows.length === 0 || forceEmptyState) {
740
+ return emptyMessage;
741
+ }
742
+ return noAutoRepeat ? children : rows.map((row, index) => /* @__PURE__ */ React2.createElement(RowProvider, { key: index, table, row }, repeatedElement(index, children)));
743
+ },
744
+ { hideIfNotFound: false },
745
+ inEditor,
746
+ loadingMessage,
747
+ forceLoadingState
748
+ );
749
+ return /* @__PURE__ */ React2.createElement(TableSchemaProvider, { table }, /* @__PURE__ */ React2.createElement(
750
+ QueryResultProvider,
751
+ {
752
+ rows: Array.isArray(maybeData == null ? void 0 : maybeData.data) ? maybeData.data : void 0,
753
+ table
754
+ },
755
+ noLayout ? /* @__PURE__ */ React2.createElement(React2.Fragment, null, " ", node, " ") : /* @__PURE__ */ React2.createElement("div", { className }, " ", node, " ")
756
+ ));
757
+ }
758
+ }
759
+ var cmsRowFieldMeta = {
760
+ name: `${componentPrefix}-row-field`,
761
+ displayName: "CMS Entry Field",
762
+ importName: "CmsRowField",
763
+ importPath: modulePath,
764
+ props: {
765
+ table: {
766
+ type: "choice",
767
+ displayName: "Model",
768
+ hidden: (props, ctx) => {
769
+ var _a, _b;
770
+ return ((_b = (_a = ctx == null ? void 0 : ctx.tables) == null ? void 0 : _a.length) != null ? _b : 0) <= 1 && !props.table;
771
+ },
772
+ helpText: "Pick model from a CMS Data Fetcher",
773
+ description: "Usually not used! Only with multiple CMS Data Loaders, use this to choose which to show. Otherwise, go select the CMS Data Loader if you want to load different data.",
774
+ options: (_, ctx) => mkTableOptions(ctx == null ? void 0 : ctx.tables),
775
+ defaultValueHint: (_, ctx) => ctx == null ? void 0 : ctx.table
776
+ },
777
+ field: {
778
+ type: "choice",
779
+ displayName: "Field",
780
+ description: "Field (from model schema) to use.",
781
+ options: ({ table }, ctx) => {
782
+ var _a;
783
+ return mkFieldOptions(ctx == null ? void 0 : ctx.tables, (_a = ctx == null ? void 0 : ctx.table) != null ? _a : table, [
784
+ "number" /* NUMBER */,
785
+ "boolean" /* BOOLEAN */,
786
+ "text" /* TEXT */,
787
+ "long-text" /* LONG_TEXT */,
788
+ "date-time" /* DATE_TIME */,
789
+ "rich-text" /* RICH_TEXT */,
790
+ "image" /* IMAGE */,
791
+ "file" /* FILE */,
792
+ "enum" /* ENUM */
793
+ ]);
794
+ },
795
+ defaultValueHint: (_, ctx) => {
796
+ var _a, _b;
797
+ return ((_a = ctx == null ? void 0 : ctx.fieldMeta) == null ? void 0 : _a.name) || ((_b = ctx == null ? void 0 : ctx.fieldMeta) == null ? void 0 : _b.identifier);
798
+ }
799
+ },
800
+ dateFormat: {
801
+ type: "choice",
802
+ displayName: "Date Format",
803
+ hidden: ({ field }, ctx) => {
804
+ if (!ctx) {
805
+ return true;
806
+ }
807
+ const { table: tableIdentifier, tables } = ctx;
808
+ const table = tables == null ? void 0 : tables.find((t) => t.identifier === tableIdentifier);
809
+ if (!table) {
810
+ return true;
811
+ }
812
+ const fieldMeta = table.schema.fields.find(
813
+ (f) => f.identifier === field
814
+ );
815
+ if (!fieldMeta) {
816
+ return true;
817
+ }
818
+ return fieldMeta.type !== "date-time" /* DATE_TIME */;
819
+ },
820
+ options: [
821
+ {
822
+ label: "July 26, 2014",
823
+ value: "MMMM D, YYYY"
824
+ },
825
+ {
826
+ label: "July 26, 2014 10:02 PM",
827
+ value: "MMMM D, YYYY h:mm A"
828
+ },
829
+ {
830
+ label: "Jul 26, 2014",
831
+ value: "MMM D, YYYY"
832
+ },
833
+ {
834
+ label: "Jul 26, 2014 10:02 PM",
835
+ value: "MMM D, YYYY h:mm A"
836
+ },
837
+ {
838
+ label: "Saturday, July 26, 2014",
839
+ value: "dddd, MMMM D, YYYY"
840
+ },
841
+ {
842
+ label: "7/26/2014",
843
+ value: "M/D/YYYY"
844
+ },
845
+ {
846
+ label: "7/26/2014 10:02 PM",
847
+ value: "M/D/YYYY h:mm A"
848
+ },
849
+ {
850
+ label: "26/7/2014",
851
+ value: "D/M/YYYY"
852
+ },
853
+ {
854
+ label: "26/7/2014 10:02 PM",
855
+ value: "D/M/YYYY h:mm A"
856
+ },
857
+ {
858
+ label: "7/26/14",
859
+ value: "M/D/YY"
860
+ },
861
+ {
862
+ label: "7/26/14 10:02 PM",
863
+ value: "M/D/YY h:mm A"
864
+ },
865
+ {
866
+ label: "26/7/14",
867
+ value: "D/M/YY"
868
+ },
869
+ {
870
+ label: "26/7/14 10:02 PM",
871
+ value: "D/M/YY h:mm A"
872
+ }
873
+ ]
874
+ },
875
+ usePlasmicTheme: {
876
+ type: "boolean",
877
+ displayName: "Use Plasmic tag styles?",
878
+ description: "For HTML content, use tag styles defined in Plasmic",
879
+ advanced: true
880
+ },
881
+ themeResetClassName: {
882
+ type: "themeResetClass",
883
+ targetAllTags: true
884
+ }
885
+ },
886
+ defaultStyles: {
887
+ objectFit: "cover"
888
+ }
889
+ };
890
+ function CmsRowField(_a) {
891
+ var _b = _a, {
892
+ className,
893
+ table,
894
+ field,
895
+ dateFormat,
896
+ setControlContextData,
897
+ usePlasmicTheme,
898
+ themeResetClassName
899
+ } = _b, rest = __objRest(_b, [
900
+ "className",
901
+ "table",
902
+ "field",
903
+ "dateFormat",
904
+ "setControlContextData",
905
+ "usePlasmicTheme",
906
+ "themeResetClassName"
907
+ ]);
908
+ var _a2;
909
+ const tables = useTablesWithDataLoaded("rows");
910
+ const res = useRow(tables, table);
911
+ const unknown = /* @__PURE__ */ React2.createElement("div", __spreadValues({ className }, rest), "Field ", table != null ? table : "Unknown Model", ".", field != null ? field : "Unknown Field");
912
+ const fieldMeta = res ? deriveInferredTableField({
913
+ table: res.table,
914
+ tables,
915
+ field,
916
+ typeFilters: [
917
+ "text" /* TEXT */,
918
+ "long-text" /* LONG_TEXT */,
919
+ "rich-text" /* RICH_TEXT */
920
+ ]
921
+ }) : void 0;
922
+ if (tables) {
923
+ setControlContextData == null ? void 0 : setControlContextData(__spreadValues({
924
+ tables
925
+ }, res && res.row ? { table: res.table, row: res.row, fieldMeta } : {}));
926
+ }
927
+ if (!res) {
928
+ return unknown;
929
+ }
930
+ if (!res.row) {
931
+ return /* @__PURE__ */ React2.createElement("div", { className }, "Error: No CMS Entry found");
932
+ }
933
+ if (!fieldMeta) {
934
+ return unknown;
935
+ }
936
+ let data = (_a2 = res.row.data) == null ? void 0 : _a2[fieldMeta.identifier];
937
+ if (!data) {
938
+ return null;
939
+ }
940
+ if (fieldMeta.type === "date-time" /* DATE_TIME */ && dateFormat) {
941
+ data = dayjs(data).format(dateFormat);
942
+ }
943
+ return data ? renderValue(data, fieldMeta.type, __spreadValues({
944
+ className: `${usePlasmicTheme ? themeResetClassName : ""} ${className}`
945
+ }, rest)) : null;
946
+ }
947
+ var cmsCountFieldMeta = {
948
+ name: `${componentPrefix}-count`,
949
+ displayName: "CMS Entries Count",
950
+ importName: "CmsCount",
951
+ importPath: modulePath,
952
+ props: {
953
+ table: {
954
+ type: "choice",
955
+ displayName: "Model",
956
+ hidden: (props, ctx) => {
957
+ var _a, _b;
958
+ return ((_b = (_a = ctx == null ? void 0 : ctx.tables) == null ? void 0 : _a.length) != null ? _b : 0) <= 1 && !props.table;
959
+ },
960
+ helpText: "Pick model from a CMS Data Fetcher",
961
+ description: "Usually not used! Only with multiple CMS Data Loaders, use this to choose which to show. Otherwise, go select the CMS Data Loader if you want to load different data.",
962
+ options: (_, ctx) => mkTableOptions(ctx == null ? void 0 : ctx.tables),
963
+ defaultValueHint: (_, ctx) => ctx == null ? void 0 : ctx.table
964
+ }
965
+ }
966
+ };
967
+ function CmsCount(_a) {
968
+ var _b = _a, {
969
+ className,
970
+ table,
971
+ setControlContextData: _
972
+ } = _b, rest = __objRest(_b, [
973
+ "className",
974
+ "table",
975
+ "setControlContextData"
976
+ ]);
977
+ const tables = useTablesWithDataLoaded("count");
978
+ const res = useCount(tables, table);
979
+ const unknown = /* @__PURE__ */ React2.createElement("div", __spreadValues({ className }, rest), "Count: ", table != null ? table : "Unknown Model");
980
+ if (!res) {
981
+ return unknown;
982
+ }
983
+ if (res.count == null) {
984
+ return null;
985
+ } else {
986
+ return /* @__PURE__ */ React2.createElement("div", __spreadValues({ className }, rest), new Intl.NumberFormat().format(res.count));
987
+ }
988
+ }
989
+ var DEFAULT_TYPE_FILTERS = ["text" /* TEXT */];
990
+ function deriveInferredTableField(opts) {
991
+ var _a;
992
+ const { table, tables, field, typeFilters } = opts;
993
+ if (!table)
994
+ return void 0;
995
+ const schema = (_a = tables == null ? void 0 : tables.find((t) => t.identifier === table)) == null ? void 0 : _a.schema;
996
+ const fieldMeta = field ? schema == null ? void 0 : schema.fields.find((f) => f.identifier === field) : schema == null ? void 0 : schema.fields.find(
997
+ (f) => (typeFilters != null ? typeFilters : DEFAULT_TYPE_FILTERS).includes(f.type)
998
+ );
999
+ return fieldMeta;
1000
+ }
1001
+ function assertNever(_) {
1002
+ throw new Error("unexpected branch taken");
1003
+ }
1004
+ function renderValue(value, type, props) {
1005
+ switch (type) {
1006
+ case "number" /* NUMBER */:
1007
+ case "boolean" /* BOOLEAN */:
1008
+ case "text" /* TEXT */:
1009
+ case "long-text" /* LONG_TEXT */:
1010
+ case "date-time" /* DATE_TIME */:
1011
+ case "enum" /* ENUM */:
1012
+ case "ref" /* REF */:
1013
+ return /* @__PURE__ */ React2.createElement("div", __spreadValues({}, props), value);
1014
+ case "rich-text" /* RICH_TEXT */:
1015
+ return /* @__PURE__ */ React2.createElement(
1016
+ "div",
1017
+ __spreadValues({
1018
+ dangerouslySetInnerHTML: { __html: value },
1019
+ style: { whiteSpace: "normal" }
1020
+ }, props)
1021
+ );
1022
+ case "image" /* IMAGE */:
1023
+ if (value && typeof value === "object" && value.url && value.imageMeta) {
1024
+ return /* @__PURE__ */ React2.createElement(
1025
+ "img",
1026
+ __spreadValues({
1027
+ src: value.url,
1028
+ width: value.imageMeta.width,
1029
+ height: value.imageMeta.height
1030
+ }, props)
1031
+ );
1032
+ }
1033
+ return null;
1034
+ case "file" /* FILE */:
1035
+ if (value && typeof value === "object" && value.url && value.name) {
1036
+ return /* @__PURE__ */ React2.createElement("a", __spreadValues({ href: value.url, target: "_blank" }, props), value.name);
1037
+ }
1038
+ return null;
1039
+ default:
1040
+ assertNever(type);
1041
+ }
1042
+ }
1043
+ var cmsRowLinkMeta = {
1044
+ name: `${componentPrefix}-row-link`,
1045
+ displayName: "CMS Entry Link",
1046
+ importName: "CmsRowLink",
1047
+ importPath: modulePath,
1048
+ props: {
1049
+ children: {
1050
+ type: "slot",
1051
+ defaultValue: {
1052
+ type: "text",
1053
+ tag: "a",
1054
+ value: "Link"
1055
+ }
1056
+ },
1057
+ table: {
1058
+ type: "choice",
1059
+ displayName: "Model",
1060
+ hidden: (props, ctx) => {
1061
+ var _a, _b;
1062
+ return ((_b = (_a = ctx == null ? void 0 : ctx.tables) == null ? void 0 : _a.length) != null ? _b : 0) <= 1 && !props.table;
1063
+ },
1064
+ helpText: "Pick model from a CMS Data Fetcher",
1065
+ description: "Usually not used! Only with multiple CMS Data Loaders, use this to choose which to show. Otherwise, go select the CMS Data Loader if you want to load different data.",
1066
+ options: (_, ctx) => mkTableOptions(ctx == null ? void 0 : ctx.tables),
1067
+ defaultValueHint: (_, ctx) => ctx == null ? void 0 : ctx.table
1068
+ },
1069
+ field: {
1070
+ type: "choice",
1071
+ displayName: "Field",
1072
+ description: "Field (from model schema) to use.",
1073
+ options: ({ table }, ctx) => {
1074
+ var _a;
1075
+ return mkFieldOptions(ctx == null ? void 0 : ctx.tables, (_a = ctx == null ? void 0 : ctx.table) != null ? _a : table);
1076
+ },
1077
+ defaultValueHint: (_, ctx) => {
1078
+ var _a, _b;
1079
+ return ((_a = ctx == null ? void 0 : ctx.fieldMeta) == null ? void 0 : _a.name) || ((_b = ctx == null ? void 0 : ctx.fieldMeta) == null ? void 0 : _b.identifier);
1080
+ }
1081
+ },
1082
+ hrefProp: {
1083
+ type: "string",
1084
+ displayName: '"href" prop',
1085
+ description: "Prop to inject into children",
1086
+ defaultValue: "href"
1087
+ },
1088
+ prefix: {
1089
+ type: "string",
1090
+ displayName: "Optional prefix",
1091
+ description: "Prefix to prepend to prop value.",
1092
+ hidden: (_, ctx) => {
1093
+ var _a;
1094
+ return ((_a = ctx == null ? void 0 : ctx.fieldMeta) == null ? void 0 : _a.type) === "file";
1095
+ }
1096
+ },
1097
+ suffix: {
1098
+ type: "string",
1099
+ displayName: "Optional suffix",
1100
+ description: "Suffix to append to prop value.",
1101
+ hidden: (_, ctx) => {
1102
+ var _a;
1103
+ return ((_a = ctx == null ? void 0 : ctx.fieldMeta) == null ? void 0 : _a.type) === "file";
1104
+ }
1105
+ }
1106
+ }
1107
+ };
1108
+ function CmsRowLink({
1109
+ table,
1110
+ field,
1111
+ hrefProp,
1112
+ children,
1113
+ setControlContextData,
1114
+ prefix,
1115
+ suffix
1116
+ }) {
1117
+ var _a;
1118
+ const tables = useTablesWithDataLoaded("rows");
1119
+ const res = useRow(tables, table);
1120
+ if (!res || !res.row) {
1121
+ return /* @__PURE__ */ React2.createElement(React2.Fragment, null, children);
1122
+ }
1123
+ const fieldMeta = deriveInferredTableField({
1124
+ table: res.table,
1125
+ tables,
1126
+ field,
1127
+ typeFilters: ["file" /* FILE */, "text" /* TEXT */]
1128
+ });
1129
+ if (tables) {
1130
+ setControlContextData == null ? void 0 : setControlContextData({
1131
+ tables,
1132
+ table: res.table,
1133
+ row: res.row,
1134
+ fieldMeta
1135
+ });
1136
+ }
1137
+ if (!fieldMeta) {
1138
+ return /* @__PURE__ */ React2.createElement(React2.Fragment, null, children);
1139
+ }
1140
+ if (!children) {
1141
+ return null;
1142
+ }
1143
+ const value = ((_a = res.row.data) == null ? void 0 : _a[fieldMeta.identifier]) || "";
1144
+ const childrenWithProps = React2.Children.map(children, (child) => {
1145
+ if (React2.isValidElement(child)) {
1146
+ return React2.cloneElement(child, {
1147
+ [hrefProp]: fieldMeta.type === "file" /* FILE */ ? value.url : prefix || suffix ? `${prefix || ""}${value}${suffix || ""}` : value
1148
+ });
1149
+ }
1150
+ return child;
1151
+ });
1152
+ return /* @__PURE__ */ React2.createElement(React2.Fragment, null, childrenWithProps != null ? childrenWithProps : null);
1153
+ }
1154
+ var cmsRowImageMeta = {
1155
+ name: `${componentPrefix}-row-image`,
1156
+ displayName: "CMS Entry Image",
1157
+ importName: "CmsRowImage",
1158
+ importPath: modulePath,
1159
+ props: {
1160
+ children: {
1161
+ type: "slot",
1162
+ defaultValue: {
1163
+ type: "img",
1164
+ src: "https://studio.plasmic.app/static/img/placeholder-full.png"
1165
+ }
1166
+ },
1167
+ table: {
1168
+ type: "choice",
1169
+ displayName: "Model",
1170
+ hidden: (props, ctx) => {
1171
+ var _a, _b;
1172
+ return ((_b = (_a = ctx == null ? void 0 : ctx.tables) == null ? void 0 : _a.length) != null ? _b : 0) <= 1 && !props.table;
1173
+ },
1174
+ helpText: "Pick model from a CMS Data Fetcher",
1175
+ description: "Usually not used! Only with multiple CMS Data Loaders, use this to choose which to show. Otherwise, go select the CMS Data Loader if you want to load different data.",
1176
+ options: (_, ctx) => mkTableOptions(ctx == null ? void 0 : ctx.tables),
1177
+ defaultValueHint: (_, ctx) => ctx == null ? void 0 : ctx.table
1178
+ },
1179
+ field: {
1180
+ type: "choice",
1181
+ displayName: "Field",
1182
+ description: "Field (from model schema) to use.",
1183
+ options: ({ table }, ctx) => {
1184
+ var _a;
1185
+ return mkFieldOptions(ctx == null ? void 0 : ctx.tables, (_a = ctx == null ? void 0 : ctx.table) != null ? _a : table, ["image" /* IMAGE */]);
1186
+ },
1187
+ defaultValueHint: (_, ctx) => {
1188
+ var _a, _b;
1189
+ return ((_a = ctx == null ? void 0 : ctx.fieldMeta) == null ? void 0 : _a.name) || ((_b = ctx == null ? void 0 : ctx.fieldMeta) == null ? void 0 : _b.identifier);
1190
+ }
1191
+ },
1192
+ srcProp: {
1193
+ type: "string",
1194
+ displayName: 'Image "src" prop',
1195
+ description: "Prop to inject into children",
1196
+ defaultValue: "src"
1197
+ }
1198
+ }
1199
+ };
1200
+ function CmsRowImage({
1201
+ table,
1202
+ field,
1203
+ srcProp,
1204
+ children,
1205
+ setControlContextData
1206
+ }) {
1207
+ var _a;
1208
+ const tables = useTablesWithDataLoaded("rows");
1209
+ const res = useRow(tables, table);
1210
+ if (!res || !res.row) {
1211
+ return /* @__PURE__ */ React2.createElement(React2.Fragment, null, children);
1212
+ }
1213
+ const fieldMeta = deriveInferredTableField({
1214
+ table: res.table,
1215
+ tables,
1216
+ field,
1217
+ typeFilters: ["image" /* IMAGE */]
1218
+ });
1219
+ if (tables) {
1220
+ setControlContextData == null ? void 0 : setControlContextData({
1221
+ tables,
1222
+ table: res.table,
1223
+ row: res.row,
1224
+ fieldMeta
1225
+ });
1226
+ }
1227
+ if (!fieldMeta) {
1228
+ return /* @__PURE__ */ React2.createElement(React2.Fragment, null, children);
1229
+ }
1230
+ const value = ((_a = res.row.data) == null ? void 0 : _a[fieldMeta.identifier]) || "";
1231
+ const childrenWithProps = React2.Children.map(children, (child) => {
1232
+ if (React2.isValidElement(child) && value) {
1233
+ if (typeof value === "object" && value.url && value.imageMeta) {
1234
+ return React2.cloneElement(child, {
1235
+ [srcProp]: {
1236
+ src: value.url,
1237
+ fullHeight: value.imageMeta.height,
1238
+ fullWidth: value.imageMeta.width
1239
+ }
1240
+ });
1241
+ }
1242
+ return React2.cloneElement(child, { [srcProp]: value });
1243
+ }
1244
+ return child;
1245
+ });
1246
+ return /* @__PURE__ */ React2.createElement(React2.Fragment, null, childrenWithProps);
1247
+ }
1248
+ var cmsRowFieldValueMeta = {
1249
+ name: `${componentPrefix}-row-value`,
1250
+ displayName: "CMS Entry Value",
1251
+ importName: "CmsRowFieldValue",
1252
+ importPath: modulePath,
1253
+ props: {
1254
+ children: {
1255
+ type: "slot"
1256
+ },
1257
+ table: {
1258
+ type: "choice",
1259
+ displayName: "Model",
1260
+ hidden: (props, ctx) => {
1261
+ var _a, _b;
1262
+ return ((_b = (_a = ctx == null ? void 0 : ctx.tables) == null ? void 0 : _a.length) != null ? _b : 0) <= 1 && !props.table;
1263
+ },
1264
+ helpText: "Pick model from a CMS Data Fetcher",
1265
+ description: "Usually not used! Only with multiple CMS Data Loaders, use this to choose which to show. Otherwise, go select the CMS Data Loader if you want to load different data.",
1266
+ options: (_, ctx) => mkTableOptions(ctx == null ? void 0 : ctx.tables),
1267
+ defaultValueHint: (_, ctx) => ctx == null ? void 0 : ctx.table
1268
+ },
1269
+ field: {
1270
+ type: "choice",
1271
+ displayName: "Field",
1272
+ description: "Field (from model schema) to use.",
1273
+ options: ({ table }, ctx) => {
1274
+ var _a;
1275
+ return mkFieldOptions(ctx == null ? void 0 : ctx.tables, (_a = ctx == null ? void 0 : ctx.table) != null ? _a : table);
1276
+ },
1277
+ defaultValueHint: (_, ctx) => {
1278
+ var _a, _b;
1279
+ return ((_a = ctx == null ? void 0 : ctx.fieldMeta) == null ? void 0 : _a.name) || ((_b = ctx == null ? void 0 : ctx.fieldMeta) == null ? void 0 : _b.identifier);
1280
+ }
1281
+ },
1282
+ valueProp: {
1283
+ type: "string",
1284
+ displayName: "Value prop",
1285
+ description: "Prop to inject into children as",
1286
+ defaultValue: "children"
1287
+ }
1288
+ }
1289
+ };
1290
+ function CmsRowFieldValue(_a) {
1291
+ var _b = _a, {
1292
+ table,
1293
+ field,
1294
+ valueProp,
1295
+ children,
1296
+ setControlContextData
1297
+ } = _b, rest = __objRest(_b, [
1298
+ "table",
1299
+ "field",
1300
+ "valueProp",
1301
+ "children",
1302
+ "setControlContextData"
1303
+ ]);
1304
+ var _a2;
1305
+ const tables = useTablesWithDataLoaded("rows");
1306
+ const res = useRow(tables, table);
1307
+ if (!res || !res.row) {
1308
+ return /* @__PURE__ */ React2.createElement(React2.Fragment, null, children);
1309
+ }
1310
+ const fieldMeta = deriveInferredTableField({
1311
+ table: res.table,
1312
+ tables,
1313
+ field,
1314
+ typeFilters: ["text" /* TEXT */]
1315
+ });
1316
+ if (tables) {
1317
+ setControlContextData == null ? void 0 : setControlContextData({
1318
+ tables,
1319
+ table: res.table,
1320
+ row: res.row,
1321
+ fieldMeta
1322
+ });
1323
+ }
1324
+ if (!fieldMeta) {
1325
+ return /* @__PURE__ */ React2.createElement(React2.Fragment, null, children);
1326
+ }
1327
+ const value = ((_a2 = res.row.data) == null ? void 0 : _a2[fieldMeta.identifier]) || "";
1328
+ const childrenWithProps = React2.Children.map(children, (child) => {
1329
+ if (React2.isValidElement(child)) {
1330
+ return React2.cloneElement(child, __spreadProps(__spreadValues({}, rest), { [valueProp]: value }));
1331
+ }
1332
+ return child;
1333
+ });
1334
+ return /* @__PURE__ */ React2.createElement(React2.Fragment, null, childrenWithProps);
1335
+ }
1336
+
1337
+ // src/custom-functions.ts
1338
+ import registerFunction from "@plasmicapp/host/registerFunction";
1339
+ function getCmsHost() {
1340
+ var _a;
1341
+ return (_a = globalThis["__PLASMIC_CMS_HOST__"]) != null ? _a : DEFAULT_HOST;
1342
+ }
1343
+ function createTableOptions(_args, ctx) {
1344
+ if (!(ctx == null ? void 0 : ctx.tables)) {
1345
+ return [];
1346
+ }
1347
+ return ctx.tables.map((table) => ({
1348
+ value: table.identifier,
1349
+ label: table.name
1350
+ }));
1351
+ }
1352
+ var sharedTableFnContext = (cmsId, cmsPublicToken, ..._args) => {
1353
+ if (!cmsId || !cmsPublicToken) {
1354
+ return {
1355
+ dataKey: "",
1356
+ fetcher: () => __async(void 0, null, function* () {
1357
+ return { tables: [] };
1358
+ })
1359
+ };
1360
+ }
1361
+ return {
1362
+ dataKey: `cms_tables/${JSON.stringify({
1363
+ cmsId,
1364
+ cmsPublicToken
1365
+ })}`,
1366
+ fetcher: () => __async(void 0, null, function* () {
1367
+ const api = mkApi({
1368
+ databaseId: cmsId,
1369
+ databaseToken: cmsPublicToken,
1370
+ host: getCmsHost()
1371
+ });
1372
+ const tables = yield api.fetchTables();
1373
+ return { tables };
1374
+ })
1375
+ };
1376
+ };
1377
+ var cmsIdParam = {
1378
+ name: "cmsId",
1379
+ type: "string",
1380
+ description: "The cms ID"
1381
+ };
1382
+ var cmsPublicTokenParam = {
1383
+ name: "cmsPublicToken",
1384
+ type: "string",
1385
+ description: "The cms public token"
1386
+ };
1387
+ var tableIdParam = {
1388
+ name: "tableId",
1389
+ type: "choice",
1390
+ options: createTableOptions
1391
+ };
1392
+ var paramsParam = {
1393
+ name: "params",
1394
+ type: "object",
1395
+ description: "The parameters to filter the content (e.g., for sorting, limit, offset, advanced queries)"
1396
+ };
1397
+ var useDraftParam = {
1398
+ name: "useDraft",
1399
+ type: "boolean",
1400
+ description: "Whether to use draft data. Defaults to false."
1401
+ };
1402
+ var localeParam = {
1403
+ name: "locale",
1404
+ type: "string",
1405
+ description: "The locale to use. Defaults to empty string."
1406
+ };
1407
+ function fetchTables(cmsId, cmsPublicToken) {
1408
+ return __async(this, null, function* () {
1409
+ const api = mkApi({
1410
+ databaseId: cmsId,
1411
+ databaseToken: cmsPublicToken,
1412
+ host: getCmsHost()
1413
+ });
1414
+ return api.fetchTables();
1415
+ });
1416
+ }
1417
+ function fetchContent(cmsId, cmsPublicToken, tableId, params, useDraft, locale) {
1418
+ return __async(this, null, function* () {
1419
+ const api = mkApi({
1420
+ databaseId: cmsId,
1421
+ databaseToken: cmsPublicToken,
1422
+ host: getCmsHost(),
1423
+ useDraft,
1424
+ locale
1425
+ });
1426
+ return api.query(tableId, params);
1427
+ });
1428
+ }
1429
+ function fetchCount(cmsId, cmsPublicToken, tableId, params, useDraft) {
1430
+ return __async(this, null, function* () {
1431
+ const api = mkApi({
1432
+ databaseId: cmsId,
1433
+ databaseToken: cmsPublicToken,
1434
+ host: getCmsHost(),
1435
+ useDraft
1436
+ });
1437
+ return api.count(tableId, params);
1438
+ });
1439
+ }
1440
+ function registerAllCustomFunctions(loader) {
1441
+ function _registerFunction(fn, meta) {
1442
+ if (loader) {
1443
+ loader.registerFunction(fn, meta);
1444
+ } else {
1445
+ registerFunction(fn, meta);
1446
+ }
1447
+ }
1448
+ _registerFunction(fetchTables, {
1449
+ name: "fetchTables",
1450
+ description: "Fetches the tables from the cms",
1451
+ importPath: "@plasmicpkgs/plasmic-cms",
1452
+ params: [cmsIdParam, cmsPublicTokenParam]
1453
+ });
1454
+ _registerFunction(fetchContent, {
1455
+ name: "fetchContent",
1456
+ description: "Fetch content from a cms table",
1457
+ importPath: "@plasmicpkgs/plasmic-cms",
1458
+ params: [
1459
+ cmsIdParam,
1460
+ cmsPublicTokenParam,
1461
+ tableIdParam,
1462
+ paramsParam,
1463
+ useDraftParam,
1464
+ localeParam
1465
+ ],
1466
+ fnContext: sharedTableFnContext
1467
+ });
1468
+ _registerFunction(fetchCount, {
1469
+ name: "fetchCount",
1470
+ description: "Fetch the count of entries from a cms table",
1471
+ importPath: "@plasmicpkgs/plasmic-cms",
1472
+ params: [
1473
+ cmsIdParam,
1474
+ cmsPublicTokenParam,
1475
+ tableIdParam,
1476
+ paramsParam,
1477
+ useDraftParam
1478
+ ],
1479
+ fnContext: sharedTableFnContext
1480
+ });
1481
+ }
1482
+
1483
+ // src/index.tsx
1484
+ function registerAll(loader) {
1485
+ const _registerComponent = (Component, defaultMeta) => {
1486
+ if (loader) {
1487
+ loader.registerComponent(Component, defaultMeta);
1488
+ } else {
1489
+ registerComponent(Component, defaultMeta);
1490
+ }
1491
+ };
1492
+ const _registerGlobalContext = (Component, defaultMeta) => {
1493
+ if (loader) {
1494
+ loader.registerGlobalContext(Component, defaultMeta);
1495
+ } else {
1496
+ registerGlobalContext(Component, defaultMeta);
1497
+ }
1498
+ };
1499
+ _registerGlobalContext(CmsCredentialsProvider, cmsCredentialsProviderMeta);
1500
+ _registerComponent(CmsQueryRepeater, cmsQueryRepeaterMeta);
1501
+ _registerComponent(CmsRowField, cmsRowFieldMeta);
1502
+ _registerComponent(CmsRowLink, cmsRowLinkMeta);
1503
+ _registerComponent(CmsRowImage, cmsRowImageMeta);
1504
+ _registerComponent(CmsRowFieldValue, cmsRowFieldValueMeta);
1505
+ _registerComponent(CmsCount, cmsCountFieldMeta);
1506
+ }
1507
+ export {
1508
+ API,
1509
+ CmsCount,
1510
+ CmsCredentialsProvider,
1511
+ CmsQueryRepeater,
1512
+ CmsRowField,
1513
+ CmsRowFieldValue,
1514
+ CmsRowImage,
1515
+ CmsRowLink,
1516
+ HttpError,
1517
+ cmsCountFieldMeta,
1518
+ cmsCredentialsProviderMeta,
1519
+ cmsQueryRepeaterMeta,
1520
+ cmsRowFieldMeta,
1521
+ cmsRowFieldValueMeta,
1522
+ cmsRowImageMeta,
1523
+ cmsRowLinkMeta,
1524
+ fetchContent,
1525
+ fetchCount,
1526
+ fetchTables,
1527
+ mkApi,
1528
+ registerAll,
1529
+ registerAllCustomFunctions
1530
+ };
1531
+ //# sourceMappingURL=index.esm.js.map