refactor(editor): merge implementation of createTemplateJob (#11474)

This commit is contained in:
Saul-Mirone
2025-04-05 12:59:48 +00:00
parent aed7f40568
commit 41499c1cd6
8 changed files with 77 additions and 98 deletions
@@ -23,6 +23,7 @@ import { styleMap } from 'lit/directives/style-map.js';
import { unsafeSVG } from 'lit/directives/unsafe-svg.js';
import { EdgelessRootService } from '../../../edgeless-root-service.js';
import { createTemplateJob } from '../../../services/template.js';
import { builtInTemplates } from './builtin-templates.js';
import { defaultPreview, Triangle } from './cards.js';
import type { Template } from './template-type.js';
@@ -287,7 +288,11 @@ export class EdgelessTemplatePanel extends WithDisposable(LitElement) {
x: bound.x + bound.w / 2,
y: bound.y + bound.h / 2,
};
const templateJob = this.service.createTemplateJob(template.type, center);
const templateJob = createTemplateJob(
this.edgeless.std,
template.type,
center
);
try {
const { assets } = template;
@@ -11,7 +11,6 @@ import {
RootBlockSchema,
} from '@blocksuite/affine-model';
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { Bound, getCommonBound } from '@blocksuite/global/gfx';
import type { BlockStdScope } from '@blocksuite/std';
import type {
GfxController,
@@ -33,12 +32,6 @@ import clamp from 'lodash-es/clamp';
import { RootService } from '../root-service.js';
import { TemplateJob } from './services/template.js';
import {
createInsertPlaceMiddleware,
createRegenerateIndexMiddleware,
createStickerMiddleware,
replaceIdMiddleware,
} from './services/template-middlewares.js';
import { getCursorMode } from './utils/query.js';
export class EdgelessRootService extends RootService implements SurfaceContext {
@@ -160,46 +153,6 @@ export class EdgelessRootService extends RootService implements SurfaceContext {
);
}
createTemplateJob(
type: 'template' | 'sticker',
center?: { x: number; y: number }
) {
const middlewares: ((job: TemplateJob) => void)[] = [];
if (type === 'template') {
const bounds = [...this.blocks, ...this.elements].map(i =>
Bound.deserialize(i.xywh)
);
const currentContentBound = getCommonBound(bounds);
if (currentContentBound) {
currentContentBound.x +=
currentContentBound.w + 20 / this.viewport.zoom;
middlewares.push(createInsertPlaceMiddleware(currentContentBound));
}
const idxGenerator = this.layer.createIndexGenerator();
middlewares.push(createRegenerateIndexMiddleware(() => idxGenerator()));
}
if (type === 'sticker') {
middlewares.push(
createStickerMiddleware(center || this.viewport.center, () =>
this.layer.generateIndex()
)
);
}
middlewares.push(replaceIdMiddleware);
return TemplateJob.create({
model: this.surface,
type,
middlewares,
});
}
generateIndex() {
return this.layer.generateIndex();
}
@@ -6,6 +6,7 @@ export * from './edgeless-root-block.js';
export { EdgelessRootPreviewBlockComponent } from './edgeless-root-preview-block.js';
export { EdgelessRootService } from './edgeless-root-service.js';
export * from './gfx-tool';
export * from './services/template.js';
export * from './utils/clipboard-utils.js';
export { sortEdgelessElements } from './utils/clone-utils.js';
export { isCanvasElement } from './utils/query.js';
@@ -1,10 +1,14 @@
import type {
SurfaceBlockModel,
SurfaceBlockTransformer,
import {
getSurfaceBlock,
type SurfaceBlockModel,
type SurfaceBlockTransformer,
} from '@blocksuite/affine-block-surface';
import type { ConnectorElementModel } from '@blocksuite/affine-model';
import { BlockSuiteError } from '@blocksuite/global/exceptions';
import { Bound, getCommonBound } from '@blocksuite/global/gfx';
import { assertType } from '@blocksuite/global/utils';
import type { BlockStdScope } from '@blocksuite/std';
import { GfxControllerIdentifier } from '@blocksuite/std/gfx';
import {
type BlockModel,
type BlockSnapshot,
@@ -15,6 +19,13 @@ import {
} from '@blocksuite/store';
import { Subject } from 'rxjs';
import type * as Y from 'yjs';
import {
createInsertPlaceMiddleware,
createRegenerateIndexMiddleware,
createStickerMiddleware,
replaceIdMiddleware,
} from './template-middlewares';
/**
* Those block contains other block's id
* should defer the loading
@@ -369,3 +380,53 @@ export class TemplateJob {
iterate(this._template.blocks, this._template);
}
}
export function createTemplateJob(
std: BlockStdScope,
type: 'template' | 'sticker',
center?: { x: number; y: number }
) {
const surface = getSurfaceBlock(std.store);
if (!surface) {
throw new BlockSuiteError(
BlockSuiteError.ErrorCode.NoSurfaceModelError,
'This doc is missing surface block in edgeless.'
);
}
const gfx = std.get(GfxControllerIdentifier);
const middlewares: ((job: TemplateJob) => void)[] = [];
const { layer, viewport } = gfx;
const blocks = layer.blocks;
const elements = layer.canvasElements;
if (type === 'template') {
const bounds = [...blocks, ...elements].map(i => Bound.deserialize(i.xywh));
const currentContentBound = getCommonBound(bounds);
if (currentContentBound) {
currentContentBound.x += currentContentBound.w + 20 / viewport.zoom;
middlewares.push(createInsertPlaceMiddleware(currentContentBound));
}
const idxGenerator = layer.createIndexGenerator();
middlewares.push(createRegenerateIndexMiddleware(() => idxGenerator()));
}
if (type === 'sticker') {
middlewares.push(
createStickerMiddleware(center || viewport.center, () =>
layer.generateIndex()
)
);
}
middlewares.push(replaceIdMiddleware);
return TemplateJob.create({
model: surface,
type,
middlewares,
});
}