Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(presets): upload image using blob #6872

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/presets/src/ai/actions/edgeless-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function actionToStream<T extends keyof BlockSuitePresets.AIActions>(
>,
extract?: (host: EditorHost) => Promise<{
content?: string;
attachments?: string[];
attachments?: (string | Blob)[];
} | void>
) {
const action = AIProvider.actions[id];
Expand Down Expand Up @@ -167,7 +167,7 @@ function actionToGeneration<T extends keyof BlockSuitePresets.AIActions>(
>,
extract?: (host: EditorHost) => Promise<{
content?: string;
attachments?: string[];
attachments?: (string | Blob)[];
} | void>
) {
return (host: EditorHost) => {
Expand Down Expand Up @@ -204,7 +204,7 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(
customInput?: (host: EditorHost) => Promise<{
input?: string;
content?: string;
attachments?: string[];
attachments?: (string | Blob)[];
} | void>
) {
return (host: EditorHost) => {
Expand Down
7 changes: 5 additions & 2 deletions packages/presets/src/ai/entries/edgeless/actions-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getCopilotSelectedElems } from '../../actions/edgeless-response.js';
import { translateLangs } from '../../actions/types.js';
import { getAIPanel } from '../../ai-panel.js';
import { mindMapToMarkdown } from '../../utils/edgeless.js';
import { canvasToBlob } from '../../utils/image.js';
import { getEdgelessRootFromEditor } from '../../utils/selection-utils.js';

const translateSubItem = translateLangs.map(lang => {
Expand Down Expand Up @@ -248,7 +249,8 @@ export const createGroup: AIItemGroupConfig = {
1
);
if (!canvas) return;
const png = canvas.toDataURL('image/png', 0.843);
// const png = canvas.toDataURL('image/png');
const png = await canvasToBlob(canvas);
if (!png) return;
return {
attachments: [png],
Expand Down Expand Up @@ -276,7 +278,8 @@ export const createGroup: AIItemGroupConfig = {
1
);
if (!canvas) return;
const png = canvas.toDataURL('image/png', 0.843);
// const png = canvas.toDataURL('image/png');
const png = await canvasToBlob(canvas);
if (!png) return;
return {
attachments: [png],
Expand Down
10 changes: 10 additions & 0 deletions packages/presets/src/ai/utils/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,13 @@ export function readBlobAsURL(blob: Blob | File) {
reader.readAsDataURL(blob);
});
}

export function canvasToBlob(
canvas: HTMLCanvasElement,
type = 'image/png',
quality?: number
) {
return new Promise<Blob | null>(resolve =>
canvas.toBlob(resolve, type, quality)
);
}
Loading