2021-12-27 18:44:53 +02:00
|
|
|
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';
|
2024-06-10 23:31:38 +02:00
|
|
|
import { fileUrlToResourceUrl, parseResourceUrl, urlProtocol } from '@joplin/lib/urlUtils';
|
2023-08-23 19:16:06 +02:00
|
|
|
import { fileUriToPath } from '@joplin/utils/url';
|
2024-06-10 23:31:38 +02:00
|
|
|
import { urlDecode } from '@joplin/lib/string-utils';
|
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
2021-12-27 18:44:53 +02:00
|
|
|
|
|
|
|
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');
|
|
|
|
|
2024-06-10 23:31:38 +02:00
|
|
|
const fromFileUrl = fileUrlToResourceUrl(link, Setting.value('resourceDir'));
|
|
|
|
if (fromFileUrl) {
|
|
|
|
link = fromFileUrl;
|
|
|
|
}
|
|
|
|
|
2021-12-27 18:44:53 +02:00
|
|
|
if (link.startsWith('joplin://') || link.startsWith(':/')) {
|
2022-11-28 19:01:00 +02:00
|
|
|
const parsedUrl = parseResourceUrl(link);
|
|
|
|
if (parsedUrl) {
|
|
|
|
const { itemId, hash } = parsedUrl;
|
|
|
|
await openItemById(itemId, context.dispatch, hash);
|
|
|
|
} else {
|
2024-02-22 23:29:16 +02:00
|
|
|
void bridge().openExternal(link);
|
2022-11-28 19:01:00 +02:00
|
|
|
}
|
2021-12-27 18:44:53 +02:00
|
|
|
} 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());
|
2024-02-22 23:29:16 +02:00
|
|
|
void bridge().openItem(decodedPath);
|
2021-12-27 18:44:53 +02:00
|
|
|
} else {
|
2024-02-22 23:29:16 +02:00
|
|
|
void bridge().openExternal(link);
|
2021-12-27 18:44:53 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bridge().showErrorMessageBox(_('Unsupported link or message: %s', link));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|