Skip to content

Commit

Permalink
Added sentence case transformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdulcet committed Dec 10, 2024
1 parent 374ed91 commit 46b6bf6
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ For instance, it converts quotes like `"these"` to `“these”`, which are typo
Additionally, you can convert text into more than 40 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 works with modern Firefox v112 or higher, Chromium/Chrome and Thunderbird v112 or higher.
This extension works with modern Firefox v125 or higher, Chromium/Chrome and Thunderbird v125 or higher.

## Download

Expand Down
2 changes: 1 addition & 1 deletion assets/texts/en/amoDescription.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
The add-on is free/libre open-source software and developed on GitHub. <a href="https://github.com/rugk/unicodify">Fork it on GitHub</a> and contribute.
<a href="https://github.com/rugk/unicodify/contribute">There are some easy issues to start with.</a>

This extension works with modern Firefox and Thunderbird v112 or higher.
This extension works with modern Firefox and Thunderbird v125 or higher.


<b>🙋‍♀️ Contribute 🙋‍♀️</b>
Expand Down
2 changes: 1 addition & 1 deletion assets/texts/en/atnDescription.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
The add-on is free/libre open-source software and developed on GitHub. <a href="https://github.com/rugk/unicodify">Fork it on GitHub</a> and contribute.
<a href="https://github.com/rugk/unicodify/contribute">There are some easy issues to start with.</a>

This extension works with modern Firefox and Thunderbird v112 or higher.
This extension works with modern Firefox and Thunderbird v125 or higher.


