BlockSuite API Documentation / @blocksuite/affine-gfx-turbo-renderer
@blocksuite/affine-gfx-turbo-renderer
Classes
Extension
abstract BlockLayoutHandlerExtension<T>
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
Extended by
CodeLayoutHandlerExtensionImageLayoutHandlerExtensionListLayoutHandlerExtensionNoteLayoutHandlerExtensionParagraphLayoutHandlerExtension
Type Parameters
T
T extends BlockLayout = BlockLayout
Constructors
Properties
blockType
abstractreadonlyblockType:string
Methods
calculateBound()
abstractcalculateBound(layout):object
Parameters
layout
T
Returns
object
rect
rect:
Rect
subRects
subRects:
Rect[]
queryLayout()
abstractqueryLayout(model,host,viewportRecord):T|null
Parameters
model
host
viewportRecord
Returns
T | null
Other
ViewportLayoutPainter
Constructors
Constructor
new ViewportLayoutPainter(
extensions):ViewportLayoutPainter
Parameters
extensions
Returns
Properties
provider
provider:
ServiceProvider
Methods
getPainter()
getPainter(
type):BlockLayoutPainter|null
Parameters
type
string
Returns
BlockLayoutPainter | null
handler()
handler(
e):Promise<void>
Parameters
e
MessageEvent<MessagePaint>
Returns
Promise<void>
paint()
paint(
layout,version):void
Parameters
layout
version
number
Returns
void
paintTree()
paintTree(
layout,version):void
Parameters
layout
version
number
Returns
void
setSize()
setSize(
layoutRectW,layoutRectH,dpr,zoom):void
Parameters
layoutRectW
number
layoutRectH
number
dpr
number
zoom
number
Returns
void
ViewportTurboRendererExtension
Manages the Turbo Rendering process for the viewport, coordinating between the main thread and a painter worker. Turbo Rendering optimizes performance by rendering block content onto a canvas bitmap, falling back to standard DOM rendering during interactions.
To add Turbo Rendering support for a new block type (e.g., 'affine:my-block'):
- In the block's package (e.g.,
blocksuite/affine/blocks/my-block): a. Add@blocksuite/affine/gfx/turbo-rendereras a dependency inpackage.jsonand create asrc/turbodirectory. b. Implement the Layout Handler (e.g.,MyBlockLayoutHandlerExtension) and Painter Worker (e.g.,MyBlockLayoutPainterExtension). Refer toParagraphLayoutHandlerExtensionandParagraphLayoutPainterExtensioninblocksuite/affine/blocks/block-paragraphfor implementation examples. c. Export the Layout Handler and Painter Worker extensions from the block package's mainsrc/index.tsby adding these two explicit export statements:typescriptexport * from './turbo/my-block-layout-handler'; export * from './turbo/my-block-painter.worker';
d. Add an export mapping for the painter worker in package.json under the exports field (e.g., "./turbo-painter": "./src/turbo/my-block-painter.worker.ts"). e. Add a TypeScript project reference to blocksuite/affine/gfx/turbo-renderer in tsconfig.json.
In the application integration point (e.g.,
packages/frontend/core/src/blocksuite/extensionsandblocksuite/integration-test/src/__tests__/utils/renderer-entry.ts): a. Inturbo-renderer.ts(or the file setting upTurboRendererConfigFactory):- Import and add the new Layout Handler extension to the
patchTurboRendererExtensionarray (or equivalent DI setup). See howParagraphLayoutHandlerExtensionis added as a reference. b. Inturbo-painter.worker.ts(the painter worker entry point): - Import and add the new Painter Worker extension to the
ViewportLayoutPainterconstructor's extension array. See howParagraphLayoutPainterExtensionis added as a reference.
- Import and add the new Layout Handler extension to the
Run
yarn affine initfrom the workspace root to update generated configuration files (workspace.gen.ts) and the lockfile (yarn.lock).
Note: Always ensure the directory structure and export patterns match the paragraph block (blocksuite/affine/blocks/block-paragraph) for consistency.
Extends
Constructors
Constructor
new ViewportTurboRendererExtension(
gfx):ViewportTurboRendererExtension
Parameters
gfx
Returns
ViewportTurboRendererExtension
Overrides
Properties
canvas
readonlycanvas:HTMLCanvasElement
layoutCacheData
layoutCacheData:
ViewportLayoutTree|null=null
optimizedBlockIds
optimizedBlockIds:
string[] =[]
state$
readonlystate$:BehaviorSubject<RenderingState>
key
statickey:string='viewportTurboRenderer'
Overrides
Accessors
currentState
Get Signature
get currentState():
RenderingState
Returns
layoutCache
Get Signature
get layoutCache():
ViewportLayoutTree
Returns
options
Get Signature
get options():
RendererOptions
Returns
selection
Get Signature
get selection():
GfxSelectionManager
Returns
viewport
Get Signature
get viewport():
Viewport
Returns
Methods
canUseBitmapCache()
canUseBitmapCache():
boolean
Returns
boolean
invalidate()
invalidate():
void
Returns
void
mounted()
mounted():
void
Returns
void
Overrides
refresh()
refresh():
Promise<void>
Returns
Promise<void>
unmounted()
unmounted():
void
Returns
void
Overrides
extendGfx()
staticextendGfx(gfx):void
Parameters
gfx
Returns
void
Overrides
setup()
staticsetup(di):void
Parameters
di
Container
Returns
void
Overrides
Interfaces
BlockLayout
Extends
Record<string,unknown>
Extended by
Indexable
[key: string]: unknown
Properties
blockId
blockId:
string
rect
rect:
object
h
h:
number
w
w:
number
x
x:
number
y
y:
number
type
type:
string
BlockLayoutPainter
Methods
paint()
paint(
ctx,block,layoutBaseX,layoutBaseY):void
Parameters
ctx
OffscreenCanvasRenderingContext2D
block
layoutBaseX
number
layoutBaseY
number
Returns
void
BlockLayoutTreeNode
Properties
blockId
blockId:
string
children
children:
BlockLayoutTreeNode[]
layout
layout:
BlockLayout
type
type:
string
Rect
Properties
h
h:
number
w
w:
number
x
x:
number
y
y:
number
RendererOptions
Properties
debounceTime
debounceTime:
number
enableBitmapRendering?
optionalenableBitmapRendering:boolean
zoomThreshold
zoomThreshold:
number
TextRect
Properties
rect
rect:
Rect
text
text:
string
TurboRendererConfig
Properties
options?
optionaloptions:Partial<RendererOptions>
painterWorkerEntry()
painterWorkerEntry: () =>
Worker
Returns
Worker
ViewportLayoutTree
Properties
overallRect
overallRect:
object
h
h:
number
w
w:
number
x
x:
number
y
y:
number
roots
roots:
BlockLayoutTreeNode[]
ViewportState
Properties
viewportX
viewportX:
number
viewportY
viewportY:
number
viewScale
viewScale:
number
zoom
zoom:
number
Type Aliases
HostToWorkerMessage
HostToWorkerMessage =
MessagePaint
MessageBitmapPainted
MessageBitmapPainted =
object
Properties
bitmap
bitmap:
ImageBitmap
type
type:
"bitmapPainted"
version
version:
number
MessagePaint
MessagePaint =
object
Properties
data
data:
object
dpr
dpr:
number
height
height:
number
layout
layout:
ViewportLayoutTree
version
version:
number
width
width:
number
zoom
zoom:
number
type
type:
"paintLayout"
MessagePaintError
MessagePaintError =
object
Properties
blockType
blockType:
string
error
error:
string
type
type:
"paintError"
RenderingState
RenderingState =
"inactive"|"pending"|"zooming"|"rendering"|"ready"
Represents the rendering state of the ViewportTurboRenderer
- inactive: Renderer is not active
- pending: Bitmap is invalid or not yet available, falling back to DOM rendering
- zooming: Zooming in or out, will use fast canvas placeholder rendering
- rendering: Currently rendering to a bitmap (async operation in progress)
- ready: Bitmap is valid and rendered, DOM elements can be safely removed
WorkerToHostMessage
WorkerToHostMessage =
MessageBitmapPainted|MessagePaintError
Variables
BlockLayoutHandlersIdentifier
constBlockLayoutHandlersIdentifier:ServiceIdentifier<BlockLayoutHandlerExtension<BlockLayout>> & <U>(variant) =>ServiceIdentifier<U>
BlockPainterProvider
constBlockPainterProvider:ServiceIdentifier<BlockLayoutPainter> & <U>(variant) =>ServiceIdentifier<U>
TurboRendererConfigFactory
constTurboRendererConfigFactory:ConfigFactory<TurboRendererConfig>
ViewportTurboRendererIdentifier
constViewportTurboRendererIdentifier:ServiceIdentifier<GfxExtension>
Functions
BlockLayoutPainterExtension()
BlockLayoutPainterExtension(
type,painter):ExtensionType
Parameters
type
string
painter
() => BlockLayoutPainter
Returns
getBaseline()
getBaseline(
fontSize):number
Parameters
fontSize
number
Returns
number
getSentenceRects()
getSentenceRects(
element,sentence):TextRect[]
Parameters
element
Element
sentence
string
Returns
TextRect[]
segmentSentences()
segmentSentences(
text):string[]
Parameters
text
string
Returns
string[]