一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
81 行
2.7 KiB
TypeScript
81 行
2.7 KiB
TypeScript
import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService';
|
|
import shim from '@joplin/lib/shim';
|
|
import { _ } from '@joplin/lib/locale';
|
|
import { parseResourceUrl, urlProtocol } from '@joplin/lib/urlUtils';
|
|
import Logger from '@joplin/utils/Logger';
|
|
import goToNote from './util/goToNote';
|
|
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';
|
|
import goToFolder from './util/goToFolder';
|
|
|
|
const logger = Logger.create('openItemCommand');
|
|
|
|
const allowedExternalProtocols = ['http:', 'https:', 'mailto:'];
|
|
|
|
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) {
|
|
throw new Error(`Item not found: ${itemId}`);
|
|
}
|
|
|
|
if (item.type_ === ModelType.Note) {
|
|
await goToNote(itemId, hash);
|
|
} else if (item.type_ === ModelType.Resource) {
|
|
await showResource(item);
|
|
} else if (item.type_ === ModelType.Folder) {
|
|
await goToFolder(item.id);
|
|
} 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');
|
|
|
|
try {
|
|
if (link.startsWith('joplin://') || link.startsWith(':/')) {
|
|
const parsedResourceUrl = parseResourceUrl(link);
|
|
const parsedCallbackUrl = isCallbackUrl(link) ? parseCallbackUrl(link) : null;
|
|
|
|
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 {
|
|
throw new Error('Unsupported link format.');
|
|
}
|
|
} else if (urlProtocol(link)) {
|
|
// The link may come from untrusted note content, and this
|
|
// reaches the OS URI dispatcher.
|
|
if (!allowedExternalProtocols.includes(urlProtocol(link))) {
|
|
throw new Error('Unsupported protocol');
|
|
}
|
|
shim.openUrl(link);
|
|
} else {
|
|
throw new Error('Unsupported protocol');
|
|
}
|
|
} catch (error) {
|
|
const errorMessage = _('Unsupported link or message: %s.\nError: %s', link, error);
|
|
logger.error(errorMessage);
|
|
await shim.showErrorDialog(errorMessage);
|
|
}
|
|
},
|
|
};
|
|
};
|