@firecms/collection_editor 3.0.0-alpha.31 → 3.0.0-alpha.32

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@firecms/collection_editor",
3
- "version": "3.0.0-alpha.31",
3
+ "version": "3.0.0-alpha.32",
4
4
  "main": "./dist/index.umd.js",
5
5
  "module": "./dist/index.es.js",
6
6
  "types": "dist/index.d.ts",
@@ -14,8 +14,10 @@
14
14
  "./package.json": "./package.json"
15
15
  },
16
16
  "dependencies": {
17
- "@firecms/data_import": "^3.0.0-alpha.31",
18
- "@firecms/schema_inference": "^3.0.0-alpha.31"
17
+ "@firecms/data_import": "^3.0.0-alpha.32",
18
+ "@firecms/schema_inference": "^3.0.0-alpha.32",
19
+ "json5": "^2.2.3",
20
+ "prism-react-renderer": "^2.3.0"
19
21
  },
20
22
  "peerDependencies": {
21
23
  "react": "^18.2.0",
@@ -76,5 +78,5 @@
76
78
  "publishConfig": {
77
79
  "access": "public"
78
80
  },
79
- "gitHead": "50f21001373521c63e1eea47f551037eb9e4d0c9"
81
+ "gitHead": "f545e3572aa7f025222713984025288bf62b0d4c"
80
82
  }
@@ -1,4 +1,5 @@
1
1
  import React, { useEffect, useMemo, useState } from "react";
2
+ import JSON5 from "json5";
2
3
 
3
4
  import { Field, FormikErrors, getIn, useFormikContext } from "formik";
