generated from obsidianmd/obsidian-sample-plugin
/
main.ts
195 lines (171 loc) · 6.29 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
import { App, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { getAPI } from 'obsidian-dataview';
import PeopleSuggest from 'suggest';
import { logger } from 'utils';
interface PeopleLinkPluginSettings {
triggerPrefix: string;
dataviewSource: string;
suggestionsLimit: number;
createPersonIfNotExists: boolean;
newPersonLocation: string;
openNewPersonInSplitPane: boolean;
}
const DEFAULT_SETTINGS: PeopleLinkPluginSettings = {
triggerPrefix: '@',
dataviewSource: '"People"',
suggestionsLimit: 5,
createPersonIfNotExists: true,
newPersonLocation: 'People',
openNewPersonInSplitPane: true,
}
export default class PeopleLinkPlugin extends Plugin {
settings: PeopleLinkPluginSettings;
peopleSuggest: PeopleSuggest;
async onload() {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require('./manifest.json')
console.log(`Plugin loading: ${pkg.id} ${pkg.version} BUILD_ENV=${process.env.BUILD_ENV}`)
await this.loadSettings();
this.peopleSuggest = new PeopleSuggest(this.app, this)
this.registerEditorSuggest(this.peopleSuggest);
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new PeopleLinkSettingTab(this.app, this));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class PeopleLinkSettingTab extends PluginSettingTab {
plugin: PeopleLinkPlugin;
constructor(app: App, plugin: PeopleLinkPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
const warnings = []
if (!getAPI()) {
warnings.push(`To ensure proper functionality, you must install <a href="obsidian://show-plugin?id=dataview">Obsidian Dataview</a> alongside this plugin.`)
}
const nldatesPlugin = this.app.plugins.plugins['nldates-obsidian']
if (nldatesPlugin?.settings.isAutosuggestEnabled && nldatesPlugin?.settings.autocompleteTriggerPhrase === this.plugin.settings.triggerPrefix) {
warnings.push(`Trigger prefix ${this.plugin.settings.triggerPrefix} is conflict with <a href="obsidian://show-plugin?id=nldates-obsidian">Natural Language Dates</a>. Please change one of them, or disable the autosuggest feature in that plugin.`)
}
if (warnings.length) {
containerEl.createEl('div').innerHTML = `
<div style="color: var(--color-red); font-size: 1.2em; border: 1px solid var(--color-red); padding: 1em; padding-bottom: 0; margin: 0.5em 0 1.5em">
<b>Warnings</b>
<ul style="padding-left: 1em;">
${warnings.map(warning => `<li style="margin-bottom: .5em;">${warning}</li>`).join('')}
</ul>
</div>`;
}
containerEl.createEl('h2', {text: 'People Link Settings'});
new Setting(containerEl)
.setName('Trigger Prefix')
.setDesc('Character(s) that will cause the people autosuggest to open')
.addText(text => text
.setValue(this.plugin.settings.triggerPrefix)
.onChange(async (value) => {
this.plugin.settings.triggerPrefix = value;
await this.plugin.saveSettings();
}));
const dataviewSetting = new Setting(containerEl)
.setName('Dataview Source')
.setDesc(`The dataview source is used to identify notes for people. It can be folders, tags, files, or a combination of them. Check more information at Dataview documentation of `);
dataviewSetting.descEl.createEl('a', {
text: 'Sources',
attr: {
href: 'https://blacksmithgu.github.io/obsidian-dataview/reference/sources/'
}
})
const dataviewPreview = dataviewSetting.settingEl.createEl('div', {
attr: {
'style': 'margin: -0.25em 0 0.75em; padding: 0.75em; background: var(--color-base-20);'
}
})
dataviewSetting.settingEl.insertAdjacentElement('afterend', dataviewPreview)
dataviewPreview.createEl('div', {
text: 'Query preview',
attr: {
'style': 'padding-bottom: 0.3em;'
}
})
const dataviewPreviewContent = dataviewPreview.createEl('div', {
attr: {
'style': 'font-size: .8em; font-family: monospace; white-space: pre-wrap;'
}
})
const updateDataviewPreview = () => {
const dv = getAPI()
let html = ''
if (dv) {
try {
const result = dv.pages(this.plugin.settings.dataviewSource)
html = `Success, got ${result.length} people in result`
logger.debug('dataview result', result)
} catch (err) {
html = `${err}`
}
} else {
html = `Error: could not get dataview API`
}
dataviewPreviewContent.innerHTML = html
}
dataviewSetting
.addText(text => text
.setValue(this.plugin.settings.dataviewSource)
.onChange(async (value) => {
this.plugin.settings.dataviewSource = value;
this.plugin.peopleSuggest.setSuggestionsCacheValidity(false)
updateDataviewPreview()
await this.plugin.saveSettings();
}));
updateDataviewPreview()
new Setting(containerEl)
.setName('Suggestions Limit')
.setDesc(`The maximum number of suggestions to show.`)
.addText(text => {
text
.setValue(this.plugin.settings.suggestionsLimit.toString())
.onChange(async (value) => {
this.plugin.settings.suggestionsLimit = parseInt(value);
await this.plugin.saveSettings();
});
text.inputEl.setAttribute('type', 'number');
})
new Setting(containerEl)
.setName('Create person if not exists')
.setDesc(`Create a person when press enter if one doesn't already exist.`)
.addToggle(text => text
.setValue(this.plugin.settings.createPersonIfNotExists)
.onChange(async (value) => {
this.plugin.settings.createPersonIfNotExists = value
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('New person location')
.setDesc(`New person will be created at this folder location.`)
.addText(text => text
.setValue(this.plugin.settings.newPersonLocation)
.onChange(async (value) => {
this.plugin.settings.newPersonLocation = value
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Open new person in split pane')
.setDesc(`If disabled, new person will be opened in a new tab.`)
.addToggle(text => text
.setValue(this.plugin.settings.openNewPersonInSplitPane)
.onChange(async (value) => {
this.plugin.settings.openNewPersonInSplitPane = value
await this.plugin.saveSettings();
}));
}
}