blocksuite/docs/api/@blocksuite/store/classes/Text.md
BlockSuite API Documentation / @blocksuite/store / Text
Text is an abstraction of Y.Text. It provides useful methods to manipulate the text content.
const text = new Text('Hello, world!');
text.insert(' blocksuite', 7);
text.delete(7, 1);
text.format(7, 1, { bold: true });
text.join(new Text(' blocksuite'));
text.split(7, 1);
Text delta is a format from Y.js.
TextAttributes extends BaseTextAttributes = BaseTextAttributes
new Text<
TextAttributes>(input?):Text<TextAttributes>
The input can be a string, a Y.Text instance, or an array of DeltaInsert.
string | YText | DeltaInsert<TextAttributes>[]
Text<TextAttributes>
get deltas$():
Signal<DeltaOperation[]>
Get the text delta as a signal.
Signal<DeltaOperation[]>
applyDelta(
delta):void
Apply a delta to the text.
DeltaOperation[]
The delta to apply.
void
const text = new Text('Hello, world!');
text.applyDelta([{insert: ' blocksuite', attributes: { bold: true }}]);
clear():
void
Clear the text content.
void
clone():
Text<{bold?:true|null;code?:true|null;italic?:true|null;link?:string|null;strike?:true|null;underline?:true|null; }>
Clone the text to a new Text instance.
Text<{ bold?: true | null; code?: true | null; italic?: true | null; link?: string | null; strike?: true | null; underline?: true | null; }>
A new Text instance.
delete(
index,length):void
Delete the text content.
number
The index to delete.
number
The length to delete.
void
format(
index,length,format):void
Format the text content.
number
The index to format.
number
The length to format.
Record<string, unknown>
The format to apply.
void
const text = new Text('Hello, world!');
text.format(7, 1, { bold: true });
insert(
content,index,attributes?):void
Insert content at the specified index.
string
The content to insert.
number
The index to insert.
Record<string, unknown>
void
const text = new Text('Hello, world!');
text.insert(' blocksuite', 7);
join(
other):void
Join current text with another text.
Text
The other text to join.
void
const text = new Text('Hello, world!');
const other = new Text(' blocksuite');
text.join(other);
replace(
index,length,content,attributes?):void
Replace the text content with a new content.
number
The index to replace.
number
The length to replace.
string
The content to replace.
Record<string, unknown>
The attributes to replace.
void
const text = new Text('Hello, world!');
text.replace(7, 1, ' blocksuite');
sliceToDelta(
begin,end?):DeltaOperation[]
Slice the text to a delta.
number
The begin index.
number
The end index.
DeltaOperation[]
The delta of the sliced text.
split(
index,length):Text
Split the text into another Text.
number
The index to split.
number = 0
The length to split.
Text
The right part of the text.
const text = new Text('Hello, world!');
text.split(7, 1);
NOTE: The string included in [index, index + length) will be deleted.
Here are three cases for point position(index + length):
[{insert: 'abc', ...}, {insert: 'def', ...}, {insert: 'ghi', ...}]
1. abc|de|fghi
left: [{insert: 'abc', ...}]
right: [{insert: 'f', ...}, {insert: 'ghi', ...}]
2. abc|def|ghi
left: [{insert: 'abc', ...}]
right: [{insert: 'ghi', ...}]
3. abc|defg|hi
left: [{insert: 'abc', ...}]
right: [{insert: 'hi', ...}]
toDelta():
DeltaOperation[]
Get the text delta.
DeltaOperation[]
The delta of the text.
toString():
string
Get the text content as a string. In most cases, you should not use this method. It will lose the delta attributes information.
string
The text content.