-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
426 lines (382 loc) · 13.9 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import { App, Editor, MarkdownView, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import {JiraInstanceSuggestModal} from 'Modals/JiraInstanceSuggestModal'
import { JiraIssueInputModal } from 'Modals/JiraIssueInputModal';
import { IJiraInstanceUrl } from 'Models/JiraInstanceUrl';
interface LocalSettings {
jira_instance_url: string;
jira_instance_urls: IJiraInstanceUrl[];
local_issue_path: string;
local_issue_info_file: string;
input_modal_settings: {
insert_newline_after_return: boolean;
},
issue_creation_settings: {
create_issue_inside_project_folder: boolean
}
}
const DEFAULT_SETTINGS: LocalSettings = {
/**
* @deprecated In version 1.0.X, this only supported one url,
* instead, use the jira_instance_urls array instead
* Refer to deprecation-notes for more info
*/
jira_instance_url: '',
jira_instance_urls: [],
local_issue_path: '',
local_issue_info_file: '_Info',
input_modal_settings: {
insert_newline_after_return: true
},
issue_creation_settings: {
create_issue_inside_project_folder: true
}
}
export default class JiraLinkerPlugin extends Plugin {
settings: LocalSettings;
/**
* Handling for if user used older versions containing only one url instance
* this will load it to the array list.
*
* See deprecation-notes for more info
*
* @private
*/
async fixV1_2_0(){
if (this.settings.jira_instance_url !== ''){
this.settings.jira_instance_urls.push({IsDefault: false, Title: '', Url: this.settings.jira_instance_url})
this.settings.jira_instance_url = ''
await this.saveSettings();
}
}
/**
* This contains migrations of data structures to allow older
* versions to be upgraded to the current feature set
*
* @private
*/
async applyVersionChanges(){
await this.fixV1_2_0()
}
async onload() {
await this.loadSettings();
// Do not remove, this is for updating
// legacy versions to the current feature set
await this.applyVersionChanges()
// 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: async (editor: Editor, view: MarkdownView) => {
if (this.settings.jira_instance_urls.length > 1) {
// Get suggestion
const suggestor = new JiraInstanceSuggestModal(this.app, this.settings.jira_instance_urls, (instance) => {
this.insertJiraLink(instance.Url, editor)
})
suggestor.setPlaceholder('Select a Jira instance')
suggestor.open()
} else {
const instanceUrl = this.settings.jira_instance_urls.length == 0 ? "" : this.settings.jira_instance_urls[0].Url;
this.insertJiraLink(instanceUrl, editor);
}
}
});
// This adds an editor command that can link a Jira issue to the local Jira instance
this.addCommand({
id: 'cmd-link-jira-issue-default-instance',
name: 'Link Jira issue (default instance)',
editorCallback: async (editor: Editor, view: MarkdownView) => {
// Check if no instances exists
if (this.settings.jira_instance_urls.length == 0) {
this.insertJiraLink("", editor);
} else {
// Find the default instance
let foundIndex = -1;
const defaultInstance = this.settings.jira_instance_urls.find((x, index, instance) => {
// Record index if found
const condition = x.IsDefault
if (condition){
foundIndex = index
}
return condition
})
// If no defeault instance found, use the first listed instance
?? this.settings.jira_instance_urls[0];
// If no default is found, alert the user
if (foundIndex == -1){
new Notice(`No default Jira Instance configured, using the first instance available: ${
this.settings.jira_instance_urls[0].Title !== "" ?
this.settings.jira_instance_urls[0].Title :
this.settings.jira_instance_urls[0].Url
}`)
}
// Execute the Jira Link on the default instance
this.insertJiraLink(defaultInstance.Url, editor)
}
}
});
// 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
// Note: Issue Main File is optional if project folder is disabled but required if enabled
if (local_issue_main_file == '' &&
this.settings.issue_creation_settings.create_issue_inside_project_folder ){
const msg = 'The local issue main file has not been set in settings'
new Notice(msg)
return;
}
if (content == ''){
new JiraIssueInputModal(this.app, this.settings.input_modal_settings.insert_newline_after_return, (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));
}
insertJiraLink(jira_url: string, editor: Editor){
// Check Jira URL
if (jira_url == ''){
const msg = 'The Jira URL has not been set in settings'
new Notice(msg)
return;
}
const content = editor.getSelection();
// Check for content, ask for it if not selected
if (content == ''){
new JiraIssueInputModal(this.app, this.settings.input_modal_settings.insert_newline_after_return, (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);
}
}
/**
* Create a URL for linking to Jira web instance
* @param {string} url The Jira instance url
* @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(jira_url: string, jira_issue: string): string {
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 {
if (this.settings.issue_creation_settings.create_issue_inside_project_folder){
// ie - issues/123/_info
return `[[${local_path}/${jira_issue}/${main_file_name}|${jira_issue}]]`
} else {
// ie - issues/123_info
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 JiraLinkerSettingTab extends PluginSettingTab {
plugin: JiraLinkerPlugin;
constructor(app: App, plugin: JiraLinkerPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
this.add_jira_instance_settings(containerEl);
this.add_jira_local_issue_settings(containerEl);
this.add_misc_settings(containerEl);
}
add_jira_instance_settings(containerEl : HTMLElement) {
const desc = document.createDocumentFragment();
const content = document.createElement('div')
content.innerHTML = `
<p>The list of domain URLs for your Jira instances</p>
<p>Denote your default instance by selecting the <strong>Set As Default</strong> button. If <span style="text-decoration: underline;">no default is selected</span>, the <strong>first</strong> instance will be used</p>
</br>
<em>Note: A title is optional for your instances, but recommended for organization.</em>
`
desc.append(content)
new Setting(containerEl)
.setName('Jira Instances')
.setDesc(desc)
this.plugin.settings.jira_instance_urls.forEach((url, index) => {
const s = new Setting(containerEl);
// Remove the name and description since we aren't using them. This
// plus the css class `.setting-item-info:empty` will get us more space
s.nameEl.remove();
s.descEl.remove();
// Conditionally add Default button
if (!this.plugin.settings.jira_instance_urls[index].IsDefault){
s.addButton((cb) => {
cb.setButtonText("Set As Default")
cb.onClick(cb => {
for (let c = 0; c < this.plugin.settings.jira_instance_urls.length; c++){
this.plugin.settings.jira_instance_urls[c].IsDefault = false;
}
this.plugin.settings.jira_instance_urls[index].IsDefault = true;
this.plugin.saveSettings();
this.display();
})
})
} else {
s.addButton((cb) => {
cb.setButtonText('Default')
cb.buttonEl.className = 'assigned-default-button';
cb.onClick(cb => {
this.plugin.settings.jira_instance_urls[index].IsDefault = false;
this.plugin.saveSettings();
this.display();
})
})
}
s.addText((cb) => {
cb.setPlaceholder('Add an optional title')
cb.setValue(this.plugin.settings.jira_instance_urls[index].Title)
cb.onChange(async (value) => {
this.plugin.settings.jira_instance_urls[index].Title = value
await this.plugin.saveSettings();
})
})
.addText((cb) => {
cb.setPlaceholder('Example: https://myinstance.atlassian.net')
cb.setValue(this.plugin.settings.jira_instance_urls[index].Url);
cb.onChange(async (value) => {
if (value.endsWith('/')) {
value = value.slice(0, -1);
}
this.plugin.settings.jira_instance_urls[index].Url = value
await this.plugin.saveSettings();
})
cb.inputEl.classList.add("setting_jira_instance_url")
})
.addExtraButton((cb) => {
cb.setIcon('cross')
.setTooltip('delete instance')
.onClick(async () => {
this.plugin.settings.jira_instance_urls.splice(index, 1);
await this.plugin.saveSettings();
// Force refresh display
this.display();
})
})
})
new Setting(containerEl).addButton((cb) => {
cb.setButtonText("Add new Jira instance")
.setCta()
.onClick(async () => {
this.plugin.settings.jira_instance_urls.push({
Title: '',
IsDefault: false,
Url: ''
});
await this.plugin.saveSettings();
// // Force refresh
this.display();
});
});
}
add_jira_local_issue_settings(containerEl: HTMLElement) : void {
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();
}));
}
add_misc_settings(containerEl: HTMLElement): void {
// New Line Insertion
new Setting(containerEl)
.setName('New Line Insertion')
.setDesc('Allow New Line After Pressing \'Return\' on Jira Issue Insertion')
.addToggle(newValue => newValue
.setValue(this.plugin.settings.input_modal_settings.insert_newline_after_return)
.onChange(async (value) => {
this.plugin.settings.input_modal_settings.insert_newline_after_return = value;
await this.plugin.saveSettings();
}))
// Create issue inside project folder
const desc = document.createDocumentFragment();
const content = document.createElement('div')
content.innerHTML = `
<p>When linking with a local issue, a project folder with the issue name is created. Otherwise a note will be created instead the Local Issue Path. If disabled, "Local Issue Main File Name" becomes optional and can be left blank</p>
<p><strong>With</strong> Project Folder: issue/PROJ-123/_Info</p>
<p><strong>Without</strong> Project Folder: issue/PROJ-123_Info</p>
`
desc.append(content)
new Setting(containerEl)
.setName('Create Project Folder for Local Issues')
.setDesc(desc)
.addToggle(newValue => newValue
.setValue(this.plugin.settings.issue_creation_settings.create_issue_inside_project_folder)
.onChange(async (value) => {
this.plugin.settings.issue_creation_settings.create_issue_inside_project_folder = value;
await this.plugin.saveSettings();
})
)
}
}