fix(core): edgeless text ai action should generate image correctly (#10158)

[BS-2570](https://linear.app/affine-design/issue/BS-2570/edgeless-text-没有被正确的作为上下文放入-app)
This commit is contained in:
donteatfriedrice
2025-02-13 09:38:12 +00:00
parent 887bbcf641
commit 40c6e42ab8
3 changed files with 39 additions and 39 deletions
@@ -23,7 +23,7 @@ export const LIT_REACT_PORTAL = 'lit-react-portal';
@customElement(LIT_REACT_PORTAL)
class LitReactPortal extends LitElement {
portalId!: string;
notify!: PortalListener;
notify?: PortalListener;
static override get properties() {
return {
@@ -34,7 +34,7 @@ class LitReactPortal extends LitElement {
override connectedCallback() {
super.connectedCallback();
this.notify({
this.notify?.({
name: 'connectedCallback',
target: this,
});
@@ -47,7 +47,7 @@ class LitReactPortal extends LitElement {
) {
super.attributeChangedCallback(name, oldVal, newVal);
if (name.toLowerCase() === 'portalid') {
this.notify({
this.notify?.({
name: 'willUpdate',
target: this,
});
@@ -61,7 +61,7 @@ class LitReactPortal extends LitElement {
override disconnectedCallback() {
super.disconnectedCallback();
this.notify({
this.notify?.({
name: 'disconnectedCallback',
target: this,
});
@@ -272,15 +272,19 @@ export class ChatPanel extends WithDisposable(ShadowlessElement) {
const userId = (await AIProvider.userInfo)?.id;
if (!userId) return;
this._chatSessionId = await AIProvider.session?.createSession(
this.doc.workspace.id,
this.doc.id
);
if (this._chatSessionId) {
this._chatContextId = await AIProvider.context?.createContext(
try {
this._chatSessionId = await AIProvider.session?.createSession(
this.doc.workspace.id,
this._chatSessionId
this.doc.id
);
if (this._chatSessionId) {
this._chatContextId = await AIProvider.context?.createContext(
this.doc.workspace.id,
this._chatSessionId
);
}
} catch (e) {
console.error('init panel error', e);
}
await this._updateHistory();
await this._updateChips();
@@ -50,39 +50,35 @@ export async function allToCanvas(host: EditorHost) {
export async function elementsToCanvas(host: EditorHost, elements: GfxModel[]) {
const edgelessRoot = getEdgelessRootFromEditor(host);
const { notes, frames, shapes, images } = BlocksUtils.splitElements(elements);
if (notes.length + frames.length + images.length + shapes.length === 0) {
return;
}
const canvas = await edgelessRoot.clipboardController.toCanvas(
[...notes, ...frames, ...images],
shapes
);
if (!canvas) {
return;
}
return canvas;
}
const { notes, frames, shapes, images, edgelessTexts, embedSyncedDocs } =
BlocksUtils.splitElements(elements);
export async function frameToCanvas(
frame: FrameBlockModel,
editor: EditorHost
) {
const edgelessRoot = getEdgelessRootFromEditor(editor);
const { notes, frames, shapes, images } = BlocksUtils.splitElements(
edgelessRoot.service.frame.getElementsInFrameBound(frame, true)
);
if (notes.length + frames.length + images.length + shapes.length === 0) {
const blockElements = [
...notes,
...frames,
...images,
...edgelessTexts,
...embedSyncedDocs,
];
const hasElements = blockElements.length > 0 || shapes.length > 0;
if (!hasElements) {
return;
}
const canvas = await edgelessRoot.clipboardController.toCanvas(
[...notes, ...frames, ...images],
shapes
);
if (!canvas) {
try {
const canvas = await edgelessRoot.clipboardController.toCanvas(
blockElements,
shapes
);
if (!canvas) {
return;
}
return canvas;
} catch (e) {
console.error('elementsToCanvas error', e);
return;
}
return canvas;
}
export async function selectedToPng(editor: EditorHost) {