Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context menu interface for Yomitan #1028

Merged
merged 12 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dev/data/manifest-variants.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"unlimitedStorage",
"declarativeNetRequest",
"scripting",
"offscreen"
"offscreen",
"contextMenus"
],
"optional_permissions": [
"clipboardRead",
Expand Down
5 changes: 5 additions & 0 deletions ext/data/schemas/options-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"popupScaleRelativeToPageZoom",
"popupScaleRelativeToVisualViewport",
"showGuide",
"enableContextMenuScanSelected",
"compactTags",
"glossaryLayoutMode",
"mainDictionary",
Expand Down Expand Up @@ -206,6 +207,10 @@
"type": "boolean",
"default": true
},
"enableContextMenuScanSelected": {
"type": "boolean",
"default": true
},
"compactTags": {
"type": "boolean",
"default": false
Expand Down
13 changes: 11 additions & 2 deletions ext/js/app/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class Frontend {
['frontendRequestReadyBroadcast', this._onMessageRequestFrontendReadyBroadcast.bind(this)],
['frontendSetAllVisibleOverride', this._onApiSetAllVisibleOverride.bind(this)],
['frontendClearAllVisibleOverride', this._onApiClearAllVisibleOverride.bind(this)],
['frontendScanSelectedText', this._onApiScanSelectedText.bind(this)],
]);

