Skip to content

Commit

Permalink
duplicate current line #35
Browse files Browse the repository at this point in the history
  • Loading branch information
maheshj01 committed Oct 15, 2024
1 parent 636563a commit 79a48b3
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/app/(main)/_components/completion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const TextCompletionInput: React.FC<TextCompletionInputProps> = ({
const newValue = event.target.value;
setInputValue(newValue);
markdownFormatter.current.setValue(newValue);

};

const clipBoardHasUrl = async () => {
Expand Down Expand Up @@ -123,6 +122,10 @@ const TextCompletionInput: React.FC<TextCompletionInputProps> = ({

if ((event.ctrlKey || event.metaKey) && event.shiftKey) {
switch (event.key.toLowerCase()) {
case 'd':
event.preventDefault();
duplicateCurrentLine();
break;
case 'x':
event.preventDefault();
applyFormatting('~~');
Expand Down Expand Up @@ -233,6 +236,23 @@ const TextCompletionInput: React.FC<TextCompletionInputProps> = ({
}
};

const duplicateCurrentLine = () => {
const textarea = inputRef.current;
if (!textarea) return;

const cursorPosition = textarea.selectionStart;
const value = textarea.value;
let currentLineStart = value.lastIndexOf('\n', cursorPosition - 1) + 1;
let currentLineEnd = value.indexOf('\n', cursorPosition);
if (currentLineEnd === -1) currentLineEnd = value.length;

const currentLine = value.substring(currentLineStart, currentLineEnd);
const newLine = `${currentLine}`;

const newValue = value.substring(0, currentLineEnd) + "\n" + newLine + value.substring(currentLineEnd);
const newCursorPosition = currentLineEnd + 1;
updateValue(newValue, newCursorPosition + newLine.length);
}


const onEnter = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
Expand Down

0 comments on commit 79a48b3

Please sign in to comment.