diff --git a/manifest.json b/manifest.json index 53f0971..360423f 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "share-note", "name": "Share Note", - "version": "0.2.3", + "version": "0.2.4", "minAppVersion": "0.15.0", "description": "Share a note publicly with full styling. Data is stored encrypted on the server and only you have the key.", "author": "Alan Grainger", diff --git a/package-lock.json b/package-lock.json index ad081f7..e391016 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "share-note", - "version": "0.2.0", + "version": "0.2.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "share-note", - "version": "0.2.0", + "version": "0.2.4", "license": "MIT", "devDependencies": { "@types/node": "^16.11.6", diff --git a/package.json b/package.json index 99224f4..8f9db50 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "share-note", - "version": "0.2.3", + "version": "0.2.4", "description": "Share a note publicly with full styling. Data is stored encrypted on the server and only you have the key.", "main": "main.js", "scripts": { diff --git a/src/main.ts b/src/main.ts index 18a3cc1..7fe68bb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import { Plugin } from 'obsidian' +import { Plugin, TFile } from 'obsidian' import { DEFAULT_SETTINGS, ShareSettings, ShareSettingsTab } from './settings' import Note from './note' import API from './api' @@ -53,6 +53,21 @@ export default class SharePlugin extends Plugin { await this.uploadNote(true) } }) + + // Add a menu item to the 3-dot editor menu + this.registerEvent( + this.app.workspace.on('file-menu', (menu, file) => { + if (file instanceof TFile && file.extension === 'md') { + menu.addItem((item) => { + item.setIcon('share-2') + item.setTitle('Copy shared link') + item.onClick(async () => { + await this.copyShareLink(file) + }) + }) + } + }) + ) } onunload () { @@ -67,13 +82,21 @@ export default class SharePlugin extends Plugin { await this.saveData(this.settings) } - async uploadNote (forceUpload = false) { + /** + * Upload a note. + * @param forceUpload - Optionally force an upload of all related assets + * @param forceClipboard - Optionally copy the link to the clipboard, regardless of the user setting + */ + async uploadNote (forceUpload = false, forceClipboard = false) { const note = new Note(this) if (forceUpload) { note.forceUpload() } + if (forceClipboard) { + note.forceClipboard() + } try { - await note.parse() + await note.share() } catch (e) { if (e.message === 'Unknown error') { new StatusMessage('There was an error uploading the note, please try again.', StatusType.Error) @@ -81,4 +104,21 @@ export default class SharePlugin extends Plugin { } note.status.hide() // clean up status just in case } + + /** + * Copy the share link to the clipboard. The note will be shared first if neccessary. + * @param file + */ + async copyShareLink (file: TFile) { + const meta = this.app.metadataCache.getFileCache(file) + const shareLink = meta?.frontmatter?.[this.settings.yamlField + '_link'] + if (shareLink) { + // The note is already shared, copy the link to the clipboard + await navigator.clipboard.writeText(shareLink) + new StatusMessage('Shared link copied to clipboard') + } else { + // The note is not already shared, share it first and copy the link to the clipboard + await this.uploadNote(false, true) + } + } } diff --git a/src/note.ts b/src/note.ts index 3f4af6a..2ca2559 100644 --- a/src/note.ts +++ b/src/note.ts @@ -23,6 +23,7 @@ export default class Note { meta: CachedMetadata | null yamlField: YamlField isForceUpload: boolean + isForceClipboard: boolean outputFile: Template constructor (plugin: SharePlugin) { @@ -37,7 +38,7 @@ export default class Note { } } - async parse () { + async share () { const startMode = this.leaf.getViewState() const previewMode = this.leaf.getViewState() previewMode.state.mode = 'preview' @@ -167,10 +168,11 @@ export default class Note { frontmatter[this.yamlField.link] = shareLink frontmatter[this.yamlField.updated] = moment().format() }) - if (this.plugin.settings.clipboard) { + if (this.plugin.settings.clipboard || this.isForceClipboard) { // Copy the share link to the clipboard await navigator.clipboard.writeText(shareLink) shareMessage += ' and the link is copied to your clipboard' + this.isForceClipboard = false } } @@ -266,7 +268,17 @@ export default class Note { return Object.keys(mimes).find(x => mimes[x].includes((mimeType || '').toLowerCase())) } + /** + * Force all related assets to upload again + */ forceUpload () { this.isForceUpload = true } + + /** + * Copy the shared link to the clipboard, regardless of the user setting + */ + forceClipboard () { + this.isForceClipboard = true + } } diff --git a/versions.json b/versions.json index 893a0d5..3a7d654 100644 --- a/versions.json +++ b/versions.json @@ -9,5 +9,6 @@ "0.1.10": "0.15.0", "0.2.0": "0.15.0", "0.2.1": "0.15.0", - "0.2.3": "0.15.0" + "0.2.3": "0.15.0", + "0.2.4": "0.15.0" }