2020-11-07 17:59:37 +02:00
|
|
|
import { utils as pluginUtils, PluginStates } from '@joplin/lib/services/plugins/reducer';
|
|
|
|
import CommandService from '@joplin/lib/services/CommandService';
|
|
|
|
import eventManager from '@joplin/lib/eventManager';
|
|
|
|
import InteropService from '@joplin/lib/services/interop/InteropService';
|
|
|
|
import MenuUtils from '@joplin/lib/services/commands/MenuUtils';
|
2020-10-09 19:35:46 +02:00
|
|
|
import InteropServiceHelper from '../../InteropServiceHelper';
|
2020-11-07 17:59:37 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import { MenuItemLocation } from '@joplin/lib/services/plugins/api/types';
|
2021-10-16 11:07:41 +02:00
|
|
|
import { getNoteCallbackUrl } from '@joplin/lib/callbackUrlUtils';
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
import BaseModel from '@joplin/lib/BaseModel';
|
2021-10-01 20:35:27 +02:00
|
|
|
const bridge = require('@electron/remote').require('./bridge').default;
|
2019-01-29 20:02:34 +02:00
|
|
|
const Menu = bridge().Menu;
|
|
|
|
const MenuItem = bridge().MenuItem;
|
2021-01-22 19:41:11 +02:00
|
|
|
import Note from '@joplin/lib/models/Note';
|
2021-01-29 20:45:11 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
2021-10-16 11:07:41 +02:00
|
|
|
const { clipboard } = require('electron');
|
2019-01-29 20:02:34 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
interface ContextMenuProps {
|
2020-11-12 21:29:22 +02:00
|
|
|
notes: any[];
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
dispatch: Function;
|
|
|
|
watchedNoteFiles: string[];
|
|
|
|
plugins: PluginStates;
|
2020-11-17 13:50:46 +02:00
|
|
|
inConflictFolder: boolean;
|
2021-05-19 15:00:16 +02:00
|
|
|
customCss: string;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class NoteListUtils {
|
2023-03-06 16:22:01 +02:00
|
|
|
public static makeContextMenu(noteIds: string[], props: ContextMenuProps) {
|
2020-07-03 23:32:39 +02:00
|
|
|
const cmdService = CommandService.instance();
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
const menuUtils = new MenuUtils(cmdService);
|
|
|
|
|
2020-05-21 10:14:33 +02:00
|
|
|
const notes = noteIds.map(id => BaseModel.byId(props.notes, id));
|
2019-01-29 20:02:34 +02:00
|
|
|
|
2020-10-10 14:32:30 +02:00
|
|
|
const singleNoteId = noteIds.length === 1 ? noteIds[0] : null;
|
|
|
|
|
2019-01-29 20:02:34 +02:00
|
|
|
let hasEncrypted = false;
|
|
|
|
for (let i = 0; i < notes.length; i++) {
|
2019-07-29 14:13:23 +02:00
|
|
|
if (notes[i].encryption_applied) hasEncrypted = true;
|
2019-01-29 20:02:34 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
const menu = new Menu();
|
2019-01-29 20:02:34 +02:00
|
|
|
|
|
|
|
if (!hasEncrypted) {
|
2019-07-29 14:13:23 +02:00
|
|
|
menu.append(
|
2020-10-18 22:52:10 +02:00
|
|
|
new MenuItem(menuUtils.commandToStatefulMenuItem('setTags', noteIds))
|
2019-07-29 14:13:23 +02:00
|
|
|
);
|
|
|
|
|
2020-04-04 19:03:09 +02:00
|
|
|
menu.append(
|
2020-10-18 22:52:10 +02:00
|
|
|
new MenuItem(menuUtils.commandToStatefulMenuItem('moveToFolder', noteIds))
|
2020-04-04 19:03:09 +02:00
|
|
|
);
|
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
menu.append(
|
2023-07-16 18:42:42 +02:00
|
|
|
new MenuItem(menuUtils.commandToStatefulMenuItem('duplicateNote', noteIds))
|
2019-07-29 14:13:23 +02:00
|
|
|
);
|
2019-01-29 20:02:34 +02:00
|
|
|
|
2020-10-10 14:32:30 +02:00
|
|
|
if (singleNoteId) {
|
|
|
|
const cmd = props.watchedNoteFiles.includes(singleNoteId) ? 'stopExternalEditing' : 'startExternalEditing';
|
2020-10-18 22:52:10 +02:00
|
|
|
menu.append(new MenuItem(menuUtils.commandToStatefulMenuItem(cmd, singleNoteId)));
|
2020-01-06 23:16:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 20:02:34 +02:00
|
|
|
if (noteIds.length <= 1) {
|
2019-07-29 14:13:23 +02:00
|
|
|
menu.append(
|
2023-07-16 18:42:42 +02:00
|
|
|
new MenuItem(
|
|
|
|
menuUtils.commandToStatefulMenuItem('toggleNoteType', noteIds)
|
|
|
|
)
|
2019-07-29 14:13:23 +02:00
|
|
|
);
|
2019-01-29 20:02:34 +02:00
|
|
|
} else {
|
2020-11-12 21:13:28 +02:00
|
|
|
const switchNoteType = async (noteIds: string[], type: string) => {
|
2019-01-29 20:02:34 +02:00
|
|
|
for (let i = 0; i < noteIds.length; i++) {
|
|
|
|
const note = await Note.load(noteIds[i]);
|
|
|
|
const newNote = Note.changeNoteType(note, type);
|
|
|
|
if (newNote === note) continue;
|
|
|
|
await Note.save(newNote, { userSideValidation: true });
|
|
|
|
eventManager.emit('noteTypeToggle', { noteId: note.id });
|
|
|
|
}
|
2019-07-29 14:13:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
menu.append(
|
|
|
|
new MenuItem({
|
|
|
|
label: _('Switch to note type'),
|
|
|
|
click: async () => {
|
|
|
|
await switchNoteType(noteIds, 'note');
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
menu.append(
|
|
|
|
new MenuItem({
|
|
|
|
label: _('Switch to to-do type'),
|
|
|
|
click: async () => {
|
|
|
|
await switchNoteType(noteIds, 'todo');
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2019-01-29 20:02:34 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
menu.append(
|
|
|
|
new MenuItem({
|
|
|
|
label: _('Copy Markdown link'),
|
|
|
|
click: async () => {
|
|
|
|
const links = [];
|
|
|
|
for (let i = 0; i < noteIds.length; i++) {
|
|
|
|
const note = await Note.load(noteIds[i]);
|
|
|
|
links.push(Note.markdownTag(note));
|
|
|
|
}
|
|
|
|
clipboard.writeText(links.join(' '));
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2019-01-29 20:02:34 +02:00
|
|
|
|
2021-10-16 11:07:41 +02:00
|
|
|
if (noteIds.length === 1) {
|
|
|
|
menu.append(
|
|
|
|
new MenuItem({
|
|
|
|
label: _('Copy external link'),
|
|
|
|
click: () => {
|
|
|
|
clipboard.writeText(getNoteCallbackUrl(noteIds[0]));
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-06 19:14:12 +02:00
|
|
|
if ([9, 10].includes(Setting.value('sync.target'))) {
|
2021-01-29 20:45:11 +02:00
|
|
|
menu.append(
|
|
|
|
new MenuItem(
|
|
|
|
menuUtils.commandToStatefulMenuItem('showShareNoteDialog', noteIds.slice())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2019-12-13 03:16:34 +02:00
|
|
|
|
2019-01-29 20:02:34 +02:00
|
|
|
const exportMenu = new Menu();
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
const ioService = InteropService.instance();
|
2019-01-29 20:02:34 +02:00
|
|
|
const ioModules = ioService.modules();
|
|
|
|
for (let i = 0; i < ioModules.length; i++) {
|
|
|
|
const module = ioModules[i];
|
|
|
|
if (module.type !== 'exporter') continue;
|
2020-10-09 19:35:46 +02:00
|
|
|
if (noteIds.length > 1 && module.isNoteArchive === false) continue;
|
2019-01-29 20:02:34 +02:00
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
exportMenu.append(
|
|
|
|
new MenuItem({
|
|
|
|
label: module.fullLabel(),
|
|
|
|
click: async () => {
|
2020-11-17 13:50:46 +02:00
|
|
|
await InteropServiceHelper.export(props.dispatch.bind(this), module, {
|
|
|
|
sourceNoteIds: noteIds,
|
|
|
|
includeConflicts: props.inConflictFolder,
|
2020-12-19 19:42:18 +02:00
|
|
|
plugins: props.plugins,
|
2021-05-19 15:00:16 +02:00
|
|
|
customCss: props.customCss,
|
2020-11-17 13:50:46 +02:00
|
|
|
});
|
2019-07-29 14:13:23 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2019-01-29 20:02:34 +02:00
|
|
|
}
|
|
|
|
|
2020-02-11 16:14:04 +02:00
|
|
|
exportMenu.append(
|
2020-07-03 23:32:39 +02:00
|
|
|
new MenuItem(
|
2020-10-18 22:52:10 +02:00
|
|
|
menuUtils.commandToStatefulMenuItem('exportPdf', noteIds)
|
2020-07-03 23:32:39 +02:00
|
|
|
)
|
2020-02-11 16:14:04 +02:00
|
|
|
);
|
2019-01-29 20:02:34 +02:00
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
const exportMenuItem = new MenuItem({ label: _('Export'), submenu: exportMenu });
|
2019-01-29 20:02:34 +02:00
|
|
|
|
|
|
|
menu.append(exportMenuItem);
|
|
|
|
}
|
|
|
|
|
2019-07-29 14:13:23 +02:00
|
|
|
menu.append(
|
2023-07-16 18:42:42 +02:00
|
|
|
new MenuItem(
|
|
|
|
menuUtils.commandToStatefulMenuItem('deleteNote', noteIds)
|
|
|
|
)
|
2019-07-29 14:13:23 +02:00
|
|
|
);
|
2019-01-29 20:02:34 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
const pluginViewInfos = pluginUtils.viewInfosByType(props.plugins, 'menuItem');
|
|
|
|
|
|
|
|
for (const info of pluginViewInfos) {
|
|
|
|
const location = info.view.location;
|
2020-11-14 02:02:17 +02:00
|
|
|
if (location !== MenuItemLocation.Context && location !== MenuItemLocation.NoteListContextMenu) continue;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2021-08-09 20:04:40 +02:00
|
|
|
if (cmdService.isEnabled(info.view.commandName)) {
|
|
|
|
menu.append(
|
|
|
|
new MenuItem(menuUtils.commandToStatefulMenuItem(info.view.commandName, noteIds))
|
|
|
|
);
|
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2019-01-29 20:02:34 +02:00
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|