refactor(electron): tab title/icon update logic (#7675)

fix AF-1122
fix AF-1136
This commit is contained in:
pengx17
2024-08-01 16:43:18 +00:00
parent e60b2d64e5
commit 07409b8a91
20 changed files with 261 additions and 217 deletions

View File

@@ -4,7 +4,6 @@ import '@affine/component/theme/theme.css';
import { NotificationCenter } from '@affine/component';
import { AffineContext } from '@affine/component/context';
import { GlobalLoading } from '@affine/component/global-loading';
import { registerAffineCommand } from '@affine/core/commands';
import { AppFallback } from '@affine/core/components/affine/app-container';
import { configureCommonModules } from '@affine/core/modules';
import { configureAppTabsHeaderModule } from '@affine/core/modules/app-tabs-header';
@@ -22,7 +21,6 @@ import {
import { Telemetry } from '@affine/core/telemetry';
import createEmotionCache from '@affine/core/utils/create-emotion-cache';
import { createI18n, setUpLanguage } from '@affine/i18n';
import { SettingsIcon } from '@blocksuite/icons/rc';
import { CacheProvider } from '@emotion/react';
import {
Framework,
@@ -127,14 +125,3 @@ export function App() {
</Suspense>
);
}
registerAffineCommand({
id: 'affine:reload',
category: 'affine:general',
label: 'Reload current tab',
icon: <SettingsIcon />,
keyBinding: '$mod+R',
run() {
location.reload();
},
});

View File

@@ -7,6 +7,7 @@ import {
addTab,
closeTab,
initAndShowMainWindow,
reloadView,
showDevTools,
showMainWindow,
undoCloseTab,
@@ -92,8 +93,13 @@ export function createApplicationMenu() {
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{
label: 'Reload',
accelerator: 'CommandOrControl+R',
click() {
reloadView().catch(console.error);
},
},
{
label: 'Open devtools',
accelerator: isMac ? 'Cmd+Option+I' : 'Ctrl+Shift+I',

View File

@@ -23,6 +23,7 @@ import {
showTab,
showTabContextMenu,
updateWorkbenchMeta,
updateWorkbenchViewMeta,
} from '../windows-manager';
import { getChallengeResponse } from './challenge';
import { uiSubjects } from './subject';
@@ -168,6 +169,12 @@ export const uiHandlers = {
) => {
return updateWorkbenchMeta(...args);
},
updateWorkbenchViewMeta: async (
_,
...args: Parameters<typeof updateWorkbenchViewMeta>
) => {
return updateWorkbenchViewMeta(...args);
},
getTabViewsMeta: async () => {
return getTabViewsMeta();
},

View File

@@ -1,8 +1,8 @@
import { z } from 'zod';
export const workbenchViewModuleSchema = z.enum([
export const workbenchViewIconNameSchema = z.enum([
'trash',
'all',
'allDocs',
'collection',
'tag',
'doc', // refers to a doc whose mode is not yet being resolved
@@ -22,7 +22,7 @@ export const workbenchViewMetaSchema = z.object({
.optional(),
// todo: move title/module to cached stated
title: z.string().optional(),
moduleName: workbenchViewModuleSchema.optional(),
iconName: workbenchViewIconNameSchema.optional(),
});
export const workbenchMetaSchema = z.object({
@@ -42,4 +42,4 @@ export const TabViewsMetaKey = 'tabViewsMetaSchema';
export type TabViewsMetaSchema = z.infer<typeof tabViewsMetaSchema>;
export type WorkbenchMeta = z.infer<typeof workbenchMetaSchema>;
export type WorkbenchViewMeta = z.infer<typeof workbenchViewMetaSchema>;
export type WorkbenchViewModule = z.infer<typeof workbenchViewModuleSchema>;
export type WorkbenchViewModule = z.infer<typeof workbenchViewIconNameSchema>;

View File

@@ -258,15 +258,50 @@ export class WebContentViewsManager {
if (index === -1) {
return;
}
const workbench = workbenches[index];
const newWorkbenches = workbenches.toSpliced(index, 1, {
...workbenches[index],
...workbench,
...patch,
views: patch.views
? patch.views.map(v => {
const existing = workbench.views.find(e => e.id === v.id);
return {
...existing,
...v,
};
})
: workbench.views,
});
this.patchTabViewsMeta({
workbenches: newWorkbenches,
});
};
updateWorkbenchViewMeta = (
workbenchId: string,
viewId: string,
patch: Partial<WorkbenchViewMeta>
) => {
const workbench = this.tabViewsMeta.workbenches.find(
w => w.id === workbenchId
);
if (!workbench) {
return;
}
const views = workbench.views;
const viewIndex = views.findIndex(v => v.id === viewId);
if (viewIndex === -1) {
return;
}
const newViews = views.toSpliced(viewIndex, 1, {
...views[viewIndex],
...patch,
});
this.updateWorkbenchMeta(workbenchId, {
views: newViews,
});
};
isActiveTab = (id: string) => {
return this.activeWorkbenchId === id;
};
@@ -345,10 +380,18 @@ export class WebContentViewsManager {
addTab = async (option?: AddTabOption) => {
if (!option) {
const activeWorkbench = this.activeWorkbenchMeta;
const basename = (activeWorkbench?.basename ?? '') + '/';
option = {
basename: '/',
basename,
view: {
title: 'New Tab',
path: basename.startsWith('/workspace')
? {
pathname: 'all',
}
: undefined,
},
};
}
@@ -393,18 +436,18 @@ export class WebContentViewsManager {
let view = this.tabViewsMap.get(id);
if (!view) {
view = await this.createAndAddView('app', id);
const workbench = this.tabViewsMeta.workbenches.find(w => w.id === id);
const viewMeta = workbench?.views[workbench.activeViewIndex];
if (workbench && viewMeta) {
const url = new URL(
workbench.basename + (viewMeta.path?.pathname ?? ''),
mainWindowOrigin
);
url.hash = viewMeta.path?.hash ?? '';
url.search = viewMeta.path?.search ?? '';
logger.info(`loading tab ${id} at ${url.href}`);
view.webContents.loadURL(url.href).catch(logger.error);
}
}
const workbench = this.tabViewsMeta.workbenches.find(w => w.id === id);
const viewMeta = workbench?.views[workbench.activeViewIndex];
if (workbench && viewMeta) {
const url = new URL(
workbench.basename + (viewMeta.path?.pathname ?? ''),
mainWindowOrigin
);
url.hash = viewMeta.path?.hash ?? '';
url.search = viewMeta.path?.search ?? '';
logger.info(`loading tab ${id} at ${url.href}`);
view.webContents.loadURL(url.href).catch(logger.error);
}
return view;
};
@@ -835,6 +878,19 @@ export const updateWorkbenchMeta = (
) => {
WebContentViewsManager.instance.updateWorkbenchMeta(id, meta);
};
export const updateWorkbenchViewMeta = (
workbenchId: string,
viewId: string,
meta: Partial<WorkbenchViewMeta>
) => {
WebContentViewsManager.instance.updateWorkbenchViewMeta(
workbenchId,
viewId,
meta
);
};
export const getWorkbenchMeta = (id: string) => {
return TabViewsMetaState.value.workbenches.find(w => w.id === id);
};
@@ -851,6 +907,13 @@ export const closeTab = WebContentViewsManager.instance.closeTab;
export const undoCloseTab = WebContentViewsManager.instance.undoCloseTab;
export const activateView = WebContentViewsManager.instance.activateView;
export const reloadView = async () => {
const id = WebContentViewsManager.instance.activeWorkbenchId;
if (id) {
await WebContentViewsManager.instance.loadTab(id);
}
};
export const onTabAction = (fn: (event: TabAction) => void) => {
const { unsubscribe } =
WebContentViewsManager.instance.tabAction$.subscribe(fn);