Skip to content

Commit

Permalink
Override paste behavior #35
Browse files Browse the repository at this point in the history
  • Loading branch information
maheshj01 committed Oct 8, 2024
1 parent 388e718 commit 9b67093
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
2 changes: 1 addition & 1 deletion public/sw.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 38 additions & 8 deletions src/app/(main)/_components/completion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ const TextCompletionInput: React.FC<TextCompletionInputProps> = ({

};

const handleKeyDown = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
const clipBoardHasUrl = async () => {
const text = await navigator.clipboard.readText();
const urlRegex = /https?:\/\/[^\s]+/g;
return urlRegex.test(text);
}

const handleKeyDown = async (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (event.ctrlKey || event.metaKey) {
switch (event.key.toLowerCase()) {
case 'b':
Expand Down Expand Up @@ -80,6 +86,15 @@ const TextCompletionInput: React.FC<TextCompletionInputProps> = ({
onUndo();
}
break;
case 'v':
event.preventDefault();
if (await clipBoardHasUrl()) {
const text = await navigator.clipboard.readText();
applyLinkFormatting(text);
} else {
replaceSelection();
}
break;
case 'y':
event.preventDefault();
onRedo();
Expand Down Expand Up @@ -275,15 +290,30 @@ const TextCompletionInput: React.FC<TextCompletionInputProps> = ({
updateValue(value, newCursorPos);
};

const applyLinkFormatting = () => {
const replaceSelection = async () => {
const replacement = await navigator.clipboard.readText();
const textarea = inputRef.current;
if (!textarea) return;
if (!replacement || !textarea) return;
const replacedtext = inputValue.substring(0, textarea.selectionStart) + replacement +
inputValue.substring(textarea.selectionEnd)
updateValue(replacedtext, textarea.selectionStart + replacement.length);
}

const { value, newCursorPos } = markdownFormatter.current.applyLinkFormatting(
textarea.selectionStart,
textarea.selectionEnd
);
updateValue(value, newCursorPos);
const applyLinkFormatting = (text?: string) => {
const textarea = inputRef.current;
if (!textarea) return;
const selection = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
if (selection.length == 0) {
replaceSelection();
} else {

const { value, newCursorPos } = markdownFormatter.current.applyLinkFormatting(
textarea.selectionStart,
textarea.selectionEnd,
text
);
updateValue(value, newCursorPos);
}
};

const applyListFormatting = (listSyntax: string) => {
Expand Down
20 changes: 14 additions & 6 deletions src/app/(main)/_services/MDFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@ export class MarkdownFormatter {
return { value: newValue, newCursorPos };
}

public applyLinkFormatting(start: number, end: number): { value: string, newCursorPos: number } {
public applyLinkFormatting(start: number, end: number, url?: string): { value: string, newCursorPos: number } {
const selectedText = this.value.substring(start, end);
const newValue =
this.value.substring(0, start) +
`[${selectedText}]()` +
this.value.substring(end);
if (url) {
const newValue =
this.value.substring(0, start) +
`[${selectedText}](${url})` +
this.value.substring(end);
return { value: newValue, newCursorPos: end + 3 };
} else {
const newValue =
this.value.substring(0, start) +
`[${selectedText}]()` +
this.value.substring(end);
return { value: newValue, newCursorPos: end + 3 };
}

return { value: newValue, newCursorPos: end + 3 };
}

public applyListFormatting(start: number, listSyntax: string): { value: string, newCursorPos: number } {
Expand Down

0 comments on commit 9b67093

Please sign in to comment.