Skip to content
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

feat: Support for Markdown Formatting Keyboard Shortcuts #3936

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
114 changes: 114 additions & 0 deletions packages/shared/src/components/fields/MarkdownInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,119 @@ function MarkdownInput(
focusInput(textareaRef.current, [content.length, content.length]);
}, []);

const handleMarkdownShortcut = (
e: React.KeyboardEvent<HTMLTextAreaElement>,
) => {
const textarea = textareaRef.current;
const { selectionStart, selectionEnd, value } = textarea;

const updateTextarea = (
newValue: string,
newStart: number,
newEnd: number,
) => {
onValueUpdate?.(newValue);
callbacks.onInput?.({
currentTarget: { value: newValue },
} as React.FormEvent<HTMLTextAreaElement>);

requestAnimationFrame(() => {
textarea.value = newValue;
textarea.setSelectionRange(newStart, newEnd);
});
};

const handleTextFormatting = (symbol: string) => {
const selectedValue = value.substring(selectionStart, selectionEnd);
const trimmedValue = selectedValue.trim();

const leadingWhitespace =
selectedValue.length - selectedValue.trimStart().length;
const newStart = selectionStart + leadingWhitespace;
const newEnd = newStart + trimmedValue.length;
const symbolLength = symbol.length;

const isFormatted =
value.substring(newStart - symbolLength, newStart) === symbol &&
value.substring(newEnd, newEnd + symbolLength) === symbol;

let updatedValue;
let updatedStart;
let updatedEnd;

if (isFormatted) {
updatedValue =
value.substring(0, newStart - symbolLength) +
trimmedValue +
value.substring(newEnd + symbolLength);
updatedStart = newStart - symbolLength;
updatedEnd = newEnd - symbolLength;
} else {
updatedValue =
value.substring(0, newStart) +
symbol +
trimmedValue +
symbol +
value.substring(newEnd);
updatedStart = newStart + symbolLength;
updatedEnd = newEnd + symbolLength;
}

updateTextarea(updatedValue, updatedStart, updatedEnd);
};

const handleLinkPaste = () => {
navigator.clipboard.readText().then((clipboardText) => {
const selectedText = value.substring(selectionStart, selectionEnd);
const trimmedText = selectedText.trim();

const leadingWhitespace =
selectedText.length - selectedText.trimStart().length;
const trailingWhitespace =
selectedText.length - selectedText.trimEnd().length;

const newStart = selectionStart + leadingWhitespace;
const newEnd = selectionEnd - trailingWhitespace;

const newText =
/^https?:\/\//.test(clipboardText) && trimmedText
? `${value.substring(
0,
newStart,
)}[${trimmedText}](${clipboardText})${value.substring(newEnd)}`
: value.substring(0, selectionStart) +
clipboardText +
value.substring(selectionEnd);

const linkEnd = newStart + `[${trimmedText}](${clipboardText})`.length;
updateTextarea(newText, linkEnd, linkEnd);
});
};

if (e.metaKey || e.ctrlKey) {
switch (e.key) {
case 'b':
e.preventDefault();
handleTextFormatting('**');
break;
case 'i':
e.preventDefault();
handleTextFormatting('_');
break;
case 'v':
e.preventDefault();
handleLinkPaste();
Abhi-Bohora marked this conversation as resolved.
Show resolved Hide resolved
break;
case 'l':
e.preventDefault();
onLinkCommand?.();
break;
default:
break;
}
}
};

return (
<div
className={classNames(
Expand Down Expand Up @@ -258,6 +371,7 @@ function MarkdownInput(
)}
value={input}
onClick={onInputClick}
onKeyDown={handleMarkdownShortcut}
onDragOver={(e) => e.preventDefault()} // for better experience and stop opening the file with browser
maxLength={maxInputLength}
/>
Expand Down