compose: Fix bug where spoiler would not always start on a new line.

Earlier, when a selection not starting at the beginning of a line was
formatted as a spoiler, the spoiler would not start on a new line, and
so would not be rendered as a spoiler. The `Header` highlighting too
was off by one character.

Now, the spoiler starts on a new line, and the `Header` highlighting
works as expected too.
This commit is contained in:
N-Shar-ma 2024-01-12 20:07:48 +05:30
parent be13557ead
commit c46375f6fc
1 changed files with 4 additions and 4 deletions

View File

@ -581,8 +581,8 @@ export function format_text($textarea, type, inserted_content) {
};
const format_spoiler = () => {
let spoiler_syntax_start = "```spoiler \n";
const spoiler_syntax_start_without_break = "```spoiler ";
const spoiler_syntax_start = "```spoiler \n";
let spoiler_syntax_start_without_break = "```spoiler ";
let spoiler_syntax_end = "\n```";
// For when the entire spoiler block (with no header) is selected.
@ -708,7 +708,7 @@ export function format_text($textarea, type, inserted_content) {
}
if (range.start > 0 && text[range.start - 1] !== "\n") {
spoiler_syntax_start = "\n" + spoiler_syntax_start;
spoiler_syntax_start_without_break = "\n" + spoiler_syntax_start_without_break;
}
if (range.end < text.length && text[range.end] !== "\n") {
spoiler_syntax_end = spoiler_syntax_end + "\n";
@ -720,7 +720,7 @@ export function format_text($textarea, type, inserted_content) {
wrapSelection(field, spoiler_syntax_start_with_header, spoiler_syntax_end);
field.setSelectionRange(
range.start + spoiler_syntax_start.length - 1,
range.start + spoiler_syntax_start_without_break.length,
range.start + spoiler_syntax_start_with_header.length - 1,
);
};