Skip to content

Commit

Permalink
WIP commit, Implementing plugin API schema
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Oct 4, 2023
1 parent 6910326 commit 49592a8
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/main/ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import fspExistsIpcInvoke from './invokes/fspExists';
import fspMkdirIpcInvoke from './invokes/fspMkdir';
import fspWriteFileIpcInvoke from './invokes/fspWriteFile';
import getXtoryTemplatesIpcInvoke from './invokes/getXtoryTemplates';
import getFileTypesIpcInvoke from './invokes/getFileTypes';
import openProjectIpcInvoke from './invokes/openProject';

const on = (channel: ChannelsMain, ipcAction: IpcAction): void => {
Expand All @@ -36,4 +37,5 @@ handle(ChannelsMain.fspExists, fspExistsIpcInvoke);
handle(ChannelsMain.fspMkdir, fspMkdirIpcInvoke);
handle(ChannelsMain.fspWriteFile, fspWriteFileIpcInvoke);
handle(ChannelsMain.getXtoryTemplates, getXtoryTemplatesIpcInvoke);
handle(ChannelsMain.getFileTypes, getFileTypesIpcInvoke);
handle(ChannelsMain.openProject, openProjectIpcInvoke);
13 changes: 13 additions & 0 deletions src/main/ipc/invokes/getFileTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IpcMainInvokeEvent } from 'electron';
import project from 'main/project';

export default async function getFileTypes(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
event: IpcMainInvokeEvent,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: undefined
): Promise<string[]> {
const pluginsService = project.pluginsService;
const fileTypes = pluginsService.getFileTypePlugins();
return ['ok'];
}
11 changes: 10 additions & 1 deletion src/main/project/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WebContents } from 'electron';

import LoggingService from 'main/services/loggingService';
import type LoggingService from 'main/services/loggingService';
import type PluginsService from 'main/services/pluginsService';

import {
ChannelsRenderer,
Expand Down Expand Up @@ -35,6 +36,14 @@ export default class ProjectManager {
return this.#project.loggingService;
}

static get pluginsService(): PluginsService {
this.#throwIfNotInit();
if (!this.#project) {
throw Error('No Project Is Open!');
}
return this.#project.pluginsService;
}

static init(projectLoader: ProjectLoaderType) {
if (this.#isInit) {
throw new Error(
Expand Down
13 changes: 9 additions & 4 deletions src/main/services/pluginsService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import project from 'main/project';
import { LogLevel } from 'shared/types';
import { sanitizePath, tryGetAsync } from 'shared/utils';

import PluginBuilder from './pluginBuilder';
import PluginApi from './pluginApi';

import IService from '../IService';

Expand All @@ -26,7 +26,6 @@ class PluginsService implements IService {
this.#plugins = {};
try {
await this.loadPlugins();
console.log(this.#plugins);
return true;
} catch (error) {
return false;
Expand Down Expand Up @@ -85,6 +84,12 @@ class PluginsService implements IService {
return true;
}

// TODO: consider better naming
getFileTypePlugins() {
const plugins = this.#plugins;
console.log(plugins);
}

// eslint-disable-next-line class-methods-use-this
async #runMainScript(pluginName: string, scriptPath: string): Promise<void> {
const context = {
Expand All @@ -93,15 +98,15 @@ class PluginsService implements IService {
return `Required ${path}`;
},
logger: project.logger,
builder: new PluginBuilder(),
api: new PluginApi(),
};
const script = await readFile(scriptPath, 'utf8');

// execute script in private context
try {
// eslint-disable-next-line no-new-func
new Function(`with(this) { ${script} }`).call(context);
const plugin = context.builder.build();
const plugin = context.api.build();
this.#plugins[pluginName] = plugin;
} catch (error) {
project.logger.log(LogLevel.error, ['plugin', pluginName], error);
Expand Down
50 changes: 50 additions & 0 deletions src/main/services/pluginsService/pluginApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
type ConnectionInfo = {
in: number;
out: number;
};

type NodeInfo = {
type: string;
connection: ConnectionInfo;
};

// builder.flowView.setFileType('xxx') // *.xxx
// builder.flowView.setNodes(xxx)
// builder.globalShortcut.setShortcutAction(yyy)

class FlowViewBuilder {
#fileType: string | null = null;

#nodes: NodeInfo[] | null = null;

setFileType(extension: string) {
this.#fileType = extension;
return this;
}

setNodes(nodes: NodeInfo[]) {
this.#nodes = nodes;
return this;
}

// TODO: making these build functions private can help with a well constructed API
build() {
const fileType = this.#fileType;
const nodes = this.#nodes;
return { fileType, nodes };
}
}

export default class PluginBuilder {
flowView: FlowViewBuilder;

constructor() {
this.flowView = new FlowViewBuilder();
}

build() {
const flowViewBuilder = this.flowView;
const flowView = flowViewBuilder.build();
return { flowView };
}
}
31 changes: 0 additions & 31 deletions src/main/services/pluginsService/pluginBuilder.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/renderer/components/Tab/TabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import React from 'react';
import Box from '@mui/material/Box';
import TextField from '@mui/material/TextField';

import { ChannelsMain } from 'shared/types';

import { useAppSelector, useAppDispatch } from 'renderer/state/store/index';
import {
setTabData,
Expand All @@ -20,6 +22,17 @@ export interface TabViewProps {

export default function TabView({ tabId }: TabViewProps) {
const dispatch = useAppDispatch();
const [fileTypes, setFileTypes] = React.useState<string[]>([]);

React.useEffect(() => {
async function getFileTypes() {
const result: string[] = await window.electron.ipcRenderer.invoke(
ChannelsMain.getFileTypes
);
setFileTypes(result);
}
getFileTypes().catch(e => console.log('eee', e));
}, []);

const tabState = useAppSelector(
(state) => state.tabsState.tabs.find((tab) => tab.id === tabId)!
Expand All @@ -32,6 +45,7 @@ export default function TabView({ tabId }: TabViewProps) {
[dispatch, tabId]
);
const dispatchFileView = () => {
console.log(fileTypes, 'fileTypes')
if (tabData.extension === 'xflow') {
return <FlowView tabId={tabId} setTabIsDirty={setTabIsDirty} />;
}
Expand Down
1 change: 1 addition & 0 deletions src/shared/types/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum ChannelsMain {
browseFileSystem = 'browseFileSystem',
createNewProject = 'createNewProject',
getXtoryTemplates = 'getXtoryTemplates',
getFileTypes = 'getFileTypes',
openProject = 'openProject',
openFileAsTab = 'openFileAsTab',
fspExists = 'fspExists',
Expand Down

0 comments on commit 49592a8

Please sign in to comment.