Skip to content

Commit

Permalink
fix: add toolbarLocked slot
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Apr 23, 2024
1 parent 3bf12a2 commit 82eb599
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import { stopPropagation } from '../../../../_common/utils/event.js';
import { getImageFilesFromLocal } from '../../../../_common/utils/filesys.js';
import type { FrameBlockModel } from '../../../../index.js';
import { Bound, clamp } from '../../../../surface-block/index.js';
import { CopilotSelectionController } from '../../controllers/tools/copilot-tool.js';
import type { EdgelessRootBlockComponent } from '../../edgeless-root-block.js';
import { isFrameBlock } from '../../utils/query.js';
import { launchIntoFullscreen } from '../utils.js';
Expand All @@ -59,6 +58,10 @@ export class EdgelessToolbar extends WithDisposable(LitElement) {
bottom: 28px;
}
:host([disabled]) {
pointer-events: none;
}
.edgeless-toolbar-container-placeholder {
position: absolute;
width: 463px;
Expand Down Expand Up @@ -304,7 +307,7 @@ export class EdgelessToolbar extends WithDisposable(LitElement) {
})
);
_disposables.add(
this.edgeless.slots.navigatorSettingUpdated.on(
edgeless.slots.navigatorSettingUpdated.on(
({ hideToolbar, fillScreen }) => {
if (hideToolbar !== undefined && hideToolbar !== this._hideToolbar) {
this._hideToolbar = hideToolbar;
Expand All @@ -316,6 +319,12 @@ export class EdgelessToolbar extends WithDisposable(LitElement) {
}
)
);
// The toolbar should be disabled while edgeless AI is in progress.
_disposables.add(
edgeless.slots.toolbarLocked.on(disabled => {
this.toggleAttribute('disabled', disabled);
})
);

this._tryLoadNavigatorStateLocalRecord();
}
Expand Down Expand Up @@ -349,16 +358,6 @@ export class EdgelessToolbar extends WithDisposable(LitElement) {
}
}

// The toolbar should be disabled while edgeless AI is in progress.
private _shouldBeDisabled() {
return (
this.edgelessTool.type === 'copilot' &&
this.edgeless.tools.currentController instanceof
CopilotSelectionController &&
this.edgeless.tools.currentController.processing
);
}

protected override updated(changedProperties: PropertyValues) {
const { type } = this.edgelessTool;
if (
Expand Down Expand Up @@ -596,7 +595,6 @@ export class EdgelessToolbar extends WithDisposable(LitElement) {
<style>
.edgeless-toolbar-container {
top: ${this._canHideToolbar() ? '100px' : '0px'};
pointer-events: ${this._shouldBeDisabled() ? 'none' : 'auto'};
}
</style>
${this.edgeless.edgelessTool.type === 'frameNavigator' &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ export class CopilotSelectionController extends EdgelessToolController<CopilotSe

// AI processing
get processing() {
const aiPanel = this._edgeless.widgetElements[
AFFINE_AI_PANEL_WIDGET
] as AffineAIPanelWidget;
const aiPanel = this._edgeless.host.view.getWidget(
AFFINE_AI_PANEL_WIDGET,
this._edgeless.doc.root!.id
) as AffineAIPanelWidget;
return aiPanel && aiPanel.state !== 'hidden';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export class EdgelessRootService extends RootService {
}>(),
tagClicked: new Slot<{ tagId: string }>(),
editorModeSwitch: new Slot<'edgeless' | 'page'>(),

toolbarLocked: new Slot<boolean>(),
};

private _surface!: SurfaceBlockModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,9 @@ export class EdgelessToolsManager {
(isCopilotType && isLastTypeDefault) ||
// (isDefaultType && isLastTypeCopilot) ||
(isCopilotType && isLastTypeCopilot)
)
) {
state = this.selection.selections; // selection should remain same when switching between default and lasso tool
else if (
} else if (
((isDefaultType && !isLastTypeLasso) || isLassoType) &&
((isDefaultType && !isLastTypeCopilot) || isCopilotType) &&
isEmptyState &&
Expand Down
2 changes: 2 additions & 0 deletions packages/blocks/src/root-block/widgets/ai-panel/ai-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface AffineAIPanelWidgetConfig {

finishStateConfig: AIPanelAnswerConfig;
errorStateConfig: AIPanelErrorConfig;
hideCallback?: () => void;
discardCallback?: () => void;

positionConfig?: Partial<ComputePositionConfig>;
Expand Down Expand Up @@ -155,6 +156,7 @@ export class AffineAIPanelWidget extends WidgetElement {
this._inputText = null;
this._answer = null;
this._stopAutoUpdate = undefined;
this.config?.hideCallback?.();
};

discard = (callback: () => void = this._discardCallback) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export class EdgelessCopilotWidget extends WidgetElement<
this._clickOutsideOff = null;
}

lockToolbar(disabled: boolean) {
this.edgeless.slots.toolbarLocked.emit(disabled);
}

private _updateSelection(rect: DOMRect) {
this._selectionModelRect = rect;

Expand Down
19 changes: 14 additions & 5 deletions packages/presets/src/ai/actions/edgeless-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(
};

edgelessCopilot.hideCopilotPanel();
edgelessCopilot.lockToolbar(true);

assertExists(aiPanel.config);

Expand All @@ -224,11 +225,19 @@ export function actionToHandler<T extends keyof BlockSuitePresets.AIActions>(
aiPanel.config.answerRenderer = actionToRenderer(id, host, ctx);
aiPanel.config.finishStateConfig = actionToResponse(id, host, ctx);
aiPanel.config.discardCallback = () => {
edgelessCopilot.visible = false;
edgelessCopilot.edgeless.service.tool.switchToDefaultMode({
elements: [],
editing: false,
});
aiPanel.hide();
// @TODO: remove `async` wrapper when removing selected-rect
(async () => {
await aiPanel.updateComplete;
edgelessCopilot.visible = false;
edgelessCopilot.edgeless.service.tool.switchToDefaultMode({
elements: [],
editing: false,
});
})().catch(console.error);
};
aiPanel.config.hideCallback = () => {
edgelessCopilot.lockToolbar(false);
};

if (edgelessCopilot.visible) {
Expand Down
14 changes: 9 additions & 5 deletions packages/presets/src/ai/actions/edgeless-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,15 @@ export function discard(
handler: () => {
const callback = () => {
panel.hide();
copilot.visible = false;
copilot.edgeless.service.tool.switchToDefaultMode({
elements: [],
editing: false,
});
// @TODO: remove `async` wrapper when removing selected-rect
(async () => {
await panel.updateComplete;
copilot.edgeless.service.tool.switchToDefaultMode({
elements: [],
editing: false,
});
copilot.visible = false;
})().catch(console.error);
};
panel.discard(callback);
},
Expand Down

0 comments on commit 82eb599

Please sign in to comment.