Add a menu item to copy shared link

This commit is contained in:
Alan Grainger 2023-09-12 20:45:30 +02:00
parent c8d0b5b562
commit bbf71b6d06
6 changed files with 63 additions and 10 deletions

View file

@ -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",

4
package-lock.json generated
View file

@ -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",

View file

@ -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": {

View file

@ -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)
}
}
}

View file

@ -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
}
}

View file

@ -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"
}