-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.ts
311 lines (251 loc) · 8 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
import { App, debounce, Plugin, PluginSettingTab, Setting } from 'obsidian';
import EMERGE_MOTION from './animations/gemmy_emerge.gif';
import POP_MOTION from './animations/gemmy_pop.gif';
import DISAPPEAR_MOTION from './animations/gemmy_disappear.gif';
import ANGRY_MOTION from './animations/gemmy_angry.gif';
import LOOK_MOTION from './animations/gemmy_lookAround.gif'
import IDLE_MOTION from './animations/gemmy_idle.gif'
import DISAPPOINT_IMG from './animations/gemmy_disappoint.gif'
interface GemmySettings {
// how often does Gemmy talk in idle mode, in minutes
idleTalkFrequency: number;
// the number of minutes you must write before Gemmy appears to mock you
writingModeGracePeriod: number;
}
const DEFAULT_SETTINGS: GemmySettings = {
idleTalkFrequency: 5,
writingModeGracePeriod: 5
};
const GEMMY_IDLE_QUOTES = [
"Did you know that a vault is just a folder of plain text notes?",
"I see you're checking out a ChatGPT plugin, would you consider me instead?",
"You have plugins that you can update!",
"Hi I'm Gemmy! Like Clippy but shinier!",
"Everything is connected. Everything.",
`It looks like you’re writing a note.
Would you like help?
- Get help with writing the note
- Just type the note without help
- [ ] Don’t show me this tip again`,
'Can’t decide which note to work on? Try the Random Note core plugin!',
'Are you sure you don’t want to upload all your notes just so we can chat?',
'How tall would all your notes be if you stacked them up?',
'Wanna teach me to say things? Find me on GitHub and open a pull request!',
'Have you considered using Comic Sans?'
];
const WRITING_MODE_QUOTES = [
`Is that the best you can do? Keep writing!`,
`Write first, editor later.`,
`I love hearing your keyboard. Don't stop.`,
`How about we review some old notes today?`,
`Stuck? Try journaling what happened today and see if that gives you inspiration.`,
`Maybe it's time to go get some water or coffee.`,
`Anything is better than a blank page, even me. Write something!`
];
const BUBBLE_DURATION = 5000;
export default class Gemmy extends Plugin {
settings: GemmySettings;
gemmyEl: HTMLElement;
imageEl: HTMLElement;
inWritingMode: boolean = false;
idleTimeout: number;
writingModeTimeout: number;
appeared: boolean = false;
async onload() {
await this.loadSettings();
let gemmyEl = this.gemmyEl = createDiv('gemmy-container');
gemmyEl.setAttribute('aria-label-position', 'top');
gemmyEl.setAttribute('aria-label-delay', '0');
gemmyEl.setAttribute('aria-label-classes', 'gemmy-tooltip');
this.imageEl = gemmyEl.createEl('img', {});
this.addCommand({
id: 'show',
name: 'Show Gemmy',
callback: () => {
this.appear();
}
});
this.addCommand({
id: 'hide',
name: 'Hide Gemmy',
callback: () => {
this.disappear();
}
});
this.addCommand({
id: 'enter-writing-mode',
name: 'Enter writing mode',
callback: () => {
this.enterWritingMode();
}
});
this.addCommand({
id: 'leave-writing-mode',
name: 'Leave writing mode',
callback: () => {
this.leaveWritingMode();
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new GemmySettingTab(this.app, this));
this.gemmyEl.addEventListener('mouseenter', () => {
if (this.inWritingMode) {
return;
}
this.saySomething(GEMMY_IDLE_QUOTES, true);
this.idleTimeout && clearTimeout(this.idleTimeout);
});
this.gemmyEl.addEventListener('mouseleave', () => {
if (this.inWritingMode) {
return;
}
this.imageEl.setAttribute('src', IDLE_MOTION);
this.startNextIdleTimeout();
});
this.startNextIdleTimeout();
// debounce editor-change event on workspace
this.registerEvent(this.app.workspace.on('editor-change', debounce(() => {
if (!this.inWritingMode) {
return;
}
this.disappear();
this.setWritingModeTimeout();
}, 500)));
app.workspace.onLayoutReady(this.appear.bind(this));
}
appear() {
let { gemmyEl, imageEl } = this;
imageEl.setAttribute('src', EMERGE_MOTION);
// Quicker if we're in writing mode
if (this.inWritingMode) {
imageEl.setAttribute('src', POP_MOTION);
setTimeout(() => {
this.appeared = true;
this.saySomething(WRITING_MODE_QUOTES, true);
}, 1800);
}
else {
imageEl.setAttribute('src', EMERGE_MOTION);
setTimeout(() => {
imageEl.setAttribute('src', IDLE_MOTION);
this.appeared = true;
}, 3800);
}
document.body.appendChild(gemmyEl);
gemmyEl.show();
}
disappear() {
this.idleTimeout && window.clearTimeout(this.idleTimeout);
this.writingModeTimeout && window.clearTimeout(this.writingModeTimeout);
this.imageEl.setAttribute('src', DISAPPEAR_MOTION);
// remote tooltip
this.gemmyEl.dispatchEvent(new MouseEvent('mouseout', { bubbles: true, clientX: 10, clientY: 10 }));
setTimeout(() => {
this.gemmyEl.hide();
this.appeared = false;
}, 1300);
}
enterWritingMode() {
this.inWritingMode = true;
this.disappear();
this.setWritingModeTimeout();
}
leaveWritingMode() {
this.inWritingMode = false;
this.disappear();
window.clearTimeout(this.writingModeTimeout);
}
setWritingModeTimeout() {
if (this.writingModeTimeout) {
window.clearTimeout(this.writingModeTimeout);
}
this.writingModeTimeout = window.setTimeout(() => {
if (!this.inWritingMode) {
return;
}
this.appear();
}, this.settings.writingModeGracePeriod * 1000);
}
startNextIdleTimeout() {
// if the set time is 5 minutes, this will set timeout to be a random time between 4-6 minutes
// the range will be 80% - 120%
let randomFactor = 0.8 + 0.4 * Math.random();
let randomizedTimeout = randomFactor * this.settings.idleTalkFrequency * 60000;
if (this.idleTimeout) {
window.clearTimeout(this.idleTimeout);
}
this.idleTimeout = window.setTimeout(() => {
if (this.inWritingMode) {
return;
}
this.saySomething(GEMMY_IDLE_QUOTES, false);
this.startNextIdleTimeout();
}, randomizedTimeout);
}
saySomething(quotes: string[], persistent: boolean) {
if (!this.appeared) {
return;
}
let randomThing = quotes[Math.floor(Math.random() * quotes.length)];
this.gemmyEl.setAttr('aria-label', randomThing);
this.gemmyEl.setAttr('aria-label-position', 'top');
this.gemmyEl.dispatchEvent(new MouseEvent('mouseover', { bubbles: true, clientX: 10, clientY: 10 }))
if (this.inWritingMode) {
this.imageEl.setAttribute('src', ANGRY_MOTION);
setTimeout(() => {
this.imageEl.setAttribute('src', DISAPPOINT_IMG);
}, 1000);
}
else {
this.imageEl.setAttribute('src', LOOK_MOTION);
}
if (!persistent) {
setTimeout(() => {
this.gemmyEl.dispatchEvent(new MouseEvent('mouseout', { bubbles: true, clientX: 10, clientY: 10 }));
this.imageEl.setAttribute('src', IDLE_MOTION);
}, BUBBLE_DURATION);
}
}
onunload() {
this.disappear();
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class GemmySettingTab extends PluginSettingTab {
plugin: Gemmy;
constructor(app: App, plugin: Gemmy) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName('Idle talk frequency')
.setDesc('How often does Gemmy speak when idle, in minutes.')
.addSlider(slider => slider
.setLimits(5, 60, 5)
.setValue(this.plugin.settings.idleTalkFrequency)
.setDynamicTooltip()
.onChange(async (value) => {
this.plugin.settings.idleTalkFrequency = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Writing mode grace period')
.setDesc('How soon Gemmy starts to get disappointed after you stop tying in writing mode, in seconds.')
.addSlider(slider => slider
.setLimits(5, 180, 5)
.setDynamicTooltip()
.setValue(this.plugin.settings.writingModeGracePeriod)
.onChange(async (value) => {
this.plugin.settings.writingModeGracePeriod = value;
await this.plugin.saveSettings();
}));
}
}