this._hotkeyHandler.registerActions([
Expand Down Expand Up @@ -260,6 +261,13 @@ export class Frontend {
void this._scanSelectedText(false, true);
}

/**
* @returns {void}
*/
_onApiScanSelectedText() {
void this._scanSelectedText(false, true, true);
}

/**
* @returns {void}
*/
Expand Down Expand Up @@ -934,13 +942,14 @@ export class Frontend {
/**
* @param {boolean} allowEmptyRange
* @param {boolean} disallowExpandSelection
* @param {boolean} showEmpty show empty popup if no results are found
* @returns {Promise<boolean>}
*/
async _scanSelectedText(allowEmptyRange, disallowExpandSelection) {
async _scanSelectedText(allowEmptyRange, disallowExpandSelection, showEmpty = false) {
const range = this._getFirstSelectionRange(allowEmptyRange);
if (range === null) { return false; }
const source = disallowExpandSelection ? TextSourceRange.createLazy(range) : TextSourceRange.create(range);
await this._textScanner.search(source, {focus: true, restoreSelection: true});
await this._textScanner.search(source, {focus: true, restoreSelection: true}, showEmpty);
return true;
}

Expand Down
15 changes: 15 additions & 0 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,21 @@ export class Backend {
this._clipboardMonitor.stop();
}

if (options.general.enableContextMenuScanSelected) {
chrome.contextMenus.create({
id: 'yomitan_lookup',
title: 'Lookup in Yomitan',
contexts: ['selection'],
});
chrome.contextMenus.onClicked.addListener((info) => {
if (info.selectionText) {
this._sendMessageAllTabsIgnoreResponse({action: 'frontendScanSelectedText'});
}
});
} else {
chrome.contextMenus.remove('yomitan_lookup', () => this._checkLastError(chrome.runtime.lastError));
}

void this._accessibilityController.update(this._getOptionsFull(false));

this._sendMessageAllTabsIgnoreResponse({action: 'applicationOptionsUpdated', params: {source}});
Expand Down
11 changes: 11 additions & 0 deletions ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ export class OptionsUtil {
this._updateVersion36,
this._updateVersion37,
this._updateVersion38,
this._updateVersion39,
];
/* eslint-enable @typescript-eslint/unbound-method */
if (typeof targetVersion === 'number' && targetVersion < result.length) {
Expand Down Expand Up @@ -1313,6 +1314,16 @@ export class OptionsUtil {
await this._applyAnkiFieldTemplatesPatch(options, '/data/templates/anki-field-templates-upgrade-v38.handlebars');
}

/**
* - Add new setting enableContextMenuScanSelected
* @type {import('options-util').UpdateFunction}
*/
async _updateVersion39(options) {
for (const profile of options.profiles) {
profile.options.general.enableContextMenuScanSelected = true;
}
}

/**
* @param {string} url
* @returns {Promise<chrome.tabs.Tab>}
Expand Down
11 changes: 7 additions & 4 deletions ext/js/language/text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,11 @@ export class TextScanner extends EventDispatcher {
/**
* @param {import('text-source').TextSource} textSource
* @param {import('text-scanner').InputInfoDetail} [inputDetail]
* @param {boolean} showEmpty
*/
async search(textSource, inputDetail) {
async search(textSource, inputDetail, showEmpty = false) {
const inputInfo = this._createInputInfo(null, 'script', 'script', true, [], [], inputDetail);
await this._search(textSource, this._searchTerms, this._searchKanji, inputInfo);
await this._search(textSource, this._searchTerms, this._searchKanji, inputInfo, showEmpty);
}

// Private
Expand All @@ -437,8 +438,9 @@ export class TextScanner extends EventDispatcher {
* @param {boolean} searchTerms
* @param {boolean} searchKanji
* @param {import('text-scanner').InputInfo} inputInfo
* @param {boolean} showEmpty shows a "No results found" popup if no results are found
*/
async _search(textSource, searchTerms, searchKanji, inputInfo) {
async _search(textSource, searchTerms, searchKanji, inputInfo, showEmpty = false) {
try {
const inputInfoDetail = inputInfo.detail;
const selectionRestoreInfo = (
Expand All @@ -465,7 +467,8 @@ export class TextScanner extends EventDispatcher {
const result = await this._findDictionaryEntries(textSource, searchTerms, searchKanji, optionsContext);
if (result !== null) {
({dictionaryEntries, sentence, type} = result);
} else if (textSource !== null && textSource instanceof TextSourceElement && await this._isTextLookupWorthy(textSource.fullContent)) {
} else if (showEmpty || (textSource !== null && textSource instanceof TextSourceElement && await this._isTextLookupWorthy(textSource.fullContent))) {
// Shows a "No results found" message
dictionaryEntries = [];
sentence = {text: '', offset: 0};
}
Expand Down
8 changes: 8 additions & 0 deletions ext/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ <h1>Yomitan Settings</h1>
<label class="toggle"><input type="checkbox" data-setting="general.showGuide"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label>
</div>
</div></div>
<div class="settings-item advanced-only"><div class="settings-item-inner">
<div class="settings-item-left">
<div class="settings-item-label">Show "Lookup in Yomitan" in right-click menu</div>
</div>
<div class="settings-item-right">
<label class="toggle"><input type="checkbox" data-setting="general.enableContextMenuScanSelected"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label>
</div>
</div></div>
<div class="settings-item advanced-only"><div class="settings-item-inner settings-item-inner-wrappable">
<div class="settings-item-left">
<div class="settings-item-label">Maximum number of results</div>
Expand Down
3 changes: 2 additions & 1 deletion test/options-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ function createProfileOptionsUpdatedTestData1() {
popupScaleRelativeToPageZoom: false,
popupScaleRelativeToVisualViewport: true,
showGuide: true,
enableContextMenuScanSelected: true,
compactTags: false,
glossaryLayoutMode: 'default',
mainDictionary: '',
Expand Down Expand Up @@ -605,7 +606,7 @@ function createOptionsUpdatedTestData1() {
},
],
profileCurrent: 0,
version: 38,
version: 39,
global: {
database: {
prefixWildcardsSupported: false,
Expand Down
4 changes: 4 additions & 0 deletions types/ext/application.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export type ApiSurface = {
};
return: void;
};
frontendScanSelectedText: {
params: void;
return: void;
};
frameEndpointReady: {
params: FrameEndpointReadyDetails;
return: void;
Expand Down
1 change: 1 addition & 0 deletions types/ext/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export type GeneralOptions = {
popupScaleRelativeToPageZoom: boolean;
popupScaleRelativeToVisualViewport: boolean;
showGuide: boolean;
enableContextMenuScanSelected: boolean;
compactTags: boolean;
glossaryLayoutMode: GlossaryLayoutMode;
mainDictionary: string;
Expand Down