Introduce menu entry support

Summary:
[interesting] since it shows how Flipper APIs are exposed through sandy. However, the next diff is a much simpler example of that

This diff adds support for adding menu entries for sandy plugin (renamed keyboard actions to menus, as it always creates a menu entry, but not necessarily a keyboard shortcut)

```

  client.addMenuEntry(
    // custom entry
    {
      label: 'Reset Selection',
      topLevelMenu: 'Edit',
      handler: () => {
        selectedID.set(null);
      },
    },
    // based on built-in action (sets standard label, shortcut)
    {
      action: 'createPaste',
      handler: () => {
        console.log('creating paste');
      },
    },
  );
```

Most of this diff is introducing the concept of FlipperUtils, a set of static Flipper methods (not related to a device or client) that can be used from Sandy. This will for example be used to implement things as `createPaste` as well

Reviewed By: nikoant

Differential Revision: D22766990

fbshipit-source-id: ce90af3b700e6c3d9a779a3bab4673ba356f3933
This commit is contained in:
Michel Weststrate
2020-08-04 07:44:56 -07:00
committed by Facebook GitHub Bot
parent 94eaaf5dca
commit 9c202a4a10
21 changed files with 335 additions and 48 deletions

View File

@@ -40,6 +40,7 @@ import {debounce} from 'lodash';
import {batch} from 'react-redux'; import {batch} from 'react-redux';
import {SandyPluginInstance} from 'flipper-plugin'; import {SandyPluginInstance} from 'flipper-plugin';
import {flipperMessagesClientPlugin} from './utils/self-inspection/plugins/FlipperMessagesClientPlugin'; import {flipperMessagesClientPlugin} from './utils/self-inspection/plugins/FlipperMessagesClientPlugin';
import {getFlipperLibImplementation} from './utils/flipperLibImplementation';
type Plugins = Array<string>; type Plugins = Array<string>;
@@ -310,7 +311,12 @@ export default class Client extends EventEmitter {
// TODO: needs to be wrapped in error tracking T68955280 // TODO: needs to be wrapped in error tracking T68955280
this.sandyPluginStates.set( this.sandyPluginStates.set(
plugin.id, plugin.id,
new SandyPluginInstance(this, plugin, initialStates[pluginId]), new SandyPluginInstance(
getFlipperLibImplementation(),
plugin,
this,
initialStates[pluginId],
),
); );
} }
}); });
@@ -355,7 +361,7 @@ export default class Client extends EventEmitter {
// TODO: needs to be wrapped in error tracking T68955280 // TODO: needs to be wrapped in error tracking T68955280
this.sandyPluginStates.set( this.sandyPluginStates.set(
plugin.id, plugin.id,
new SandyPluginInstance(this, plugin), new SandyPluginInstance(getFlipperLibImplementation(), plugin, this),
); );
} }
} }

View File

