diff --git a/src/adapter/events.ts b/src/adapter/events.ts index 8f6903162..8a3746a4c 100644 --- a/src/adapter/events.ts +++ b/src/adapter/events.ts @@ -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: E) => void @@ -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 { @@ -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) @@ -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) diff --git a/src/adapter/index.ts b/src/adapter/index.ts index e1d1f3d04..900aae52e 100644 --- a/src/adapter/index.ts +++ b/src/adapter/index.ts @@ -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 @@ -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() diff --git a/src/core.ts b/src/core.ts index 7c4c6c3e0..af8b5ba4e 100644 --- a/src/core.ts +++ b/src/core.ts @@ -1,4 +1,9 @@ -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 @@ -6,7 +11,7 @@ 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 @@ -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() }) @@ -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) { @@ -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) } @@ -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) } @@ -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) } diff --git a/src/index.ts b/src/index.ts index 315f4adc1..79e724e0f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,14 +35,14 @@ export function littlefoot(userSettings: Partial = {}): 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 { diff --git a/src/types.ts b/src/types.ts index 1bcce0fd0..27a51d8d9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 -}>