Skip to content
Open
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 scripts/manifests/chromemanifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "__MSG_extensionName__",
"short_name": "__MSG_extensionNameShort__",
"version": "0.8",
"version": "0.9",
"author": "Teal Dulcet, rugk",

"description": "__MSG_extensionDescription__",
Expand Down
2 changes: 1 addition & 1 deletion scripts/manifests/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Unicodify DEV VERSION",
"short_name": "__MSG_extensionNameShort__",
"version": "0.8",
"version": "0.9",
"author": "Teal Dulcet, rugk",

"description": "__MSG_extensionDescription__",
Expand Down
2 changes: 1 addition & 1 deletion scripts/manifests/firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "__MSG_extensionName__",
"short_name": "__MSG_extensionNameShort__",
"version": "0.8",
"version": "0.9",
"author": "Teal Dulcet, rugk",

"description": "__MSG_extensionDescription__",
Expand Down
2 changes: 1 addition & 1 deletion scripts/manifests/thunderbirdmanifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "__MSG_extensionName__",
"short_name": "__MSG_extensionNameShort__",
"version": "0.8",
"version": "0.9",
"author": "Teal Dulcet, rugk",

"description": "__MSG_extensionDescription__",
Expand Down
71 changes: 30 additions & 41 deletions src/background/modules/AutocorrectHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,13 @@ function applySettings() {
}

// Longest autocorrection
longest = 0;

for (const symbol in autocorrections) {
if (symbol.length > longest) {
longest = symbol.length;
}
}
longest = Math.max(...Object.keys(autocorrections).map((s) => s.length), 0);
console.log("Longest autocorrection", longest);

symbolpatterns = createTree(Object.keys(autocorrections));
const symbolpatternsRegexpString = createTree(Object.keys(autocorrections));

// Do not autocorrect for these patterns
antipatterns = [];
let antipatternsList = [];
for (const x in autocorrections) {
let length = 0;
let index = x.length;
Expand All @@ -152,31 +146,21 @@ function applySettings() {
if (length) {
length = x.length - (index + length);
if (length > 1) {
antipatterns.push(x.slice(0, -(length - 1)));
antipatternsList.push(x.slice(0, -(length - 1)));
}
}
}
antipatterns = antipatterns.filter((item, pos) => antipatterns.indexOf(item) === pos);
console.log("Do not autocorrect for these patterns", antipatterns);
antipatternsList = antipatternsList.filter((item, pos) => antipatternsList.indexOf(item) === pos);
console.log("Do not autocorrect for these patterns", antipatternsList);

antipatterns = createTree(antipatterns);
const antipatternsRegexpString = createTree(antipatternsList);

symbolpatterns = new RegExp(`(${symbolpatterns})$`, "u");
antipatterns = new RegExp(`(${antipatterns})$`, "u");
symbolpatterns = new RegExp(`(${symbolpatternsRegexpString})$`, "u");
antipatterns = new RegExp(`(${antipatternsRegexpString})$`, "u");
const end = performance.now();
console.log(`The new autocorrect settings were applied in ${end - start} ms.`);
}

/**
* On error.
*
* @param {string} error
* @returns {void}
*/
function onError(error) {
console.error(`Error: ${error}`);
}

