You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-11-23 22:36:32 +02:00
Desktop: Add dialog to select a note and link to it (#11891)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService';
|
||||
import { _ } from '@joplin/lib/locale';
|
||||
import { GotoAnythingUserData, Mode, UserDataCallbackReject, UserDataCallbackResolve } from '../../../plugins/GotoAnything';
|
||||
const PluginManager = require('@joplin/lib/services/PluginManager');
|
||||
|
||||
export enum UiType {
|
||||
@@ -8,6 +9,10 @@ export enum UiType {
|
||||
ControlledApi = 'controlledApi',
|
||||
}
|
||||
|
||||
export interface GotoAnythingOptions {
|
||||
mode?: Mode;
|
||||
}
|
||||
|
||||
export const declaration: CommandDeclaration = {
|
||||
name: 'gotoAnything',
|
||||
label: () => _('Goto Anything...'),
|
||||
@@ -24,19 +29,26 @@ function menuItemById(id: string) {
|
||||
// calling the click() handler.
|
||||
export const runtime = (): CommandRuntime => {
|
||||
return {
|
||||
execute: async (_context: CommandContext, uiType: UiType = UiType.GotoAnything) => {
|
||||
execute: async (_context: CommandContext, uiType: UiType = UiType.GotoAnything, options: GotoAnythingOptions = null) => {
|
||||
options = {
|
||||
mode: Mode.Default,
|
||||
...options,
|
||||
};
|
||||
|
||||
if (uiType === UiType.GotoAnything) {
|
||||
menuItemById('gotoAnything').click();
|
||||
} else if (uiType === UiType.CommandPalette) {
|
||||
menuItemById('commandPalette').click();
|
||||
} else if (uiType === UiType.ControlledApi) {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
||||
return new Promise((resolve: Function, reject: Function) => {
|
||||
return new Promise((resolve: UserDataCallbackResolve, reject: UserDataCallbackReject) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
||||
const menuItem = PluginManager.instance().menuItems().find((i: any) => i.id === 'controlledApi');
|
||||
menuItem.userData = {
|
||||
const userData: GotoAnythingUserData = {
|
||||
callback: { resolve, reject },
|
||||
mode: options.mode,
|
||||
};
|
||||
menuItem.userData = userData;
|
||||
menuItem.click();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import * as exportPdf from './exportPdf';
|
||||
import * as gotoAnything from './gotoAnything';
|
||||
import * as hideModalMessage from './hideModalMessage';
|
||||
import * as leaveSharedFolder from './leaveSharedFolder';
|
||||
import * as linkToNote from './linkToNote';
|
||||
import * as moveToFolder from './moveToFolder';
|
||||
import * as newFolder from './newFolder';
|
||||
import * as newNote from './newNote';
|
||||
@@ -56,6 +57,7 @@ const index: any[] = [
|
||||
gotoAnything,
|
||||
hideModalMessage,
|
||||
leaveSharedFolder,
|
||||
linkToNote,
|
||||
moveToFolder,
|
||||
newFolder,
|
||||
newNote,
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import CommandService, { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService';
|
||||
import { _ } from '@joplin/lib/locale';
|
||||
import { Mode } from '../../../plugins/GotoAnything';
|
||||
import { GotoAnythingOptions, UiType } from './gotoAnything';
|
||||
import { ModelType } from '@joplin/lib/BaseModel';
|
||||
import Logger from '@joplin/utils/Logger';
|
||||
import markdownUtils from '@joplin/lib/markdownUtils';
|
||||
|
||||
const logger = Logger.create('linkToNote');
|
||||
|
||||
export const declaration: CommandDeclaration = {
|
||||
name: 'linkToNote',
|
||||
label: () => _('Link to note...'),
|
||||
};
|
||||
|
||||
export const runtime = (): CommandRuntime => {
|
||||
return {
|
||||
execute: async (_context: CommandContext) => {
|
||||
const options: GotoAnythingOptions = {
|
||||
mode: Mode.TitleOnly,
|
||||
};
|
||||
const result = await CommandService.instance().execute('gotoAnything', UiType.ControlledApi, options);
|
||||
if (!result) return result;
|
||||
|
||||
if (result.type !== ModelType.Note) {
|
||||
logger.warn('Retrieved item is not a note:', result);
|
||||
return null;
|
||||
}
|
||||
|
||||
const link = `[${markdownUtils.escapeTitleText(result.item.title)}](:/${markdownUtils.escapeLinkUrl(result.item.id)})`;
|
||||
await CommandService.instance().execute('insertText', link);
|
||||
return result;
|
||||
},
|
||||
|
||||
enabledCondition: 'markdownEditorPaneVisible || richTextEditorVisible',
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user