chore: merge blocksuite source code (#9213)

This commit is contained in:
Mirone
2024-12-20 15:38:06 +08:00
committed by GitHub
parent 2c9ef916f4
commit 30200ff86d
2031 changed files with 238888 additions and 229 deletions
@@ -0,0 +1,55 @@
import * as Y from 'yjs';
import { NATIVE_UNIQ_IDENTIFIER } from '../consts.js';
export type OnBoxedChange = (data: unknown) => void;
export class Boxed<T = unknown> {
static from = <T>(map: Y.Map<T>, onChange?: OnBoxedChange): Boxed<T> => {
return new Boxed<T>(map.get('value') as T, onChange);
};
static is = (value: unknown): value is Boxed => {
return (
value instanceof Y.Map && value.get('type') === NATIVE_UNIQ_IDENTIFIER
);
};
private readonly _map: Y.Map<T>;
private _onChange?: OnBoxedChange;
getValue = () => {
return this._map.get('value');
};
setValue = (value: T) => {
return this._map.set('value', value);
};
get yMap() {
return this._map;
}
constructor(value: T, onChange?: OnBoxedChange) {
this._onChange = onChange;
if (
value instanceof Y.Map &&
value.doc &&
value.get('type') === NATIVE_UNIQ_IDENTIFIER
) {
this._map = value;
} else {
this._map = new Y.Map();
this._map.set('type', NATIVE_UNIQ_IDENTIFIER as T);
this._map.set('value', value);
}
this._map.observeDeep(() => {
this._onChange?.(this.getValue());
});
}
bind(onChange: OnBoxedChange) {
this._onChange = onChange;
}
}
@@ -0,0 +1,4 @@
export * from './boxed.js';
export * from './proxy.js';
export * from './text.js';
export * from './utils.js';
@@ -0,0 +1,357 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import type { YArrayEvent, YMapEvent } from 'yjs';
import { Array as YArray, Map as YMap } from 'yjs';
import { Boxed, type OnBoxedChange } from './boxed.js';
import { type OnTextChange, Text } from './text.js';
import type { UnRecord } from './utils.js';
import { BaseReactiveYData, native2Y, y2Native } from './utils.js';
export type ProxyOptions<T> = {
onChange?: (data: T) => void;
};
const proxies = new WeakMap<any, BaseReactiveYData<any, any>>();
export class ReactiveYArray extends BaseReactiveYData<
unknown[],
YArray<unknown>
> {
private _observer = (event: YArrayEvent<unknown>) => {
this._onObserve(event, () => {
let retain = 0;
event.changes.delta.forEach(change => {
if (change.retain) {
retain += change.retain;
return;
}
if (change.delete) {
this._updateWithSkip(() => {
this._source.splice(retain, change.delete);
});
return;
}
if (change.insert) {
const _arr = [change.insert].flat();
const proxyList = _arr.map(value => createYProxy(value));
this._updateWithSkip(() => {
this._source.splice(retain, 0, ...proxyList);
});
retain += change.insert.length;
}
});
});
};
protected _getProxy = () => {
return new Proxy(this._source, {
has: (target, p) => {
return Reflect.has(target, p);
},
set: (target, p, value, receiver) => {
if (typeof p !== 'string') {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'key cannot be a symbol'
);
}
const index = Number(p);
if (this._skipNext || Number.isNaN(index)) {
return Reflect.set(target, p, value, receiver);
}
if (this._stashed.has(index)) {
const result = Reflect.set(target, p, value, receiver);
this._options.onChange?.(this._proxy);
return result;
}
const reactive = proxies.get(this._ySource);
if (!reactive) {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'YData is not subscribed before changes'
);
}
const doc = this._ySource.doc;
if (!doc) {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'YData is not bound to a Y.Doc'
);
}
const yData = native2Y(value);
this._transact(doc, () => {
if (index < this._ySource.length) {
this._ySource.delete(index, 1);
}
this._ySource.insert(index, [yData]);
});
const data = createYProxy(yData, this._options);
return Reflect.set(target, p, data, receiver);
},
get: (target, p, receiver) => {
return Reflect.get(target, p, receiver);
},
deleteProperty: (target, p): boolean => {
if (typeof p !== 'string') {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'key cannot be a symbol'
);
}
const proxied = proxies.get(this._ySource);
if (!proxied) {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'YData is not subscribed before changes'
);
}
const doc = this._ySource.doc;
if (!doc) {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'YData is not bound to a Y.Doc'
);
}
const index = Number(p);
if (this._skipNext || Number.isNaN(index)) {
return Reflect.deleteProperty(target, p);
}
this._transact(doc, () => {
this._ySource.delete(index, 1);
});
return Reflect.deleteProperty(target, p);
},
});
};
protected readonly _proxy: unknown[];
constructor(
protected readonly _source: unknown[],
protected readonly _ySource: YArray<unknown>,
protected readonly _options: ProxyOptions<unknown[]>
) {
super();
this._proxy = this._getProxy();
proxies.set(_ySource, this);
_ySource.observe(this._observer);
}
pop(prop: number) {
const value = this._source[prop];
this._stashed.delete(prop);
this._proxy[prop] = value;
}
stash(prop: number) {
this._stashed.add(prop);
}
}
export class ReactiveYMap extends BaseReactiveYData<UnRecord, YMap<unknown>> {
private _observer = (event: YMapEvent<unknown>) => {
this._onObserve(event, () => {
event.keysChanged.forEach(key => {
const type = event.changes.keys.get(key);
if (!type) {
return;
}
if (type.action === 'delete') {
this._updateWithSkip(() => {
delete this._source[key];
});
} else if (type.action === 'add' || type.action === 'update') {
const current = this._ySource.get(key);
this._updateWithSkip(() => {
this._source[key] = proxies.has(current)
? proxies.get(current)
: createYProxy(current, this._options);
});
}
});
});
};
protected _getProxy = () => {
return new Proxy(this._source, {
has: (target, p) => {
return Reflect.has(target, p);
},
set: (target, p, value, receiver) => {
if (typeof p !== 'string') {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'key cannot be a symbol'
);
}
if (this._skipNext) {
return Reflect.set(target, p, value, receiver);
}
if (this._stashed.has(p)) {
const result = Reflect.set(target, p, value, receiver);
this._options.onChange?.(this._proxy);
return result;
}
const reactive = proxies.get(this._ySource);
if (!reactive) {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'YData is not subscribed before changes'
);
}
const doc = this._ySource.doc;
if (!doc) {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'YData is not bound to a Y.Doc'
);
}
const yData = native2Y(value);
this._transact(doc, () => {
this._ySource.set(p, yData);
});
const data = createYProxy(yData, this._options);
return Reflect.set(target, p, data, receiver);
},
get: (target, p, receiver) => {
return Reflect.get(target, p, receiver);
},
deleteProperty: (target, p) => {
if (typeof p !== 'string') {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'key cannot be a symbol'
);
}
if (this._skipNext) {
return Reflect.deleteProperty(target, p);
}
const proxied = proxies.get(this._ySource);
if (!proxied) {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'YData is not subscribed before changes'
);
}
const doc = this._ySource.doc;
if (!doc) {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'YData is not bound to a Y.Doc'
);
}
this._transact(doc, () => {
this._ySource.delete(p);
});
return Reflect.deleteProperty(target, p);
},
});
};
protected readonly _proxy: UnRecord;
// eslint-disable-next-line sonarjs/no-identical-functions
constructor(
protected readonly _source: UnRecord,
protected readonly _ySource: YMap<unknown>,
protected readonly _options: ProxyOptions<UnRecord>
) {
super();
this._proxy = this._getProxy();
proxies.set(_ySource, this);
_ySource.observe(this._observer);
}
// eslint-disable-next-line sonarjs/no-identical-functions
pop(prop: string) {
const value = this._source[prop];
this._stashed.delete(prop);
this._proxy[prop] = value;
}
stash(prop: string) {
this._stashed.add(prop);
}
}
export function createYProxy<Data>(
yAbstract: unknown,
options: ProxyOptions<Data> = {}
): Data {
if (proxies.has(yAbstract)) {
return proxies.get(yAbstract)!.proxy as Data;
}
return y2Native(yAbstract, {
transform: (value, origin) => {
if (value instanceof Text) {
value.bind(options.onChange as OnTextChange);
return value;
}
if (Boxed.is(origin)) {
(value as Boxed).bind(options.onChange as OnBoxedChange);
return value;
}
if (origin instanceof YArray) {
const data = new ReactiveYArray(
value as unknown[],
origin,
options as ProxyOptions<unknown[]>
);
return data.proxy;
}
if (origin instanceof YMap) {
const data = new ReactiveYMap(
value as UnRecord,
origin,
options as ProxyOptions<UnRecord>
);
return data.proxy;
}
return value;
},
}) as Data;
}
export function stashProp(yMap: YMap<unknown>, prop: string): void;
export function stashProp(yMap: YArray<unknown>, prop: number): void;
export function stashProp(yAbstract: unknown, prop: string | number) {
const proxy = proxies.get(yAbstract);
if (!proxy) {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'YData is not subscribed before changes'
);
}
proxy.stash(prop);
}
export function popProp(yMap: YMap<unknown>, prop: string): void;
export function popProp(yMap: YArray<unknown>, prop: number): void;
export function popProp(yAbstract: unknown, prop: string | number) {
const proxy = proxies.get(yAbstract);
if (!proxy) {
throw new BlockSuiteError(
ErrorCode.ReactiveProxyError,
'YData is not subscribed before changes'
);
}
proxy.pop(prop);
}
@@ -0,0 +1,336 @@
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import type { BaseTextAttributes, DeltaInsert } from '@blocksuite/inline';
import { type Signal, signal } from '@preact/signals-core';
import * as Y from 'yjs';
export interface OptionalAttributes {
attributes?: Record<string, any>;
}
export type DeltaOperation = {
insert?: string;
delete?: number;
retain?: number;
} & OptionalAttributes;
export type OnTextChange = (data: Y.Text) => void;
export class Text {
private _deltas$: Signal<DeltaOperation[]>;
private _length$: Signal<number>;
private _onChange?: OnTextChange;
private readonly _yText: Y.Text;
get deltas$() {
return this._deltas$;
}
get length() {
return this._length$.value;
}
get yText() {
return this._yText;
}
constructor(
input?: Y.Text | string | DeltaInsert[],
onChange?: OnTextChange
) {
this._onChange = onChange;
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(() => {
this._length$.value = this._yText.length;
this._deltas$.value = this._yText.toDelta();
this._onChange?.(this._yText);
});
}
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);
}
applyDelta(delta: DeltaOperation[]) {
this._transact(() => {
this._yText?.applyDelta(delta);
});
}
bind(onChange?: OnTextChange) {
this._onChange = onChange;
}
clear() {
if (!this._yText.length) {
return;
}
this._transact(() => {
this._yText.delete(0, this._yText.length);
});
}
clone() {
return new Text(this._yText.clone(), this._onChange);
}
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(index: number, length: number, format: any) {
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: 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: ' +
length +
', text length: ' +
this._yText.length
);
}
this._transact(() => {
this._yText.insert(index, content, attributes);
});
}
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(
index: number,
length: number,
content: string,
attributes?: BaseTextAttributes
) {
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);
});
}
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;
// eslint-disable-next-line @typescript-eslint/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;
}
/**
* 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[] = [];
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, this._onChange);
return rightText;
}
toDelta(): DeltaOperation[] {
return this._yText?.toDelta() || [];
}
toString() {
return this._yText?.toString() || '';
}
}
@@ -0,0 +1,157 @@
import type { Doc as YDoc, YEvent } from 'yjs';
import { Array as YArray, Map as YMap, Text as YText, UndoManager } from 'yjs';
import { Boxed } from './boxed.js';
import type { ProxyOptions } from './proxy.js';
import { Text } from './text.js';
export type Native2Y<T> =
T extends Record<string, infer U>
? YMap<U>
: T extends Array<infer U>
? YArray<U>
: T;
export function isPureObject(value: unknown): value is object {
return (
value !== null &&
typeof value === 'object' &&
Object.prototype.toString.call(value) === '[object Object]' &&
[Object, undefined, null].some(x => x === value.constructor)
);
}
type TransformOptions = {
deep?: boolean;
transform?: (value: unknown, origin: unknown) => unknown;
};
export function native2Y<T>(
value: T,
{ deep = true, transform = x => x }: TransformOptions = {}
): Native2Y<T> {
if (value instanceof Boxed) {
return value.yMap as Native2Y<T>;
}
if (value instanceof Text) {
if (value.yText.doc) {
return value.yText.clone() as Native2Y<T>;
}
return value.yText as Native2Y<T>;
}
if (Array.isArray(value)) {
const yArray: YArray<unknown> = new YArray<unknown>();
const result = value.map(item => {
return deep ? native2Y(item, { deep, transform }) : item;
});
yArray.insert(0, result);
return yArray as Native2Y<T>;
}
if (isPureObject(value)) {
const yMap = new YMap<unknown>();
Object.entries(value).forEach(([key, value]) => {
yMap.set(key, deep ? native2Y(value, { deep, transform }) : value);
});
return yMap as Native2Y<T>;
}
return value as Native2Y<T>;
}
export function y2Native(
yAbstract: unknown,
{ deep = true, transform = x => x }: TransformOptions = {}
) {
if (Boxed.is(yAbstract)) {
const data = new Boxed(yAbstract);
return transform(data, yAbstract);
}
if (yAbstract instanceof YText) {
const data = new Text(yAbstract);
return transform(data, yAbstract);
}
if (yAbstract instanceof YArray) {
const data: unknown[] = yAbstract
.toArray()
.map(item => (deep ? y2Native(item, { deep, transform }) : item));
return transform(data, yAbstract);
}
if (yAbstract instanceof YMap) {
const data: Record<string, unknown> = Object.fromEntries(
Array.from(yAbstract.entries()).map(([key, value]) => {
return [key, deep ? y2Native(value, { deep, transform }) : value] as [
string,
unknown,
];
})
);
return transform(data, yAbstract);
}
return transform(yAbstract, yAbstract);
}
export type UnRecord = Record<string, unknown>;
export abstract class BaseReactiveYData<T, Y> {
protected _getOrigin = (
doc: YDoc
): {
doc: YDoc;
proxy: true;
target: BaseReactiveYData<any, any>;
} => {
return {
doc,
proxy: true,
target: this,
};
};
protected _onObserve = (event: YEvent<any>, handler: () => void) => {
if (
event.transaction.origin?.proxy !== true &&
(!event.transaction.local ||
event.transaction.origin instanceof UndoManager)
) {
handler();
}
this._options.onChange?.(this._proxy);
};
protected abstract readonly _options: ProxyOptions<T>;
protected abstract readonly _proxy: T;
protected _skipNext = false;
protected abstract readonly _source: T;
protected readonly _stashed = new Set<string | number>();
protected _transact = (doc: YDoc, fn: () => void) => {
doc.transact(fn, this._getOrigin(doc));
};
protected _updateWithSkip = (fn: () => void) => {
this._skipNext = true;
fn();
this._skipNext = false;
};
protected abstract readonly _ySource: Y;
get proxy() {
return this._proxy;
}
protected abstract _getProxy(): T;
abstract pop(prop: string | number): void;
abstract stash(prop: string | number): void;
}