Skip to content

Commit dc40bf7

Browse files
committed
fix(core): ask AI input box in the whiteboard is blocked by the menu
1 parent 149433b commit dc40bf7

File tree

1 file changed

+70
-62
lines changed
  • packages/frontend/core/src/blocksuite/ai/widgets/edgeless-copilot

1 file changed

+70
-62
lines changed

packages/frontend/core/src/blocksuite/ai/widgets/edgeless-copilot/index.ts

+70-62
Original file line numberDiff line numberDiff line change
@@ -77,78 +77,86 @@ export class EdgelessCopilotWidget extends WidgetComponent<RootBlockModel> {
7777
this._visible = visible;
7878
}
7979

80-
private _showCopilotPanel() {
80+
private _showCopilotInput() {
8181
requestConnectedFrame(() => {
82-
if (!this._copilotPanel) {
83-
const panel = new EdgelessCopilotPanel();
84-
panel.host = this.host;
85-
panel.groups = this.groups;
86-
this.renderRoot.append(panel);
87-
this._copilotPanel = panel;
88-
}
89-
9082
const referenceElement = this.selectionElem;
91-
const panel = this._copilotPanel;
92-
// @TODO: optimize
93-
const viewport = this.gfx.viewport;
94-
9583
if (!referenceElement || !referenceElement.isConnected) return;
9684

9785
// show ai input
9886
const rootBlockId = this.host.doc.root?.id;
99-
if (rootBlockId) {
100-
const aiPanel = this.host.view.getWidget(
101-
AFFINE_AI_PANEL_WIDGET,
102-
rootBlockId
103-
);
104-
if (aiPanel instanceof AffineAIPanelWidget && aiPanel.config) {
105-
aiPanel.setState('input', referenceElement);
106-
}
87+
if (!rootBlockId) return;
88+
89+
const input = this.host.view.getWidget(
90+
AFFINE_AI_PANEL_WIDGET,
91+
rootBlockId
92+
);
93+
94+
if (input instanceof AffineAIPanelWidget) {
95+
input.setState('input', referenceElement);
96+
requestAnimationFrame(() => {
97+
this._createCopilotPanel();
98+
this._updateCopilotPanel(input);
99+
});
107100
}
101+
}, this);
102+
}
108103

109-
autoUpdate(referenceElement, panel, () => {
110-
computePosition(referenceElement, panel, {
111-
placement: 'right-start',
112-
middleware: [
113-
offset({
114-
mainAxis: 16,
115-
crossAxis: 45,
116-
}),
117-
flip({
118-
mainAxis: true,
104+
private _createCopilotPanel() {
105+
if (!this._copilotPanel) {
106+
const panel = new EdgelessCopilotPanel();
107+
panel.host = this.host;
108+
panel.groups = this.groups;
109+
this.renderRoot.append(panel);
110+
this._copilotPanel = panel;
111+
}
112+
}
113+
114+
private _updateCopilotPanel(referenceElement: HTMLElement) {
115+
// @TODO: optimize
116+
const viewport = this.gfx.viewport;
117+
const panel = this._copilotPanel;
118+
if (!panel) return;
119+
autoUpdate(referenceElement, panel, () => {
120+
computePosition(referenceElement, panel, {
121+
placement: 'bottom-start',
122+
middleware: [
123+
offset({
124+
mainAxis: 4,
125+
}),
126+
flip({
127+
mainAxis: true,
128+
crossAxis: true,
129+
flipAlignment: true,
130+
}),
131+
shift(() => {
132+
const { left, top, width, height } = viewport;
133+
return {
134+
padding: 20,
119135
crossAxis: true,
120-
flipAlignment: true,
121-
}),
122-
shift(() => {
123-
const { left, top, width, height } = viewport;
124-
return {
125-
padding: 20,
126-
crossAxis: true,
127-
rootBoundary: {
128-
x: left,
129-
y: top,
130-
width,
131-
height: height - 100,
132-
},
133-
};
134-
}),
135-
size({
136-
apply: ({ elements }) => {
137-
const { height } = viewport;
138-
elements.floating.style.maxHeight = `${height - 140}px`;
136+
rootBoundary: {
137+
x: left,
138+
y: top,
139+
width,
140+
height: height - 100,
139141
},
140-
}),
141-
],
142+
};
143+
}),
144+
size({
145+
apply: ({ elements }) => {
146+
const { height } = viewport;
147+
elements.floating.style.maxHeight = `${height - 140}px`;
148+
},
149+
}),
150+
],
151+
})
152+
.then(({ x, y }) => {
153+
panel.style.left = `${x}px`;
154+
panel.style.top = `${y}px`;
142155
})
143-
.then(({ x, y }) => {
144-
panel.style.left = `${x}px`;
145-
panel.style.top = `${y}px`;
146-
})
147-
.catch(e => {
148-
console.warn("Can't compute EdgelessCopilotPanel position", e);
149-
});
150-
});
151-
}, this);
156+
.catch(e => {
157+
console.warn("Can't compute EdgelessCopilotPanel position", e);
158+
});
159+
});
152160
}
153161

154162
private _updateSelection(rect: DOMRect) {
@@ -201,7 +209,7 @@ export class EdgelessCopilotWidget extends WidgetComponent<RootBlockModel> {
201209
this._visible = true;
202210
this._updateSelection(CopilotSelectionTool.area);
203211
if (shouldShowPanel) {
204-
this._showCopilotPanel();
212+
this._showCopilotInput();
205213
this._watchClickOutside();
206214
} else {
207215
this.hideCopilotPanel();

0 commit comments

Comments
 (0)