Skip to content

Commit 83bb01b

Browse files
authored
Data source schema & providers (#6633)
* Setup initial schema types * Setup initial schema methods * Add DataSource tests * Up test * Add test getValue * Cleanup * Up test * Add setValue method * Up docs * Check nested arrays * Add a test for nested setValue * Improve setValue for nested values * Improve setValue for nested values * Setup relation tests * Add getResolvedRecords * Resolve one to many relations in DataSource records * Add DataSourceSchema * Up type * Start data source providers * Start test provider * Add tests for loadProvider * Cleanup * Skip records if DataSource provider is set * Load providers on project load * Type keymap events * Move modal events * Move layer events * Move RTE events * Move selector events * Move StyleManager events * Move editor events * Start DataSource callbacks * Add data source callbacks * Update docs * Up device_manager jsdoc * Format
1 parent fe88fc8 commit 83bb01b

File tree

41 files changed

+1586
-403
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1586
-403
lines changed

docs/api.mjs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,33 +97,43 @@ async function generateDocs() {
9797
throw `File not found '${filePath}'`;
9898
}
9999

100-
return build([filePath], { shallow: true })
101-
.then((cm) => formats.md(cm /*{ markdownToc: true }*/))
102-
.then(async (output) => {
103-
let addLogs = [];
104-
let result = output
105-
.replace(/\*\*\\\[/g, '**[')
106-
.replace(/\*\*\(\\\[/g, '**([')
107-
.replace(/<\\\[/g, '<[')
108-
.replace(/<\(\\\[/g, '<([')
109-
.replace(/\| \\\[/g, '| [')
110-
.replace(/\\n```js/g, '```js')
111-
.replace(/docsjs\./g, '')
112-
.replace('**Extends ModuleModel**', '')
113-
.replace('**Extends Model**', '');
100+
try {
101+
return build([filePath], { shallow: true })
102+
.then((cm) => formats.md(cm /*{ markdownToc: true }*/))
103+
.then(async (output) => {
104+
let addLogs = [];
105+
let result = output
106+
.replace(/\*\*\\\[/g, '**[')
107+
.replace(/\*\*\(\\\[/g, '**([')
108+
.replace(/<\\\[/g, '<[')
109+
.replace(/<\(\\\[/g, '<([')
110+
.replace(/\| \\\[/g, '| [')
111+
.replace(/\\n```js/g, '```js')
112+
.replace(/docsjs\./g, '')
113+
.replace('**Extends ModuleModel**', '')
114+
.replace('**Extends Model**', '');
114115

115-
// Search for module event documentation
116-
if (result.indexOf(REPLACE_EVENTS) >= 0) {
117-
const eventsMd = await getEventsMdFromTypes(filePath);
118-
if (eventsMd && result.indexOf(REPLACE_EVENTS) >= 0) {
119-
addLogs.push('replaced events');
116+
// Search for module event documentation
117+
if (result.indexOf(REPLACE_EVENTS) >= 0) {
118+
try {
119+
const eventsMd = await getEventsMdFromTypes(filePath);
120+
if (eventsMd && result.indexOf(REPLACE_EVENTS) >= 0) {
121+
addLogs.push('replaced events');
122+
}
123+
result = eventsMd ? result.replace(REPLACE_EVENTS, `## Available Events\n${eventsMd}`) : result;
124+
} catch (err) {
125+
console.error(`Failed getting events: ${file[0]}`);
126+
throw err;
127+
}
120128
}
121-
result = eventsMd ? result.replace(REPLACE_EVENTS, `## Available Events\n${eventsMd}`) : result;
122-
}
123129

124-
writeFileSync(`${docRoot}/api/${file[1]}`, result);
125-
log('Created', file[1], addLogs.length ? `(${addLogs.join(', ')})` : '');
126-
});
130+
writeFileSync(`${docRoot}/api/${file[1]}`, result);
131+
log('Created', file[1], addLogs.length ? `(${addLogs.join(', ')})` : '');
132+
});
133+
} catch (err) {
134+
console.error(`Build failed: ${file[0]}`);
135+
throw err;
136+
}
127137
}),
128138
);
129139

docs/api/block_manager.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ editor.on('block:custom', ({ container, blocks, ... }) => { ... });
8484
editor.on('block', ({ event, model, ... }) => { ... });
8585
```
8686

87+
* BlocksEventCallback
88+
8789
[Block]: block.html
8890

8991
[Component]: component.html

docs/api/canvas.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ editor.on('canvas:frame:load:body', ({ window }) => {
122122
});
123123
```
124124

125+
* `canvas:frame:unload` Frame is unloading from the canvas.
126+
127+
```javascript
128+
editor.on('canvas:frame:unload', ({ frame }) => {
129+
console.log('Unloading frame', frame);
130+
});
131+
```
132+
125133
[Component]: component.html
126134

127135
[Frame]: frame.html

docs/api/component.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ By setting override to specific properties, changes of those properties will be
137137
### Parameters
138138

139139
* `value` **([Boolean][3] | [String][1] | [Array][5]<[String][1]>)**&#x20;
140-
* `options` **DynamicWatchersOptions** (optional, default `{}`)
140+
* `options` **DataWatchersOptions** (optional, default `{}`)
141141

142142
### Examples
143143

@@ -335,8 +335,7 @@ Get the style of the component
335335

336336
### Parameters
337337

338-
* `options` **any** (optional, default `{}`)
339-
* `optsAdd` **any** (optional, default `{}`)
338+
* `opts` **GetComponentStyleOpts?**&#x20;
340339

341340
Returns **[Object][2]**&#x20;
342341

@@ -363,7 +362,7 @@ Return all component's attributes
363362

364363
### Parameters
365364

366-
* `opts` **{noClass: [boolean][3]?, noStyle: [boolean][3]?}** (optional, default `{}`)
365+
* `opts` **{noClass: [boolean][3]?, noStyle: [boolean][3]?, skipResolve: [boolean][3]?}** (optional, default `{}`)
367366

368367
Returns **[Object][2]**&#x20;
369368

docs/api/datasource.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,44 @@ dataSource.addRecord({ id: 'id3', name: 'value3' });
3131
* `props` **DataSourceProps** Properties to initialize the data source.
3232
* `opts` **DataSourceOptions** Options to initialize the data source.
3333

34+
### hasProvider
35+
36+
Indicates if the data source has a provider for records.
37+
38+
### getResolvedRecords
39+
40+
Retrieves all records from the data source with resolved relations based on the schema.
41+
42+
### upSchema
43+
44+
Update the schema.
45+
46+
#### Parameters
47+
48+
* `schema` **Partial\<any>**&#x20;
49+
* `opts` **SetOptions?**&#x20;
50+
51+
#### Examples
52+
53+
```javascript
54+
dataSource.upSchema({ name: { type: 'string' } });
55+
```
56+
57+
### getSchemaField
58+
59+
Get schema field definition.
60+
61+
#### Parameters
62+
63+
* `fieldKey` **any**&#x20;
64+
65+
#### Examples
66+
67+
```javascript
68+
const fieldSchema = dataSource.getSchemaField('name');
69+
fieldSchema.type; // 'string'
70+
```
71+
3472
## defaults
3573

3674
Returns the default properties for the data source.
@@ -55,6 +93,12 @@ Retrieves the collection of records associated with this data source.
5593

5694
Returns **DataRecords\<DRProps>** The collection of data records.
5795

96+
## records
97+
98+
Retrieves the collection of records associated with this data source.
99+
100+
Returns **DataRecords\<DRProps>** The collection of data records.
101+
58102
## em
59103

60104
Retrieves the editor model associated with this data source.

docs/api/datasources.md

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,26 @@ editor.on('data:path', ({ dataSource, dataRecord, path }) => {
4444
editor.on('data:pathSource:SOURCE_ID', ({ dataSource, dataRecord, path }) => { ... });
4545
```
4646

47+
* `data:provider:load` Data source provider load.
48+
49+
```javascript
50+
editor.on('data:provider:load', ({ dataSource, result }) => { ... });
51+
```
52+
53+
* `data:provider:loadAll` Load of all data source providers (eg. on project load).
54+
55+
```javascript
56+
editor.on('data:provider:loadAll', () => { ... });
57+
```
58+
4759
* `data` Catch-all event for all the events mentioned above.
4860

4961
```javascript
5062
editor.on('data', ({ event, model, ... }) => { ... });
5163
```
5264

65+
* DataSourcesEventCallback
66+
5367
## Methods
5468

5569
* [add][1] - Add a new data source.
@@ -101,15 +115,32 @@ Returns **[DataSource]** Data source.
101115

102116
## getValue
103117

104-
Get value from data sources by key
118+
Get value from data sources by path.
105119

106120
### Parameters
107121

108-
* `key` **[String][7]** Path to value.
109-
* `defValue` **any**&#x20;
122+
* `path` **[String][7]** Path to value.
123+
* `defValue` **any** Default value if the path is not found.
110124

111125
Returns **any** const value = dsm.getValue('ds\_id.record\_id.propName', 'defaultValue');
112126

127+
## setValue
128+
129+
Set value in data sources by path.
130+
131+
### Parameters
132+
133+
* `path` **[String][7]** Path to value in format 'dataSourceId.recordId.propName'
134+
* `value` **any** Value to set
135+
136+
### Examples
137+
138+
```javascript
139+
dsm.setValue('ds_id.record_id.propName', 'new value');
140+
```
141+
142+
Returns **[Boolean][8]** Returns true if the value was set successfully
143+
113144
## remove
114145

115146
Remove data source.
@@ -152,7 +183,7 @@ data record, and optional property path.
152183

153184
Store data sources to a JSON object.
154185

155-
Returns **[Array][8]** Stored data sources.
186+
Returns **[Array][9]** Stored data sources.
156187

157188
## load
158189

@@ -178,4 +209,6 @@ Returns **[Object][6]** Loaded data sources.
178209

179210
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
180211

181-
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
212+
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
213+
214+
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

docs/api/device_manager.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,35 @@ const deviceManager = editor.Devices;
1919
```
2020

2121
## Available Events
22+
* `device:add` New device added to the collection. The `Device` is passed as an argument.
2223

23-
* `device:add` - Added new device. The [Device] is passed as an argument to the callback
24-
* `device:remove` - Device removed. The [Device] is passed as an argument to the callback
25-
* `device:select` - New device selected. The newly selected [Device] and the previous one, are passed as arguments to the callback
26-
* `device:update` - Device updated. The updated [Device] and the object containing changes are passed as arguments to the callback
27-
* `device` - Catch-all event for all the events mentioned above. An object containing all the available data about the triggered event is passed as an argument to the callback
24+
```javascript
25+
editor.on('device:add', (device) => { ... });
26+
```
27+
28+
* `device:remove` Device removed from the collection. The `Device` is passed as an argument.
29+
30+
```javascript
31+
editor.on('device:remove', (device) => { ... });
32+
```
33+
34+
* `device:select` A new device is selected. The `Device` is passed as an argument.
35+
36+
```javascript
37+
editor.on('device:select', (device) => { ... });
38+
```
39+
40+
* `device:update` Device updated. The `Device` and the object containing changes are passed as arguments.
41+
42+
```javascript
43+
editor.on('device:update', (device) => { ... });
44+
```
45+
46+
* `device` Catch-all event for all the events mentioned above.
47+
48+
```javascript
49+
editor.on('device', ({ event, model, ... }) => { ... });
50+
```
2851

2952
## Methods
3053

docs/api/editor.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ editor.on('load', () => { ... });
4242
editor.on('project:load', ({ project, initial }) => { ... });
4343
```
4444

45+
* `project:loaded` Similar to `project:load`, but triggers only if the project is loaded successfully.
46+
47+
```javascript
48+
editor.on('project:loaded', ({ project, initial }) => { ... });
49+
50+
// Loading an empty project, won't trigger this event.
51+
editor.loadProjectData({});
52+
```
53+
4554
* `project:get` Event triggered on request of the project data. This can be used to extend the project with custom data.
4655

4756
```javascript
@@ -516,6 +525,7 @@ Load data from the JSON project
516525
### Parameters
517526

518527
* `data` **[Object][16]** Project to load
528+
* `options` **[Object][16]?** Custom options that could be passed to the project load events. (optional, default `{}`)
519529

520530
### Examples
521531

@@ -722,7 +732,7 @@ Trigger event
722732
### Parameters
723733

724734
* `event` **[string][18]** Event to trigger
725-
* `args` **...[Array][19]\<any>**&#x20;
735+
* `args` **...any**&#x20;
726736

727737
Returns **this**&#x20;
728738

docs/api/keymaps.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,30 @@ const editor = grapesjs.init({
1919
})
2020
```
2121

22-
Once the editor is instantiated you can use its API and listen to its events. Before using these methods, you should get the module from the instance.
22+
Once the editor is instantiated you can use its API. Before using these methods you should get the module from the instance.
2323

2424
```js
25-
// Listen to events
26-
editor.on('keymap:add', () => { ... });
27-
28-
// Use the API
2925
const keymaps = editor.Keymaps;
30-
keymaps.add(...);
3126
```
3227

3328
## Available Events
29+
* `keymap:add` New keymap added. The new keymap object is passed as an argument to the callback.
30+
31+
```javascript
32+
editor.on('keymap:add', (keymap) => { ... });
33+
```
34+
35+
* `keymap:remove` Keymap removed. The removed keymap object is passed as an argument to the callback.
3436

35-
* `keymap:add` - New keymap added. The new keyamp object is passed as an argument
36-
* `keymap:remove` - Keymap removed. The removed keyamp object is passed as an argument
37-
* `keymap:emit` - Some keymap emitted, in arguments you get keymapId, shortcutUsed, Event
38-
* `keymap:emit:{keymapId}` - `keymapId` emitted, in arguments you get keymapId, shortcutUsed, Event
37+
```javascript
38+
editor.on('keymap:remove', (keymap) => { ... });
39+
```
40+
41+
* `keymap:emit` Some keymap emitted. The keymapId, shortcutUsed, and Event are passed as arguments to the callback.
42+
43+
```javascript
44+
editor.on('keymap:emit', (keymapId, shortcutUsed, event) => { ... });
45+
```
3946

4047
## Methods
4148

0 commit comments

Comments
 (0)