|
| 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 | + |
0 commit comments