diff --git a/core/blockly.ts b/core/blockly.ts index c14f3452b39..9e8599ed93e 100644 --- a/core/blockly.ts +++ b/core/blockly.ts @@ -152,6 +152,7 @@ import {IKeyboardAccessible} from './interfaces/i_keyboard_accessible.js'; import {IMetricsManager} from './interfaces/i_metrics_manager.js'; import {IMovable} from './interfaces/i_movable.js'; import {IObservable, isObservable} from './interfaces/i_observable.js'; +import {IPaster, isPaster} from './interfaces/i_paster.js'; import {IPositionable} from './interfaces/i_positionable.js'; import {IRegistrable} from './interfaces/i_registrable.js'; import {ISelectable} from './interfaces/i_selectable.js'; @@ -606,6 +607,7 @@ export {Input}; export {inputs}; export {InsertionMarkerManager}; export {IObservable, isObservable}; +export {IPaster, isPaster}; export {IPositionable}; export {IRegistrable}; export {ISelectable}; diff --git a/core/clipboard.ts b/core/clipboard.ts index 662a1c9738a..9cad96751c0 100644 --- a/core/clipboard.ts +++ b/core/clipboard.ts @@ -8,6 +8,8 @@ import * as goog from '../closure/goog/goog.js'; goog.declareModuleId('Blockly.clipboard'); import type {CopyData, ICopyable} from './interfaces/i_copyable.js'; +import {BlockPaster} from './clipboard/block_paster.js'; +import * as registry from './clipboard/registry.js'; /** Metadata about the object that is currently on the clipboard. */ let copyData: CopyData | null = null; @@ -82,3 +84,5 @@ export const TEST_ONLY = { duplicateInternal, copyInternal, }; + +export {BlockPaster, registry}; diff --git a/core/clipboard/block_paster.ts b/core/clipboard/block_paster.ts index 363f70cad83..60ca336a5db 100644 --- a/core/clipboard/block_paster.ts +++ b/core/clipboard/block_paster.ts @@ -5,13 +5,16 @@ */ import {BlockSvg} from '../block_svg.js'; -import {CopyData} from '../interfaces/i_copyable'; +import {registry} from '../clipboard.js'; +import {CopyData} from '../interfaces/i_copyable.js'; import {IPaster} from '../interfaces/i_paster.js'; -import {State, append} from '../serialization/blocks'; +import {State, append} from '../serialization/blocks.js'; import {Coordinate} from '../utils/coordinate.js'; import {WorkspaceSvg} from '../workspace_svg.js'; export class BlockPaster implements IPaster { + static TYPE = 'block'; + paste( copyData: BlockCopyData, workspace: WorkspaceSvg, @@ -29,3 +32,5 @@ export class BlockPaster implements IPaster { } export interface BlockCopyData extends CopyData {} + +registry.register(BlockPaster.TYPE, new BlockPaster()); diff --git a/core/clipboard/registry.ts b/core/clipboard/registry.ts new file mode 100644 index 00000000000..5a8c8342ee2 --- /dev/null +++ b/core/clipboard/registry.ts @@ -0,0 +1,31 @@ +/** + * @license + * Copyright 2023 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import {ICopyable, CopyData} from '../interfaces/i_copyable.js'; +import type {IPaster} from '../interfaces/i_paster.js'; +import * as registry from '../registry.js'; + +/** + * Registers the given paster so that it cna be used for pasting. + * + * @param type The type of the paster to register, e.g. 'block', 'comment', etc. + * @param paster The paster to register. + */ +export function register( + type: string, + paster: IPaster, +) { + registry.register(registry.Type.PASTER, type, paster); +} + +/** + * Unregisters the paster associated with the given type. + * + * @param type The type of the paster to unregister. + */ +export function unregister(type: string) { + registry.unregister(registry.Type.PASTER, type); +} diff --git a/core/clipboard/workspace_comment_paster.ts b/core/clipboard/workspace_comment_paster.ts index 11211564ce7..7959802d6bc 100644 --- a/core/clipboard/workspace_comment_paster.ts +++ b/core/clipboard/workspace_comment_paster.ts @@ -9,10 +9,13 @@ import {CopyData} from '../interfaces/i_copyable.js'; import {Coordinate} from '../utils/coordinate.js'; import {WorkspaceSvg} from '../workspace_svg.js'; import {WorkspaceCommentSvg} from '../workspace_comment_svg.js'; +import {registry} from '../clipboard.js'; export class WorkspaceCommentPaster implements IPaster { + static TYPE = 'workspace-comment'; + paste( copyData: WorkspaceCommentCopyData, workspace: WorkspaceSvg, @@ -28,3 +31,5 @@ export class WorkspaceCommentPaster } export interface WorkspaceCommentCopyData extends CopyData {} + +registry.register(WorkspaceCommentPaster.TYPE, new WorkspaceCommentPaster()); diff --git a/core/registry.ts b/core/registry.ts index a2022700045..2893f3d0f96 100644 --- a/core/registry.ts +++ b/core/registry.ts @@ -22,6 +22,8 @@ import type {Options} from './options.js'; import type {Renderer} from './renderers/common/renderer.js'; import type {Theme} from './theme.js'; import type {ToolboxItem} from './toolbox/toolbox_item.js'; +import {IPaster} from './interfaces/i_paster.js'; +import {CopyData, ICopyable} from './interfaces/i_copyable.js'; /** * A map of maps. With the keys being the type and name of the class we are @@ -96,6 +98,9 @@ export class Type<_T> { /** @internal */ static ICON = new Type('icon'); + + /** @internal */ + static PASTER = new Type>('paster'); } /**