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

Desktop : Partially resolves #1718 : Adding Special Paste to Context menu #4676

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ function CodeMirror(props: NoteBodyEditorProps, ref: any) {
textPaste: () => {
editorPaste();
},
textPasteSpecial: () => {
editorSpecialPaste();
},
textSelectAll: () => {
return editorRef.current.execCommand('selectAll');
},
Expand Down Expand Up @@ -277,6 +280,23 @@ function CodeMirror(props: NoteBodyEditorProps, ref: any) {
}
}, []);

const editorSpecialPaste = useCallback(() => {
if (editorRef.current) {
const clipboardContent = clipboard.readText();
const selectionContent = editorRef.current.getSelection();

const urlRegex = new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)?/gi);

if (clipboardContent.length > 0 && clipboardContent.match(urlRegex)) {
if (selectionContent.length > 0) {
editorRef.current.replaceSelection(`[${selectionContent}](${clipboardContent})`);
} else {
editorRef.current.replaceSelection(`[${clipboardContent}](${clipboardContent})`);
}
}
}
}, []);

const editorPasteText = useCallback(() => {
if (editorRef.current) {
editorRef.current.replaceSelection(clipboard.readText());
Expand Down Expand Up @@ -674,6 +694,16 @@ function CodeMirror(props: NoteBodyEditorProps, ref: any) {
})
);

menu.append(
new MenuItem({
label: _('Special Paste'),
enabled: true,
click: async () => {
editorSpecialPaste();
},
})
);

const spellCheckerMenuItems = SpellCheckerService.instance().contextMenuItems(params.misspelledWord, params.dictionarySuggestions);

for (const item of spellCheckerMenuItems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const declarations: CommandDeclaration[] = [
label: () => _('Paste'),
role: 'paste',
},
{
name: 'textPasteSpecial',
label: () => _('Paste Special'),
},
{
name: 'textSelectAll',
label: () => _('Select all'),
Expand Down
2 changes: 2 additions & 0 deletions packages/lib/services/KeymapService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const defaultKeymapItems = {
{ accelerator: 'Cmd+C', command: 'textCopy' },
{ accelerator: 'Cmd+X', command: 'textCut' },
{ accelerator: 'Cmd+V', command: 'textPaste' },
{ accelerator: 'Shift+Cmd+V', command: 'textPasteSpecial' },
{ accelerator: 'Cmd+A', command: 'textSelectAll' },
{ accelerator: 'Cmd+B', command: 'textBold' },
{ accelerator: 'Cmd+I', command: 'textItalic' },
Expand Down Expand Up @@ -64,6 +65,7 @@ const defaultKeymapItems = {
{ accelerator: 'Ctrl+C', command: 'textCopy' },
{ accelerator: 'Ctrl+X', command: 'textCut' },
{ accelerator: 'Ctrl+V', command: 'textPaste' },
{ accelerator: 'Shift+Cmd+V', command: 'textPasteSpecial' },
{ accelerator: 'Ctrl+A', command: 'textSelectAll' },
{ accelerator: 'Ctrl+B', command: 'textBold' },
{ accelerator: 'Ctrl+I', command: 'textItalic' },
Expand Down