Move cursor/selection algo to lib/utilities.

This commit is contained in:
Claas Augner
2016-12-29 10:16:17 +01:00
parent 37386faff2
commit 2e9bbccea9
2 changed files with 32 additions and 31 deletions

View File

@ -300,5 +300,35 @@ export function defaultHomepage() {
return Discourse.SiteSettings.top_menu.split("|")[0].split(",")[0];
}
export function determinePostReplaceSelection({ selection, needle, replacement }) {
const diff = (replacement.end - replacement.start) - (needle.end - needle.start);
if (selection.end <= needle.start) {
// Selection ends (and starts) before needle.
return { start: selection.start, end: selection.end };
} else if (selection.start <= needle.start) {
// Selection starts before needle...
if (selection.end < needle.end) {
// ... and ends inside needle.
return { start: selection.start, end: needle.start };
} else {
// ... and spans needle completely.
return { start: selection.start, end: selection.end + diff };
}
} else if (selection.start < needle.end) {
// Selection starts inside needle...
if (selection.end <= needle.end) {
// ... and ends inside needle.
return { start: replacement.end, end: replacement.end };
} else {
// ... and spans end of needle.
return { start: replacement.end, end: selection.end + diff };
}
} else {
// Selection starts (and ends) behind needle.
return { start: selection.start + diff, end: selection.end + diff };
}
}
// This prevents a mini racer crash
export default {};