Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

refactor(editor): remove edgeless service in ai widgets (#10361)

Saul-Mirone04ed2bdab733
diff --git a/blocksuite/affine/block-root/src/edgeless/edgeless-keyboard.ts b/blocksuite/affine/block-root/src/edgeless/edgeless-keyboard.ts
index e6ee791794..f93d12f5fc 100644
--- a/blocksuite/affine/block-root/src/edgeless/edgeless-keyboard.ts
+++ b/blocksuite/affine/block-root/src/edgeless/edgeless-keyboard.ts
@@ -33,7 +33,6 @@ import { Bound, getCommonBound } from '@blocksuite/global/utils';
import { PageKeyboardManager } from '../keyboard/keyboard-manager.js';
import type { EdgelessRootBlockComponent } from './edgeless-root-block.js';
-import { CopilotTool } from './gfx-tool/copilot-tool.js';
import { LassoTool } from './gfx-tool/lasso-tool.js';
import { ShapeTool } from './gfx-tool/shape-tool.js';
import {
@@ -326,9 +325,6 @@ export class EdgelessPageKeyboardManager extends PageKeyboardManager {
if (currentTool instanceof LassoTool && currentTool.isSelecting) {
currentTool.abort();
}
- if (currentTool instanceof CopilotTool) {
- currentTool.abort();
- }
if (!this.rootComponent.service.selection.empty) {
rootComponent.selection.clear();
diff --git a/blocksuite/affine/block-root/src/widgets/edgeless-copilot-panel/index.ts b/blocksuite/affine/block-root/src/widgets/edgeless-copilot-panel/index.ts
index f19fbca779..2653d7ec3e 100644
--- a/blocksuite/affine/block-root/src/widgets/edgeless-copilot-panel/index.ts
+++ b/blocksuite/affine/block-root/src/widgets/edgeless-copilot-panel/index.ts
@@ -6,8 +6,6 @@ import { WithDisposable } from '@blocksuite/global/utils';
import { css, html, LitElement, nothing } from 'lit';
import { property } from 'lit/decorators.js';
-import type { EdgelessRootBlockComponent } from '../../edgeless/edgeless-root-block.js';
-
export class EdgelessCopilotPanel extends WithDisposable(LitElement) {
static override styles = css`
:host {
@@ -34,7 +32,7 @@ export class EdgelessCopilotPanel extends WithDisposable(LitElement) {
`;
private _getChain() {
- return this.edgeless.service.std.command.chain();
+ return this.host.std.command.chain();
}
override connectedCallback(): void {
@@ -74,9 +72,6 @@ export class EdgelessCopilotPanel extends WithDisposable(LitElement) {
`;
}
- @property({ attribute: false })
- accessor edgeless!: EdgelessRootBlockComponent;
-
@property({ attribute: false })
accessor entry: 'toolbar' | 'selection' | undefined = undefined;
diff --git a/blocksuite/affine/block-root/src/widgets/edgeless-copilot-panel/toolbar-entry.ts b/blocksuite/affine/block-root/src/widgets/edgeless-copilot-panel/toolbar-entry.ts
index 6a86ef6cee..2593f50b39 100644
--- a/blocksuite/affine/block-root/src/widgets/edgeless-copilot-panel/toolbar-entry.ts
+++ b/blocksuite/affine/block-root/src/widgets/edgeless-copilot-panel/toolbar-entry.ts
@@ -1,12 +1,14 @@
import type { AIItemGroupConfig } from '@blocksuite/affine-components/ai-item';
import { AIStarIcon } from '@blocksuite/affine-components/icons';
import type { EditorHost } from '@blocksuite/block-std';
-import { isGfxGroupCompatibleModel } from '@blocksuite/block-std/gfx';
+import {
+ GfxControllerIdentifier,
+ isGfxGroupCompatibleModel,
+} from '@blocksuite/block-std/gfx';
import { WithDisposable } from '@blocksuite/global/utils';
import { css, html, LitElement } from 'lit';
import { property } from 'lit/decorators.js';
-import type { EdgelessRootBlockComponent } from '../../edgeless/edgeless-root-block.js';
import type { CopilotTool } from '../../edgeless/gfx-tool/copilot-tool.js';
import { sortEdgelessElements } from '../../edgeless/utils/clone-utils.js';
@@ -26,9 +28,13 @@ export class EdgelessCopilotToolbarEntry extends WithDisposable(LitElement) {
this._showCopilotPanel();
};
+ private get _gfx() {
+ return this.host.std.get(GfxControllerIdentifier);
+ }
+
private _showCopilotPanel() {
const selectedElements = sortEdgelessElements(
- this.edgeless.service.selection.selectedElements
+ this._gfx.selection.selectedElements
);
const toBeSelected = new Set(selectedElements);
@@ -45,10 +51,11 @@ export class EdgelessCopilotToolbarEntry extends WithDisposable(LitElement) {
}
});
- this.edgeless.gfx.tool.setTool('copilot');
- (
- this.edgeless.gfx.tool.currentTool$.peek() as CopilotTool
- ).updateSelectionWith(Array.from(toBeSelected), 10);
+ this._gfx.tool.setTool('copilot');
+ (this._gfx.tool.currentTool$.peek() as CopilotTool).updateSelectionWith(
+ Array.from(toBeSelected),
+ 10
+ );
}
override render() {
@@ -61,9 +68,6 @@ export class EdgelessCopilotToolbarEntry extends WithDisposable(LitElement) {
</edgeless-tool-icon-button>`;
}
- @property({ attribute: false })
- accessor edgeless!: EdgelessRootBlockComponent;
-
@property({ attribute: false })
accessor groups!: AIItemGroupConfig[];
diff --git a/blocksuite/affine/block-root/src/widgets/edgeless-copilot/index.ts b/blocksuite/affine/block-root/src/widgets/edgeless-copilot/index.ts
index ca2d035ba1..95b68a9ac7 100644
--- a/blocksuite/affine/block-root/src/widgets/edgeless-copilot/index.ts
+++ b/blocksuite/affine/block-root/src/widgets/edgeless-copilot/index.ts
@@ -1,3 +1,4 @@
+import { EdgelessLegacySlotIdentifier } from '@blocksuite/affine-block-surface';
import type { AIItemGroupConfig } from '@blocksuite/affine-components/ai-item';
import type { RootBlockModel } from '@blocksuite/affine-model';
import {
@@ -5,6 +6,7 @@ import {
requestConnectedFrame,
} from '@blocksuite/affine-shared/utils';
import { WidgetComponent } from '@blocksuite/block-std';
+import { GfxControllerIdentifier } from '@blocksuite/block-std/gfx';
import { Bound, getCommonBoundWithRotation } from '@blocksuite/global/utils';
import {
autoUpdate,
@@ -19,7 +21,6 @@ import { css, html, nothing } from 'lit';
import { query, state } from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
-import type { EdgelessRootBlockComponent } from '../../edgeless/edgeless-root-block.js';
import {
AFFINE_AI_PANEL_WIDGET,
AffineAIPanelWidget,
@@ -28,10 +29,7 @@ import { EdgelessCopilotPanel } from '../edgeless-copilot-panel/index.js';
export const AFFINE_EDGELESS_COPILOT_WIDGET = 'affine-edgeless-copilot-widget';
-export class EdgelessCopilotWidget extends WidgetComponent<
- RootBlockModel,
- EdgelessRootBlockComponent
-> {
+export class EdgelessCopilotWidget extends WidgetComponent<RootBlockModel> {
static override styles = css`
.copilot-selection-rect {
position: absolute;
@@ -51,8 +49,8 @@ export class EdgelessCopilotWidget extends WidgetComponent<
groups: AIItemGroupConfig[] = [];
- get edgeless() {
- return this.block;
+ get gfx() {
+ return this.std.get(GfxControllerIdentifier);
}
get selectionModelRect() {
@@ -81,7 +79,6 @@ export class EdgelessCopilotWidget extends WidgetComponent<
const panel = new EdgelessCopilotPanel();
panel.host = this.host;
panel.groups = this.groups;
- panel.edgeless = this.edgeless;
this.renderRoot.append(panel);
this._copilotPanel = panel;
}
@@ -89,7 +86,7 @@ export class EdgelessCopilotWidget extends WidgetComponent<
const referenceElement = this.selectionElem;
const panel = this._copilotPanel;
// @TODO: optimize
- const viewport = this.edgeless.service.viewport;
+ const viewport = this.gfx.viewport;
if (!referenceElement || !referenceElement.isConnected) return;
@@ -153,11 +150,8 @@ export class EdgelessCopilotWidget extends WidgetComponent<
private _updateSelection(rect: DOMRect) {
this._selectionModelRect = rect;
- const zoom = this.edgeless.service.viewport.zoom;
- const [x, y] = this.edgeless.service.viewport.toViewCoord(
- rect.left,
- rect.top
- );
+ const zoom = this.gfx.viewport.zoom;
+ const [x, y] = this.gfx.viewport.toViewCoord(rect.left, rect.top);
const [width, height] = [rect.width * zoom, rect.height * zoom];
this._selectionRect = { x, y, width, height };
@@ -176,7 +170,7 @@ export class EdgelessCopilotWidget extends WidgetComponent<
return;
}
- const off = this.block.dispatcher.add('pointerDown', ctx => {
+ const off = this.std.event.add('pointerDown', ctx => {
const e = ctx.get('pointerState').raw;
if (
e.button === MOUSE_BUTTON.MAIN &&
@@ -196,7 +190,7 @@ export class EdgelessCopilotWidget extends WidgetComponent<
override connectedCallback(): void {
super.connectedCallback();
- const CopilotSelectionTool = this.edgeless.gfx.tool.get('copilot');
+ const CopilotSelectionTool = this.gfx.tool.get('copilot');
this._disposables.add(
CopilotSelectionTool.draggingAreaUpdated.on(shouldShowPanel => {
@@ -212,7 +206,7 @@ export class EdgelessCopilotWidget extends WidgetComponent<
);
this._disposables.add(
- this.edgeless.service.viewport.viewportUpdated.on(() => {
+ this.gfx.viewport.viewportUpdated.on(() => {
if (!this._visible) return;
this._updateSelection(CopilotSelectionTool.area);
@@ -221,7 +215,7 @@ export class EdgelessCopilotWidget extends WidgetComponent<
this._disposables.add(
effect(() => {
- const currentTool = this.edgeless.gfx.tool.currentToolName$.value;
+ const currentTool = this.gfx.tool.currentToolName$.value;
if (!this._visible || currentTool === 'copilot') return;
@@ -234,8 +228,8 @@ export class EdgelessCopilotWidget extends WidgetComponent<
}
determineInsertionBounds(width = 800, height = 95) {
- const elements = this.edgeless.service.selection.selectedElements;
- const offsetY = 20 / this.edgeless.service.viewport.zoom;
+ const elements = this.gfx.selection.selectedElements;
+ const offsetY = 20 / this.gfx.viewport.zoom;
const bounds = new Bound(0, 0, width, height);
if (elements.length) {
const { x, y, h } = getCommonBoundWithRotation(elements);
@@ -256,7 +250,8 @@ export class EdgelessCopilotWidget extends WidgetComponent<
}
lockToolbar(disabled: boolean) {
- this.edgeless.slots.toolbarLocked.emit(disabled);
+ const legacySlot = this.std.get(EdgelessLegacySlotIdentifier);
+ legacySlot.toolbarLocked.emit(disabled);
}
override render() {
diff --git a/packages/frontend/core/src/blocksuite/presets/ai/actions/edgeless-handler.ts b/packages/frontend/core/src/blocksuite/presets/ai/actions/edgeless-handler.ts
index 241ea706c2..a23de4d22b 100644
--- a/packages/frontend/core/src/blocksuite/presets/ai/actions/edgeless-handler.ts
+++ b/packages/frontend/core/src/blocksuite/presets/ai/actions/edgeless-handler.ts
@@ -350,8 +350,8 @@ function updateEdgelessAIPanelConfig<
config.hideCallback = () => {
aiPanel.updateComplete
.finally(() => {
- edgelessCopilot.edgeless.gfx.tool.setTool('default');
- edgelessCopilot.edgeless.gfx.selection.set({
+ edgelessCopilot.gfx.tool.setTool('default');
+ edgelessCopilot.gfx.selection.set({
elements: [],
editing: false,
});
diff --git a/packages/frontend/core/src/blocksuite/presets/ai/entries/edgeless/index.ts b/packages/frontend/core/src/blocksuite/presets/ai/entries/edgeless/index.ts
index aff9c2287c..6a8495dc71 100644
--- a/packages/frontend/core/src/blocksuite/presets/ai/entries/edgeless/index.ts
+++ b/packages/frontend/core/src/blocksuite/presets/ai/entries/edgeless/index.ts
@@ -71,7 +71,6 @@ export function setupEdgelessElementToolbarAIEntry(
};
return html`<edgeless-copilot-toolbar-entry
- .edgeless=${edgeless}
.host=${edgeless.host}
.groups=${edgelessAIGroups}
.onClick=${handler}