-
Notifications
You must be signed in to change notification settings - Fork 46
/
date-suggest.ts
164 lines (142 loc) · 4.51 KB
/
date-suggest.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
import {
App,
Editor,
EditorPosition,
EditorSuggest,
EditorSuggestContext,
EditorSuggestTriggerInfo,
MarkdownView,
TFile,
} from "obsidian";
import type NaturalLanguageDates from "src/main";
import { generateMarkdownLink } from "src/utils";
interface IDateCompletion {
label: string;
}
export default class DateSuggest extends EditorSuggest<IDateCompletion> {
private plugin: NaturalLanguageDates;
private app: App;
constructor(app: App, plugin: NaturalLanguageDates) {
super(app);
this.app = app;
this.plugin = plugin;
// @ts-ignore
this.scope.register(["Shift"], "Enter", (evt: KeyboardEvent) => {
// @ts-ignore
this.suggestions.useSelectedItem(evt);
return false;
});
if (this.plugin.settings.autosuggestToggleLink) {
this.setInstructions([{ command: "Shift", purpose: "Keep text as alias" }]);
}
}
getSuggestions(context: EditorSuggestContext): IDateCompletion[] {
const suggestions = this.getDateSuggestions(context);
if (suggestions.length) {
return suggestions;
}
// catch-all if there are no matches
return [{ label: context.query }];
}
getDateSuggestions(context: EditorSuggestContext): IDateCompletion[] {
if (context.query.match(/^time/)) {
return ["now", "+15 minutes", "+1 hour", "-15 minutes", "-1 hour"]
.map((val) => ({ label: `time:${val}` }))
.filter((item) => item.label.toLowerCase().startsWith(context.query));
}
if (context.query.match(/(next|last|this)/i)) {
const reference = context.query.match(/(next|last|this)/i)[1];
return [
"week",
"month",
"year",
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
]
.map((val) => ({ label: `${reference} ${val}` }))
.filter((items) => items.label.toLowerCase().startsWith(context.query));
}
const relativeDate =
context.query.match(/^in ([+-]?\d+)/i) || context.query.match(/^([+-]?\d+)/i);
if (relativeDate) {
const timeDelta = relativeDate[1];
return [
{ label: `in ${timeDelta} minutes` },
{ label: `in ${timeDelta} hours` },
{ label: `in ${timeDelta} days` },
{ label: `in ${timeDelta} weeks` },
{ label: `in ${timeDelta} months` },
{ label: `${timeDelta} days ago` },
{ label: `${timeDelta} weeks ago` },
{ label: `${timeDelta} months ago` },
].filter((items) => items.label.toLowerCase().startsWith(context.query));
}
return [{ label: "Today" }, { label: "Yesterday" }, { label: "Tomorrow" }].filter(
(items) => items.label.toLowerCase().startsWith(context.query)
);
}
renderSuggestion(suggestion: IDateCompletion, el: HTMLElement): void {
el.setText(suggestion.label);
}
selectSuggestion(suggestion: IDateCompletion, event: KeyboardEvent | MouseEvent): void {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeView) {
return;
}
const includeAlias = event.shiftKey;
let dateStr = "";
let makeIntoLink = this.plugin.settings.autosuggestToggleLink;
if (suggestion.label.startsWith("time:")) {
const timePart = suggestion.label.substring(5);
dateStr = this.plugin.parseTime(timePart).formattedString;
makeIntoLink = false;
} else {
dateStr = this.plugin.parseDate(suggestion.label).formattedString;
}
if (makeIntoLink) {
dateStr = generateMarkdownLink(
this.app,
dateStr,
includeAlias ? suggestion.label : undefined
);
}
activeView.editor.replaceRange(dateStr, this.context.start, this.context.end);
}
onTrigger(
cursor: EditorPosition,
editor: Editor,
file: TFile
): EditorSuggestTriggerInfo {
if (!this.plugin.settings.isAutosuggestEnabled) {
return null;
}
const triggerPhrase = this.plugin.settings.autocompleteTriggerPhrase;
const startPos = this.context?.start || {
line: cursor.line,
ch: cursor.ch - triggerPhrase.length,
};
if (!editor.getRange(startPos, cursor).startsWith(triggerPhrase)) {
return null;
}
const precedingChar = editor.getRange(
{
line: startPos.line,
ch: startPos.ch - 1,
},
startPos
);
if (precedingChar && /[`a-zA-Z0-9]/.test(precedingChar)) {
return null;
}
return {
start: startPos,
end: cursor,
query: editor.getRange(startPos, cursor).substring(triggerPhrase.length),
};
}
}