mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-27 02:42:25 +08:00
refactor(editor): improve element adapters (#11473)
This commit is contained in:
3
blocksuite/affine/gfx/shape/src/adapter/index.ts
Normal file
3
blocksuite/affine/gfx/shape/src/adapter/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './markdown';
|
||||
export * from './plain-text';
|
||||
export * from './utils';
|
||||
46
blocksuite/affine/gfx/shape/src/adapter/markdown/index.ts
Normal file
46
blocksuite/affine/gfx/shape/src/adapter/markdown/index.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { ElementToMarkdownAdapterExtension } from '@blocksuite/affine-block-surface';
|
||||
import type { MindMapTreeNode } from '@blocksuite/affine-model';
|
||||
|
||||
import { getShapeText, getShapeType } from '../utils';
|
||||
|
||||
export const shapeToMarkdownAdapterMatcher = ElementToMarkdownAdapterExtension({
|
||||
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 null;
|
||||
}
|
||||
}
|
||||
|
||||
// If it is not, we should return the text and shapeType
|
||||
const text = getShapeText(elementModel);
|
||||
const type = getShapeType(elementModel);
|
||||
if (!text && !type) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const shapeType = type.charAt(0).toUpperCase() + type.slice(1);
|
||||
content = `${shapeType}, with text label "${text}"`;
|
||||
return {
|
||||
type: 'paragraph',
|
||||
children: [
|
||||
{
|
||||
type: 'text',
|
||||
value: content,
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
});
|
||||
35
blocksuite/affine/gfx/shape/src/adapter/plain-text/index.ts
Normal file
35
blocksuite/affine/gfx/shape/src/adapter/plain-text/index.ts
Normal file
@@ -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 };
|
||||
},
|
||||
});
|
||||
36
blocksuite/affine/gfx/shape/src/adapter/utils.ts
Normal file
36
blocksuite/affine/gfx/shape/src/adapter/utils.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import type { DeltaInsert } from '@blocksuite/store';
|
||||
|
||||
export function getShapeType(elementModel: Record<string, unknown>): string {
|
||||
let shapeType = '';
|
||||
if (elementModel.type !== 'shape') {
|
||||
return shapeType;
|
||||
}
|
||||
|
||||
if (
|
||||
'shapeType' in elementModel &&
|
||||
typeof elementModel.shapeType === 'string'
|
||||
) {
|
||||
shapeType = elementModel.shapeType;
|
||||
}
|
||||
return shapeType;
|
||||
}
|
||||
|
||||
export function getShapeText(elementModel: Record<string, unknown>): string {
|
||||
let text = '';
|
||||
if (elementModel.type !== 'shape') {
|
||||
return text;
|
||||
}
|
||||
|
||||
if (
|
||||
'text' in elementModel &&
|
||||
typeof elementModel.text === 'object' &&
|
||||
elementModel.text
|
||||
) {
|
||||
let delta: DeltaInsert[] = [];
|
||||
if ('delta' in elementModel.text) {
|
||||
delta = elementModel.text.delta as DeltaInsert[];
|
||||
}
|
||||
text = delta.map(d => d.insert).join('');
|
||||
}
|
||||
return text;
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './adapter';
|
||||
export * from './consts';
|
||||
export * from './draggable';
|
||||
export * from './element-renderer';
|
||||
|
||||
Reference in New Issue
Block a user