Browse Source

Refactor CommandCopyComponent

typed-command-runs
Artur Arseniev 5 months ago
parent
commit
8e7e0b4d6d
  1. 4
      packages/core/src/commands/registry.ts
  2. 17
      packages/core/src/commands/view/CopyComponent.ts
  3. 37
      packages/core/test/specs/commands/view/CopyComponent.ts

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

@ -9,6 +9,7 @@ import type {
import type { FullscreenCommandRegistryRun, FullscreenCommandRegistryStop } from './view/Fullscreen';
import type { PreviewCommandRegistryRun, PreviewCommandRegistryStop } from './view/Preview';
import type { ResizeCommandRegistryRun, ResizeCommandRegistryStop } from './view/Resize';
import type { CopyComponentCommandRegistryRun } from './view/CopyComponent';
type CommandRegistryHandler = (...args: any[]) => any;
type CommandRegistryEntry<TRegistry, TId extends string> = TId extends keyof TRegistry
@ -20,7 +21,8 @@ type CommandRegistryEntry<TRegistry, TId extends string> = TId extends keyof TRe
export interface CommandRegistryRun
extends FullscreenCommandRegistryRun,
PreviewCommandRegistryRun,
ResizeCommandRegistryRun {}
ResizeCommandRegistryRun,
CopyComponentCommandRegistryRun {}
export interface CommandRegistryStop
extends FullscreenCommandRegistryStop,

17
packages/core/src/commands/view/CopyComponent.ts

@ -1,9 +1,16 @@
import { CommandObject } from './CommandAbstract';
import Editor from '../../editor';
import type { CommandPublicFnFromHandler } from '../registryHelpers';
import CommandAbstract from './CommandAbstract';
export default {
run(ed) {
export interface CopyComponentCommandRegistryRun {
'core:copy': CommandPublicFnFromHandler<CommandCopyComponent['run']>;
copy: CommandPublicFnFromHandler<CommandCopyComponent['run']>;
}
export default class CommandCopyComponent extends CommandAbstract {
run(ed: Editor) {
const em = ed.getModel();
const models = [...ed.getSelectedAll()].map((md) => md.delegate?.copy?.(md) || md).filter(Boolean);
models.length && em.set('clipboard', models);
},
} as CommandObject;
}
}

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

@ -0,0 +1,37 @@
import CopyComponent from '../../../../src/commands/view/CopyComponent';
describe('CopyComponent command', () => {
test('should set the clipboard with selected components', () => {
const command = new CopyComponent({});
const set = jest.fn();
const selected = [{ id: 'cmp-1' }, { id: 'cmp-2' }];
const editor = {
getModel: jest.fn(() => ({ set })),
getSelectedAll: jest.fn(() => selected),
};
command.run(editor);
expect(set).toHaveBeenCalledWith('clipboard', selected);
});
test('should use delegated copy target when available', () => {
const command = new CopyComponent({});
const set = jest.fn();
const delegated = { id: 'delegated' };
const component = {
delegate: {
copy: jest.fn(() => delegated),
},
};
const editor = {
getModel: jest.fn(() => ({ set })),
getSelectedAll: jest.fn(() => [component]),
};
command.run(editor);
expect(component.delegate.copy).toHaveBeenCalledWith(component);
expect(set).toHaveBeenCalledWith('clipboard', [delegated]);
});
});
Loading…
Cancel
Save