Skip to content

Commit

Permalink
Merge pull request #82 from braydie/master
Browse files Browse the repository at this point in the history
Added mouseover event for page titles to show day name
  • Loading branch information
Stvad authored Jun 17, 2020
2 parents a823036 + 1123884 commit d74c04c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
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'
6 changes: 5 additions & 1 deletion src/ts/core/common/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ export function addDays(date: Date, days: number) {
return result
}

export const isValid = (date: Date) => !isNaN(date.getTime())
export const isValid = (date: Date) => !isNaN(date.getTime())

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

export const getDayName = (date: Date) => days[date.getDay()]
57 changes: 57 additions & 0 deletions src/ts/core/features/day-title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {browser} from 'webextension-polyfill-ts'
import {RoamDate} from '../roam/date'
import {getDayName} from '../common/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 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

0 comments on commit d74c04c

Please sign in to comment.