react-mde
Version:
React Markdown Editor
103 lines (102 loc) • 3.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBreaksNeededForEmptyLineAfter = exports.getBreaksNeededForEmptyLineBefore = exports.selectWord = exports.getSurroundingWord = void 0;
function getSurroundingWord(text, position) {
if (!text)
throw Error("Argument 'text' should be truthy");
var isWordDelimiter = function (c) { return c === " " || c.charCodeAt(0) === 10; };
// leftIndex is initialized to 0 because if selection is 0, it won't even enter the iteration
var start = 0;
// rightIndex is initialized to text.length because if selection is equal to text.length it won't even enter the interation
var end = text.length;
// iterate to the left
for (var i = position; i - 1 > -1; i--) {
if (isWordDelimiter(text[i - 1])) {
start = i;
break;
}
}
// iterate to the right
for (var i = position; i < text.length; i++) {
if (isWordDelimiter(text[i])) {
end = i;
break;
}
}
return { start: start, end: end };
}
exports.getSurroundingWord = getSurroundingWord;
/**
* If the cursor is inside a word and (selection.start === selection.end)
* returns a new Selection where the whole word is selected
* @param text
* @param selection
*/
function selectWord(_a) {
var text = _a.text, selection = _a.selection;
if (text && text.length && selection.start === selection.end) {
// the user is pointing to a word
return getSurroundingWord(text, selection.start);
}
return selection;
}
exports.selectWord = selectWord;
/**
* Gets the number of line-breaks that would have to be inserted before the given 'startPosition'
* to make sure there's an empty line between 'startPosition' and the previous text
*/
function getBreaksNeededForEmptyLineBefore(text, startPosition) {
if (text === void 0) { text = ""; }
if (startPosition === 0)
return 0;
// rules:
// - If we're in the first line, no breaks are needed
// - Otherwise there must be 2 breaks before the previous character. Depending on how many breaks exist already, we
// may need to insert 0, 1 or 2 breaks
var neededBreaks = 2;
var isInFirstLine = true;
for (var i = startPosition - 1; i >= 0 && neededBreaks >= 0; i--) {
switch (text.charCodeAt(i)) {
case 32: // blank space
continue;
case 10: // line break
neededBreaks--;
isInFirstLine = false;
break;
default:
return neededBreaks;
}
}
return isInFirstLine ? 0 : neededBreaks;
}
exports.getBreaksNeededForEmptyLineBefore = getBreaksNeededForEmptyLineBefore;
/**
* Gets the number of line-breaks that would have to be inserted after the given 'startPosition'
* to make sure there's an empty line between 'startPosition' and the next text
*/
function getBreaksNeededForEmptyLineAfter(text, startPosition) {
if (text === void 0) { text = ""; }
if (startPosition === text.length - 1)
return 0;
// rules:
// - If we're in the first line, no breaks are needed
// - Otherwise there must be 2 breaks before the previous character. Depending on how many breaks exist already, we
// may need to insert 0, 1 or 2 breaks
var neededBreaks = 2;
var isInLastLine = true;
for (var i = startPosition; i < text.length && neededBreaks >= 0; i++) {
switch (text.charCodeAt(i)) {
case 32:
continue;
case 10: {
neededBreaks--;
isInLastLine = false;
break;
}
default:
return neededBreaks;
}
}
return isInLastLine ? 0 : neededBreaks;
}
exports.getBreaksNeededForEmptyLineAfter = getBreaksNeededForEmptyLineAfter;