1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Plugins: Added "openItem" command

This commit is contained in:
Laurent Cozic 2021-12-27 17:44:53 +01:00
parent 27b62bf129
commit 83c0c48c83
6 changed files with 54 additions and 26 deletions

View File

@ -298,6 +298,9 @@ packages/app-desktop/gui/MainScreen/commands/openFolder.js.map
packages/app-desktop/gui/MainScreen/commands/openFolderDialog.d.ts
packages/app-desktop/gui/MainScreen/commands/openFolderDialog.js
packages/app-desktop/gui/MainScreen/commands/openFolderDialog.js.map
packages/app-desktop/gui/MainScreen/commands/openItem.d.ts
packages/app-desktop/gui/MainScreen/commands/openItem.js
packages/app-desktop/gui/MainScreen/commands/openItem.js.map
packages/app-desktop/gui/MainScreen/commands/openNote.d.ts
packages/app-desktop/gui/MainScreen/commands/openNote.js
packages/app-desktop/gui/MainScreen/commands/openNote.js.map

3
.gitignore vendored
View File

@ -288,6 +288,9 @@ packages/app-desktop/gui/MainScreen/commands/openFolder.js.map
packages/app-desktop/gui/MainScreen/commands/openFolderDialog.d.ts
packages/app-desktop/gui/MainScreen/commands/openFolderDialog.js
packages/app-desktop/gui/MainScreen/commands/openFolderDialog.js.map
packages/app-desktop/gui/MainScreen/commands/openItem.d.ts
packages/app-desktop/gui/MainScreen/commands/openItem.js
packages/app-desktop/gui/MainScreen/commands/openItem.js.map
packages/app-desktop/gui/MainScreen/commands/openNote.d.ts
packages/app-desktop/gui/MainScreen/commands/openNote.js
packages/app-desktop/gui/MainScreen/commands/openNote.js.map

View File

@ -12,6 +12,7 @@ import * as newSubFolder from './newSubFolder';
import * as newTodo from './newTodo';
import * as openFolder from './openFolder';
import * as openFolderDialog from './openFolderDialog';
import * as openItem from './openItem';
import * as openNote from './openNote';
import * as openTag from './openTag';
import * as print from './print';
@ -50,6 +51,7 @@ const index:any[] = [
newTodo,
openFolder,
openFolderDialog,
openItem,
openNote,
openTag,
print,

View File

@ -0,0 +1,39 @@
import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService';
import shim from '@joplin/lib/shim';
import { _ } from '@joplin/lib/locale';
import bridge from '../../../services/bridge';
import { openItemById } from '../../NoteEditor/utils/contextMenu';
const { parseResourceUrl, urlProtocol, fileUriToPath } = require('@joplin/lib/urlUtils');
const { urlDecode } = require('@joplin/lib/string-utils');
export const declaration: CommandDeclaration = {
name: 'openItem',
};
export const runtime = (): CommandRuntime => {
return {
execute: async (context: CommandContext, link: string) => {
if (!link) throw new Error('Link cannot be empty');
if (link.startsWith('joplin://') || link.startsWith(':/')) {
const { itemId, hash } = parseResourceUrl(link);
await openItemById(itemId, context.dispatch, hash);
} else if (urlProtocol(link)) {
if (link.indexOf('file://') === 0) {
// When using the file:// protocol, openPath doesn't work (does
// nothing) with URL-encoded paths.
//
// shell.openPath seems to work with file:// urls on Windows,
// but doesn't on macOS, so we need to convert it to a path
// before passing it to openPath.
const decodedPath = fileUriToPath(urlDecode(link), shim.platformName());
require('electron').shell.openPath(decodedPath);
} else {
require('electron').shell.openExternal(link);
}
} else {
bridge().showErrorMessageBox(_('Unsupported link or message: %s', link));
}
},
};
};

View File

@ -236,9 +236,11 @@ function CodeMirror(props: NoteBodyEditorProps, ref: any) {
if (!('args' in value)) value.args = [];
if (editorRef.current[value.name]) {
editorRef.current[value.name](...value.args);
const result = editorRef.current[value.name](...value.args);
return result;
} else if (editorRef.current.commandExists(value.name)) {
editorRef.current.execCommand(value.name);
const result = editorRef.current.execCommand(value.name);
return result;
} else {
reg.logger().warn('CodeMirror execCommand: unsupported command: ', value.name);
}

View File

@ -1,16 +1,11 @@
import { useCallback } from 'react';
import { FormNote } from './types';
import contextMenu, { openItemById } from './contextMenu';
import { _ } from '@joplin/lib/locale';
import contextMenu from './contextMenu';
import CommandService from '@joplin/lib/services/CommandService';
import PostMessageService from '@joplin/lib/services/PostMessageService';
import ResourceFetcher from '@joplin/lib/services/ResourceFetcher';
import { reg } from '@joplin/lib/registry';
import shim from '@joplin/lib/shim';
const bridge = require('@electron/remote').require('./bridge').default;
const { urlDecode } = require('@joplin/lib/string-utils');
const urlUtils = require('@joplin/lib/urlUtils');
const { fileUriToPath } = require('@joplin/lib/urlUtils');
export default function useMessageHandler(scrollWhenReady: any, setScrollWhenReady: Function, editorRef: any, setLocalSearchResultCount: Function, dispatch: Function, formNote: FormNote) {
return useCallback(async (event: any) => {
@ -47,23 +42,6 @@ export default function useMessageHandler(scrollWhenReady: any, setScrollWhenRea
}, dispatch);
menu.popup(bridge().window());
} else if (msg.indexOf('joplin://') === 0) {
const { itemId, hash } = urlUtils.parseResourceUrl(msg);
await openItemById(itemId, dispatch, hash);
} else if (urlUtils.urlProtocol(msg)) {
if (msg.indexOf('file://') === 0) {
// When using the file:// protocol, openPath doesn't work (does
// nothing) with URL-encoded paths.
//
// shell.openPath seems to work with file:// urls on Windows,
// but doesn't on macOS, so we need to convert it to a path
// before passing it to openPath.
const decodedPath = fileUriToPath(urlDecode(msg), shim.platformName());
require('electron').shell.openPath(decodedPath);
} else {
require('electron').shell.openExternal(msg);
}
} else if (msg.indexOf('#') === 0) {
// This is an internal anchor, which is handled by the WebView so skip this case
} else if (msg === 'contentScriptExecuteCommand') {
@ -73,7 +51,8 @@ export default function useMessageHandler(scrollWhenReady: any, setScrollWhenRea
} else if (msg === 'postMessageService.message') {
void PostMessageService.instance().postMessage(arg0);
} else {
bridge().showErrorMessageBox(_('Unsupported link or message: %s', msg));
await CommandService.instance().execute('openItem', msg);
// bridge().showErrorMessageBox(_('Unsupported link or message: %s', msg));
}
}, [dispatch, setLocalSearchResultCount, scrollWhenReady, formNote]);
}