Skip to content

Commit

Permalink
feat: add a command to use the native copy of Obsidian
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jun 8, 2024
1 parent 89b33ad commit 58980c4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/i18n/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 26 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { removeDataBasePluginRelationShip } from "./utils/pluginFix";
import {
canvasSelectionText,
copySelectionRange,
getAnchor,
getHead,
getSelectionAsHTML,
} from "./utils/selection";

Expand Down Expand Up @@ -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.`);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 58980c4

Please sign in to comment.