blockly
Version:
Blockly is a library for building visual programming editors.
140 lines • 5.74 kB
TypeScript
/**
* @license
* Copyright 2011 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { IConnectionChecker } from './interfaces/i_connection_checker.js';
import type { RenderedConnection } from './rendered_connection.js';
import type { Coordinate } from './utils/coordinate.js';
/**
* Database of connections.
* Connections are stored in order of their vertical component. This way
* connections in an area may be looked up quickly using a binary search.
*/
export declare class ConnectionDB {
private readonly connectionChecker;
/** Array of connections sorted by y position in workspace units. */
private readonly connections;
/**
* Count of how many times bulk updates have been enabled. When reaching 0,
* the connection DB will be resorted.
*/
private bulkCounter;
/**
* @param connectionChecker The workspace's connection type checker, used to
* decide if connections are valid during a drag.
*/
constructor(connectionChecker: IConnectionChecker);
/**
* Add a connection to the database. Should not already exist in the database.
*
* @param connection The connection to be added.
* @param yPos The y position used to decide where to insert the connection.
* @internal
*/
addConnection(connection: RenderedConnection, yPos: number): void;
/**
* Finds the index of the given connection.
*
* Starts by doing a binary search to find the approximate location, then
* linearly searches nearby for the exact connection.
*
* @param conn The connection to find.
* @param yPos The y position used to find the index of the connection.
* @returns The index of the connection, or -1 if the connection was not
* found.
*/
private findIndexOfConnection;
/**
* Finds the correct index for the given y position.
*
* @param yPos The y position used to decide where to insert the connection.
* @returns The candidate index.
*/
private calculateIndexForYPos;
/**
* Remove a connection from the database. Must already exist in DB.
*
* @param connection The connection to be removed.
* @param yPos The y position used to find the index of the connection.
* @throws {Error} If the connection cannot be found in the database.
*/
removeConnection(connection: RenderedConnection, yPos: number): void;
/**
* Moves the given connection in the connection DB to reflect its new Y
* position. For performance, this will be deferred if the connection DB is in
* the middle of a bulk update.
*
* @param connection The connection that is moving. Should be provided before
* its `Connection.y` field has been updated.
* @param newYPos The y coordinate (in workspace units) to which the
* connection will move.
*/
reorderConnection(connection: RenderedConnection, newYPos: number): void;
/**
* Find all nearby connections to the given connection.
* Type checking does not apply, since this function is used for bumping.
*
* @param connection The connection whose neighbours should be returned.
* @param maxRadius The maximum radius to another connection.
* @returns List of connections.
*/
getNeighbours(connection: RenderedConnection, maxRadius: number): RenderedConnection[];
/**
* Is the candidate connection close to the reference connection.
* Extremely fast; only looks at Y distance.
*
* @param index Index in database of candidate connection.
* @param baseY Reference connection's Y value.
* @param maxRadius The maximum radius to another connection.
* @returns True if connection is in range.
*/
private isInYRange;
/**
* Find the closest compatible connection to this connection.
*
* @param conn The connection searching for a compatible mate.
* @param maxRadius The maximum radius to another connection.
* @param dxy Offset between this connection's location in the database and
* the current location (as a result of dragging).
* @returns Contains two properties: 'connection' which is either another
* connection or null, and 'radius' which is the distance.
*/
searchForClosest(conn: RenderedConnection, maxRadius: number, dxy: Coordinate): {
connection: RenderedConnection | null;
radius: number;
};
/**
* Notifies the connection DB that a series of connection moves is about to
* begin. May be called multiple times, and must be paired with a
* corresponding call to `endBulkUpdates()`.
*
* @internal
*/
beginBulkUpdates(): void;
/**
* Notifies the connection DB that the most recent bulk series of connection
* moves has ended. Must be called after the corresponding
* `beginBulkUpdates()`. If all in-flight bulk updates have ended, this method
* will reorder the connection DB to reflect the current state of the
* connections.
*
* @internal
*/
endBulkUpdates(): void;
/**
* Sorts the connections in the connection DB to reflect the current
* coordinates of the connections to allow for quick and correct connection
* lookups. Run automatically when a bulk update finishes.
*/
private reorderConnections;
/**
* Initialize a set of connection DBs for a workspace.
*
* @param checker The workspace's connection checker, used to decide if
* connections are valid during a drag.
* @returns Array of databases.
*/
static init(checker: IConnectionChecker): ConnectionDB[];
}
//# sourceMappingURL=connection_db.d.ts.map