forked from kbravh/obsidian-tweet-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
111 lines (92 loc) · 4.6 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
import {addIcon, Editor, MarkdownView, Plugin} from 'obsidian'
import {DEFAULT_SETTINGS, TTMSettings, TTMSettingTab} from 'src/settings'
import {pasteTweet} from 'src/util'
import {Tweet} from 'src/types/tweet'
import {TweetCompleteModal} from 'src/TweetCompleteModal'
import {TweetUrlModal} from 'src/TweetUrlModal'
import {createPollManager, PollManager} from 'src/pollManager'
interface PasteFunction {
(this: Plugin, ev: ClipboardEvent): void
}
export default class TTM extends Plugin {
settings: TTMSettings
currentTweet: Tweet
tweetMarkdown = ''
bearerToken: string
tweetComplete: TweetCompleteModal
pasteFunction: PasteFunction
pollManager: PollManager
pollIntervalID: number //should be ReturnType<typeof window.setInterval> but that seems to return the wrong type
async onload(): Promise<void> {
console.info('Loading Tweet to Markdown')
addIcon(
'twitter',
'<path fill="currentColor" stroke="currentColor" d="M50.583 35.174c1.361-.402 3.076-.258 4.5-.44a58.326 58.326 0 007.834-1.572c8.806-2.383 17.139-6.943 23.833-13.147 2.062-1.91 4.303-3.939 5.917-6.257h.083c3.816 7.251 3.671 15.886-1.344 22.583-1.275 1.702-3.033 3.74-4.99 4.667v.083c3.307-.027 6.312-1.211 9.25-2.583 0 7.586-4.087 14.845-10.916 18.354-1.642.844-3.549 1.747-5.417 1.896v.083c2.866.807 6.173 1.05 9.084.333-.173 1.982-1.734 4.281-2.886 5.834-3.764 5.07-9.756 8.333-16.114 8.333 1.602 1.471 3.6 2.649 5.5 3.692a41.641 41.641 0 0017.166 5.05c2.5.172 4.93-.152 7.417-.159v.084c-2.239 1.191-4.352 2.578-6.667 3.64-4.485 2.06-9.222 3.599-14.083 4.475-3.516.634-6.939.718-10.5.718-4.04 0-7.936-.12-11.917-.9-14.826-2.908-27.945-11.673-36.221-24.35-4.66-7.138-7.832-15.174-9.242-23.583-.465-2.772-.588-5.543-.78-8.334-.066-.976.374-2.43.027-3.33-.168-.432-.769-.727-1.117-1.003a50.039 50.039 0 01-2.5-2.098c-2.432-2.2-4.773-4.77-6.5-7.569l4.167 1.524 7.5 1.643v-.083c-1.046-.452-1.967-1.27-2.834-1.994-2.753-2.298-5.13-5.684-6.083-9.173 1.223.36 2.427 1.252 3.583 1.812 1.972.954 4.064 1.691 6.167 2.295.88.253 1.994.713 2.917.69.46-.012.843-.552 1.166-.833.89-.77 1.769-1.517 2.75-2.171 2.52-1.68 5.43-2.732 8.417-3.174 9.758-1.444 19.486 4.708 22.414 14.13.706 2.271 1.075 4.79.912 7.168-.085 1.234-.393 2.441-.493 3.666z"/>'
)
await this.loadSettings()
// clean up null string default settings
if (this.settings.noteLocation === null) {
this.settings.noteLocation = ''
console.info('Cleaning up note location setting.')
await this.saveSettings()
}
if (this.settings.assetLocation === null) {
this.settings.assetLocation = ''
console.info('Cleaning up asset location setting.')
await this.saveSettings()
}
// clean up deprecated textOnly option
if (this.settings.textOnly) {
this.settings.includeImages = false
this.settings.includeLinks = false
await this.saveSettings()
}
const pasteTweetWrapper = (
event: ClipboardEvent,
editor: Editor,
markdownView: MarkdownView
): Promise<void> => {
return pasteTweet(event, editor, markdownView, this)
}
// add link paste event listener
this.app.workspace.on('editor-paste', pasteTweetWrapper, this)
// add twitter icon
this.addRibbonIcon('twitter', 'Tweet to Markdown', () => {
new TweetUrlModal(this.app, this).open()
})
this.addCommand({
id: 'open-tweet-url-modal',
name: 'Download Tweet from URL',
callback: () => {
new TweetUrlModal(this.app, this).open()
},
})
this.addSettingTab(new TTMSettingTab(this.app, this))
this.pollManager = createPollManager(this.app, this)
}
onunload(): void {
console.info('unloading Tweet to Markdown')
}
async loadSettings(): Promise<void> {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData())
}
async saveSettings(): Promise<void> {
await this.saveData(this.settings)
if(this.settings.pollEnabled) {
if(this.pollIntervalID)
clearInterval(this.pollIntervalID) //registerInterval will still try to cancel it when unloading, but so be it.
this.pollManager.pollRun();
var numHours = 24 //by default
switch(this.settings.pollFrequency) {
case 'monthly': { numHours = 24*30 }
case 'weekly': { numHours = 24*7 }
case 'twoDaily': { numHours = 24*2 }
case 'daily': { numHours = 24*1 }
case 'twiceDaily': { numHours = 24/2 }
case 'hourly': { numHours = 1 }
}
this.pollIntervalID = window.setInterval(() => this.pollManager.pollRun(), 1000*60*60*numHours)
this.registerInterval(this.pollIntervalID)
}
}
}