refactor(editor): merge get surface util (#9371)

This commit is contained in:
Saul-Mirone
2024-12-27 09:42:13 +00:00
parent 6977b0a948
commit 6da10f9a93
15 changed files with 47 additions and 54 deletions

View File

@@ -1,4 +1,4 @@
import type { SurfaceBlockModel } from '@blocksuite/affine-block-surface'; import { getSurfaceBlock } from '@blocksuite/affine-block-surface';
import { import {
type DocMode, type DocMode,
type ImageBlockModel, type ImageBlockModel,
@@ -263,11 +263,6 @@ export function isEmptyNote(note: BlockModel) {
}); });
} }
function getSurfaceBlock(doc: Doc) {
const blocks = doc.getBlocksByFlavour('affine:surface');
return blocks.length !== 0 ? (blocks[0].model as SurfaceBlockModel) : null;
}
/** /**
* Gets the document content with a max length. * Gets the document content with a max length.
*/ */

View File

@@ -104,7 +104,7 @@ import {
tryMoveNode, tryMoveNode,
} from './utils/mindmap/utils'; } from './utils/mindmap/utils';
export * from './extensions'; export * from './extensions';
export { getLastPropsKey } from './utils/get-last-props-key'; export { getLastPropsKey, getSurfaceBlock } from './utils';
export type { Options } from './utils/rough/core'; export type { Options } from './utils/rough/core';
export { sortIndex } from './utils/sort'; export { sortIndex } from './utils/sort';
export { updateXYWH } from './utils/update-xywh.js'; export { updateXYWH } from './utils/update-xywh.js';

View File

