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

View File

@@ -0,0 +1,2 @@
export * from './markdown';
export * from './plain-text';

View File

@@ -0,0 +1,69 @@
import { ElementToMarkdownAdapterExtension } from '@blocksuite/affine-block-surface';
import { getShapeText } from '@blocksuite/affine-gfx-shape';
import {
buildMindMapTree,
type MindMapTreeNode,
} from '@blocksuite/affine-model';
export const mindmapToMarkdownAdapterMatcher =
ElementToMarkdownAdapterExtension({
name: 'mindmap',
match: elementModel => elementModel.type === 'mindmap',
toAST: (elementModel, context) => {
if (elementModel.type !== 'mindmap') {
return null;
}
const mindMapTree = buildMindMapTree(elementModel);
if (!mindMapTree) {
return null;
}
const { walkerContext, elements } = context;
const traverseMindMapTree = (node: MindMapTreeNode) => {
const shapeElement = elements[node.id as string];
const shapeText = getShapeText(shapeElement);
walkerContext
.openNode(
{
type: 'listItem',
spread: false,
children: [],
},
'children'
)
.openNode(
{
type: 'paragraph',
children: [
{
type: 'text',
value: shapeText,
},
],
},
'children'
)
.closeNode();
node.children.forEach(child => {
traverseMindMapTree(child);
walkerContext.closeNode();
});
};
// First create a list node for the mind map tree
walkerContext.openNode(
{
type: 'list',
ordered: false,
spread: false,
children: [],
},
'children'
);
traverseMindMapTree(mindMapTree);
walkerContext.closeNode().closeNode();
return null;
},
});

View File

@@ -0,0 +1,13 @@
import { ElementToPlainTextAdapterExtension } from '@blocksuite/affine-block-surface';
import { getMindMapTreeText } from '../utils';
export const mindmapToPlainTextAdapterMatcher =
ElementToPlainTextAdapterExtension({
name: 'mindmap',
match: elementModel => elementModel.type === 'mindmap',
toAST: (elementModel, context) => {
const mindMapContent = getMindMapTreeText(elementModel, context.elements);
return { content: mindMapContent };
},
});

View File

@@ -0,0 +1,65 @@
import { getShapeText } from '@blocksuite/affine-gfx-shape';
import {
buildMindMapTree,
type MindMapTreeNode,
} from '@blocksuite/affine-model';
/**
* traverse the mindMapTree and construct the content string
* like:
* - Root
* - Child 1
* - Child 1.1
* - Child 1.2
* - Child 2
* - Child 2.1
* - Child 2.2
* - Child 3
* - Child 3.1
* - Child 3.2
* @param elementModel - the mindmap element model
* @param elements - the elements map
* @returns the mindmap tree text
*/
export function getMindMapTreeText(
elementModel: Record<string, unknown>,
elements: Record<string, Record<string, unknown>>,
options: {
prefix: string;
repeat: number;
} = {
prefix: ' ',
repeat: 2,
}
): string {
let mindMapContent = '';
if (elementModel.type !== 'mindmap') {
return mindMapContent;
}
const mindMapTree = buildMindMapTree(elementModel);
if (!mindMapTree) {
return mindMapContent;
}
let layer = 0;
const traverseMindMapTree = (
node: MindMapTreeNode,
prefix: string,
repeat: number
) => {
const shapeElement = elements[node.id as string];
const shapeText = getShapeText(shapeElement);
if (shapeElement) {
mindMapContent += `${prefix.repeat(layer * repeat)}- ${shapeText}\n`;
}
node.children.forEach(child => {
layer++;
traverseMindMapTree(child, prefix, repeat);
layer--;
});
};
traverseMindMapTree(mindMapTree, options.prefix, options.repeat);
return mindMapContent;
}

View File

@@ -1,3 +1,4 @@
export * from './adapter';
export * from './element-renderer';
export * from './element-transform';
export * from './indicator-overlay';