feat(editor): support linked-doc in rich-text column (#9634)

close: BS-2345
This commit is contained in:
zzj3720
2025-01-10 14:43:40 +00:00
parent cc08094b17
commit c016f8e37e
6 changed files with 488 additions and 152 deletions
@@ -1,6 +1,10 @@
import type { AffineTextAttributes } from '@blocksuite/affine-shared/types';
import { propertyType, t } from '@blocksuite/data-view';
import type { DeltaInsert } from '@blocksuite/inline';
import { Text } from '@blocksuite/store';
import { HostContextKey } from '../../context/host-context.js';
import { isLinkedDoc } from '../../utils/title-doc.js';
import { type RichTextCellType, toYText } from '../utils.js';
export const richTextColumnType = propertyType('rich-text');
@@ -16,7 +20,25 @@ export const richTextColumnModelConfig =
value: new Text(value),
};
},
cellToJson: ({ value }) => value?.toString() ?? null,
cellToJson: ({ value, dataSource }) => {
const host = dataSource.contextGet(HostContextKey);
if (host) {
const collection = host.std.workspace;
const yText = toYText(value);
const deltas = yText.toDelta();
const text = deltas
.map((delta: DeltaInsert<AffineTextAttributes>) => {
if (isLinkedDoc(delta)) {
const linkedDocId = delta.attributes?.reference?.pageId as string;
return collection.getDoc(linkedDocId)?.meta?.title;
}
return delta.insert;
})
.join('');
return text;
}
return value?.toString() ?? null;
},
cellFromJson: ({ value }) =>
typeof value !== 'string' ? undefined : new Text(value),
onUpdate: ({ value, callback }) => {