snapshot of https://github.com/toeverything/AFFiNE.git (canary, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Build & Test / Analyze (javascript, affine) (push) Failing after 4s
Build & Test / Analyze (typescript, affine) (push) Failing after 3s
Build & Test / Rust test filter (push) Failing after 4s
Build & Test / Check yarn binary (push) Failing after 3s
Build & Test / E2E BlockSuite Test (3) (push) Failing after 3s
Build & Test / Analyze (javascript, blocksuite) (push) Failing after 3s
Build & Test / Analyze (typescript, blocksuite) (push) Failing after 3s
Build & Test / Mobile native build filter (push) Failing after 3s
Build & Test / E2E BlockSuite Test (1) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (2) (push) Failing after 4s
Build & Test / E2E Mobile Test (1) (push) Failing after 3s
Build & Test / Unit Test (1) (push) Has been skipped
Build & Test / Unit Test (2) (push) Has been skipped
Build & Test / Unit Test (3) (push) Has been skipped
Build & Test / Unit Test (4) (push) Has been skipped
Build & Test / Unit Test (5) (push) Has been skipped
Build & Test / Native Unit Test (push) Has been skipped
Build & Test / E2E BlockSuite Test (4) (push) Failing after 3s
Build & Test / E2E BlockSuite Test (5) (push) Failing after 3s
Build & Test / E2E BlockSuite Cross Browser Test (push) Failing after 3s
Build & Test / E2E Mobile Test (2) (push) Failing after 3s
Build & Test / Build AFFiNE native (x86_64-unknown-linux-gnu) (push) Failing after 3s
Build & Test / Build Server native (push) Failing after 3s
Build & Test / Check Git Status (push) Has been skipped
Build & Test / Server Test (0, 4) (push) Has been skipped
Build & Test / Server Test (1, 4) (push) Has been skipped
Build & Test / Server Test (2, 4) (push) Has been skipped
Build & Test / Server Test (3, 4) (push) Has been skipped
Build & Test / Server Test with Elasticsearch (push) Has been skipped
Build & Test / Build @affine/electron renderer (push) Failing after 3s
Build & Test / Copilot test filter (push) Failing after 4s
Build & Test / Lint Rust (push) Has been skipped
Build & Test / Server Test with Embedded Search (push) Has been skipped
Build & Test / Server Test with Manticore Search (push) Has been skipped
Build & Test / Server E2E Test (push) Has been skipped
Build & Test / Cloud Desktop E2E Test (push) Has been skipped
Build & Test / Cloud E2E Test 1/2 (push) Has been skipped
Build & Test / Cloud E2E Test 2/2 (push) Has been skipped
Build & Test / Build Android app (push) Has been skipped
Build & Test / Test Android app (push) Has been skipped
Build & Test / Run native tests (push) Has been skipped
Build & Test / Server Copilot Api Test (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (1, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (2, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (3, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (4, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (5, 5) (push) Has been skipped
Release / Canary Gate (push) Has been skipped
Build & Test / Build AFFiNE native (aarch64-apple-darwin) (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-apple-darwin) (push) Has been cancelled
Build & Test / Desktop Test (ubuntu-latest, linux, x64, x86_64-unknown-linux-gnu, true) (push) Has been cancelled
Build & Test / Desktop Test (windows-latest, windows, x64, x86_64-pc-windows-msvc, true) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, arm64, aarch64-apple-darwin, true) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, x64, x86_64-apple-darwin, false) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled
Build & Test / Build iOS app (push) Has been cancelled
Copilot Test Automatically / Setup Test (push) Failing after 1s
Release / Prepare (push) Failing after 2s
Release / Release Cloud (push) Has been skipped
Release / Release Desktop (push) Has been skipped
Release / Release Docker Image (push) Has been skipped
Release / Release Mobile (push) Has been skipped
Some checks failed
Build & Test / Analyze (javascript, affine) (push) Failing after 4s
Build & Test / Analyze (typescript, affine) (push) Failing after 3s
Build & Test / Rust test filter (push) Failing after 4s
Build & Test / Check yarn binary (push) Failing after 3s
Build & Test / E2E BlockSuite Test (3) (push) Failing after 3s
Build & Test / Analyze (javascript, blocksuite) (push) Failing after 3s
Build & Test / Analyze (typescript, blocksuite) (push) Failing after 3s
Build & Test / Mobile native build filter (push) Failing after 3s
Build & Test / E2E BlockSuite Test (1) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (2) (push) Failing after 4s
Build & Test / E2E Mobile Test (1) (push) Failing after 3s
Build & Test / Unit Test (1) (push) Has been skipped
Build & Test / Unit Test (2) (push) Has been skipped
Build & Test / Unit Test (3) (push) Has been skipped
Build & Test / Unit Test (4) (push) Has been skipped
Build & Test / Unit Test (5) (push) Has been skipped
Build & Test / Native Unit Test (push) Has been skipped
Build & Test / E2E BlockSuite Test (4) (push) Failing after 3s
Build & Test / E2E BlockSuite Test (5) (push) Failing after 3s
Build & Test / E2E BlockSuite Cross Browser Test (push) Failing after 3s
Build & Test / E2E Mobile Test (2) (push) Failing after 3s
Build & Test / Build AFFiNE native (x86_64-unknown-linux-gnu) (push) Failing after 3s
Build & Test / Build Server native (push) Failing after 3s
Build & Test / Check Git Status (push) Has been skipped
Build & Test / Server Test (0, 4) (push) Has been skipped
Build & Test / Server Test (1, 4) (push) Has been skipped
Build & Test / Server Test (2, 4) (push) Has been skipped
Build & Test / Server Test (3, 4) (push) Has been skipped
Build & Test / Server Test with Elasticsearch (push) Has been skipped
Build & Test / Build @affine/electron renderer (push) Failing after 3s
Build & Test / Copilot test filter (push) Failing after 4s
Build & Test / Lint Rust (push) Has been skipped
Build & Test / Server Test with Embedded Search (push) Has been skipped
Build & Test / Server Test with Manticore Search (push) Has been skipped
Build & Test / Server E2E Test (push) Has been skipped
Build & Test / Cloud Desktop E2E Test (push) Has been skipped
Build & Test / Cloud E2E Test 1/2 (push) Has been skipped
Build & Test / Cloud E2E Test 2/2 (push) Has been skipped
Build & Test / Build Android app (push) Has been skipped
Build & Test / Test Android app (push) Has been skipped
Build & Test / Run native tests (push) Has been skipped
Build & Test / Server Copilot Api Test (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (1, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (2, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (3, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (4, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (5, 5) (push) Has been skipped
Release / Canary Gate (push) Has been skipped
Build & Test / Build AFFiNE native (aarch64-apple-darwin) (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-apple-darwin) (push) Has been cancelled
Build & Test / Desktop Test (ubuntu-latest, linux, x64, x86_64-unknown-linux-gnu, true) (push) Has been cancelled
Build & Test / Desktop Test (windows-latest, windows, x64, x86_64-pc-windows-msvc, true) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, arm64, aarch64-apple-darwin, true) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, x64, x86_64-apple-darwin, false) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled
Build & Test / Build iOS app (push) Has been cancelled
Copilot Test Automatically / Setup Test (push) Failing after 1s
Release / Prepare (push) Failing after 2s
Release / Release Cloud (push) Has been skipped
Release / Release Desktop (push) Has been skipped
Release / Release Docker Image (push) Has been skipped
Release / Release Mobile (push) Has been skipped
This commit is contained in:
12
blocksuite/framework/store/src/reactive/text/attributes.ts
Normal file
12
blocksuite/framework/store/src/reactive/text/attributes.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const baseTextAttributes = z.object({
|
||||
bold: z.literal(true).optional().nullable().catch(undefined),
|
||||
italic: z.literal(true).optional().nullable().catch(undefined),
|
||||
underline: z.literal(true).optional().nullable().catch(undefined),
|
||||
strike: z.literal(true).optional().nullable().catch(undefined),
|
||||
code: z.literal(true).optional().nullable().catch(undefined),
|
||||
link: z.string().optional().nullable().catch(undefined),
|
||||
});
|
||||
|
||||
export type BaseTextAttributes = z.infer<typeof baseTextAttributes>;
|
||||
3
blocksuite/framework/store/src/reactive/text/index.ts
Normal file
3
blocksuite/framework/store/src/reactive/text/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './attributes';
|
||||
export * from './text';
|
||||
export * from './types';
|
||||
472
blocksuite/framework/store/src/reactive/text/text.ts
Normal file
472
blocksuite/framework/store/src/reactive/text/text.ts
Normal file
@@ -0,0 +1,472 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import { type Signal, signal } from '@preact/signals-core';
|
||||
import * as Y from 'yjs';
|
||||
|
||||
import type { BaseTextAttributes } from './attributes';
|
||||
import type { DeltaInsert, DeltaOperation, OnTextChange } from './types';
|
||||
|
||||
/**
|
||||
* Text is an abstraction of Y.Text.
|
||||
* It provides useful methods to manipulate the text content.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* 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 {@link https://docs.yjs.dev/api/delta-format delta} is a format from Y.js.
|
||||
*
|
||||
* @category Reactive
|
||||
*/
|
||||
export class Text<
|
||||
TextAttributes extends BaseTextAttributes = BaseTextAttributes,
|
||||
> {
|
||||
private readonly _deltas$: Signal<DeltaOperation[]>;
|
||||
|
||||
private readonly _length$: Signal<number>;
|
||||
|
||||
private _onChange?: OnTextChange;
|
||||
|
||||
private readonly _yText: Y.Text;
|
||||
|
||||
/**
|
||||
* Get the text delta as a signal.
|
||||
*/
|
||||
get deltas$() {
|
||||
return this._deltas$;
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this._length$.value;
|
||||
}
|
||||
|
||||
get yText() {
|
||||
return this._yText;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param input - The input can be a string, a Y.Text instance, or an array of DeltaInsert.
|
||||
*/
|
||||
constructor(input?: Y.Text | string | DeltaInsert<TextAttributes>[]) {
|
||||
let length = 0;
|
||||
if (typeof input === 'string') {
|
||||
const text = input.replaceAll('\r\n', '\n');
|
||||
length = text.length;
|
||||
this._yText = new Y.Text(text);
|
||||
} else if (input instanceof Y.Text) {
|
||||
this._yText = input;
|
||||
if (input.doc) {
|
||||
length = input.length;
|
||||
}
|
||||
} else if (input instanceof Array) {
|
||||
for (const delta of input) {
|
||||
if (delta.insert) {
|
||||
delta.insert = delta.insert.replaceAll('\r\n', '\n');
|
||||
length += delta.insert.length;
|
||||
}
|
||||
}
|
||||
const yText = new Y.Text();
|
||||
yText.applyDelta(input);
|
||||
this._yText = yText;
|
||||
} else {
|
||||
this._yText = new Y.Text();
|
||||
}
|
||||
|
||||
this._length$ = signal(length);
|
||||
this._deltas$ = signal(this._yText.doc ? this._yText.toDelta() : []);
|
||||
this._yText.observe(event => {
|
||||
const isLocal =
|
||||
!event.transaction.origin ||
|
||||
!this._yText.doc ||
|
||||
event.transaction.origin instanceof Y.UndoManager ||
|
||||
event.transaction.origin.proxy
|
||||
? true
|
||||
: event.transaction.origin === this._yText.doc.clientID;
|
||||
this._length$.value = this._yText.length;
|
||||
this._deltas$.value = this._yText.toDelta();
|
||||
this._onChange?.(this._yText, isLocal);
|
||||
});
|
||||
}
|
||||
|
||||
private _transact(callback: () => void) {
|
||||
const doc = this._yText.doc;
|
||||
if (!doc) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ReactiveProxyError,
|
||||
'Failed to transact text! yText is not attached to a doc'
|
||||
);
|
||||
}
|
||||
doc.transact(() => {
|
||||
callback();
|
||||
}, doc.clientID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a delta to the text.
|
||||
*
|
||||
* @param delta - The delta to apply.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const text = new Text('Hello, world!');
|
||||
* text.applyDelta([{insert: ' blocksuite', attributes: { bold: true }}]);
|
||||
* ```
|
||||
*/
|
||||
applyDelta(delta: DeltaOperation[]) {
|
||||
this._transact(() => {
|
||||
this._yText?.applyDelta(delta);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
bind(onChange?: OnTextChange) {
|
||||
this._onChange = onChange;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the text content.
|
||||
*/
|
||||
clear() {
|
||||
if (!this._yText.length) {
|
||||
return;
|
||||
}
|
||||
this._transact(() => {
|
||||
this._yText.delete(0, this._yText.length);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone the text to a new Text instance.
|
||||
*
|
||||
* @returns A new Text instance.
|
||||
*/
|
||||
clone() {
|
||||
const text = new Text(this._yText.clone());
|
||||
text.bind(this._onChange);
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the text content.
|
||||
*
|
||||
* @param index - The index to delete.
|
||||
* @param length - The length to delete.
|
||||
*/
|
||||
delete(index: number, length: number) {
|
||||
if (length === 0) {
|
||||
return;
|
||||
}
|
||||
if (index < 0 || length < 0 || index + length > this._yText.length) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ReactiveProxyError,
|
||||
'Failed to delete text! Index or length out of range, index: ' +
|
||||
index +
|
||||
', length: ' +
|
||||
length +
|
||||
', text length: ' +
|
||||
this._yText.length
|
||||
);
|
||||
}
|
||||
this._transact(() => {
|
||||
this._yText.delete(index, length);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the text content.
|
||||
*
|
||||
* @param index - The index to format.
|
||||
* @param length - The length to format.
|
||||
* @param format - The format to apply.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const text = new Text('Hello, world!');
|
||||
* text.format(7, 1, { bold: true });
|
||||
* ```
|
||||
*/
|
||||
format(index: number, length: number, format: Record<string, unknown>) {
|
||||
if (length === 0) {
|
||||
return;
|
||||
}
|
||||
if (index < 0 || length < 0 || index + length > this._yText.length) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ReactiveProxyError,
|
||||
'Failed to format text! Index or length out of range, index: ' +
|
||||
index +
|
||||
', length: ' +
|
||||
length +
|
||||
', text length: ' +
|
||||
this._yText.length
|
||||
);
|
||||
}
|
||||
this._transact(() => {
|
||||
this._yText.format(index, length, format);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert content at the specified index.
|
||||
*
|
||||
* @param content - The content to insert.
|
||||
* @param index - The index to insert.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const text = new Text('Hello, world!');
|
||||
* text.insert(' blocksuite', 7);
|
||||
* ```
|
||||
*/
|
||||
insert(content: string, index: number, attributes?: Record<string, unknown>) {
|
||||
if (!content.length) {
|
||||
return;
|
||||
}
|
||||
if (index < 0 || index > this._yText.length) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ReactiveProxyError,
|
||||
'Failed to insert text! Index or length out of range, index: ' +
|
||||
index +
|
||||
', length: ' +
|
||||
content.length +
|
||||
', text length: ' +
|
||||
this._yText.length
|
||||
);
|
||||
}
|
||||
this._transact(() => {
|
||||
this._yText.insert(index, content, attributes);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Join current text with another text.
|
||||
*
|
||||
* @param other - The other text to join.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const text = new Text('Hello, world!');
|
||||
* const other = new Text(' blocksuite');
|
||||
* text.join(other);
|
||||
* ```
|
||||
*/
|
||||
join(other: Text) {
|
||||
if (!other || !other.toDelta().length) {
|
||||
return;
|
||||
}
|
||||
this._transact(() => {
|
||||
const yOther = other._yText;
|
||||
const delta: DeltaOperation[] = yOther.toDelta();
|
||||
delta.unshift({ retain: this._yText.length });
|
||||
this._yText.applyDelta(delta);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the text content with a new content.
|
||||
*
|
||||
* @param index - The index to replace.
|
||||
* @param length - The length to replace.
|
||||
* @param content - The content to replace.
|
||||
* @param attributes - The attributes to replace.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* const text = new Text('Hello, world!');
|
||||
* text.replace(7, 1, ' blocksuite');
|
||||
* ```
|
||||
*/
|
||||
replace(
|
||||
index: number,
|
||||
length: number,
|
||||
content: string,
|
||||
attributes?: Record<string, unknown>
|
||||
) {
|
||||
if (index < 0 || length < 0 || index + length > this._yText.length) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ReactiveProxyError,
|
||||
'Failed to replace text! The length of the text is' +
|
||||
this._yText.length +
|
||||
', but you are trying to replace from' +
|
||||
index +
|
||||
'to' +
|
||||
index +
|
||||
length
|
||||
);
|
||||
}
|
||||
this._transact(() => {
|
||||
this._yText.delete(index, length);
|
||||
this._yText.insert(index, content, attributes);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Slice the text to a delta.
|
||||
*
|
||||
* @param begin - The begin index.
|
||||
* @param end - The end index.
|
||||
*
|
||||
* @returns The delta of the sliced text.
|
||||
*/
|
||||
sliceToDelta(begin: number, end?: number): DeltaOperation[] {
|
||||
const result: DeltaOperation[] = [];
|
||||
if (end && begin >= end) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (begin === 0 && end === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const delta = this.toDelta();
|
||||
if (begin < 1 && !end) {
|
||||
return delta;
|
||||
}
|
||||
|
||||
if (delta && delta instanceof Array) {
|
||||
let charNum = 0;
|
||||
// oxlint-disable-next-line typescript/prefer-for-of
|
||||
for (let i = 0; i < delta.length; i++) {
|
||||
const content = delta[i];
|
||||
let contentText: string = content.insert || '';
|
||||
const contentLen = contentText.length;
|
||||
|
||||
const isLastOp = end && charNum + contentLen > end;
|
||||
const isFirstOp = charNum + contentLen > begin && result.length === 0;
|
||||
if (isFirstOp && isLastOp) {
|
||||
contentText = contentText.slice(begin - charNum, end - charNum);
|
||||
result.push({
|
||||
...content,
|
||||
insert: contentText,
|
||||
});
|
||||
break;
|
||||
} else if (isFirstOp || isLastOp) {
|
||||
contentText = isLastOp
|
||||
? contentText.slice(0, end - charNum)
|
||||
: contentText.slice(begin - charNum);
|
||||
|
||||
result.push({
|
||||
...content,
|
||||
insert: contentText,
|
||||
});
|
||||
} else {
|
||||
result.length > 0 && result.push(content);
|
||||
}
|
||||
|
||||
if (end && charNum + contentLen > end) {
|
||||
break;
|
||||
}
|
||||
|
||||
charNum = charNum + contentLen;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Split the text into another Text.
|
||||
*
|
||||
* @param index - The index to split.
|
||||
* @param length - The length to split.
|
||||
*
|
||||
* @returns The right part of the text.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* 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', ...}]
|
||||
* ```
|
||||
*/
|
||||
split(index: number, length = 0): Text {
|
||||
if (index < 0 || length < 0 || index + length > this._yText.length) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ReactiveProxyError,
|
||||
'Failed to split text! Index or length out of range, index: ' +
|
||||
index +
|
||||
', length: ' +
|
||||
length +
|
||||
', text length: ' +
|
||||
this._yText.length
|
||||
);
|
||||
}
|
||||
const deltas = this._yText.toDelta();
|
||||
if (!(deltas instanceof Array)) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ReactiveProxyError,
|
||||
'This text cannot be split because we failed to get the deltas of it.'
|
||||
);
|
||||
}
|
||||
let tmpIndex = 0;
|
||||
const rightDeltas: DeltaInsert<TextAttributes>[] = [];
|
||||
for (let i = 0; i < deltas.length; i++) {
|
||||
const insert = deltas[i].insert;
|
||||
if (typeof insert === 'string') {
|
||||
if (tmpIndex + insert.length >= index + length) {
|
||||
const insertRight = insert.slice(index + length - tmpIndex);
|
||||
rightDeltas.push({
|
||||
insert: insertRight,
|
||||
attributes: deltas[i].attributes,
|
||||
});
|
||||
rightDeltas.push(...deltas.slice(i + 1));
|
||||
break;
|
||||
}
|
||||
tmpIndex += insert.length;
|
||||
} else {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ReactiveProxyError,
|
||||
'This text cannot be split because it contains non-string insert.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
this.delete(index, this.length - index);
|
||||
const rightYText = new Y.Text();
|
||||
rightYText.applyDelta(rightDeltas);
|
||||
const rightText = new Text(rightYText);
|
||||
rightText.bind(this._onChange);
|
||||
|
||||
return rightText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the text delta.
|
||||
*
|
||||
* @returns The delta of the text.
|
||||
*/
|
||||
toDelta(): DeltaOperation[] {
|
||||
return this._yText?.toDelta() || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the text content as a string.
|
||||
* In most cases, you should not use this method. It will lose the delta attributes information.
|
||||
*
|
||||
* @returns The text content.
|
||||
*/
|
||||
toString() {
|
||||
return this._yText?.toString() || '';
|
||||
}
|
||||
}
|
||||
22
blocksuite/framework/store/src/reactive/text/types.ts
Normal file
22
blocksuite/framework/store/src/reactive/text/types.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import type * as Y from 'yjs';
|
||||
|
||||
import type { BaseTextAttributes } from './attributes';
|
||||
|
||||
export interface OptionalAttributes {
|
||||
attributes?: Record<string, any>;
|
||||
}
|
||||
|
||||
export type DeltaOperation = {
|
||||
insert?: string;
|
||||
delete?: number;
|
||||
retain?: number;
|
||||
} & OptionalAttributes;
|
||||
|
||||
export type OnTextChange = (data: Y.Text, isLocal: boolean) => void;
|
||||
|
||||
export type DeltaInsert<
|
||||
TextAttributes extends BaseTextAttributes = BaseTextAttributes,
|
||||
> = {
|
||||
insert: string;
|
||||
attributes?: TextAttributes;
|
||||
};
|
||||
Reference in New Issue
Block a user