generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
183 lines (152 loc) · 5.78 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
import { App, Editor, MarkdownView, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { findCycle } from './cycle';
import { ObjType, tryExpandPosToMachADate } from './expandToDate';
import { Logger, LogLevel } from './logger';
interface CtrlXASettings {
// list of cycles of words
mySetting: string[][];
// logging level for this plugin, default is INFO
// possible values are DEBUG, INFO, WARN, ERROR, NONE
loggingLevel: string;
}
// These are the default settings to inspire user.
// first list are callouts, see https://help.obsidian.md/Editing+and+formatting/Callouts
const DEFAULT_SETTINGS: CtrlXASettings = {
mySetting: [
["note","abstract","info","todo","tip","success","question","warning","failure","danger","bug"],
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
["true", "false"],
["yes", "no"],
['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'],
['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'],
['verbose', 'debug', 'info', 'warn', 'error', 'fatal']
],
loggingLevel: 'INFO',
}
const logger= new Logger(LogLevel.INFO);
export default class CtrlXAPlugin extends Plugin {
settings: CtrlXASettings;
async onload() {
logger.info('Loading plugin');
await this.loadSettings();
// This adds an editor command to cycle up
// e.g. : from "Monday" to "Tuesday", "January" to "February"
this.addCommand({
id: 'cycle-up',
name: 'Cycle up',
editorCallback: (editor: Editor, _view: MarkdownView) => {
cycle(editor, 1, this.settings.mySetting);
}
});
// This adds an editor command to cycle down
// e.g. : from "Tuesday" to "Monday"
this.addCommand({
id: 'cycle-down',
name: 'Cycle down',
editorCallback: (editor: Editor, _view: MarkdownView) => {
cycle(editor, -1, this.settings.mySetting);
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
// especially the lists contents
this.addSettingTab(new CtrlXASettingTab(this.app, this));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
// trigger spaces in settings.mySetting
for (let i = 0; i < this.settings.mySetting.length; i++) {
this.settings.mySetting[i] = this.settings.mySetting[i].map(item => item.trim());
}
// logging settings.mySetting
logger.info('Settings loaded and trimmed');
logger.info('Log level is ' + this.settings.loggingLevel);
logger.setLogLevel(this.settings.loggingLevel);
for (let i = 0; i < this.settings.mySetting.length; i++) {
logger.debug('settings.mySetting[' + i + '] = ' + this.settings.mySetting[i]);
}
}
async saveSettings() {
await this.saveData(this.settings);
}
}
function cycle(parEditor: Editor, parDirection: number, parCycles: string[][]) {
const curLine = parEditor.getLine(parEditor.getCursor().line);
const fromPos = parEditor.wordAt(parEditor.getCursor())?.from.ch ?? -1;
const toPos = parEditor.wordAt(parEditor.getCursor())?.to.ch ?? -1;
const wordAt = parEditor.wordAt(parEditor.getCursor());
const obj:ObjType = {
curLine,
fromPos,
toPos,
};
logger.debug("Line >" + curLine + "<");
logger.debug("Word from pos >" + fromPos + "<");
logger.debug("Word to pos >" + toPos + "<");
logger.debug("Word at cursor >" + wordAt + "<");
if (wordAt == null) {
logger.debug("No word at cursor, doing nothing");
return;
}
const expanded:boolean = tryExpandPosToMachADate(obj)
if (expanded) {
logger.debug("Selection expanded to match a date");
logger.debug("New word from pos >" + obj.fromPos + "<");
logger.debug("New word to pos >" + obj.toPos + "<");
}
const wordToReplace = curLine.slice(obj.fromPos, obj.toPos);
logger.debug("Replacing word >" + wordToReplace + "<");
const wordNew = findCycle(wordToReplace, parDirection, parCycles);
logger.debug("New word >" + wordNew + "<");
parEditor.replaceRange(wordNew,
{ line: parEditor.getCursor().line, ch: obj.fromPos },
{ line: parEditor.getCursor().line, ch: obj.toPos });
}
class CtrlXASettingTab extends PluginSettingTab {
plugin: CtrlXAPlugin;
constructor(parApp: App, parPlugin: CtrlXAPlugin) {
super(parApp, parPlugin);
this.plugin = parPlugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Cycle Lists' });
function createSetting(containerEl : HTMLElement, parIndex : number, parPlugin: CtrlXAPlugin) {
new Setting(containerEl)
.setName( 'Cycle lists ' + parIndex)
.setDesc('E.g. : "Monday, Tuesday, Wednesday,...')
.addText(text => text
.setPlaceholder('Monday,...')
.setValue(parPlugin.settings.mySetting[parIndex] ? parPlugin.settings.mySetting[parIndex].join(",") : "")
.onChange(async (value) => {
parPlugin.settings.mySetting[parIndex] = value ? value.split(",").map(item => item.trim()) : [];
await parPlugin.saveSettings();
logger.debug('settings saved');
}));
}
let i;
for (i = 0; i < 10; i++) {
createSetting(containerEl, i, this.plugin);
}
containerEl.createEl('h2', { text: 'Advanced' });
new Setting(containerEl)
.setName('Logging level')
.setDesc('Logging level for this plugin, default is INFO. Choose NONE to disable logging.')
.addDropdown(dropdown => dropdown
.addOption('NONE', 'NONE')
.addOption('ERROR', 'ERROR')
.addOption('WARN', 'WARN')
.addOption('INFO', 'INFO')
.addOption('DEBUG', 'DEBUG')
.setValue(this.plugin.settings.loggingLevel)
.onChange(async (value) => {
this.plugin.settings.loggingLevel = value;
await this.plugin.saveSettings();
logger.setLogLevel(value);
logger.info('settings loglevel saved');
}));
}
}