refactor(editor): improve element adapters (#11473)

This commit is contained in:
Saul-Mirone
2025-04-05 09:40:13 +00:00
parent 0fbca31c27
commit aed7f40568
45 changed files with 292 additions and 248 deletions
@@ -0,0 +1,35 @@
import { ElementToPlainTextAdapterExtension } from '@blocksuite/affine-block-surface';
import type { MindMapTreeNode } from '@blocksuite/affine-model';
import { getShapeText, getShapeType } from '../utils';
export const shapeToPlainTextAdapterMatcher =
ElementToPlainTextAdapterExtension({
name: 'shape',
match: elementModel => elementModel.type === 'shape',
toAST: (elementModel, context) => {
let content = '';
const { walkerContext } = context;
const mindMapNodeMaps = walkerContext.getGlobalContext(
'surface:mindMap:nodeMapArray'
) as Array<Map<string, MindMapTreeNode>>;
if (mindMapNodeMaps && mindMapNodeMaps.length > 0) {
// Check if the elementModel is a mindMap node
// If it is, we should return { content: '' } directly
// And get the content when we handle the whole mindMap
const isMindMapNode = mindMapNodeMaps.some(nodeMap =>
nodeMap.has(elementModel.id as string)
);
if (isMindMapNode) {
return { content };
}
}
// If it is not, we should return the text and shapeType
const text = getShapeText(elementModel);
const type = getShapeType(elementModel);
const shapeType = type.charAt(0).toUpperCase() + type.slice(1);
content = `${shapeType}, with text label "${text}"`;
return { content };
},
});