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