-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add better blockquote support (#194)
- Surround highlighted text and auto-filled description with <blockquote> by default when saving - Properly display bookmark description with blockquote on the main screen and when expanding the description
- Loading branch information
Showing
5 changed files
with
164 additions
and
11 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
app/src/main/kotlin/com/fibelatti/pinboard/core/android/CustomQuoteSpan.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.fibelatti.pinboard.core.android | ||
|
||
import android.graphics.Canvas | ||
import android.graphics.Paint | ||
import android.text.Layout | ||
import android.text.Spannable | ||
import android.text.style.LeadingMarginSpan | ||
import android.text.style.LineBackgroundSpan | ||
import android.text.style.QuoteSpan | ||
import androidx.annotation.ColorInt | ||
import com.fibelatti.pinboard.core.android.CustomQuoteSpan.Companion.replaceQuoteSpans | ||
|
||
/** | ||
* [android.text.style.QuoteSpan] requires min API 28 in order to customize the stripe color and gap, | ||
* so this is a substitute which also allows customizing the background color. | ||
* | ||
* @see replaceQuoteSpans | ||
*/ | ||
class CustomQuoteSpan( | ||
@ColorInt private val backgroundColor: Int, | ||
@ColorInt private val stripeColor: Int, | ||
private val stripeWidth: Float, | ||
private val gap: Float, | ||
) : LeadingMarginSpan, LineBackgroundSpan { | ||
|
||
override fun getLeadingMargin(first: Boolean): Int = (stripeWidth + gap).toInt() | ||
|
||
override fun drawLeadingMargin( | ||
c: Canvas, | ||
p: Paint, | ||
x: Int, | ||
dir: Int, | ||
top: Int, | ||
baseline: Int, | ||
bottom: Int, | ||
text: CharSequence, | ||
start: Int, | ||
end: Int, | ||
first: Boolean, | ||
layout: Layout, | ||
) { | ||
val style = p.style | ||
val paintColor = p.color | ||
p.style = Paint.Style.FILL | ||
p.color = stripeColor | ||
c.drawRect(x.toFloat(), top.toFloat(), x + dir * stripeWidth, bottom.toFloat(), p) | ||
p.style = style | ||
p.color = paintColor | ||
} | ||
|
||
override fun drawBackground( | ||
c: Canvas, | ||
p: Paint, | ||
left: Int, | ||
right: Int, | ||
top: Int, | ||
baseline: Int, | ||
bottom: Int, | ||
text: CharSequence, | ||
start: Int, | ||
end: Int, | ||
lnum: Int, | ||
) { | ||
val paintColor = p.color | ||
p.color = backgroundColor | ||
c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), p) | ||
p.color = paintColor | ||
} | ||
|
||
companion object { | ||
|
||
fun replaceQuoteSpans( | ||
spannable: Spannable, | ||
@ColorInt backgroundColor: Int, | ||
@ColorInt stripeColor: Int, | ||
stripeWidth: Float, | ||
gap: Float, | ||
) { | ||
val quoteSpans = spannable.getSpans(0, spannable.length, QuoteSpan::class.java) | ||
for (quoteSpan in quoteSpans) { | ||
val start = spannable.getSpanStart(quoteSpan) | ||
val end = spannable.getSpanEnd(quoteSpan) | ||
val flags = spannable.getSpanFlags(quoteSpan) | ||
|
||
spannable.removeSpan(quoteSpan) | ||
spannable.setSpan(CustomQuoteSpan(backgroundColor, stripeColor, stripeWidth, gap), start, end, flags) | ||
} | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
app/src/main/kotlin/com/fibelatti/pinboard/core/android/composable/TextWithBlockquote.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.fibelatti.pinboard.core.android.composable | ||
|
||
import android.text.TextUtils | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.toArgb | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.unit.TextUnit | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import androidx.core.text.HtmlCompat | ||
import androidx.core.text.toSpannable | ||
import com.fibelatti.core.extension.setupLinks | ||
import com.fibelatti.pinboard.core.android.CustomQuoteSpan | ||
import com.google.android.material.textview.MaterialTextView | ||
|
||
@Composable | ||
fun TextWithBlockquote( | ||
text: String, | ||
modifier: Modifier = Modifier, | ||
textColor: Color = MaterialTheme.colorScheme.onSurface, | ||
textSize: TextUnit = 14.sp, | ||
maxLines: Int = Int.MAX_VALUE, | ||
blockquoteBackgroundColor: Color = MaterialTheme.colorScheme.surfaceVariant, | ||
blockquoteStripeColor: Color = MaterialTheme.colorScheme.onSurfaceVariant, | ||
) { | ||
val argTextColor = textColor.toArgb() | ||
val rgbBlockquoteBackgroundColor = blockquoteBackgroundColor.toArgb() | ||
val rgbBlockquoteStripeColor = blockquoteStripeColor.toArgb() | ||
val stripeWidth = with(LocalDensity.current) { 2.dp.toPx() } | ||
val gap = with(LocalDensity.current) { 8.dp.toPx() } | ||
|
||
val formattedText = remember(text) { | ||
HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY).toSpannable().apply { | ||
CustomQuoteSpan.replaceQuoteSpans( | ||
spannable = this, | ||
backgroundColor = rgbBlockquoteBackgroundColor, | ||
stripeColor = rgbBlockquoteStripeColor, | ||
stripeWidth = stripeWidth, | ||
gap = gap, | ||
) | ||
} | ||
} | ||
|
||
AndroidView( | ||
factory = { context -> | ||
MaterialTextView(context).apply { | ||
this.text = formattedText | ||
this.textSize = textSize.value | ||
this.maxLines = maxLines | ||
this.ellipsize = TextUtils.TruncateAt.END | ||
this.setTextColor(argTextColor) | ||
setupLinks() | ||
} | ||
}, | ||
modifier = modifier, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters