-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathday-title.ts
57 lines (49 loc) · 1.49 KB
/
day-title.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
import {RoamDate} from '../roam/date'
import {getDayName} from '../common/date'
import {Feature, Settings} from '../settings'
import {Browser} from 'src/core/common/browser'
export const config: Feature = {
id: 'day-title',
name: 'Daily Notes Day Titles',
}
Settings.isActive(config.id).then(active => {
if (active) {
registerEventListener()
}
})
Browser.addMessageListener(async message => {
if (message?.featureId === config.id) {
registerEventListener()
}
})
const getDayFromDate = (name: string) => {
const re = /(.*) (\d+).{2}, (\d{4})/i
const matches = name.match(re)
if (matches && matches.length === 4) {
const date = RoamDate.parse(name)
return getDayName(date)
}
return null
}
const isElementPageViewTitle = (element: HTMLElement) =>
(element.parentNode?.parentNode as HTMLElement)?.classList?.contains('rm-ref-page-view-title')
const registerEventListener = () => {
document.querySelector('body')?.addEventListener('mouseover', ev => {
const target = ev.target as HTMLElement
if (target === null) {
return
}
let day = null
if (
target.classList.contains('rm-page-ref') ||
target.classList.contains(`rm-title-display`) ||
isElementPageViewTitle(target)
) {
day = getDayFromDate(target.innerText)
}
if (day === null) {
return
}
target.setAttribute('title', day)
})
}