generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
108 lines (87 loc) · 2.43 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
import { App, Editor, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { getStoryTitle } from "./pivotal";
import { PT_REGEX, PT_URL_LENGTH } from "./constants";
interface PTLinkSettings {
ptToken: string;
}
interface StoryJson {
name: string;
estimate: number;
url: string;
}
const DEFAULT_SETTINGS: PTLinkSettings = {
ptToken: ''
}
export default class PTLink extends Plugin {
settings: PTLinkSettings;
async onload() {
await this.loadSettings();
const getLinkTitle = (storyJson: StoryJson): string => {
return `[${storyJson?.name} (${storyJson?.estimate || '??'})](${storyJson?.url})`
}
// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: 'update-pt-link',
name: 'Update PT Link',
editorCallback: (editor: Editor): void => {
const url = editor.getSelection();
getStoryTitle(this.settings.ptToken, url).then((storyJson) => {
editor.replaceSelection(getLinkTitle(storyJson));
});
}
});
this.registerEvent(
this.app.workspace.on(
'editor-paste',
(
event: ClipboardEvent,
editor: Editor
) => {
const url = event.clipboardData?.getData('text/plain') || '';
if (!PT_REGEX.test(url)) {
return;
}
getStoryTitle(this.settings.ptToken, url).then((storyJson) => {
const endCursor = editor.getCursor();
const startCursor = {...endCursor, ch: endCursor.ch - PT_URL_LENGTH}
editor.replaceRange(
getLinkTitle(storyJson),
startCursor,
endCursor
);
});
},
),
);
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SettingTab(this.app, this));
}
onunload() {
}
async loadSettings(): Promise<void> {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
return await this.saveData(this.settings);
}
}
class SettingTab extends PluginSettingTab {
plugin: PTLink;
constructor(app: App, plugin: PTLink) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName('Pivotal Tracker Token')
.addText(text => text
.setPlaceholder('Enter your token')
.setValue(this.plugin.settings.ptToken)
.onChange(async (value) => {
this.plugin.settings.ptToken = value;
await this.plugin.saveSettings();
}));
}
}