feat: expose services in preload script

This commit is contained in:
tiddlygit-test 2021-01-20 23:20:23 +08:00
parent a0e876ed3e
commit c02fb95809
5 changed files with 82 additions and 7 deletions

View file

@ -20,8 +20,6 @@ import i18n from './i18n';
import AppWrapper from './components/app-wrapper';
import getWorkspacesAsList from './helpers/get-workspaces-as-list';
const DialogAbout = React.lazy(async () => await import('./components/dialog-about'));
const DialogAddWorkspace = React.lazy(async () => await import('./components/dialog-add-workspace'));
const DialogAuth = React.lazy(async () => await import('./components/dialog-auth'));
@ -39,9 +37,6 @@ const Main = React.lazy(async () => await import('./components/main'));
declare global {
interface Window {
meta: {
windowName: string;
};
remote: any;
preventClosingWindow: boolean;
}

View file

@ -0,0 +1,72 @@
/**
* Provide API from main services to GUI (for example, preference window), and tiddlywiki
* This file should be required by BrowserView's preload script to work
*/
import { contextBridge } from 'electron';
import serviceIdentifier from '@services/serviceIdentifier';
import { IAuthenticationService, Authentication } from '@services/auth';
import { IGitService, Git } from '@services/git';
import { IMenuService, MenuService } from '@services/menu';
import { INotificationService, Notification } from '@services/notifications';
import { IPreferenceService, Preference } from '@services/preferences';
import { ISystemPreferenceService, SystemPreference } from '@services/systemPreferences';
import { IUpdaterService, Updater } from '@services/updater';
import { IViewService, View } from '@services/view';
import { IWikiService, Wiki } from '@services/wiki';
import { IWikiGitWorkspaceService, WikiGitWorkspace } from '@services/wikiGitWorkspace';
import { IWindowService, Window } from '@services/windows';
import { IWorkspaceService, Workspace } from '@services/workspaces';
import { IWorkspaceViewService, WorkspaceView } from '@services/workspacesView';
import { container } from '@services/container';
const authService = container.get<IAuthenticationService>(serviceIdentifier.Authentication);
const gitService = container.get<IGitService>(serviceIdentifier.Git);
const menuService = container.get<IMenuService>(serviceIdentifier.MenuService);
const notificationService = container.get<INotificationService>(serviceIdentifier.Notification);
const preferenceService = container.get<IPreferenceService>(serviceIdentifier.Preference);
const systemPreferenceService = container.get<ISystemPreferenceService>(serviceIdentifier.SystemPreference);
const viewService = container.get<IViewService>(serviceIdentifier.View);
const updaterService = container.get<IUpdaterService>(serviceIdentifier.Updater);
const wikiService = container.get<IWikiService>(serviceIdentifier.Wiki);
const wikiGitWorkspaceService = container.get<IWikiGitWorkspaceService>(serviceIdentifier.WikiGitWorkspace);
const windowService = container.get<IWindowService>(serviceIdentifier.Window);
const workspaceService = container.get<IWorkspaceService>(serviceIdentifier.Workspace);
const workspaceViewService = container.get<IWorkspaceViewService>(serviceIdentifier.WorkspaceView);
contextBridge.exposeInMainWorld('service', {
auth: authService,
git: gitService,
menu: menuService,
notification: notificationService,
preference: preferenceService,
systemPreference: systemPreferenceService,
updater: updaterService,
view: viewService,
wiki: wikiService,
wikiGitWorkspace: wikiGitWorkspaceService,
window: windowService,
workspace: workspaceService,
workspaceView: workspaceViewService,
});
declare global {
interface Window {
service: {
auth: IAuthenticationService;
git: IGitService;
menu: IMenuService;
notification: INotificationService;
preference: IPreferenceService;
systemPreference: ISystemPreferenceService;
updater: IUpdaterService;
view: IViewService;
wiki: IWikiService;
wikiGitWorkspace: IWikiGitWorkspaceService;
window: IWindowService;
workspace: IWorkspaceService;
workspaceView: IWorkspaceViewService;
};
}
}

View file

@ -4,6 +4,7 @@ import './common/i18n';
import './common/require-nodejs';
import './common/simple-context-menu';
import './common/authing-postmessage';
import './common/services';
import { MetaDataChannel } from '@/constants/channels';
import { WindowNames } from '@services/windows/WindowProperties';
@ -16,6 +17,13 @@ contextBridge.exposeInMainWorld('meta', browserViewMetaData);
ipcRenderer.on(MetaDataChannel.getViewMetaData, (event) => {
event.returnValue = browserViewMetaData;
});
declare global {
interface Window {
meta: {
windowName: WindowNames;
};
}
}
if (windowName === WindowNames.view) {
void import('./view');

View file

@ -1,6 +1,6 @@
/**
* Provide API from electron to tiddlywiki
* This file should be required by BrowserView's preload script to work
* This file should be required by view.ts preload script to work
*/
import { contextBridge } from 'electron';

View file

@ -1,6 +1,6 @@
/**
* Call tiddlywiki api from electron
* This file should be required by BrowserView's preload script to work
* This file should be required by view.ts preload script to work
*/
import { ipcRenderer, webFrame } from 'electron';
import Promise from 'bluebird';