mirror of https://github.com/artf/grapesjs.git
3 changed files with 52 additions and 6 deletions
@ -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; |
|||
} |
|||
} |
|||
|
|||
@ -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…
Reference in new issue