Skip to content

Commit

Permalink
Hot key to trigger auto complete (#93)
Browse files Browse the repository at this point in the history
* Hot key to trigger auto complete

* Update help message.
  • Loading branch information
bartekplus authored Apr 6, 2024
1 parent 4b2ccdf commit 7d1ed7e
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 22 deletions.
6 changes: 6 additions & 0 deletions platform/chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@
"mac": "Command+Shift+F"
},
"description": "Toggle on/off FluentTyper on active tab"
},
"trigger-ft-active-tab": {
"suggested_key": {
"default": "Ctrl+Period"
},
"description": "Trigger FluentTyper on active tab"
}
},
"offline_enabled": true,
Expand Down
6 changes: 6 additions & 0 deletions platform/edge/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
"mac": "Command+Shift+F"
},
"description": "Toggle on/off FluentTyper on active tab"
},
"trigger-ft-active-tab": {
"suggested_key": {
"default": "Ctrl+Period"
},
"description": "Trigger FluentTyper on active tab"
}
},
"offline_enabled": true,
Expand Down
6 changes: 6 additions & 0 deletions platform/firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
"mac": "Command+Shift+F"
},
"description": "Toggle on/off FluentTyper on active tab"
},
"trigger-ft-active-tab": {
"suggested_key": {
"default": "Ctrl+Period"
},
"description": "Trigger FluentTyper on active tab"
}
},
"browser_specific_settings": {
Expand Down
16 changes: 13 additions & 3 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class BackgroundServiceWorker {
/**
* Toggles the content script on or off for the active tab.
*/
toggleOnOffActiveTab() {
sendCommandToActiveTabContentScript(command) {
// Query for the active tab in the current window.
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
// Check for any error that occurred during the query.
Expand All @@ -120,7 +120,7 @@ class BackgroundServiceWorker {
const currentTab = tabs[0];

const message = {
command: "backgroundPageToggle",
command: command,
context: {},
};

Expand Down Expand Up @@ -155,6 +155,9 @@ class BackgroundServiceWorker {
autocompleteSeparatorSource: language
? LANG_SEPERATOR_CHARS_REGEX[language].source // Retrieve the separator character regex pattern based on the language setting value
: DEFAULT_SEPERATOR_CHARS_REGEX.source, // Use the default pattern if the language setting value is undefined or null
minWordLengthToPredict: await backgroundServiceWorker.settings.get(
"minWordLengthToPredict"
),
},
};

Expand Down Expand Up @@ -257,9 +260,16 @@ function onCommand(command) {
switch (command) {
case "toggle-ft-active-tab":
// Call the toggleOnOffActiveTab method on the background service worker.
backgroundServiceWorker.toggleOnOffActiveTab();
backgroundServiceWorker.sendCommandToActiveTabContentScript(
"toggle-ft-active-tab"
);
break;

case "trigger-ft-active-tab":
backgroundServiceWorker.sendCommandToActiveTabContentScript(
"trigger-ft-active-tab"
);
break;
default:
// Log an error message if the command is unknown.
console.error("Unknown command: ", command);
Expand Down
30 changes: 17 additions & 13 deletions src/cs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
this._autocompleteSeparatorSource = this.autocompleteSeparator.source;
// Node for observing DOM changes
this.observerNode = document.body || document.documentElement;
// Active element - last element that received key input
this.activeHelperArrId = null;
// Minimum characters typed by user to start prediction
this.minWordLengthToPredict = 0;

// Add message listener for handling plugin messages
chrome.runtime.onMessage.addListener(this.messageHandler.bind(this));
Expand Down Expand Up @@ -437,7 +441,7 @@
// when the spacebar is hit, select the current match
spaceSelectsMatch: this.autocomplete,
// turn tribute into an autocomplete
autocompleteMode: true,
autocompleteMode: this.minWordLengthToPredict !== Number.MAX_VALUE,
autocompleteSeparator: this.autocompleteSeparator,
// Customize the elements used to wrap matched strings within the results list
// defaults to <span></span> if undefined
Expand All @@ -447,7 +451,7 @@
skip: true, // true will skip local search, useful if doing server-side search
},
// specify the minimum number of characters that must be typed before menu appears
menuShowMinLength: 0,
menuShowMinLength: this.minWordLengthToPredict,
keys: tribueKeyFn,
supportRevert: true,
selectByDigit: this.selectByDigit,
Expand Down Expand Up @@ -545,16 +549,9 @@
* @param {string} helperArrId - The ID of the Tribute element's helper array to handle key down events for.
* @param {KeyboardEvent} event - The key down event to handle.
*/
elementKeyDownEventHandler(helperArrId, event) {
elementKeyDownEventHandler(helperArrId) {
this.activeHelperArrId = helperArrId;
// If the event is a spacebar press with the control key held down, trigger the Tribute.js menu for the Tribute element with the given helper array ID.
if (
event &&
event.code === "Space" &&
event.getModifierState &&
event.getModifierState("Control")
) {
this.triggerTribute(helperArrId);
}
}

/**
Expand Down Expand Up @@ -602,7 +599,9 @@
this.lang = config.lang;
// Set the selectByDigit option
this.selectByDigit = config.selectByDigit;

// Minimum characters typed by user to start prediction
this.minWordLengthToPredict =
config.minWordLengthToPredict === -1 ? Number.MAX_VALUE : 0;
// Force restart to reload config
this.enabled = false;
this.enabled = config.enabled;
Expand Down Expand Up @@ -715,12 +714,17 @@
// Send a status message to the sender
sendStatusMsg = true;
break;
case "backgroundPageToggle":
case "toggle-ft-active-tab":
// Toggle TributeJS enable/disable state
this.enabled = !this.enabled;
// Send a status message to the sender
sendStatusMsg = true;
break;
case "trigger-ft-active-tab":
this.triggerTribute(this.activeHelperArrId);
// Send a status message to the sender
sendStatusMsg = true;
break;
default:
// Unknown message type, log it to the console
console.log("Unknown message:");
Expand Down
4 changes: 2 additions & 2 deletions src/presageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ class PresageHandler {
dateFormat
) {
this.numSuggestions = numSuggestions;
this.minWordLengthToPredict = minWordLengthToPredict;
this.predictNextWordAfterSeparatorChar = minWordLengthToPredict === 0;
this.minWordLengthToPredict = Math.max(0, minWordLengthToPredict);
this.predictNextWordAfterSeparatorChar = this.minWordLengthToPredict === 0;
this.insertSpaceAfterAutocomplete = insertSpaceAfterAutocomplete;
this.autoCapitalize = autoCapitalize;
this.applySpacingRules = applySpacingRules;
Expand Down
3 changes: 1 addition & 2 deletions src/third_party/fancier-settings/js/classes/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ class Events {
type = this.removeOn(type);
const events = this.events[type];
if (!events) return this;
args = Array(args);
events.forEach((fn) => {
fn.apply(this, args);
fn.apply(this, [args]);
});
return this;
}
Expand Down
6 changes: 4 additions & 2 deletions src/third_party/fancier-settings/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ const manifest = {
group: i18n.get("General"),
name: "minWordLengthToPredict",
type: "slider",
min: 0,
min: -1,
max: 12,
display: true,
label:
"This setting determines the minimum word length required to trigger the prediction feature. If set to '0', predictions will appear after a whitespace:",
"This setting determines the minimum word length required to trigger the prediction feature.<br>" +
"If set to '0', predictions will appear after a whitespace.<br>If set to '-1', predictions must be triggered manually using a key shortcut.<br>" +
"Default shortcut: Ctrl+Period. To change, consult your browser's help section (<a href='https://support.mozilla.org/en-US/kb/manage-extension-shortcuts-firefox'>Firefox</a>, <a href='chrome://extensions/shortcuts'>Chrome</a>). Please note that some key combinations may not work.",
default: 1,
},
{
Expand Down

0 comments on commit 7d1ed7e

Please sign in to comment.