MCPcopy
hub / github.com/GrapesJS/grapesjs / AssetManager

Class AssetManager

packages/core/src/asset_manager/index.ts:49–427  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

47const assetCmd = 'open-assets';
48
49export default class AssetManager extends ItemManagerModule<AssetManagerConfig, Assets> {
50 storageKey = 'assets';
51 Asset = Asset;
52 Assets = Assets;
53 assetsVis: Assets;
54 am?: AssetsView;
55 fu?: FileUploaderView;
56 _bhv?: any;
57 events = AssetsEvents;
58
59 /**
60 * Initialize module
61 * @param {Object} config Configurations
62 * @private
63 */
64 constructor(em: EditorModel) {
65 // @ts-ignore
66 super(em, 'AssetManager', new Assets([], em), AssetsEvents, defConfig());
67 const { all, config } = this;
68 // @ts-ignore
69 this.assetsVis = new Assets([]);
70 const ppfx = config.pStylePrefix;
71 if (ppfx) {
72 config.stylePrefix = `${ppfx}${config.stylePrefix}`;
73 }
74
75 // Setup the sync between the global and public collections
76 all.on('add', (model: Asset) => this.getAllVisible().add(model));
77 all.on('remove', (model: Asset) => this.getAllVisible().remove(model));
78
79 this.__onAllEvent = debounce(() => this.__trgCustom(), 0);
80
81 return this;
82 }
83
84 /**
85 * Open the asset manager.
86 * @param {Object} [options] Options for the asset manager.
87 * @param {Array<String>} [options.types=['image']] Types of assets to show.
88 * @param {Function} [options.select] Type of operation to perform on asset selection. If not specified, nothing will happen.
89 * @example
90 * assetManager.open({
91 * select(asset, complete) {
92 * const selected = editor.getSelected();
93 * if (selected && selected.is('image')) {
94 * selected.addAttributes({ src: asset.getSrc() });
95 * // The default AssetManager UI will trigger `select(asset, false)` on asset click
96 * // and `select(asset, true)` on double-click
97 * complete && assetManager.close();
98 * }
99 * }
100 * });
101 * // with your custom types (you should have assets with those types declared)
102 * assetManager.open({ types: ['doc'], ... });
103 */
104 open(options: AssetOpenOptions = {}) {
105 const cmd = this.em.Commands;
106 cmd.run(assetCmd, {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected