From f0aa16113fe6327066a8a0daeec8d4edbf4be864 Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Sun, 8 Oct 2023 05:08:05 +0800 Subject: [PATCH] EPUB: fix `media:` properties --- epub.js | 21 ++++++++++++++++----- view.js | 3 ++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/epub.js b/epub.js index 2b6d567..322c6e1 100644 --- a/epub.js +++ b/epub.js @@ -165,12 +165,23 @@ const getMetadata = opf => { : getValue(obj, arr.find(filter))] })) - const getProperties = prefix => Object.fromEntries($$($metadata, 'meta') + const $$meta = $$($metadata, 'meta') + const getMetasByPrefix = prefix => $$meta .filter(filterAttribute('property', x => x?.startsWith(prefix))) - .map(el => [el.getAttribute('property').replace(prefix, ''), - getElementText(el)])) - const rendition = getProperties('rendition:') - const media = getProperties('media:') + .map(el => [el.getAttribute('property').replace(prefix, ''), el]) + + const rendition = Object.fromEntries(getMetasByPrefix('rendition:') + .map(([k, el]) => [k, getElementText(el)])) + + const media = { narrator: [], duration: {} } + for (const [k, el] of getMetasByPrefix('media:')) { + const v = getElementText(el) + if (k === 'duration') media.duration[ + el.getAttribute('refines')?.split('#')?.[1] ?? ''] = parseClock(v) + else if (k === 'active-class') media.activeClass = v + else if (k === 'narrator') media.narrator.push(v) + else if (k === 'playback-active-class') media.playbackActiveClass = v + } return { metadata, rendition, media } } diff --git a/view.js b/view.js index 93ada40..4609ece 100644 --- a/view.js +++ b/view.js @@ -114,7 +114,8 @@ export class View extends HTMLElement { this.#root.append(this.renderer) if (book.sections.some(section => section.mediaOverlay)) { - const activeClass = book.media['active-class'] + book.media.activeClass ||= '-epub-media-overlay-active' + const activeClass = book.media.activeClass this.mediaOverlay = book.getMediaOverlay() let lastActive this.mediaOverlay.addEventListener('highlight', e => {