Skip to content

Commit

Permalink
Revert "feat: Support for Markdown Formatting Keyboard Shortcuts (#3936
Browse files Browse the repository at this point in the history
…)"

This reverts commit 546cf7c.
  • Loading branch information
sshanzel committed Jan 9, 2025
1 parent 2c1ceb5 commit e831ffc
Showing 1 changed file with 0 additions and 127 deletions.
127 changes: 0 additions & 127 deletions packages/shared/src/components/fields/MarkdownInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ 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 @@ -173,131 +172,6 @@ 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();
break;
case 'l':
e.preventDefault();
onLinkCommand?.();
break;
default:
break;
}
}
};

return (
<div
className={classNames(
Expand Down Expand Up @@ -386,7 +260,6 @@ 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

0 comments on commit e831ffc

Please sign in to comment.