/**
* Set autocorrect settings.
*
Expand Down Expand Up @@ -206,7 +190,6 @@ function sendSettings(autocorrect) {

browser.tabs.query({}).then((tabs) => {
for (const tab of tabs) {
// This requires Thunderbird 78.4: https://bugzilla.mozilla.org/show_bug.cgi?id=1641576
browser.tabs.sendMessage(
tab.id,
{
Expand All @@ -220,9 +203,13 @@ function sendSettings(autocorrect) {
symbolpatterns: IS_CHROME ? symbolpatterns.source : symbolpatterns,
antipatterns: IS_CHROME ? antipatterns.source : antipatterns
}
).catch(onError);
).catch((error) => {
console.error(`Error: ${error}`);
});
}
}).catch(onError);
}).catch((error) => {
console.error(`Error: ${error}`);
});
}

/**
Expand Down Expand Up @@ -256,18 +243,20 @@ BrowserCommunication.addListener(COMMUNICATION_MESSAGE_TYPE.AUTOCORRECT_BACKGROU

browser.runtime.onMessage.addListener((message) => {
// console.log(message);
if (message.type === COMMUNICATION_MESSAGE_TYPE.AUTOCORRECT_CONTENT) {
const response = {
type: COMMUNICATION_MESSAGE_TYPE.AUTOCORRECT_CONTENT,
enabled: settings.enabled,
quotes: settings.quotes,
fracts: settings.fracts,
numbers: settings.numbers,
autocorrections,
longest,
symbolpatterns: IS_CHROME ? symbolpatterns.source : symbolpatterns,
antipatterns: IS_CHROME ? antipatterns.source : antipatterns
};
return Promise.resolve(response);
if (message.type !== COMMUNICATION_MESSAGE_TYPE.AUTOCORRECT_CONTENT) {
return;
}

const response = {
type: COMMUNICATION_MESSAGE_TYPE.AUTOCORRECT_CONTENT,
enabled: settings.enabled,
quotes: settings.quotes,
fracts: settings.fracts,
numbers: settings.numbers,
autocorrections,
longest,
symbolpatterns: IS_CHROME ? symbolpatterns.source : symbolpatterns,
antipatterns: IS_CHROME ? antipatterns.source : antipatterns
};
return Promise.resolve(response);
});
25 changes: 15 additions & 10 deletions src/background/modules/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import * as Notifications from "/common/modules/Notifications.js";
import { COMMUNICATION_MESSAGE_TYPE } from "/common/modules/data/BrowserCommunicationTypes.js";
import { menuStructure, SEPARATOR_ID_PREFIX, TRANSFORMATION_TYPE } from "/common/modules/data/Fonts.js";

// Thunderbird
// https://bugzilla.mozilla.org/show_bug.cgi?id=1641573
const IS_THUNDERBIRD = Boolean(globalThis.messenger);
const menus = browser.menus || browser.contextMenus; // fallback for Thunderbird
const PREVIEW_STRING_CUT_LENGTH = 100; // a setting that may improve performance by not calculating invisible parts of the context menu

Expand All @@ -20,15 +23,15 @@ let pasteSymbol = null;
* Thunderbird workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=1641575
*
* @param {string} text
* @param {string} fieldId
* @param {string} [fieldId]
* @returns {void}
*/
function fallback(text, fieldId) {
navigator.clipboard.writeText(text);
const fieldName = fieldId.startsWith("compose") ? fieldId.slice("compose".length) : fieldId;
const fieldName = fieldId?.startsWith("compose") ? fieldId.slice("compose".length) : fieldId;
Notifications.showNotification(
"menuNotificationPressCtrlVTitle",
"menuNotificationPressCtrlVContent",
IS_THUNDERBIRD ? "menuNotificationPressCtrlVContent" : `The add-on was unable to access this tab directly, so the transformed text has been copied to your clipboard.\nPlease press ${pasteSymbol}-V to do the transformation.`,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As always, later localize this, but well this extension is anyway kinda experimental still.... so yeah, fine.

[
pasteSymbol,
fieldName
Expand All @@ -46,21 +49,20 @@ function fallback(text, fieldId) {
* @throws {Error}
*/
async function handleMenuChoosen(info, tab) {
let text = info.selectionText;
const text = info.selectionText;

if (!text) {
return;
}

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

// Thunderbird workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=1641575
if (info.fieldId) {
if (info.fieldId === "composeSubject") {
const details = await browser.compose.getComposeDetails(tab.id);
if (details.subject.split(info.selectionText).length === 2) {
browser.compose.setComposeDetails(tab.id, { subject: details.subject.replace(info.selectionText, output) });
if (details.subject.split(text).length === 2) {
browser.compose.setComposeDetails(tab.id, { subject: details.subject.replace(text, output) });
return;
}
}
Expand All @@ -71,7 +73,10 @@ async function handleMenuChoosen(info, tab) {
browser.tabs.sendMessage(tab.id, {
type: COMMUNICATION_MESSAGE_TYPE.INSERT,
text: output
}, { frameId: info.frameId });
}, { frameId: info.frameId }).catch((error) => {
console.error(`Error: ${error}`);
fallback(output);
});
}

/**
Expand Down Expand Up @@ -304,7 +309,7 @@ export async function init() {
}
menus.onClicked.addListener(handleMenuChoosen);

pasteSymbol = platformInfo.os === "mac" ? "\u2318" : browser.i18n.getMessage("menuCtrlKey");
pasteSymbol = platformInfo.os === "mac" ? "\u{2318}" : browser.i18n.getMessage("menuCtrlKey");
}

BrowserCommunication.addListener(COMMUNICATION_MESSAGE_TYPE.UNICODE_FONT, async (request) => {
Expand Down
12 changes: 11 additions & 1 deletion src/common/modules/UnicodeTransformationHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ function capitalizeEachWord(text) {
* @returns {string}
*/
function sentenceCase(text) {
return Array.from(segmenterSentence.segment(text), ({ segment: [head, ...tail] }) => head.toLocaleUpperCase() + tail.join("")).join("");
return Array.from(segmenterSentence.segment(text), ({ segment }) => {
let found = false;
return Array.from(segmenterWord.segment(segment), ({ segment, isWordLike }) => {
if (!found && isWordLike) {
found = true;
const [head, ...tail] = segment;
return head.toLocaleUpperCase() + tail.join("");
}
return segment;
}).join("");
}).join("");
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/common/modules/data/Fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ export const fontLetters = Object.freeze(
* @type {Object.<string, string>}
*/
export const formats = Object.freeze({
Overlined: "\u0305",
DoubleOverlined: "\u033F",
Strikethrough: "\u0336",
Underlined: "\u0332",
DoubleUnderlined: "\u0333"
Overlined: "\u{305}",
DoubleOverlined: "\u{33F}",
Strikethrough: "\u{336}",
Underlined: "\u{332}",
DoubleUnderlined: "\u{333}"
});
Loading