Skip to content

🐛 Fix HTML and RTF pasteboard view rendering issues #2592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class HtmlPasteItem(
val htmlUtils = getHtmlUtils()
}

private val htmlTextCache by lazy {
htmlUtils.getHtmlText(html)
}

constructor(jsonObject: JsonObject) : this(
identifiers = jsonObject["identifiers"]!!.jsonPrimitive.content.split(","),
hash = jsonObject["hash"]!!.jsonPrimitive.content,
Expand Down Expand Up @@ -63,11 +67,11 @@ class HtmlPasteItem(
}

override fun getSearchContent(): String {
return htmlUtils.getHtmlText(html).lowercase()
return htmlTextCache.lowercase()
}

override fun getTitle(): String {
return htmlUtils.getHtmlText(html)
return htmlTextCache
}

override fun update(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ data class RtfPasteItem(
val rtfUtils = getRtfUtils()
}

private val rtfTextCache by lazy {
rtfUtils.getRtfText(rtf)
}

constructor(jsonObject: JsonObject) : this(
identifiers = jsonObject["identifiers"]!!.jsonPrimitive.content.split(","),
hash = jsonObject["hash"]!!.jsonPrimitive.content,
Expand Down Expand Up @@ -63,11 +67,11 @@ data class RtfPasteItem(
}

override fun getSearchContent(): String {
return rtfUtils.getRtfText(rtf).lowercase()
return rtfTextCache.lowercase()
}

override fun getTitle(): String {
return rtfUtils.getRtfText(rtf)
return rtfTextCache
}

override fun update(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.crosspaste.ui.paste

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -23,7 +23,6 @@ import coil3.compose.SubcomposeAsyncImage
import coil3.compose.SubcomposeAsyncImageContent
import coil3.request.ImageRequest
import coil3.request.crossfade
import com.crosspaste.app.AppSize
import com.crosspaste.image.coil.GenerateImageItem
import com.crosspaste.image.coil.ImageLoaders
import com.crosspaste.rendering.RenderingHelper
Expand All @@ -38,7 +37,6 @@ fun GenerateImageView(
preview: Boolean,
alignment: Alignment = Alignment.Center,
) {
val appSize = koinInject<AppSize>()
val imageLoaders = koinInject<ImageLoaders>()
val platformContext = koinInject<PlatformContext>()
val renderingHelper = koinInject<RenderingHelper>()
Expand All @@ -63,20 +61,26 @@ fun GenerateImageView(
-> {
Row(
modifier =
Modifier.size(appSize.searchWindowDetailViewDpSize)
Modifier.fillMaxSize()
.padding(10.dp),
) {
Text(
text = text,
fontFamily = FontFamily.SansSerif,
maxLines = 4,
maxLines =
if (preview) {
4
} else {
Int.MAX_VALUE
},
softWrap = true,
overflow = TextOverflow.Ellipsis,
style =
TextStyle(
fontWeight = FontWeight.Normal,
color = MaterialTheme.colorScheme.onBackground,
fontSize = 14.sp,
color = MaterialTheme.colorScheme.onSurface,
fontSize = 16.sp,
lineHeight = 20.sp,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.crosspaste.info.PasteInfos.SIZE
import com.crosspaste.info.PasteInfos.TYPE
import com.crosspaste.paste.item.PasteHtml
import com.crosspaste.paste.item.PasteItem
import com.crosspaste.paste.item.PasteText
import com.crosspaste.path.UserDataPathProvider
import com.crosspaste.ui.base.UISupport
import com.crosspaste.ui.paste.GenerateImageView
Expand Down Expand Up @@ -66,7 +67,7 @@ fun HtmlToImageDetailView(
)
},
imagePath = filePath,
text = pasteHtml.getText(),
text = pasteData.getPasteItem(PasteText::class)?.text ?: pasteHtml.getText(),
preview = false,
alignment = Alignment.TopStart,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.crosspaste.info.PasteInfos.SIZE
import com.crosspaste.info.PasteInfos.TYPE
import com.crosspaste.paste.item.PasteItem
import com.crosspaste.paste.item.PasteRtf
import com.crosspaste.paste.item.PasteText
import com.crosspaste.path.UserDataPathProvider
import com.crosspaste.ui.base.UISupport
import com.crosspaste.ui.paste.GenerateImageView
Expand Down Expand Up @@ -66,7 +67,7 @@ fun RtfToImageDetailView(
)
},
imagePath = filePath,
text = pasteRtf.getText(),
text = pasteData.getPasteItem(PasteText::class)?.text ?: pasteRtf.getText(),
preview = false,
alignment = Alignment.TopStart,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.crosspaste.db.paste.PasteData
import com.crosspaste.paste.item.PasteHtml
import com.crosspaste.paste.item.PasteText
import com.crosspaste.path.UserDataPathProvider
import com.crosspaste.ui.paste.GenerateImageView
import org.koin.compose.koinInject
Expand All @@ -24,11 +25,15 @@ fun HtmlToImagePreviewView(pasteData: PasteData) {
)
}

val previewText =
pasteData.getPasteItem(PasteText::class)?.previewText()
?: pasteHtml.getText()

SimplePreviewContentView(pasteData) {
GenerateImageView(
modifier = Modifier.fillMaxSize(),
imagePath = filePath,
text = pasteHtml.getText(),
text = previewText,
preview = true,
alignment = Alignment.TopStart,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.crosspaste.db.paste.PasteData
import com.crosspaste.paste.item.PasteRtf
import com.crosspaste.paste.item.PasteText
import com.crosspaste.path.UserDataPathProvider
import com.crosspaste.ui.paste.GenerateImageView
import org.koin.compose.koinInject
Expand All @@ -24,11 +25,15 @@ fun RtfToImagePreviewView(pasteData: PasteData) {
)
}

val previewText =
pasteData.getPasteItem(PasteText::class)?.previewText()
?: pasteRtf.getText()

SimplePreviewContentView(pasteData) {
GenerateImageView(
modifier = Modifier.fillMaxSize(),
imagePath = filePath,
text = pasteRtf.getText(),
text = previewText,
preview = true,
alignment = Alignment.TopStart,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.crosspaste.utils

import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.safety.Safelist

actual fun getHtmlUtils(): HtmlUtils {
return DesktopHtmlUtils
Expand All @@ -15,6 +17,13 @@ object DesktopHtmlUtils : HtmlUtils {
}

override fun getHtmlText(html: String): String {
return Jsoup.parse(html).text()
val jsoupDoc: Document = Jsoup.parse(html)
val outputSettings = Document.OutputSettings()
outputSettings.prettyPrint(false)
jsoupDoc.outputSettings(outputSettings)
jsoupDoc.select("br").before("\\n")
jsoupDoc.select("p").before("\\n")
val str = jsoupDoc.html().replace("\\\\n".toRegex(), "\n")
return Jsoup.clean(str, "", Safelist.none(), outputSettings)
}
}