Skip to content

Commit 59d215d

Browse files
committed
initial
1 parent 9d1bf9f commit 59d215d

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

main.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Copy As Note (Obsidian plugin)
3+
*
4+
* Mobile workflow:
5+
* - This plugin exposes a command that can be manually added to the Obsidian Mobile toolbar.
6+
* - It does NOT auto-add a ribbon/status bar icon.
7+
*/
8+
9+
const { Plugin, Notice } = require('obsidian');
10+
11+
module.exports = class MobileCopyNotePlugin extends Plugin {
12+
async onload() {
13+
// Obsidian Mobile toolbar rules: expose a command that the user can add manually.
14+
this.addCommand({
15+
id: 'copy-active-note-to-clipboard',
16+
name: 'Copy current note to clipboard',
17+
callback: async () => {
18+
await this.copyCurrentNoteToClipboard();
19+
},
20+
});
21+
}
22+
23+
async copyCurrentNoteToClipboard() {
24+
const activeFile = this.app.workspace.getActiveFile();
25+
26+
if (!activeFile) {
27+
new Notice('No active note to copy.');
28+
return;
29+
}
30+
31+
if (activeFile.extension !== 'md') {
32+
new Notice('Active file is not a markdown note.');
33+
return;
34+
}
35+
36+
let content;
37+
try {
38+
// Read raw file text from the vault (not rendered/preview content)
39+
content = await this.app.vault.read(activeFile);
40+
} catch (error) {
41+
console.error('Copy As Note: Failed to read active file', error);
42+
new Notice('Failed to read current note.');
43+
return;
44+
}
45+
46+
const copied = await this.tryCopyTextToClipboard(content);
47+
if (copied) {
48+
new Notice('Copied current note to clipboard.');
49+
} else {
50+
new Notice('Failed to copy note to clipboard.');
51+
}
52+
}
53+
54+
async tryCopyTextToClipboard(text) {
55+
// Strategy 1: Standard Web Clipboard API (preferred)
56+
try {
57+
if (navigator && navigator.clipboard && navigator.clipboard.writeText) {
58+
await navigator.clipboard.writeText(text);
59+
return true;
60+
}
61+
} catch (error) {
62+
console.warn('Copy As Note: navigator.clipboard.writeText failed', error);
63+
}
64+
65+
// Strategy 2: Obsidian clipboard API (if available)
66+
try {
67+
const obsidianClipboard = this.app && this.app.clipboard;
68+
if (obsidianClipboard && obsidianClipboard.writeText) {
69+
await obsidianClipboard.writeText(text);
70+
return true;
71+
}
72+
} catch (error) {
73+
console.warn('Copy As Note: app.clipboard.writeText failed', error);
74+
}
75+
76+
// Strategy 3: Legacy execCommand fallback
77+
try {
78+
const textarea = document.createElement('textarea');
79+
textarea.value = text;
80+
textarea.setAttribute('readonly', '');
81+
textarea.style.position = 'fixed';
82+
textarea.style.left = '-9999px';
83+
textarea.style.top = '0';
84+
textarea.style.opacity = '0';
85+
86+
document.body.appendChild(textarea);
87+
textarea.focus();
88+
textarea.select();
89+
textarea.setSelectionRange(0, textarea.value.length);
90+
91+
const ok = document.execCommand && document.execCommand('copy');
92+
document.body.removeChild(textarea);
93+
return !!ok;
94+
} catch (error) {
95+
console.warn('Copy As Note: execCommand copy fallback failed', error);
96+
return false;
97+
}
98+
}
99+
100+
onunload() {
101+
// No UI elements to clean up; command registration is handled by Obsidian.
102+
}
103+
};
104+

manifest.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": "copy-as-note",
3+
"name": "Copy As Note",
4+
"version": "1.0.0",
5+
"minAppVersion": "0.15.0",
6+
"main": "main.js",
7+
"description": "Adds a mobile-friendly ribbon icon to copy the full raw text of the current note to the clipboard.",
8+
"author": "Obsidian User",
9+
"isDesktopOnly": false
10+
}

styles.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* Ensure the button looks clickable */
2+
.mobile-copy-note-button {
3+
cursor: pointer;
4+
display: flex;
5+
align-items: center;
6+
justify-content: center;
7+
padding: 0 5px;
8+
}
9+
10+
/* Align the icon properly within the status bar height */
11+
.mobile-copy-note-icon {
12+
display: flex;
13+
align-items: center;
14+
}
15+
16+
/* Mobile specific adjustments if necessary */
17+
.is-mobile .mobile-copy-note-button {
18+
/* Mobile status bar items sometimes need extra spacing */
19+
margin-left: 5px;
20+
}

0 commit comments

Comments
 (0)