|
| 1 | +/* |
| 2 | + * Copyright 2024 Readium Foundation. All rights reserved. |
| 3 | + * Use of this source code is governed by the BSD-style license |
| 4 | + * available in the top-level LICENSE file of the project. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.readium.navigator.common |
| 8 | + |
| 9 | +import androidx.compose.runtime.Composable |
| 10 | +import androidx.compose.runtime.rememberCoroutineScope |
| 11 | +import kotlinx.coroutines.CoroutineScope |
| 12 | +import kotlinx.coroutines.launch |
| 13 | +import org.readium.r2.shared.ExperimentalReadiumApi |
| 14 | +import org.readium.r2.shared.util.AbsoluteUrl |
| 15 | +import org.readium.r2.shared.util.Url |
| 16 | + |
| 17 | +/** |
| 18 | + * This listener lets you decide what to do when hyperlinks are activated, whether they point to |
| 19 | + * a readingOrder item, a non-linear resource or external content. |
| 20 | + */ |
| 21 | +@ExperimentalReadiumApi |
| 22 | +public interface HyperlinkListener { |
| 23 | + |
| 24 | + public fun onReadingOrderLinkActivated(location: HyperlinkLocation, context: LinkContext?) |
| 25 | + |
| 26 | + public fun onNonLinearLinkActivated(location: HyperlinkLocation, context: LinkContext?) |
| 27 | + |
| 28 | + public fun onExternalLinkActivated(url: AbsoluteUrl, context: LinkContext?) |
| 29 | +} |
| 30 | + |
| 31 | +@ExperimentalReadiumApi |
| 32 | +public data class HyperlinkLocation( |
| 33 | + public val href: Url, |
| 34 | + public val fragment: String? = null |
| 35 | +) |
| 36 | + |
| 37 | +@ExperimentalReadiumApi |
| 38 | +public sealed interface LinkContext |
| 39 | + |
| 40 | +@ExperimentalReadiumApi |
| 41 | +public data class FootnoteContext( |
| 42 | + public val noteContent: String |
| 43 | +) : LinkContext |
| 44 | + |
| 45 | +@ExperimentalReadiumApi |
| 46 | +public class NullHyperlinkListener : HyperlinkListener { |
| 47 | + override fun onReadingOrderLinkActivated(location: HyperlinkLocation, context: LinkContext?) { |
| 48 | + } |
| 49 | + |
| 50 | + override fun onNonLinearLinkActivated(location: HyperlinkLocation, context: LinkContext?) { |
| 51 | + } |
| 52 | + |
| 53 | + override fun onExternalLinkActivated(url: AbsoluteUrl, context: LinkContext?) { |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * A [HyperlinkListener] following links to readingOrder items. |
| 59 | + * |
| 60 | + * Activations of links to external content or non-linear items are ignored by default. |
| 61 | + * To handle them, pass [onNonLinearLinkActivated] and [onExternalLinkActivated] delegates. |
| 62 | + */ |
| 63 | +@ExperimentalReadiumApi |
| 64 | +@Composable |
| 65 | +public fun <L : Location> defaultHyperlinkListener( |
| 66 | + controller: NavigationController<L, *>, |
| 67 | + shouldFollowReadingOrderLink: (HyperlinkLocation, LinkContext?) -> Boolean = { _, _ -> true }, |
| 68 | + onNonLinearLinkActivated: (HyperlinkLocation, LinkContext?) -> Unit = { _, _ -> }, |
| 69 | + onExternalLinkActivated: (AbsoluteUrl, LinkContext?) -> Unit = { _, _ -> } |
| 70 | +): HyperlinkListener { |
| 71 | + val coroutineScope = rememberCoroutineScope() |
| 72 | + |
| 73 | + return DefaultHyperlinkListener( |
| 74 | + coroutineScope = coroutineScope, |
| 75 | + controller = controller, |
| 76 | + shouldFollowReadingOrderLink = shouldFollowReadingOrderLink, |
| 77 | + onNonLinearLinkActivatedDelegate = onNonLinearLinkActivated, |
| 78 | + onExternalLinkActivatedDelegate = onExternalLinkActivated |
| 79 | + ) |
| 80 | +} |
| 81 | + |
| 82 | +@ExperimentalReadiumApi |
| 83 | +private class DefaultHyperlinkListener<L : Location>( |
| 84 | + private val coroutineScope: CoroutineScope, |
| 85 | + private val controller: NavigationController<L, *>, |
| 86 | + private val shouldFollowReadingOrderLink: (HyperlinkLocation, LinkContext?) -> Boolean, |
| 87 | + private val onNonLinearLinkActivatedDelegate: (HyperlinkLocation, LinkContext?) -> Unit, |
| 88 | + private val onExternalLinkActivatedDelegate: (AbsoluteUrl, LinkContext?) -> Unit |
| 89 | +) : HyperlinkListener { |
| 90 | + |
| 91 | + override fun onReadingOrderLinkActivated(location: HyperlinkLocation, context: LinkContext?) { |
| 92 | + if (shouldFollowReadingOrderLink(location, context)) { |
| 93 | + coroutineScope.launch { controller.goTo(location) } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + override fun onNonLinearLinkActivated(location: HyperlinkLocation, context: LinkContext?) { |
| 98 | + onNonLinearLinkActivatedDelegate(location, context) |
| 99 | + } |
| 100 | + |
| 101 | + override fun onExternalLinkActivated(url: AbsoluteUrl, context: LinkContext?) { |
| 102 | + onExternalLinkActivatedDelegate(url, context) |
| 103 | + } |
| 104 | +} |
0 commit comments