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

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