2023-06-30 10:55:56 +02:00
/* eslint-disable multiline-comment-style */
2020-10-09 19:35:46 +02:00
import Plugin from '../Plugin' ;
2023-07-27 17:05:56 +02:00
import Logger from '@joplin/utils/Logger' ;
2020-10-21 01:23:55 +02:00
import { ContentScriptType , Script } from './types' ;
2020-10-09 19:35:46 +02:00
2020-11-19 17:25:02 +02:00
const logger = Logger . create ( 'joplin.plugins' ) ;
2020-10-09 19:35:46 +02:00
/ * *
* This class provides access to plugin - related features .
* /
export default class JoplinPlugins {
private plugin : Plugin ;
2020-11-19 17:25:02 +02:00
public constructor ( plugin : Plugin ) {
2020-10-09 19:35:46 +02: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 14:29:21 +02:00
public async register ( script : Script ) {
2020-10-09 19:35:46 +02:00
if ( script . onStart ) {
const startTime = Date . now ( ) ;
2020-11-19 17:25:02 +02:00
logger . info ( ` Starting plugin: ${ this . plugin . id } ` ) ;
2020-10-09 19:35:46 +02: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 13:16:49 +02: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 16:40:25 +02:00
void script . onStart ( { } ) . catch ( ( error : any ) = > {
2020-10-09 19:35:46 +02: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 21:13:28 +02:00
const newError : Error = new Error ( error . message ) ;
2020-10-09 19:35:46 +02:00
newError . stack = error . stack ;
2020-11-19 17:25:02 +02:00
logger . error ( ` Uncaught exception in plugin " ${ this . plugin . id } ": ` , newError ) ;
2022-09-30 18:23:14 +02:00
// eslint-disable-next-line promise/prefer-await-to-then -- Old code before rule was applied
2020-10-09 19:35:46 +02:00
} ) . then ( ( ) = > {
2020-11-19 17:25:02 +02:00
logger . info ( ` Finished running onStart handler: ${ this . plugin . id } (Took ${ Date . now ( ) - startTime } ms) ` ) ;
2020-10-22 15:51:59 +02:00
this . plugin . emit ( 'started' ) ;
2020-10-09 19:35:46 +02:00
} ) ;
}
}
2020-10-21 01:23:55 +02:00
/ * *
2021-01-12 01:33:10 +02:00
* @deprecated Use joplin . contentScripts . register ( )
2020-10-21 01:23:55 +02:00
* /
2020-12-11 14:29:21 +02:00
public async registerContentScript ( type : ContentScriptType , id : string , scriptPath : string ) {
2021-08-05 13:02:03 +02:00
this . plugin . deprecationNotice ( '1.8' , 'joplin.plugins.registerContentScript() is deprecated in favour of joplin.contentScripts.register()' , true ) ;
2020-10-21 01:23:55 +02:00
return this . plugin . registerContentScript ( type , id , scriptPath ) ;
}
2021-01-12 01:33:10 +02:00
2021-01-24 17:51:35 +02:00
/ * *
2021-01-27 14:48:47 +02: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 17:51:35 +02:00
* /
public async dataDir ( ) : Promise < string > {
return this . plugin . dataDir ( ) ;
}
2021-01-12 01:33:10 +02:00
2021-01-24 20:03:33 +02:00
/ * *
2021-01-27 14:48:47 +02: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 20:03:33 +02:00
* /
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2021-01-24 20:03:33 +02:00
public require ( _path : string ) : any {
// Just a stub. Implementation has to be done within plugin process, in plugin_index.js
}
2020-10-09 19:35:46 +02:00
}