BlockSuite API Documentation / @blocksuite/affine-block-code
@blocksuite/affine-block-code
Classes
Extension
CodeLayoutHandlerExtension
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
blockType
readonlyblockType:"affine:code"='affine:code'
Overrides
BlockLayoutHandlerExtension.blockType
Methods
calculateBound()
calculateBound(
layout):object
Parameters
layout
Returns
object
rect
rect:
Rect
subRects
subRects:
Rect[]
Overrides
BlockLayoutHandlerExtension.calculateBound
queryLayout()
queryLayout(
model,host,viewportRecord):CodeLayout|null
Parameters
model
host
viewportRecord
Returns
CodeLayout | null
Overrides
BlockLayoutHandlerExtension.queryLayout
setup()
staticsetup(di):void
Parameters
di
Container
Returns
void
Overrides
BlockLayoutHandlerExtension.setup
Other
CodeBlockClipboard
A life cycle watcher is an extension that watches the life cycle of the editor. It is used to perform actions when the editor is created, mounted, rendered, or unmounted.
When creating a life cycle watcher, you must define a key that is unique to the watcher. The key is used to identify the watcher in the dependency injection container.
class MyLifeCycleWatcher extends LifeCycleWatcher {
static override readonly key = 'my-life-cycle-watcher';In the life cycle watcher, the methods will be called in the following order:
created: Called when the std is created.rendered: Called whenstd.renderis called.mounted: Called when the editor host is mounted.unmounted: Called when the editor host is unmounted.
Extends
Constructors
Properties
key
readonlystatickey:"code-block-clipboard"='code-block-clipboard'
Overrides
Accessors
_adapters
Get Signature
get _adapters():
ClipboardAdapterConfig[]
Returns
Overrides
Methods
CodeBlockClipboardController
A life cycle watcher is an extension that watches the life cycle of the editor. It is used to perform actions when the editor is created, mounted, rendered, or unmounted.
When creating a life cycle watcher, you must define a key that is unique to the watcher. The key is used to identify the watcher in the dependency injection container.
class MyLifeCycleWatcher extends LifeCycleWatcher {
static override readonly key = 'my-life-cycle-watcher';In the life cycle watcher, the methods will be called in the following order:
created: Called when the std is created.rendered: Called whenstd.renderis called.mounted: Called when the editor host is mounted.unmounted: Called when the editor host is unmounted.
Extends
Constructors
Constructor
new CodeBlockClipboardController(
std,clipboard):CodeBlockClipboardController
Parameters
std
clipboard
Returns
Overrides
Properties
clipboard
readonlyclipboard:CodeBlockClipboard
onPaste
onPaste:
UIEventHandler
key
statickey:string='code-block-clipboard-controller'
Overrides
Methods
_init()
protected_init():void
Returns
void
mounted()
mounted():
void
Called when editor host is mounted. Which means the editor host emit the connectedCallback lifecycle event.
Returns
void
Overrides
unmounted()
unmounted():
void
Called when editor host is unmounted. Which means the editor host emit the disconnectedCallback lifecycle event.
Returns
void
Overrides
setup()
staticsetup(di):void
Parameters
di
Container
Returns
void
Overrides
CodeBlockHighlighter
A life cycle watcher is an extension that watches the life cycle of the editor. It is used to perform actions when the editor is created, mounted, rendered, or unmounted.
When creating a life cycle watcher, you must define a key that is unique to the watcher. The key is used to identify the watcher in the dependency injection container.
class MyLifeCycleWatcher extends LifeCycleWatcher {
static override readonly key = 'my-life-cycle-watcher';In the life cycle watcher, the methods will be called in the following order:
created: Called when the std is created.rendered: Called whenstd.renderis called.mounted: Called when the editor host is mounted.unmounted: Called when the editor host is unmounted.
Extends
Constructors
Properties
highlighter$
highlighter$:
Signal<HighlighterCore|null>
key
statickey:string='code-block-highlighter'
Overrides
Accessors
themeKey
Get Signature
get themeKey():
string|undefined
Returns
string | undefined
Methods
mounted()
mounted():
void
Called when editor host is mounted. Which means the editor host emit the connectedCallback lifecycle event.
Returns
void
Overrides
unmounted()
unmounted():
void
Called when editor host is unmounted. Which means the editor host emit the disconnectedCallback lifecycle event.
Returns
void
Overrides
CodeBlockComponent
Extends
CaptionedBlockComponent<CodeBlockModel>
Constructors
Other
collapsed$
collapsed$:
Signal<boolean>
highlightTokens$
highlightTokens$:
Signal<ThemedToken[][]>
languageName$
languageName$:
Signal<string>
preview$
preview$:
Signal<boolean>
styles
staticstyles:CSSResult=codeBlockStyles
Overrides
CaptionedBlockComponent.styles
blockContainerStyles
Overrides
CaptionedBlockComponent.blockContainerStyles
highlighter
Get Signature
get highlighter():
CodeBlockHighlighter
Returns
inlineEditor
Get Signature
get inlineEditor():
InlineEditor<{bold?:true|null;code?:true|null;italic?:true|null;link?:string|null;strike?:true|null;underline?:true|null; }> |undefined
Returns
InlineEditor<{ bold?: true | null; code?: true | null; italic?: true | null; link?: string | null; strike?: true | null; underline?: true | null; }> | undefined
inlineManager
Get Signature
get inlineManager():
InlineManager<AffineTextAttributes>
Returns
InlineManager<AffineTextAttributes>
isCommentHighlighted
Get Signature
get isCommentHighlighted():
boolean
Returns
boolean
langs
Get Signature
get langs():
BundledLanguageInfo[]
Returns
BundledLanguageInfo[]
notificationService
Get Signature
get notificationService():
NotificationService|null
Returns
NotificationService | null
readonly
Get Signature
get readonly():
boolean
Returns
boolean
topContenteditableElement
Get Signature
get topContenteditableElement():
BlockComponent<BlockModel<object>,BlockService,string> |null
Returns
BlockComponent<BlockModel<object>, BlockService, string> | null
Overrides
CaptionedBlockComponent.topContenteditableElement
useCaptionEditor
Overrides
CaptionedBlockComponent.useCaptionEditor
useZeroWidth
Overrides
CaptionedBlockComponent.useZeroWidth
connectedCallback()
connectedCallback():
void
Returns
void
Overrides
CaptionedBlockComponent.connectedCallback
copyCode()
copyCode():
void
Returns
void
getUpdateComplete()
getUpdateComplete():
Promise<boolean>
Returns
Promise<boolean>
Overrides
CaptionedBlockComponent.getUpdateComplete
renderBlock()
renderBlock():
TemplateResult<1>
Returns
TemplateResult<1>
Overrides
CaptionedBlockComponent.renderBlock
setCollapsed()
setCollapsed(
collapsed):void
Parameters
collapsed
boolean
Returns
void
setPreviewState()
setPreviewState(
preview):void
Parameters
preview
boolean
Returns
void
setWrap()
setWrap(
wrap):void
Parameters
wrap
boolean
Returns
void
attributes
controllers
dev-mode
lifecycle
properties
rendering
styles
updates
AffineCodeToolbarWidget
Extends
Constructors
Other
moreGroups
protectedmoreGroups:MenuItemGroup<CodeBlockToolbarContext>[]
primaryGroups
protectedprimaryGroups:MenuItemGroup<CodeBlockToolbarContext>[]
addMoretems()
addMoretems(
items,index?,type?):AffineCodeToolbarWidget
Parameters
items
AdvancedMenuItem<CodeBlockToolbarContext>[]
index?
number
type?
string
Returns
addPrimaryItems()
addPrimaryItems(
items,index?):AffineCodeToolbarWidget
Parameters
items
AdvancedMenuItem<CodeBlockToolbarContext>[]
index?
number
Returns
attributes
controllers
dev-mode
lifecycle
properties
rendering
styles
updates
firstUpdated()
firstUpdated():
void
Invoked when the element is first updated. Implement to perform one time work on the element after update.
firstUpdated() {
this.renderRoot.getElementById('my-text-area').focus();
}Setting properties inside this method will trigger the element to update again after this update cycle completes.
Returns
void
Overrides
WidgetComponent.firstUpdated
Interfaces
CodeBlockConfig
Properties
langs?
optionallangs:BundledLanguageInfo[]
showLineNumbers?
optionalshowLineNumbers:boolean
Whether to show line numbers in the code block.
Default
truetheme?
optionaltheme:object
dark?
optionaldark:ThemeInput
light?
optionallight:ThemeInput
CodeLayout
Extends
Indexable
[key: string]: unknown
Properties
type
type:
"affine:code"
Overrides
Type Aliases
CodeBlockPreviewContext
CodeBlockPreviewContext =
object
Properties
lang
lang:
string
renderer
renderer:
CodeBlockPreviewRenderer
CodeBlockPreviewRenderer()
CodeBlockPreviewRenderer = (
model) =>HTMLTemplateResult|null
Parameters
model
Returns
HTMLTemplateResult | null
Variables
AFFINE_CODE_TOOLBAR_WIDGET
constAFFINE_CODE_TOOLBAR_WIDGET:"affine-code-toolbar-widget"='affine-code-toolbar-widget'
CODE_BLOCK_DEFAULT_DARK_THEME
constCODE_BLOCK_DEFAULT_DARK_THEME:Promise<__module>
CODE_BLOCK_DEFAULT_LIGHT_THEME
constCODE_BLOCK_DEFAULT_LIGHT_THEME:Promise<__module>
CodeBlockConfigExtension
constCodeBlockConfigExtension:ConfigFactory<CodeBlockConfig>
CodeBlockHtmlAdapterExtension
constCodeBlockHtmlAdapterExtension:ExtensionType&object
Type Declaration
identifier
identifier:
ServiceIdentifier<BlockHtmlAdapterMatcher>
codeBlockHtmlAdapterMatcher
constcodeBlockHtmlAdapterMatcher:BlockHtmlAdapterMatcher
CodeBlockMarkdownAdapterExtension
constCodeBlockMarkdownAdapterExtension:ExtensionType&object
Type Declaration
identifier
identifier:
ServiceIdentifier<BlockMarkdownAdapterMatcher>
CodeBlockMarkdownAdapterExtensions
constCodeBlockMarkdownAdapterExtensions:ExtensionType[]
codeBlockMarkdownAdapterMatcher
constcodeBlockMarkdownAdapterMatcher:BlockMarkdownAdapterMatcher
CodeBlockNotionHtmlAdapterExtension
constCodeBlockNotionHtmlAdapterExtension:ExtensionType&object
Type Declaration
identifier
identifier:
ServiceIdentifier<BlockNotionHtmlAdapterMatcher>
codeBlockNotionHtmlAdapterMatcher
constcodeBlockNotionHtmlAdapterMatcher:BlockNotionHtmlAdapterMatcher
CodeBlockPlainTextAdapterExtension
constCodeBlockPlainTextAdapterExtension:ExtensionType&object
Type Declaration
identifier
identifier:
ServiceIdentifier<BlockPlainTextAdapterMatcher>
codeBlockPlainTextAdapterMatcher
constcodeBlockPlainTextAdapterMatcher:BlockPlainTextAdapterMatcher
CodeBlockPreviewIdentifier
constCodeBlockPreviewIdentifier:ServiceIdentifier<CodeBlockPreviewContext> & <U>(variant) =>ServiceIdentifier<U>
CodeClipboardAdapterConfigIdentifier
constCodeClipboardAdapterConfigIdentifier:ServiceIdentifier<ClipboardAdapterConfig> & <U>(variant) =>ServiceIdentifier<U>
CodeLayoutPainterExtension
constCodeLayoutPainterExtension:ExtensionType
CodeMarkdownPreprocessorExtension
constCodeMarkdownPreprocessorExtension:ExtensionType&object
Type Declaration
identifier
identifier:
ServiceIdentifier<MarkdownAdapterPreprocessor>
Functions
CodeBlockPreviewExtension()
CodeBlockPreviewExtension(
lang,renderer):ExtensionType
Parameters
lang
string
renderer
Returns
CodeClipboardAdapterConfigExtension()
CodeClipboardAdapterConfigExtension(
config):ExtensionType
Parameters
config
Returns
getCodeClipboardExtensions()
getCodeClipboardExtensions():
ExtensionType[]
Returns
normalizeGetter()
normalizeGetter<
T>(p):Promise<T>
Type Parameters
T
T
Parameters
p
MaybeGetter<T>
Returns
Promise<T>