-
-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blocks): add plain text adapter for mind map element (#9006)
- Loading branch information
1 parent
0f25a1e
commit 661c628
Showing
16 changed files
with
288 additions
and
58 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
packages/affine/block-surface/src/adapters/plain-text/element-adapter/elements/brush.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 7 additions & 3 deletions
10
packages/affine/block-surface/src/adapters/plain-text/element-adapter/elements/connector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 7 additions & 3 deletions
10
packages/affine/block-surface/src/adapters/plain-text/element-adapter/elements/group.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 12 additions & 10 deletions
22
packages/affine/block-surface/src/adapters/plain-text/element-adapter/elements/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import { brushElementModelToPlainTextAdapterMatcher } from './brush.js'; | ||
import { connectorElementModelToPlainTextAdapterMatcher } from './connector.js'; | ||
import { groupElementModelToPlainTextAdapterMatcher } from './group.js'; | ||
import { shapeElementModelToPlainTextAdapterMatcher } from './shape.js'; | ||
import { textElementModelToPlainTextAdapterMatcher } from './text.js'; | ||
import { brushToPlainTextAdapterMatcher } from './brush.js'; | ||
import { connectorToPlainTextAdapterMatcher } from './connector.js'; | ||
import { groupToPlainTextAdapterMatcher } from './group.js'; | ||
import { mindmapToPlainTextAdapterMatcher } from './mindmap.js'; | ||
import { shapeToPlainTextAdapterMatcher } from './shape.js'; | ||
import { textToPlainTextAdapterMatcher } from './text.js'; | ||
|
||
export const elementModelToPlainTextAdapterMatchers = [ | ||
groupElementModelToPlainTextAdapterMatcher, | ||
shapeElementModelToPlainTextAdapterMatcher, | ||
connectorElementModelToPlainTextAdapterMatcher, | ||
brushElementModelToPlainTextAdapterMatcher, | ||
textElementModelToPlainTextAdapterMatcher, | ||
groupToPlainTextAdapterMatcher, | ||
shapeToPlainTextAdapterMatcher, | ||
connectorToPlainTextAdapterMatcher, | ||
brushToPlainTextAdapterMatcher, | ||
textToPlainTextAdapterMatcher, | ||
mindmapToPlainTextAdapterMatcher, | ||
]; |
48 changes: 48 additions & 0 deletions
48
packages/affine/block-surface/src/adapters/plain-text/element-adapter/elements/mindmap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { MindMapTreeNode } from '../../../types/mindmap.js'; | ||
import type { ElementModelToPlainTextAdapterMatcher } from '../type.js'; | ||
|
||
import { buildMindMapTree } from '../../../utils/mindmap.js'; | ||
import { getShapeText } from '../../../utils/shape.js'; | ||
|
||
export const mindmapToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterMatcher = | ||
{ | ||
name: 'mindmap', | ||
match: elementModel => elementModel.type === 'mindmap', | ||
toAST: (elementModel, context) => { | ||
let content = ''; | ||
const mindMapTree = buildMindMapTree(elementModel); | ||
if (!mindMapTree) { | ||
return { content }; | ||
} | ||
// 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 | ||
const { elements } = context; | ||
let layer = 0; | ||
let mindMapContent = ''; | ||
const traverseMindMapTree = (node: MindMapTreeNode, prefix: string) => { | ||
const shapeElement = elements[node.id as string]; | ||
const shapeText = getShapeText(shapeElement); | ||
if (shapeElement) { | ||
mindMapContent += `${prefix.repeat(layer * 4)}- ${shapeText}\n`; | ||
} | ||
node.children.forEach(child => { | ||
layer++; | ||
traverseMindMapTree(child, prefix); | ||
layer--; | ||
}); | ||
}; | ||
traverseMindMapTree(mindMapTree, ' '); | ||
content = `Mind Map with nodes:\n${mindMapContent}`; | ||
return { content }; | ||
}, | ||
}; |
41 changes: 28 additions & 13 deletions
41
packages/affine/block-surface/src/adapters/plain-text/element-adapter/elements/shape.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,42 @@ | ||
import type { DeltaInsert } from '@blocksuite/inline/types'; | ||
import type { MindMapTreeNode } from '../../../types/mindmap.js'; | ||
import type { ElementModelToPlainTextAdapterMatcher } from '../type.js'; | ||
|
||
import type { ElementModelToPlainTextAdapterMatcher } from './type.js'; | ||
import { getShapeText } from '../../../utils/shape.js'; | ||
|
||
export const shapeElementModelToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterMatcher = | ||
export const shapeToPlainTextAdapterMatcher: ElementModelToPlainTextAdapterMatcher = | ||
{ | ||
name: 'shape', | ||
match: elementModel => elementModel.type === 'shape', | ||
toAST: elementModel => { | ||
let text = ''; | ||
let shapeType = ''; | ||
if ('text' in elementModel && elementModel.text) { | ||
let delta: DeltaInsert[] = []; | ||
if ('delta' in elementModel.text) { | ||
delta = elementModel.text.delta as DeltaInsert[]; | ||
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 }; | ||
} | ||
text = delta.map(d => d.insert).join(''); | ||
} | ||
if ('shapeType' in elementModel) { | ||
|
||
// If it is not, we should return the text and shapeType | ||
const text = getShapeText(elementModel); | ||
let shapeType = ''; | ||
if ( | ||
'shapeType' in elementModel && | ||
typeof elementModel.shapeType === 'string' | ||
) { | ||
shapeType = | ||
elementModel.shapeType.charAt(0).toUpperCase() + | ||
elementModel.shapeType.slice(1); | ||
} | ||
const content = `${shapeType}, with text label "${text}"`; | ||
content = `${shapeType}, with text label "${text}"`; | ||
return { content }; | ||
}, | ||
}; |
10 changes: 7 additions & 3 deletions
10
packages/affine/block-surface/src/adapters/plain-text/element-adapter/elements/text.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 17 additions & 7 deletions
24
packages/affine/block-surface/src/adapters/plain-text/element-adapter/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ain-text/element-adapter/elements/type.ts → ...apters/plain-text/element-adapter/type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import type { TextBuffer } from '@blocksuite/affine-shared/adapters'; | ||
|
||
import type { ElementModelMatcher } from '../../../type.js'; | ||
import type { ElementModelMatcher } from '../../type.js'; | ||
|
||
export type ElementModelToPlainTextAdapterMatcher = | ||
ElementModelMatcher<TextBuffer>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
import type { ASTWalkerContext } from '@blocksuite/store'; | ||
|
||
import type { ElementModelMap } from '../element-model/index.js'; | ||
|
||
export type ElementModelAdapterContext<TNode extends object = never> = { | ||
walkerContext: ASTWalkerContext<TNode>; | ||
elements: Record<string, Record<string, unknown>>; | ||
}; | ||
|
||
export type ElementModelMatcher<TNode extends object = never> = { | ||
name: keyof ElementModelMap; | ||
match: (elementModel: ElementModelMap[keyof ElementModelMap]) => boolean; | ||
toAST: (elementModel: ElementModelMap[keyof ElementModelMap]) => TNode; | ||
match: (element: Record<string, unknown>) => boolean; | ||
toAST: ( | ||
element: Record<string, unknown>, | ||
context: ElementModelAdapterContext<TNode> | ||
) => TNode; | ||
}; | ||
|
||
export abstract class ElementModelAdapter<AST = unknown> { | ||
export abstract class ElementModelAdapter< | ||
AST = unknown, | ||
TNode extends object = never, | ||
> { | ||
/** | ||
* Convert element model to AST format | ||
*/ | ||
abstract fromElementModel( | ||
elementModel: ElementModelMap[keyof ElementModelMap] | ||
element: Record<string, unknown>, | ||
context: ElementModelAdapterContext<TNode> | ||
): AST; | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/affine/block-surface/src/adapters/types/mindmap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export interface MindMapTreeNode { | ||
id: string; | ||
index: string; | ||
children: MindMapTreeNode[]; | ||
} | ||
|
||
export interface MindMapNode { | ||
index: string; | ||
parent?: string; | ||
} | ||
|
||
export type MindMapJson = Record<string, MindMapNode>; | ||
|
||
export interface MindMapElement { | ||
index: string; | ||
seed: number; | ||
children: { | ||
'affine:surface:ymap': boolean; | ||
json: MindMapJson; | ||
}; | ||
layoutType: number; | ||
style: number; | ||
type: 'mindmap'; | ||
id: string; | ||
} |
Oops, something went wrong.