BlockSuite API Documentation / @blocksuite/affine-gfx-connector
@blocksuite/affine-gfx-connector
Classes
Extension
ConnectorTool
Understanding Extensions
Extensions provide a way to extend the functionality of a system using dependency injection. They allow you to register services, implementations, and factories in the DI container, which can then be retrieved and used by different parts of the application.
Extensions are particularly useful for:
- Registering different implementations for different types
- Creating pluggable architecture where components can be added or removed
- Managing dependencies between different parts of the application
Usage Example: Fruit Processing System
Let's consider a fruit processing system where different types of fruits need different processing methods. We'll show how to implement this using extensions.
Step 1: Define the interfaces
interface FruitProcessor {
process(fruit: Fruit): void;
}
interface Fruit {
type: string;
// other properties
}Step 2: Create a service identifier
import { createIdentifier } from '@blocksuite/global/di';
const FruitProcessorProvider = createIdentifier<FruitProcessor>('fruit-processor-provider');Step 3: Create implementations
class AppleProcessor implements FruitProcessor {
process(fruit: Fruit): void {
console.log('Slicing apple');
// Apple-specific processing
}
}
class BananaProcessor implements FruitProcessor {
process(fruit: Fruit): void {
console.log('Peeling banana');
// Banana-specific processing
}
}Step 4: Create an extension factory
const FruitProcessorExtension = (
fruitType: string,
implementation: new () => FruitProcessor
): ExtensionType => {
return {
setup: di => {
di.addImpl(FruitProcessorProvider(fruitType), implementation);
}
};
};Step 5: Create concrete extensions
export const AppleProcessorExtension = FruitProcessorExtension('apple', AppleProcessor);
export const BananaProcessorExtension = FruitProcessorExtension('banana', BananaProcessor);Step 6: Use the extensions
import { Container } from '@blocksuite/global/di';
class FruitProcessingSystem {
provider: ServiceProvider;
constructor(extensions: ExtensionType[]) {
const container = new Container();
// Set up all extensions
extensions.forEach(ext => ext.setup(container));
// Create a provider from the container
this.provider = container.provider();
}
processFruit(fruit: Fruit) {
// Get the appropriate processor based on fruit type
const processor = this.provider.get(FruitProcessorProvider(fruit.type));
// Process the fruit
processor.process(fruit);
}
}
// Initialize the system with extensions
const system = new FruitProcessingSystem([
AppleProcessorExtension,
BananaProcessorExtension
]);
// Use the system
system.processFruit({ type: 'apple' }); // Output: Slicing apple
system.processFruit({ type: 'banana' }); // Output: Peeling bananaNote: We deliberately used a non-block specific example here. In BlockSuite, the extension pattern can be applied to any entity that can be configured by third parties, not just blocks. This includes different tools in the whiteboard, different column types in database blocks, and many other extensible components. The pattern remains the same regardless of what you're extending.
Extends
Constructors
Properties
toolName
statictoolName:string='connector'
Overrides
Accessors
Methods
click()
click():
void
Returns
void
Overrides
deactivate()
deactivate():
void
Called when the tool is deactivated.
Returns
void
Overrides
dragEnd()
dragEnd():
void
Returns
void
Overrides
dragMove()
dragMove(
e):void
Parameters
e
Returns
void
Overrides
dragStart()
dragStart():
void
Returns
void
Overrides
findTargetByPoint()
findTargetByPoint(
point):void
Parameters
point
IVec
Returns
void
getNextMode()
getNextMode():
ConnectorMode
Returns
pointerDown()
pointerDown(
e):void
Parameters
e
Returns
void
Overrides
pointerMove()
pointerMove(
e):void
Parameters
e
Returns
void
Overrides
pointerUp()
pointerUp(
_):void
Parameters
_
Returns
void
Overrides
quickConnect()
quickConnect(
point,element):void
Parameters
point
IVec
element
Returns
void
ConnectorFilter
Understanding Extensions
Extensions provide a way to extend the functionality of a system using dependency injection. They allow you to register services, implementations, and factories in the DI container, which can then be retrieved and used by different parts of the application.
Extensions are particularly useful for:
- Registering different implementations for different types
- Creating pluggable architecture where components can be added or removed
- Managing dependencies between different parts of the application
Usage Example: Fruit Processing System
Let's consider a fruit processing system where different types of fruits need different processing methods. We'll show how to implement this using extensions.
Step 1: Define the interfaces
interface FruitProcessor {
process(fruit: Fruit): void;
}
interface Fruit {
type: string;
// other properties
}Step 2: Create a service identifier
import { createIdentifier } from '@blocksuite/global/di';
const FruitProcessorProvider = createIdentifier<FruitProcessor>('fruit-processor-provider');Step 3: Create implementations
class AppleProcessor implements FruitProcessor {
process(fruit: Fruit): void {
console.log('Slicing apple');
// Apple-specific processing
}
}
class BananaProcessor implements FruitProcessor {
process(fruit: Fruit): void {
console.log('Peeling banana');
// Banana-specific processing
}
}Step 4: Create an extension factory
const FruitProcessorExtension = (
fruitType: string,
implementation: new () => FruitProcessor
): ExtensionType => {
return {
setup: di => {
di.addImpl(FruitProcessorProvider(fruitType), implementation);
}
};
};Step 5: Create concrete extensions
export const AppleProcessorExtension = FruitProcessorExtension('apple', AppleProcessor);
export const BananaProcessorExtension = FruitProcessorExtension('banana', BananaProcessor);Step 6: Use the extensions
import { Container } from '@blocksuite/global/di';
class FruitProcessingSystem {
provider: ServiceProvider;
constructor(extensions: ExtensionType[]) {
const container = new Container();
// Set up all extensions
extensions.forEach(ext => ext.setup(container));
// Create a provider from the container
this.provider = container.provider();
}
processFruit(fruit: Fruit) {
// Get the appropriate processor based on fruit type
const processor = this.provider.get(FruitProcessorProvider(fruit.type));
// Process the fruit
processor.process(fruit);
}
}
// Initialize the system with extensions
const system = new FruitProcessingSystem([
AppleProcessorExtension,
BananaProcessorExtension
]);
// Use the system
system.processFruit({ type: 'apple' }); // Output: Slicing apple
system.processFruit({ type: 'banana' }); // Output: Peeling bananaNote: We deliberately used a non-block specific example here. In BlockSuite, the extension pattern can be applied to any entity that can be configured by third parties, not just blocks. This includes different tools in the whiteboard, different column types in database blocks, and many other extensible components. The pattern remains the same regardless of what you're extending.
Extends
Constructors
Properties
key
statickey:string='connector-filter'
Overrides
Accessors
Methods
mounted()
mounted():
void
Returns
void
Overrides
InteractivityExtension.mounted
Other
ConnectionOverlay
An overlay is a layer covered on top of elements, can be used for rendering non-CRDT state indicators.
Extends
Constructors
Constructor
new ConnectionOverlay(
gfx):ConnectionOverlay
Parameters
gfx
Returns
Overrides
Properties
highlightPoint
highlightPoint:
IVec|null=null
points
points:
IVec[] =[]
sourceBounds
sourceBounds:
IBound|null=null
targetBounds
targetBounds:
IBound|null=null
overlayName
staticoverlayName:string='connection'
Overrides
Methods
_clearRect()
_clearRect():
void
Returns
void
clear()
clear():
void
Returns
void
Overrides
dispose()
dispose():
void
Returns
void
Overrides
render()
render(
ctx):void
Parameters
ctx
CanvasRenderingContext2D
Returns
void
Overrides
renderConnector()
renderConnector(
point,excludedIds):Connection
Render the connector at the given point. It will try to find the closest connectable element and render the connector. If the point is not close to any connectable element, it will just render the connector at the given point.
Parameters
point
IVec
the point to render the connector
excludedIds
string[] = []
the ids of the elements that should be excluded
Returns
the connection result
ConnectorPathGenerator
Extends
Constructors
Constructor
new ConnectorPathGenerator(
options):ConnectorPathGenerator
Parameters
options
getElementById
(id) => GfxModel | null
Returns
Overrides
Properties
Methods
hasRelatedElement()
hasRelatedElement(
connecter):boolean
Parameters
connecter
ConnectorElementModel | LocalConnectorElementModel
Returns
boolean
updatePath()
staticupdatePath(connector,path,elementGetter?):void
Parameters
connector
ConnectorElementModel | LocalConnectorElementModel
path
PointLocation[] | null
elementGetter?
(id) => GfxModel | null
Returns
void
PathGenerator
Extended by
Constructors
Constructor
new PathGenerator():
PathGenerator
Returns
Properties
_aStarRunner
protected_aStarRunner:AStarRunner|null=null
Methods
_prepareOrthogonalConnectorInfo()
protected_prepareOrthogonalConnectorInfo(connectorInfo): [IVec,IVec,IVec,IVec,Bound|null,Bound|null,Bound|null,Bound|null]
Parameters
connectorInfo
Returns
[IVec, IVec, IVec, IVec, Bound | null, Bound | null, Bound | null, Bound | null]
generateOrthogonalConnectorPath()
generateOrthogonalConnectorPath(
input):IVec[]
Parameters
input
Returns
IVec[]
ConnectorElementView
The methods that a graphic element should implement. It is already included in the GfxCompatibleInterface interface.
Extends
Constructors
Properties
type
statictype:string='connector'
Overrides
Accessors
Methods
onCreated()
onCreated():
void
Returns
void
Overrides
onDragEnd()
onDragEnd(
context):void
Parameters
context
Returns
void
Overrides
onDragMove()
onDragMove(
context):void
Parameters
context
Returns
void
Overrides
GfxElementModelView.onDragMove
onDragStart()
onDragStart(
context):void
Parameters
context
Returns
void
Overrides
GfxElementModelView.onDragStart
Type Aliases
Connectable
Connectable =
Exclude<GfxModel,ConnectorElementModel|BrushElementModel|GroupElementModel>
ConnectorToolOptions
ConnectorToolOptions =
object
Properties
mode
mode:
ConnectorMode
OrthogonalConnectorInput
OrthogonalConnectorInput =
object
Properties
endBound
endBound:
Bound|null
endPoint
endPoint:
PointLocation
startBound
startBound:
Bound|null
startPoint
startPoint:
PointLocation
Variables
connector
constconnector:ElementRenderer<ConnectorElementModel|LocalConnectorElementModel>
ConnectorDomRendererExtension
constConnectorDomRendererExtension:ExtensionType
Extension to register the DOM-based renderer for 'connector' elements.
ConnectorElementRendererExtension
constConnectorElementRendererExtension:ExtensionType&object
Type Declaration
identifier
identifier:
ServiceIdentifier<ElementRenderer<ConnectorElementModel|LocalConnectorElementModel>>
ConnectorEndpointLocations
constConnectorEndpointLocations:IVec[]
ConnectorEndpointLocationsOnTriangle
constConnectorEndpointLocationsOnTriangle:IVec[]
ConnectorInteraction
constConnectorInteraction:ExtensionType
connectorQuickTool
constconnectorQuickTool:ExtensionType
connectorToMarkdownAdapterMatcher
constconnectorToMarkdownAdapterMatcher:ExtensionType&object
Type Declaration
identifier
identifier:
ServiceIdentifier<ElementToMarkdownAdapterMatcher>
connectorToolbarConfig
constconnectorToolbarConfig:object
Type Declaration
actions
readonlyactions: [{id:"a.stroke-color";content:TemplateResult<1> |null; }, {id:"b.style";when: (ctx) =>boolean;content:TemplateResult<1> |null; }, {actions: [{id:"a.start-point-style";content:TemplateResult<1> |null; }, {icon:TemplateResult<1>;id:"b.flip-direction";tooltip:"Flip direction";run:void; }, {id:"c.end-point-style";content:TemplateResult<1> |null; }, {id:"d.connector-shape";content:TemplateResult<1> |null; }];id:"c.endpoint-style"; }, {icon:TemplateResult<1>;id:"g.text";tooltip:"Add text";run:void;when:boolean; },...ToolbarGenericAction[]]
when()
readonlywhen: (ctx) =>boolean
Parameters
ctx
ToolbarContext
Returns
boolean
connectorToolbarExtension
constconnectorToolbarExtension:ExtensionType
connectorToPlainTextAdapterMatcher
constconnectorToPlainTextAdapterMatcher:ExtensionType&object
Type Declaration
identifier
identifier:
ServiceIdentifier<ElementToPlainTextAdapterMatcher>
Functions
calculateNearestLocation()
calculateNearestLocation(
point,bounds,locations,shortestDistance):IVec
Parameters
point
IVec
bounds
IBound
locations
IVec[] = ConnectorEndpointLocations
shortestDistance
number = Number.POSITIVE_INFINITY
Returns
IVec
connectorBaseDomRenderer()
connectorBaseDomRenderer(
model,element,renderer):void
Renders a ConnectorElementModel to a given HTMLElement using DOM/SVG. This function is intended to be registered via the DomElementRendererExtension.
Parameters
model
The connector element model containing rendering properties.
ConnectorElementModel | LocalConnectorElementModel
element
HTMLElement
The HTMLElement to apply the connector's styles to.
renderer
The main DOMRenderer instance, providing access to viewport and color utilities.
Returns
void
connectorDomRenderer()
connectorDomRenderer(
model,element,renderer):void
Parameters
model
element
HTMLElement
renderer
Returns
void
getAnchors()
getAnchors(
ele):object[]
Parameters
ele
Returns
object[]
getNearestConnectableAnchor()
getNearestConnectableAnchor(
ele,point):PointLocation|null
Parameters
ele
point
IVec
Returns
PointLocation | null
isConnectorAndBindingsAllSelected()
isConnectorAndBindingsAllSelected(
connector,selected):boolean
Parameters
connector
ConnectorElementModel | LocalConnectorElementModel
selected
GfxModel[]
Returns
boolean
isConnectorWithLabel()
isConnectorWithLabel(
model):boolean
Parameters
model
GfxModel | GfxLocalElementModel
Returns
boolean
mountConnectorLabelEditor()
mountConnectorLabelEditor(
connector,edgeless,point?):void
Parameters
connector
edgeless
point?
IVec
Returns
void