trilium/src/public/app/desktop.js

144 lines
4.5 KiB
JavaScript
Raw Normal View History

import appContext from "./services/app_context.js";
import utils from './services/utils.js';
import noteTooltipService from './services/note_tooltip.js';
2020-04-12 20:22:51 +08:00
import bundleService from "./services/bundle.js";
import noteAutocompleteService from './services/note_autocomplete.js';
import macInit from './services/mac_init.js';
2020-02-29 18:28:30 +08:00
import contextMenu from "./services/context_menu.js";
2020-03-01 17:41:23 +08:00
import DesktopLayout from "./widgets/desktop_layout.js";
2020-04-12 20:22:51 +08:00
import glob from "./services/glob.js";
glob.setupGlobs();
2018-03-24 23:18:46 +08:00
if (utils.isElectron()) {
2020-04-12 20:22:51 +08:00
utils.dynamicRequire('electron').ipcRenderer.on('globalShortcut', async function(event, actionName) {
2020-03-18 04:39:26 +08:00
appContext.triggerCommand(actionName);
});
}
2018-03-26 10:37:02 +08:00
$('[data-toggle="tooltip"]').tooltip({
html: true
});
2020-02-15 03:18:09 +08:00
macInit.init();
2020-03-17 04:16:09 +08:00
bundleService.getWidgetBundlesByParent().then(widgetBundles => {
const desktopLayout = new DesktopLayout(widgetBundles);
appContext.setLayout(desktopLayout);
appContext.start();
});
noteTooltipService.setupGlobalTooltip();
noteAutocompleteService.init();
if (utils.isElectron()) {
2020-04-12 20:22:51 +08:00
const electron = utils.dynamicRequire('electron');
2020-02-29 18:28:30 +08:00
const {webContents} = electron.remote.getCurrentWindow();
webContents.on('context-menu', (event, params) => {
2020-02-29 18:28:30 +08:00
const {editFlags} = params;
const hasText = params.selectionText.trim().length > 0;
const isMac = process.platform === "darwin";
const platformModifier = isMac ? 'Meta' : 'Ctrl';
2020-02-29 18:28:30 +08:00
const items = [];
2020-02-29 18:28:30 +08:00
if (params.misspelledWord) {
for (const suggestion of params.dictionarySuggestions) {
items.push({
title: suggestion,
command: "replaceMisspelling",
spellingSuggestion: suggestion,
2020-02-29 18:28:30 +08:00
uiIcon: "empty"
});
}
2020-02-29 18:28:30 +08:00
items.push({
title: `Add "${params.misspelledWord}" to dictionary`,
uiIcon: "plus",
handler: () => webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord)
});
items.push({ title: `----` });
}
if (params.isEditable) {
items.push({
enabled: editFlags.canCut && hasText,
title: `Cut <kbd>${platformModifier}+X`,
uiIcon: "cut",
handler: () => webContents.cut()
});
}
if (params.isEditable || hasText) {
items.push({
enabled: editFlags.canCopy && hasText,
title: `Copy <kbd>${platformModifier}+C`,
uiIcon: "copy",
handler: () => webContents.copy()
});
}
if (params.linkURL.length !== 0 && params.mediaType === 'none') {
items.push({
title: `Copy link`,
uiIcon: "copy",
handler: () => {
electron.clipboard.write({
bookmark: params.linkText,
text: params.linkURL
});
}
});
}
if (params.isEditable) {
items.push({
enabled: editFlags.canPaste,
title: `Paste <kbd>${platformModifier}+V`,
uiIcon: "paste",
handler: () => webContents.paste()
});
}
if (params.isEditable) {
items.push({
enabled: editFlags.canPaste,
title: `Paste as plain text <kbd>${platformModifier}+Shift+V`,
uiIcon: "paste",
handler: () => webContents.pasteAndMatchStyle()
});
}
if (hasText) {
const shortenedSelection = params.selectionText.length > 15
? (params.selectionText.substr(0, 13) + "…")
: params.selectionText;
items.push({
enabled: editFlags.canPaste,
title: `Search for "${shortenedSelection}" with DuckDuckGo`,
uiIcon: "search-alt",
handler: () => electron.shell.openExternal(`https://duckduckgo.com/?q=${encodeURIComponent(params.selectionText)}`)
});
}
2020-03-09 00:17:18 +08:00
if (items.length === 0) {
return;
}
2020-02-29 18:28:30 +08:00
contextMenu.show({
x: params.x,
y: params.y,
items,
2020-02-29 18:28:30 +08:00
selectMenuItemHandler: ({command, spellingSuggestion}) => {
if (command === 'replaceMisspelling') {
2020-02-29 18:28:30 +08:00
webContents.insertText(spellingSuggestion);
}
}
});
});
}