Skip to content

Improve markdown textarea for indentation and lists #31406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions web_src/js/features/comp/ComboMarkdownEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {easyMDEToolbarActions} from './EasyMDEToolbarActions.js';
import {initTextExpander} from './TextExpander.js';
import {showErrorToast} from '../../modules/toast.js';
import {POST} from '../../modules/fetch.js';
import {initTextareaMarkdown} from './EditorMarkdown.js';

let elementIdCounter = 0;

Expand Down Expand Up @@ -84,17 +85,6 @@ class ComboMarkdownEditor {
if (el.nodeName === 'BUTTON' && !el.getAttribute('type')) el.setAttribute('type', 'button');
}

this.textarea.addEventListener('keydown', (e) => {
if (e.shiftKey) {
e.target._shiftDown = true;
}
});
this.textarea.addEventListener('keyup', (e) => {
if (!e.shiftKey) {
e.target._shiftDown = false;
}
});

const monospaceButton = this.container.querySelector('.markdown-switch-monospace');
const monospaceEnabled = localStorage?.getItem('markdown-editor-monospace') === 'true';
const monospaceText = monospaceButton.getAttribute(monospaceEnabled ? 'data-disable-text' : 'data-enable-text');
Expand All @@ -118,6 +108,7 @@ class ComboMarkdownEditor {
await this.switchToEasyMDE();
});

initTextareaMarkdown(this.textarea);
if (this.dropzone) {
initTextareaPaste(this.textarea, this.dropzone);
}
Expand Down
100 changes: 100 additions & 0 deletions web_src/js/features/comp/EditorMarkdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {triggerEditorContentChanged} from './Paste.js';

function handleIndentSelection(textarea, e) {
const selStart = textarea.selectionStart;
const selEnd = textarea.selectionEnd;
if (selEnd === selStart) return; // do not process when no selection

e.preventDefault();
const lines = textarea.value.split('\n');
const selectedLines = [];

let pos = 0;
for (let i = 0; i < lines.length; i++) {
if (pos > selEnd) break;
if (pos >= selStart) selectedLines.push(i);
pos += lines[i].length + 1;
}

for (const i of selectedLines) {
if (e.shiftKey) {
lines[i] = lines[i].replace(/^(\t| {1,2})/, '');
} else {
lines[i] = ` ${lines[i]}`;
}
}

// re-calculating the selection range
let newSelStart, newSelEnd;
pos = 0;
for (let i = 0; i < lines.length; i++) {
if (i === selectedLines[0]) {
newSelStart = pos;
}
if (i === selectedLines[selectedLines.length - 1]) {
newSelEnd = pos + lines[i].length;
break;
}
pos += lines[i].length + 1;
}
textarea.value = lines.join('\n');
textarea.setSelectionRange(newSelStart, newSelEnd);
triggerEditorContentChanged(textarea);
}

function handleNewline(textarea, e) {
const selStart = textarea.selectionStart;
const selEnd = textarea.selectionEnd;
if (selEnd !== selStart) return; // do not process when there is a selection

const value = textarea.value;

// find the current line
const lineStart = value.lastIndexOf('\n', selStart - 1) + 1;
let lineEnd = value.indexOf('\n', selStart);
lineEnd = lineEnd < 0 ? value.length : lineEnd;
let line = value.slice(lineStart, lineEnd);
if (!line) return; // if the line is empty, do nothing, let the browser handle it

// parse the indention
const indention = /^\s*/.exec(line)[0];
line = line.slice(indention.length);

// parse the prefixes: "1. ", "- ", "* ", "[ ] ", "[x] "
const prefixMatch = /^([0-9]+\.|[-*]|\[ \]|\[x\])\s/.exec(line);
let prefix = '';
if (prefixMatch) {
prefix = prefixMatch[0];
if (lineStart + prefix.length > selStart) prefix = ''; // do not add new line if cursor is at prefix
}

line = line.slice(prefix.length);
if (!indention && !prefix) return; // if no indention and no prefix, do nothing, let the browser handle it

e.preventDefault();
if (!line) {
// clear current line
textarea.value = value.slice(0, lineStart) + value.slice(lineEnd);
} else {
// start a new line with the same indention and prefix
let newPrefix = prefix;
if (newPrefix === '[x]') newPrefix = '[ ]';
if (/^\d+\./.test(newPrefix)) newPrefix = `1. `; // a simple approach, otherwise it needs to parse the lines after the current line
const newLine = `\n${indention}${newPrefix}`;
textarea.value = value.slice(0, selStart) + newLine + value.slice(selEnd);
textarea.setSelectionRange(selStart + newLine.length, selStart + newLine.length);
}
triggerEditorContentChanged(textarea);
}

