You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-06-24 23:26:50 +02:00
All: Use Lerna to manage monorepo
This commit is contained in:
@ -0,0 +1,28 @@
|
||||
import { PluginStates } from '../reducer';
|
||||
import { ExtraRendererRule } from '@joplinapp/renderer/MdToHtml';
|
||||
import { ContentScriptType } from '../api/types';
|
||||
|
||||
export default function contentScriptsToRendererRules(plugins:PluginStates):ExtraRendererRule[] {
|
||||
const output:ExtraRendererRule[] = [];
|
||||
|
||||
for (const pluginId in plugins) {
|
||||
const plugin = plugins[pluginId];
|
||||
const contentScripts = plugin.contentScripts[ContentScriptType.MarkdownItPlugin];
|
||||
if (!contentScripts) continue;
|
||||
|
||||
for (const contentScript of contentScripts) {
|
||||
const module = require(contentScript.path);
|
||||
if (!module.default || typeof module.default !== 'function') throw new Error(`Content script must export a function under the "default" key: Plugin: ${pluginId}: Script: ${contentScript.id}`);
|
||||
|
||||
const loadedModule = module.default({});
|
||||
if (!loadedModule.plugin) throw new Error(`Content script must export a "plugin" key: Plugin: ${pluginId}: Script: ${contentScript.id}`);
|
||||
|
||||
output.push({
|
||||
id: contentScript.id,
|
||||
module: loadedModule,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
8
packages/lib/services/plugins/utils/createViewHandle.ts
Normal file
8
packages/lib/services/plugins/utils/createViewHandle.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import uuid from '../../../uuid';
|
||||
import Plugin from '../Plugin';
|
||||
|
||||
export type ViewHandle = string;
|
||||
|
||||
export default function createViewHandle(plugin:Plugin):ViewHandle {
|
||||
return `plugin-view-${plugin.id}-${uuid.createNano()}`;
|
||||
}
|
45
packages/lib/services/plugins/utils/executeSandboxCall.ts
Normal file
45
packages/lib/services/plugins/utils/executeSandboxCall.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import Global from '../api/Global';
|
||||
|
||||
type EventHandler = (callbackId:string, args:any[]) => void;
|
||||
|
||||
function createEventHandlers(arg:any, eventHandler:EventHandler) {
|
||||
if (Array.isArray(arg)) {
|
||||
for (let i = 0; i < arg.length; i++) {
|
||||
arg[i] = createEventHandlers(arg[i], eventHandler);
|
||||
}
|
||||
return arg;
|
||||
} else if (typeof arg === 'string' && arg.indexOf('___plugin_event_') === 0) {
|
||||
const callbackId = arg;
|
||||
return async (...args:any[]) => {
|
||||
const result = await eventHandler(callbackId, args);
|
||||
return result;
|
||||
};
|
||||
} else if (arg === null || arg === undefined) {
|
||||
return arg;
|
||||
} else if (typeof arg === 'object') {
|
||||
for (const n in arg) {
|
||||
arg[n] = createEventHandlers(arg[n], eventHandler);
|
||||
}
|
||||
}
|
||||
|
||||
return arg;
|
||||
}
|
||||
|
||||
export default async function executeSandboxCall(pluginId:string, sandbox:Global, path:string, args:any[], eventHandler:EventHandler) {
|
||||
const pathFragments = path.split('.');
|
||||
|
||||
let parent:any = null;
|
||||
let fn:any = sandbox;
|
||||
|
||||
if (!fn) throw new Error(`No sandbox for plugin ${pluginId}`); // Sanity check as normally cannot happen
|
||||
|
||||
for (const pathFragment of pathFragments) {
|
||||
parent = fn;
|
||||
fn = fn[pathFragment];
|
||||
if (!fn) throw new Error(`Property or method "${pathFragment}" does not exist in "${path}"`);
|
||||
}
|
||||
|
||||
const convertedArgs = createEventHandlers(args, eventHandler);
|
||||
|
||||
return fn.apply(parent, convertedArgs);
|
||||
}
|
37
packages/lib/services/plugins/utils/manifestFromObject.ts
Normal file
37
packages/lib/services/plugins/utils/manifestFromObject.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { PluginManifest, PluginPermission } from './types';
|
||||
|
||||
export default function manifestFromObject(o:any):PluginManifest {
|
||||
|
||||
const getString = (name:string, required:boolean = true, defaultValue:string = ''):string => {
|
||||
if (required && !o[name]) throw new Error(`Missing required field: ${name}`);
|
||||
if (!o[name]) return defaultValue;
|
||||
if (typeof o[name] !== 'string') throw new Error(`Field must be a string: ${name}`);
|
||||
return o[name];
|
||||
};
|
||||
|
||||
const getNumber = (name:string, required:boolean = true):number => {
|
||||
if (required && !o[name]) throw new Error(`Missing required field: ${name}`);
|
||||
if (!o[name]) return 0;
|
||||
if (typeof o[name] !== 'number') throw new Error(`Field must be a number: ${name}`);
|
||||
return o[name];
|
||||
};
|
||||
|
||||
const permissions:PluginPermission[] = [];
|
||||
|
||||
const manifest = {
|
||||
manifest_version: getNumber('manifest_version', true),
|
||||
name: getString('name', true),
|
||||
version: getString('version', true),
|
||||
description: getString('description', false),
|
||||
homepage_url: getString('homepage_url', false),
|
||||
permissions: permissions,
|
||||
};
|
||||
|
||||
if (o.permissions) {
|
||||
for (const p of o.permissions) {
|
||||
manifest.permissions.push(p);
|
||||
}
|
||||
}
|
||||
|
||||
return manifest;
|
||||
}
|
29
packages/lib/services/plugins/utils/mapEventHandlersToIds.ts
Normal file
29
packages/lib/services/plugins/utils/mapEventHandlersToIds.ts
Normal file
@ -0,0 +1,29 @@
|
||||
let eventHandlerIndex_ = 1;
|
||||
|
||||
export interface EventHandlers {
|
||||
[key:string]: Function;
|
||||
}
|
||||
|
||||
export default function mapEventHandlersToIds(arg:any, eventHandlers:EventHandlers) {
|
||||
if (Array.isArray(arg)) {
|
||||
for (let i = 0; i < arg.length; i++) {
|
||||
arg[i] = mapEventHandlersToIds(arg[i], eventHandlers);
|
||||
}
|
||||
return arg;
|
||||
} else if (typeof arg === 'function') {
|
||||
const id = `___plugin_event_${eventHandlerIndex_}`;
|
||||
eventHandlerIndex_++;
|
||||
eventHandlers[id] = arg;
|
||||
return id;
|
||||
} else if (arg === null) {
|
||||
return null;
|
||||
} else if (arg === undefined) {
|
||||
return undefined;
|
||||
} else if (typeof arg === 'object') {
|
||||
for (const n in arg) {
|
||||
arg[n] = mapEventHandlersToIds(arg[n], eventHandlers);
|
||||
}
|
||||
}
|
||||
|
||||
return arg;
|
||||
}
|
12
packages/lib/services/plugins/utils/types.ts
Normal file
12
packages/lib/services/plugins/utils/types.ts
Normal file
@ -0,0 +1,12 @@
|
||||
export enum PluginPermission {
|
||||
Model = 'model',
|
||||
}
|
||||
|
||||
export interface PluginManifest {
|
||||
manifest_version: number,
|
||||
name: string,
|
||||
version: string,
|
||||
description?: string,
|
||||
homepage_url?: string,
|
||||
permissions?: PluginPermission[],
|
||||
}
|
Reference in New Issue
Block a user