Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mouseover event for page titles to show day name #82

Merged
merged 10 commits into from
Jun 17, 2020
2 changes: 1 addition & 1 deletion src/ts/contentScripts/entry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
import '../../core/features'
import '../../core/settings/dispatcher'
import '../../core/settings/shortcuts'
import '../../core/features/fuzzy_date'
import '../../core/features/fuzzy_date'
56 changes: 56 additions & 0 deletions src/ts/core/features/day-title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {browser} from 'webextension-polyfill-ts'
import {RoamDate} from '../roam/date'
import {Feature, Settings} from '../settings'

export const config: Feature = {
id: 'day-title',
name: 'Daily Notes Day Titles',
}

Settings.isActive(config.id).then(active => {
if (active) {
registerEventListener()
}
})

browser.runtime.onMessage.addListener(async message => {
if (message?.featureId === config.id) {
registerEventListener()
}
})

const getDayFromDate = (name: string) => {
let re = /(.*) (\d+).{2}, (\d{4})/i
const matches = name.match(re)
if (matches && matches.length === 4) {
const date = RoamDate.parse(name)
return RoamDate.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)
})
}
2 changes: 2 additions & 0 deletions src/ts/core/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {config as blockManipulation} from './block-manipulation'
import {config as estimate} from './estimates'
import {config as navigation} from './navigation'
import {config as livePreview} from './livePreview'
import {config as dateTitle} from './day-title'
import {filterAsync, mapAsync} from '../common/async'

export const Features = {
Expand All @@ -17,6 +18,7 @@ export const Features = {
estimate,
customCss,
navigation,
dateTitle,
livePreview,
]),

Expand Down
5 changes: 5 additions & 0 deletions src/ts/core/roam/date/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import dateFormat from 'dateformat'

const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

export const RoamDate = {
formatString: `mmmm dS, yyyy`,
pageFormatString() {
Expand All @@ -22,4 +24,7 @@ export const RoamDate = {
parseFromReference(name: string): Date {
return this.parse(name.slice(2).slice(0, -2))
},
getDayName(date: Date) {
return days[date.getDay()]
},
}