@@ -26,8 +26,9 @@ import electron, {MenuItemConstructorOptions} from 'electron';
import {notNull} from './utils/typeUtils'; import {notNull} from './utils/typeUtils';
import constants from './fb-stubs/constants'; import constants from './fb-stubs/constants';
import {Logger} from './fb-interfaces/Logger'; import {Logger} from './fb-interfaces/Logger';
import {NormalizedMenuEntry, buildInMenuEntries} from 'flipper-plugin';
export type DefaultKeyboardAction = 'clear' | 'goToBottom' | 'createPaste'; export type DefaultKeyboardAction = keyof typeof buildInMenuEntries;
export type TopLevelMenu = 'Edit' | 'View' | 'Window' | 'Help'; export type TopLevelMenu = 'Edit' | 'View' | 'Window' | 'Help';
export type KeyboardAction = { export type KeyboardAction = {
@@ -37,26 +38,6 @@ export type KeyboardAction = {
topLevelMenu: TopLevelMenu; topLevelMenu: TopLevelMenu;
}; };
const defaultKeyboardActions: Array<KeyboardAction> = [
{
label: 'Clear',
accelerator: 'CmdOrCtrl+K',
topLevelMenu: 'View',
action: 'clear',
},
{
label: 'Go To Bottom',
accelerator: 'CmdOrCtrl+B',
topLevelMenu: 'View',
action: 'goToBottom',
},
{
label: 'Create Paste',
topLevelMenu: 'Edit',
action: 'createPaste',
},
];
export type KeyboardActions = Array<DefaultKeyboardAction | KeyboardAction>; export type KeyboardActions = Array<DefaultKeyboardAction | KeyboardAction>;
const menuItems: Map<string, electron.MenuItem> = new Map(); const menuItems: Map<string, electron.MenuItem> = new Map();
@@ -82,14 +63,12 @@ export function setupMenuBar(
logger, logger,
); );
// collect all keyboard actions from all plugins // collect all keyboard actions from all plugins
const registeredActions: Set<KeyboardAction> = new Set( const registeredActions = new Set(
plugins plugins
.map((plugin) => plugin.keyboardActions || []) .map((plugin) => plugin.keyboardActions || [])
.reduce((acc: KeyboardActions, cv) => acc.concat(cv), []) .flat()
.map((action: DefaultKeyboardAction | KeyboardAction) => .map((action: DefaultKeyboardAction | KeyboardAction) =>
typeof action === 'string' typeof action === 'string' ? buildInMenuEntries[action] : action,
? defaultKeyboardActions.find((a) => a.action === action)
: action,
) )
.filter(notNull), .filter(notNull),
); );
@@ -97,7 +76,7 @@ export function setupMenuBar(
// add keyboard actions to // add keyboard actions to
registeredActions.forEach((keyboardAction) => { registeredActions.forEach((keyboardAction) => {
if (keyboardAction != null) { if (keyboardAction != null) {
appendMenuItem(template, actionHandler, keyboardAction); appendMenuItem(template, keyboardAction);
} }
}); });
@@ -126,7 +105,6 @@ export function setupMenuBar(
function appendMenuItem( function appendMenuItem(
template: Array<MenuItemConstructorOptions>, template: Array<MenuItemConstructorOptions>,
actionHandler: (action: string) => void,
item: KeyboardAction, item: KeyboardAction,
) { ) {
const keyboardAction = item; const keyboardAction = item;
@@ -183,6 +161,49 @@ export function activateMenuItems(
); );
} }
export function addSandyPluginEntries(entries: NormalizedMenuEntry[]) {
if (!electron.remote.Menu) {
return;
}
// disable all keyboard actions
for (const item of menuItems) {
item[1].enabled = false;
}
pluginActionHandler = (action: string) => {
entries.find((e) => e.action === action)?.handler();
};
let changedItems = false;
const currentMenu = electron.remote.Menu.getApplicationMenu();
for (const entry of entries) {
const item = menuItems.get(entry.action!);
if (item) {
item.enabled = true;
item.accelerator = entry.accelerator;
} else {
const parent = currentMenu?.items.find(
(i) => i.label === entry.topLevelMenu,
);
if (parent) {
const item = new electron.remote.MenuItem({
enabled: true,
click: () => pluginActionHandler?.(entry.action!),
label: entry.label,
accelerator: entry.accelerator,
});
parent.submenu!.append(item);
menuItems.set(entry.action!, item);
changedItems = true;
}
}
}
if (changedItems) {
electron.remote.Menu.setApplicationMenu(currentMenu);
}
}
function getTemplate( function getTemplate(
app: electron.App, app: electron.App,
shell: electron.Shell, shell: electron.Shell,

View File

@@ -12,7 +12,7 @@ import adb, {Client as ADBClient} from 'adbkit';
import {Priority} from 'adbkit-logcat'; import {Priority} from 'adbkit-logcat';
import ArchivedDevice from './ArchivedDevice'; import ArchivedDevice from './ArchivedDevice';
import {createWriteStream} from 'fs'; import {createWriteStream} from 'fs';
import {LogLevel, DeviceType} from 'flipper-plugin'; import type {LogLevel, DeviceType} from 'flipper-plugin';
const DEVICE_RECORDING_DIR = '/sdcard/flipper_recorder'; const DEVICE_RECORDING_DIR = '/sdcard/flipper_recorder';

View File

@@ -7,8 +7,8 @@
* @format * @format
*/ */
import {DeviceLogEntry, DeviceType} from 'flipper-plugin';
import BaseDevice from './BaseDevice'; import BaseDevice from './BaseDevice';
import type {DeviceLogEntry, DeviceType} from 'flipper-plugin';
import {OS, DeviceShell} from './BaseDevice'; import {OS, DeviceShell} from './BaseDevice';
import {SupportFormRequestDetailsState} from '../reducers/supportForm'; import {SupportFormRequestDetailsState} from '../reducers/supportForm';

View File

@@ -8,7 +8,7 @@
*/ */
import stream from 'stream'; import stream from 'stream';
import {DeviceLogListener} from 'flipper'; import type {DeviceLogListener} from 'flipper';
import {sortPluginsByName} from '../utils/pluginUtils'; import {sortPluginsByName} from '../utils/pluginUtils';
import { import {
DeviceLogEntry, DeviceLogEntry,
@@ -16,7 +16,8 @@ import {
SandyPluginDefinition, SandyPluginDefinition,
DeviceType, DeviceType,
} from 'flipper-plugin'; } from 'flipper-plugin';
import {DevicePluginMap, FlipperDevicePlugin} from '../plugin'; import type {DevicePluginMap, FlipperDevicePlugin} from '../plugin';
import {getFlipperLibImplementation} from '../utils/flipperLibImplementation';
export type DeviceShell = { export type DeviceShell = {
stdout: stream.Readable; stdout: stream.Readable;
@@ -180,7 +181,11 @@ export default class BaseDevice {
this.devicePlugins.push(plugin.id); this.devicePlugins.push(plugin.id);
this.sandyPluginStates.set( this.sandyPluginStates.set(
plugin.id, plugin.id,
new SandyDevicePluginInstance(this, plugin), new SandyDevicePluginInstance(
getFlipperLibImplementation(),
plugin,
this,
),
); // TODO T70582933: pass initial state if applicable ); // TODO T70582933: pass initial state if applicable
} }
} else { } else {

View File

@@ -8,7 +8,7 @@
*/ */
import BaseDevice, {OS} from './BaseDevice'; import BaseDevice, {OS} from './BaseDevice';
import {DeviceType} from 'flipper-plugin'; import type {DeviceType} from 'flipper-plugin';
export default class FlipperSelfInspectionDevice extends BaseDevice { export default class FlipperSelfInspectionDevice extends BaseDevice {
constructor(serial: string, deviceType: DeviceType, title: string, os: OS) { constructor(serial: string, deviceType: DeviceType, title: string, os: OS) {

View File

@@ -7,7 +7,7 @@
* @format * @format
*/ */
import {LogLevel, DeviceLogEntry, DeviceType} from 'flipper-plugin'; import type {LogLevel, DeviceLogEntry, DeviceType} from 'flipper-plugin';
import child_process, {ChildProcess} from 'child_process'; import child_process, {ChildProcess} from 'child_process';
import BaseDevice from './BaseDevice'; import BaseDevice from './BaseDevice';
import JSONStream from 'JSONStream'; import JSONStream from 'JSONStream';

View File

@@ -11,7 +11,7 @@ import {ChildProcess} from 'child_process';
import {Store} from '../reducers/index'; import {Store} from '../reducers/index';
import {setXcodeDetected} from '../reducers/application'; import {setXcodeDetected} from '../reducers/application';
import {Logger} from '../fb-interfaces/Logger'; import {Logger} from '../fb-interfaces/Logger';
import {DeviceType} from 'flipper-plugin'; import type {DeviceType} from 'flipper-plugin';
import {promisify} from 'util'; import {promisify} from 'util';
import path from 'path'; import path from 'path';
import child_process from 'child_process'; import child_process from 'child_process';

View File

@@ -10,6 +10,7 @@
import {Provider} from 'react-redux'; import {Provider} from 'react-redux';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import {useState, useEffect} from 'react'; import {useState, useEffect} from 'react';
import ContextMenuProvider from './ui/components/ContextMenuProvider'; import ContextMenuProvider from './ui/components/ContextMenuProvider';
import GK from './fb-stubs/GK'; import GK from './fb-stubs/GK';
import {init as initLogger} from './fb-stubs/Logger'; import {init as initLogger} from './fb-stubs/Logger';
@@ -36,6 +37,7 @@ import {enableMapSet} from 'immer';
import os from 'os'; import os from 'os';
import QuickPerformanceLogger, {FLIPPER_QPL_EVENTS} from './fb-stubs/QPL'; import QuickPerformanceLogger, {FLIPPER_QPL_EVENTS} from './fb-stubs/QPL';
import {PopoverProvider} from './ui/components/PopoverProvider'; import {PopoverProvider} from './ui/components/PopoverProvider';
import {initializeFlipperLibImplementation} from './utils/flipperLibImplementation';
if (process.env.NODE_ENV === 'development' && os.platform() === 'darwin') { if (process.env.NODE_ENV === 'development' && os.platform() === 'darwin') {
// By default Node.JS has its internal certificate storage and doesn't use // By default Node.JS has its internal certificate storage and doesn't use
@@ -111,6 +113,7 @@ function setProcessState(store: Store) {
} }
function init() { function init() {
initializeFlipperLibImplementation(store, logger);
ReactDOM.render(<AppFrame />, document.getElementById('root')); ReactDOM.render(<AppFrame />, document.getElementById('root'));
initLauncherHooks(config(), store); initLauncherHooks(config(), store);
const sessionId = store.getState().application.sessionId; const sessionId = store.getState().application.sessionId;

View File

@@ -25,7 +25,7 @@ import SupportRequestFormV2 from '../fb-stubs/SupportRequestFormV2';
import SupportRequestDetails from '../fb-stubs/SupportRequestDetails'; import SupportRequestDetails from '../fb-stubs/SupportRequestDetails';
import {getPluginKey, isDevicePluginDefinition} from '../utils/pluginUtils'; import {getPluginKey, isDevicePluginDefinition} from '../utils/pluginUtils';
import {deconstructClientId} from '../utils/clientUtils'; import {deconstructClientId} from '../utils/clientUtils';
import {FlipperDevicePlugin, PluginDefinition, isSandyPlugin} from '../plugin'; import {PluginDefinition} from '../plugin';
import {RegisterPluginAction} from './plugins'; import {RegisterPluginAction} from './plugins';
export type StaticView = export type StaticView =

View File

@@ -16,6 +16,7 @@ import {
act as testingLibAct, act as testingLibAct,
} from '@testing-library/react'; } from '@testing-library/react';
import {queries} from '@testing-library/dom'; import {queries} from '@testing-library/dom';
import {TestUtils} from 'flipper-plugin';
import { import {
selectPlugin, selectPlugin,
@@ -36,6 +37,7 @@ import {registerPlugins} from '../reducers/plugins';
import PluginContainer from '../PluginContainer'; import PluginContainer from '../PluginContainer';
import {getPluginKey, isDevicePluginDefinition} from '../utils/pluginUtils'; import {getPluginKey, isDevicePluginDefinition} from '../utils/pluginUtils';
import {getInstance} from '../fb-stubs/Logger'; import {getInstance} from '../fb-stubs/Logger';
import {setFlipperLibImplementation} from '../utils/flipperLibImplementation';
type MockFlipperResult = { type MockFlipperResult = {
client: Client; client: Client;
@@ -62,6 +64,8 @@ export async function createMockFlipperWithPlugin(
): Promise<MockFlipperResult> { ): Promise<MockFlipperResult> {
const store = createStore(rootReducer); const store = createStore(rootReducer);
const logger = getInstance(); const logger = getInstance();
setFlipperLibImplementation(TestUtils.createMockFlipperLib());
store.dispatch(registerPlugins([pluginClazz])); store.dispatch(registerPlugins([pluginClazz]));
function createDevice(serial: string): BaseDevice { function createDevice(serial: string): BaseDevice {

View File

@@ -0,0 +1,38 @@
/**
* 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 type {FlipperLib} from 'flipper-plugin';
import type {Logger} from '../fb-interfaces/Logger';
import type {Store} from '../reducers';
let flipperLibInstance: FlipperLib | undefined;
export function initializeFlipperLibImplementation(
_store: Store,
_logger: Logger,
) {
// late require to avoid cyclic dependency
const {addSandyPluginEntries} = require('../MenuBar');
flipperLibInstance = {
enableMenuEntries(entries) {
addSandyPluginEntries(entries);
},
};
}
export function getFlipperLibImplementation(): FlipperLib {
if (!flipperLibInstance) {
throw new Error('Flipper lib not instantiated');
}
return flipperLibInstance;
}
export function setFlipperLibImplementation(impl: FlipperLib) {
flipperLibInstance = impl;
}

View File

@@ -286,3 +286,45 @@ test('device plugins can receive deeplinks', async () => {
plugin.triggerDeepLink('test'); plugin.triggerDeepLink('test');
expect(plugin.instance.field1.get()).toBe('test'); expect(plugin.instance.field1.get()).toBe('test');
}); });
test('plugins can register menu entries', async () => {
const plugin = TestUtils.startPlugin({
plugin(client: PluginClient) {
const counter = createState(0);
client.addMenuEntry(
{
action: 'createPaste',
handler() {
counter.set(counter.get() + 1);
},
},
{
label: 'Custom Action',
topLevelMenu: 'Edit',
handler() {
counter.set(counter.get() + 3);
},
},
);
return {counter};
},
Component() {
return null;
},
});
expect(plugin.instance.counter.get()).toBe(0);
plugin.triggerDeepLink('test');
plugin.triggerMenuEntry('createPaste');
plugin.triggerMenuEntry('Custom Action');
expect(plugin.instance.counter.get()).toBe(4);
expect(plugin.flipperLib.enableMenuEntries).toBeCalledTimes(1);
plugin.deactivate();
expect(() => {
plugin.triggerMenuEntry('Non Existing');
}).toThrowErrorMatchingInlineSnapshot(
`"No menu entry found with action: Non Existing"`,
);
});

View File

@@ -7,6 +7,7 @@
* @format * @format
*/ */
import './plugin/PluginBase';
import * as TestUtilites from './test-utils/test-utils'; import * as TestUtilites from './test-utils/test-utils';
export { export {
@@ -26,6 +27,12 @@ export {SandyPluginDefinition} from './plugin/SandyPluginDefinition';
export {SandyPluginRenderer} from './plugin/PluginRenderer'; export {SandyPluginRenderer} from './plugin/PluginRenderer';
export {SandyPluginContext, usePlugin} from './plugin/PluginContext'; export {SandyPluginContext, usePlugin} from './plugin/PluginContext';
export {createState, useValue, Atom} from './state/atom'; export {createState, useValue, Atom} from './state/atom';
export {FlipperLib} from './plugin/FlipperLib';
export {
MenuEntry,
NormalizedMenuEntry,
buildInMenuEntries,
} from './plugin/MenuEntry';
// It's not ideal that this exists in flipper-plugin sources directly, // It's not ideal that this exists in flipper-plugin sources directly,
// but is the least pain for plugin authors. // but is the least pain for plugin authors.

View File

@@ -9,6 +9,7 @@
import {SandyPluginDefinition} from './SandyPluginDefinition'; import {SandyPluginDefinition} from './SandyPluginDefinition';
import {BasePluginInstance, BasePluginClient} from './PluginBase'; import {BasePluginInstance, BasePluginClient} from './PluginBase';
import {FlipperLib} from './FlipperLib';
export type DeviceLogListener = (entry: DeviceLogEntry) => void; export type DeviceLogListener = (entry: DeviceLogEntry) => void;
@@ -69,11 +70,12 @@ export class SandyDevicePluginInstance extends BasePluginInstance {
client: DevicePluginClient; client: DevicePluginClient;
constructor( constructor(
realDevice: RealFlipperDevice, flipperLib: FlipperLib,
definition: SandyPluginDefinition, definition: SandyPluginDefinition,
realDevice: RealFlipperDevice,
initialStates?: Record<string, any>, initialStates?: Record<string, any>,
) { ) {
super(definition, initialStates); super(flipperLib, definition, initialStates);
const device: Device = { const device: Device = {
// N.B. we model OS as string, not as enum, to make custom device types possible in the future // N.B. we model OS as string, not as enum, to make custom device types possible in the future
os: realDevice.os, os: realDevice.os,

View File

@@ -0,0 +1,17 @@
/**
* 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 {NormalizedMenuEntry} from './MenuEntry';
/**
* This interface exposes all global methods for which an implementation will be provided by Flipper itself
*/
export interface FlipperLib {
enableMenuEntries(menuEntries: NormalizedMenuEntry[]): void;
}

View File

@@ -0,0 +1,66 @@
/**
* 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
*/
export type DefaultKeyboardAction = 'clear' | 'goToBottom' | 'createPaste';
export type TopLevelMenu = 'Edit' | 'View' | 'Window' | 'Help';
export type MenuEntry = BuiltInMenuEntry | CustomMenuEntry;
export type NormalizedMenuEntry = {
label: string;
accelerator?: string;
topLevelMenu: TopLevelMenu;
handler: () => void;
action: string;
};
export type CustomMenuEntry = {
label: string;
accelerator?: string;
topLevelMenu: TopLevelMenu;
handler: () => void;
};
export type BuiltInMenuEntry = {
action: keyof typeof buildInMenuEntries;
handler: () => void;
};
export const buildInMenuEntries = {
clear: {
label: 'Clear',
accelerator: 'CmdOrCtrl+K',
topLevelMenu: 'View',
action: 'clear',
},
goToBottom: {
label: 'Go To Bottom',
accelerator: 'CmdOrCtrl+B',
topLevelMenu: 'View',
action: 'goToBottom',
},
createPaste: {
label: 'Create Paste',
topLevelMenu: 'Edit',
action: 'createPaste',
},
} as const;
export function normalizeMenuEntry(entry: MenuEntry): NormalizedMenuEntry;
export function normalizeMenuEntry(entry: any): NormalizedMenuEntry {
const builtInEntry:
| NormalizedMenuEntry
| undefined = (buildInMenuEntries as any)[entry.action];
return builtInEntry
? {...builtInEntry, ...entry}
: {
...entry,
action: entry.action || entry.label,
};
}

View File

@@ -9,6 +9,7 @@
import {SandyPluginDefinition} from './SandyPluginDefinition'; import {SandyPluginDefinition} from './SandyPluginDefinition';
import {BasePluginInstance, BasePluginClient} from './PluginBase'; import {BasePluginInstance, BasePluginClient} from './PluginBase';
import {FlipperLib} from './FlipperLib';
type EventsContract = Record<string, any>; type EventsContract = Record<string, any>;
type MethodsContract = Record<string, (params: any) => Promise<any>>; type MethodsContract = Record<string, (params: any) => Promise<any>>;
@@ -96,11 +97,12 @@ export class SandyPluginInstance extends BasePluginInstance {
connected = false; connected = false;
constructor( constructor(
realClient: RealFlipperClient, flipperLib: FlipperLib,
definition: SandyPluginDefinition, definition: SandyPluginDefinition,
realClient: RealFlipperClient,
initialStates?: Record<string, any>, initialStates?: Record<string, any>,
) { ) {
super(definition, initialStates); super(flipperLib, definition, initialStates);
this.realClient = realClient; this.realClient = realClient;
this.definition = definition; this.definition = definition;
this.client = { this.client = {

View File

@@ -10,6 +10,8 @@
import {SandyPluginDefinition} from './SandyPluginDefinition'; import {SandyPluginDefinition} from './SandyPluginDefinition';
import {EventEmitter} from 'events'; import {EventEmitter} from 'events';
import {Atom} from '../state/atom'; import {Atom} from '../state/atom';
import {MenuEntry, NormalizedMenuEntry, normalizeMenuEntry} from './MenuEntry';
import {FlipperLib} from './FlipperLib';
export interface BasePluginClient { export interface BasePluginClient {
/** /**
@@ -31,6 +33,11 @@ export interface BasePluginClient {
* Triggered when this plugin is opened through a deeplink * Triggered when this plugin is opened through a deeplink
*/ */
onDeepLink(cb: (deepLink: unknown) => void): void; onDeepLink(cb: (deepLink: unknown) => void): void;
/**
* Register menu entries in the Flipper toolbar
*/
addMenuEntry(...entry: MenuEntry[]): void;
} }
let currentPluginInstance: BasePluginInstance | undefined = undefined; let currentPluginInstance: BasePluginInstance | undefined = undefined;
@@ -46,6 +53,8 @@ export function getCurrentPluginInstance(): typeof currentPluginInstance {
} }
export abstract class BasePluginInstance { export abstract class BasePluginInstance {
/** generally available Flipper APIs */
flipperLib: FlipperLib;
/** the original plugin definition */ /** the original plugin definition */
definition: SandyPluginDefinition; definition: SandyPluginDefinition;
/** the plugin instance api as used inside components and such */ /** the plugin instance api as used inside components and such */
@@ -62,10 +71,14 @@ export abstract class BasePluginInstance {
// last seen deeplink // last seen deeplink
lastDeeplink?: any; lastDeeplink?: any;
menuEntries: NormalizedMenuEntry[] = [];
constructor( constructor(
flipperLib: FlipperLib,
definition: SandyPluginDefinition, definition: SandyPluginDefinition,
initialStates?: Record<string, any>, initialStates?: Record<string, any>,
) { ) {
this.flipperLib = flipperLib;
this.definition = definition; this.definition = definition;
this.initialStates = initialStates; this.initialStates = initialStates;
} }
@@ -95,6 +108,21 @@ export abstract class BasePluginInstance {
onDestroy: (cb) => { onDestroy: (cb) => {
this.events.on('destroy', cb); this.events.on('destroy', cb);
}, },
addMenuEntry: (...entries) => {
for (const entry of entries) {
const normalized = normalizeMenuEntry(entry);
if (
this.menuEntries.find(
(existing) =>
existing.label === normalized.label ||
existing.action === normalized.action,
)
) {
throw new Error(`Duplicate menu entry: '${normalized.label}'`);
}
this.menuEntries.push(normalizeMenuEntry(entry));
}
},
}; };
} }
@@ -103,6 +131,7 @@ export abstract class BasePluginInstance {
this.assertNotDestroyed(); this.assertNotDestroyed();
if (!this.activated) { if (!this.activated) {
this.activated = true; this.activated = true;
this.flipperLib.enableMenuEntries(this.menuEntries);
this.events.emit('activate'); this.events.emit('activate');
} }
} }
@@ -112,8 +141,8 @@ export abstract class BasePluginInstance {
return; return;
} }
if (this.activated) { if (this.activated) {
this.lastDeeplink = undefined;
this.activated = false; this.activated = false;
this.lastDeeplink = undefined;
this.events.emit('deactivate'); this.events.emit('deactivate');
} }
} }

View File

@@ -35,6 +35,7 @@ import {
DeviceLogListener, DeviceLogListener,
} from '../plugin/DevicePlugin'; } from '../plugin/DevicePlugin';
import {BasePluginInstance} from '../plugin/PluginBase'; import {BasePluginInstance} from '../plugin/PluginBase';
import {FlipperLib} from '../plugin/FlipperLib';
type Renderer = RenderResult<typeof queries>; type Renderer = RenderResult<typeof queries>;
@@ -61,6 +62,11 @@ type ExtractEventsType<
: never; : never;
interface BasePluginResult { interface BasePluginResult {
/**
* Mock for Flipper utilities
*/
flipperLib: FlipperLib;
/** /**
* Emulates the 'onActivate' event * Emulates the 'onActivate' event
*/ */
@@ -84,6 +90,11 @@ interface BasePluginResult {
* Grab all the persistable state * Grab all the persistable state
*/ */
exportState(): any; exportState(): any;
/**
* Trigger menu entry by label
*/
triggerMenuEntry(label: string): void;
} }
interface StartPluginResult<Module extends FlipperPluginModule<any>> interface StartPluginResult<Module extends FlipperPluginModule<any>>
@@ -165,8 +176,9 @@ export function startPlugin<Module extends FlipperPluginModule<any>>(
} }
const sendStub = jest.fn(); const sendStub = jest.fn();
const fakeFlipper: RealFlipperClient = { const flipperUtils = createMockFlipperLib();
isBackgroundPlugin() { const fakeFlipperClient: RealFlipperClient = {
isBackgroundPlugin(_pluginId: string) {
return !!options?.isBackgroundPlugin; return !!options?.isBackgroundPlugin;
}, },
initPlugin() { initPlugin() {
@@ -186,8 +198,9 @@ export function startPlugin<Module extends FlipperPluginModule<any>>(
}; };
const pluginInstance = new SandyPluginInstance( const pluginInstance = new SandyPluginInstance(
fakeFlipper, flipperUtils,
definition, definition,
fakeFlipperClient,
options?.initialState, options?.initialState,
); );
@@ -258,10 +271,12 @@ export function startDevicePlugin<Module extends FlipperDevicePluginModule>(
); );
} }
const flipperLib = createMockFlipperLib();
const testDevice = createMockDevice(options); const testDevice = createMockDevice(options);
const pluginInstance = new SandyDevicePluginInstance( const pluginInstance = new SandyDevicePluginInstance(
testDevice, flipperLib,
definition, definition,
testDevice,
options?.initialState, options?.initialState,
); );
@@ -306,10 +321,17 @@ export function renderDevicePlugin<Module extends FlipperDevicePluginModule>(
}; };
} }
export function createMockFlipperLib(): FlipperLib {
return {
enableMenuEntries: jest.fn(),
};
}
function createBasePluginResult( function createBasePluginResult(
pluginInstance: BasePluginInstance, pluginInstance: BasePluginInstance,
): BasePluginResult { ): BasePluginResult {
return { return {
flipperLib: pluginInstance.flipperLib,
activate: () => pluginInstance.activate(), activate: () => pluginInstance.activate(),
deactivate: () => pluginInstance.deactivate(), deactivate: () => pluginInstance.deactivate(),
exportState: () => pluginInstance.exportState(), exportState: () => pluginInstance.exportState(),
@@ -317,6 +339,13 @@ function createBasePluginResult(
pluginInstance.triggerDeepLink(deepLink); pluginInstance.triggerDeepLink(deepLink);
}, },
destroy: () => pluginInstance.destroy(), destroy: () => pluginInstance.destroy(),
triggerMenuEntry: (action: string) => {
const entry = pluginInstance.menuEntries.find((e) => e.action === action);
if (!entry) {
throw new Error('No menu entry found with action: ' + action);
}
entry.handler();
},
}; };
} }

View File

@@ -49,6 +49,22 @@ export function plugin(client: FlipperClient<Events, {}>) {
const rows = createState<PersistedState>({}, {persist: 'rows'}); const rows = createState<PersistedState>({}, {persist: 'rows'});
const selectedID = createState<string | null>(null, {persist: 'selection'}); const selectedID = createState<string | null>(null, {persist: 'selection'});
client.addMenuEntry(
{
label: 'Reset Selection',
topLevelMenu: 'Edit',
handler: () => {
selectedID.set(null);
},
},
{
action: 'createPaste',
handler: () => {
console.log('creating paste');
},
},
);
client.onMessage('newRow', (row) => { client.onMessage('newRow', (row) => {
rows.update((draft) => { rows.update((draft) => {
draft[row.id] = row; draft[row.id] = row;