@@ -2,6 +2,7 @@ import { BlockService } from '@blocksuite/block-std';
import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx'; import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
import { type SurfaceBlockModel, SurfaceBlockSchema } from './surface-model.js'; import { type SurfaceBlockModel, SurfaceBlockSchema } from './surface-model.js';
import { getSurfaceBlock } from './utils/get-surface-block.js';
export class SurfaceBlockService extends BlockService { export class SurfaceBlockService extends BlockService {
static override readonly flavour = SurfaceBlockSchema.model.flavour; static override readonly flavour = SurfaceBlockSchema.model.flavour;
@@ -15,9 +16,10 @@ export class SurfaceBlockService extends BlockService {
override mounted(): void { override mounted(): void {
super.mounted(); super.mounted();
this.surface = this.doc.getBlockByFlavour( const surface = getSurfaceBlock(this.doc);
'affine:surface'
)[0] as SurfaceBlockModel; // FIXME: BS-2271
this.surface = surface!;
if (!this.surface) { if (!this.surface) {
const disposable = this.doc.slots.blockUpdated.on(payload => { const disposable = this.doc.slots.blockUpdated.on(payload => {

View File

@@ -0,0 +1,8 @@
import type { Doc } from '@blocksuite/store';
import type { SurfaceBlockModel } from '../surface-model';
export function getSurfaceBlock(doc: Doc) {
const blocks = doc.getBlocksByFlavour('affine:surface');
return blocks.length !== 0 ? (blocks[0].model as SurfaceBlockModel) : null;
}

View File

@@ -34,3 +34,4 @@ export function normalizeWheelDeltaY(delta: number, zoom = 1) {
} }
export { getLastPropsKey } from './get-last-props-key'; export { getLastPropsKey } from './get-last-props-key';
export { getSurfaceBlock } from './get-surface-block';

View File

@@ -1,3 +1,4 @@
import { getSurfaceBlock } from '@blocksuite/affine-block-surface';
import type { FrameBlockModel, NoteBlockModel } from '@blocksuite/affine-model'; import type { FrameBlockModel, NoteBlockModel } from '@blocksuite/affine-model';
import { NoteDisplayMode } from '@blocksuite/affine-model'; import { NoteDisplayMode } from '@blocksuite/affine-model';
import { DocModeProvider } from '@blocksuite/affine-shared/services'; import { DocModeProvider } from '@blocksuite/affine-shared/services';
@@ -15,7 +16,6 @@ import {
isFrameBlock, isFrameBlock,
isNoteBlock, isNoteBlock,
} from '../../root-block/edgeless/utils/query.js'; } from '../../root-block/edgeless/utils/query.js';
import { getSurfaceBlock } from '../../surface-ref-block/utils.js';
export function addBlocksToDoc( export function addBlocksToDoc(
targetDoc: Doc, targetDoc: Doc,

View File

@@ -1,10 +1,12 @@
import { EdgelessCRUDIdentifier } from '@blocksuite/affine-block-surface'; import {
EdgelessCRUDIdentifier,
getSurfaceBlock,
} from '@blocksuite/affine-block-surface';
import { focusTextModel } from '@blocksuite/affine-components/rich-text'; import { focusTextModel } from '@blocksuite/affine-components/rich-text';
import type { Command } from '@blocksuite/block-std'; import type { Command } from '@blocksuite/block-std';
import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx'; import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
import { Bound } from '@blocksuite/global/utils'; import { Bound } from '@blocksuite/global/utils';
import { getSurfaceBlock } from '../../surface-ref-block/utils.js';
import { import {
EDGELESS_TEXT_BLOCK_MIN_HEIGHT, EDGELESS_TEXT_BLOCK_MIN_HEIGHT,
EDGELESS_TEXT_BLOCK_MIN_WIDTH, EDGELESS_TEXT_BLOCK_MIN_WIDTH,

View File

@@ -3,6 +3,7 @@ import {
EdgelessLegacySlotIdentifier, EdgelessLegacySlotIdentifier,
type ElementRenderer, type ElementRenderer,
elementRenderers, elementRenderers,
getSurfaceBlock,
type SurfaceBlockModel, type SurfaceBlockModel,
type SurfaceContext, type SurfaceContext,
} from '@blocksuite/affine-block-surface'; } from '@blocksuite/affine-block-surface';
@@ -30,7 +31,6 @@ import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { Bound, getCommonBound } from '@blocksuite/global/utils'; import { Bound, getCommonBound } from '@blocksuite/global/utils';
import { effect } from '@preact/signals-core'; import { effect } from '@preact/signals-core';
import { getSurfaceBlock } from '../../surface-ref-block/utils.js';
import { RootService } from '../root-service.js'; import { RootService } from '../root-service.js';
import { GfxBlockModel } from './block-model.js'; import { GfxBlockModel } from './block-model.js';
import type { EdgelessFrameManager } from './frame-manager.js'; import type { EdgelessFrameManager } from './frame-manager.js';

View File

@@ -2,9 +2,10 @@ import type {
SurfaceBlockComponent, SurfaceBlockComponent,
SurfaceBlockModel, SurfaceBlockModel,
} from '@blocksuite/affine-block-surface'; } from '@blocksuite/affine-block-surface';
import { Overlay } from '@blocksuite/affine-block-surface'; import { getSurfaceBlock, Overlay } from '@blocksuite/affine-block-surface';
import type { ConnectorElementModel } from '@blocksuite/affine-model'; import type { ConnectorElementModel } from '@blocksuite/affine-model';
import type { GfxController } from '@blocksuite/block-std/gfx'; import type { GfxController } from '@blocksuite/block-std/gfx';
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
import { Bound, Point } from '@blocksuite/global/utils'; import { Bound, Point } from '@blocksuite/global/utils';
import { isConnectable } from '../utils/query.js'; import { isConnectable } from '../utils/query.js';
@@ -52,9 +53,13 @@ export class EdgelessSnapManager extends Overlay {
}; };
private get _surface() { private get _surface() {
const surfaceModel = this.gfx.doc.getBlockByFlavour( const surfaceModel = getSurfaceBlock(this.gfx.doc);
'affine:surface' if (!surfaceModel) {
)[0] as SurfaceBlockModel; throw new BlockSuiteError(
ErrorCode.ValueNotExists,
'Surface block not found in doc when creating snap manager'
);
}
return this.gfx.std.view.getBlock(surfaceModel.id) as SurfaceBlockComponent; return this.gfx.std.view.getBlock(surfaceModel.id) as SurfaceBlockComponent;
} }

View File

@@ -1,4 +1,5 @@
import { addSiblingAttachmentBlocks } from '@blocksuite/affine-block-attachment'; import { addSiblingAttachmentBlocks } from '@blocksuite/affine-block-attachment';
import { getSurfaceBlock } from '@blocksuite/affine-block-surface';
import { import {
getInlineEditorByModel, getInlineEditorByModel,
insertContent, insertContent,
@@ -61,7 +62,6 @@ import { cssVarV2 } from '@toeverything/theme/v2';
import type { TemplateResult } from 'lit'; import type { TemplateResult } from 'lit';
import { toggleEmbedCardCreateModal } from '../../../_common/components/embed-card/modal/embed-card-create-modal.js'; import { toggleEmbedCardCreateModal } from '../../../_common/components/embed-card/modal/embed-card-create-modal.js';
import { getSurfaceBlock } from '../../../surface-ref-block/utils.js';
import type { PageRootBlockComponent } from '../../page/page-root-block.js'; import type { PageRootBlockComponent } from '../../page/page-root-block.js';
import { formatDate, formatTime } from '../../utils/misc.js'; import { formatDate, formatTime } from '../../utils/misc.js';
import type { AffineLinkedDocWidget } from '../linked-doc/index.js'; import type { AffineLinkedDocWidget } from '../linked-doc/index.js';

View File

@@ -5,6 +5,7 @@ import {
LoomIcon, LoomIcon,
YoutubeIcon, YoutubeIcon,
} from '@blocksuite/affine-block-embed'; } from '@blocksuite/affine-block-embed';
import { getSurfaceBlock } from '@blocksuite/affine-block-surface';
import { import {
ArrowDownBigIcon, ArrowDownBigIcon,
ArrowUpBigIcon, ArrowUpBigIcon,
@@ -51,7 +52,6 @@ import type { TemplateResult } from 'lit';
import { toggleEmbedCardCreateModal } from '../../../_common/components/embed-card/modal/embed-card-create-modal.js'; import { toggleEmbedCardCreateModal } from '../../../_common/components/embed-card/modal/embed-card-create-modal.js';
import type { DataViewBlockComponent } from '../../../data-view-block/index.js'; import type { DataViewBlockComponent } from '../../../data-view-block/index.js';
import { getSurfaceBlock } from '../../../surface-ref-block/utils.js';
import type { RootBlockComponent } from '../../types.js'; import type { RootBlockComponent } from '../../types.js';
import { formatDate, formatTime } from '../../utils/misc.js'; import { formatDate, formatTime } from '../../utils/misc.js';
import type { AffineLinkedDocWidget } from '../linked-doc/index.js'; import type { AffineLinkedDocWidget } from '../linked-doc/index.js';

View File

@@ -1,9 +1,8 @@
import { getSurfaceBlock } from '@blocksuite/affine-block-surface';
import type { SurfaceRefProps } from '@blocksuite/affine-model'; import type { SurfaceRefProps } from '@blocksuite/affine-model';
import { matchFlavours } from '@blocksuite/affine-shared/utils'; import { matchFlavours } from '@blocksuite/affine-shared/utils';
import type { BlockCommands, Command } from '@blocksuite/block-std'; import type { BlockCommands, Command } from '@blocksuite/block-std';
import { getSurfaceBlock } from './utils.js';
export const insertSurfaceRefBlockCommand: Command< export const insertSurfaceRefBlockCommand: Command<
'selectedModels', 'selectedModels',
'insertedSurfaceRefBlockId', 'insertedSurfaceRefBlockId',

View File

@@ -1,4 +1,5 @@
import { import {
getSurfaceBlock,
type SurfaceBlockModel, type SurfaceBlockModel,
SurfaceElementModel, SurfaceElementModel,
} from '@blocksuite/affine-block-surface'; } from '@blocksuite/affine-block-surface';
@@ -306,10 +307,7 @@ export class SurfaceRefBlockComponent extends BlockComponent<SurfaceRefBlockMode
} }
private _initReferencedModel() { private _initReferencedModel() {
const surfaceModel: SurfaceBlockModel | null = const surfaceModel = getSurfaceBlock(this.doc);
(this.doc.getBlocksByFlavour('affine:surface')[0]?.model as
| SurfaceBlockModel
| undefined) ?? null;
this._surfaceModel = surfaceModel; this._surfaceModel = surfaceModel;
const findReferencedModel = (): [ const findReferencedModel = (): [
@@ -338,15 +336,11 @@ export class SurfaceRefBlockComponent extends BlockComponent<SurfaceRefBlockMode
.find( .find(
doc => doc =>
doc.getBlock(this.model.reference) || doc.getBlock(this.model.reference) ||
( getSurfaceBlock(doc)!.getElementById(this.model.reference)
doc.getBlocksByFlavour('affine:surface')[0]
.model as SurfaceBlockModel
).getElementById(this.model.reference)
); );
if (doc) { if (doc) {
this._surfaceModel = doc.getBlocksByFlavour('affine:surface')[0] this._surfaceModel = getSurfaceBlock(doc);
.model as SurfaceBlockModel;
} }
if (doc && doc.getBlock(this.model.reference)) { if (doc && doc.getBlock(this.model.reference)) {
@@ -356,12 +350,9 @@ export class SurfaceRefBlockComponent extends BlockComponent<SurfaceRefBlockMode
]; ];
} }
if (doc && doc.getBlocksByFlavour('affine:surface')[0]) { if (doc && getSurfaceBlock(doc)) {
return [ return [
( getSurfaceBlock(doc)!.getElementById(this.model.reference),
doc.getBlocksByFlavour('affine:surface')[0]
.model as SurfaceBlockModel
).getElementById(this.model.reference),
doc.id, doc.id,
]; ];
} }

View File

@@ -1,12 +1,5 @@
import type { SurfaceBlockModel } from '@blocksuite/affine-block-surface';
import type { Doc } from '@blocksuite/store';
import { html } from 'lit'; import { html } from 'lit';
export function getSurfaceBlock(doc: Doc) {
const blocks = doc.getBlocksByFlavour('affine:surface');
return blocks.length !== 0 ? (blocks[0].model as SurfaceBlockModel) : null;
}
export const noContentPlaceholder = html` export const noContentPlaceholder = html`
<svg <svg
width="182" width="182"

View File

@@ -7,7 +7,6 @@ import type {
EdgelessRootService, EdgelessRootService,
MindmapElementModel, MindmapElementModel,
ShapeElementModel, ShapeElementModel,
SurfaceBlockModel,
} from '@blocksuite/affine/blocks'; } from '@blocksuite/affine/blocks';
import { import {
addImages, addImages,
@@ -17,6 +16,7 @@ import {
EDGELESS_TEXT_BLOCK_MIN_WIDTH, EDGELESS_TEXT_BLOCK_MIN_WIDTH,
EdgelessTextBlockModel, EdgelessTextBlockModel,
fitContent, fitContent,
getSurfaceBlock,
ImageBlockModel, ImageBlockModel,
InsertBelowIcon, InsertBelowIcon,
LightLoadingIcon, LightLoadingIcon,
@@ -363,9 +363,8 @@ function responseToCreateImage(host: EditorHost) {
} }
export function responseToExpandMindmap(host: EditorHost, ctx: AIContext) { export function responseToExpandMindmap(host: EditorHost, ctx: AIContext) {
const [surface] = host.doc.getBlockByFlavour( const surface = getSurfaceBlock(host.doc);
'affine:surface' if (!surface) return;
) as SurfaceBlockModel[];
const elements = ctx.get().selectedElements; const elements = ctx.get().selectedElements;
const mindmapNode = ctx.get().node; const mindmapNode = ctx.get().node;
@@ -414,9 +413,8 @@ function responseToBrainstormMindmap(host: EditorHost, ctx: AIContext) {
const edgelessService = getEdgelessService(host); const edgelessService = getEdgelessService(host);
const edgelessCopilot = getEdgelessCopilotWidget(host); const edgelessCopilot = getEdgelessCopilotWidget(host);
const selectionRect = edgelessCopilot.selectionModelRect; const selectionRect = edgelessCopilot.selectionModelRect;
const [surface] = host.doc.getBlockByFlavour( const surface = getSurfaceBlock(host.doc);
'affine:surface' if (!surface) return;
) as SurfaceBlockModel[];
const { node, style, selectedElements } = ctx.get(); const { node, style, selectedElements } = ctx.get();
if (!node) return; if (!node) return;
@@ -478,9 +476,8 @@ function responseToMakeItReal(host: EditorHost, ctx: AIContext) {
html = preprocessHtml(html); html = preprocessHtml(html);
const edgelessCopilot = getEdgelessCopilotWidget(host); const edgelessCopilot = getEdgelessCopilotWidget(host);
const [surface] = host.doc.getBlockByFlavour( const surface = getSurfaceBlock(host.doc);
'affine:surface' if (!surface) return;
) as SurfaceBlockModel[];
const data = ctx.get(); const data = ctx.get();
const bounds = edgelessCopilot.determineInsertionBounds( const bounds = edgelessCopilot.determineInsertionBounds(