2023-06-30 09:55:56 +01:00
/* eslint-disable multiline-comment-style */
2020-10-09 18:35:46 +01:00
import Plugin from '../Plugin' ;
2023-07-27 16:05:56 +01:00
import Logger from '@joplin/utils/Logger' ;
2020-10-21 00:23:55 +01:00
import { ContentScriptType , Script } from './types' ;
2020-10-09 18:35:46 +01:00
2020-11-19 15:25:02 +00:00
const logger = Logger . create ( 'joplin.plugins' );
2020-10-09 18:35:46 +01:00
/**
* This class provides access to plugin-related features.
*/
export default class JoplinPlugins {
private plugin : Plugin ;
2020-11-19 15:25:02 +00:00
public constructor ( plugin : Plugin ) {
2020-10-09 18:35:46 +01:00
this . plugin = plugin ;
}
/**
* Registers a new plugin. This is the entry point when creating a plugin. You should pass a simple object with an `onStart` method to it.
* That `onStart` method will be executed as soon as the plugin is loaded.
*
* ```typescript
* joplin.plugins.register({
* onStart: async function() {
* // Run your plugin code here
* }
* });
* ```
*/
2020-12-11 12:29:21 +00:00
public async register ( script : Script ) {
2020-10-09 18:35:46 +01:00
if ( script . onStart ) {
const startTime = Date . now ();
2020-11-19 15:25:02 +00:00
logger . info ( `Starting plugin: ${ this . plugin . id } ` );
2020-10-09 18:35:46 +01:00
// We don't use `await` when calling onStart because the plugin might be awaiting
// in that call too (for example, when opening a dialog on startup) so we don't
// want to get stuck here.
2024-04-05 12:16:49 +01:00
// eslint-disable-next-line promise/prefer-await-to-then, @typescript-eslint/no-explicit-any -- Old code before rule was applied, Old code before rule was applied
2020-11-25 14:40:25 +00:00
void script . onStart ({}). catch (( error : any ) => {
2020-10-09 18:35:46 +01:00
// For some reason, error thrown from the executed script do not have the type "Error"
// but are instead plain object. So recreate the Error object here so that it can
// be handled correctly by loggers, etc.
2020-11-12 19:13:28 +00:00
const newError : Error = new Error ( error . message );
2020-10-09 18:35:46 +01:00
newError . stack = error . stack ;
2020-11-19 15:25:02 +00:00
logger . error ( `Uncaught exception in plugin " ${ this . plugin . id } ":` , newError );
2022-09-30 17:23:14 +01:00
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
2020-10-09 18:35:46 +01:00
}). then (() => {
2020-11-19 15:25:02 +00:00
logger . info ( `Finished running onStart handler: ${ this . plugin . id } (Took ${ Date . now () - startTime } ms)` );
2020-10-22 14:51:59 +01:00
this . plugin . emit ( 'started' );
2020-10-09 18:35:46 +01:00
});
}
}
2020-10-21 00:23:55 +01:00
/**
2021-01-11 23:33:10 +00:00
* @deprecated Use joplin.contentScripts.register()
2020-10-21 00:23:55 +01:00
*/
2020-12-11 12:29:21 +00:00
public async registerContentScript ( type : ContentScriptType , id : string , scriptPath : string ) {
2021-08-05 12:02:03 +01:00
this . plugin . deprecationNotice ( '1.8' , 'joplin.plugins.registerContentScript() is deprecated in favour of joplin.contentScripts.register()' , true );
2020-10-21 00:23:55 +01:00
return this . plugin . registerContentScript ( type , id , scriptPath );
}
2021-01-11 23:33:10 +00:00
2021-01-24 15:51:35 +00:00
/**
2021-01-27 12:48:47 +00:00
* Gets the plugin own data directory path. Use this to store any
* plugin-related data. Unlike [[installationDir]], any data stored here
* will be persisted.
2021-01-24 15:51:35 +00:00
*/
public async dataDir () : Promise < string > {
return this . plugin . dataDir ();
}
2021-01-11 23:33:10 +00:00
2021-01-24 18:03:33 +00:00
/**
2021-01-27 12:48:47 +00:00
* Gets the plugin installation directory. This can be used to access any
* asset that was packaged with the plugin. This directory should be
* considered read-only because any data you store here might be deleted or
* re-created at any time. To store new persistent data, use [[dataDir]].
*/
public async installationDir () : Promise < string > {
return this . plugin . baseDir ;
}
/**
* @deprecated Use joplin.require()
2021-01-24 18:03:33 +00:00
*/
2024-04-05 12:16:49 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2021-01-24 18:03:33 +00:00
public require ( _path : string ) : any {
// Just a stub. Implementation has to be done within plugin process, in plugin_index.js
}
2020-10-09 18:35:46 +01:00
}