mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-22 19:53:48 +08:00
feat: make width and height of generated image same with input image (#7356)
[BS-641](https://linear.app/affine-design/issue/BS-641/通过image-action生成的图像尺寸应与原图保持一致)
This commit is contained in:
@@ -10,6 +10,7 @@ import type {
|
|||||||
SurfaceBlockModel,
|
SurfaceBlockModel,
|
||||||
} from '@blocksuite/blocks';
|
} from '@blocksuite/blocks';
|
||||||
import {
|
import {
|
||||||
|
Bound,
|
||||||
DeleteIcon,
|
DeleteIcon,
|
||||||
EDGELESS_ELEMENT_TOOLBAR_WIDGET,
|
EDGELESS_ELEMENT_TOOLBAR_WIDGET,
|
||||||
EDGELESS_TEXT_BLOCK_MIN_HEIGHT,
|
EDGELESS_TEXT_BLOCK_MIN_HEIGHT,
|
||||||
@@ -245,6 +246,11 @@ const defaultHandler = (host: EditorHost) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Image handler for inserting generated image into the edgeless document.
|
||||||
|
* Should make the inserting image size same with the input image if there is an input image.
|
||||||
|
* @param host
|
||||||
|
*/
|
||||||
const imageHandler = (host: EditorHost) => {
|
const imageHandler = (host: EditorHost) => {
|
||||||
const aiPanel = getAIPanel(host);
|
const aiPanel = getAIPanel(host);
|
||||||
// `DataURL` or `URL`
|
// `DataURL` or `URL`
|
||||||
@@ -253,6 +259,13 @@ const imageHandler = (host: EditorHost) => {
|
|||||||
|
|
||||||
const edgelessCopilot = getEdgelessCopilotWidget(host);
|
const edgelessCopilot = getEdgelessCopilotWidget(host);
|
||||||
const bounds = edgelessCopilot.determineInsertionBounds();
|
const bounds = edgelessCopilot.determineInsertionBounds();
|
||||||
|
const selectedElements = getCopilotSelectedElems(host);
|
||||||
|
const selectedImageBlockModel = selectedElements.find(
|
||||||
|
model => model instanceof ImageBlockModel
|
||||||
|
);
|
||||||
|
const selectedBound = selectedImageBlockModel
|
||||||
|
? Bound.deserialize(selectedImageBlockModel.xywh)
|
||||||
|
: null;
|
||||||
|
|
||||||
edgelessCopilot.hideCopilotPanel();
|
edgelessCopilot.hideCopilotPanel();
|
||||||
aiPanel.hide();
|
aiPanel.hide();
|
||||||
@@ -269,7 +282,25 @@ const imageHandler = (host: EditorHost) => {
|
|||||||
const [x, y] = edgelessRoot.service.viewport.toViewCoord(minX, minY);
|
const [x, y] = edgelessRoot.service.viewport.toViewCoord(minX, minY);
|
||||||
|
|
||||||
host.doc.transact(() => {
|
host.doc.transact(() => {
|
||||||
edgelessRoot.addImages([img], [x, y], true).catch(console.error);
|
edgelessRoot
|
||||||
|
.addImages([img], [x, y], true)
|
||||||
|
.then(blockIds => {
|
||||||
|
const imageBlockId = blockIds[0];
|
||||||
|
const imageBlock = host.doc.getBlock(imageBlockId);
|
||||||
|
if (!imageBlock || !selectedBound) return;
|
||||||
|
|
||||||
|
// Update the image width and height to the same with the selected image
|
||||||
|
const imageModel = imageBlock.model as ImageBlockModel;
|
||||||
|
const imageBound = Bound.deserialize(imageModel.xywh);
|
||||||
|
const newBound = new Bound(
|
||||||
|
imageBound.x,
|
||||||
|
imageBound.y,
|
||||||
|
selectedBound.w,
|
||||||
|
selectedBound.h
|
||||||
|
);
|
||||||
|
host.doc.updateBlock(imageModel, { xywh: newBound.serialize() });
|
||||||
|
})
|
||||||
|
.catch(console.error);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
|
|||||||
Reference in New Issue
Block a user