Files
AFFiNE-Mirror/blocksuite/framework/store/src/reactive/base-reactive-data.ts
T
Saul-Mirone 1858947e0c feat(editor): flat block data (#9854)
Flat block data.

A new block type to flatten the block data

```typescript
// For developers
type Model = {
  blocks: Record<string, {
    flavour: string;
    cells: Record<string, {
      rowId: string;
      colId: string;
      text: Text;
    }>;
    cols: Record<string, {
      align: string;
    }>
    rows: Record<string, {
      backgroundColor: string;
    }>
  }>
}

// How it's saved in yjs
const yData = {
  blocks: {
    'blockId1': {
      flavour: 'affine:table',
      'prop:rows:row1:backgroundColor': 'white',
      'prop:cols:col1:align': 'left',
      'prop:cells:cell1:rowId': 'row1',
      'prop:cells:cell1:colId': 'col1',
      'prop:cells:cell1:text': YText,
      prop:children: []
    },
  }
}
```
2025-01-25 12:57:21 +00:00

66 lines
1.4 KiB
TypeScript

import type { Doc as YDoc, YEvent } from 'yjs';
import { UndoManager } from 'yjs';
import type { ProxyOptions } from './types';
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) => {
if (this._skipNext) {
return;
}
this._skipNext = true;
fn();
this._skipNext = false;
};
protected abstract readonly _ySource: Y;
get proxy() {
return this._proxy;
}
abstract pop(prop: string | number): void;
abstract stash(prop: string | number): void;
}