1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-30 23:44:55 +02:00

All: Add support for application plugins (#3257)

This commit is contained in:
Laurent
2020-10-09 18:35:46 +01:00
committed by GitHub
parent 833fb1264f
commit fe41d37f8f
804 changed files with 95622 additions and 5307 deletions

View File

@ -1,8 +1,15 @@
const Note = require('lib/models/Note.js');
const Alarm = require('lib/models/Alarm.js');
import Logger from 'lib/Logger';
import Alarm from 'lib/models/Alarm';
class AlarmService {
static setDriver(v) {
const Note = require('lib/models/Note.js');
export default class AlarmService {
private static driver_:any;
private static logger_:Logger;
// private static inAppNotificationHandler_:any;
static setDriver(v:any) {
this.driver_ = v;
if (this.driver_.setService) this.driver_.setService(this);
@ -13,7 +20,7 @@ class AlarmService {
return this.driver_;
}
static setLogger(v) {
static setLogger(v:Logger) {
this.logger_ = v;
}
@ -21,8 +28,8 @@ class AlarmService {
return this.logger_;
}
static setInAppNotificationHandler(v) {
this.inAppNotificationHandler_ = v;
static setInAppNotificationHandler(v:any) {
// this.inAppNotificationHandler_ = v;
if (this.driver_.setInAppNotificationHandler) this.driver_.setInAppNotificationHandler(v);
}
@ -43,7 +50,7 @@ class AlarmService {
// When passing a note, make sure it has all the required properties
// (better to pass a complete note or else just the ID)
static async updateNoteNotification(noteOrId, isDeleted = false) {
static async updateNoteNotification(noteOrId:any, isDeleted:boolean = false) {
try {
let note = null;
let noteId = null;
@ -118,5 +125,3 @@ class AlarmService {
}
}
}
module.exports = AlarmService;

View File

@ -1,11 +1,16 @@
import { Notification } from 'lib/models/Alarm';
const PushNotification = require('react-native-push-notification');
class AlarmServiceDriver {
export default class AlarmServiceDriver {
private PushNotification_:any = null;
PushNotificationHandler_() {
if (!this.PushNotification_) {
PushNotification.configure({
// (required) Called when a remote or local notification is opened or received
onNotification: function(notification) {
onNotification: function(notification:any) {
console.info('Notification was opened: ', notification);
// process the notification
},
@ -27,11 +32,11 @@ class AlarmServiceDriver {
throw new Error('Available only for non-persistent alarms');
}
async clearNotification(id) {
async clearNotification(id:any) {
return this.PushNotificationHandler_().cancelLocalNotifications({ id: `${id}` });
}
async scheduleNotification(notification) {
async scheduleNotification(notification:Notification) {
const config = {
id: `${notification.id}`,
message: notification.title,
@ -41,5 +46,3 @@ class AlarmServiceDriver {
this.PushNotificationHandler_().localNotificationSchedule(config);
}
}
module.exports = AlarmServiceDriver;

View File

@ -1,11 +1,13 @@
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import { Notification } from 'lib/models/Alarm';
const PushNotificationIOS = require('@react-native-community/push-notification-ios').default;
export default class AlarmServiceDriver {
private hasPermission_:boolean = null;
private inAppNotificationHandler_:any = null;
class AlarmServiceDriver {
constructor() {
this.hasPermission_ = null;
this.inAppNotificationHandler_ = null;
PushNotificationIOS.addEventListener('localNotification', instance => {
PushNotificationIOS.addEventListener('localNotification', (instance:any) => {
if (!this.inAppNotificationHandler_) return;
if (!instance || !instance._data || !instance._data.id) {
@ -26,17 +28,17 @@ class AlarmServiceDriver {
throw new Error('Available only for non-persistent alarms');
}
setInAppNotificationHandler(v) {
setInAppNotificationHandler(v:any) {
this.inAppNotificationHandler_ = v;
}
async hasPermissions(perm = null) {
async hasPermissions(perm:any = null) {
if (perm !== null) return perm.alert && perm.badge && perm.sound;
if (this.hasPermission_ !== null) return this.hasPermission_;
return new Promise((resolve) => {
PushNotificationIOS.checkPermissions(async perm => {
PushNotificationIOS.checkPermissions(async (perm:any) => {
const ok = await this.hasPermissions(perm);
this.hasPermission_ = ok;
resolve(ok);
@ -45,27 +47,28 @@ class AlarmServiceDriver {
}
async requestPermissions() {
const newPerm = await PushNotificationIOS.requestPermissions({
const options:any = {
alert: 1,
badge: 1,
sound: 1,
});
};
const newPerm = await PushNotificationIOS.requestPermissions(options);
this.hasPermission_ = null;
return this.hasPermissions(newPerm);
}
async clearNotification(id) {
async clearNotification(id:any) {
PushNotificationIOS.cancelLocalNotifications({ id: `${id}` });
}
async scheduleNotification(notification) {
async scheduleNotification(notification:Notification) {
if (!(await this.hasPermissions())) {
const ok = await this.requestPermissions();
if (!ok) return;
}
// ID must be a string and userInfo must be supplied otherwise cancel won't work
const iosNotification = {
const iosNotification:any = {
id: `${notification.id}`,
alertTitle: notification.title,
fireDate: notification.date.toISOString(),
@ -77,5 +80,3 @@ class AlarmServiceDriver {
PushNotificationIOS.scheduleLocalNotification(iosNotification);
}
}
module.exports = AlarmServiceDriver;

View File

@ -1,16 +1,27 @@
const notifier = require('node-notifier');
const { bridge } = require('electron').remote.require('./bridge');
import eventManager from 'lib/eventManager';
import { Notification } from 'lib/models/Alarm';
import shim from 'lib/shim';
class AlarmServiceDriverNode {
constructor(options) {
const notifier = require('node-notifier');
const bridge = require('electron').remote.require('./bridge').default;
interface Options {
appName: string,
}
export default class AlarmServiceDriverNode {
private appName_:string;
private notifications_:any = {};
private service_:any = null;
constructor(options:Options) {
// Note: appName is required to get the notification to work. It must be the same as the appId defined in package.json
// https://github.com/mikaelbr/node-notifier/issues/144#issuecomment-319324058
this.appName_ = options.appName;
this.notifications_ = {};
this.service_ = null;
}
setService(s) {
setService(s:any) {
this.service_ = s;
}
@ -22,17 +33,17 @@ class AlarmServiceDriverNode {
return false;
}
notificationIsSet(id) {
notificationIsSet(id:string) {
return id in this.notifications_;
}
async clearNotification(id) {
async clearNotification(id:string) {
if (!this.notificationIsSet(id)) return;
clearTimeout(this.notifications_[id].timeoutId);
shim.clearTimeout(this.notifications_[id].timeoutId);
delete this.notifications_[id];
}
async scheduleNotification(notification) {
async scheduleNotification(notification:Notification) {
const now = Date.now();
const interval = notification.date.getTime() - now;
if (interval < 0) return;
@ -43,7 +54,7 @@ class AlarmServiceDriverNode {
this.logger().info(`AlarmServiceDriverNode::scheduleNotification: Notification ${notification.id} with interval: ${interval}ms`);
if (this.notifications_[notification.id]) clearTimeout(this.notifications_[notification.id].timeoutId);
if (this.notifications_[notification.id]) shim.clearTimeout(this.notifications_[notification.id].timeoutId);
let timeoutId = null;
@ -56,7 +67,7 @@ class AlarmServiceDriverNode {
if (interval >= maxInterval) {
this.logger().info(`AlarmServiceDriverNode::scheduleNotification: Notification interval is greater than ${maxInterval}ms - will reschedule in ${maxInterval}ms`);
timeoutId = setTimeout(() => {
timeoutId = shim.setTimeout(() => {
if (!this.notifications_[notification.id]) {
this.logger().info(`AlarmServiceDriverNode::scheduleNotification: Notification ${notification.id} has been deleted - not rescheduling it`);
return;
@ -64,8 +75,8 @@ class AlarmServiceDriverNode {
this.scheduleNotification(this.notifications_[notification.id]);
}, maxInterval);
} else {
timeoutId = setTimeout(() => {
const o = {
timeoutId = shim.setTimeout(() => {
const o:any = {
appID: this.appName_,
title: notification.title,
icon: `${bridge().electronApp().buildDir()}/icons/512x512.png`,
@ -79,11 +90,13 @@ class AlarmServiceDriverNode {
this.logger().info('AlarmServiceDriverNode::scheduleNotification: Triggering notification:', o);
notifier.notify(o, (error, response) => {
notifier.notify(o, (error:any, response:any) => {
this.logger().info('AlarmServiceDriverNode::scheduleNotification: node-notifier response:', error, response);
});
this.clearNotification(notification.id);
eventManager.emit('noteAlarmTrigger', { noteId: notification.noteId });
}, interval);
}
@ -91,5 +104,3 @@ class AlarmServiceDriverNode {
this.notifications_[notification.id].timeoutId = timeoutId;
}
}
module.exports = AlarmServiceDriverNode;

View File

@ -1,15 +1,17 @@
class BaseService {
logger() {
import Logger from 'lib/Logger';
export default class BaseService {
static logger_:Logger = null;
protected instanceLogger_:Logger = null;
logger():Logger {
if (this.instanceLogger_) return this.instanceLogger_;
if (!BaseService.logger_) throw new Error('BaseService.logger_ not set!!');
return BaseService.logger_;
}
setLogger(v) {
setLogger(v:Logger) {
this.instanceLogger_ = v;
}
}
BaseService.logger_ = null;
module.exports = BaseService;

View File

@ -0,0 +1,31 @@
import { ContextKeyExpr, ContextKeyExpression } from './contextkey/contextkey';
export default class BooleanExpression {
private expression_:string;
private rules_:ContextKeyExpression = null;
constructor(expression:string) {
this.expression_ = expression;
}
private createContext(ctx: any) {
return {
getValue: (key: string) => {
return ctx[key];
},
};
}
private get rules():ContextKeyExpression {
if (!this.rules_) {
this.rules_ = ContextKeyExpr.deserialize(this.expression_);
}
return this.rules_;
}
public evaluate(context:any):boolean {
return this.rules.evaluate(this.createContext(context));
}
}

View File

@ -1,21 +1,38 @@
import KeymapService from './KeymapService';
const BaseService = require('lib/services/BaseService');
const eventManager = require('lib/eventManager');
import eventManager from 'lib/eventManager';
import markdownUtils, { MarkdownTableHeader, MarkdownTableRow } from 'lib/markdownUtils';
import BaseService from 'lib/services/BaseService';
import shim from 'lib/shim';
type LabelFunction = () => string;
export interface CommandRuntime {
execute(props:any):void
execute(props:any):Promise<any>
isEnabled?(props:any):boolean
// "state" type is "AppState" but in order not to introduce a
// dependency to the desktop app (so that the service can
// potentially be used by the mobile app too), we keep it as "any".
// Individual commands can define it as state:AppState when relevant.
//
// In general this method should reduce the provided state to only
// what's absolutely necessary. For example, if the property of a
// note is needed, return only that particular property and not the
// whole note object. This will ensure that components that depends
// on this command are not uncessarily re-rendered. A note object for
// example might change frequently but its markdown_language property
// will almost never change.
mapStateToProps?(state:any):any
// Used for the (optional) toolbar button title
title?(props:any):string,
props?:any
// props?:any
}
export interface CommandDeclaration {
name: string
// Used for the menu item label, and toolbar button tooltip
label?():string,
label?: LabelFunction | string,
// This is a bit of a hack because some labels don't make much sense in isolation. For example,
// the commmand to focus the note list is called just "Note list". This makes sense within the menu
@ -25,8 +42,16 @@ export interface CommandDeclaration {
// label() => _('Note list'),
// parentLabel() => _('Focus'),
// Which will be displayed as "Focus: Note list" in the keymap config screen.
parentLabel?():string,
parentLabel?:LabelFunction | string,
// All free Font Awesome icons are available: https://fontawesome.com/icons?d=gallery&m=free
iconName?: string,
// Will be used by TinyMCE (which doesn't support Font Awesome icons).
// Defaults to the "preferences" icon (a cog) if not specified.
// https://www.tiny.cloud/docs/advanced/editor-icon-identifiers/
tinymceIconName?: string,
// Same as `role` key in Electron MenuItem:
// https://www.electronjs.org/docs/api/menu-item#new-menuitemoptions
// Note that due to a bug in Electron, menu items with a role cannot
@ -39,15 +64,6 @@ export interface Command {
runtime?: CommandRuntime,
}
export interface ToolbarButtonInfo {
name: string,
tooltip: string,
iconName: string,
enabled: boolean,
onClick():void,
title: string,
}
interface Commands {
[key:string]: Command;
}
@ -94,13 +110,9 @@ export default class CommandService extends BaseService {
private commands_:Commands = {};
private commandPreviousStates_:CommandStates = {};
private mapStateToPropsIID_:any = null;
private keymapService:KeymapService = null;
initialize(store:any, keymapService:KeymapService) {
initialize(store:any) {
utils.store = store;
this.keymapService = keymapService;
}
public on(eventName:string, callback:Function) {
@ -111,69 +123,7 @@ export default class CommandService extends BaseService {
eventManager.off(eventName, callback);
}
private propsHaveChanged(previous:any, next:any) {
if (!previous && next) return true;
for (const n in previous) {
if (previous[n] !== next[n]) return true;
}
return false;
}
scheduleMapStateToProps(state:any) {
if (this.mapStateToPropsIID_) clearTimeout(this.mapStateToPropsIID_);
this.mapStateToPropsIID_ = setTimeout(() => {
this.mapStateToProps(state);
}, 50);
}
private mapStateToProps(state:any) {
const newState = state;
const changedCommands:any = {};
for (const name in this.commands_) {
const command = this.commands_[name];
if (!command.runtime) continue;
if (!command.runtime.mapStateToProps) {
command.runtime.props = {};
continue;
}
const newProps = command.runtime.mapStateToProps(state);
const haveChanged = this.propsHaveChanged(command.runtime.props, newProps);
if (haveChanged) {
const previousState = this.commandPreviousStates_[name];
command.runtime.props = newProps;
const newState:CommandState = {
enabled: this.isEnabled(name),
title: this.title(name),
};
if (!previousState || previousState.title !== newState.title || previousState.enabled !== newState.enabled) {
changedCommands[name] = newState;
}
this.commandPreviousStates_[name] = newState;
}
}
if (Object.keys(changedCommands).length) {
eventManager.emit('commandsEnabledStateChange', { commands: changedCommands });
}
return newState;
}
private commandByName(name:string, options:CommandByNameOptions = null):Command {
public commandByName(name:string, options:CommandByNameOptions = null):Command {
options = {
mustExist: true,
runtimeMustBeRegistered: false,
@ -192,17 +142,10 @@ export default class CommandService extends BaseService {
}
registerDeclaration(declaration:CommandDeclaration) {
// if (this.commands_[declaration.name]) throw new Error(`There is already a command with name ${declaration.name}`);
declaration = { ...declaration };
if (!declaration.label) declaration.label = () => '';
if (!declaration.label) declaration.label = '';
if (!declaration.iconName) declaration.iconName = '';
// In TypeScript it's not an issue, but in JavaScript it's easy to accidentally set the label
// to a string instead of a function, and it will cause strange errors that are hard to debug.
// So here check early that we have the right type.
if (typeof declaration.label !== 'function') throw new Error(`declaration.label must be a function: ${declaration.name}`);
this.commands_[declaration.name] = {
declaration: declaration,
};
@ -243,35 +186,42 @@ export default class CommandService extends BaseService {
delete this.commandPreviousStates_[commandName];
}
execute(commandName:string, args:any = null) {
console.info('CommandService::execute:', commandName, args);
async execute(commandName:string, props:any = null):Promise<any> {
const command = this.commandByName(commandName);
command.runtime.execute(args ? args : {});
this.logger().info('CommandService::execute:', commandName, props);
return command.runtime.execute(props ? props : {});
}
scheduleExecute(commandName:string, args:any = null) {
setTimeout(() => {
scheduleExecute(commandName:string, args:any) {
shim.setTimeout(() => {
this.execute(commandName, args);
}, 10);
}
isEnabled(commandName:string):boolean {
isEnabled(commandName:string, props:any):boolean {
const command = this.commandByName(commandName);
if (!command || !command.runtime) return false;
if (!command.runtime.props) return false;
return command.runtime.isEnabled(command.runtime.props);
// if (!command.runtime.props) return false;
return command.runtime.isEnabled(props);
}
title(commandName:string):string {
commandMapStateToProps(commandName:string, state:any):any {
const command = this.commandByName(commandName);
if (!command || !command.runtime || !command.runtime.props) return null;
return command.runtime.title(command.runtime.props);
if (!command.runtime) return null;
if (!command.runtime.mapStateToProps) return {};
return command.runtime.mapStateToProps(state);
}
iconName(commandName:string):string {
title(commandName:string, props:any):string {
const command = this.commandByName(commandName);
if (!command || !command.runtime) return null;
return command.runtime.title(props);
}
iconName(commandName:string, variant:string = null):string {
const command = this.commandByName(commandName);
if (!command) throw new Error(`No such command: ${commandName}`);
if (variant === 'tinymce') return command.declaration.tinymceIconName ? command.declaration.tinymceIconName : 'preferences';
return command.declaration.iconName;
}
@ -279,8 +229,15 @@ export default class CommandService extends BaseService {
const command = this.commandByName(commandName);
if (!command) throw new Error(`Command: ${commandName} is not declared`);
const output = [];
if (fullLabel && command.declaration.parentLabel && command.declaration.parentLabel()) output.push(command.declaration.parentLabel());
output.push(command.declaration.label());
const parentLabel = (d:CommandDeclaration):string => {
if (!d.parentLabel) return '';
if (typeof d.parentLabel === 'function') return d.parentLabel();
return d.parentLabel as string;
};
if (fullLabel && parentLabel(command.declaration)) output.push(parentLabel(command.declaration));
output.push(typeof command.declaration.label === 'function' ? command.declaration.label() : command.declaration.label);
return output.join(': ');
}
@ -289,58 +246,37 @@ export default class CommandService extends BaseService {
return !!command;
}
private extractExecuteArgs(command:Command, executeArgs:any) {
if (executeArgs) return executeArgs;
if (!command.runtime) throw new Error(`Command: ${command.declaration.name}: Runtime is not defined - make sure it has been registered.`);
if (command.runtime.props) return command.runtime.props;
return {};
}
commandToToolbarButton(commandName:string, executeArgs:any = null):ToolbarButtonInfo {
const command = this.commandByName(commandName, { runtimeMustBeRegistered: true });
return {
name: commandName,
tooltip: this.label(commandName),
iconName: command.declaration.iconName,
enabled: this.isEnabled(commandName),
onClick: () => {
this.execute(commandName, this.extractExecuteArgs(command, executeArgs));
public commandsToMarkdownTable(state:any):string {
const headers:MarkdownTableHeader[] = [
{
name: 'commandName',
label: 'Name',
},
title: this.title(commandName),
};
}
commandToMenuItem(commandName:string, executeArgs:any = null) {
const command = this.commandByName(commandName);
const item:any = {
id: command.declaration.name,
label: this.label(commandName),
click: () => {
this.execute(commandName, this.extractExecuteArgs(command, executeArgs));
{
name: 'description',
label: 'Description',
},
};
{
name: 'props',
label: 'Props',
},
];
if (command.declaration.role) item.role = command.declaration.role;
if (this.keymapService.acceleratorExists(commandName)) {
item.accelerator = this.keymapService.getAccelerator(commandName);
const rows:MarkdownTableRow[] = [];
for (const commandName in this.commands_) {
const props = this.commandMapStateToProps(commandName, state);
const row:MarkdownTableRow = {
commandName: commandName,
description: this.label(commandName),
props: JSON.stringify(props),
};
rows.push(row);
}
return item;
}
commandsEnabledState(previousState:any = null):any {
const output:any = {};
for (const name in this.commands_) {
const enabled = this.isEnabled(name);
if (!previousState || previousState[name] !== enabled) {
output[name] = enabled;
}
}
return output;
return markdownUtils.createMarkdownTable(headers, rows);
}
}

View File

@ -3,8 +3,9 @@ const BaseModel = require('lib/BaseModel');
const MasterKey = require('lib/models/MasterKey');
const Resource = require('lib/models/Resource');
const ResourceService = require('lib/services/ResourceService');
const { Logger } = require('lib/logger.js');
const Logger = require('lib/Logger').default;
const EventEmitter = require('events');
const shim = require('lib/shim').default;
class DecryptionWorker {
constructor() {
@ -64,7 +65,7 @@ class DecryptionWorker {
async scheduleStart() {
if (this.scheduleId_) return;
this.scheduleId_ = setTimeout(() => {
this.scheduleId_ = shim.setTimeout(() => {
this.scheduleId_ = null;
this.start({
masterKeyNotLoadedHandler: 'dispatch',
@ -280,16 +281,16 @@ class DecryptionWorker {
async destroy() {
this.eventEmitter_.removeAllListeners();
if (this.scheduleId_) {
clearTimeout(this.scheduleId_);
shim.clearTimeout(this.scheduleId_);
this.scheduleId_ = null;
}
this.eventEmitter_ = null;
DecryptionWorker.instance_ = null;
return new Promise((resolve) => {
const iid = setInterval(() => {
const iid = shim.setInterval(() => {
if (!this.startCalls_.length) {
clearInterval(iid);
shim.clearInterval(iid);
resolve();
}
}, 100);

View File

@ -1,7 +1,7 @@
const { padLeft } = require('lib/string-utils.js');
const { Logger } = require('lib/logger.js');
const { shim } = require('lib/shim.js');
const Setting = require('lib/models/Setting.js');
const Logger = require('lib/Logger').default;
const shim = require('lib/shim').default;
const Setting = require('lib/models/Setting').default;
const MasterKey = require('lib/models/MasterKey');
const BaseItem = require('lib/models/BaseItem');
const JoplinError = require('lib/JoplinError');

View File

@ -1,13 +1,13 @@
const { Logger } = require('lib/logger.js');
const Logger = require('lib/Logger').default;
const Note = require('lib/models/Note');
const Setting = require('lib/models/Setting');
const { shim } = require('lib/shim');
const Setting = require('lib/models/Setting').default;
const shim = require('lib/shim').default;
const EventEmitter = require('events');
const { splitCommandString } = require('lib/string-utils');
const { fileExtension, basename } = require('lib/path-utils');
const spawn = require('child_process').spawn;
const chokidar = require('chokidar');
const { bridge } = require('electron').remote.require('./bridge');
const bridge = require('electron').remote.require('./bridge').default;
const { time } = require('lib/time-utils.js');
const { ErrorNotFound } = require('./rest/errors');
@ -249,16 +249,16 @@ class ExternalEditWatcher {
try {
const subProcess = spawn(path, args, options);
const iid = setInterval(() => {
const iid = shim.setInterval(() => {
if (subProcess && subProcess.pid) {
/* was_debug */ this.logger().info(`Started editor with PID ${subProcess.pid}`);
clearInterval(iid);
shim.clearInterval(iid);
resolve();
}
}, 100);
subProcess.on('error', error => {
clearInterval(iid);
shim.clearInterval(iid);
reject(wrapError(error));
});
} catch (error) {

View File

@ -1,40 +0,0 @@
/* eslint @typescript-eslint/no-unused-vars: 0, no-unused-vars: ["error", { "argsIgnorePattern": ".*" }], */
const Setting = require('lib/models/Setting');
class InteropService_Exporter_Base {
constructor() {
this.context_ = {};
}
async init(destDir, options = {}) {}
async prepareForProcessingItemType(type, itemsToExport) {}
async processItem(ItemClass, item) {}
async processResource(resource, filePath) {}
async close() {}
setMetadata(md) {
this.metadata_ = md;
}
metadata() {
return this.metadata_;
}
updateContext(context) {
this.context_ = Object.assign(this.context_, context);
}
context() {
return this.context_;
}
async temporaryDirectory_(createIt) {
const md5 = require('md5');
const tempDir = `${Setting.value('tempDir')}/${md5(Math.random() + Date.now())}`;
if (createIt) await require('fs-extra').mkdirp(tempDir);
return tempDir;
}
}
module.exports = InteropService_Exporter_Base;

View File

@ -1,29 +0,0 @@
const InteropService_Exporter_Base = require('lib/services/InteropService_Exporter_Base');
const { basename } = require('lib/path-utils.js');
const { shim } = require('lib/shim');
class InteropService_Exporter_Json extends InteropService_Exporter_Base {
async init(destDir) {
this.destDir_ = destDir;
this.resourceDir_ = destDir ? `${destDir}/resources` : null;
await shim.fsDriver().mkdir(this.destDir_);
await shim.fsDriver().mkdir(this.resourceDir_);
}
async processItem(ItemClass, item) {
const fileName = ItemClass.systemPath(item, 'json');
const filePath = `${this.destDir_}/${fileName}`;
const serialized = JSON.stringify(item);
await shim.fsDriver().writeFile(filePath, serialized, 'utf-8');
}
async processResource(resource, filePath) {
const destResourcePath = `${this.resourceDir_}/${basename(filePath)}`;
await shim.fsDriver().copy(filePath, destResourcePath);
}
async close() {}
}
module.exports = InteropService_Exporter_Json;

View File

@ -1,27 +0,0 @@
const Setting = require('lib/models/Setting');
class InteropService_Importer_Base {
setMetadata(md) {
this.metadata_ = md;
}
metadata() {
return this.metadata_;
}
async init(sourcePath, options) {
this.sourcePath_ = sourcePath;
this.options_ = options;
}
async exec() {}
async temporaryDirectory_(createIt) {
const md5 = require('md5');
const tempDir = `${Setting.value('tempDir')}/${md5(Math.random() + Date.now())}`;
if (createIt) await require('fs-extra').mkdirp(tempDir);
return tempDir;
}
}
module.exports = InteropService_Importer_Base;

View File

@ -1,4 +1,4 @@
const Setting = require('lib/models/Setting');
const Setting = require('lib/models/Setting').default;
const ItemChange = require('lib/models/ItemChange');
class ItemChangeUtils {

View File

@ -1,9 +1,9 @@
import { KeyboardEvent } from 'react';
import eventManager from 'lib/eventManager';
import shim from 'lib/shim';
import { _ } from 'lib/locale';
const BaseService = require('lib/services/BaseService');
const eventManager = require('lib/eventManager');
const { shim } = require('lib/shim');
const { _ } = require('lib/locale');
const BaseService = require('lib/services/BaseService').default;
const keysRegExp = /^([0-9A-Z)!@#$%^&*(:+<_>?~{|}";=,\-./`[\\\]']|F1*[1-9]|F10|F2[0-4]|Plus|Space|Tab|Backspace|Delete|Insert|Return|Enter|Up|Down|Left|Right|Home|End|PageUp|PageDown|Escape|Esc|VolumeUp|VolumeDown|VolumeMute|MediaNextTrack|MediaPreviousTrack|MediaStop|MediaPlayPause|PrintScreen)$/;
const modifiersRegExp = {
@ -94,15 +94,22 @@ export default class KeymapService extends BaseService {
private platform: string;
private customKeymapPath: string;
private defaultKeymapItems: KeymapItem[];
private lastSaveTime_:number;
constructor() {
super();
this.lastSaveTime_ = Date.now();
// By default, initialize for the current platform
// Manual initialization allows testing for other platforms
this.initialize();
}
get lastSaveTime():number {
return this.lastSaveTime_;
}
initialize(platform: string = shim.platformName()) {
this.platform = platform;
@ -130,14 +137,9 @@ export default class KeymapService extends BaseService {
if (await shim.fsDriver().exists(customKeymapPath)) {
this.logger().info(`KeymapService: Loading keymap from file: ${customKeymapPath}`);
try {
const customKeymapFile = await shim.fsDriver().readFile(customKeymapPath, 'utf-8');
// Custom keymaps are supposed to contain an array of keymap items
this.overrideKeymap(JSON.parse(customKeymapFile));
} catch (err) {
const message = err.message || '';
throw new Error(_('Error: %s', message));
}
const customKeymapFile = await shim.fsDriver().readFile(customKeymapPath, 'utf-8');
// Custom keymaps are supposed to contain an array of keymap items
this.overrideKeymap(JSON.parse(customKeymapFile));
}
}
@ -149,6 +151,8 @@ export default class KeymapService extends BaseService {
const customKeymapItems = this.getCustomKeymapItems();
await shim.fsDriver().writeFile(customKeymapPath, JSON.stringify(customKeymapItems, null, 2), 'utf-8');
this.lastSaveTime_ = Date.now();
// Refresh the menu items so that the changes are reflected
eventManager.emit('keymapChange');
} catch (err) {
@ -161,6 +165,28 @@ export default class KeymapService extends BaseService {
return !!this.keymap[command];
}
private convertToPlatform(accelerator:string) {
return accelerator
.replace(/CmdOrCtrl/g, this.platform === 'darwin' ? 'Cmd' : 'Ctrl')
.replace(/Option/g, this.platform === 'darwin' ? 'Option' : 'Alt')
.replace(/Alt/g, this.platform === 'darwin' ? 'Option' : 'Alt');
}
registerCommandAccelerator(commandName:string, accelerator:string) {
// If the command is already registered, we don't register it again and
// we don't update the accelerator. This is because it might have been
// modified by the user and we don't want the plugin to overwrite this.
if (this.keymap[commandName]) return;
const validatedAccelerator = this.convertToPlatform(accelerator);
this.validateAccelerator(validatedAccelerator);
this.keymap[commandName] = {
command: commandName,
accelerator: validatedAccelerator,
};
}
setAccelerator(command: string, accelerator: string) {
this.keymap[command].accelerator = accelerator;
}
@ -199,6 +225,12 @@ export default class KeymapService extends BaseService {
}
});
for (const commandName in this.keymap) {
if (!this.defaultKeymapItems.find((item:KeymapItem) => item.command === commandName)) {
customkeymapItems.push(this.keymap[commandName]);
}
}
return customkeymapItems;
}
@ -213,7 +245,14 @@ export default class KeymapService extends BaseService {
// Validate individual custom keymap items
// Throws if there are any issues in the keymap item
this.validateKeymapItem(item);
this.setAccelerator(item.command, item.accelerator);
// If the command does not exist in the keymap, we are loading a new
// command accelerator so we need to register it.
if (!this.keymap[item.command]) {
this.registerCommandAccelerator(item.command, item.accelerator);
} else {
this.setAccelerator(item.command, item.accelerator);
}
}
// Validate the entire keymap for duplicates
@ -227,18 +266,18 @@ export default class KeymapService extends BaseService {
private validateKeymapItem(item: KeymapItem) {
if (!item.hasOwnProperty('command')) {
throw new Error(_('"%s" is missing the required "%s" property.', JSON.stringify(item), 'command'));
} else if (!this.keymap.hasOwnProperty(item.command)) {
throw new Error(_('Invalid %s: %s.', 'command', item.command));
throw new Error(_('"%s" is missing the required "%s" property.', JSON.stringify(item), _('command')));
// } else if (!this.keymap.hasOwnProperty(item.command)) {
// throw new Error(_('Invalid %s: %s.', _('command'), item.command));
}
if (!item.hasOwnProperty('accelerator')) {
throw new Error(_('"%s" is missing the required "%s" property.', JSON.stringify(item), 'accelerator'));
throw new Error(_('"%s" is missing the required "%s" property.', JSON.stringify(item), _('accelerator')));
} else if (item.accelerator !== null) {
try {
this.validateAccelerator(item.accelerator);
} catch {
throw new Error(_('Invalid %s: %s.', 'accelerator', item.command));
throw new Error(_('Invalid %s: %s.', _('accelerator'), item.command));
}
}
}
@ -355,7 +394,9 @@ export default class KeymapService extends BaseService {
eventManager.off(eventName, callback);
}
static instance() {
private static instance_:KeymapService = null;
static instance():KeymapService {
if (this.instance_) return this.instance_;
this.instance_ = new KeymapService();

View File

@ -1,4 +1,4 @@
const BaseService = require('lib/services/BaseService.js');
const BaseService = require('lib/services/BaseService').default;
const Mutex = require('async-mutex').Mutex;
class KvStore extends BaseService {

View File

@ -1,4 +1,4 @@
const BaseService = require('lib/services/BaseService');
const BaseService = require('lib/services/BaseService').default;
const Migration = require('lib/models/Migration');
class MigrationService extends BaseService {

View File

@ -1,4 +1,4 @@
const { Logger } = require('lib/logger.js');
const Logger = require('lib/Logger').default;
const KeymapService = require('lib/services/KeymapService').default;
class PluginManager {

View File

@ -1,12 +1,12 @@
import AsyncActionQueue from '../../AsyncActionQueue';
const { Logger } = require('lib/logger.js');
const Setting = require('lib/models/Setting');
import shim from 'lib/shim';
import { _ } from 'lib/locale';
const Logger = require('lib/Logger').default;
const Setting = require('lib/models/Setting').default;
const Resource = require('lib/models/Resource');
const { shim } = require('lib/shim');
const EventEmitter = require('events');
const chokidar = require('chokidar');
const { bridge } = require('electron').remote.require('./bridge');
const { _ } = require('lib/locale');
const bridge = require('electron').remote.require('./bridge').default;
interface WatchedItem {
resourceId: string,

View File

@ -1,11 +1,9 @@
import produce, { Draft, setAutoFreeze } from 'immer';
import produce, { Draft } from 'immer';
export const defaultState = {
watchedResources: {},
};
setAutoFreeze(false); // TODO: REMOVE ONCE PLUGIN BRANCH HAS BEEN MERGED!!
const reducer = produce((draft: Draft<any>, action:any) => {
if (action.type.indexOf('RESOURCE_EDIT_WATCHER_') !== 0) return;

View File

@ -1,11 +1,11 @@
const Resource = require('lib/models/Resource');
const Setting = require('lib/models/Setting');
const BaseService = require('lib/services/BaseService');
const Setting = require('lib/models/Setting').default;
const BaseService = require('lib/services/BaseService').default;
const ResourceService = require('lib/services/ResourceService');
const { Dirnames } = require('lib/services/synchronizer/utils/types');
const { Logger } = require('lib/logger.js');
const Logger = require('lib/Logger').default;
const EventEmitter = require('events');
const { shim } = require('lib/shim');
const shim = require('lib/shim').default;
class ResourceFetcher extends BaseService {
constructor(fileApi = null) {
@ -196,14 +196,14 @@ class ResourceFetcher extends BaseService {
async waitForAllFinished() {
return new Promise((resolve) => {
const iid = setInterval(() => {
const iid = shim.setInterval(() => {
if (!this.updateReportIID_ &&
!this.scheduleQueueProcessIID_ &&
!this.queue_.length &&
!this.autoAddResourcesCalls_.length &&
!Object.getOwnPropertyNames(this.fetchingItems_).length) {
clearInterval(iid);
shim.clearInterval(iid);
resolve();
}
}, 100);
@ -245,11 +245,11 @@ class ResourceFetcher extends BaseService {
scheduleQueueProcess() {
if (this.scheduleQueueProcessIID_) {
clearTimeout(this.scheduleQueueProcessIID_);
shim.clearTimeout(this.scheduleQueueProcessIID_);
this.scheduleQueueProcessIID_ = null;
}
this.scheduleQueueProcessIID_ = setTimeout(() => {
this.scheduleQueueProcessIID_ = shim.setTimeout(() => {
this.processQueue_();
this.scheduleQueueProcessIID_ = null;
}, 100);
@ -258,7 +258,7 @@ class ResourceFetcher extends BaseService {
scheduleAutoAddResources() {
if (this.scheduleAutoAddResourcesIID_) return;
this.scheduleAutoAddResourcesIID_ = setTimeout(() => {
this.scheduleAutoAddResourcesIID_ = shim.setTimeout(() => {
this.scheduleAutoAddResourcesIID_ = null;
ResourceFetcher.instance().autoAddResources();
}, 1000);
@ -272,11 +272,11 @@ class ResourceFetcher extends BaseService {
async destroy() {
this.eventEmitter_.removeAllListeners();
if (this.scheduleQueueProcessIID_) {
clearTimeout(this.scheduleQueueProcessIID_);
shim.clearTimeout(this.scheduleQueueProcessIID_);
this.scheduleQueueProcessIID_ = null;
}
if (this.scheduleAutoAddResourcesIID_) {
clearTimeout(this.scheduleAutoAddResourcesIID_);
shim.clearTimeout(this.scheduleAutoAddResourcesIID_);
this.scheduleAutoAddResourcesIID_ = null;
}
await this.waitForAllFinished();

View File

@ -3,10 +3,10 @@ const NoteResource = require('lib/models/NoteResource');
const Note = require('lib/models/Note');
const Resource = require('lib/models/Resource');
const BaseModel = require('lib/BaseModel');
const BaseService = require('lib/services/BaseService');
const BaseService = require('lib/services/BaseService').default;
const SearchEngine = require('lib/services/searchengine/SearchEngine');
const Setting = require('lib/models/Setting');
const { shim } = require('lib/shim');
const Setting = require('lib/models/Setting').default;
const shim = require('lib/shim').default;
const ItemChangeUtils = require('lib/services/ItemChangeUtils');
const { sprintf } = require('sprintf-js');
@ -160,7 +160,7 @@ class ResourceService extends BaseService {
this.isRunningInBackground_ = true;
const service = this.instance();
service.maintenanceTimer1_ = setTimeout(() => {
service.maintenanceTimer1_ = shim.setTimeout(() => {
service.maintenance();
}, 1000 * 30);
@ -171,7 +171,7 @@ class ResourceService extends BaseService {
async cancelTimers() {
if (this.maintenanceTimer1_) {
clearTimeout(this.maintenanceTimer1);
shim.clearTimeout(this.maintenanceTimer1);
this.maintenanceTimer1_ = null;
}
if (this.maintenanceTimer2_) {
@ -180,9 +180,9 @@ class ResourceService extends BaseService {
}
return new Promise((resolve) => {
const iid = setInterval(() => {
const iid = shim.setInterval(() => {
if (!this.maintenanceCalls_.length) {
clearInterval(iid);
shim.clearInterval(iid);
resolve();
}
}, 100);

View File

@ -1,14 +1,15 @@
const ItemChange = require('lib/models/ItemChange');
const Note = require('lib/models/Note');
const Folder = require('lib/models/Folder');
const Setting = require('lib/models/Setting');
const Setting = require('lib/models/Setting').default;
const Revision = require('lib/models/Revision');
const BaseModel = require('lib/BaseModel');
const ItemChangeUtils = require('lib/services/ItemChangeUtils');
const { shim } = require('lib/shim');
const BaseService = require('lib/services/BaseService');
const { _ } = require('lib/locale.js');
const shim = require('lib/shim').default;
const BaseService = require('lib/services/BaseService').default;
const { _ } = require('lib/locale');
const { sprintf } = require('sprintf-js');
const { wrapError } = require('lib/errorUtils');
class RevisionService extends BaseService {
constructor() {
@ -69,36 +70,41 @@ class RevisionService extends BaseService {
}
async createNoteRevision_(note, parentRevId = null) {
const parentRev = parentRevId ? await Revision.load(parentRevId) : await Revision.latestRevision(BaseModel.TYPE_NOTE, note.id);
try {
const parentRev = parentRevId ? await Revision.load(parentRevId) : await Revision.latestRevision(BaseModel.TYPE_NOTE, note.id);
const output = {
parent_id: '',
item_type: BaseModel.TYPE_NOTE,
item_id: note.id,
item_updated_time: note.updated_time,
};
const output = {
parent_id: '',
item_type: BaseModel.TYPE_NOTE,
item_id: note.id,
item_updated_time: note.updated_time,
};
const noteMd = this.noteMetadata_(note);
const noteTitle = note.title ? note.title : '';
const noteBody = note.body ? note.body : '';
const noteMd = this.noteMetadata_(note);
const noteTitle = note.title ? note.title : '';
const noteBody = note.body ? note.body : '';
if (!parentRev) {
output.title_diff = Revision.createTextPatch('', noteTitle);
output.body_diff = Revision.createTextPatch('', noteBody);
output.metadata_diff = Revision.createObjectPatch({}, noteMd);
} else {
if (Date.now() - parentRev.updated_time < Setting.value('revisionService.intervalBetweenRevisions')) return null;
if (!parentRev) {
output.title_diff = Revision.createTextPatch('', noteTitle);
output.body_diff = Revision.createTextPatch('', noteBody);
output.metadata_diff = Revision.createObjectPatch({}, noteMd);
} else {
if (Date.now() - parentRev.updated_time < Setting.value('revisionService.intervalBetweenRevisions')) return null;
const merged = await Revision.mergeDiffs(parentRev);
output.parent_id = parentRev.id;
output.title_diff = Revision.createTextPatch(merged.title, noteTitle);
output.body_diff = Revision.createTextPatch(merged.body, noteBody);
output.metadata_diff = Revision.createObjectPatch(merged.metadata, noteMd);
const merged = await Revision.mergeDiffs(parentRev);
output.parent_id = parentRev.id;
output.title_diff = Revision.createTextPatch(merged.title, noteTitle);
output.body_diff = Revision.createTextPatch(merged.body, noteBody);
output.metadata_diff = Revision.createObjectPatch(merged.metadata, noteMd);
}
if (this.isEmptyRevision_(output)) return null;
return Revision.save(output);
} catch (error) {
const newError = wrapError(`Could not create revision for note: ${note.id}`, error);
throw newError;
}
if (this.isEmptyRevision_(output)) return null;
return Revision.save(output);
}
async collectRevisions() {
@ -270,7 +276,7 @@ class RevisionService extends BaseService {
this.logger().info(`RevisionService::runInBackground: Starting background service with revision collection interval ${collectRevisionInterval}`);
this.maintenanceTimer1_ = setTimeout(() => {
this.maintenanceTimer1_ = shim.setTimeout(() => {
this.maintenance();
}, 1000 * 4);
@ -281,7 +287,7 @@ class RevisionService extends BaseService {
async cancelTimers() {
if (this.maintenanceTimer1_) {
clearTimeout(this.maintenanceTimer1);
shim.clearTimeout(this.maintenanceTimer1);
this.maintenanceTimer1_ = null;
}
if (this.maintenanceTimer2_) {
@ -290,9 +296,9 @@ class RevisionService extends BaseService {
}
return new Promise((resolve) => {
const iid = setInterval(() => {
const iid = shim.setInterval(() => {
if (!this.maintenanceCalls_.length) {
clearInterval(iid);
shim.clearInterval(iid);
resolve();
}
}, 100);

View File

@ -1,8 +1,8 @@
/* eslint-disable import/prefer-default-export */
import KeychainService from './keychain/KeychainService';
const Setting = require('lib/models/Setting');
const { uuid } = require('lib/uuid.js');
const Setting = require('lib/models/Setting').default;
const uuid = require('lib/uuid').default;
// This function takes care of initialising both the keychain service and settings.
//

View File

@ -0,0 +1,128 @@
import CommandService from '../CommandService';
import KeymapService from '../KeymapService';
import propsHaveChanged from './propsHaveChanged';
const { createSelectorCreator, defaultMemoize } = require('reselect');
const { createCachedSelector } = require('re-reselect');
interface MenuItem {
id: string,
label: string,
click: Function,
role?: any,
accelerator?: string,
}
interface MenuItems {
[key: string]: MenuItem,
}
interface MenuItemProps {
[key:string]: any,
}
interface MenuItemPropsCache {
[key:string]: any,
}
interface MenuItemCache {
[key:string]: MenuItems,
}
const createShallowObjectEqualSelector = createSelectorCreator(
defaultMemoize,
(prev:any, next:any) => {
if (Object.keys(prev).length !== Object.keys(next).length) return false;
for (const n in prev) {
if (prev[n] !== next[n]) return false;
}
return true;
}
);
// This selector ensures that for the given command names, the same toolbar
// button array is returned if the underlying toolbar buttons have not changed.
const selectObjectByCommands = createCachedSelector(
(state:any) => state.array,
(array:any[]) => array
)({
keySelector: (_state:any, commandNames:string[]) => {
return commandNames.join('_');
},
selectorCreator: createShallowObjectEqualSelector,
});
export default class MenuUtils {
private service_:CommandService;
private menuItemCache_:MenuItemCache = {};
private menuItemPropsCache_:MenuItemPropsCache = {};
constructor(service:CommandService) {
this.service_ = service;
}
private get service():CommandService {
return this.service_;
}
private get keymapService():KeymapService {
return KeymapService.instance();
}
private commandToMenuItem(commandName:string, onClick:Function):MenuItem {
const command = this.service.commandByName(commandName);
const item:MenuItem = {
id: command.declaration.name,
label: this.service.label(commandName),
click: () => onClick(command.declaration.name),
};
if (command.declaration.role) item.role = command.declaration.role;
if (this.keymapService && this.keymapService.acceleratorExists(commandName)) {
item.accelerator = this.keymapService.getAccelerator(commandName);
}
return item;
}
public commandToStatefulMenuItem(commandName:string, props:any = null):MenuItem {
const output = this.commandsToMenuItems([commandName], () => {
return this.service.execute(commandName, props ? props : {});
});
return output[commandName];
}
public commandsToMenuItems(commandNames:string[], onClick:Function):MenuItems {
const key:string = `${this.keymapService.lastSaveTime}_${commandNames.join('_')}`;
if (this.menuItemCache_[key]) return this.menuItemCache_[key];
const output:MenuItems = {};
for (const commandName of commandNames) {
output[commandName] = this.commandToMenuItem(commandName, onClick);
}
this.menuItemCache_[key] = output;
return output;
}
public commandsToMenuItemProps(state:any, commandNames:string[]):MenuItemProps {
const output:MenuItemProps = {};
for (const commandName of commandNames) {
const newProps = this.service.commandMapStateToProps(commandName, state);
if (newProps === null || propsHaveChanged(this.menuItemPropsCache_[commandName], newProps)) {
output[commandName] = newProps;
this.menuItemPropsCache_[commandName] = newProps;
} else {
output[commandName] = this.menuItemPropsCache_[commandName];
}
}
return selectObjectByCommands({ array: output }, commandNames);
}
}

View File

@ -0,0 +1,83 @@
import CommandService from '../CommandService';
import propsHaveChanged from './propsHaveChanged';
import { stateUtils } from 'lib/reducer';
const separatorItem = { type: 'separator' };
export interface ToolbarButtonInfo {
name: string,
tooltip: string,
iconName: string,
enabled: boolean,
onClick():void,
title: string,
}
interface ToolbarButtonCacheItem {
props: any,
info: ToolbarButtonInfo,
}
interface ToolbarButtonCache {
[key:string]: ToolbarButtonCacheItem,
}
export default class ToolbarButtonUtils {
private service_:CommandService;
private toolbarButtonCache_:ToolbarButtonCache = {};
constructor(service:CommandService) {
this.service_ = service;
}
private get service():CommandService {
return this.service_;
}
private commandToToolbarButton(commandName:string, props:any):ToolbarButtonInfo {
if (this.toolbarButtonCache_[commandName] && !propsHaveChanged(this.toolbarButtonCache_[commandName].props, props)) {
return this.toolbarButtonCache_[commandName].info;
}
const command = this.service.commandByName(commandName, { runtimeMustBeRegistered: true });
const output = {
name: commandName,
tooltip: this.service.label(commandName),
iconName: command.declaration.iconName,
enabled: this.service.isEnabled(commandName, props),
onClick: async () => {
this.service.execute(commandName, props);
},
title: this.service.title(commandName, props),
};
this.toolbarButtonCache_[commandName] = {
props: props,
info: output,
};
return this.toolbarButtonCache_[commandName].info;
}
// This method ensures that if the provided commandNames and state hasn't changed
// the output also won't change. Invididual toolbarButtonInfo also won't changed
// if the state they use hasn't changed. This is to avoid useless renders of the toolbars.
public commandsToToolbarButtons(state:any, commandNames:string[]):ToolbarButtonInfo[] {
const output:ToolbarButtonInfo[] = [];
for (const commandName of commandNames) {
if (commandName === '-') {
output.push(separatorItem as any);
continue;
}
const props = this.service.commandMapStateToProps(commandName, state);
output.push(this.commandToToolbarButton(commandName, props));
}
return stateUtils.selectArrayShallow({ array: output }, commandNames.join('_'));
}
}

View File

@ -0,0 +1,13 @@
export default function propsHaveChanged(previous:any, next:any):boolean {
if (!previous && next) return true;
if (Object.keys(previous).length !== Object.keys(next).length) return true;
for (const n in previous) {
if (previous[n] !== next[n]) {
return true;
}
}
return false;
}

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,8 @@
import { ModuleType, FileSystemItem, ImportModuleOutputFormat, Module, ImportOptions, ExportOptions, ImportExportResult, defaultImportExportModule } from './types';
import InteropService_Importer_Custom from './InteropService_Importer_Custom';
import InteropService_Exporter_Custom from './InteropService_Exporter_Custom';
import shim from 'lib/shim';
import { _ } from 'lib/locale';
const BaseItem = require('lib/models/BaseItem.js');
const BaseModel = require('lib/BaseModel.js');
const Resource = require('lib/models/Resource.js');
@ -6,133 +11,130 @@ const NoteTag = require('lib/models/NoteTag.js');
const Note = require('lib/models/Note.js');
const ArrayUtils = require('lib/ArrayUtils');
const { sprintf } = require('sprintf-js');
const { shim } = require('lib/shim');
const { _ } = require('lib/locale');
const { fileExtension } = require('lib/path-utils');
const { toTitleCase } = require('lib/string-utils');
const EventEmitter = require('events');
export default class InteropService {
private defaultModules_:Module[];
private userModules_:Module[] = [];
private eventEmitter_:any = null;
private static instance_:InteropService;
public static instance():InteropService {
if (!this.instance_) this.instance_ = new InteropService();
return this.instance_;
}
class InteropService {
constructor() {
this.modules_ = null;
this.eventEmitter_ = new EventEmitter();
}
on(eventName:string, callback:Function) {
return this.eventEmitter_.on(eventName, callback);
}
off(eventName:string, callback:Function) {
return this.eventEmitter_.removeListener(eventName, callback);
}
modules() {
if (this.modules_) return this.modules_;
// - canDoMultiExport: Tells whether the format can package multiple notes into one file. Default: true.
let importModules = [
{
format: 'jex',
fileExtensions: ['jex'],
sources: ['file'],
description: _('Joplin Export File'),
},
{
format: 'md',
fileExtensions: ['md', 'markdown', 'txt'],
sources: ['file', 'directory'],
isNoteArchive: false, // Tells whether the file can contain multiple notes (eg. Enex or Jex format)
description: _('Markdown'),
},
{
format: 'raw',
sources: ['directory'],
description: _('Joplin Export Directory'),
},
{
format: 'enex',
fileExtensions: ['enex'],
sources: ['file'],
description: _('Evernote Export File (as Markdown)'),
importerClass: 'InteropService_Importer_EnexToMd',
isDefault: true,
},
{
format: 'enex',
fileExtensions: ['enex'],
sources: ['file'],
description: _('Evernote Export File (as HTML)'),
// TODO: Consider doing this the same way as the multiple `md` importers are handled
importerClass: 'InteropService_Importer_EnexToHtml',
outputFormat: 'html',
},
];
let exportModules = [
{
format: 'jex',
fileExtensions: ['jex'],
target: 'file',
canDoMultiExport: true,
description: _('Joplin Export File'),
},
{
format: 'raw',
target: 'directory',
description: _('Joplin Export Directory'),
},
{
format: 'json',
target: 'directory',
description: _('Json Export Directory'),
},
{
format: 'md',
target: 'directory',
description: _('Markdown'),
},
{
format: 'html',
fileExtensions: ['html', 'htm'],
target: 'file',
canDoMultiExport: false,
description: _('HTML File'),
},
{
format: 'html',
target: 'directory',
description: _('HTML Directory'),
},
];
importModules = importModules.map(a => {
const className = a.importerClass || `InteropService_Importer_${toTitleCase(a.format)}`;
const output = Object.assign({}, {
type: 'importer',
path: `lib/services/${className}`,
outputFormat: 'md',
}, a);
if (!('isNoteArchive' in output)) output.isNoteArchive = true;
return output;
});
exportModules = exportModules.map(a => {
const className = `InteropService_Exporter_${toTitleCase(a.format)}`;
return Object.assign(
{},
if (!this.defaultModules_) {
const importModules:Module[] = [
{
type: 'exporter',
path: `lib/services/${className}`,
...defaultImportExportModule(ModuleType.Importer),
format: 'jex',
fileExtensions: ['jex'],
sources: [FileSystemItem.File],
description: _('Joplin Export File'),
},
a
);
});
{
...defaultImportExportModule(ModuleType.Importer),
format: 'md',
fileExtensions: ['md', 'markdown', 'txt'],
sources: [FileSystemItem.File, FileSystemItem.Directory],
isNoteArchive: false, // Tells whether the file can contain multiple notes (eg. Enex or Jex format)
description: _('Markdown'),
},
{
...defaultImportExportModule(ModuleType.Importer),
format: 'raw',
sources: [FileSystemItem.Directory],
description: _('Joplin Export Directory'),
},
{
...defaultImportExportModule(ModuleType.Importer),
format: 'enex',
fileExtensions: ['enex'],
sources: [FileSystemItem.File],
description: _('Evernote Export File (as Markdown)'),
importerClass: 'InteropService_Importer_EnexToMd',
isDefault: true,
},
{
...defaultImportExportModule(ModuleType.Importer),
format: 'enex',
fileExtensions: ['enex'],
sources: [FileSystemItem.File],
description: _('Evernote Export File (as HTML)'),
// TODO: Consider doing this the same way as the multiple `md` importers are handled
importerClass: 'InteropService_Importer_EnexToHtml',
outputFormat: ImportModuleOutputFormat.Html,
},
];
this.modules_ = importModules.concat(exportModules);
const exportModules:Module[] = [
{
...defaultImportExportModule(ModuleType.Exporter),
format: 'jex',
fileExtensions: ['jex'],
target: FileSystemItem.File,
description: _('Joplin Export File'),
},
{
...defaultImportExportModule(ModuleType.Exporter),
format: 'raw',
target: FileSystemItem.Directory,
description: _('Joplin Export Directory'),
},
{
...defaultImportExportModule(ModuleType.Exporter),
format: 'md',
target: FileSystemItem.Directory,
description: _('Markdown'),
},
{
...defaultImportExportModule(ModuleType.Exporter),
format: 'html',
fileExtensions: ['html', 'htm'],
target: FileSystemItem.File,
isNoteArchive: false,
description: _('HTML File'),
},
{
...defaultImportExportModule(ModuleType.Exporter),
format: 'html',
target: FileSystemItem.Directory,
description: _('HTML Directory'),
},
];
this.modules_ = this.modules_.map(a => {
a.fullLabel = function(moduleSource = null) {
const label = [`${this.format.toUpperCase()} - ${this.description}`];
if (moduleSource && this.sources.length > 1) {
label.push(`(${moduleSource === 'file' ? _('File') : _('Directory')})`);
}
return label.join(' ');
};
return a;
});
this.defaultModules_ = importModules.concat(exportModules);
}
return this.modules_;
return this.defaultModules_.concat(this.userModules_);
}
public registerModule(module:Module) {
module = {
...defaultImportExportModule(module.type),
...module,
};
this.userModules_.push(module);
this.eventEmitter_.emit('modulesChanged');
}
// Find the module that matches the given type ("importer" or "exporter")
@ -140,7 +142,7 @@ class InteropService {
// or exporters, such as ENEX. In this case, the one marked as "isDefault"
// is returned. This is useful to auto-detect the module based on the format.
// For more precise matching, newModuleFromPath_ should be used.
findModuleByFormat_(type, format, target = null, outputFormat = null) {
findModuleByFormat_(type:ModuleType, format:string, target:FileSystemItem = null, outputFormat:ImportModuleOutputFormat = null) {
const modules = this.modules();
const matches = [];
for (let i = 0; i < modules.length; i++) {
@ -162,6 +164,24 @@ class InteropService {
return matches.length ? matches[0] : null;
}
private modulePath(module:Module) {
let className = '';
if (module.type === ModuleType.Importer) {
className = module.importerClass || `InteropService_Importer_${toTitleCase(module.format)}`;
} else {
className = `InteropService_Exporter_${toTitleCase(module.format)}`;
}
return `lib/services/interop/${className}`;
}
private newModuleFromCustomFactory(module:Module) {
if (module.type === ModuleType.Importer) {
return new InteropService_Importer_Custom(module);
} else {
return new InteropService_Exporter_Custom(module);
}
}
/**
* NOTE TO FUTURE SELF: It might make sense to simply move all the existing
* formatters to the `newModuleFromPath_` approach, so that there's only one way
@ -169,12 +189,21 @@ class InteropService {
* https://github.com/laurent22/joplin/pull/1795#discussion_r322379121) but
* we can do it if it ever becomes necessary.
*/
newModuleByFormat_(type, format, outputFormat = 'md') {
newModuleByFormat_(type:ModuleType, format:string, outputFormat:ImportModuleOutputFormat = ImportModuleOutputFormat.Markdown) {
const moduleMetadata = this.findModuleByFormat_(type, format, null, outputFormat);
if (!moduleMetadata) throw new Error(_('Cannot load "%s" module for format "%s" and output "%s"', type, format, outputFormat));
const ModuleClass = require(moduleMetadata.path);
const output = new ModuleClass();
let output = null;
if (moduleMetadata.isCustom) {
output = this.newModuleFromCustomFactory(moduleMetadata);
} else {
const ModuleClass = require(this.modulePath(moduleMetadata)).default;
output = new ModuleClass();
}
output.setMetadata(moduleMetadata);
return output;
}
@ -186,21 +215,32 @@ class InteropService {
*
* https://github.com/laurent22/joplin/pull/1795#pullrequestreview-281574417
*/
newModuleFromPath_(type, options) {
newModuleFromPath_(type:ModuleType, options:any) {
let modulePath = options && options.modulePath ? options.modulePath : '';
if (!modulePath) {
const moduleMetadata = this.findModuleByFormat_(type, options.format, options.target);
modulePath = moduleMetadata.path;
if (!moduleMetadata) throw new Error(_('Cannot load "%s" module for format "%s" and target "%s"', type, options.format, options.target));
modulePath = this.modulePath(moduleMetadata);
}
const ModuleClass = require(modulePath);
const output = new ModuleClass();
const moduleMetadata = this.findModuleByFormat_(type, options.format, options.target);
output.setMetadata({ options, ...moduleMetadata }); // TODO: Check that this metadata is equivalent to module above
let output = null;
if (moduleMetadata.isCustom) {
output = this.newModuleFromCustomFactory(moduleMetadata);
} else {
const ModuleClass = require(modulePath).default;
output = new ModuleClass();
}
output.setMetadata({ options, ...moduleMetadata });
return output;
}
moduleByFileExtension_(type, ext) {
moduleByFileExtension_(type:ModuleType, ext:string) {
ext = ext.toLowerCase();
const modules = this.modules();
@ -214,21 +254,18 @@ class InteropService {
return null;
}
async import(options) {
async import(options:ImportOptions):Promise<ImportExportResult> {
if (!(await shim.fsDriver().exists(options.path))) throw new Error(_('Cannot find "%s".', options.path));
options = Object.assign(
{},
{
format: 'auto',
destinationFolderId: null,
destinationFolder: null,
},
options
);
options = {
format: 'auto',
destinationFolderId: null,
destinationFolder: null,
...options,
};
if (options.format === 'auto') {
const module = this.moduleByFileExtension_('importer', fileExtension(options.path));
const module = this.moduleByFileExtension_(ModuleType.Importer, fileExtension(options.path));
if (!module) throw new Error(_('Please specify import format for %s', options.path));
// eslint-disable-next-line require-atomic-updates
options.format = module.format;
@ -241,14 +278,14 @@ class InteropService {
options.destinationFolder = folder;
}
let result = { warnings: [] };
let result:ImportExportResult = { warnings: [] };
let importer = null;
if (options.modulePath) {
importer = this.newModuleFromPath_('importer', options);
importer = this.newModuleFromPath_(ModuleType.Importer, options);
} else {
importer = this.newModuleByFormat_('importer', options.format, options.outputFormat);
importer = this.newModuleByFormat_(ModuleType.Importer, options.format, options.outputFormat);
}
await importer.init(options.path, options);
@ -257,17 +294,19 @@ class InteropService {
return result;
}
async export(options) {
options = Object.assign({}, options);
if (!options.format) options.format = 'jex';
async export(options:ExportOptions):Promise<ImportExportResult> {
options = {
format: 'jex',
...options,
};
const exportPath = options.path ? options.path : null;
let sourceFolderIds = options.sourceFolderIds ? options.sourceFolderIds : [];
const sourceNoteIds = options.sourceNoteIds ? options.sourceNoteIds : [];
const result = { warnings: [] };
const itemsToExport = [];
const result:ImportExportResult = { warnings: [] };
const itemsToExport:any[] = [];
const queueExportItem = (itemType, itemOrId) => {
const queueExportItem = (itemType:number, itemOrId:any) => {
itemsToExport.push({
type: itemType,
itemOrId: itemOrId,
@ -275,7 +314,7 @@ class InteropService {
};
const exportedNoteIds = [];
let resourceIds = [];
let resourceIds:string[] = [];
// Recursively get all the folders that have valid parents
const folderIds = await Folder.childrenIds('', true);
@ -329,11 +368,11 @@ class InteropService {
await queueExportItem(BaseModel.TYPE_TAG, exportedTagIds[i]);
}
const exporter = this.newModuleFromPath_('exporter', options);
const exporter = this.newModuleFromPath_(ModuleType.Exporter, options);
await exporter.init(exportPath, options);
const typeOrder = [BaseModel.TYPE_FOLDER, BaseModel.TYPE_RESOURCE, BaseModel.TYPE_NOTE, BaseModel.TYPE_TAG, BaseModel.TYPE_NOTE_TAG];
const context = {
const context:any = {
resourcePaths: {},
};
@ -373,7 +412,7 @@ class InteropService {
await exporter.processResource(item, resourcePath);
}
await exporter.processItem(ItemClass, item);
await exporter.processItem(itemType, item);
} catch (error) {
console.error(error);
result.warnings.push(error.message);
@ -386,5 +425,3 @@ class InteropService {
return result;
}
}
module.exports = InteropService;

View File

@ -0,0 +1,41 @@
/* eslint @typescript-eslint/no-unused-vars: 0, no-unused-vars: ["error", { "argsIgnorePattern": ".*" }], */
const Setting = require('lib/models/Setting').default;
export default class InteropService_Exporter_Base {
private context_:any = {};
private metadata_:any = {};
// @ts-ignore
async init(destDir:string, options:any = {}) {}
// @ts-ignore
async prepareForProcessingItemType(itemType:number, itemsToExport:any[]) {}
// @ts-ignore
async processItem(itemType:number, item:any) {}
// @ts-ignore
async processResource(resource:any, filePath:string) {}
async close() {}
setMetadata(md:any) {
this.metadata_ = md;
}
metadata() {
return this.metadata_;
}
updateContext(context:any) {
this.context_ = Object.assign({}, this.context_, context);
}
context() {
return this.context_;
}
async temporaryDirectory_(createIt:boolean) {
const md5 = require('md5');
const tempDir = `${Setting.value('tempDir')}/${md5(Math.random() + Date.now())}`;
if (createIt) await require('fs-extra').mkdirp(tempDir);
return tempDir;
}
}

View File

@ -0,0 +1,35 @@
import { ExportContext } from '../plugins/api/types';
import InteropService_Exporter_Base from './InteropService_Exporter_Base';
import { ExportOptions, Module } from './types';
export default class InteropService_Exporter_Custom extends InteropService_Exporter_Base {
private customContext_:ExportContext;
private module_:Module = null;
constructor(module:Module) {
super();
this.module_ = module;
}
async init(destPath:string, options:ExportOptions) {
this.customContext_ = {
destPath: destPath,
options: options,
};
return this.module_.onInit(this.customContext_);
}
async processItem(itemType:number, item:any) {
return this.module_.onProcessItem(this.customContext_, itemType, item);
}
async processResource(resource:any, filePath:string) {
return this.module_.onProcessResource(this.customContext_, resource, filePath);
}
async close() {
return this.module_.onClose(this.customContext_);
}
}

View File

@ -1,19 +1,19 @@
const InteropService_Exporter_Base = require('lib/services/InteropService_Exporter_Base');
const InteropService_Exporter_Base = require('lib/services/interop/InteropService_Exporter_Base').default;
const { basename, friendlySafeFilename, rtrimSlashes } = require('lib/path-utils.js');
const BaseModel = require('lib/BaseModel');
const Folder = require('lib/models/Folder');
const Note = require('lib/models/Note');
const Setting = require('lib/models/Setting');
const { shim } = require('lib/shim');
const Setting = require('lib/models/Setting').default;
const shim = require('lib/shim').default;
const { themeStyle } = require('lib/theme');
const { dirname } = require('lib/path-utils.js');
const { escapeHtml } = require('lib/string-utils.js');
const markupLanguageUtils = require('lib/markupLanguageUtils');
const { assetsToHeaders } = require('lib/joplin-renderer');
class InteropService_Exporter_Html extends InteropService_Exporter_Base {
export default class InteropService_Exporter_Html extends InteropService_Exporter_Base {
async init(path, options = {}) {
async init(path:string, options:any = {}) {
this.customCss_ = options.customCss ? options.customCss : '';
if (this.metadata().target === 'file') {
@ -33,7 +33,7 @@ class InteropService_Exporter_Html extends InteropService_Exporter_Base {
this.style_ = themeStyle(Setting.THEME_LIGHT);
}
async makeDirPath_(item, pathPart = null) {
async makeDirPath_(item:any, pathPart:string = null) {
let output = '';
while (true) {
if (item.type_ === BaseModel.TYPE_FOLDER) {
@ -49,7 +49,7 @@ class InteropService_Exporter_Html extends InteropService_Exporter_Base {
}
}
async processNoteResources_(item) {
async processNoteResources_(item:any) {
const target = this.metadata().target;
const linkedResourceIds = await Note.linkedResourceIds(item.body);
const relativePath = target === 'directory' ? rtrimSlashes(await this.makeDirPath_(item, '..')) : '';
@ -66,7 +66,7 @@ class InteropService_Exporter_Html extends InteropService_Exporter_Base {
return newBody;
}
async processItem(ItemClass, item) {
async processItem(_itemType:number, item:any) {
if ([BaseModel.TYPE_NOTE, BaseModel.TYPE_FOLDER].indexOf(item.type_) < 0) return;
let dirPath = '';
@ -127,7 +127,7 @@ class InteropService_Exporter_Html extends InteropService_Exporter_Base {
}
}
async processResource(resource, filePath) {
async processResource(resource:any, filePath:string) {
const destResourcePath = `${this.resourceDir_}/${basename(filePath)}`;
await shim.fsDriver().copy(filePath, destResourcePath);
this.resources_.push(resource);
@ -135,5 +135,3 @@ class InteropService_Exporter_Html extends InteropService_Exporter_Base {
async close() {}
}
module.exports = InteropService_Exporter_Html;

View File

@ -1,11 +1,11 @@
const InteropService_Exporter_Base = require('lib/services/InteropService_Exporter_Base');
const InteropService_Exporter_Raw = require('lib/services/InteropService_Exporter_Raw');
import { _ } from 'lib/locale';
const InteropService_Exporter_Base = require('lib/services/interop/InteropService_Exporter_Base').default;
const InteropService_Exporter_Raw = require('lib/services/interop/InteropService_Exporter_Raw').default;
const fs = require('fs-extra');
const { shim } = require('lib/shim');
const { _ } = require('lib/locale');
const shim = require('lib/shim').default;
class InteropService_Exporter_Jex extends InteropService_Exporter_Base {
async init(destPath) {
export default class InteropService_Exporter_Jex extends InteropService_Exporter_Base {
async init(destPath:string) {
if (await shim.fsDriver().isDirectory(destPath)) throw new Error(`Path is a directory: ${destPath}`);
this.tempDir_ = await this.temporaryDirectory_(false);
@ -14,17 +14,17 @@ class InteropService_Exporter_Jex extends InteropService_Exporter_Base {
await this.rawExporter_.init(this.tempDir_);
}
async processItem(ItemClass, item) {
return this.rawExporter_.processItem(ItemClass, item);
async processItem(itemType:number, item:any) {
return this.rawExporter_.processItem(itemType, item);
}
async processResource(resource, filePath) {
async processResource(resource:any, filePath:string) {
return this.rawExporter_.processResource(resource, filePath);
}
async close() {
const stats = await shim.fsDriver().readDirStats(this.tempDir_, { recursive: true });
const filePaths = stats.filter(a => !a.isDirectory()).map(a => a.path);
const filePaths = stats.filter((a:any) => !a.isDirectory()).map((a:any) => a.path);
if (!filePaths.length) throw new Error(_('There is no data to export.'));
@ -41,5 +41,3 @@ class InteropService_Exporter_Jex extends InteropService_Exporter_Base {
await fs.remove(this.tempDir_);
}
}
module.exports = InteropService_Exporter_Jex;

View File

@ -1,13 +1,13 @@
const InteropService_Exporter_Base = require('lib/services/InteropService_Exporter_Base');
const InteropService_Exporter_Base = require('lib/services/interop/InteropService_Exporter_Base').default;
const { basename, dirname, friendlySafeFilename } = require('lib/path-utils.js');
const BaseModel = require('lib/BaseModel');
const Folder = require('lib/models/Folder');
const Note = require('lib/models/Note');
const { shim } = require('lib/shim');
const markdownUtils = require('lib/markdownUtils');
const shim = require('lib/shim').default;
const markdownUtils = require('lib/markdownUtils').default;
class InteropService_Exporter_Md extends InteropService_Exporter_Base {
async init(destDir) {
export default class InteropService_Exporter_Md extends InteropService_Exporter_Base {
async init(destDir:string) {
this.destDir_ = destDir;
this.resourceDir_ = destDir ? `${destDir}/_resources` : null;
this.createdDirs_ = [];
@ -16,7 +16,7 @@ class InteropService_Exporter_Md extends InteropService_Exporter_Base {
await shim.fsDriver().mkdir(this.resourceDir_);
}
async makeDirPath_(item, pathPart = null, findUniqueFilename = true) {
async makeDirPath_(item:any, pathPart:string = null, findUniqueFilename:boolean = true) {
let output = '';
while (true) {
if (item.type_ === BaseModel.TYPE_FOLDER) {
@ -32,34 +32,34 @@ class InteropService_Exporter_Md extends InteropService_Exporter_Base {
}
}
async relaceLinkedItemIdsByRelativePaths_(item) {
async relaceLinkedItemIdsByRelativePaths_(item:any) {
const relativePathToRoot = await this.makeDirPath_(item, '..');
const newBody = await this.replaceResourceIdsByRelativePaths_(item.body, relativePathToRoot);
return await this.replaceNoteIdsByRelativePaths_(newBody, relativePathToRoot);
}
async replaceResourceIdsByRelativePaths_(noteBody, relativePathToRoot) {
async replaceResourceIdsByRelativePaths_(noteBody:string, relativePathToRoot:string) {
const linkedResourceIds = await Note.linkedResourceIds(noteBody);
const resourcePaths = this.context() && this.context().resourcePaths ? this.context().resourcePaths : {};
const createRelativePath = function(resourcePath) {
const createRelativePath = function(resourcePath:string) {
return `${relativePathToRoot}_resources/${basename(resourcePath)}`;
};
return await this.replaceItemIdsByRelativePaths_(noteBody, linkedResourceIds, resourcePaths, createRelativePath);
}
async replaceNoteIdsByRelativePaths_(noteBody, relativePathToRoot) {
async replaceNoteIdsByRelativePaths_(noteBody:string, relativePathToRoot:string) {
const linkedNoteIds = await Note.linkedNoteIds(noteBody);
const notePaths = this.context() && this.context().notePaths ? this.context().notePaths : {};
const createRelativePath = function(notePath) {
const createRelativePath = function(notePath:string) {
return markdownUtils.escapeLinkUrl(`${relativePathToRoot}${notePath}`.trim());
};
return await this.replaceItemIdsByRelativePaths_(noteBody, linkedNoteIds, notePaths, createRelativePath);
}
async replaceItemIdsByRelativePaths_(noteBody, linkedItemIds, paths, fn_createRelativePath) {
async replaceItemIdsByRelativePaths_(noteBody:string, linkedItemIds:string[], paths:any, fn_createRelativePath:Function) {
let newBody = noteBody;
for (let i = 0; i < linkedItemIds.length; i++) {
@ -71,16 +71,16 @@ class InteropService_Exporter_Md extends InteropService_Exporter_Base {
return newBody;
}
async prepareForProcessingItemType(type, itemsToExport) {
if (type === BaseModel.TYPE_NOTE) {
async prepareForProcessingItemType(itemType:number, itemsToExport:any[]) {
if (itemType === BaseModel.TYPE_NOTE) {
// Create unique file path for the note
const context = {
const context:any = {
notePaths: {},
};
for (let i = 0; i < itemsToExport.length; i++) {
const itemType = itemsToExport[i].type;
if (itemType !== type) continue;
if (itemType !== itemType) continue;
const itemOrId = itemsToExport[i].itemOrId;
const note = typeof itemOrId === 'object' ? itemOrId : await Note.load(itemOrId);
@ -102,7 +102,7 @@ class InteropService_Exporter_Md extends InteropService_Exporter_Base {
}
}
async processItem(ItemClass, item) {
async processItem(_itemType:number, item:any) {
if ([BaseModel.TYPE_NOTE, BaseModel.TYPE_FOLDER].indexOf(item.type_) < 0) return;
if (item.type_ === BaseModel.TYPE_FOLDER) {
@ -125,12 +125,10 @@ class InteropService_Exporter_Md extends InteropService_Exporter_Base {
}
}
async processResource(resource, filePath) {
async processResource(_resource:any, filePath:string) {
const destResourcePath = `${this.resourceDir_}/${basename(filePath)}`;
await shim.fsDriver().copy(filePath, destResourcePath);
}
async close() {}
}
module.exports = InteropService_Exporter_Md;

View File

@ -1,9 +1,10 @@
const InteropService_Exporter_Base = require('lib/services/InteropService_Exporter_Base');
const InteropService_Exporter_Base = require('lib/services/interop/InteropService_Exporter_Base').default;
const BaseItem = require('lib/models/BaseItem.js');
const { basename } = require('lib/path-utils.js');
const { shim } = require('lib/shim');
const shim = require('lib/shim').default;
class InteropService_Exporter_Raw extends InteropService_Exporter_Base {
async init(destDir) {
export default class InteropService_Exporter_Raw extends InteropService_Exporter_Base {
async init(destDir:string) {
this.destDir_ = destDir;
this.resourceDir_ = destDir ? `${destDir}/resources` : null;
@ -11,18 +12,17 @@ class InteropService_Exporter_Raw extends InteropService_Exporter_Base {
await shim.fsDriver().mkdir(this.resourceDir_);
}
async processItem(ItemClass, item) {
async processItem(itemType:number, item:any) {
const ItemClass = BaseItem.getClassByItemType(itemType);
const serialized = await ItemClass.serialize(item);
const filePath = `${this.destDir_}/${ItemClass.systemPath(item)}`;
await shim.fsDriver().writeFile(filePath, serialized, 'utf-8');
}
async processResource(resource, filePath) {
async processResource(_resource:any, filePath:string) {
const destResourcePath = `${this.resourceDir_}/${basename(filePath)}`;
await shim.fsDriver().copy(filePath, destResourcePath);
}
async close() {}
}
module.exports = InteropService_Exporter_Raw;

View File

@ -0,0 +1,34 @@
/* eslint @typescript-eslint/no-unused-vars: 0, no-unused-vars: 0 */
import { ImportExportResult } from './types';
const Setting = require('lib/models/Setting').default;
export default class InteropService_Importer_Base {
private metadata_:any = null;
protected sourcePath_:string = '';
protected options_:any = {}
setMetadata(md:any) {
this.metadata_ = md;
}
metadata() {
return this.metadata_;
}
async init(sourcePath:string, options:any) {
this.sourcePath_ = sourcePath;
this.options_ = options;
}
// @ts-ignore
async exec(result:ImportExportResult) {}
async temporaryDirectory_(createIt:boolean) {
const md5 = require('md5');
const tempDir = `${Setting.value('tempDir')}/${md5(Math.random() + Date.now())}`;
if (createIt) await require('fs-extra').mkdirp(tempDir);
return tempDir;
}
}

View File

@ -0,0 +1,20 @@
import InteropService_Importer_Base from './InteropService_Importer_Base';
import { ImportExportResult, Module } from './types';
export default class InteropService_Importer_Custom extends InteropService_Importer_Base {
private module_:Module = null;
constructor(handler:Module) {
super();
this.module_ = handler;
}
async exec(result:ImportExportResult) {
return this.module_.onExec({
sourcePath: this.sourcePath_,
options: this.options_,
warnings: result.warnings,
});
}
}

View File

@ -1,9 +1,11 @@
const InteropService_Importer_Base = require('lib/services/InteropService_Importer_Base');
import { ImportExportResult } from './types';
const InteropService_Importer_Base = require('lib/services/interop/InteropService_Importer_Base').default;
const Folder = require('lib/models/Folder.js');
const { filename } = require('lib/path-utils.js');
class InteropService_Importer_EnexToHtml extends InteropService_Importer_Base {
async exec(result) {
export default class InteropService_Importer_EnexToHtml extends InteropService_Importer_Base {
async exec(result:ImportExportResult) {
const { importEnex } = require('lib/import-enex');
let folder = this.options_.destinationFolder;
@ -18,5 +20,3 @@ class InteropService_Importer_EnexToHtml extends InteropService_Importer_Base {
return result;
}
}
module.exports = InteropService_Importer_EnexToHtml;

View File

@ -1,9 +1,11 @@
const InteropService_Importer_Base = require('lib/services/InteropService_Importer_Base');
import { ImportExportResult } from './types';
const InteropService_Importer_Base = require('lib/services/interop/InteropService_Importer_Base').default;
const Folder = require('lib/models/Folder.js');
const { filename } = require('lib/path-utils.js');
class InteropService_Importer_EnexToMd extends InteropService_Importer_Base {
async exec(result) {
export default class InteropService_Importer_EnexToMd extends InteropService_Importer_Base {
async exec(result:ImportExportResult) {
const { importEnex } = require('lib/import-enex');
let folder = this.options_.destinationFolder;
@ -18,5 +20,3 @@ class InteropService_Importer_EnexToMd extends InteropService_Importer_Base {
return result;
}
}
module.exports = InteropService_Importer_EnexToMd;

View File

@ -1,10 +1,12 @@
const InteropService_Importer_Base = require('lib/services/InteropService_Importer_Base');
const InteropService_Importer_Raw = require('lib/services/InteropService_Importer_Raw');
import { ImportExportResult } from './types';
const InteropService_Importer_Base = require('lib/services/interop/InteropService_Importer_Base').default;
const InteropService_Importer_Raw = require('lib/services/interop/InteropService_Importer_Raw').default;
const { filename } = require('lib/path-utils.js');
const fs = require('fs-extra');
class InteropService_Importer_Jex extends InteropService_Importer_Base {
async exec(result) {
export default class InteropService_Importer_Jex extends InteropService_Importer_Base {
async exec(result:ImportExportResult) {
const tempDir = await this.temporaryDirectory_(true);
try {
@ -32,5 +34,3 @@ class InteropService_Importer_Jex extends InteropService_Importer_Base {
return result;
}
}
module.exports = InteropService_Importer_Jex;

View File

@ -1,15 +1,17 @@
const InteropService_Importer_Base = require('lib/services/InteropService_Importer_Base');
import { ImportExportResult } from './types';
import { _ } from 'lib/locale';
const InteropService_Importer_Base = require('lib/services/interop/InteropService_Importer_Base').default;
const Folder = require('lib/models/Folder.js');
const Note = require('lib/models/Note.js');
const { basename, filename, rtrimSlashes, fileExtension, dirname } = require('lib/path-utils.js');
const { shim } = require('lib/shim');
const { _ } = require('lib/locale');
const { extractImageUrls } = require('lib/markdownUtils');
const shim = require('lib/shim').default;
const { extractImageUrls } = require('lib/markdownUtils').default;
const { unique } = require('lib/ArrayUtils');
const { pregQuote } = require('lib/string-utils-common');
class InteropService_Importer_Md extends InteropService_Importer_Base {
async exec(result) {
export default class InteropService_Importer_Md extends InteropService_Importer_Base {
async exec(result:ImportExportResult) {
let parentFolderId = null;
const sourcePath = rtrimSlashes(this.sourcePath_);
@ -38,7 +40,7 @@ class InteropService_Importer_Md extends InteropService_Importer_Base {
return result;
}
async importDirectory(dirPath, parentFolderId) {
async importDirectory(dirPath:string, parentFolderId:string) {
console.info(`Import: ${dirPath}`);
const supportedFileExtension = this.metadata().fileExtensions;
@ -60,10 +62,10 @@ class InteropService_Importer_Md extends InteropService_Importer_Base {
* Parse text for links, attempt to find local file, if found create Joplin resource
* and update link accordingly.
*/
async importLocalImages(filePath, md) {
async importLocalImages(filePath:string, md:string) {
let updated = md;
const imageLinks = unique(extractImageUrls(md));
await Promise.all(imageLinks.map(async (encodedLink) => {
await Promise.all(imageLinks.map(async (encodedLink:string) => {
const link = decodeURI(encodedLink);
const attachmentPath = filename(`${dirname(filePath)}/${link}`, true);
const pathWithExtension = `${attachmentPath}.${fileExtension(link)}`;
@ -80,7 +82,7 @@ class InteropService_Importer_Md extends InteropService_Importer_Base {
return updated;
}
async importFile(filePath, parentFolderId) {
async importFile(filePath:string, parentFolderId:string) {
const stat = await shim.fsDriver().stat(filePath);
if (!stat) throw new Error(`Cannot read ${filePath}`);
const title = filename(filePath);
@ -104,5 +106,3 @@ class InteropService_Importer_Md extends InteropService_Importer_Base {
return Note.save(note, { autoTimestamp: false });
}
}
module.exports = InteropService_Importer_Md;

View File

@ -1,4 +1,6 @@
const InteropService_Importer_Base = require('lib/services/InteropService_Importer_Base');
import { ImportExportResult } from './types';
const InteropService_Importer_Base = require('lib/services/interop/InteropService_Importer_Base').default;
const BaseItem = require('lib/models/BaseItem.js');
const BaseModel = require('lib/BaseModel.js');
const Resource = require('lib/models/Resource.js');
@ -7,18 +9,18 @@ const NoteTag = require('lib/models/NoteTag.js');
const Note = require('lib/models/Note.js');
const Tag = require('lib/models/Tag.js');
const { sprintf } = require('sprintf-js');
const { shim } = require('lib/shim');
const shim = require('lib/shim').default;
const { fileExtension } = require('lib/path-utils');
const { uuid } = require('lib/uuid.js');
const uuid = require('lib/uuid').default;
class InteropService_Importer_Raw extends InteropService_Importer_Base {
async exec(result) {
const itemIdMap = {};
const createdResources = {};
export default class InteropService_Importer_Raw extends InteropService_Importer_Base {
async exec(result:ImportExportResult) {
const itemIdMap:any = {};
const createdResources:any = {};
const noteTagsToCreate = [];
const destinationFolderId = this.options_.destinationFolderId;
const replaceLinkedItemIds = async noteBody => {
const replaceLinkedItemIds = async (noteBody:string) => {
let output = noteBody;
const itemIds = Note.linkedItemIds(noteBody);
@ -33,7 +35,7 @@ class InteropService_Importer_Raw extends InteropService_Importer_Base {
const stats = await shim.fsDriver().readDirStats(this.sourcePath_);
const folderExists = function(stats, folderId) {
const folderExists = function(stats:any[], folderId:string) {
folderId = folderId.toLowerCase();
for (let i = 0; i < stats.length; i++) {
const stat = stats[i];
@ -43,7 +45,7 @@ class InteropService_Importer_Raw extends InteropService_Importer_Base {
return false;
};
let defaultFolder_ = null;
let defaultFolder_:any = null;
const defaultFolder = async () => {
if (defaultFolder_) return defaultFolder_;
const folderTitle = await Folder.findUniqueItemTitle(this.options_.defaultFolderTitle ? this.options_.defaultFolderTitle : 'Imported', '');
@ -52,7 +54,7 @@ class InteropService_Importer_Raw extends InteropService_Importer_Base {
return defaultFolder_;
};
const setFolderToImportTo = async itemParentId => {
const setFolderToImportTo = async (itemParentId:string) => {
// Logic is a bit complex here:
// - If a destination folder was specified, move the note to it.
// - Otherwise, if the associated folder exists, use this.
@ -167,5 +169,3 @@ class InteropService_Importer_Raw extends InteropService_Importer_Base {
return result;
}
}
module.exports = InteropService_Importer_Raw;

View File

@ -0,0 +1,125 @@
import { _ } from 'lib/locale';
export interface CustomImportContext {
sourcePath: string,
options: ImportOptions,
warnings: string[],
}
export interface CustomExportContext {
destPath: string,
options: ExportOptions,
}
export enum ModuleType {
Importer = 'importer',
Exporter = 'exporter',
}
export enum FileSystemItem {
File = 'file',
Directory = 'directory',
}
export enum ImportModuleOutputFormat {
Markdown = 'md',
Html = 'html',
}
// For historical reasons the import and export modules share the same
// interface, except that some properties are used only for import
// and others only for export.
export interface Module {
// ---------------------------------------
// Shared properties
// ---------------------------------------
type: ModuleType,
format: string,
fileExtensions: string[],
description: string,
path?: string,
// Only applies to single file exporters or importers
// It tells whether the format can package multiple notes into one file.
// For example JEX or ENEX can, but HTML cannot.
// Default: true.
isNoteArchive?: boolean,
// A custom module is one that was not hard-coded, that was created at runtime
// by a plugin for example. If `isCustom` is `true` if it is expected that all
// the event handlers below are defined (it's enforced by the plugin API).
isCustom?: boolean,
// ---------------------------------------
// Import-only properties
// ---------------------------------------
sources?: FileSystemItem[],
importerClass?: string,
outputFormat?: ImportModuleOutputFormat,
isDefault?: boolean,
fullLabel?: Function,
// Used only if `isCustom` is true
onExec?(context:any): Promise<void>;
// ---------------------------------------
// Export-only properties
// ---------------------------------------
target?: FileSystemItem,
// Used only if `isCustom` is true
onInit?(context:any): Promise<void>;
onProcessItem?(context:any, itemType:number, item:any):Promise<void>;
onProcessResource?(context:any, resource:any, filePath:string):Promise<void>;
onClose?(context:any):Promise<void>;
}
export interface ImportOptions {
path?: string,
format?: string
modulePath?: string,
destinationFolderId?: string,
destinationFolder?: any,
outputFormat?: ImportModuleOutputFormat,
}
export interface ExportOptions {
format?: string,
path?:string,
sourceFolderIds?: string[],
sourceNoteIds?: string[],
modulePath?:string,
target?:FileSystemItem,
}
export interface ImportExportResult {
warnings: string[],
}
function moduleFullLabel(moduleSource:FileSystemItem = null):string {
const label = [`${this.format.toUpperCase()} - ${this.description}`];
if (moduleSource && this.sources.length > 1) {
label.push(`(${moduleSource === 'file' ? _('File') : _('Directory')})`);
}
return label.join(' ');
}
export function defaultImportExportModule(type:ModuleType):Module {
return {
type: type,
format: '',
fileExtensions: [],
sources: [],
description: '',
isNoteArchive: true,
importerClass: '',
outputFormat: ImportModuleOutputFormat.Markdown,
isDefault: false,
fullLabel: moduleFullLabel,
isCustom: false,
target: FileSystemItem.File,
};
}

View File

@ -1,6 +1,6 @@
import KeychainServiceDriverBase from './KeychainServiceDriverBase';
const Setting = require('lib/models/Setting');
const BaseService = require('lib/services/BaseService');
const Setting = require('lib/models/Setting').default;
const BaseService = require('lib/services/BaseService').default;
export default class KeychainService extends BaseService {

View File

@ -1,5 +1,5 @@
import KeychainServiceDriverBase from './KeychainServiceDriverBase';
const { shim } = require('lib/shim.js');
import shim from 'lib/shim';
// keytar throws an error when system keychain is not present;
// even when keytar itself is installed.

View File

@ -0,0 +1,11 @@
import Plugin from './Plugin';
import BaseService from '../BaseService';
import Global from './api/Global';
export default abstract class BasePluginRunner extends BaseService {
async run(plugin:Plugin, sandbox:Global) {
throw new Error(`Not implemented: ${plugin} / ${sandbox}`);
}
}

View File

@ -0,0 +1,25 @@
import { MenuItemLocation } from './api/types';
import ViewController from './ViewController';
export default class MenuItemController extends ViewController {
constructor(id:string, pluginId:string, store:any, commandName:string, location:MenuItemLocation) {
super(id, pluginId, store);
this.store.dispatch({
type: 'PLUGIN_VIEW_ADD',
pluginId: pluginId,
view: {
id: this.handle,
type: this.type,
commandName: commandName,
location: location,
},
});
}
public get type():string {
return 'menuItem';
}
}

View File

@ -0,0 +1,59 @@
import { PluginManifest } from './utils/types';
import ViewController from './ViewController';
import shim from 'lib/shim';
import { ViewHandle } from './utils/createViewHandle';
interface ViewControllers {
[key:string]: ViewController
}
export default class Plugin {
private id_:string;
private baseDir_:string;
private manifest_:PluginManifest;
private scriptText_:string;
private enabled_:boolean = true;
// @ts-ignore Should be useful later on
private logger_:any = null;
private viewControllers_:ViewControllers = {};
constructor(id:string, baseDir:string, manifest:PluginManifest, scriptText:string, logger:any) {
this.id_ = id;
this.baseDir_ = shim.fsDriver().resolve(baseDir);
this.manifest_ = manifest;
this.scriptText_ = scriptText;
this.logger_ = logger;
}
public get id():string {
return this.id_;
}
public get enabled():boolean {
return this.enabled_;
}
public get manifest():PluginManifest {
return this.manifest_;
}
public get scriptText():string {
return this.scriptText_;
}
public get baseDir():string {
return this.baseDir_;
}
public addViewController(v:ViewController) {
if (this.viewControllers_[v.handle]) throw new Error(`View already added: ${v.handle}`);
this.viewControllers_[v.handle] = v;
}
public viewController(handle:ViewHandle):ViewController {
if (!this.viewControllers_[handle]) throw new Error(`View not found: ${handle}`);
return this.viewControllers_[handle];
}
}

View File

@ -0,0 +1,119 @@
import Plugin from 'lib/services/plugins/Plugin';
import manifestFromObject from 'lib/services/plugins/utils/manifestFromObject';
import Global from 'lib/services/plugins/api/Global';
import BasePluginRunner from 'lib/services/plugins/BasePluginRunner';
import BaseService from '../BaseService';
import shim from 'lib/shim';
const { filename } = require('lib/path-utils');
const nodeSlug = require('slug');
interface Plugins {
[key:string]: Plugin
}
function makePluginId(source:string):string {
// https://www.npmjs.com/package/slug#options
return nodeSlug(source, nodeSlug.defaults.modes['rfc3986']).substr(0,32);
}
export default class PluginService extends BaseService {
private static instance_:PluginService = null;
public static instance():PluginService {
if (!this.instance_) {
this.instance_ = new PluginService();
}
return this.instance_;
}
private store_:any = null;
private platformImplementation_:any = null;
private plugins_:Plugins = {};
private runner_:BasePluginRunner = null;
initialize(platformImplementation:any, runner:BasePluginRunner, store:any) {
this.store_ = store;
this.runner_ = runner;
this.platformImplementation_ = platformImplementation;
}
public get plugins():Plugins {
return this.plugins_;
}
public pluginById(id:string):Plugin {
if (!this.plugins_[id]) throw new Error(`Plugin not found: ${id}`);
return this.plugins_[id];
}
async loadPlugin(path:string):Promise<Plugin> {
const fsDriver = shim.fsDriver();
let distPath = path;
if (!(await fsDriver.exists(`${distPath}/manifest.json`))) {
distPath = `${path}/dist`;
}
this.logger().info(`PluginService: Loading plugin from ${path}`);
const manifestPath = `${distPath}/manifest.json`;
const indexPath = `${distPath}/index.js`;
const manifestContent = await fsDriver.readFile(manifestPath);
const manifest = manifestFromObject(JSON.parse(manifestContent));
const scriptText = await fsDriver.readFile(indexPath);
const pluginId = makePluginId(filename(path));
// After transforming the plugin path to an ID, multiple plugins might end up with the same ID. For
// example "MyPlugin" and "myplugin" would have the same ID. Technically it's possible to have two
// such folders but to keep things sane we disallow it.
if (this.plugins_[pluginId]) throw new Error(`There is already a plugin with this ID: ${pluginId}`);
const plugin = new Plugin(pluginId, distPath, manifest, scriptText, this.logger());
this.store_.dispatch({
type: 'PLUGIN_ADD',
plugin: {
id: pluginId,
views: {},
},
});
return plugin;
}
async loadAndRunPlugins(pluginDirOrPaths:string | string[]) {
let pluginPaths = [];
if (Array.isArray(pluginDirOrPaths)) {
pluginPaths = pluginDirOrPaths;
} else {
pluginPaths = (await shim.fsDriver().readDirStats(pluginDirOrPaths))
.filter((stat:any) => stat.isDirectory())
.map((stat:any) => `${pluginDirOrPaths}/${stat.path}`);
}
for (const pluginPath of pluginPaths) {
if (pluginPath.indexOf('_') === 0) {
this.logger().info(`PluginService: Plugin name starts with "_" and has not been loaded: ${pluginPath}`);
continue;
}
try {
const plugin = await this.loadPlugin(pluginPath);
await this.runPlugin(plugin);
} catch (error) {
this.logger().error(`PluginService: Could not load plugin: ${pluginPath}`, error);
}
}
}
async runPlugin(plugin:Plugin) {
this.plugins_[plugin.id] = plugin;
const pluginApi = new Global(this.logger(), this.platformImplementation_, plugin, this.store_);
return this.runner_.run(plugin, pluginApi);
}
}

View File

@ -0,0 +1,25 @@
import { ToolbarButtonLocation } from './api/types';
import ViewController from './ViewController';
export default class ToolbarButtonController extends ViewController {
constructor(id:string, pluginId:string, store:any, commandName:string, location:ToolbarButtonLocation) {
super(id, pluginId, store);
this.store.dispatch({
type: 'PLUGIN_VIEW_ADD',
pluginId: pluginId,
view: {
id: this.handle,
type: this.type,
commandName: commandName,
location: location,
},
});
}
public get type():string {
return 'toolbarButton';
}
}

View File

@ -0,0 +1,43 @@
import { ViewHandle } from './utils/createViewHandle';
export default class ViewController {
private handle_:ViewHandle;
private pluginId_:string;
private store_:any;
constructor(handle:ViewHandle, pluginId:string, store:any) {
this.handle_ = handle;
this.pluginId_ = pluginId;
this.store_ = store;
}
protected get storeView():any {
return this.store_.pluginService.plugins[this.pluginId_].views[this.handle];
}
protected get store():any {
return this.store_;
}
public get pluginId():string {
return this.pluginId_;
}
public get key():string {
return this.handle_;
}
public get handle():ViewHandle {
return this.handle_;
}
public get type():string {
throw new Error('Must be overriden');
}
public emitMessage(event:any) {
console.info('Calling ViewController.emitMessage - but not implemented', event);
}
}

View File

@ -0,0 +1,127 @@
import ViewController from './ViewController';
import shim from 'lib/shim';
import { ButtonId, ButtonSpec } from './api/types';
const { toSystemSlashes } = require('lib/path-utils');
export enum ContainerType {
Panel = 'panel',
Dialog = 'dialog',
}
export interface Options {
containerType: ContainerType,
}
interface CloseResponse {
resolve: Function;
reject: Function;
}
export default class WebviewController extends ViewController {
private baseDir_:string;
private messageListener_:Function = null;
private closeResponse_:CloseResponse = null;
constructor(id:string, pluginId:string, store:any, baseDir:string) {
super(id, pluginId, store);
this.baseDir_ = toSystemSlashes(baseDir, 'linux');
this.store.dispatch({
type: 'PLUGIN_VIEW_ADD',
pluginId: pluginId,
view: {
id: this.handle,
type: this.type,
containerType: ContainerType.Panel,
html: '',
scripts: [],
opened: false,
buttons: null,
},
});
}
public get type():string {
return 'webview';
}
private setStoreProp(name:string, value:any) {
this.store.dispatch({
type: 'PLUGIN_VIEW_PROP_SET',
pluginId: this.pluginId,
id: this.handle,
name: name,
value: value,
});
}
public get html():string {
return this.storeView.html;
}
public set html(html:string) {
this.setStoreProp('html', html);
}
public get containerType():ContainerType {
return this.storeView.containerType;
}
public set containerType(containerType:ContainerType) {
this.setStoreProp('containerType', containerType);
}
public async addScript(path:string) {
const fullPath = toSystemSlashes(shim.fsDriver().resolve(`${this.baseDir_}/${path}`), 'linux');
if (fullPath.indexOf(this.baseDir_) !== 0) throw new Error(`Script appears to be outside of plugin base directory: ${fullPath} (Base dir: ${this.baseDir_})`);
this.store.dispatch({
type: 'PLUGIN_VIEW_PROP_PUSH',
pluginId: this.pluginId,
id: this.handle,
name: 'scripts',
value: fullPath,
});
}
public emitMessage(event:any) {
if (!this.messageListener_) return;
this.messageListener_(event.message);
}
public onMessage(callback:any) {
this.messageListener_ = callback;
}
// ---------------------------------------------
// Specific to dialogs
// ---------------------------------------------
public async open():Promise<ButtonId> {
this.setStoreProp('opened', true);
return new Promise((resolve:Function, reject:Function) => {
this.closeResponse_ = { resolve, reject };
});
}
public async close() {
this.setStoreProp('opened', false);
}
public closeWithResponse(result:ButtonId) {
this.close();
this.closeResponse_.resolve(result);
}
public get buttons():ButtonSpec[] {
return this.storeView.buttons;
}
public set buttons(buttons:ButtonSpec[]) {
this.setStoreProp('buttons', buttons);
}
}

View File

@ -0,0 +1,88 @@
import Plugin from '../Plugin';
import Joplin from './Joplin';
import Logger from 'lib/Logger';
/**
* @ignore
*/
const builtinModules = require('builtin-modules');
/**
* @ignore
*/
export default class Global {
private joplin_: Joplin;
private requireWhiteList_:string[] = null;
// private consoleWrapper_:any = null;
constructor(logger:Logger, implementation:any, plugin: Plugin, store: any) {
this.joplin_ = new Joplin(logger, implementation.joplin, plugin, store);
// this.consoleWrapper_ = this.createConsoleWrapper(plugin.id);
}
// Wraps console calls to allow prefixing them with "Plugin PLUGIN_ID:"
// private createConsoleWrapper(pluginId:string) {
// const wrapper:any = {};
// for (const n in console) {
// if (!console.hasOwnProperty(n)) continue;
// wrapper[n] = (...args:any[]) => {
// const newArgs = args.slice();
// newArgs.splice(0, 0, `Plugin "${pluginId}":`);
// return (console as any)[n](...newArgs);
// };
// }
// return wrapper;
// }
get joplin(): Joplin {
return this.joplin_;
}
private requireWhiteList():string[] {
if (!this.requireWhiteList_) {
this.requireWhiteList_ = builtinModules.slice();
this.requireWhiteList_.push('fs-extra');
}
return this.requireWhiteList_;
}
// get console(): any {
// return this.consoleWrapper_;
// }
require(filePath:string):any {
if (!this.requireWhiteList().includes(filePath)) throw new Error(`Path not allowed: ${filePath}`);
return require(filePath);
}
// To get webpack to work with Node module we need to set the parameter `target: "node"`, however
// when setting this, the code generated by webpack will try to access the `process` global variable,
// which won't be defined in the sandbox. So here we simply forward the variable, which makes it all work.
get process():any {
return process;
}
// setTimeout(fn: Function, interval: number) {
// return shim.setTimeout(() => {
// fn();
// }, interval);
// }
// setInterval(fn: Function, interval: number) {
// return shim.setInterval(() => {
// fn();
// }, interval);
// }
// alert(message:string) {
// return alert(message);
// }
// confirm(message:string) {
// return confirm(message);
// }
}

View File

@ -0,0 +1,74 @@
import Plugin from '../Plugin';
import JoplinData from './JoplinData';
import JoplinPlugins from './JoplinPlugins';
import JoplinWorkspace from './JoplinWorkspace';
import JoplinFilters from './JoplinFilters';
import JoplinCommands from './JoplinCommands';
import JoplinViews from './JoplinViews';
import JoplinInterop from './JoplinInterop';
import JoplinSettings from './JoplinSettings';
import Logger from 'lib/Logger';
/**
* This is the main entry point to the Joplin API. You can access various services using the provided accessors.
*/
export default class Joplin {
private data_: JoplinData = null;
private plugins_: JoplinPlugins = null;
private workspace_: JoplinWorkspace = null;
private filters_: JoplinFilters = null;
private commands_: JoplinCommands = null;
private views_: JoplinViews = null;
private interop_: JoplinInterop = null;
private settings_: JoplinSettings = null;
constructor(logger:Logger, implementation:any, plugin: Plugin, store: any) {
this.data_ = new JoplinData();
this.plugins_ = new JoplinPlugins(logger, plugin);
this.workspace_ = new JoplinWorkspace(implementation.workspace, store);
this.filters_ = new JoplinFilters();
this.commands_ = new JoplinCommands();
this.views_ = new JoplinViews(implementation.views, plugin, store);
this.interop_ = new JoplinInterop();
this.settings_ = new JoplinSettings(plugin);
}
get data(): JoplinData {
return this.data_;
}
get plugins(): JoplinPlugins {
return this.plugins_;
}
get workspace(): JoplinWorkspace {
return this.workspace_;
}
/**
* @ignore
*
* Not sure if it's the best way to hook into the app
* so for now disable filters.
*/
get filters(): JoplinFilters {
return this.filters_;
}
get commands(): JoplinCommands {
return this.commands_;
}
get views(): JoplinViews {
return this.views_;
}
get interop(): JoplinInterop {
return this.interop_;
}
get settings(): JoplinSettings {
return this.settings_;
}
}

View File

@ -0,0 +1,75 @@
import CommandService, { CommandDeclaration, CommandRuntime } from 'lib/services/CommandService';
import { Command } from './types';
/**
* This class allows executing or registering new Joplin commands. Commands can be executed or associated with
* {@link JoplinViewsToolbarButtons | toolbar buttons} or {@link JoplinViewsMenuItems | menu items}.
*
* [View the demo plugin](https://github.com/laurent22/joplin/CliClient/tests/support/plugins/register_command)
*
* ## Executing Joplin's internal commands
*
* It is also possible to execute internal Joplin's commands which, as of now, are not well documented.
* You can find the list directly on GitHub though at the following locations:
*
* https://github.com/laurent22/joplin/tree/dev/ElectronClient/gui/MainScreen/commands
* https://github.com/laurent22/joplin/tree/dev/ElectronClient/commands
* https://github.com/laurent22/joplin/tree/dev/ElectronClient/gui/NoteEditor/commands/editorCommandDeclarations.ts
*
* To view what arguments are supported, you can open any of these files and look at the `execute()` command.
*/
export default class JoplinCommands {
/**
* <span class="platform-desktop">desktop</span> Executes the given command.
* The `props` are the arguments passed to the command, and they vary based on the command
*
* ```typescript
* // Create a new note in the current notebook:
* await joplin.commands.execute('newNote');
*
* // Create a new sub-notebook under the provided notebook
* // Note: internally, notebooks are called "folders".
* await joplin.commands.execute('newFolder', { parent_id: "SOME_FOLDER_ID" });
* ```
*/
async execute(commandName: string, props: any = null):Promise<any> {
return CommandService.instance().execute(commandName, props);
}
/**
* <span class="platform-desktop">desktop</span> Registers a new command.
*
* ```typescript
* // Register a new commmand called "testCommand1"
*
* await joplin.commands.register({
* name: 'testCommand1',
* label: 'My Test Command 1',
* iconName: 'fas fa-music',
* execute: () => {
* alert('Testing plugin command 1');
* },
* });
* ```
*/
async register(command:Command) {
const declaration:CommandDeclaration = {
name: command.name,
label: command.label,
};
if ('iconName' in command) declaration.iconName = command.iconName;
const runtime:CommandRuntime = {
execute: command.execute,
};
if ('isEnabled' in command) runtime.isEnabled = command.isEnabled;
if ('mapStateToProps' in command) runtime.mapStateToProps = command.mapStateToProps;
CommandService.instance().registerDeclaration(declaration);
CommandService.instance().registerRuntime(declaration.name, runtime);
}
}

View File

@ -0,0 +1,81 @@
import Api from 'lib/services/rest/Api';
import { Path } from './types';
/**
* This module provides access to the Joplin data API: https://joplinapp.org/api/
* This is the main way to retrieve data, such as notes, notebooks, tags, etc.
* or to update them or delete them.
*
* This is also what you would use to search notes, via the `search` endpoint.
*
* [View the demo plugin](https://github.com/laurent22/joplin/CliClient/tests/support/plugins/simple)
*
* In general you would use the methods in this class as if you were using a REST API. There are four methods that map to GET, POST, PUT and DELETE calls.
* And each method takes these parameters:
*
* * `path`: This is an array that represents the path to the resource in the form `["resouceName", "resourceId", "resourceLink"]` (eg. ["tags", ":id", "notes"]). The "resources" segment is the name of the resources you want to access (eg. "notes", "folders", etc.). If not followed by anything, it will refer to all the resources in that collection. The optional "resourceId" points to a particular resources within the collection. Finally, an optional "link" can be present, which links the resource to a collection of resources. This can be used in the API for example to retrieve all the notes associated with a tag.
* * `query`: (Optional) The query parameters. In a URL, this is the part after the question mark "?". In this case, it should be an object with key/value pairs.
* * `data`: (Optional) Applies to PUT and POST calls only. The request body contains the data you want to create or modify, for example the content of a note or folder.
* * `files`: (Optional) Used to create new resources and associate them with files.
*
* Please refer to the [Joplin API documentation](https://joplinapp.org/api/) for complete details about each call. As the plugin runs within the Joplin application **you do not need an authorisation token** to use this API.
*
* For example:
*
* ```typescript
* // Get a note ID, title and body
* const noteId = 'some_note_id';
* const note = await joplin.data.get(['notes', noteId], { fields: ['id', 'title', 'body'] });
*
* // Get all folders
* const folders = await joplin.data.get(['folders']);
*
* // Set the note body
* await joplin.data.put(['notes', noteId], null, { body: "New note body" });
*
* // Create a new note under one of the folders
* await joplin.data.post(['notes'], null, { body: "my new note", title: "some title", parent_id: folders[0].id });
* ```
*/
export default class JoplinData {
private api_: any = new Api();
private pathSegmentRegex_:RegExp;
private serializeApiBody(body: any) {
if (typeof body !== 'string') { return JSON.stringify(body); }
return body;
}
private pathToString(path:Path):string {
if (!this.pathSegmentRegex_) {
this.pathSegmentRegex_ = /^([a-z0-9]+)$/;
}
if (!Array.isArray(path)) throw new Error(`Path must be an array: ${JSON.stringify(path)}`);
if (path.length < 1) throw new Error(`Path must have at least one element: ${JSON.stringify(path)}`);
if (path.length > 3) throw new Error(`Path must have no more than 3 elements: ${JSON.stringify(path)}`);
for (const p of path) {
if (!this.pathSegmentRegex_.test(p)) throw new Error(`Path segments must only contain lowercase letters and digits: ${JSON.stringify(path)}`);
}
return path.join('/');
}
async get(path: Path, query: any = null) {
return this.api_.route('GET', this.pathToString(path), query);
}
async post(path: Path, query: any = null, body: any = null, files: any[] = null) {
return this.api_.route('POST', this.pathToString(path), query, this.serializeApiBody(body), files);
}
async put(path: Path, query: any = null, body: any = null, files: any[] = null) {
return this.api_.route('PUT', this.pathToString(path), query, this.serializeApiBody(body), files);
}
async delete(path: Path, query: any = null) {
return this.api_.route('DELETE', this.pathToString(path), query);
}
}

View File

@ -0,0 +1,17 @@
import eventManager from 'lib/eventManager';
/**
* @ignore
*
* Not sure if it's the best way to hook into the app
* so for now disable filters.
*/
export default class JoplinFilters {
async on(name: string, callback: Function) {
eventManager.filterOn(name, callback);
}
async off(name: string, callback: Function) {
eventManager.filterOff(name, callback);
}
}

View File

@ -0,0 +1,41 @@
import InteropService from 'lib/services/interop/InteropService';
import { Module, ModuleType } from 'lib/services/interop/types';
import { ExportModule, ImportModule } from './types';
/**
* Provides a way to create modules to import external data into Joplin or to export notes into any arbitrary format.
*
* [View the demo plugin](https://github.com/laurent22/joplin/CliClient/tests/support/plugins/json_export)
*
* To implement an import or export module, you would simply define an object with various event handlers that are called
* by the application during the import/export process.
*
* See the documentation of the [[ExportModule]] and [[ImportModule]] for more information.
*
* You may also want to refer to the Joplin API documentation to see the list of properties for each item (note, notebook, etc.) - https://joplinapp.org/api/
*/
export default class JoplinInterop {
async registerExportModule(module:ExportModule) {
const internalModule:Module = {
...module,
type: ModuleType.Exporter,
isCustom: true,
fileExtensions: module.fileExtensions ? module.fileExtensions : [],
};
return InteropService.instance().registerModule(internalModule);
}
async registerImportModule(module:ImportModule) {
const internalModule:Module = {
...module,
type: ModuleType.Importer,
isCustom: true,
fileExtensions: module.fileExtensions ? module.fileExtensions : [],
};
return InteropService.instance().registerModule(internalModule);
}
}

View File

@ -0,0 +1,51 @@
import Plugin from '../Plugin';
import Logger from 'lib/Logger';
import { Script } from './types';
/**
* This class provides access to plugin-related features.
*/
export default class JoplinPlugins {
private logger: Logger;
private plugin: Plugin;
constructor(logger:Logger, plugin:Plugin) {
this.logger = logger;
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
* }
* });
* ```
*/
async register(script: Script) {
if (script.onStart) {
const startTime = Date.now();
this.logger.info(`Starting plugin: ${this.plugin.id}`);
// 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.
script.onStart({}).catch((error:any) => {
// 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.
const newError:Error = new Error(error.message);
newError.stack = error.stack;
this.logger.error(`In plugin ${this.plugin.id}:`, newError);
}).then(() => {
this.logger.info(`Finished running onStart handler: ${this.plugin.id} (Took ${Date.now() - startTime}ms)`);
});
}
}
}

View File

@ -0,0 +1,76 @@
import Setting, { SettingItem as InternalSettingItem } from 'lib/models/Setting';
import Plugin from '../Plugin';
import { SettingItem, SettingSection } from './types';
/**
* This API allows registering new settings and setting sections, as well as getting and setting settings. Once a setting has been registered it will appear in the config screen and be editable by the user.
*
* Settings are essentially key/value pairs.
*
* Note: Currently this API does **not** provide access to Joplin's built-in settings. This is by design as plugins that modify user settings could give unexpected results
*
* [View the demo plugin](https://github.com/laurent22/joplin/CliClient/tests/support/plugins/settings)
*/
export default class JoplinSettings {
private plugin_:Plugin = null;
constructor(plugin: Plugin) {
this.plugin_ = plugin;
}
// Ensures that the plugin settings and sections are within their own namespace, to prevent them from
// overwriting other plugin settings or the default settings.
private namespacedKey(key:string):string {
return `plugin-${this.plugin_.id}.${key}`;
}
/**
* Registers a new setting. Note that registering a setting item is dynamic and will be gone next time Joplin starts.
* What it means is that you need to register the setting every time the plugin starts (for example in the onStart event).
* The setting value however will be preserved from one launch to the next so there is no risk that it will be lost even if for some
* reason the plugin fails to start at some point.
*/
async registerSetting(key:string, settingItem:SettingItem) {
const internalSettingItem:InternalSettingItem = {
key: key,
value: settingItem.value,
type: settingItem.type,
public: settingItem.public,
label: () => settingItem.label,
description: (_appType:string) => settingItem.description,
};
if ('isEnum' in settingItem) internalSettingItem.isEnum = settingItem.isEnum;
if ('section' in settingItem) internalSettingItem.section = this.namespacedKey(settingItem.section);
if ('options' in settingItem) internalSettingItem.options = settingItem.options;
if ('appTypes' in settingItem) internalSettingItem.appTypes = settingItem.appTypes;
if ('secure' in settingItem) internalSettingItem.secure = settingItem.secure;
if ('advanced' in settingItem) internalSettingItem.advanced = settingItem.advanced;
if ('minimum' in settingItem) internalSettingItem.minimum = settingItem.minimum;
if ('maximum' in settingItem) internalSettingItem.maximum = settingItem.maximum;
if ('step' in settingItem) internalSettingItem.step = settingItem.step;
return Setting.registerSetting(this.namespacedKey(key), internalSettingItem);
}
/**
* Registers a new setting section. Like for registerSetting, it is dynamic and needs to be done every time the plugin starts.
*/
async registerSection(name:string, section:SettingSection) {
return Setting.registerSection(this.namespacedKey(name), section);
}
/**
* Gets a setting value (only applies to setting you registered from your plugin)
*/
async value(key:string):Promise<any> {
return Setting.value(this.namespacedKey(key));
}
/**
* Sets a setting value (only applies to setting you registered from your plugin)
*/
async setValue(key:string, value:any) {
return Setting.setValue(this.namespacedKey(key), value);
}
}

View File

@ -0,0 +1,11 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const Entities = require('html-entities').AllHtmlEntities;
const htmlentities = new Entities().encode;
class JoplinUtils {
escapeHtml(text) {
return htmlentities(text);
}
}
exports.default = JoplinUtils;
// # sourceMappingURL=JoplinUtils.js.map

View File

@ -0,0 +1,50 @@
import Plugin from '../Plugin';
import JoplinViewsDialogs from './JoplinViewsDialogs';
import JoplinViewsMenuItems from './JoplinViewsMenuItems';
import JoplinViewsToolbarButtons from './JoplinViewsToolbarButtons';
import JoplinViewsPanels from './JoplinViewsPanels';
/**
* This namespace provides access to view-related services.
*
* All view services provide a `create()` method which you would use to create the view object, whether it's a dialog, a toolbar button or a menu item.
* In some cases, the `create()` method will return a [[ViewHandle]], which you would use to act on the view, for example to set certain properties or call some methods.
*/
export default class JoplinViews {
private store: any;
private plugin: Plugin;
private dialogs_:JoplinViewsDialogs = null;
private panels_:JoplinViewsPanels = null;
private menuItems_:JoplinViewsMenuItems = null;
private toolbarButtons_:JoplinViewsToolbarButtons = null;
private implementation_:any = null;
constructor(implementation:any, plugin: Plugin, store: any) {
this.store = store;
this.plugin = plugin;
this.implementation_ = implementation;
}
public get dialogs():JoplinViewsDialogs {
if (!this.dialogs_) this.dialogs_ = new JoplinViewsDialogs(this.implementation_.dialogs, this.plugin, this.store);
return this.dialogs_;
}
public get panels():JoplinViewsPanels {
if (!this.panels_) this.panels_ = new JoplinViewsPanels(this.plugin, this.store);
return this.panels_;
}
public get menuItems():JoplinViewsMenuItems {
if (!this.menuItems_) this.menuItems_ = new JoplinViewsMenuItems(this.plugin, this.store);
return this.menuItems_;
}
public get toolbarButtons():JoplinViewsToolbarButtons {
if (!this.toolbarButtons_) this.toolbarButtons_ = new JoplinViewsToolbarButtons(this.plugin, this.store);
return this.toolbarButtons_;
}
}

View File

@ -0,0 +1,68 @@
import Plugin from '../Plugin';
import createViewHandle from '../utils/createViewHandle';
import WebviewController, { ContainerType } from '../WebviewController';
import { ButtonSpec, ViewHandle, ButtonId } from './types';
/**
* Allows creating and managing dialogs. A dialog is modal window that contains a webview and a row of buttons. You can update the update the webview using the `setHtml` method.
* Dialogs are hidden by default and you need to call `open()` to open them. Once the user clicks on a button, the `open` call will return and provide the button ID that was
* clicked on. There is currently no "close" method since the dialog should be thought as a modal one and thus can only be closed by clicking on one of the buttons.
*
* [View the demo plugin](https://github.com/laurent22/joplin/CliClient/tests/support/plugins/dialog)
*/
export default class JoplinViewsDialogs {
private store: any;
private plugin: Plugin;
private implementation_:any;
constructor(implementation:any, plugin: Plugin, store: any) {
this.store = store;
this.plugin = plugin;
this.implementation_ = implementation;
}
private controller(handle:ViewHandle):WebviewController {
return this.plugin.viewController(handle) as WebviewController;
}
/**
* Creates a new dialog
*/
async create():Promise<ViewHandle> {
const handle = createViewHandle(this.plugin);
const controller = new WebviewController(handle, this.plugin.id, this.store, this.plugin.baseDir);
controller.containerType = ContainerType.Dialog;
this.plugin.addViewController(controller);
return handle;
}
/**
* Displays a message box with OK/Cancel buttons. Returns the button index that was clicked - "0" for OK and "1" for "Cancel"
*/
async showMessageBox(message:string):Promise<number> {
return this.implementation_.showMessageBox(message);
}
/**
* Sets the dialog HTML content
*/
async setHtml(handle:ViewHandle, html:string) {
return this.controller(handle).html = html;
}
/**
* Sets the dialog buttons.
*/
async setButtons(handle:ViewHandle, buttons:ButtonSpec[]) {
return this.controller(handle).buttons = buttons;
}
/**
* Opens the dialog
*/
async open(handle:ViewHandle):Promise<ButtonId> {
return this.controller(handle).open();
}
}

View File

@ -0,0 +1,35 @@
import KeymapService from 'lib/services/KeymapService';
import { CreateMenuItemOptions, MenuItemLocation } from './types';
import MenuItemController from '../MenuItemController';
import Plugin from '../Plugin';
import createViewHandle from '../utils/createViewHandle';
/**
* Allows creating and managing menu items.
*
* [View the demo plugin](https://github.com/laurent22/joplin/CliClient/tests/support/plugins/register_command)
*/
export default class JoplinViewsMenuItems {
private store: any;
private plugin: Plugin;
constructor(plugin: Plugin, store: any) {
this.store = store;
this.plugin = plugin;
}
/**
* Creates a new menu item and associate it with the given command. You can specify under which menu the item should appear using the `location` parameter.
*/
async create(commandName:string, location:MenuItemLocation = MenuItemLocation.Tools, options:CreateMenuItemOptions = null) {
const handle = createViewHandle(this.plugin);
const controller = new MenuItemController(handle, this.plugin.id, this.store, commandName, location);
this.plugin.addViewController(controller);
if (options && options.accelerator) {
KeymapService.instance().registerCommandAccelerator(commandName, options.accelerator);
}
}
}

View File

@ -0,0 +1,57 @@
import Plugin from '../Plugin';
import createViewHandle from '../utils/createViewHandle';
import WebviewController from '../WebviewController';
import { ViewHandle } from './types';
/**
* Allows creating and managing view panels. View panels currently are displayed at the right of the sidebar and allows displaying any HTML content (within a webview) and update it in real-time. For example
* it could be used to display a table of content for the active note, or display various metadata or graph.
*
* [View the demo plugin](https://github.com/laurent22/joplin/CliClient/tests/support/plugins/toc)
*/
export default class JoplinViewsPanels {
private store: any;
private plugin: Plugin;
constructor(plugin: Plugin, store: any) {
this.store = store;
this.plugin = plugin;
}
private controller(handle:ViewHandle):WebviewController {
return this.plugin.viewController(handle) as WebviewController;
}
/**
* Creates a new panel
*/
async create():Promise<ViewHandle> {
const handle = createViewHandle(this.plugin);
const controller = new WebviewController(handle, this.plugin.id, this.store, this.plugin.baseDir);
this.plugin.addViewController(controller);
return handle;
}
/**
* Sets the panel webview HTML
*/
async setHtml(handle:ViewHandle, html:string) {
return this.controller(handle).html = html;
}
/**
* Adds and loads a new JS or CSS files into the panel.
*/
async addScript(handle:ViewHandle, scriptPath:string) {
return this.controller(handle).addScript(scriptPath);
}
/**
* Called when a message is sent from the webview (using postMessage).
*/
async onMessage(handle:ViewHandle, callback:Function) {
return this.controller(handle).onMessage(callback);
}
}

View File

@ -0,0 +1,30 @@
import { ToolbarButtonLocation } from './types';
import Plugin from '../Plugin';
import ToolbarButtonController from '../ToolbarButtonController';
import createViewHandle from '../utils/createViewHandle';
/**
* Allows creating and managing toolbar buttons.
*
* [View the demo plugin](https://github.com/laurent22/joplin/CliClient/tests/support/plugins/register_command)
*/
export default class JoplinViewsToolbarButtons {
private store: any;
private plugin: Plugin;
constructor(plugin: Plugin, store: any) {
this.store = store;
this.plugin = plugin;
}
/**
* Creates a new toolbar button and associate it with the given command.
*/
async create(commandName:string, location:ToolbarButtonLocation) {
const handle = createViewHandle(this.plugin);
const controller = new ToolbarButtonController(handle, this.plugin.id, this.store, commandName, location);
this.plugin.addViewController(controller);
}
}

View File

@ -0,0 +1,68 @@
import eventManager from 'lib/eventManager';
/**
* @ignore
*/
const Note = require('lib/models/Note');
/**
* The workspace service provides access to all the parts of Joplin that are being worked on - i.e. the currently selected notes or notebooks as well
* as various related events, such as when a new note is selected, or when the note content changes.
*
* [View the demo plugin](https://github.com/laurent22/joplin/CliClient/tests/support/plugins)
*/
export default class JoplinWorkspace {
// TODO: unregister events when plugin is closed or disabled
private store: any;
// private implementation_:any;
constructor(_implementation:any, store: any) {
this.store = store;
// this.implementation_ = implementation;
}
/**
* Called when a new note or notes are selected.
*/
async onNoteSelectionChange(callback: Function) {
eventManager.appStateOn('selectedNoteIds', callback);
}
/**
* Called when the content of a note changes.
*/
async onNoteContentChange(callback: Function) {
eventManager.on('noteContentChange', callback);
}
/**
* Called when an alarm associated with a to-do is triggered.
*/
async onNoteAlarmTrigger(callback:Function) {
eventManager.on('noteAlarmTrigger', callback);
}
/**
* Called when the synchronisation process has finished.
*/
async onSyncComplete(callback:Function) {
eventManager.on('syncComplete', callback);
}
/**
* Gets the currently selected note
*/
async selectedNote():Promise<any> {
const noteIds = this.store.getState().selectedNoteIds;
if (noteIds.length !== 1) { return null; }
return Note.load(noteIds[0]);
}
/**
* Gets the IDs of the selected notes (can be zero, one, or many). Use the data API to retrieve information about these notes.
*/
async selectedNoteIds():Promise<string[]> {
return this.store.getState().selectedNoteIds.slice();
}
}

View File

@ -0,0 +1,252 @@
// =================================================================
// Command API types
// =================================================================
export interface Command {
name: string
label: string
iconName?: string,
execute(props:any):Promise<any>
isEnabled?(props:any):boolean
mapStateToProps?(state:any):any
}
// =================================================================
// Interop API types
// =================================================================
export enum FileSystemItem {
File = 'file',
Directory = 'directory',
}
export enum ImportModuleOutputFormat {
Markdown = 'md',
Html = 'html',
}
/**
* Used to implement a module to export data from Joplin. [View the demo plugin](https://github.com/laurent22/joplin/CliClient/tests/support/plugins/json_export) for an example.
*
* In general, all the event handlers you'll need to implement take a `context` object as a first argument. This object will contain the export or import path as well as various optional properties, such as which notes or notebooks need to be exported.
*
* To get a better sense of what it will contain it can be useful to print it using `console.info(context)`.
*/
export interface ExportModule {
/**
* The format to be exported, eg "enex", "jex", "json", etc.
*/
format: string,
/**
* The description that will appear in the UI, for example in the menu item.
*/
description: string,
/**
* Whether the module will export a single file or multiple files in a directory. It affects the open dialog that will be presented to the user when using your exporter.
*/
target: FileSystemItem,
/**
* Only applies to single file exporters or importers
* It tells whether the format can package multiple notes into one file.
* For example JEX or ENEX can, but HTML cannot.
*/
isNoteArchive: boolean,
/**
* The extensions of the files exported by your module. For example, it is `["htm", "html"]` for the HTML module, and just `["jex"]` for the JEX module.
*/
fileExtensions?: string[],
/**
* Called when the export process starts.
*/
onInit(context:ExportContext): Promise<void>;
/**
* Called when an item needs to be processed. An "item" can be any Joplin object, such as a note, a folder, a notebook, etc.
*/
onProcessItem(context:ExportContext, itemType:number, item:any):Promise<void>;
/**
* Called when a resource file needs to be exported.
*/
onProcessResource(context:ExportContext, resource:any, filePath:string):Promise<void>;
/**
* Called when the export process is done.
*/
onClose(context:ExportContext):Promise<void>;
}
export interface ImportModule {
/**
* The format to be exported, eg "enex", "jex", "json", etc.
*/
format: string,
/**
* The description that will appear in the UI, for example in the menu item.
*/
description: string,
/**
* Only applies to single file exporters or importers
* It tells whether the format can package multiple notes into one file.
* For example JEX or ENEX can, but HTML cannot.
*/
isNoteArchive: boolean,
/**
* The type of sources that are supported by the module. Tells whether the module can import files or directories or both.
*/
sources: FileSystemItem[],
/**
* Tells the file extensions of the exported files.
*/
fileExtensions?: string[],
/**
* Tells the type of notes that will be generated, either HTML or Markdown (default).
*/
outputFormat?: ImportModuleOutputFormat,
/**
* Called when the import process starts. There is only one event handler within which you should import the complete data.
*/
onExec(context:ImportContext): Promise<void>;
}
export interface ExportOptions {
format?: string,
path?:string,
sourceFolderIds?: string[],
sourceNoteIds?: string[],
modulePath?:string,
target?:FileSystemItem,
}
export interface ExportContext {
destPath: string,
options: ExportOptions,
/**
* You can attach your own custom data using this propery - it will then be passed to each event handler, allowing you to keep state from one event to the next.
*/
userData?: any,
}
export interface ImportContext {
sourcePath: string,
options: any,
warnings: string[],
}
// =================================================================
// Misc types
// =================================================================
export interface Script {
onStart?(event:any):Promise<void>,
}
// =================================================================
// View API types
// =================================================================
export type ButtonId = string;
export interface ButtonSpec {
id: ButtonId,
title?: string,
onClick?():void,
}
export interface CreateMenuItemOptions {
accelerator: string,
}
export enum MenuItemLocation {
File = 'file',
Edit = 'edit',
View = 'view',
Note = 'note',
Tools = 'tools',
Help = 'help',
Context = 'context',
}
export enum ToolbarButtonLocation {
/**
* This toolbar in the top right corner of the application. It applies to the note as a whole, including its metadata.
*/
NoteToolbar = 'noteToolbar',
/**
* This toolbar is right above the text editor. It applies to the note body only.
*/
EditorToolbar = 'editorToolbar',
}
export type ViewHandle = string;
export interface EditorCommand {
name: string;
value?: any;
}
// =================================================================
// Settings types
// =================================================================
export enum SettingItemType {
Int = 1,
String = 2,
Bool = 3,
Array = 4,
Object = 5,
Button = 6,
}
// Redefine a simplified interface to mask internal details
// and to remove function calls as they would have to be async.
export interface SettingItem {
value: any,
type: SettingItemType,
public: boolean,
label:string,
description?:string,
isEnum?: boolean,
section?: string,
options?:any,
appTypes?:string[],
secure?: boolean,
advanced?: boolean,
minimum?: number,
maximum?: number,
step?: number,
}
export interface SettingSection {
label: string,
iconName?: string,
description?: string,
name?: string,
}
// =================================================================
// Data API types
// =================================================================
/**
* An array of at least one element and at most three elements.
*
* [0]: Resource name (eg. "notes", "folders", "tags", etc.)
* [1]: (Optional) Resource ID.
* [2]: (Optional) Resource link.
*/
export type Path = string[];

View File

@ -0,0 +1,102 @@
import { Draft } from 'immer';
export interface ViewInfo {
view: any,
plugin: any,
}
interface PluginViewState {
id: string,
type: string,
}
interface PluginViewStates {
[key:string]: PluginViewState,
}
interface PluginState {
id:string,
views:PluginViewStates,
}
export interface PluginStates {
[key:string]: PluginState;
}
export interface State {
plugins: PluginStates,
}
export const stateRootKey = 'pluginService';
export const defaultState:State = {
plugins: {},
};
export const utils = {
viewInfosByType: function(plugins:PluginStates, type:string):ViewInfo[] {
const output:ViewInfo[] = [];
for (const pluginId in plugins) {
const plugin = plugins[pluginId];
for (const viewId in plugin.views) {
const view = plugin.views[viewId];
if (view.type !== type) continue;
output.push({
plugin: plugin,
view: view,
});
}
}
return output;
},
commandNamesFromViews: function(plugins:PluginStates, toolbarType:string):string[] {
const infos = utils.viewInfosByType(plugins, 'toolbarButton');
return infos
.filter((info:ViewInfo) => info.view.location === toolbarType)
.map((info:ViewInfo) => info.view.commandName);
},
};
const reducer = (draft: Draft<any>, action:any) => {
if (action.type.indexOf('PLUGIN_') !== 0) return;
// All actions should be scoped to a plugin, except when adding a new plugin
if (!action.pluginId && action.type !== 'PLUGIN_ADD') throw new Error(`action.pluginId is required. Action was: ${JSON.stringify(action)}`);
try {
switch (action.type) {
case 'PLUGIN_ADD':
if (draft.pluginService.plugins[action.plugin.id]) throw new Error(`Plugin is already loaded: ${JSON.stringify(action)}`);
draft.pluginService.plugins[action.plugin.id] = action.plugin;
break;
case 'PLUGIN_VIEW_ADD':
draft.pluginService.plugins[action.pluginId].views[action.view.id] = { ...action.view };
break;
case 'PLUGIN_VIEW_PROP_SET':
draft.pluginService.plugins[action.pluginId].views[action.id][action.name] = action.value;
break;
case 'PLUGIN_VIEW_PROP_PUSH':
draft.pluginService.plugins[action.pluginId].views[action.id][action.name].push(action.value);
break;
}
} catch (error) {
error.message = `In plugin reducer: ${error.message} Action: ${JSON.stringify(action)}`;
throw error;
}
};
export default reducer;

View File

@ -0,0 +1,42 @@
export type Target = (name:string, args:any[]) => any;
const handler:any = {};
handler.get = function(target:Target, prop:string) {
let t = target as any;
// There's probably a cleaner way to do this but not sure how. The idea is to keep
// track of the calling chain current state. So if the user call `joplin.something.test("bla")`
// we know we need to pass "joplin.something.test" with args "bla" to the target.
// But also, if the user does this:
//
// const ns = joplin.view.dialogs;
// await ns.create();
// await ns.open();
//
// We need to know what "ns" maps to, so that's why call-specific context needs to be kept,
// and the easiest way to do this is to create a new target when the call chain starts,
// and attach a custom "__joplinNamespace" property to it.
if (!t.__joplinNamespace) {
const originalTarget = t;
const newTarget:any = (name:string, args:any[]) => {
return originalTarget(name, args);
};
newTarget.__joplinNamespace = [prop];
t = newTarget;
} else {
t.__joplinNamespace.push(prop);
}
return new Proxy(t, handler);
};
handler.apply = function(target:Target, _thisArg:any, argumentsList:any[]) {
const path = (target as any).__joplinNamespace.join('.');
(target as any).__joplinNamespace.pop();
return target(path, argumentsList);
};
export default function sandboxProxy(target:Target):any {
return new Proxy(target, handler);
}

View File

@ -0,0 +1,8 @@
import uuid from 'lib/uuid';
import Plugin from '../Plugin';
export type ViewHandle = string;
export default function createViewHandle(plugin:Plugin):ViewHandle {
return `plugin-view-${plugin.id}-${uuid.createNano()}`;
}

View 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);
}

View 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'),
permissions: permissions,
};
if (o.permissions) {
for (const p of o.permissions) {
manifest.permissions.push(p);
}
}
return manifest;
}

View 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;
}

View 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[],
}

View File

@ -0,0 +1,8 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const uuid_1 = require('lib/uuid');
function viewIdGen(plugin) {
return `plugin-view-${plugin.id}-${uuid_1.default.createNano()}`;
}
exports.default = viewIdGen;
// # sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmlld0lkR2VuLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsidmlld0lkR2VuLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQ0EsbUNBQTRCO0FBRTVCLFNBQXdCLFNBQVMsQ0FBQyxNQUFhO0lBQzlDLE9BQU8sZUFBZSxNQUFNLENBQUMsRUFBRSxJQUFJLGNBQUksQ0FBQyxVQUFVLEVBQUUsRUFBRSxDQUFBO0FBQ3ZELENBQUM7QUFGRCw0QkFFQyJ9

View File

@ -1,13 +1,13 @@
const { time } = require('lib/time-utils');
const BaseItem = require('lib/models/BaseItem.js');
const Alarm = require('lib/models/Alarm');
const Alarm = require('lib/models/Alarm').default;
const Folder = require('lib/models/Folder.js');
const Note = require('lib/models/Note.js');
const BaseModel = require('lib/BaseModel.js');
const DecryptionWorker = require('lib/services/DecryptionWorker');
const ResourceFetcher = require('lib/services/ResourceFetcher');
const Resource = require('lib/models/Resource');
const { _ } = require('lib/locale.js');
const { _ } = require('lib/locale');
const { toTitleCase } = require('lib/string-utils.js');
class ReportService {

View File

@ -1,3 +1,8 @@
import Setting from 'lib/models/Setting';
import Logger from 'lib/Logger';
import shim from 'lib/shim';
import uuid from 'lib/uuid';
const { ltrimSlashes } = require('lib/path-utils.js');
const { Database } = require('lib/database.js');
const Folder = require('lib/models/Folder');
@ -6,13 +11,10 @@ const Tag = require('lib/models/Tag');
const BaseItem = require('lib/models/BaseItem');
const Resource = require('lib/models/Resource');
const BaseModel = require('lib/BaseModel');
const Setting = require('lib/models/Setting');
const htmlUtils = require('lib/htmlUtils');
const markupLanguageUtils = require('lib/markupLanguageUtils');
const mimeUtils = require('lib/mime-utils.js').mime;
const { Logger } = require('lib/logger.js');
const md5 = require('md5');
const { shim } = require('lib/shim');
const HtmlToMd = require('lib/HtmlToMd');
const urlUtils = require('lib/urlUtils.js');
const ArrayUtils = require('lib/ArrayUtils.js');
@ -23,13 +25,18 @@ const SearchEngineUtils = require('lib/services/searchengine/SearchEngineUtils')
const { FoldersScreenUtils } = require('lib/folders-screen-utils.js');
const uri2path = require('file-uri-to-path');
const { MarkupToHtml } = require('lib/joplin-renderer');
const { uuid } = require('lib/uuid');
const { ErrorMethodNotAllowed, ErrorForbidden, ErrorBadRequest, ErrorNotFound } = require('./errors');
class Api {
constructor(token = null, actionApi = null) {
export default class Api {
private token_:string | Function;
private knownNounces_:any = {};
private logger_:Logger;
private actionApi_:any;
private htmlToMdParser_:any;
constructor(token:string = null, actionApi:any = null) {
this.token_ = token;
this.knownNounces_ = {};
this.logger_ = new Logger();
this.actionApi_ = actionApi;
}
@ -38,7 +45,7 @@ class Api {
return typeof this.token_ === 'function' ? this.token_() : this.token_;
}
parsePath(path) {
parsePath(path:string) {
path = ltrimSlashes(path);
if (!path) return { callName: '', params: [] };
@ -51,7 +58,7 @@ class Api {
};
}
async route(method, path, query = null, body = null, files = null) {
async route(method:string, path:string, query:any = null, body:any = null, files:string[] = null) {
if (!files) files = [];
if (!query) query = {};
@ -66,13 +73,13 @@ class Api {
this.knownNounces_[query.nounce] = requestMd5;
}
const request = {
const request:any = {
method: method,
path: ltrimSlashes(path),
query: query ? query : {},
body: body,
bodyJson_: null,
bodyJson: function(disallowedProperties = null) {
bodyJson: function(disallowedProperties:string[] = null) {
if (!this.bodyJson_) this.bodyJson_ = JSON.parse(this.body);
if (disallowedProperties) {
@ -104,17 +111,17 @@ class Api {
request.params = params;
if (!this[parsedPath.callName]) throw new ErrorNotFound();
if (!(this as any)[parsedPath.callName]) throw new ErrorNotFound();
try {
return await this[parsedPath.callName](request, id, link);
return await (this as any)[parsedPath.callName](request, id, link);
} catch (error) {
if (!error.httpCode) error.httpCode = 500;
throw error;
}
}
setLogger(l) {
setLogger(l:Logger) {
this.logger_ = l;
}
@ -122,23 +129,24 @@ class Api {
return this.logger_;
}
readonlyProperties(requestMethod) {
readonlyProperties(requestMethod:string) {
const output = ['created_time', 'updated_time', 'encryption_blob_encrypted', 'encryption_applied', 'encryption_cipher_text'];
if (requestMethod !== 'POST') output.splice(0, 0, 'id');
return output;
}
fields_(request, defaultFields) {
fields_(request:any, defaultFields:string[]) {
const query = request.query;
if (!query || !query.fields) return defaultFields;
if (Array.isArray(query.fields)) return query.fields.slice();
const fields = query.fields
.split(',')
.map(f => f.trim())
.filter(f => !!f);
.map((f:string) => f.trim())
.filter((f:string) => !!f);
return fields.length ? fields : defaultFields;
}
checkToken_(request) {
checkToken_(request:any) {
// For now, whitelist some calls to allow the web clipper to work
// without an extra auth step
const whiteList = [['GET', 'ping'], ['GET', 'tags'], ['GET', 'folders'], ['POST', 'notes']];
@ -152,7 +160,7 @@ class Api {
if (request.query.token !== this.token) throw new ErrorForbidden('Invalid "token" parameter');
}
async defaultAction_(modelType, request, id = null, link = null) {
async defaultAction_(modelType:number, request:any, id:string = null, link:string = null) {
this.checkToken_(request);
if (link) throw new ErrorNotFound(); // Default action doesn't support links at all for now
@ -169,7 +177,7 @@ class Api {
if (id) {
return getOneModel();
} else {
const options = {};
const options:any = {};
const fields = this.fields_(request, []);
if (fields.length) options.fields = fields;
return await ModelClass.all(options);
@ -201,7 +209,7 @@ class Api {
throw new ErrorMethodNotAllowed();
}
async action_ping(request) {
async action_ping(request:any) {
if (request.method === 'GET') {
return 'JoplinClipperServer';
}
@ -209,7 +217,7 @@ class Api {
throw new ErrorMethodNotAllowed();
}
async action_search(request) {
async action_search(request:any) {
this.checkToken_(request);
if (request.method !== 'GET') throw new ErrorMethodNotAllowed();
@ -221,7 +229,7 @@ class Api {
if (queryType !== BaseItem.TYPE_NOTE) {
const ModelClass = BaseItem.getClassByItemType(queryType);
const options = {};
const options:any = {};
const fields = this.fields_(request, []);
if (fields.length) options.fields = fields;
const sqlQueryPart = query.replace(/\*/g, '%');
@ -234,7 +242,7 @@ class Api {
}
}
async action_folders(request, id = null, link = null) {
async action_folders(request:any, id:string = null, link:string = null) {
if (request.method === 'GET' && !id) {
const folders = await FoldersScreenUtils.allForDisplay({ fields: this.fields_(request, ['id', 'parent_id', 'title']) });
const output = await Folder.allAsTree(folders);
@ -253,7 +261,7 @@ class Api {
return this.defaultAction_(BaseModel.TYPE_FOLDER, request, id, link);
}
async action_tags(request, id = null, link = null) {
async action_tags(request:any, id:string = null, link:string = null) {
if (link === 'notes') {
const tag = await Tag.load(id);
if (!tag) throw new ErrorNotFound();
@ -287,11 +295,11 @@ class Api {
return this.defaultAction_(BaseModel.TYPE_TAG, request, id, link);
}
async action_master_keys(request, id = null, link = null) {
async action_master_keys(request:any, id:string = null, link:string = null) {
return this.defaultAction_(BaseModel.TYPE_MASTER_KEY, request, id, link);
}
async action_resources(request, id = null, link = null) {
async action_resources(request:any, id:string = null, link:string = null) {
// fieldName: "data"
// headers: Object
// originalFilename: "test.jpg"
@ -327,27 +335,27 @@ class Api {
return this.defaultAction_(BaseModel.TYPE_RESOURCE, request, id, link);
}
notePreviewsOptions_(request) {
notePreviewsOptions_(request:any) {
const fields = this.fields_(request, []); // previews() already returns default fields
const options = {};
const options:any = {};
if (fields.length) options.fields = fields;
return options;
}
defaultSaveOptions_(model, requestMethod) {
const options = { userSideValidation: true };
defaultSaveOptions_(model:any, requestMethod:string) {
const options:any = { userSideValidation: true };
if (requestMethod === 'POST' && model.id) options.isNew = true;
return options;
}
defaultLoadOptions_(request) {
const options = {};
defaultLoadOptions_(request:any) {
const options:any = {};
const fields = this.fields_(request, []);
if (fields.length) options.fields = fields;
return options;
}
async execServiceActionFromRequest_(externalApi, request) {
async execServiceActionFromRequest_(externalApi:any, request:any) {
const action = externalApi[request.action];
if (!action) throw new ErrorNotFound(`Invalid action: ${request.action}`);
const args = Object.assign({}, request);
@ -355,7 +363,7 @@ class Api {
return action(args);
}
async action_services(request, serviceName) {
async action_services(request:any, serviceName:string) {
this.checkToken_(request);
if (request.method !== 'POST') throw new ErrorMethodNotAllowed();
@ -366,7 +374,7 @@ class Api {
return this.execServiceActionFromRequest_(externalApi, JSON.parse(request.body));
}
async action_notes(request, id = null, link = null) {
async action_notes(request:any, id:string = null, link:string = null) {
this.checkToken_(request);
if (request.method === 'GET') {
@ -398,17 +406,17 @@ class Api {
const requestId = Date.now();
const requestNote = JSON.parse(request.body);
const allowFileProtocolImages = urlUtils.urlProtocol(requestNote.base_url).toLowerCase() === 'file:';
// const allowFileProtocolImages = urlUtils.urlProtocol(requestNote.base_url).toLowerCase() === 'file:';
const imageSizes = requestNote.image_sizes ? requestNote.image_sizes : {};
let note = await this.requestNoteToNote_(requestNote);
let note:any = await this.requestNoteToNote_(requestNote);
const imageUrls = ArrayUtils.unique(markupLanguageUtils.extractImageUrls(note.markup_language, note.body));
this.logger().info(`Request (${requestId}): Downloading images: ${imageUrls.length}`);
let result = await this.downloadImages_(imageUrls, allowFileProtocolImages);
let result = await this.downloadImages_(imageUrls); // , allowFileProtocolImages);
this.logger().info(`Request (${requestId}): Creating resources from paths: ${Object.getOwnPropertyNames(result).length}`);
@ -469,8 +477,8 @@ class Api {
return this.htmlToMdParser_;
}
async requestNoteToNote_(requestNote) {
const output = {
async requestNoteToNote_(requestNote:any) {
const output:any = {
title: requestNote.title ? requestNote.title : '',
body: requestNote.body ? requestNote.body : '',
};
@ -547,19 +555,19 @@ class Api {
}
// Note must have been saved first
async attachImageFromDataUrl_(note, imageDataUrl, cropRect) {
async attachImageFromDataUrl_(note:any, imageDataUrl:string, cropRect:any) {
const tempDir = Setting.value('tempDir');
const mime = mimeUtils.fromDataUrl(imageDataUrl);
let ext = mimeUtils.toFileExtension(mime) || '';
if (ext) ext = `.${ext}`;
const tempFilePath = `${tempDir}/${md5(`${Math.random()}_${Date.now()}`)}${ext}`;
const imageConvOptions = {};
const imageConvOptions:any = {};
if (cropRect) imageConvOptions.cropRect = cropRect;
await shim.imageFromDataUrl(imageDataUrl, tempFilePath, imageConvOptions);
return await shim.attachFileToNote(note, tempFilePath);
}
async tryToGuessImageExtFromMimeType_(response, imagePath) {
async tryToGuessImageExtFromMimeType_(response:any, imagePath:string) {
const mimeType = netUtils.mimeTypeFromHeaders(response.headers);
if (!mimeType) return imagePath;
@ -571,7 +579,7 @@ class Api {
return newImagePath;
}
async buildNoteStyleSheet_(stylesheets) {
async buildNoteStyleSheet_(stylesheets:any[]) {
if (!stylesheets) return [];
const output = [];
@ -597,7 +605,7 @@ class Api {
return output;
}
async downloadImage_(url /* , allowFileProtocolImages */) {
async downloadImage_(url:string /* , allowFileProtocolImages */) {
const tempDir = Setting.value('tempDir');
const isDataUrl = url && url.toLowerCase().indexOf('data:') === 0;
@ -633,13 +641,13 @@ class Api {
}
}
async downloadImages_(urls, allowFileProtocolImages) {
async downloadImages_(urls:string[] /* , allowFileProtocolImages:boolean */) {
const PromisePool = require('es6-promise-pool');
const output = {};
const output:any = {};
const downloadOne = async url => {
const imagePath = await this.downloadImage_(url, allowFileProtocolImages);
const downloadOne = async (url:string) => {
const imagePath = await this.downloadImage_(url); // , allowFileProtocolImages);
if (imagePath) output[url] = { path: imagePath, originalUrl: url };
};
@ -658,10 +666,10 @@ class Api {
return output;
}
async createResourcesFromPaths_(urls) {
async createResourcesFromPaths_(urls:string[]) {
for (const url in urls) {
if (!urls.hasOwnProperty(url)) continue;
const urlInfo = urls[url];
const urlInfo:any = urls[url];
try {
const resource = await shim.createResourceFromPath(urlInfo.path);
urlInfo.resource = resource;
@ -672,10 +680,10 @@ class Api {
return urls;
}
async removeTempFiles_(urls) {
async removeTempFiles_(urls:string[]) {
for (const url in urls) {
if (!urls.hasOwnProperty(url)) continue;
const urlInfo = urls[url];
const urlInfo:any = urls[url];
try {
await shim.fsDriver().remove(urlInfo.path);
} catch (error) {
@ -684,18 +692,18 @@ class Api {
}
}
replaceImageUrlsByResources_(markupLanguage, md, urls, imageSizes) {
const imageSizesIndexes = {};
replaceImageUrlsByResources_(markupLanguage:number, md:string, urls:any, imageSizes:any) {
const imageSizesIndexes:any = {};
if (markupLanguage === MarkupToHtml.MARKUP_LANGUAGE_HTML) {
return htmlUtils.replaceImageUrls(md, imageUrl => {
const urlInfo = urls[imageUrl];
return htmlUtils.replaceImageUrls(md, (imageUrl:string) => {
const urlInfo:any = urls[imageUrl];
if (!urlInfo || !urlInfo.resource) return imageUrl;
return Resource.internalUrl(urlInfo.resource);
});
} else {
// eslint-disable-next-line no-useless-escape
return md.replace(/(!\[.*?\]\()([^\s\)]+)(.*?\))/g, (match, before, imageUrl, after) => {
return md.replace(/(!\[.*?\]\()([^\s\)]+)(.*?\))/g, (_match:any, before:string, imageUrl:string, after:string) => {
const urlInfo = urls[imageUrl];
if (!urlInfo || !urlInfo.resource) return before + imageUrl + after;
if (!(urlInfo.originalUrl in imageSizesIndexes)) imageSizesIndexes[urlInfo.originalUrl] = 0;
@ -723,5 +731,3 @@ class Api {
}
}
}
module.exports = Api;

View File

@ -1,6 +1,6 @@
const { Logger } = require('lib/logger.js');
const Logger = require('lib/Logger').default;
const ItemChange = require('lib/models/ItemChange.js');
const Setting = require('lib/models/Setting.js');
const Setting = require('lib/models/Setting').default;
const Note = require('lib/models/Note.js');
const BaseModel = require('lib/BaseModel.js');
const ItemChangeUtils = require('lib/services/ItemChangeUtils');
@ -9,6 +9,7 @@ const removeDiacritics = require('diacritics').remove;
const { sprintf } = require('sprintf-js');
const filterParser = require('./filterParser').default;
const queryBuilder = require('./queryBuilder').default;
const shim = require('lib/shim').default;
class SearchEngine {
@ -95,7 +96,7 @@ class SearchEngine {
scheduleSyncTables() {
if (this.scheduleSyncTablesIID_) return;
this.scheduleSyncTablesIID_ = setTimeout(async () => {
this.scheduleSyncTablesIID_ = shim.setTimeout(async () => {
try {
await this.syncTables();
} catch (error) {
@ -672,15 +673,15 @@ class SearchEngine {
async destroy() {
if (this.scheduleSyncTablesIID_) {
clearTimeout(this.scheduleSyncTablesIID_);
shim.clearTimeout(this.scheduleSyncTablesIID_);
this.scheduleSyncTablesIID_ = null;
}
SearchEngine.instance_ = null;
return new Promise((resolve) => {
const iid = setInterval(() => {
const iid = shim.setInterval(() => {
if (!this.syncCalls_.length) {
clearInterval(iid);
shim.clearInterval(iid);
this.instance_ = null;
resolve();
}

View File

@ -1,4 +1,5 @@
import { Dirnames } from './utils/types';
import shim from 'lib/shim';
const JoplinError = require('lib/JoplinError');
const { time } = require('lib/time-utils');
@ -277,7 +278,7 @@ export default class LockHandler {
inProgress: false,
};
this.refreshTimers_[handle].id = setInterval(async () => {
this.refreshTimers_[handle].id = shim.setInterval(async () => {
if (this.refreshTimers_[handle].inProgress) return;
const defer = () => {
@ -310,7 +311,7 @@ export default class LockHandler {
if (error) {
if (this.refreshTimers_[handle]) {
clearInterval(this.refreshTimers_[handle].id);
shim.clearInterval(this.refreshTimers_[handle].id);
delete this.refreshTimers_[handle];
}
errorHandler(error);
@ -331,7 +332,7 @@ export default class LockHandler {
return;
}
clearInterval(this.refreshTimers_[handle].id);
shim.clearInterval(this.refreshTimers_[handle].id);
delete this.refreshTimers_[handle];
}

View File

@ -1,6 +1,6 @@
import LockHandler, { LockType } from './LockHandler';
import { Dirnames } from './utils/types';
const BaseService = require('lib/services/BaseService.js');
const BaseService = require('lib/services/BaseService').default;
// To add a new migration:
// - Add the migration logic in ./migrations/VERSION_NUM.js
@ -13,7 +13,7 @@ const migrations = [
require('./migrations/2.js').default,
];
const Setting = require('lib/models/Setting');
const Setting = require('lib/models/Setting').default;
const { sprintf } = require('sprintf-js');
const JoplinError = require('lib/JoplinError');

View File

@ -1,6 +1,6 @@
import { useEffect, useState } from 'react';
import MigrationHandler from 'lib/services/synchronizer/MigrationHandler';
const Setting = require('lib/models/Setting');
const Setting = require('lib/models/Setting').default;
const { reg } = require('lib/registry');
export interface SyncTargetUpgradeResult {