Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
goblindegook committed Jun 10, 2019
1 parent 4c684a2 commit ee2eabb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 50 deletions.
15 changes: 7 additions & 8 deletions src/adapter/events.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import throttle from 'lodash.throttle'
import { off, on } from 'delegated-events'
import { Core, FootnoteAction } from '../core'
import { Footnote } from '../types'

type EventHandler<E extends Event> = (e: E) => void

Expand All @@ -24,7 +23,7 @@ function closestButtonId(target: Element): string | null | undefined {
}

function handleTap(
get: (id: string) => Footnote | undefined,
get: Core['findById'],
action: FootnoteAction,
dismissAll: () => void
): EventListener {
Expand All @@ -41,13 +40,13 @@ function handleTap(
}

function handleHover(
findFootnote: (id: string) => Footnote | undefined,
get: Core['findById'],
action: FootnoteAction
): EventListener {
return event => {
event.preventDefault()
const id = closestFootnoteId(event.target as HTMLElement)
const footnote = id && findFootnote(id)
const footnote = id && get(id)

if (footnote) {
action(footnote)
Expand Down Expand Up @@ -94,19 +93,19 @@ export function bindContentScrollHandler(contentElement: Element): void {

export function addEventListeners({
dismissAll,
findFootnote,
findById,
hover,
repositionAll,
resizeAll,
toggle,
unhover
}: Core): () => void {
const toggleOnTap = handleTap(findFootnote, toggle, dismissAll)
const toggleOnTap = handleTap(findById, toggle, dismissAll)
const dismissOnEscape = handleEscape(dismissAll)
const throttledReposition = throttle(repositionAll)
const throttledResize = throttle(resizeAll)
const showOnHover = handleHover(findFootnote, hover)
const hideOnHover = handleHover(findFootnote, unhover)
const showOnHover = handleHover(findById, hover)
const hideOnHover = handleHover(findById, unhover)

document.addEventListener('touchend', toggleOnTap)
document.addEventListener('click', toggleOnTap)
Expand Down
19 changes: 3 additions & 16 deletions src/adapter/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createFootnote } from './footnote'
import { createDocumentFootnotes, restoreOriginalFootnotes } from './setup'
import { Adapter, Settings } from '../types'
import { Settings } from '../types'
import { Adapter } from '../core'

export type RawFootnote = {
readonly id: string
Expand All @@ -17,21 +18,7 @@ export function createAdapter(settings: Settings): Adapter {
const footnotes = createDocumentFootnotes(settings).map(createFootnote)

return {
findFootnote: id => {
return footnotes.find(footnote => footnote.getId() === id)
},
forEachFootnote: callback => {
footnotes.forEach(callback)
},
forEachFootnoteExcept: (callback, except) => {
const exceptId = except.getId()
footnotes
.filter(footnote => footnote.getId() !== exceptId)
.forEach(callback)
},
hasHoveredFootnotes: () => {
return footnotes.some(footnote => footnote.isHovered())
},
footnotes: () => footnotes,
unmount: () => {
footnotes.forEach(footnote => footnote.unmount())
restoreOriginalFootnotes()
Expand Down
40 changes: 26 additions & 14 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Footnote, Adapter, Settings } from './types'
import { Footnote, Settings } from './types'

export type Adapter = Readonly<{
footnotes: () => readonly Footnote[]
unmount: () => void
}>

export type FootnoteAction = (footnote: Footnote, delay?: number) => void

export type Core = Readonly<{
activate: FootnoteAction
dismiss: FootnoteAction
dismissAll: (delay?: number) => void
findFootnote: (id: string) => Footnote | undefined
findById: (id: string) => Footnote | undefined
hover: FootnoteAction
repositionAll: () => void
resizeAll: () => void
Expand All @@ -24,7 +29,7 @@ function createActivate(adapter: Adapter, settings: Settings): FootnoteAction {

footnote.activate(activateCallback)

adapter.forEachFootnote(current => {
adapter.footnotes().forEach(current => {
current.reposition()
current.resize()
})
Expand Down Expand Up @@ -57,24 +62,25 @@ export function createCore(adapter: Adapter, settings: Settings): Core {
const dismiss = createDismiss(settings)

return {
findFootnote: adapter.findFootnote,

unmount: adapter.unmount,

activate,

dismiss,

findById: id =>
adapter.footnotes().find(footnote => footnote.getId() === id),

unmount: adapter.unmount,

dismissAll(delay = settings.dismissDelay) {
adapter.forEachFootnote(current => dismiss(current, delay))
adapter.footnotes().forEach(current => dismiss(current, delay))
},

repositionAll() {
adapter.forEachFootnote(current => current.reposition())
adapter.footnotes().forEach(current => current.reposition())
},

resizeAll() {
adapter.forEachFootnote(current => current.resize())
adapter.footnotes().forEach(current => current.resize())
},

toggle(footnote) {
Expand All @@ -83,7 +89,10 @@ export function createCore(adapter: Adapter, settings: Settings): Core {
dismiss(footnote)
} else {
if (!allowMultiple) {
adapter.forEachFootnoteExcept(dismiss, footnote)
adapter
.footnotes()
.filter(current => current.getId() !== footnote.getId())
.forEach(dismiss)
}
activate(footnote)
}
Expand All @@ -94,7 +103,10 @@ export function createCore(adapter: Adapter, settings: Settings): Core {
footnote.startHovering()
if (activateOnHover && !footnote.isActive()) {
if (!allowMultiple) {
adapter.forEachFootnoteExcept(dismiss, footnote)
adapter
.footnotes()
.filter(current => current.getId() !== footnote.getId())
.forEach(dismiss)
}
activate(footnote, delay)
}
Expand All @@ -105,8 +117,8 @@ export function createCore(adapter: Adapter, settings: Settings): Core {
footnote.stopHovering()
if (dismissOnUnhover) {
setTimeout(() => {
if (!adapter.hasHoveredFootnotes()) {
adapter.forEachFootnote(dismiss)
if (!adapter.footnotes().some(f => f.isHovered())) {
adapter.footnotes().forEach(dismiss)
}
}, delay)
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ export function littlefoot(userSettings: Partial<Settings> = {}): Littlefoot {

return {
activate(id) {
const footnote = core.findFootnote(id)
const footnote = core.findById(id)
if (footnote) {
core.activate(footnote)
}
},

dismiss(id, delay) {
const footnote = id && core.findFootnote(id)
const footnote = id && core.findById(id)
if (footnote) {
core.dismiss(footnote, delay)
} else {
Expand Down
10 changes: 0 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,3 @@ export type Footnote = Readonly<{
stopHovering: () => void
unmount: () => void
}>

type FootnoteCallback = (current: Footnote) => void

export type Adapter = Readonly<{
findFootnote: (id: string) => Footnote | undefined
forEachFootnote: (callback: FootnoteCallback, selector?: string) => void
forEachFootnoteExcept: (callback: FootnoteCallback, except: Footnote) => void
hasHoveredFootnotes: () => boolean
unmount: () => void
}>

0 comments on commit ee2eabb

Please sign in to comment.