Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created March 30, 2023 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/ffe6debd7f0b8a7a43c7e073b0c0287d to your computer and use it in GitHub Desktop.
Save bjoerntx/ffe6debd7f0b8a7a43c7e073b0c0287d to your computer and use it in GitHub Desktop.
function setSentenceCase() {
// get the start offset and the selected text
TXTextControl.selection.getStart(function(start) {
TXTextControl.selection.getText(function(curText) {
// in Text Control, CRLF is one character
text = curText.split("\r\n").join("\n");
// get all matches including indexes
var matchesAll = [...text.matchAll(/\.\ +/g)];
// to get the actual length, another match is required in JS
var matches = [...text.match(/\.\ +/g)];
const indexes = matchesAll.map(match => match.index);
// loop through array of indexes
for (let index = 0; index < indexes.length; ++index) {
const element = indexes[index];
var sel = TXTextControl.selection;
var bounds = { "start": matches[index].length + start + element, "length": 1 };
sel.setBounds(bounds);
sel.setText(text.charAt(matches[index].length + element).toUpperCase());
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment