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

Minor bug fixes and performance improvements #36

Merged
merged 2 commits into from
May 5, 2021
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
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"root": true,
"parserOptions": {
"ecmaVersion": 2017,
"ecmaVersion": 12,
"sourceType": "module",
"ecmaFeatures": {
"impliedStrict": true
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

<img height="200" width="200" src="assets/header.svg">

This is a (Firefox and Thunderbird) add-on (WebExtension) that allows you to autocorrect common text sequences and convert text characters to a look like a special font.
This is a (Firefox, Chromium/Chrome and Thunderbird) add-on (WebExtension) that allows you to autocorrect common text sequences and convert text characters to a look like a special font.
For instance, it converts quotes like `"these"` to `“these”`, which are typographically correct.

Additionally, you can convert text into more than 20 different font styles and casing changes.
You can enable and disable any features in the options and adjust more settings regarding the behavior of the add-on.

This extension only works with modern Firefox and Thunderbird v78 or higher.
This extension works with modern Firefox v78 or higher, Chromium/Chrome and Thunderbird v78 or higher.

## Download

Expand Down
6 changes: 3 additions & 3 deletions src/background/modules/AutocorrectHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ function applySettings() {
antipatterns = antipatterns.filter((item, pos) => antipatterns.indexOf(item) === pos);
console.log("Do not autocorrect for these patterns", antipatterns);

antipatterns.forEach((symbol, index) => {
for (const [index, symbol] of antipatterns.entries()) {
antipatterns[index] = symbol.replace(regExSpecialChars, "\\$&");
});
}

symbolpatterns = new RegExp(`(${symbolpatterns.join("|")})$`);
antipatterns = new RegExp(`(${antipatterns.join("|")})$`);
Expand Down Expand Up @@ -130,7 +130,7 @@ function sendSettings(autocorrect) {

browser.tabs.query({}).then((tabs) => {
for (const tab of tabs) {
// This is currently not supported in Thunderbird: https://bugzilla.mozilla.org/show_bug.cgi?id=1641576
// This requires Thunderbird 78.4: https://bugzilla.mozilla.org/show_bug.cgi?id=1641576
browser.tabs.sendMessage(
tab.id,
{
Expand Down
25 changes: 11 additions & 14 deletions src/background/modules/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { COMMUNICATION_MESSAGE_TYPE } from "/common/modules/data/BrowserCommunic
import { menuStructure, SEPARATOR_ID, TRANSFORMATION_TYPE } from "/common/modules/data/Fonts.js";

const menus = browser.menus || browser.contextMenus; // fallback for Thunderbird
const PREVIEW_STRING_CUT_LENGTH = 1000; // a setting that may improve performance by not calculating invisible parts of the context menu
const PREVIEW_STRING_CUT_LENGTH = 100; // a setting that may improve performance by not calculating invisible parts of the context menu

let lastCachedUnicodeFontSettings = null;

Expand All @@ -29,8 +29,7 @@ function handleMenuChoosen(info, tab) {
}

text = text.normalize();
const menuItem = info.menuItemId;
const output = UnicodeTransformationHandler.transformText(text, menuItem);
const output = UnicodeTransformationHandler.transformText(text, info.menuItemId);

browser.tabs.executeScript(tab.id, {
code: `insertIntoPage("${output}");`,
Expand Down Expand Up @@ -68,17 +67,16 @@ async function handleMenuShown(info) {
const menuIsShown = info.menuIds.length > 0;
if (!lastCachedUnicodeFontSettings.livePreview) {
if (menuIsShown) {
return Promise.resolve();
return;
}

// continue re-creating deleted menu, but without any example text
text = null;
}

buildMenu(lastCachedUnicodeFontSettings, text, menuIsShown);
await buildMenu(lastCachedUnicodeFontSettings, text, menuIsShown);

menus.refresh();
return Promise.resolve();
}

/**
Expand All @@ -89,21 +87,21 @@ async function handleMenuShown(info) {
* @param {bool?} [refreshMenu=false]
* @returns {void}
*/
function buildMenu(unicodeFontSettings, exampleText = null, refreshMenu = false) {
async function buildMenu(unicodeFontSettings, exampleText = null, refreshMenu = false) {
if (unicodeFontSettings.changeFont) {
addMenuItems(menuStructure[TRANSFORMATION_TYPE.FONT], unicodeFontSettings, exampleText, refreshMenu);
await addMenuItems(menuStructure[TRANSFORMATION_TYPE.FONT], unicodeFontSettings, exampleText, refreshMenu);
}
if (unicodeFontSettings.changeFont &&
unicodeFontSettings.changeCase &&
!refreshMenu) {
menus.create({
await menus.create({
id: "seperator-case-font",
type: "separator",
contexts: ["editable"]
});
}
if (unicodeFontSettings.changeCase) {
addMenuItems(menuStructure[TRANSFORMATION_TYPE.CASING], unicodeFontSettings, exampleText, refreshMenu);
await addMenuItems(menuStructure[TRANSFORMATION_TYPE.CASING], unicodeFontSettings, exampleText, refreshMenu);
}
}

Expand All @@ -116,14 +114,14 @@ function buildMenu(unicodeFontSettings, exampleText = null, refreshMenu = false)
* @param {bool?} [refreshMenu=false]
* @returns {void}
*/
function addMenuItems(menuItems, unicodeFontSettings = lastCachedUnicodeFontSettings, exampleText = null, refreshMenu = false) {
async function addMenuItems(menuItems, unicodeFontSettings = lastCachedUnicodeFontSettings, exampleText = null, refreshMenu = false) {
for (const transformationId of menuItems) {
if (transformationId === SEPARATOR_ID) {
if (refreshMenu) {
continue;
}

menus.create({
await menus.create({
// id: id,
type: "separator",
contexts: ["editable"]
Expand All @@ -146,10 +144,9 @@ function addMenuItems(menuItems, unicodeFontSettings = lastCachedUnicodeFontSett
if (refreshMenu) {
menus.update(transformationId, {
"title": menuText,
"contexts": ["editable"],
});
} else {
menus.create({
await menus.create({
"id": transformationId,
"title": menuText,
"contexts": ["editable"],
Expand Down
2 changes: 1 addition & 1 deletion src/common/modules/UnicodeTransformationHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function getTransformationType(transformationId) {
function capitalizeEachWord(text) {
// Regular expression Unicode property escapes and lookbehind assertions require Firefox/Thunderbird 78
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#bcd:javascript.builtins.RegExp
return text.replace(/(?<=^|\P{Alpha})\p{Alpha}\S*/gu, ([h, ...t]) => h.toLocaleUpperCase() + t.join(''));
return text.replace(/(?<=^|\P{Alpha})\p{Alpha}\S*/gu, ([h, ...t]) => h.toLocaleUpperCase() + t.join(""));
}

/**
Expand Down
16 changes: 11 additions & 5 deletions src/content_scripts/autocorrect.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ function getCaretPosition(target) {
if (target.isContentEditable || IS_THUNDERBIRD) {
target.focus();
const _range = document.getSelection().getRangeAt(0);
if (!_range.collapsed) {
return null;
}
const range = _range.cloneRange();
const temp = document.createTextNode("\0");
range.insertNode(temp);
Expand All @@ -75,6 +78,9 @@ function getCaretPosition(target) {
}
// input and textarea fields
else {
if (target.selectionStart !== target.selectionEnd) {
return null;
}
return target.selectionStart;
}
}
Expand All @@ -89,9 +95,9 @@ function getCaretPosition(target) {
* @returns {void}
*/
function insertAtCaret(target, atext) {
const isSuccess = document.execCommand("insertText", false, atext);

if(isSuccess) {
// document.execCommand is deprecated, although there is not yet an alternative: https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
// insertReplacementText
if(document.execCommand("insertText", false, atext)) {
return;
}

Expand Down Expand Up @@ -157,8 +163,8 @@ function countChars(str) {
function deleteCaret(target, atext) {
const count = countChars(atext);
if (count > 0) {
const isSuccess = document.execCommand("delete", false);
if (isSuccess) {
// document.execCommand is deprecated, although there is not yet an alternative: https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
if (document.execCommand("delete", false)) {
rugk marked this conversation as resolved.
Show resolved Hide resolved
for (let i = 0; i < count - 1; ++i) {
document.execCommand("delete", false);
}
Expand Down