@kids-reporter/cms-core 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,35 +4,28 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.richTextEditor = void 0;
7
-
8
7
  var _types = require("@keystone-6/core/types");
9
-
10
8
  var _core = require("@keystone-6/core");
11
-
12
9
  const richTextEditor = ({
13
10
  defaultValue = null,
14
11
  disabledButtons = [],
15
12
  ...config
16
13
  } = {}) => meta => {
17
14
  var _config$db;
18
-
19
15
  if (config.isIndexed === 'unique') {
20
16
  throw Error("isIndexed: 'unique' is not a supported option for field type textEditor");
21
17
  }
22
-
23
18
  const resolve = val => val === null && meta.provider === 'postgresql' ? 'DbNull' : val;
24
-
25
- return (0, _types.jsonFieldTypePolyfilledForSQLite)(meta.provider, { ...config,
19
+ return (0, _types.jsonFieldTypePolyfilledForSQLite)(meta.provider, {
20
+ ...config,
26
21
  input: {
27
22
  create: {
28
23
  arg: _core.graphql.arg({
29
24
  type: _core.graphql.JSON
30
25
  }),
31
-
32
26
  resolve(val) {
33
27
  return resolve(val === undefined ? defaultValue : val);
34
28
  }
35
-
36
29
  },
37
30
  update: {
38
31
  arg: _core.graphql.arg({
@@ -57,5 +50,4 @@ const richTextEditor = ({
57
50
  map: (_config$db = config.db) === null || _config$db === void 0 ? void 0 : _config$db.map
58
51
  });
59
52
  };
60
-
61
53
  exports.richTextEditor = richTextEditor;
@@ -4,44 +4,30 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.controller = exports.Field = exports.Cell = exports.CardValue = void 0;
7
-
8
7
  var _react = _interopRequireDefault(require("react"));
9
-
10
8
  var _core = require("@keystone-ui/core");
11
-
12
9
  var _fields = require("@keystone-ui/fields");
13
-
14
10
  var _components = require("@keystone-6/core/admin-ui/components");
15
-
16
11
  var _draftJs = require("draft-js");
17
-
18
- var _draftEditor = _interopRequireDefault(require("@kids-reporter/draft-editor"));
19
-
12
+ var _draftEditor = require("@kids-reporter/draft-editor");
20
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
-
22
14
  // eslint-disable-line
15
+
23
16
  // import { RichTextEditor, decorators } from '@mirrormedia/lilith-draft-editor'
24
- const {
25
- RichTextEditor,
26
- decorators
27
- } = _draftEditor.default.DraftEditor;
28
17
 
29
18
  const Field = ({
30
19
  field,
31
20
  value,
32
21
  onChange,
33
22
  autoFocus // eslint-disable-line
34
-
35
23
  }) => {
36
- return /*#__PURE__*/_react.default.createElement(_fields.FieldContainer, null, /*#__PURE__*/_react.default.createElement(_fields.FieldLabel, null, field.label, /*#__PURE__*/_react.default.createElement(_core.Stack, null, /*#__PURE__*/_react.default.createElement(RichTextEditor, {
24
+ return /*#__PURE__*/_react.default.createElement(_fields.FieldContainer, null, /*#__PURE__*/_react.default.createElement(_fields.FieldLabel, null, field.label, /*#__PURE__*/_react.default.createElement(_core.Stack, null, /*#__PURE__*/_react.default.createElement(_draftEditor.RichTextEditor, {
37
25
  disabledButtons: field.disabledButtons,
38
26
  editorState: value,
39
27
  onChange: editorState => onChange === null || onChange === void 0 ? void 0 : onChange(editorState)
40
28
  }))));
41
29
  };
42
-
43
30
  exports.Field = Field;
44
-
45
31
  const Cell = ({
46
32
  item,
47
33
  field,
@@ -50,44 +36,36 @@ const Cell = ({
50
36
  const value = item[field.path] + '';
51
37
  return linkTo ? /*#__PURE__*/_react.default.createElement(_components.CellLink, linkTo, value) : /*#__PURE__*/_react.default.createElement(_components.CellContainer, null, value);
52
38
  };
53
-
54
39
  exports.Cell = Cell;
55
40
  Cell.supportsLinkTo = true;
56
-
57
41
  const CardValue = ({
58
42
  item,
59
43
  field
60
44
  }) => {
61
45
  return /*#__PURE__*/_react.default.createElement(_fields.FieldContainer, null, /*#__PURE__*/_react.default.createElement(_fields.FieldLabel, null, field.label), item[field.path]);
62
46
  };
63
-
64
47
  exports.CardValue = CardValue;
65
-
66
48
  const controller = config => {
67
49
  var _config$fieldMeta;
68
-
69
50
  return {
51
+ description: '',
70
52
  disabledButtons: ((_config$fieldMeta = config.fieldMeta) === null || _config$fieldMeta === void 0 ? void 0 : _config$fieldMeta.disabledButtons) ?? [],
71
53
  path: config.path,
72
54
  label: config.label,
73
55
  graphqlSelection: config.path,
74
- defaultValue: _draftJs.EditorState.createEmpty(decorators),
56
+ defaultValue: _draftJs.EditorState.createEmpty(_draftEditor.decorator),
75
57
  deserialize: data => {
76
58
  const rawContentState = data[config.path];
77
-
78
59
  if (rawContentState === null) {
79
- return _draftJs.EditorState.createEmpty(decorators);
60
+ return _draftJs.EditorState.createEmpty(_draftEditor.decorator);
80
61
  }
81
-
82
62
  try {
83
63
  const contentState = (0, _draftJs.convertFromRaw)(rawContentState);
84
-
85
- const editorState = _draftJs.EditorState.createWithContent(contentState, decorators);
86
-
64
+ const editorState = _draftJs.EditorState.createWithContent(contentState, _draftEditor.decorator);
87
65
  return editorState;
88
66
  } catch (err) {
89
67
  console.error(err);
90
- return _draftJs.EditorState.createEmpty(decorators);
68
+ return _draftJs.EditorState.createEmpty(_draftEditor.decorator);
91
69
  }
92
70
  },
93
71
  serialize: editorState => {
@@ -96,7 +74,6 @@ const controller = config => {
96
74
  [config.path]: null
97
75
  };
98
76
  }
99
-
100
77
  try {
101
78
  const rawContentState = (0, _draftJs.convertToRaw)(editorState.getCurrentContent());
102
79
  return {
@@ -111,5 +88,4 @@ const controller = config => {
111
88
  }
112
89
  };
113
90
  };
114
-
115
91
  exports.controller = controller;
package/lib/index.js CHANGED
@@ -11,15 +11,10 @@ Object.defineProperty(exports, "richTextEditorButtonNames", {
11
11
  }
12
12
  });
13
13
  exports.utils = void 0;
14
-
15
14
  var _manualOrderRelationship = _interopRequireDefault(require("./utils/manual-order-relationship"));
16
-
17
15
  var _richTextEditor = require("./custom-fields/rich-text-editor");
18
-
19
- var _draftEditor = require("@kids-reporter/draft-editor/lib/draft-editor");
20
-
16
+ var _draftEditor = require("@kids-reporter/draft-editor");
21
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
-
23
18
  const customFields = {
24
19
  richTextEditor: _richTextEditor.richTextEditor
25
20
  };
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _core = require("@keystone-6/core");
9
-
10
8
  var _fields = require("@keystone-6/core/fields");
11
-
12
9
  /**
13
10
  * For `relationship` field, KeystoneJS won't take user input order into account.
14
11
  * Therefore, after the create/update operation is done,
@@ -57,10 +54,8 @@ var _fields = require("@keystone-6/core/fields");
57
54
  */
58
55
  function addManualOrderRelationshipFields(manualOrderFields = [], list) {
59
56
  var _list$hooks;
60
-
61
57
  manualOrderFields.forEach(mo => {
62
58
  var _list$fields;
63
-
64
59
  if (!((_list$fields = list.fields) !== null && _list$fields !== void 0 && _list$fields[mo.fieldName])) {
65
60
  list.fields[mo.fieldName] = (0, _fields.json)({
66
61
  label: mo.fieldLabel,
@@ -70,60 +65,59 @@ function addManualOrderRelationshipFields(manualOrderFields = [], list) {
70
65
  }
71
66
  }
72
67
  });
73
- } // add virtual field definition
74
-
68
+ }
75
69
 
70
+ // add virtual field definition
76
71
  addVirtualFieldToReturnItemsInInputOrder(list, mo);
77
- }); // decorate `resolveInput` hook
72
+ });
78
73
 
74
+ // decorate `resolveInput` hook
79
75
  list.hooks = list.hooks || {};
80
76
  const originResolveInput = (_list$hooks = list.hooks) === null || _list$hooks === void 0 ? void 0 : _list$hooks.resolveInput;
81
-
82
77
  list.hooks.resolveInput = async props => {
83
78
  let resolvedData = props.resolvedData;
84
-
85
79
  if (typeof originResolveInput === 'function') {
86
80
  resolvedData = await originResolveInput(props);
87
81
  }
88
-
89
82
  const {
90
83
  item,
91
84
  context
92
- } = props; // check if create/update item has the fields
93
- // we want to monitor
85
+ } = props;
94
86
 
87
+ // check if create/update item has the fields
88
+ // we want to monitor
95
89
  for (let i = 0; i < manualOrderFields.length; i++) {
96
90
  var _resolvedData;
97
-
98
91
  const {
99
92
  targetFieldName,
100
93
  fieldName,
101
94
  targetListName,
102
95
  targetListLabelField
103
- } = manualOrderFields[i]; // if create/update operation creates/modifies the `${targetFieldName}` field
96
+ } = manualOrderFields[i];
104
97
 
98
+ // if create/update operation creates/modifies the `${targetFieldName}` field
105
99
  if ((_resolvedData = resolvedData) !== null && _resolvedData !== void 0 && _resolvedData[targetFieldName]) {
106
- var _resolvedData$targetF3, _resolvedData$targetF4;
107
-
108
- let currentOrder = []; // update operation due to `item` not being `undefiend`
100
+ var _resolvedData$targetF2;
101
+ let currentOrder = [];
109
102
 
103
+ // update operation due to `item` not being `undefiend`
110
104
  if (item) {
111
- var _resolvedData$targetF, _resolvedData$targetF2;
105
+ var _resolvedData$targetF;
106
+ const previousOrder = Array.isArray(item[fieldName]) ? item[fieldName] : [];
112
107
 
113
- const previousOrder = Array.isArray(item[fieldName]) ? item[fieldName] : []; // user disconnects/removes some relationship items.
114
-
115
- const disconnectIds = ((_resolvedData$targetF = resolvedData[targetFieldName]) === null || _resolvedData$targetF === void 0 ? void 0 : (_resolvedData$targetF2 = _resolvedData$targetF.disconnect) === null || _resolvedData$targetF2 === void 0 ? void 0 : _resolvedData$targetF2.map(obj => obj.id.toString())) || []; // filtered out to-be-disconnected relationship items
108
+ // user disconnects/removes some relationship items.
109
+ const disconnectIds = ((_resolvedData$targetF = resolvedData[targetFieldName]) === null || _resolvedData$targetF === void 0 || (_resolvedData$targetF = _resolvedData$targetF.disconnect) === null || _resolvedData$targetF === void 0 ? void 0 : _resolvedData$targetF.map(obj => obj.id.toString())) || [];
116
110
 
111
+ // filtered out to-be-disconnected relationship items
117
112
  currentOrder = previousOrder.filter(({
118
113
  id
119
114
  }) => {
120
115
  return disconnectIds.indexOf(id) === -1;
121
116
  });
122
- } // user connects/adds some relationship item.
123
-
124
-
125
- const connectedIds = ((_resolvedData$targetF3 = resolvedData[targetFieldName]) === null || _resolvedData$targetF3 === void 0 ? void 0 : (_resolvedData$targetF4 = _resolvedData$targetF3.connect) === null || _resolvedData$targetF4 === void 0 ? void 0 : _resolvedData$targetF4.map(obj => obj.id.toString())) || [];
117
+ }
126
118
 
119
+ // user connects/adds some relationship item.
120
+ const connectedIds = ((_resolvedData$targetF2 = resolvedData[targetFieldName]) === null || _resolvedData$targetF2 === void 0 || (_resolvedData$targetF2 = _resolvedData$targetF2.connect) === null || _resolvedData$targetF2 === void 0 ? void 0 : _resolvedData$targetF2.map(obj => obj.id.toString())) || [];
127
121
  if (connectedIds.length > 0) {
128
122
  // Query relationship items from the database.
129
123
  // Therefore, we can have other fields to record in the monitoring field
@@ -133,14 +127,14 @@ function addManualOrderRelationshipFields(manualOrderFields = [], list) {
133
127
  in: connectedIds
134
128
  }
135
129
  }
136
- }); // Database query results are not sorted.
137
- // We need to sort them by ourselves.
130
+ });
138
131
 
132
+ // Database query results are not sorted.
133
+ // We need to sort them by ourselves.
139
134
  for (let i = 0; i < connectedIds.length; i++) {
140
135
  const sf = sfToConnect.find(obj => {
141
136
  return `${obj.id}` === connectedIds[i];
142
137
  });
143
-
144
138
  if (sf) {
145
139
  currentOrder.push({
146
140
  id: sf.id.toString(),
@@ -148,18 +142,17 @@ function addManualOrderRelationshipFields(manualOrderFields = [], list) {
148
142
  });
149
143
  }
150
144
  }
151
- } // records the order in the monitoring field
152
-
145
+ }
153
146
 
147
+ // records the order in the monitoring field
154
148
  resolvedData[fieldName] = currentOrder;
155
149
  }
156
150
  }
157
-
158
151
  return resolvedData;
159
152
  };
160
-
161
153
  return list;
162
154
  }
155
+
163
156
  /**
164
157
  * This functiona adds the virtual field onto Keystone6 `list` object.
165
158
  * For instance, if we want to use monitoring field `manualOrderOfAuthors`
@@ -181,29 +174,24 @@ function addManualOrderRelationshipFields(manualOrderFields = [], list) {
181
174
  *
182
175
  * And the GQL resolver will be defined in `resolve` function.
183
176
  */
184
-
185
-
186
177
  function addVirtualFieldToReturnItemsInInputOrder(list, manualOrderField) {
187
178
  const virtualFieldName = `${manualOrderField.targetFieldName}InInputOrder`;
188
179
  list.fields[virtualFieldName] = (0, _fields.virtual)({
189
180
  field: lists => {
190
181
  var _lists$manualOrderFie;
191
-
192
182
  return _core.graphql.field({
193
- type: _core.graphql.list(lists === null || lists === void 0 ? void 0 : (_lists$manualOrderFie = lists[manualOrderField.targetListName]) === null || _lists$manualOrderFie === void 0 ? void 0 : _lists$manualOrderFie.types.output),
194
-
183
+ type: _core.graphql.list(lists === null || lists === void 0 || (_lists$manualOrderFie = lists[manualOrderField.targetListName]) === null || _lists$manualOrderFie === void 0 ? void 0 : _lists$manualOrderFie.types.output),
195
184
  async resolve(item, args, context) {
196
185
  var _context$db;
197
-
198
186
  const manualOrderFieldValue = (item === null || item === void 0 ? void 0 : item[manualOrderField.fieldName]) || [];
199
-
200
187
  if (!Array.isArray(manualOrderFieldValue)) {
201
188
  return [];
202
- } // collect ids from relationship items
203
-
189
+ }
204
190
 
205
- const ids = manualOrderFieldValue.map(value => value.id); // query items from database
191
+ // collect ids from relationship items
192
+ const ids = manualOrderFieldValue.map(value => value.id);
206
193
 
194
+ // query items from database
207
195
  const unorderedItems = await ((_context$db = context.db) === null || _context$db === void 0 ? void 0 : _context$db[manualOrderField.targetListName].findMany({
208
196
  where: {
209
197
  id: {
@@ -211,18 +199,17 @@ function addVirtualFieldToReturnItemsInInputOrder(list, manualOrderField) {
211
199
  }
212
200
  }
213
201
  }));
214
- const orderedItems = []; // sort items according to input order
202
+ const orderedItems = [];
215
203
 
204
+ // sort items according to input order
216
205
  manualOrderFieldValue.forEach(value => {
217
206
  const writer = unorderedItems.find(ui => `${ui === null || ui === void 0 ? void 0 : ui.id}` === `${value === null || value === void 0 ? void 0 : value.id}`);
218
-
219
207
  if (writer) {
220
208
  orderedItems.push(writer);
221
209
  }
222
210
  });
223
211
  return orderedItems;
224
212
  }
225
-
226
213
  });
227
214
  },
228
215
  ui: {
@@ -237,6 +224,5 @@ function addVirtualFieldToReturnItemsInInputOrder(list, manualOrderField) {
237
224
  }
238
225
  });
239
226
  }
240
-
241
227
  var _default = addManualOrderRelationshipFields;
242
228
  exports.default = _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kids-reporter/cms-core",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/index.ts",
@@ -33,7 +33,7 @@
33
33
  "@keystone-ui/fields": "^7.2.0",
34
34
  "@keystone-ui/modals": "^6.0.3",
35
35
  "@twreporter/errors": "^1.1.1",
36
- "@kids-reporter/draft-editor": "0.2.1",
36
+ "@kids-reporter/draft-editor": "0.4.2",
37
37
  "axios": "^0.26.0",
38
38
  "draft-convert": "^2.1.12",
39
39
  "draft-js": "^0.11.7",
@@ -12,9 +12,8 @@ import {
12
12
  import { CellContainer, CellLink } from '@keystone-6/core/admin-ui/components'
13
13
  import { EditorState, convertFromRaw, convertToRaw } from 'draft-js'
14
14
  // import { RichTextEditor, decorators } from '@mirrormedia/lilith-draft-editor'
15
- import de from '@kids-reporter/draft-editor'
15
+ import { RichTextEditor, decorator } from '@kids-reporter/draft-editor'
16
16
 
17
- const { RichTextEditor, decorators } = de.DraftEditor
18
17
  export const Field = ({
19
18
  field,
20
19
  value,
@@ -29,7 +28,7 @@ export const Field = ({
29
28
  <RichTextEditor
30
29
  disabledButtons={field.disabledButtons}
31
30
  editorState={value}
32
- onChange={(editorState) => onChange?.(editorState)}
31
+ onChange={(editorState: EditorState) => onChange?.(editorState)}
33
32
  />
34
33
  </Stack>
35
34
  </FieldLabel>
@@ -60,26 +59,27 @@ export const controller = (
60
59
  config: FieldControllerConfig<{ disabledButtons: string[] }>
61
60
  ): FieldController<EditorState, JSONValue> & { disabledButtons: string[] } => {
62
61
  return {
62
+ description: '',
63
63
  disabledButtons: config.fieldMeta?.disabledButtons ?? [],
64
64
  path: config.path,
65
65
  label: config.label,
66
66
  graphqlSelection: config.path,
67
- defaultValue: EditorState.createEmpty(decorators),
67
+ defaultValue: EditorState.createEmpty(decorator),
68
68
  deserialize: (data) => {
69
69
  const rawContentState = data[config.path]
70
70
  if (rawContentState === null) {
71
- return EditorState.createEmpty(decorators)
71
+ return EditorState.createEmpty(decorator)
72
72
  }
73
73
  try {
74
74
  const contentState = convertFromRaw(rawContentState)
75
75
  const editorState = EditorState.createWithContent(
76
76
  contentState,
77
- decorators
77
+ decorator
78
78
  )
79
79
  return editorState
80
80
  } catch (err) {
81
81
  console.error(err)
82
- return EditorState.createEmpty(decorators)
82
+ return EditorState.createEmpty(decorator)
83
83
  }
84
84
  },
85
85
  serialize: (editorState: EditorState) => {
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import addManualOrderRelationshipFields from './utils/manual-order-relationship'
2
2
  import { richTextEditor } from './custom-fields/rich-text-editor'
3
3
 
4
- export { buttonNames as richTextEditorButtonNames } from '@kids-reporter/draft-editor/lib/draft-editor'
4
+ export { buttonNames as richTextEditorButtonNames } from '@kids-reporter/draft-editor'
5
5
 
6
6
  export const customFields = {
7
7
  richTextEditor,