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

Merged
merged 5 commits into from
Jan 6, 2025
Merged
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
127 changes: 127 additions & 0 deletions packages/shared/src/components/fields/MarkdownInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { Divider } from '../../utilities';
import { usePopupSelector } from '../../../hooks/usePopupSelector';
import { focusInput } from '../../../lib/textarea';
import CloseButton from '../../CloseButton';
import { isValidHttpUrl } from '../../../lib';

interface ClassName {
container?: string;
Expand Down Expand Up @@ -172,6 +173,131 @@ function MarkdownInput(
focusInput(textareaRef.current, [content.length, content.length]);
}, []);

const getTextBoundaries = (
text: string,
selectionStart: number,
selectionEnd: number,
) => {
const selectedText = text.substring(selectionStart, selectionEnd);
const trimmedText = selectedText.trim();
const leadingWhitespace =
selectedText.length - selectedText.trimStart().length;
const trailingWhitespace =
selectedText.length - selectedText.trimEnd().length;

return {
trimmedText,
newStart: selectionStart + leadingWhitespace,
newEnd: selectionEnd - trailingWhitespace,
};
};

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 { trimmedText, newStart, newEnd } = getTextBoundaries(
value,
selectionStart,
selectionEnd,
);
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) +
trimmedText +
value.substring(newEnd + symbolLength);
updatedStart = newStart - symbolLength;
updatedEnd = newEnd - symbolLength;
} else {
updatedValue =
value.substring(0, newStart) +
symbol +
trimmedText +
symbol +
value.substring(newEnd);
updatedStart = newStart + symbolLength;
updatedEnd = newEnd + symbolLength;
}

updateTextarea(updatedValue, updatedStart, updatedEnd);
};

const handleLinkPaste = () => {
navigator.clipboard.readText().then((clipboardText) => {
const { trimmedText, newStart, newEnd } = getTextBoundaries(
value,
selectionStart,
selectionEnd,
);

const newText =
isValidHttpUrl(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 @@ -260,6 +386,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