export function initTextareaMarkdown(textarea) {
textarea.addEventListener('keydown', (e) => {
if (e.key === 'Tab' && !e.ctrlKey && !e.metaKey && !e.altKey) {
// use Tab/Shift-Tab to indent/unindent the selected lines
handleIndentSelection(textarea, e);
} else if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey && !e.metaKey && !e.altKey) {
// use Enter to insert a new line with the same indention and prefix
handleNewline(textarea, e);
}
});
}
23 changes: 16 additions & 7 deletions web_src/js/features/comp/Paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function uploadFile(file, uploadUrl) {
return await res.json();
}

function triggerEditorContentChanged(target) {
export function triggerEditorContentChanged(target) {
target.dispatchEvent(new CustomEvent('ce-editor-content-changed', {bubbles: true}));
}

Expand Down Expand Up @@ -124,17 +124,19 @@ async function handleClipboardImages(editor, dropzone, images, e) {
}
}

function handleClipboardText(textarea, text, e) {
// when pasting links over selected text, turn it into [text](link), except when shift key is held
const {value, selectionStart, selectionEnd, _shiftDown} = textarea;
if (_shiftDown) return;
function handleClipboardText(textarea, e, text, isShiftDown) {
Copy link
Member

@silverwind silverwind Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function handleClipboardText(textarea, e, text, isShiftDown) {
function handleClipboardText(textarea, e, text, {isShiftDown} = {}) {

Better to have options argument, also easier to read at caller site.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think it's worth to have an option here. It is not optional.

Copy link
Member

@silverwind silverwind Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could remove the = {} to make it required, but I guess current 4 args is borderline acceptable. Still I prefer options arg because it's immediately clear what it does at the call site. Your variant is a boolean trap.

Copy link
Contributor Author

@wxiaoguang wxiaoguang Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I do not see any difference between handleClipboardText(el, text, e, isShiftDown) vs handleClipboardText(el, text, e, {isShiftDown}) for callers. {} doesn't help readability.
  2. I am just removing the legacy el._shiftDown, and it doesn't really cause "boolean trap" because it only uses variables, no true/false trick.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I am the man who keeps removing the "boolean trap" from Gitea's codebase:

image

Copy link
Contributor Author

@wxiaoguang wxiaoguang Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this.

  • Before: GenerateEmailAvatarLink( true/false )
  • After: GenerateEmailAvatarFastLink / GenerateEmailAvatarFinalLink

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, if you still have the variable name at caller site, that's fine.

// pasting with "shift" means "paste as original content" in most applications
if (isShiftDown) return; // let the browser handle it

// when pasting links over selected text, turn it into [text](link)
const {value, selectionStart, selectionEnd} = textarea;
const selectedText = value.substring(selectionStart, selectionEnd);
const trimmedText = text.trim();
if (selectedText && isUrl(trimmedText)) {
e.stopPropagation();
e.preventDefault();
replaceTextareaSelection(textarea, `[${selectedText}](${trimmedText})`);
}
// else, let the browser handle it
}

export function initEasyMDEPaste(easyMDE, dropzone) {
Expand All @@ -147,12 +149,19 @@ export function initEasyMDEPaste(easyMDE, dropzone) {
}

export function initTextareaPaste(textarea, dropzone) {
let isShiftDown = false;
textarea.addEventListener('keydown', (e) => {
if (e.shiftKey) isShiftDown = true;
});
textarea.addEventListener('keyup', (e) => {
if (!e.shiftKey) isShiftDown = false;
});
textarea.addEventListener('paste', (e) => {
const {images, text} = getPastedContent(e);
if (images.length) {
handleClipboardImages(new TextareaEditor(textarea), dropzone, images, e);
} else if (text) {
handleClipboardText(textarea, text, e);
handleClipboardText(textarea, e, text, isShiftDown);
}
});
}