-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
233 lines (208 loc) Β· 6.23 KB
/
index.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
import '@logseq/libs'
import { SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin'
const settings: SettingSchemaDesc[] = [
{
title: "Keybinding for Pomodoro Technique",
key: "pomodoroTechniqueKeybinding",
type: "string",
default: "mod+o",
description: "Keybinding to open Pomodoro Timer",
},
{
title: "Pomodoro Time Length",
key: "pomodoroTimeLength",
type: "number",
default: 25,
description: "Set the length of your pomodoro in minutes",
}
]
logseq.useSettingsSchema(settings);
/**
* main entry
*/
async function main () {
const genRandomStr = () => Math.random().
toString(36).
replace(/[^a-z]+/g, '').
substr(0, 5)
// models
logseq.provideModel({
async startPomoTimer (e: any) {
const { pomoId, slotId, blockUuid,durationMins } = e.dataset
const startTime = Date.now()
const block = await logseq.Editor.getBlock(blockUuid)
const content = block?.content || ""
if(!content) return
const regExp = new RegExp(`\\pomodoro_${pomoId}(,\\d+)*`, 'g');
let findData = content.match(regExp)
if(!findData) return
let flag=findData[0]
let params = flag?.split(",")
let timestamp = Date.now()
let newFlag:any;
switch(params.length){
case 1:
newFlag=`${params[0]},${durationMins},${timestamp}`
break;
case 2:
newFlag=`${params[0]},${params[1]},${timestamp}`
break;
case 3:
newFlag=`${params[0]},${params[1]},${params[2]}`
break;
}
let newContent = content.replace(flag,newFlag)
if (!newContent) return
await logseq.Editor.updateBlock(blockUuid, newContent)
renderTimer({ pomoId, slotId, startTime})
},
})
logseq.provideStyle(`
.pomodoro-timer-btn {
border: 1px solid var(--ls-border-color);
white-space: initial;
padding: 0 4px;
font-size: 15px;
border-radius: 4px;
user-select: none;
cursor: default;
display: flex;
align-content: center;
}
.pomodoro-timer-btn.is-start:hover {
opacity: .8;
}
.pomodoro-timer-btn.is-start:active {
opacity: .6;
}
.pomodoro-timer-btn.is-start {
padding: 0 6px;
cursor: pointer;
}
.pomodoro-timer-btn.is-pending {
padding: 0 6px;
background-color: #f6dbdb;
border-color: #edbdbd;
font-family: Helvetica,Times New Roman,Microsoft Yahei;
color: #cd3838;
}
.pomodoro-timer-btn.is-done {
width: auto;
background-color: #defcf0;
border-color: #9ddbc7;
color: #0F9960;
}
`)
// entries
logseq.Editor.registerSlashCommand('π
Pomodoro Technique', async () => {
const durationMins = logseq.settings?.pomodoroTimeLength || 25
await logseq.Editor.insertAtEditingCursor(
`{{renderer :pomodoro_${genRandomStr()},${durationMins}}} `,
)
})
// CommandShortcut
logseq.App.registerCommandShortcut(
{ binding: logseq.settings?.pomodoroTechniqueKeybinding as string|| "alt+o" },
async () => {
const durationMins = logseq.settings?.pomodoroTimeLength || 25
await logseq.Editor.insertAtEditingCursor(
`{{renderer :pomodoro_${genRandomStr()},${durationMins}}} `,
)
}
)
/**
* @param pomoId
* @param slotId
* @param startTime
* @param durationMins
*/
function renderTimer ({
pomoId, slotId,
startTime, durationMins,
}: any) {
if (!startTime) return
const durationTime = (durationMins || logseq.settings?.pomodoroTimeLength || 25) * 60 // default 20 minus
const keepKey = `${logseq.baseInfo.id}--${pomoId}` // -${slotId}
// const keepOrNot = () => logseq.App.queryElementById(keepKey)
const provideUi = (isDone: boolean, time: string) => {
logseq.provideUI({
key: pomoId,
slot: slotId,
reset: true,
template: `
${!isDone ?
`<a class="pomodoro-timer-btn is-pending">π
${time}</a>` :
`<a class="pomodoro-timer-btn is-done">π
β
</a>`
}
`,
})
}
function _render (init: boolean) {
const nowTime = Date.now()
const offsetTime = Math.floor((nowTime - startTime) / 1000)
const isDone = durationTime < offsetTime
const humanTime = () => {
const offset = durationTime - offsetTime
const minus = Math.floor(offset / 60)
const secs = offset % 60
return `${(minus < 10 ? '0' : '') + minus}:${(secs < 10 ? '0' : '') + secs}`
}
if(!init && isDone){
logseq.UI.showMsg("A π
is Done")
}
const provideUi = (isDone: boolean, time: string) => {
logseq.provideUI({
key: pomoId,
slot: slotId,
reset: true,
template: `
${!isDone ?
`<a class="pomodoro-timer-btn is-pending">π
${time}</a>` :
`<a class="pomodoro-timer-btn is-done">π
β
</a>`
}
`,
})
}
Promise.resolve(init || logseq.UI.queryElementById(keepKey)).then((res) => {
if (res) {
provideUi(isDone, humanTime())
!isDone && setTimeout(() => {
_render(false)
}, 1000)
}
})
}
_render(true)
}
logseq.App.onMacroRendererSlotted(({ slot, payload }) => {
let [type,durationMins, startTime] = payload.arguments
if (!type?.startsWith(':pomodoro_')) return
const identity = type.split('_')[1]?.trim()
if (!identity) return
if (!durationMins){
durationMins = durationMins || logseq.settings?.pomodoroTimeLength as string || "25"
}
const pomoId = 'pomodoro-timer-start_' + identity
if (!startTime?.trim()) {
return logseq.provideUI({
key: pomoId,
slot, reset: true,
template: `
<button
class="pomodoro-timer-btn is-start"
data-slot-id="${slot}"
data-pomo-id="${identity}"
data-block-uuid="${payload.uuid}"
data-duration-mins="${durationMins}"
data-on-click="startPomoTimer">
π
START
</button>
`,
})
}
// reset slot ui
renderTimer({ pomoId, slotId: slot, startTime, durationMins })
})
}
// bootstrap
logseq.ready(main).catch(console.error)