mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
refactor(editor): merge implementation of createTemplateJob (#11474)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { addImages } from '@blocksuite/affine/blocks/image';
|
||||
import { createTemplateJob } from '@blocksuite/affine/blocks/root';
|
||||
import { getSurfaceBlock } from '@blocksuite/affine/blocks/surface';
|
||||
import { LightLoadingIcon } from '@blocksuite/affine/components/icons';
|
||||
import { addTree } from '@blocksuite/affine/gfx/mindmap';
|
||||
@@ -46,7 +47,6 @@ import {
|
||||
getEdgelessRootFromEditor,
|
||||
getSurfaceElementFromEditor,
|
||||
} from '../utils/selection-utils';
|
||||
import { createTemplateJob } from '../utils/template-job';
|
||||
import type { AffineAIPanelWidget } from '../widgets/ai-panel/ai-panel';
|
||||
import type { EdgelessCopilotWidget } from '../widgets/edgeless-copilot';
|
||||
import { EXCLUDING_INSERT_ACTIONS, generatingStages } from './consts';
|
||||
@@ -514,7 +514,7 @@ async function responseToCreateSlides(host: EditorHost, ctx: AIContext) {
|
||||
for (let i = 0; i < contents.length; i++) {
|
||||
const image = images[i] || [];
|
||||
const content = contents[i];
|
||||
const job = createTemplateJob(host);
|
||||
const job = createTemplateJob(host.std, 'template');
|
||||
|
||||
const imagePromises = image.map(async ({ id, url }) => {
|
||||
const response = await fetch(url);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { uploadBlobForImage } from '@blocksuite/affine/blocks/image';
|
||||
import { createTemplateJob } from '@blocksuite/affine/blocks/root';
|
||||
import {
|
||||
getSurfaceBlock,
|
||||
SurfaceBlockModel,
|
||||
@@ -25,7 +26,6 @@ import { insertAbove, insertBelow, replace } from '../utils/editor-actions';
|
||||
import { preprocessHtml } from '../utils/html';
|
||||
import { fetchImageToFile } from '../utils/image';
|
||||
import { getSelections } from '../utils/selection-utils';
|
||||
import { createTemplateJob } from '../utils/template-job';
|
||||
|
||||
const PADDING = 100;
|
||||
|
||||
@@ -138,7 +138,7 @@ async function responseToCreateSlides(
|
||||
for (let i = 0; i < contents.length; i++) {
|
||||
const image = images[i];
|
||||
const content = contents[i];
|
||||
const job = createTemplateJob(host);
|
||||
const job = createTemplateJob(host.std, 'template');
|
||||
await Promise.all(
|
||||
image.map(({ id, url }) =>
|
||||
fetch(url)
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { createTemplateJob } from '@blocksuite/affine/blocks/root';
|
||||
import type { EditorHost } from '@blocksuite/affine/std';
|
||||
import { GfxControllerIdentifier } from '@blocksuite/affine/std/gfx';
|
||||
import type { BlockSnapshot } from '@blocksuite/affine/store';
|
||||
|
||||
import { markdownToSnapshot } from '../../utils';
|
||||
import { getSurfaceElementFromEditor } from '../utils/selection-utils';
|
||||
import { createTemplateJob } from '../utils/template-job';
|
||||
import {
|
||||
basicTheme,
|
||||
type PPTDoc,
|
||||
@@ -36,7 +36,7 @@ export const PPTBuilder = (host: EditorHost) => {
|
||||
};
|
||||
docs.push(doc);
|
||||
|
||||
const job = createTemplateJob(host);
|
||||
const job = createTemplateJob(host.std, 'template');
|
||||
const { images, content } = await basicTheme(doc);
|
||||
contents.push(content);
|
||||
allImages.push(images);
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
import {
|
||||
TemplateJob,
|
||||
TemplateMiddlewares,
|
||||
} from '@blocksuite/affine/blocks/root';
|
||||
import { getSurfaceBlock } from '@blocksuite/affine/blocks/surface';
|
||||
import { Bound, getCommonBound } from '@blocksuite/affine/global/gfx';
|
||||
import type { EditorHost } from '@blocksuite/affine/std';
|
||||
import { GfxController, LayerManager } from '@blocksuite/affine/std/gfx';
|
||||
|
||||
export function createTemplateJob(host: EditorHost) {
|
||||
const surface = getSurfaceBlock(host.doc);
|
||||
if (!surface) {
|
||||
throw new Error('surface is not found');
|
||||
}
|
||||
|
||||
const middlewares: ((job: TemplateJob) => void)[] = [];
|
||||
const layer = new LayerManager(host.std.get(GfxController));
|
||||
const bounds = [...layer.blocks, ...layer.canvasElements].map(i =>
|
||||
Bound.deserialize(i.xywh)
|
||||
);
|
||||
const currentContentBound = getCommonBound(bounds);
|
||||
|
||||
if (currentContentBound) {
|
||||
currentContentBound.x += currentContentBound.w + 100;
|
||||
middlewares.push(
|
||||
TemplateMiddlewares.createInsertPlaceMiddleware(currentContentBound)
|
||||
);
|
||||
}
|
||||
|
||||
const idxGenerator = layer.createIndexGenerator();
|
||||
middlewares.push(
|
||||
TemplateMiddlewares.createRegenerateIndexMiddleware(() => idxGenerator())
|
||||
);
|
||||
middlewares.push(TemplateMiddlewares.replaceIdMiddleware);
|
||||
|
||||
return TemplateJob.create({
|
||||
model: surface,
|
||||
type: 'template',
|
||||
middlewares,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user