Skip to content

Commit

Permalink
chore: fix paster exports and registration
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Jul 31, 2023
1 parent 68a8a5f commit 82b0f3e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions core/blockly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -606,6 +607,7 @@ export {Input};
export {inputs};
export {InsertionMarkerManager};
export {IObservable, isObservable};
export {IPaster, isPaster};
export {IPositionable};
export {IRegistrable};
export {ISelectable};
Expand Down
4 changes: 4 additions & 0 deletions core/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,3 +84,5 @@ export const TEST_ONLY = {
duplicateInternal,
copyInternal,
};

export {BlockPaster, registry};
9 changes: 7 additions & 2 deletions core/clipboard/block_paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockCopyData, BlockSvg> {
static TYPE = 'block';

paste(
copyData: BlockCopyData,
workspace: WorkspaceSvg,
Expand All @@ -29,3 +32,5 @@ export class BlockPaster implements IPaster<BlockCopyData, BlockSvg> {
}

export interface BlockCopyData extends CopyData {}

registry.register(BlockPaster.TYPE, new BlockPaster());
31 changes: 31 additions & 0 deletions core/clipboard/registry.ts
Original file line number Diff line number Diff line change
@@ -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<U extends CopyData, T extends ICopyable>(
type: string,
paster: IPaster<U, T>,
) {
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);
}
5 changes: 5 additions & 0 deletions core/clipboard/workspace_comment_paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WorkspaceCommentCopyData, WorkspaceCommentSvg>
{
static TYPE = 'workspace-comment';

paste(
copyData: WorkspaceCommentCopyData,
workspace: WorkspaceSvg,
Expand All @@ -28,3 +31,5 @@ export class WorkspaceCommentPaster
}

export interface WorkspaceCommentCopyData extends CopyData {}

registry.register(WorkspaceCommentPaster.TYPE, new WorkspaceCommentPaster());
5 changes: 5 additions & 0 deletions core/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -96,6 +98,9 @@ export class Type<_T> {

/** @internal */
static ICON = new Type<IIcon>('icon');

/** @internal */
static PASTER = new Type<IPaster<CopyData, ICopyable>>('paster');
}

/**
Expand Down

0 comments on commit 82b0f3e

Please sign in to comment.