forked from srz2/obsidian-jira-linker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
256 lines (222 loc) · 7.13 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
interface LocalSettings {
jira_instance_url: string;
local_issue_path: string;
local_issue_info_file: string;
}
const DEFAULT_SETTINGS: LocalSettings = {
jira_instance_url: '',
local_issue_path: '',
local_issue_info_file: '_Info'
}
export default class JiraLinkerPlugin extends Plugin {
settings: LocalSettings;
async onload() {
await this.loadSettings();
// This adds an editor command that can link a Jira issue to the local Jira instance
this.addCommand({
id: 'cmd-link-jira-issue',
name: 'Link Jira issue',
editorCallback: (editor: Editor, view: MarkdownView) => {
const jira_url = this.settings.jira_instance_url;
const content = editor.getSelection();
// Check Jira URL
if (jira_url == ''){
const msg = 'The Jira URL has not been set in settings'
new Notice(msg)
return;
}
// Check for content, ask for it if not selected
if (content == ''){
new JiraIssueInputModal(this.app, (result) => {
if (result !== ''){
const newStr = this.createWebUrl(jira_url, result)
editor.replaceSelection(newStr);
}
})
.setDescription('Enter an issue number which will then be appended to your Jira instance url')
.open();
} else {
const newStr = this.createWebUrl(jira_url, content)
editor.replaceSelection(newStr);
}
}
});
// This adds an editor command that can link a Jira issue to a local issue _Info page
this.addCommand({
id: 'cmd-link-jira-issue-info',
name: 'Link Jira issue to info',
editorCallback: (editor: Editor, view: MarkdownView) => {
const local_issue_path = this.settings.local_issue_path;
const local_issue_main_file = this.settings.local_issue_info_file;
const content = editor.getSelection();
// Check local issue path
if (local_issue_path == ''){
const msg = 'The local issue path has not been set in settings'
new Notice(msg)
return;
}
// Check local issue main file
if (local_issue_main_file == ''){
const msg = 'The local issue main file has not been set in settings'
new Notice(msg)
return;
}
if (content == ''){
new JiraIssueInputModal(this.app, (result) => {
if (result !== ''){
const newStr = this.createLocalUri(local_issue_path, result, local_issue_main_file)
editor.replaceSelection(newStr);
}
})
.setDescription('Enter an issue number to be constructed into your local issue path')
.open();
} else {
// Replace content with local _Issue relative path
const newStr = this.createLocalUri(local_issue_path, content, local_issue_main_file)
editor.replaceSelection(newStr);
}
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new JiraLinkerSettingTab(this.app, this));
}
/**
* Create a URL for linking to Jira web instance
* @param {string} url The Jira instance
* @param {string} jira_issue The Jira issue number (e.g.: JIRA-123)
* @returns {string} A fully formed markdown Url representing a Jira with the issue as a label
*/
createWebUrl(url: string, jira_issue: string): string {
const jira_url = this.settings.jira_instance_url;
return `[${jira_issue}](${jira_url}/browse/${jira_issue})`
}
/**
* Create a Uri which points to a local file
* @param {string} local_path The local path for the local issues in obsidian
* @param {string} jira_issue The Jira issue number (e.g.: JIRA-123)
* @param {string} main_file_name The name of the main file
* @returns {string} A fully formed obsidian markdown Uri for referencing an issue
*/
createLocalUri(local_path: string, jira_issue: string, main_file_name: string) : string {
return `[[${local_path}/${jira_issue}/${main_file_name}|${jira_issue}]]`
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class JiraIssueInputModal extends Modal {
title: string;
description: string;
result: string;
onSubmit: (result: string) => void;
constructor(app: App, onSubmit: (result: string) => void) {
super(app);
this.title = 'Enter your Jira issue';
this.description = 'Type in a jira issue number';
this.onSubmit = onSubmit;
this.containerEl.addEventListener('keydown', (e) =>{
if (e.key === 'Enter') {
if (this.result !== undefined && this.result !== ''){
this.close();
this.onSubmit(this.result);
}
} else if (e.key == 'Escape') {
this.close();
}
});
}
setTitle(newTitle: string): JiraIssueInputModal {
this.title = newTitle;
return this;
}
setDescription(newDescription: string): JiraIssueInputModal {
this.description = newDescription;
return this;
}
onOpen() {
const { contentEl } = this;
contentEl.createEl("h1", { text: this.title });
contentEl.createEl("p", {text: this.description})
new Setting(contentEl)
.setName("Jira Issue")
.addText((text) =>
text.onChange((value) => {
this.result = value
}));
new Setting(contentEl)
.addButton((btn) =>
btn
.setButtonText("Link Issue")
.setCta()
.onClick(() => {
this.close();
this.onSubmit(this.result);
}));
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}
class JiraLinkerSettingTab extends PluginSettingTab {
plugin: JiraLinkerPlugin;
constructor(app: App, plugin: JiraLinkerPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName('Jira Instance URL')
.setDesc('The domain URL for your Jira instance')
.addText(text => text
.setPlaceholder('Jira URL')
.setValue(this.plugin.settings.jira_instance_url)
.onChange(async (value) => {
if (value.endsWith('/')) {
value = value.slice(0, -1);
}
this.plugin.settings.jira_instance_url = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Local Issue Path')
.setDesc('The relative path to your issue folder')
.addText(text => text
.setPlaceholder('Relative issue path')
.setValue(this.plugin.settings.local_issue_path)
.onChange(async (value) => {
if (value.endsWith('/')) {
value = value.slice(0, -1);
}
this.plugin.settings.local_issue_path = value;
await this.plugin.saveSettings();
}));
// Create description for "Local Issue Main File Name" Setting
const settingMainFileNameDesc = document.createDocumentFragment();
settingMainFileNameDesc.append(
'The "Main" file name for linking to local issues (e.g.: ',
settingMainFileNameDesc.createEl('i', {
text: 'issues/JIRA-123/_Info'
}),
')'
)
new Setting(containerEl)
.setName('Local Issue Main File Name')
.setDesc(settingMainFileNameDesc)
.addText(text => text
.setPlaceholder('Local Issue Main File')
.setValue(this.plugin.settings.local_issue_info_file)
.onChange(async (value) => {
this.plugin.settings.local_issue_info_file = value;
await this.plugin.saveSettings();
}));
}
}