<b>Contribute</b>
Expand Down
2 changes: 1 addition & 1 deletion scripts/manifests/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"browser_specific_settings": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "112.0"
"strict_min_version": "125.0"
}
}
}
2 changes: 1 addition & 1 deletion scripts/manifests/firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"browser_specific_settings": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "112.0"
"strict_min_version": "125.0"
}
}
}
2 changes: 1 addition & 1 deletion scripts/manifests/thunderbirdmanifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"browser_specific_settings": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "112.0"
"strict_min_version": "125.0"
}
}
}
4 changes: 4 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@
"message": "Change casing",
"description": "An entry in the context menu. This is an entry for the case."
},
"menuCaseSentenceCase": {
"message": "Sentence case.",
"description": "An entry in the context menu. This is an entry for the case."
},
"menuCaseLowercase": {
"message": "Lowercase",
"description": "An entry in the context menu. This is an entry for the case."
Expand Down
12 changes: 6 additions & 6 deletions src/background/modules/AutocorrectHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ function createRegEx(tree) {

for (const char in tree) {
if (char) {
const escaptedChar = char.replace(regExSpecialChars, "\\$&");
const escaptedChar = RegExp.escape ? RegExp.escape(char) : char.replaceAll(regExSpecialChars, String.raw`\$&`);

const atree = tree[char];
if (!(LEAF in atree && Object.keys(atree).length === 0)) {
if (LEAF in atree && Object.keys(atree).length === 0) {
characterClass.push(escaptedChar);
} else {
const recurse = createRegEx(atree);
alternatives.push(recurse + escaptedChar);
} else {
characterClass.push(escaptedChar);
}
}
}
Expand Down Expand Up @@ -139,7 +139,7 @@ function applySettings() {
continue;
}
const aindex = x.indexOf(y);
if (aindex >= 0) {
if (aindex !== -1) {
if (aindex < index) {
index = aindex;
length = y.length;
Expand Down Expand Up @@ -235,7 +235,7 @@ export async function init() {
setSettings(autocorrect);

// Thunderbird
// Remove if part 3 of https://bugzilla.mozilla.org/show_bug.cgi?id=1630786#c4 is ever done
// Cannot register scripts in manifest.json file: https://bugzilla.mozilla.org/show_bug.cgi?id=1902843
if (browser.composeScripts) {
browser.composeScripts.register({
js: [
Expand Down
29 changes: 23 additions & 6 deletions src/common/modules/UnicodeTransformationHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { fontLetters, formats, CASE_ID_PREFIX, CODE_CASE_ID_PREFIX, FONT_ID_PREFIX, FORMAT_ID_PREFIX, TRANSFORMATION_TYPE } from "/common/modules/data/Fonts.js";

const segmenter = new Intl.Segmenter();
const segmenter1 = new Intl.Segmenter([], { granularity: "word" });
const segmenter2 = new Intl.Segmenter([], { granularity: "sentence" });

/**
* Transforms the given text according to the given transformation.
*
Expand Down Expand Up @@ -67,11 +71,23 @@ export function getTransformationType(transformationId) {
* @returns {string}
*/
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
// Intl.Segmenter is not yet supported by Firefox/Thunderbird: https://bugzilla.mozilla.org/show_bug.cgi?id=1423593
// \p{Alphabetic}
return text.replaceAll(/(?<=^|\P{Alpha})\p{Alpha}\S*/gu, ([h, ...t]) => h.toLocaleUpperCase() + t.join(""));
return Array.from(segmenter1.segment(text), ({ segment, isWordLike }) => {
if (isWordLike) {
const [h, ...t] = segment;
return h.toLocaleUpperCase() + t.join("");
}
return segment;
}).join("");
}

/**
* Sentence Case.
*
* @param {string} text
* @returns {string}
*/
function sentenceCase(text) {
return Array.from(segmenter2.segment(text), ({ segment: [h, ...t] }) => h.toLocaleUpperCase() + t.join("")).join("");
}

/**
Expand Down Expand Up @@ -132,7 +148,7 @@ function changeFormat(text, chosenFormat) {
throw new Error(`Format ${chosenFormat} could not be processed.`);
}

return Array.from(text, (letter) => letter + format).join("");
return Array.from(segmenter.segment(text), ({ segment }) => segment + format).join("");
}

/**
Expand Down Expand Up @@ -166,6 +182,7 @@ function toggleCase(atext) {
* @type {Object.<string, function(string): string>}
*/
const changeCase = Object.freeze({
SentenceCase: (str) => sentenceCase(str.toLocaleLowerCase()),
Lowercase: (str) => str.toLocaleLowerCase(),
Uppercase: (str) => str.toLocaleUpperCase(),
CapitalizeEachWord: (str) => capitalizeEachWord(str.toLocaleLowerCase()),
Expand Down
1 change: 1 addition & 0 deletions src/common/modules/data/Fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export const menuStructure = Object.freeze({
},
[TRANSFORMATION_TYPE.CASING]: {
[`${CASE_ID_PREFIX}Casing`]: [
`${CASE_ID_PREFIX}SentenceCase`,
`${CASE_ID_PREFIX}Lowercase`,
`${CASE_ID_PREFIX}Uppercase`,
`${CASE_ID_PREFIX}CapitalizeEachWord`,
Expand Down
20 changes: 8 additions & 12 deletions src/content_scripts/autocorrect.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const constants = Object.freeze({
const AUTOCORRECT_CONTENT = "autocorrectContent";
const INSERT = "insert";

const segmenter = new Intl.Segmenter();

let insertedText; // Last insert text
let deletedText; // Last deleted text
let lastTarget; // Last target
Expand Down Expand Up @@ -65,7 +67,11 @@ function getCaretPosition(target) {
// ContentEditable elements
if (target.isContentEditable || document.designMode === "on") {
target.focus();
const _range = document.getSelection().getRangeAt(0);
const selection = document.getSelection();
if (selection.rangeCount !== 1) {
return null;
}
const _range = selection.getRangeAt(0);
if (!_range.collapsed) {
return null;
}
Expand All @@ -77,7 +83,6 @@ function getCaretPosition(target) {
return caretposition;
}
// input and textarea fields

if (target.selectionStart !== target.selectionEnd) {
return null;
}
Expand Down Expand Up @@ -135,22 +140,13 @@ function insertIntoPage(atext) {
/**
* Count Unicode characters.
* Adapted from: https://blog.jonnew.com/posts/poo-dot-length-equals-two
* Intl.Segmenter is not yet supported by Firefox/Thunderbird: https://bugzilla.mozilla.org/show_bug.cgi?id=1423593
*
* @param {string} str
* @returns {number}
*/
function countChars(str) {
// removing the joiners
const split = str.split("\u200D");
let count = 0;

for (const s of split) {
// removing the variation selectors
count += Array.from(s.replaceAll(/[\uFE00-\uFE0F]/gu, "")).length;
}

return count;
return Array.from(segmenter.segment(str.replaceAll("\u200D", ""))).length;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"browser_specific_settings": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "112.0"
"strict_min_version": "125.0"
}
}
}

0 comments on commit 46b6bf6

Please sign in to comment.