Fix mention and emoji expansion & Improve leaving list completion (#6597)

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6597
Reviewed-by: 0ko <0ko@noreply.codeberg.org>
This commit is contained in:
0ko 2025-01-20 12:51:39 +00:00
commit 0379739ac9
2 changed files with 71 additions and 30 deletions

View file

@ -99,6 +99,8 @@ class ComboMarkdownEditor {
e.target._shiftDown = true;
}
if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey && !e.altKey) {
// Prevent special line break handling if currently a text expander popup is open
if (this.textarea.hasAttribute('aria-expanded')) return;
if (!this.breakLine()) return; // Nothing changed, let the default handler work.
this.options?.onContentChanged?.(this, e);
e.preventDefault();
@ -407,13 +409,27 @@ class ComboMarkdownEditor {
// Find the beginning of the current line.
const lineStart = Math.max(0, value.lastIndexOf('\n', start - 1) + 1);
// Find the end and extract the line.
const lineEnd = value.indexOf('\n', start);
const line = value.slice(lineStart, lineEnd === -1 ? value.length : lineEnd);
const nextLF = value.indexOf('\n', start);
const lineEnd = nextLF === -1 ? value.length : nextLF;
const line = value.slice(lineStart, lineEnd);
// Match any whitespace at the start + any repeatable prefix + exactly one space after.
const prefix = line.match(/^\s*((\d+)[.)]\s|[-*+]\s+(\[[ x]\]\s?)?|(>\s+)+)?/);
const prefix = line.match(/^\s*((\d+)[.)]\s|[-*+]\s{1,4}\[[ x]\]\s?|[-*+]\s|(>\s?)+)?/);
// Defer to browser if we can't do anything more useful, or if the cursor is inside the prefix.
if (!prefix || !prefix[0].length || lineStart + prefix[0].length > start) return false;
if (!prefix) return false;
const prefixLength = prefix[0].length;
if (!prefixLength || lineStart + prefixLength > start) return false;
// If the prefix is just indentation (which should always be an even number of spaces or tabs), check if a single whitespace is added to the end of the line.
// If this is the case do not leave the indentation and continue with the prefix.
if ((prefixLength % 2 === 1 && /^ +$/.test(prefix[0])) || /^\t+ $/.test(prefix[0])) {
prefix[0] = prefix[0].slice(0, prefixLength - 1);
} else if (prefixLength === lineEnd - lineStart) {
this.textarea.setSelectionRange(lineStart, lineEnd);
if (!document.execCommand('insertText', false, '\n')) {
this.textarea.setRangeText('\n');
}
return true;
}
// Insert newline + prefix.
let text = `\n${prefix[0]}`;