1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-12-02 22:49:09 +02:00

Mobile: Fixes #11455: Clicking on an external note link from within a note logs an error (#11619)

This commit is contained in:
Henry Heino
2025-01-09 07:21:06 -08:00
committed by GitHub
parent e8f305dea5
commit 72575e3c6f

View File

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