Add support for frontend hotkeys; add scanSelectedText action (#1266)

* Add support for frontend hotkeys; add scanSelectedText action

* Remove unused global

* Remove duplicate hotkey handler script
This commit is contained in:
toasted-nutbread 2021-01-17 19:28:42 -05:00 committed by GitHub
parent de5d3ec3ad
commit c875ca728f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 61 additions and 7 deletions

View File

@ -47,6 +47,7 @@
"mixed/js/frame-client.js",
"mixed/js/text-scanner.js",
"mixed/js/document-util.js",
"mixed/js/hotkey-handler.js",
"fg/js/dom-text-scanner.js",
"fg/js/popup.js",
"fg/js/text-source-range.js",

View File

@ -1011,7 +1011,7 @@
"type": "array",
"items": {
"type": "string",
"enum": ["popup", "search"],
"enum": ["popup", "search", "web"],
"default": "popup"
},
"default": ["popup", "search"]

View File

@ -16,6 +16,7 @@
*/
/* global
* HotkeyHandler
* PopupFactory
* PopupPreviewFrame
* api
@ -27,10 +28,13 @@
const {frameId} = await api.frameInformationGet();
const hotkeyHandler = new HotkeyHandler();
hotkeyHandler.prepare();
const popupFactory = new PopupFactory(frameId);
popupFactory.prepare();
const preview = new PopupPreviewFrame(frameId, popupFactory);
const preview = new PopupPreviewFrame(frameId, popupFactory, hotkeyHandler);
await preview.prepare();
document.documentElement.dataset.loaded = 'true';

View File

@ -23,9 +23,10 @@
*/
class PopupPreviewFrame {
constructor(frameId, popupFactory) {
constructor(frameId, popupFactory, hotkeyHandler) {
this._frameId = frameId;
this._popupFactory = popupFactory;
this._hotkeyHandler = hotkeyHandler;
this._frontend = null;
this._apiOptionsGetOld = null;
this._popupShown = false;
@ -74,7 +75,8 @@ class PopupPreviewFrame {
useProxyPopup: false,
pageType: 'web',
allowRootFramePopupProxy: false,
childrenSupported: false
childrenSupported: false,
hotkeyHandler: this._hotkeyHandler
});
this._frontend.setOptionsContextOverride(this._optionsContext);
await this._frontend.prepare();

View File

@ -45,6 +45,7 @@
<script src="/mixed/js/text-scanner.js"></script>
<script src="/mixed/js/document-util.js"></script>
<script src="/mixed/js/hotkey-handler.js"></script>
<script src="/fg/js/dom-text-scanner.js"></script>
<script src="/fg/js/popup.js"></script>
<script src="/fg/js/text-source-range.js"></script>

View File

@ -3014,10 +3014,15 @@
<option value="viewNote" data-scopes="popup search">View note</option>
<option value="playAudio" data-scopes="popup search">Play audio</option>
<option value="copyHostSelection" data-scopes="popup search">Copy selection</option>
<option value="scanSelectedText" data-scopes="web">Scan selected text</option>
</select>
<div class="hotkey-list-item-flex-row">
<div class="hotkey-list-item-flex-row-label">Scopes:</div>
<div class="hotkey-list-item-flex-row">
<label class="hotkey-scope-checkbox-container" data-scope="web">
<label class="checkbox"><input type="checkbox" class="hotkey-scope-checkbox" data-scope="web"><span class="checkbox-body"><span class="checkbox-fill"></span><span class="checkbox-border"></span><span class="checkbox-check"></span></span></label>
<span>Web</span>
</label>
<label class="hotkey-scope-checkbox-container" data-scope="popup">
<label class="checkbox"><input type="checkbox" class="hotkey-scope-checkbox" data-scope="popup"><span class="checkbox-body"><span class="checkbox-fill"></span><span class="checkbox-border"></span><span class="checkbox-check"></span></span></label>
<span>Popup</span>

View File

@ -17,6 +17,7 @@
/* global
* Frontend
* HotkeyHandler
* PopupFactory
* api
*/
@ -31,6 +32,9 @@
throw new Error('Failed to get frameId');
}
const hotkeyHandler = new HotkeyHandler();
hotkeyHandler.prepare();
const popupFactory = new PopupFactory(frameId);
popupFactory.prepare();
@ -42,7 +46,8 @@
parentFrameId: null,
useProxyPopup: false,
pageType: 'web',
allowRootFramePopupProxy: true
allowRootFramePopupProxy: true,
hotkeyHandler
});
await frontend.prepare();

View File

@ -19,6 +19,7 @@
* DocumentUtil
* TextScanner
* TextSourceElement
* TextSourceRange
* api
*/
@ -32,7 +33,8 @@ class Frontend {
parentFrameId,
useProxyPopup,
allowRootFramePopupProxy,
childrenSupported=true
childrenSupported=true,
hotkeyHandler
}) {
this._pageType = pageType;
this._popupFactory = popupFactory;
@ -43,6 +45,7 @@ class Frontend {
this._useProxyPopup = useProxyPopup;
this._allowRootFramePopupProxy = allowRootFramePopupProxy;
this._childrenSupported = childrenSupported;
this._hotkeyHandler = hotkeyHandler;
this._popup = null;
this._disabledOverride = false;
this._options = null;
@ -71,6 +74,10 @@ class Frontend {
['setAllVisibleOverride', {async: true, handler: this._onApiSetAllVisibleOverride.bind(this)}],
['clearAllVisibleOverride', {async: true, handler: this._onApiClearAllVisibleOverride.bind(this)}]
]);
this._hotkeyHandler.registerActions([
['scanSelectedText', this._onActionScanSelectedText.bind(this)]
]);
}
get canClearSelection() {
@ -161,6 +168,12 @@ class Frontend {
this._signalFrontendReady(frameId);
}
// Action handlers
_onActionScanSelectedText() {
this._scanSelectedText();
}
// API message handlers
_onApiGetUrl() {
@ -319,6 +332,8 @@ class Frontend {
const {scanning: scanningOptions, sentenceParsing: sentenceParsingOptions} = options;
this._options = options;
this._hotkeyHandler.setHotkeys('web', options.inputs.hotkeys);
await this._updatePopup();
const preventMiddleMouse = this._getPreventMiddleMouseValueForPageType(scanningOptions.preventMiddleMouse);
@ -646,4 +661,23 @@ class Frontend {
detail: {documentTitle}
};
}
async _scanSelectedText() {
const range = this._getFirstNonEmptySelectionRange();
if (range === null) { return false; }
const source = new TextSourceRange(range, range.toString(), null, null);
await this._textScanner.search(source, {focus: true});
return true;
}
_getFirstNonEmptySelectionRange() {
const selection = window.getSelection();
for (let i = 0, ii = selection.rangeCount; i < ii; ++i) {
const range = selection.getRangeAt(i);
if (range.toString().length > 0) {
return range;
}
}
return null;
}
}

View File

@ -46,6 +46,7 @@
"mixed/js/frame-client.js",
"mixed/js/text-scanner.js",
"mixed/js/document-util.js",
"mixed/js/hotkey-handler.js",
"fg/js/dom-text-scanner.js",
"fg/js/popup.js",
"fg/js/text-source-range.js",

View File

@ -1640,7 +1640,8 @@ class Display extends EventDispatcher {
popupFactory,
pageType: this._pageType,
allowRootFramePopupProxy: true,
childrenSupported: this._childrenSupported
childrenSupported: this._childrenSupported,
hotkeyHandler: this._hotkeyHandler
});
const frontend = new Frontend(setupNestedPopupsOptions);