Skip to content

Commit a424617

Browse files
authored
Merge branch 'GrapesJS:dev' into dev
2 parents c034a50 + d9ca5e2 commit a424617

File tree

11 files changed

+252
-133
lines changed

11 files changed

+252
-133
lines changed

docs/api/assets.md

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,71 @@ const assetManager = editor.AssetManager;
1919
```
2020

2121
## Available Events
22+
* `asset:add` New asset added to the collection. The [Asset] is passed as an argument to the callback.
2223

23-
* `asset:open` - Asset Manager opened.
24-
* `asset:close` - Asset Manager closed.
25-
* `asset:add` - Asset added. The [Asset] is passed as an argument to the callback.
26-
* `asset:remove` - Asset removed. The [Asset] is passed as an argument to the callback.
27-
* `asset:update` - Asset updated. The updated [Asset] and the object containing changes are passed as arguments to the callback.
28-
* `asset:upload:start` - Before the upload is started.
29-
* `asset:upload:end` - After the upload is ended.
30-
* `asset:upload:error` - On any error in upload, passes the error as an argument.
31-
* `asset:upload:response` - On upload response, passes the result as an argument.
32-
* `asset` - 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.
33-
* `asset:custom` - Event for handling custom Asset Manager UI.
24+
```javascript
25+
editor.on('asset:add', (asset) => { ... });
26+
```
27+
28+
* `asset:remove` Asset removed from the collection. The [Asset] is passed as an argument to the callback.
29+
30+
```javascript
31+
editor.on('asset:remove', (asset) => { ... });
32+
```
33+
34+
* `asset:update` Asset updated. The [Asset] and the object containing changes are passed as arguments to the callback.
35+
36+
```javascript
37+
editor.on('asset:update', (asset, updatedProps) => { ... });
38+
```
39+
40+
* `asset:open` Asset Manager opened.
41+
42+
```javascript
43+
editor.on('asset:open', () => { ... });
44+
```
45+
46+
* `asset:close` Asset Manager closed.
47+
48+
```javascript
49+
editor.on('asset:close', () => { ... });
50+
```
51+
52+
* `asset:upload:start` Asset upload start.
53+
54+
```javascript
55+
editor.on('asset:upload:start', () => { ... });
56+
```
57+
58+
* `asset:upload:end` Asset upload end.
59+
60+
```javascript
61+
editor.on('asset:upload:end', (result) => { ... });
62+
```
63+
64+
* `asset:upload:error` Asset upload error.
65+
66+
```javascript
67+
editor.on('asset:upload:error', (error) => { ... });
68+
```
69+
70+
* `asset:upload:response` Asset upload response.
71+
72+
```javascript
73+
editor.on('asset:upload:response', (res) => { ... });
74+
```
75+
76+
* `asset:custom` Event to use in case of [custom Asset Manager UI](https://grapesjs.com/docs/modules/Assets.html#customization).
77+
78+
```javascript
79+
editor.on('asset:custom', ({ container, assets, ... }) => { ... });
80+
```
81+
82+
* `asset` 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.
83+
84+
```javascript
85+
editor.on('asset', ({ event, model, ... }) => { ... });
86+
```
3487

3588
## Methods
3689

@@ -160,7 +213,7 @@ Remove asset
160213
### Parameters
161214

162215
* `asset` **([String][13] | [Asset])** Asset or asset URL
163-
* `opts` **Record<[string][13], any>?**
216+
* `opts` **RemoveOptions?**
164217

165218
### Examples
166219

src/asset_manager/index.ts

Lines changed: 45 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,7 @@
1414
* const assetManager = editor.AssetManager;
1515
* ```
1616
*
17-
* ## Available Events
18-
* * `asset:open` - Asset Manager opened.
19-
* * `asset:close` - Asset Manager closed.
20-
* * `asset:add` - Asset added. The [Asset] is passed as an argument to the callback.
21-
* * `asset:remove` - Asset removed. The [Asset] is passed as an argument to the callback.
22-
* * `asset:update` - Asset updated. The updated [Asset] and the object containing changes are passed as arguments to the callback.
23-
* * `asset:upload:start` - Before the upload is started.
24-
* * `asset:upload:end` - After the upload is ended.
25-
* * `asset:upload:error` - On any error in upload, passes the error as an argument.
26-
* * `asset:upload:response` - On upload response, passes the result as an argument.
27-
* * `asset` - 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.
28-
* * `asset:custom` - Event for handling custom Asset Manager UI.
17+
* {REPLACE_EVENTS}
2918
*
3019
* ## Methods
3120
* * [open](#open)
@@ -45,67 +34,20 @@
4534

4635
import { debounce, isFunction } from 'underscore';
4736
import { ItemManagerModule } from '../abstract/Module';
37+
import { AddOptions, RemoveOptions } from '../common';
4838
import EditorModel from '../editor/model/Editor';
39+
import { ProjectData } from '../storage_manager';
4940
import defaults, { AssetManagerConfig } from './config/config';
5041
import Asset from './model/Asset';
5142
import Assets from './model/Assets';
43+
import AssetsEvents, { AssetOpenOptions } from './types';
5244
import AssetsView from './view/AssetsView';
5345
import FileUploaderView from './view/FileUploader';
5446

55-
export type AssetEvent =
56-
| 'asset'
57-
| 'asset:open'
58-
| 'asset:close'
59-
| 'asset:add'
60-
| 'asset:remove'
61-
| 'asset:update'
62-
| 'asset:custom'
63-
| 'asset:upload:start'
64-
| 'asset:upload:end'
65-
| 'asset:upload:error'
66-
| 'asset:upload:response';
67-
68-
export const evAll = 'asset';
69-
export const evPfx = `${evAll}:`;
70-
export const evSelect = `${evPfx}select`;
71-
export const evUpdate = `${evPfx}update`;
72-
export const evAdd = `${evPfx}add`;
73-
export const evRemove = `${evPfx}remove`;
74-
export const evRemoveBefore = `${evRemove}:before`;
75-
export const evCustom = `${evPfx}custom`;
76-
export const evOpen = `${evPfx}open`;
77-
export const evClose = `${evPfx}close`;
78-
export const evUpload = `${evPfx}upload`;
79-
export const evUploadStart = `${evUpload}:start`;
80-
export const evUploadEnd = `${evUpload}:end`;
81-
export const evUploadError = `${evUpload}:error`;
82-
export const evUploadRes = `${evUpload}:response`;
83-
const assetCmd = 'open-assets';
84-
const assetEvents = {
85-
all: evAll,
86-
select: evSelect,
87-
update: evUpdate,
88-
add: evAdd,
89-
remove: evRemove,
90-
removeBefore: evRemoveBefore,
91-
custom: evCustom,
92-
open: evOpen,
93-
close: evClose,
94-
uploadStart: evUploadStart,
95-
uploadEnd: evUploadEnd,
96-
uploadError: evUploadError,
97-
uploadResponse: evUploadRes,
98-
};
99-
10047
// TODO
10148
type AssetProps = Record<string, any>;
10249

103-
type OpenOptions = {
104-
select?: (asset: Asset, complete: boolean) => void;
105-
types?: string[];
106-
accept?: string;
107-
target?: any;
108-
};
50+
const assetCmd = 'open-assets';
10951

11052
export default class AssetManager extends ItemManagerModule<AssetManagerConfig, Assets> {
11153
storageKey = 'assets';
@@ -115,7 +57,7 @@ export default class AssetManager extends ItemManagerModule<AssetManagerConfig,
11557
am?: AssetsView;
11658
fu?: FileUploaderView;
11759
_bhv?: any;
118-
events!: typeof assetEvents;
60+
events = AssetsEvents;
11961

12062
/**
12163
* Initialize module
@@ -124,11 +66,10 @@ export default class AssetManager extends ItemManagerModule<AssetManagerConfig,
12466
*/
12567
constructor(em: EditorModel) {
12668
// @ts-ignore
127-
super(em, 'AssetManager', new Assets([], em), assetEvents, defaults);
69+
super(em, 'AssetManager', new Assets([], em), AssetsEvents, defaults);
12870
const { all, config } = this;
12971
// @ts-ignore
13072
this.assetsVis = new Assets([]);
131-
// @ts-ignore
13273
const ppfx = config.pStylePrefix;
13374
if (ppfx) {
13475
config.stylePrefix = `${ppfx}${config.stylePrefix}`;
@@ -143,40 +84,6 @@ export default class AssetManager extends ItemManagerModule<AssetManagerConfig,
14384
return this;
14485
}
14586

146-
__propEv(ev: string, ...data: any[]) {
147-
this.em.trigger(ev, ...data);
148-
this.getAll().trigger(ev, ...data);
149-
}
150-
151-
__trgCustom() {
152-
const bhv = this.__getBehaviour();
153-
const custom = this.getConfig().custom;
154-
155-
if (!bhv.container && !(custom as any).open) {
156-
return;
157-
}
158-
this.em.trigger(this.events.custom, this.__customData());
159-
}
160-
161-
__customData() {
162-
const bhv = this.__getBehaviour();
163-
return {
164-
am: this as AssetManager,
165-
open: this.isOpen(),
166-
assets: this.getAll().models,
167-
types: bhv.types || [],
168-
container: bhv.container,
169-
close: () => this.close(),
170-
remove: (asset: string | Asset, opts?: Record<string, any>) => this.remove(asset, opts),
171-
select: (asset: Asset, complete: boolean) => {
172-
const res = this.add(asset);
173-
isFunction(bhv.select) && bhv.select(res, complete);
174-
},
175-
// extra
176-
options: bhv.options || {},
177-
};
178-
}
179-
18087
/**
18188
* Open the asset manager.
18289
* @param {Object} [options] Options for the asset manager.
@@ -197,7 +104,7 @@ export default class AssetManager extends ItemManagerModule<AssetManagerConfig,
197104
* // with your custom types (you should have assets with those types declared)
198105
* assetManager.open({ types: ['doc'], ... });
199106
*/
200-
open(options: OpenOptions = {}) {
107+
open(options: AssetOpenOptions = {}) {
201108
const cmd = this.em.Commands;
202109
cmd.run(assetCmd, {
203110
types: ['image'],
@@ -246,7 +153,7 @@ export default class AssetManager extends ItemManagerModule<AssetManagerConfig,
246153
* });
247154
* assetManager.add([{ src: 'img2.jpg' }, { src: 'img2.png' }]);
248155
*/
249-
add(asset: string | AssetProps | (string | AssetProps)[], opts: Record<string, any> = {}) {
156+
add(asset: string | AssetProps | (string | AssetProps)[], opts: AddOptions = {}) {
250157
// Put the model at the beginning
251158
if (typeof opts.at == 'undefined') {
252159
opts.at = 0;
@@ -292,15 +199,15 @@ export default class AssetManager extends ItemManagerModule<AssetManagerConfig,
292199
* const asset = assetManager.get('http://img.jpg');
293200
* assetManager.remove(asset);
294201
*/
295-
remove(asset: string | Asset, opts?: Record<string, any>) {
202+
remove(asset: string | Asset, opts?: RemoveOptions) {
296203
return this.__remove(asset, opts);
297204
}
298205

299206
store() {
300207
return this.getProjectData();
301208
}
302209

303-
load(data: Record<string, any>) {
210+
load(data: ProjectData) {
304211
return this.loadProjectData(data);
305212
}
306213

@@ -462,6 +369,40 @@ export default class AssetManager extends ItemManagerModule<AssetManagerConfig,
462369
this.config.onDblClick = func;
463370
}
464371

372+
__propEv(ev: string, ...data: any[]) {
373+
this.em.trigger(ev, ...data);
374+
this.getAll().trigger(ev, ...data);
375+
}
376+
377+
__trgCustom() {
378+
const bhv = this.__getBehaviour();
379+
const custom = this.getConfig().custom;
380+
381+
if (!bhv.container && !(custom as any).open) {
382+
return;
383+
}
384+
this.em.trigger(this.events.custom, this.__customData());
385+
}
386+
387+
__customData() {
388+
const bhv = this.__getBehaviour();
389+
return {
390+
am: this as AssetManager,
391+
open: this.isOpen(),
392+
assets: this.getAll().models,
393+
types: bhv.types || [],
394+
container: bhv.container,
395+
close: () => this.close(),
396+
remove: (asset: string | Asset, opts?: Record<string, any>) => this.remove(asset, opts),
397+
select: (asset: Asset, complete: boolean) => {
398+
const res = this.add(asset);
399+
isFunction(bhv.select) && bhv.select(res, complete);
400+
},
401+
// extra
402+
options: bhv.options || {},
403+
};
404+
}
405+
465406
__behaviour(opts = {}) {
466407
return (this._bhv = {
467408
...(this._bhv || {}),

0 commit comments

Comments
 (0)