mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-13 04:42:56 +08:00
feat: optimize edgeless ai action insert handler (#7342)
[BS-653](https://linear.app/affine-design/issue/BS-653/在-edgeless-text-上使用-ai-功能时,应该在-edgeless-text-内换行插入,而不是新建一个-note)
This commit is contained in:
@@ -12,6 +12,9 @@ import type {
|
|||||||
import {
|
import {
|
||||||
DeleteIcon,
|
DeleteIcon,
|
||||||
EDGELESS_ELEMENT_TOOLBAR_WIDGET,
|
EDGELESS_ELEMENT_TOOLBAR_WIDGET,
|
||||||
|
EDGELESS_TEXT_BLOCK_MIN_HEIGHT,
|
||||||
|
EDGELESS_TEXT_BLOCK_MIN_WIDTH,
|
||||||
|
EdgelessTextBlockModel,
|
||||||
EmbedHtmlBlockSpec,
|
EmbedHtmlBlockSpec,
|
||||||
fitContent,
|
fitContent,
|
||||||
ImageBlockModel,
|
ImageBlockModel,
|
||||||
@@ -153,37 +156,93 @@ type MindMapNode = {
|
|||||||
children: MindMapNode[];
|
children: MindMapNode[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaultHandler = (host: EditorHost) => {
|
function insertBelow(
|
||||||
const doc = host.doc;
|
host: EditorHost,
|
||||||
const panel = getAIPanel(host);
|
markdown: string,
|
||||||
const edgelessCopilot = getEdgelessCopilotWidget(host);
|
parentId: string,
|
||||||
const bounds = edgelessCopilot.determineInsertionBounds(800, 95);
|
index = 0
|
||||||
|
) {
|
||||||
|
insertFromMarkdown(host, markdown, parentId, index)
|
||||||
|
.then(() => {
|
||||||
|
const service = getService(host);
|
||||||
|
|
||||||
|
service.selection.set({
|
||||||
|
elements: [parentId],
|
||||||
|
editing: false,
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error(err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function createBlockAndInsert(
|
||||||
|
host: EditorHost,
|
||||||
|
markdown: string,
|
||||||
|
type: 'edgelessText' | 'note'
|
||||||
|
) {
|
||||||
|
const doc = host.doc;
|
||||||
|
const edgelessCopilot = getEdgelessCopilotWidget(host);
|
||||||
doc.transact(() => {
|
doc.transact(() => {
|
||||||
assertExists(doc.root);
|
assertExists(doc.root);
|
||||||
const noteBlockId = doc.addBlock(
|
let blockId = '';
|
||||||
'affine:note',
|
const bounds = edgelessCopilot.determineInsertionBounds(
|
||||||
{
|
EDGELESS_TEXT_BLOCK_MIN_WIDTH,
|
||||||
xywh: bounds.serialize(),
|
EDGELESS_TEXT_BLOCK_MIN_HEIGHT
|
||||||
displayMode: NoteDisplayMode.EdgelessOnly,
|
|
||||||
},
|
|
||||||
doc.root.id
|
|
||||||
);
|
);
|
||||||
|
const surfaceBlock = doc.getBlocksByFlavour('affine:surface')[0];
|
||||||
|
if (type === 'edgelessText') {
|
||||||
|
blockId = doc.addBlock(
|
||||||
|
'affine:edgeless-text',
|
||||||
|
{
|
||||||
|
xywh: bounds.serialize(),
|
||||||
|
},
|
||||||
|
surfaceBlock.id
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const bounds = edgelessCopilot.determineInsertionBounds(800, 95);
|
||||||
|
blockId = doc.addBlock(
|
||||||
|
'affine:note',
|
||||||
|
{
|
||||||
|
xywh: bounds.serialize(),
|
||||||
|
displayMode: NoteDisplayMode.EdgelessOnly,
|
||||||
|
},
|
||||||
|
doc.root.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
assertExists(panel.answer);
|
insertBelow(host, markdown, blockId);
|
||||||
insertFromMarkdown(host, panel.answer, noteBlockId)
|
|
||||||
.then(() => {
|
|
||||||
const service = getService(host);
|
|
||||||
|
|
||||||
service.selection.set({
|
|
||||||
elements: [noteBlockId],
|
|
||||||
editing: false,
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.error(err);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* defaultHandler is the default handler for inserting AI response into the edgeless document.
|
||||||
|
* Three situations are handled by this handler:
|
||||||
|
* 1. When selection is a single EdgelessText block, insert the response to the last of the block.
|
||||||
|
* 2. When selections are multiple EdgelessText blocks, insert the response to a new EdgelessBlock.
|
||||||
|
* 3. Otherwise, insert the response to a new Note block.
|
||||||
|
* @param host EditorHost
|
||||||
|
*/
|
||||||
|
const defaultHandler = (host: EditorHost) => {
|
||||||
|
const panel = getAIPanel(host);
|
||||||
|
const selectedElements = getCopilotSelectedElems(host);
|
||||||
|
|
||||||
|
assertExists(panel.answer);
|
||||||
|
if (
|
||||||
|
selectedElements.length === 1 &&
|
||||||
|
selectedElements[0] instanceof EdgelessTextBlockModel
|
||||||
|
) {
|
||||||
|
const edgelessTextBlockId = selectedElements[0].id;
|
||||||
|
const index = selectedElements[0].children.length;
|
||||||
|
insertBelow(host, panel.answer, edgelessTextBlockId, index);
|
||||||
|
} else if (
|
||||||
|
selectedElements.length > 1 &&
|
||||||
|
selectedElements.every(el => el instanceof EdgelessTextBlockModel)
|
||||||
|
) {
|
||||||
|
createBlockAndInsert(host, panel.answer, 'edgelessText');
|
||||||
|
} else {
|
||||||
|
createBlockAndInsert(host, panel.answer, 'note');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const imageHandler = (host: EditorHost) => {
|
const imageHandler = (host: EditorHost) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user