-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
133 lines (116 loc) · 3.81 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import type { AccountInfo } from '@azure/msal-common'
import { type App, type Editor, MarkdownView, Notice, Plugin, type PluginManifest } from 'obsidian'
import { mount } from 'svelte'
import { AuthProvider } from './src/auth-provider'
import { GraphClient } from './src/graph-client'
import { getCodeBlock } from './src/markdown-utils'
import { OneDriveWidget } from './src/onedrive-widget'
import { queryClient } from './src/onedrive-widget/query-client'
import { OneDriveSettingTab } from './src/settings-tab'
export interface OneDrivePluginSettings {
oneDriveDirectory: string
showPreview: boolean
conflictBehavior: 'rename' | 'fail' | 'replace'
}
const DEFAULT_SETTINGS: OneDrivePluginSettings = {
oneDriveDirectory: 'Obsidian',
showPreview: false,
conflictBehavior: 'fail',
}
type Callback = (value: typeof DEFAULT_SETTINGS) => void
export default class OneDrivePlugin extends Plugin {
account: AccountInfo | null = null
settings!: OneDrivePluginSettings
authProvider: AuthProvider
client!: GraphClient
pluginPath: string
callbacks: Callback[] = []
constructor(app: App, manifest: PluginManifest) {
super(app, manifest)
this.pluginPath = manifest.dir ?? ''
this.authProvider = new AuthProvider()
}
async onload() {
await this.loadSettings()
this.account = await this.authProvider.init()
this.client = new GraphClient(this.authProvider)
this.app.workspace.on('editor-drop', async (evt, editor) => {
if (evt.defaultPrevented) return
const file = evt.dataTransfer?.files[0]
if (file?.type === 'application/pdf') {
evt.preventDefault()
await this.uploadFile(file, editor)
}
})
this.addSettingTab(new OneDriveSettingTab(this))
this.registerMarkdownCodeBlockProcessor('onedrive', (source, el) => {
mount(OneDriveWidget, { target: el, props: { source }, context: new Map([['plugin', this]]) })
})
if (__DEV__) {
this.registerMarkdownCodeBlockProcessor('onedrive-dev', async (source, el) => {
const { default: OneDriveWidgetDev } = await import(
'./src/onedrive-widget/onedrive-widget-dev.svelte'
)
mount(OneDriveWidgetDev, {
target: el,
props: { source },
context: new Map([['plugin', this]]),
})
})
}
this.registerObsidianProtocolHandler('onedrive', (path) => {
this.authProvider.handleRedirect(path.hash)
})
this.addCommand({
id: 'upload-file',
name: 'Upload file',
callback: () => {
const input = document.createElement('input')
input.type = 'file'
input.onchange = (_) => {
if (input.files) {
const files = Array.from(input.files)
const view = this.app.workspace.getActiveViewOfType(MarkdownView)
if (view && files.length > 0) {
this.uploadFile(files[0], view.editor)
}
}
}
input.click()
},
})
}
onunload() {}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData())
}
async saveSettings() {
await this.saveData(this.settings)
for (const callback of this.callbacks) {
callback(this.settings)
}
}
subscribe(callback: Callback) {
this.callbacks.push(callback)
}
async uploadFile(file: File, editor: Editor) {
new Notice('Start upload')
const initialCursor = editor.getCursor()
const title = file.name.replace(/.[^.]+$/, '') // Remove file extension
const placeholder = getCodeBlock({ title })
const placeholderLineCount = placeholder.split('\n').length
editor.replaceRange(placeholder, initialCursor)
const driveItem = await this.client.uploadFile(file, this.settings)
if (driveItem?.id) {
queryClient.setQueryData(['file', driveItem.id], driveItem)
new Notice('File uploaded')
const record = { id: driveItem.id, title }
editor.replaceRange(getCodeBlock(record), initialCursor, {
line: initialCursor.line + placeholderLineCount,
ch: 0,
})
} else {
new Notice('File upload failed')
}
}
}