You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-12-02 22:49:09 +02:00
This commit is contained in:
@@ -8,6 +8,7 @@ import BaseItem from '@joplin/lib/models/BaseItem';
|
||||
import { BaseItemEntity } from '@joplin/lib/services/database/types';
|
||||
import { ModelType } from '@joplin/lib/BaseModel';
|
||||
import showResource from './util/showResource';
|
||||
import { isCallbackUrl, parseCallbackUrl } from '@joplin/lib/callbackUrlUtils';
|
||||
|
||||
const logger = Logger.create('openItemCommand');
|
||||
|
||||
@@ -15,32 +16,48 @@ export const declaration: CommandDeclaration = {
|
||||
name: 'openItem',
|
||||
};
|
||||
|
||||
const openItemById = async (itemId: string, hash?: string) => {
|
||||
logger.info(`Navigating to item ${itemId}`);
|
||||
const item: BaseItemEntity = await BaseItem.loadItemById(itemId);
|
||||
|
||||
if (item.type_ === ModelType.Note) {
|
||||
await goToNote(itemId, hash);
|
||||
} else if (item.type_ === ModelType.Resource) {
|
||||
await showResource(item);
|
||||
} else {
|
||||
throw new Error(`Unsupported item type for links: ${item.type_}`);
|
||||
}
|
||||
};
|
||||
|
||||
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 parsedUrl = parseResourceUrl(link);
|
||||
if (parsedUrl) {
|
||||
const { itemId, hash } = parsedUrl;
|
||||
try {
|
||||
if (link.startsWith('joplin://') || link.startsWith(':/')) {
|
||||
const parsedResourceUrl = parseResourceUrl(link);
|
||||
const parsedCallbackUrl = isCallbackUrl(link) ? parseCallbackUrl(link) : null;
|
||||
|
||||
logger.info(`Navigating to item ${itemId}`);
|
||||
const item: BaseItemEntity = await BaseItem.loadItemById(itemId);
|
||||
if (item.type_ === ModelType.Note) {
|
||||
await goToNote(itemId, hash);
|
||||
} else if (item.type_ === ModelType.Resource) {
|
||||
await showResource(item);
|
||||
if (parsedResourceUrl) {
|
||||
const { itemId, hash } = parsedResourceUrl;
|
||||
await openItemById(itemId, hash);
|
||||
} else if (parsedCallbackUrl) {
|
||||
const id = parsedCallbackUrl.params.id;
|
||||
if (!id) {
|
||||
throw new Error('Missing item ID');
|
||||
}
|
||||
await openItemById(id);
|
||||
} else {
|
||||
logger.error('Unsupported item type for links:', item.type_);
|
||||
throw new Error('Unsupported link format.');
|
||||
}
|
||||
} else if (urlProtocol(link)) {
|
||||
shim.openUrl(link);
|
||||
} else {
|
||||
logger.error(`Invalid Joplin link: ${link}`);
|
||||
throw new Error('Unsupported protocol');
|
||||
}
|
||||
} else if (urlProtocol(link)) {
|
||||
shim.openUrl(link);
|
||||
} else {
|
||||
const errorMessage = _('Unsupported link or message: %s', link);
|
||||
} catch (error) {
|
||||
const errorMessage = _('Unsupported link or message: %s.\nError: %s', link, error);
|
||||
logger.error(errorMessage);
|
||||
await shim.showErrorDialog(errorMessage);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user