From 58980c4a3eaad94b7c7fca3c7d3252e588864c4d Mon Sep 17 00:00:00 2001 From: Mara Date: Sat, 8 Jun 2024 14:48:57 +0200 Subject: [PATCH] feat: add a command to use the native copy of Obsidian --- src/i18n/locales/en.json | 1 + src/i18n/locales/fr.json | 1 + src/main.ts | 26 ++++++++++++++++++++++++++ src/utils/selection.ts | 4 ++-- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index 3f89959..d4dc2c0 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -17,6 +17,7 @@ }, "commands": { "all": "Copy selected text", + "brute": "Native copy", "editor": "Copy selected text from edit view", "other": "Copy selected text from opened view", "reading": "Copy selected text from reading view" diff --git a/src/i18n/locales/fr.json b/src/i18n/locales/fr.json index 0164e33..ad11ca2 100644 --- a/src/i18n/locales/fr.json +++ b/src/i18n/locales/fr.json @@ -17,6 +17,7 @@ }, "commands": { "all": "Copier le texte sélectionné", + "brute": "Copie native", "editor": "Copier le texte sélectionné depuis l'éditeur", "other": "Copier le texte sélectionné depuis la vue non markdown", "reading": "Copier le texte sélectionné depuis la vue de lecture" diff --git a/src/main.ts b/src/main.ts index d48f2a8..82209a7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -17,6 +17,8 @@ import { removeDataBasePluginRelationShip } from "./utils/pluginFix"; import { canvasSelectionText, copySelectionRange, + getAnchor, + getHead, getSelectionAsHTML, } from "./utils/selection"; @@ -354,6 +356,30 @@ export default class EnhancedCopy extends Plugin { this.registerEditorExtension(cutExt); } } + //file menu + this.registerEvent( + this.app.workspace.on("editor-menu", (menu, editor, view) => { + menu.addItem((item) => { + item.setTitle(i18next.t("commands.brute")); + item.setIcon("clipboard"); + item.onClick(() => { + navigator.clipboard.writeText(copySelectionRange(editor, this)); + }); + }); + })) + //use the native copy + this.addCommand({ + id: "copy-brute", + name: i18next.t("commands.brute"), + callback: () => { + const editor = this.app.workspace.activeEditor?.editor; + if (editor) { + navigator.clipboard.writeText(copySelectionRange(editor, this)); + } else { + navigator.clipboard.writeText(activeWindow.getSelection()?.toString() ?? ""); + } + }, + }); } onunload() { console.log(`CopyReadingInMarkdown v.${this.manifest.version} unloaded.`); diff --git a/src/utils/selection.ts b/src/utils/selection.ts index b37004a..3f56b42 100644 --- a/src/utils/selection.ts +++ b/src/utils/selection.ts @@ -49,7 +49,7 @@ export function getSelectionAsHTML(settings: GlobalSettings): string { * @param {EditorPosition} anchor Original anchor from EditorSelection */ -function getHead(head: EditorPosition, anchor: EditorPosition) { +export function getHead(head: EditorPosition, anchor: EditorPosition) { if (head.line === anchor.line) { if (head.ch < anchor.ch) { return head; @@ -68,7 +68,7 @@ function getHead(head: EditorPosition, anchor: EditorPosition) { * @param {EditorPosition} anchor Original anchor from EditorSelection */ -function getAnchor(head: EditorPosition, anchor: EditorPosition) { +export function getAnchor(head: EditorPosition, anchor: EditorPosition) { if (head.line === anchor.line) { if (head.ch < anchor.ch) { return anchor;