Browse Source

Refactor OpenAssets

typed-command-runs
Artur Arseniev 5 months ago
parent
commit
866aeebd18
  1. 3
      packages/core/src/commands/registry.ts
  2. 62
      packages/core/src/commands/view/OpenAssets.ts
  3. 37
      packages/core/test/specs/commands/view/OpenAssets.ts

3
packages/core/src/commands/registry.ts

@ -14,6 +14,7 @@ import type { PasteComponentCommandRegistryRun } from './view/PasteComponent';
import type { CanvasMoveCommandRegistryRun, CanvasMoveCommandRegistryStop } from './view/CanvasMove'; import type { CanvasMoveCommandRegistryRun, CanvasMoveCommandRegistryStop } from './view/CanvasMove';
import type { CanvasClearCommandRegistryRun } from './view/CanvasClear'; import type { CanvasClearCommandRegistryRun } from './view/CanvasClear';
import type { ExportTemplateCommandRegistryRun, ExportTemplateCommandRegistryStop } from './view/ExportTemplate'; import type { ExportTemplateCommandRegistryRun, ExportTemplateCommandRegistryStop } from './view/ExportTemplate';
import type { OpenAssetsCommandRegistryRun, OpenAssetsCommandRegistryStop } from './view/OpenAssets';
import type { OpenLayersCommandRegistryRun, OpenLayersCommandRegistryStop } from './view/OpenLayers'; import type { OpenLayersCommandRegistryRun, OpenLayersCommandRegistryStop } from './view/OpenLayers';
import type { OpenBlocksCommandRegistryRun, OpenBlocksCommandRegistryStop } from './view/OpenBlocks'; import type { OpenBlocksCommandRegistryRun, OpenBlocksCommandRegistryStop } from './view/OpenBlocks';
import type { import type {
@ -41,6 +42,7 @@ export interface CommandRegistryRun
CanvasMoveCommandRegistryRun, CanvasMoveCommandRegistryRun,
CanvasClearCommandRegistryRun, CanvasClearCommandRegistryRun,
ExportTemplateCommandRegistryRun, ExportTemplateCommandRegistryRun,
OpenAssetsCommandRegistryRun,
OpenBlocksCommandRegistryRun, OpenBlocksCommandRegistryRun,
OpenLayersCommandRegistryRun, OpenLayersCommandRegistryRun,
OpenStyleManagerCommandRegistryRun, OpenStyleManagerCommandRegistryRun,
@ -52,6 +54,7 @@ export interface CommandRegistryStop
ResizeCommandRegistryStop, ResizeCommandRegistryStop,
CanvasMoveCommandRegistryStop, CanvasMoveCommandRegistryStop,
ExportTemplateCommandRegistryStop, ExportTemplateCommandRegistryStop,
OpenAssetsCommandRegistryStop,
OpenBlocksCommandRegistryStop, OpenBlocksCommandRegistryStop,
OpenLayersCommandRegistryStop, OpenLayersCommandRegistryStop,
OpenStyleManagerCommandRegistryStop, OpenStyleManagerCommandRegistryStop,

62
packages/core/src/commands/view/OpenAssets.ts

@ -1,29 +1,53 @@
import { isFunction } from 'underscore'; import { isFunction } from 'underscore';
import Asset from '../../asset_manager/model/Asset'; import Asset from '../../asset_manager/model/Asset';
import Editor from '../../editor';
import { createEl } from '../../utils/dom'; import { createEl } from '../../utils/dom';
import { CommandObject } from './CommandAbstract'; import type { CommandPublicFnFromHandler } from '../registryHelpers';
import CommandAbstract from './CommandAbstract';
export default { export interface OpenAssetsCommandRegistryRun {
open(content: string) { 'core:open-assets': CommandPublicFnFromHandler<CommandOpenAssets['run']>;
'open-assets': CommandPublicFnFromHandler<CommandOpenAssets['run']>;
}
export interface OpenAssetsCommandRegistryStop {
'core:open-assets': CommandPublicFnFromHandler<CommandOpenAssets['stop']>;
'open-assets': CommandPublicFnFromHandler<CommandOpenAssets['stop']>;
}
export default class CommandOpenAssets extends CommandAbstract {
title = '';
editor?: Editor;
am?: any;
rendered?: HTMLElement;
open(content: string | HTMLElement) {
const { editor, title, config, am } = this; const { editor, title, config, am } = this;
if (!editor || !config || !am) return;
const { custom } = config; const { custom } = config;
if (isFunction(custom.open)) { if (isFunction(custom.open)) {
return custom.open(am.__customData()); return custom.open(am.__customData());
} }
const { Modal } = editor; const { Modal } = editor;
Modal.open({ title, content }).onceClose(() => editor.stopCommand(this.id)); Modal.open({ title, content }).onceClose(() => editor.stopCommand(this.id as string));
}, }
close() { close() {
const { custom } = this.config; const { config, am, editor } = this;
if (!config || !am || !editor) return;
const { custom } = config;
if (isFunction(custom.close)) { if (isFunction(custom.close)) {
return custom.close(this.am.__customData()); return custom.close(am.__customData());
} }
const { Modal } = this.editor;
const { Modal } = editor;
Modal && Modal.close(); Modal && Modal.close();
}, }
run(editor, sender, opts = {}) { run(editor: Editor, sender: any, opts: any = {}) {
const am = editor.AssetManager; const am = editor.AssetManager;
const config = am.getConfig(); const config = am.getConfig();
const { types = [], accept, select } = opts; const { types = [], accept, select } = opts;
@ -59,18 +83,20 @@ export default {
this.rendered = am.getContainer(); this.rendered = am.getContainer();
} }
if (accept) { const { rendered } = this;
const uploadEl = this.rendered.querySelector(`input#${config.stylePrefix}uploadFile`); if (accept && rendered) {
const uploadEl = rendered.querySelector(`input#${config.stylePrefix}uploadFile`);
uploadEl && uploadEl.setAttribute('accept', accept); uploadEl && uploadEl.setAttribute('accept', accept);
} }
} }
this.open(this.rendered); const { rendered } = this;
rendered && this.open(rendered);
return this; return this;
}, }
stop(editor) { stop(editor: Editor) {
this.editor = editor; this.editor = editor;
this.close(this.rendered); this.close();
}, }
} as CommandObject<any, { [k: string]: any }>; }

37
packages/core/test/specs/commands/view/OpenAssets.ts

@ -0,0 +1,37 @@
import OpenAssets from '../../../../src/commands/view/OpenAssets';
describe('OpenAssets command', () => {
test('open should open the modal', () => {
const onceClose = jest.fn();
const command = new OpenAssets({});
command.editor = {
Modal: {
open: jest.fn(() => ({ onceClose })),
},
stopCommand: jest.fn(),
} as any;
command.am = { __customData: jest.fn() };
command.config = { custom: false };
command.title = 'Assets';
command.open('content');
expect(command.editor.Modal.open).toHaveBeenCalledWith({ title: 'Assets', content: 'content' });
expect(onceClose).toHaveBeenCalledTimes(1);
});
test('stop should close the modal', () => {
const command = new OpenAssets({});
command.editor = {
Modal: {
close: jest.fn(),
},
} as any;
command.am = { __customData: jest.fn() };
command.config = { custom: false };
command.stop(command.editor);
expect(command.editor.Modal.close).toHaveBeenCalledTimes(1);
});
});
Loading…
Cancel
Save