Files
flipper/desktop/flipper-ui-browser/src/initializeRenderHost.tsx
Michel Weststrate 86995e0d11 Introduce static resource URLs
Summary: Introduced an API that converts a filepath, relatively to the `desktop/static/` folder in a url that can be resolved by the render environment. This will generate `file://` urls in Electron, and root relative `/` urls in browser envs

Reviewed By: aigoncharov

Differential Revision: D32767427

fbshipit-source-id: 378da7709bcb19449873358a8703b9c5a5809c57
2021-12-08 04:30:57 -08:00

87 lines
2.3 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {FlipperServer, FlipperServerConfig} from 'flipper-common';
import {getRenderHostInstance, RenderHost} from 'flipper-ui-core';
export function initializeRenderHost(
flipperServer: FlipperServer,
flipperServerConfig: FlipperServerConfig,
) {
window.FlipperRenderHostInstance = {
readTextFromClipboard() {
// TODO:
return undefined;
},
writeTextToClipboard(_text: string) {
// TODO:
},
async importFile() {
throw new Error('Not implemented');
},
async exportFile() {
throw new Error('Not implemented');
},
openLink(url: string) {
window.open(url, '_blank');
},
registerShortcut(_shortcut, _callback) {
// TODO:
return () => {};
},
hasFocus() {
return document.hasFocus();
},
onIpcEvent(event) {
console.warn('onIpcEvent not available', event);
},
sendIpcEvent(event, ..._args: any[]) {
console.warn('sendIpcEvent not available', event);
},
shouldUseDarkColors() {
return !!(
window.flipperConfig.theme === 'dark' ||
(window.flipperConfig.theme === 'system' &&
window.matchMedia?.('(prefers-color-scheme: dark)'))
);
},
restartFlipper() {
// TODO: restart server as well
window.location.reload();
},
loadDefaultPlugins: getDefaultPluginsIndex,
serverConfig: flipperServerConfig,
GK(gatekeeper) {
return flipperServerConfig.gatekeepers[gatekeeper] ?? false;
},
flipperServer,
async requirePlugin(path) {
// TODO: use `await import(path)`?
const source = await getRenderHostInstance().flipperServer.exec(
'plugin-source',
path,
);
// eslint-disable-next-line no-eval
return eval(source);
},
getStaticResourceUrl(path): string {
// the 'static' folder is mounted as static middleware in Express at the root
return '/' + path;
},
} as RenderHost;
}
function getDefaultPluginsIndex() {
// TODO:
return {};
// eslint-disable-next-line import/no-unresolved
// const index = require('../defaultPlugins');
// return index.default || index;
}