From 83c0c48c83fa57ac53166aa39090906a17c0c58b Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Mon, 27 Dec 2021 17:44:53 +0100 Subject: [PATCH] Plugins: Added "openItem" command --- .eslintignore | 3 ++ .gitignore | 3 ++ .../gui/MainScreen/commands/index.ts | 2 + .../gui/MainScreen/commands/openItem.ts | 39 +++++++++++++++++++ .../NoteBody/CodeMirror/CodeMirror.tsx | 6 ++- .../gui/NoteEditor/utils/useMessageHandler.ts | 27 ++----------- 6 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 packages/app-desktop/gui/MainScreen/commands/openItem.ts diff --git a/.eslintignore b/.eslintignore index 8770d3931..ddc583d55 100644 --- a/.eslintignore +++ b/.eslintignore @@ -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 diff --git a/.gitignore b/.gitignore index 314e1146f..9b174f0c7 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/packages/app-desktop/gui/MainScreen/commands/index.ts b/packages/app-desktop/gui/MainScreen/commands/index.ts index 97743f6c2..c5640c0c8 100644 --- a/packages/app-desktop/gui/MainScreen/commands/index.ts +++ b/packages/app-desktop/gui/MainScreen/commands/index.ts @@ -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, diff --git a/packages/app-desktop/gui/MainScreen/commands/openItem.ts b/packages/app-desktop/gui/MainScreen/commands/openItem.ts new file mode 100644 index 000000000..61984f5d5 --- /dev/null +++ b/packages/app-desktop/gui/MainScreen/commands/openItem.ts @@ -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)); + } + }, + }; +}; diff --git a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/CodeMirror.tsx b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/CodeMirror.tsx index 0f300ea10..1a4cb7128 100644 --- a/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/CodeMirror.tsx +++ b/packages/app-desktop/gui/NoteEditor/NoteBody/CodeMirror/CodeMirror.tsx @@ -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); } diff --git a/packages/app-desktop/gui/NoteEditor/utils/useMessageHandler.ts b/packages/app-desktop/gui/NoteEditor/utils/useMessageHandler.ts index a96ddb40e..cdc94e5ad 100644 --- a/packages/app-desktop/gui/NoteEditor/utils/useMessageHandler.ts +++ b/packages/app-desktop/gui/NoteEditor/utils/useMessageHandler.ts @@ -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]); }