diff --git a/src/preload/wikiOperation.ts b/src/preload/wikiOperation.ts index 5e6ed43c..d9e65006 100644 --- a/src/preload/wikiOperation.ts +++ b/src/preload/wikiOperation.ts @@ -51,7 +51,7 @@ ipcRenderer.on(WikiChannel.addTiddler, async (event, title: string, text: string `); // wait for fs to be settle setTimeout(() => { - ipcRenderer.invoke(WikiChannel.addTiddlerDone); + void ipcRenderer.invoke(WikiChannel.addTiddlerDone); }, 1000); }); // get tiddler text diff --git a/src/services/view/index.ts b/src/services/view/index.ts index 2e5249f1..8334030c 100644 --- a/src/services/view/index.ts +++ b/src/services/view/index.ts @@ -303,7 +303,7 @@ export class View implements IViewService { } // fix some case that local ip can't be load // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - const urlToReplace = (rememberLastPageVisited && workspace.lastUrl) || workspace.homeUrl; + const urlToReplace = rememberLastPageVisited ? workspace.lastUrl ?? workspace.homeUrl : workspace.homeUrl; const portReplacedUrl = replaceUrlPortWithSettingPort(urlToReplace, workspace.port); const hostReplacedUrl = await getLocalHostUrlWithActualIP(portReplacedUrl); logger.debug(`Load initialUrl: ${hostReplacedUrl} for windowName ${windowName} for workspace ${workspace.name}`, { diff --git a/src/services/view/interface.ts b/src/services/view/interface.ts index 80484f6c..9664c8df 100644 --- a/src/services/view/interface.ts +++ b/src/services/view/interface.ts @@ -9,6 +9,9 @@ import { WindowNames } from '@services/windows/WindowProperties'; * BrowserView related things, the BrowserView is the webview like frame that renders our wiki website. */ export interface IViewService { + /** + * Add a new browserView and load the url + */ addView: (workspace: IWorkspace, windowName: WindowNames) => Promise; addViewForAllBrowserViews(workspace: IWorkspace): Promise; /** @@ -34,6 +37,12 @@ export interface IViewService { reloadViewsWebContentsIfDidFailLoad: () => Promise; removeAllViewOfWorkspace: (workspaceID: string) => void; removeView: (workspaceID: string, windowName: WindowNames) => void; + /** + * Bring an already created view to the front. If it happened to not created, will call `addView()` to create one. + * @param workspaceID id, can only be main workspace id, because only main workspace will have view created. + * @param windowName you can control main window or menubar window to have this view. + * @returns + */ setActiveView: (workspaceID: string, windowName: WindowNames) => Promise; setActiveViewForAllBrowserViews(workspaceID: string): Promise; setViewsAudioPref: (_shouldMuteAudio?: boolean) => void; diff --git a/src/services/wiki/wikiOperations.ts b/src/services/wiki/wikiOperations.ts index 0b8eb1c6..a7af4215 100644 --- a/src/services/wiki/wikiOperations.ts +++ b/src/services/wiki/wikiOperations.ts @@ -5,6 +5,11 @@ import { IViewService } from '@services/view/interface'; import { IWindowService } from '@services/windows/interface'; import { WindowNames } from '@services/windows/WindowProperties'; +function sendToMainWindow(type: WikiChannel, workspaceID: string, message: string): void { + const viewService = container.get(serviceIdentifier.View); + const browserView = viewService.getView(workspaceID, WindowNames.main); + browserView?.webContents?.send?.(type, message); +} /** * Handle sending message to trigger operations defined in `src/preload/wikiOperation.ts` */ @@ -14,20 +19,9 @@ export const wikiOperations = { const createWorkspaceWindow = windowService.get(WindowNames.addWorkspace); createWorkspaceWindow?.webContents?.send(WikiChannel.createProgress, message); }, - [WikiChannel.syncProgress]: (workspaceID: string, message: string): void => { - const viewService = container.get(serviceIdentifier.View); - const browserView = viewService.getView(workspaceID, WindowNames.main); - if (browserView !== undefined) { - browserView.webContents.send(WikiChannel.syncProgress, message); - } - }, - [WikiChannel.generalNotification]: (workspaceID: string, message: string): void => { - const viewService = container.get(serviceIdentifier.View); - const browserView = viewService.getView(workspaceID, WindowNames.main); - if (browserView !== undefined) { - browserView.webContents.send(WikiChannel.generalNotification, message); - } - }, + [WikiChannel.syncProgress]: (workspaceID: string, message: string): void => sendToMainWindow(WikiChannel.syncProgress, workspaceID, message), + [WikiChannel.generalNotification]: (workspaceID: string, message: string): void => sendToMainWindow(WikiChannel.generalNotification, workspaceID, message), + [WikiChannel.openTiddler]: (workspaceID: string, tiddlerName: string): void => sendToMainWindow(WikiChannel.openTiddler, workspaceID, tiddlerName), // TODO: add more operations here from `src/preload/wikiOperation.ts` }; export type IWikiOperations = typeof wikiOperations; diff --git a/src/services/workspacesView/index.ts b/src/services/workspacesView/index.ts index d96d7965..618defff 100644 --- a/src/services/workspacesView/index.ts +++ b/src/services/workspacesView/index.ts @@ -287,7 +287,19 @@ export class WorkspaceView implements IWorkspaceViewService { if (newWorkspace === undefined) { throw new Error(`Workspace id ${nextWorkspaceID} does not exist. When setActiveWorkspaceView().`); } - logger.debug(`Set active workspace oldActiveWorkspace.id: ${oldActiveWorkspace?.id ?? 'undefined'} nextWorkspaceID: ${nextWorkspaceID}`); + logger.debug( + `Set active workspace oldActiveWorkspace.id: ${oldActiveWorkspace?.id ?? 'undefined'} nextWorkspaceID: ${nextWorkspaceID} newWorkspace.isSubWiki ${String( + newWorkspace.isSubWiki, + )}`, + ); + if (newWorkspace.isSubWiki && typeof newWorkspace.mainWikiID === 'string') { + logger.debug(`${nextWorkspaceID} is a subwiki, set its main wiki ${newWorkspace.mainWikiID} to active instead.`); + await this.setActiveWorkspaceView(newWorkspace.mainWikiID); + if (typeof newWorkspace.tagName === 'string') { + this.wikiService.wikiOperation(WikiChannel.openTiddler, newWorkspace.mainWikiID, newWorkspace.tagName); + } + return; + } // later process will use the current active workspace await this.workspaceService.setActiveWorkspace(nextWorkspaceID, oldActiveWorkspace?.id); const asyncTasks: Array> = []; diff --git a/src/services/workspacesView/interface.ts b/src/services/workspacesView/interface.ts index 5bed1303..d0239fc2 100644 --- a/src/services/workspacesView/interface.ts +++ b/src/services/workspacesView/interface.ts @@ -46,6 +46,10 @@ export interface IWorkspaceViewService { */ removeWorkspaceView(workspaceID: string): Promise; restartWorkspaceViewService(workspaceID?: string | undefined): Promise; + /** + * If is main workspace, set workspace to active and load the url. + * If is sub workspace, just load url with #tag for its main workspace. + */ setActiveWorkspaceView(workspaceID: string): Promise; setWorkspaceView(workspaceID: string, workspaceOptions: IWorkspace): Promise; setWorkspaceViews(workspaces: Record): Promise;