2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { BrowserWindow } from 'electron';
|
|
|
|
import { Menu, clipboard, nativeImage } from 'electron';
|
2021-06-18 17:04:27 +00:00
|
|
|
import { uniq } from 'lodash';
|
|
|
|
import { fileURLToPath } from 'url';
|
2020-04-15 20:44:46 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
import { maybeParseUrl } from '../ts/util/url';
|
2022-01-11 19:05:22 +00:00
|
|
|
import type { LocaleType } from './locale';
|
2021-06-18 17:04:27 +00:00
|
|
|
|
2022-06-08 22:00:32 +00:00
|
|
|
import type { MenuListType } from '../ts/types/menu';
|
2021-06-18 17:04:27 +00:00
|
|
|
|
|
|
|
export function getLanguages(
|
|
|
|
userLocale: string,
|
|
|
|
availableLocales: ReadonlyArray<string>
|
|
|
|
): Array<string> {
|
2022-06-27 20:44:13 +00:00
|
|
|
// First attempt to find the exact locale
|
|
|
|
const candidateLocales = uniq([userLocale, userLocale]).filter(l =>
|
2020-04-15 20:44:46 +00:00
|
|
|
availableLocales.includes(l)
|
|
|
|
);
|
|
|
|
if (candidateLocales.length > 0) {
|
|
|
|
return candidateLocales;
|
|
|
|
}
|
|
|
|
|
2022-06-27 20:44:13 +00:00
|
|
|
// If no languages were found then return all locales that start with the base
|
|
|
|
const baseLocale = userLocale.split('-')[0];
|
2020-04-15 20:44:46 +00:00
|
|
|
return uniq(availableLocales.filter(l => l.startsWith(baseLocale)));
|
|
|
|
}
|
2020-03-20 21:00:11 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export const setup = (
|
|
|
|
browserWindow: BrowserWindow,
|
2022-09-27 21:01:06 +00:00
|
|
|
{ name: userLocale, i18n }: LocaleType
|
2021-06-18 17:04:27 +00:00
|
|
|
): void => {
|
2020-03-20 21:00:11 +00:00
|
|
|
const { session } = browserWindow.webContents;
|
2020-04-15 20:44:46 +00:00
|
|
|
const availableLocales = session.availableSpellCheckerLanguages;
|
|
|
|
const languages = getLanguages(userLocale, availableLocales);
|
2020-03-20 21:00:11 +00:00
|
|
|
console.log(`spellcheck: user locale: ${userLocale}`);
|
2020-04-15 20:44:46 +00:00
|
|
|
console.log(
|
|
|
|
'spellcheck: available spellchecker languages: ',
|
|
|
|
availableLocales
|
|
|
|
);
|
2020-03-20 21:00:11 +00:00
|
|
|
console.log('spellcheck: setting languages to: ', languages);
|
|
|
|
session.setSpellCheckerLanguages(languages);
|
|
|
|
|
|
|
|
browserWindow.webContents.on('context-menu', (_event, params) => {
|
|
|
|
const { editFlags } = params;
|
|
|
|
const isMisspelled = Boolean(params.misspelledWord);
|
|
|
|
const isLink = Boolean(params.linkURL);
|
2020-11-03 00:47:46 +00:00
|
|
|
const isImage =
|
|
|
|
params.mediaType === 'image' && params.hasImageContents && params.srcURL;
|
|
|
|
const showMenu =
|
|
|
|
params.isEditable || editFlags.canCopy || isLink || isImage;
|
2020-03-20 21:00:11 +00:00
|
|
|
|
|
|
|
// Popup editor menu
|
|
|
|
if (showMenu) {
|
2021-06-18 17:04:27 +00:00
|
|
|
const template: MenuListType = [];
|
2020-03-20 21:00:11 +00:00
|
|
|
|
|
|
|
if (isMisspelled) {
|
|
|
|
if (params.dictionarySuggestions.length > 0) {
|
|
|
|
template.push(
|
|
|
|
...params.dictionarySuggestions.map(label => ({
|
|
|
|
label,
|
|
|
|
click: () => {
|
|
|
|
browserWindow.webContents.replaceMisspelling(label);
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
template.push({
|
2022-09-27 21:01:06 +00:00
|
|
|
label: i18n('contextMenuNoSuggestions'),
|
2020-03-20 21:00:11 +00:00
|
|
|
enabled: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
template.push({ type: 'separator' });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.isEditable) {
|
|
|
|
if (editFlags.canUndo) {
|
2022-09-27 21:01:06 +00:00
|
|
|
template.push({ label: i18n('editMenuUndo'), role: 'undo' });
|
2020-03-20 21:00:11 +00:00
|
|
|
}
|
|
|
|
// This is only ever `true` if undo was triggered via the context menu
|
|
|
|
// (not ctrl/cmd+z)
|
|
|
|
if (editFlags.canRedo) {
|
2022-09-27 21:01:06 +00:00
|
|
|
template.push({ label: i18n('editMenuRedo'), role: 'redo' });
|
2020-03-20 21:00:11 +00:00
|
|
|
}
|
|
|
|
if (editFlags.canUndo || editFlags.canRedo) {
|
|
|
|
template.push({ type: 'separator' });
|
|
|
|
}
|
|
|
|
if (editFlags.canCut) {
|
2022-09-27 21:01:06 +00:00
|
|
|
template.push({ label: i18n('editMenuCut'), role: 'cut' });
|
2020-03-20 21:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:47:46 +00:00
|
|
|
if (editFlags.canCopy || isLink || isImage) {
|
|
|
|
let click;
|
|
|
|
let label;
|
|
|
|
|
|
|
|
if (isLink) {
|
|
|
|
click = () => {
|
|
|
|
clipboard.writeText(params.linkURL);
|
|
|
|
};
|
2022-09-27 21:01:06 +00:00
|
|
|
label = i18n('contextMenuCopyLink');
|
2020-11-03 00:47:46 +00:00
|
|
|
} else if (isImage) {
|
|
|
|
click = () => {
|
2021-05-13 17:18:51 +00:00
|
|
|
const parsedSrcUrl = maybeParseUrl(params.srcURL);
|
|
|
|
if (!parsedSrcUrl || parsedSrcUrl.protocol !== 'file:') {
|
2020-11-03 00:47:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const image = nativeImage.createFromPath(
|
2021-06-18 17:04:27 +00:00
|
|
|
fileURLToPath(params.srcURL)
|
2020-11-03 00:47:46 +00:00
|
|
|
);
|
|
|
|
clipboard.writeImage(image);
|
|
|
|
};
|
2022-09-27 21:01:06 +00:00
|
|
|
label = i18n('contextMenuCopyImage');
|
2020-11-03 00:47:46 +00:00
|
|
|
} else {
|
2022-09-27 21:01:06 +00:00
|
|
|
label = i18n('editMenuCopy');
|
2020-11-03 00:47:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 21:00:11 +00:00
|
|
|
template.push({
|
2020-11-03 00:47:46 +00:00
|
|
|
label,
|
|
|
|
role: isLink || isImage ? undefined : 'copy',
|
|
|
|
click,
|
2020-03-20 21:00:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:47:46 +00:00
|
|
|
if (editFlags.canPaste && !isImage) {
|
2022-09-27 21:01:06 +00:00
|
|
|
template.push({ label: i18n('editMenuPaste'), role: 'paste' });
|
2020-03-20 21:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:47:46 +00:00
|
|
|
if (editFlags.canPaste && !isImage) {
|
2020-03-20 21:00:11 +00:00
|
|
|
template.push({
|
2022-09-27 21:01:06 +00:00
|
|
|
label: i18n('editMenuPasteAndMatchStyle'),
|
2020-03-20 21:00:11 +00:00
|
|
|
role: 'pasteAndMatchStyle',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only enable select all in editors because select all in non-editors
|
|
|
|
// results in all the UI being selected
|
|
|
|
if (editFlags.canSelectAll && params.isEditable) {
|
|
|
|
template.push({
|
2022-09-27 21:01:06 +00:00
|
|
|
label: i18n('editMenuSelectAll'),
|
2021-06-18 17:04:27 +00:00
|
|
|
role: 'selectAll',
|
2020-03-20 21:00:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const menu = Menu.buildFromTemplate(template);
|
2021-06-18 17:04:27 +00:00
|
|
|
menu.popup({
|
|
|
|
window: browserWindow,
|
|
|
|
});
|
2020-03-20 21:00:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|