4
5
  import {
@@ -7,28 +8,36 @@ import {
7
8
  Button,
8
9
  CircularProgress,
9
10
  cn,
11
+ CodeIcon,
10
12
  DebouncedTextField,
11
13
  defaultBorderMixin,
14
+ Dialog,
15
+ DialogActions,
16
+ DialogContent,
12
17
  EntityCollection,
13
18
  ErrorBoundary,
14
- PropertyConfig,
19
+ IconButton,
15
20
  isPropertyBuilder,
16
21
  makePropertiesEditable,
17
22
  Paper,
18
23
  Properties,
19
24
  Property,
25
+ PropertyConfig,
20
26
  PropertyOrBuilder,
21
27
  Tooltip,
22
28
  Typography,
23
29
  useLargeLayout,
24
30
  User,
25
- useSnackbarController,
31
+ useSnackbarController
26
32
  } from "@firecms/core";
27
33
 
34
+ import { Highlight, themes } from "prism-react-renderer"
35
+
28
36
  import { getFullId, idToPropertiesPath, namespaceToPropertiesOrderPath } from "./util";
29
37
  import { OnPropertyChangedParams, PropertyForm, PropertyFormDialog } from "./PropertyEditView";
30
38
  import { PropertyTree } from "./PropertyTree";
31
39
  import { PersistedCollection } from "../../types/persisted_collection";
40
+ import { camelCase } from "./utils/strings";
32
41
 
33
42
  type CollectionEditorFormProps = {
34
43
  showErrors: boolean;
@@ -45,6 +54,47 @@ type CollectionEditorFormProps = {
45
54
  collectionEditable: boolean;
46
55
  };
47
56
 
57
+ function GetCodeDialog({ collection, onOpenChange, open }: { onOpenChange: (open: boolean) => void, collection: any, open: any }) {
58
+ const code = "const " + camelCase(collection.name) + "Collection = " + JSON5.stringify(collection, null, "\t");
59
+ return <Dialog open={open}
60
+ onOpenChange={onOpenChange}
61
+ maxWidth={"4xl"}>
62
+ <DialogContent>
63
+ <Typography variant={"h6"} className={"my-4"}>
64
+ Code for {collection.name}
65
+ </Typography>
66
+ <Typography variant={"body2"} className={"my-4 mb-8"}>
67
+ If you want to customise the collection in code, you can add this collection code to your CMS
68
+ app configuration.
69
+ More info in the <a
70
+ rel="noopener noreferrer"
71
+ href={"https://firecms.co/docs/customization_quickstart"}>docs</a>.
72
+ </Typography>
73
+ <Highlight
74
+ theme={themes.vsDark}
75
+ code={code}
76
+ language="typescript"
77
+ >
78
+ {({ className, style, tokens, getLineProps, getTokenProps }) => (
79
+ <pre style={style} className={"p-4 rounded"}>
80
+ {tokens.map((line, i) => (
81
+ <div key={i} {...getLineProps({ line })}>
82
+ {line.map((token, key) => (
83
+ <span key={key} {...getTokenProps({ token })} />
84
+ ))}
85
+ </div>
86
+ ))}
87
+ </pre>
88
+ )}
89
+ </Highlight>
90
+
91
+ </DialogContent>
92
+ <DialogActions>
93
+ <Button onClick={() => onOpenChange(false)}>Close</Button>
94
+ </DialogActions>
95
+ </Dialog>;
96
+ }
97
+
48
98
  export function CollectionPropertiesEditorForm({
49
99
  showErrors,
50
100
  isNewCollection,
@@ -81,6 +131,7 @@ export function CollectionPropertiesEditorForm({
81
131
 
82
132
  const selectedPropertyFullId = selectedPropertyKey ? getFullId(selectedPropertyKey, selectedPropertyNamespace) : undefined;
83
133
  const selectedProperty = selectedPropertyFullId ? getIn(values.properties, selectedPropertyFullId.replaceAll(".", ".properties.")) : undefined;
134
+ const [codeDialogOpen, setCodeDialogOpen] = useState<boolean>(false);
84
135
 
85
136
  const [inferringProperties, setInferringProperties] = useState<boolean>(false);
86
137
 
@@ -112,7 +163,7 @@ export function CollectionPropertiesEditorForm({
112
163
  if (!newCollection) {
113
164
  snackbarController.open({
114
165
  type: "error",
115
- message: "Could not infer properties from data",
166
+ message: "Could not infer properties from data"
116
167
  });
117
168
  return;
118
169
  }
@@ -122,7 +173,7 @@ export function CollectionPropertiesEditorForm({
122
173
  if (newPropertyKeys.length === 0) {
123
174
  snackbarController.open({
124
175
  type: "info",
125
- message: "No new properties found",
176
+ message: "No new properties found"
126
177
  });
127
178
  return;
128
179
  }
@@ -132,7 +183,7 @@ export function CollectionPropertiesEditorForm({
132
183
  acc[propertyKey] = newCollection.properties[propertyKey];
133
184
  return acc;
134
185
  }, {} as { [key: string]: PropertyOrBuilder }),
135
- ...values.properties,
186
+ ...values.properties
136
187
  };
137
188
  const updatedPropertiesOrder = [
138
189
  ...newPropertyKeys,
@@ -323,13 +374,21 @@ export function CollectionPropertiesEditorForm({
323
374
  </div>}
324
375
 
325
376
  <div className="ml-1 mt-2 flex flex-row gap-2">
377
+ <Tooltip title={"Get the code of this collection"}>
378
+ <IconButton
379
+ variant={"filled"}
380
+ disabled={inferringProperties}
381
+ onClick={() => setCodeDialogOpen(true)}>
382
+ <CodeIcon/>
383
+ </IconButton>
384
+ </Tooltip>
326
385
  {inferPropertiesFromData && <Tooltip title={"Add new properties based on data"}>
327
- <Button
328
- variant={"text"}
386
+ <IconButton
387
+ variant={"filled"}
329
388
  disabled={inferringProperties}
330
389
  onClick={inferPropertiesFromData}>
331
390
  {inferringProperties ? <CircularProgress size={"small"}/> : <AutoAwesomeIcon/>}
332
- </Button>
391
+ </IconButton>
333
392
  </Tooltip>}
334
393
  <Tooltip title={"Add new property"}>
335
394
  <Button
@@ -458,6 +517,11 @@ export function CollectionPropertiesEditorForm({
458
517
  collectionEditable={collectionEditable}
459
518
  existingPropertyKeys={values.propertiesOrder as string[]}/>
460
519
 
520
+ <GetCodeDialog
521
+ collection={values}
522
+ open={codeDialogOpen}
523
+ onOpenChange={setCodeDialogOpen}/>
524
+
461
525
  </>
462
526
  );
463
527
  }
@@ -0,0 +1,8 @@
1
+ export function camelCase(str: string): string {
2
+ return (str.slice(0, 1).toLowerCase() + str.slice(1))
3
+ .replace(/([-_ ]){1,}/g, ' ')
4
+ .split(/[-_ ]/)
5
+ .reduce((cur, acc) => {
6
+ return cur + acc[0].toUpperCase() + acc.substring(1);
7
+ });
8
+ }