-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathindex.ts
More file actions
165 lines (144 loc) Β· 4.04 KB
/
index.ts
File metadata and controls
165 lines (144 loc) Β· 4.04 KB
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
import '@logseq/libs'
/**
* main entry
*/
async function main () {
logseq.App.showMsg('hello, pomodoro timer :)')
const genRandomStr = () => Math.random().
toString(36).
replace(/[^a-z]+/g, '').
substr(0, 5)
// models
logseq.provideModel({
async startPomoTimer (e: any) {
const { pomoId, slotId, blockUuid } = e.dataset
const startTime = Date.now()
const block = await logseq.Editor.getBlock(blockUuid)
const flag = `{{renderer :pomodoro_${pomoId}`
const newContent = block?.content?.replace(`${flag}}}`,
`${flag},${startTime}}}`)
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: 2px 4px;
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: 3px 6px;
cursor: pointer;
}
.pomodoro-timer-btn.is-pending {
padding-left: 6px;
width: 84px;
background-color: #f6dbdb;
border-color: #edbdbd;
color: #cd3838;
}
.pomodoro-timer-btn.is-done {
width: auto;
background-color: #defcf0;
border-color: #9ddbc7;
color: #0F9960;
}
`)
// entries
logseq.Editor.registerSlashCommand('π
Pomodoro Timer', async () => {
await logseq.Editor.insertAtEditingCursor(
`{{renderer :pomodoro_${genRandomStr()}}} `,
)
})
/**
* @param pomoId
* @param slotId
* @param startTime
* @param durationMins
*/
function renderTimer ({
pomoId, slotId,
startTime, durationMins,
}: any) {
if (!startTime) return
const durationTime = (durationMins || 25) * 60 // default 20 minus
const keepKey = `${logseq.baseInfo.id}--${pomoId}`
const keepOrNot = () => logseq.App.queryElementById(keepKey)
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}`
}
const provideUi = () => logseq.provideUI({
key: pomoId,
slot: slotId,
reset: true,
template: `
${!isDone ?
`<a class="pomodoro-timer-btn is-pending">
π
${humanTime()}
</a>` :
`<a class="pomodoro-timer-btn is-done">
π
β
</a>`
}
`,
})
Promise.resolve(init || keepOrNot()).then((res) => {
if (res) {
provideUi()
!isDone && setTimeout(() => {
_render(false)
}, 1000)
}
})
}
_render(true)
}
logseq.App.onMacroRendererSlotted(({ slot, payload }) => {
const [type, startTime, durationMins] = payload.arguments
if (!type?.startsWith(':pomodoro_')) return
const identity = type.split('_')[1]?.trim()
if (!identity) return
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-on-click="startPomoTimer">
π
START
</button>
`,
})
}
// reset slot ui
renderTimer({ pomoId, slotId: slot, startTime, durationMins })
})
}
// bootstrap
logseq.ready(main).catch(console.error)