office: char-level codec carries full paragraph+span formatting & excludes special content (no data loss); Online tab uses Scaffold+TopAppBar
- Commit
f1a45545f2aa165f9d07a32ebd0ff3a89ecef6bc- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-06 18:43 UTC (5 months ago)
- Parents
72dbc45
Diff
office/src/main/java/com/vayunmathur/office/MainActivity.kt
diff --git a/office/src/main/java/com/vayunmathur/office/MainActivity.kt b/office/src/main/java/com/vayunmathur/office/MainActivity.ktindex 632d3c7f6e..16c442c710 100644--- a/office/src/main/java/com/vayunmathur/office/MainActivity.kt+++ b/office/src/main/java/com/vayunmathur/office/MainActivity.kt@@ -407,31 +407,38 @@ fun OnlineTab(viewModel: OfficeViewModel, onOpenDoc: (com.vayunmathur.office.uti val docs by viewModel.onlineDocs.collectAsState() val deviceId = viewModel.syncDeviceId val clipboard = LocalClipboardManager.current- Column(Modifier.fillMaxSize().padding(16.dp)) {- Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween) {- Text("Online", style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold)- IconButton(onClick = { viewModel.refreshOnline() }) {- Icon(painterResource(com.vayunmathur.library.R.drawable.refresh_24px), "Refresh")- }- }- Text("Your device id (share this so others can send you documents):",- style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)- Row(verticalAlignment = Alignment.CenterVertically) {- Text(deviceId.ifEmpty { "…" }, style = MaterialTheme.typography.bodySmall, modifier = Modifier.weight(1f))- TextButton(onClick = { if (deviceId.isNotEmpty()) clipboard.setText(AnnotatedString(deviceId)) }) { Text("Copy") }+ Scaffold(+ topBar = {+ TopAppBar(+ title = { Text("Online") },+ actions = {+ IconButton(onClick = { viewModel.refreshOnline() }) {+ Icon(painterResource(com.vayunmathur.library.R.drawable.refresh_24px), "Refresh")+ }+ }+ ) }- Spacer(Modifier.height(12.dp))- if (docs.isEmpty()) {- Text("No online documents yet. Open a document and use \u201CShare\u201D to copy it here and send it to someone, or ask them to share to your device id, then tap refresh.",- style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)- } else {- LazyColumn(verticalArrangement = Arrangement.spacedBy(8.dp)) {- items(docs) { meta ->- Card(Modifier.fillMaxWidth().clickable { onOpenDoc(meta) }) {- ListItem(- headlineContent = { Text(meta.title) },- supportingContent = { Text(if (meta.owner) "Shared by you" else "Shared with you") }- )+ ) { pad ->+ Column(Modifier.fillMaxSize().padding(pad).padding(horizontal = 16.dp)) {+ Text("Your device id (share this so others can send you documents):",+ style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant)+ Row(verticalAlignment = Alignment.CenterVertically) {+ Text(deviceId.ifEmpty { "…" }, style = MaterialTheme.typography.bodySmall, modifier = Modifier.weight(1f))+ TextButton(onClick = { if (deviceId.isNotEmpty()) clipboard.setText(AnnotatedString(deviceId)) }) { Text("Copy") }+ }+ Spacer(Modifier.height(12.dp))+ if (docs.isEmpty()) {+ Text("No online documents yet. Open a document and use \u201CShare\u201D to copy it here and send it to someone, or ask them to share to your device id, then tap refresh.",+ style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)+ } else {+ LazyColumn(verticalArrangement = Arrangement.spacedBy(8.dp)) {+ items(docs) { meta ->+ Card(Modifier.fillMaxWidth().clickable { onOpenDoc(meta) }) {+ ListItem(+ headlineContent = { Text(meta.title) },+ supportingContent = { Text(if (meta.owner) "Shared by you" else "Shared with you") }+ )+ } } } }
office/src/main/java/com/vayunmathur/office/util/TextDocCodec.kt
diff --git a/office/src/main/java/com/vayunmathur/office/util/TextDocCodec.kt b/office/src/main/java/com/vayunmathur/office/util/TextDocCodec.ktindex 57f4a8d465..2fc8896d2b 100644--- a/office/src/main/java/com/vayunmathur/office/util/TextDocCodec.kt+++ b/office/src/main/java/com/vayunmathur/office/util/TextDocCodec.kt@@ -1,5 +1,7 @@ package com.vayunmathur.office.util +import androidx.compose.ui.text.style.TextAlign+import com.vayunmathur.library.ui.odf.ListType import com.vayunmathur.library.ui.odf.OdfContentBlock import com.vayunmathur.library.ui.odf.OdfDocument import com.vayunmathur.library.ui.odf.OdfParagraph@@ -14,12 +16,13 @@ import kotlinx.serialization.json.Json * than at line/element granularity. * * Each cell is `"<styleJson>\u0002<char>"`. A paragraph's characters each carry their span's inline- * style; each paragraph is terminated by a newline cell whose style carries the paragraph style.+ * style; each paragraph is terminated by a newline cell whose style carries the paragraph's layout. * Rebuilding groups consecutive equal-style chars into spans and splits paragraphs on newline cells, * so the result is always a valid document (we never merge serialized XML). *- * Only paragraph content is representable here; use [isEligible] to check before using this codec- * (documents with tables/images/etc. fall back to the line-level XML CRDT).+ * Only plain paragraph content is representable here (see [isEligible]); documents with tables,+ * images, footnotes, headers/footers, comments, bookmarks, fields, page setup, etc. fall back to the+ * lossless line-level XML CRDT so nothing is dropped. */ object TextDocCodec { private const val SEP = '\u0002'@@ -27,6 +30,7 @@ object TextDocCodec { @Serializable data class CellStyle(+ // Inline (character) style. val b: Boolean = false, val i: Boolean = false, val u: Boolean = false,@@ -34,19 +38,48 @@ object TextDocCodec { val sz: Float? = null, val col: Long? = null, val href: String? = null,- val pstyle: String? = null, // paragraph style name (only on newline cells)+ val ff: String? = null, // font family+ val bg: Long? = null, // highlight / background color+ val sup: Boolean = false, // superscript+ val sub: Boolean = false, // subscript+ // Paragraph layout (only on newline cells).+ val pstyle: String? = null,+ val al: String? = null, // alignment+ val ml: Float = 0f, // left margin (indent)+ val ti: Float = 0f, // first-line indent+ val ll: Int = 0, // list level+ val lt: String? = null, // list type+ val chk: Boolean = false, // checkbox checked+ val lh: Float? = null, // line-height percent+ val pbg: Long? = null, // paragraph background color ) - /** True if the document is representable as pure paragraphs (safe for char-level merge). */- fun isEligible(doc: OdfDocument): Boolean =- doc is OdfDocument.TextDocument && doc.content.all { it is OdfContentBlock.Paragraph }+ /**+ * True only if the document is representable as plain paragraphs with no content the codec can't+ * carry — so switching to char-level never loses data. Anything richer uses the line-level codec.+ */+ fun isEligible(doc: OdfDocument): Boolean {+ if (doc !is OdfDocument.TextDocument) return false+ if (doc.footnotes.isNotEmpty() || doc.headerParagraphs.isNotEmpty() || doc.footerParagraphs.isNotEmpty() ||+ doc.bookmarks.isNotEmpty() || doc.changes.isNotEmpty() || doc.pageSetup != null+ ) return false+ for (block in doc.content) {+ val para = (block as? OdfContentBlock.Paragraph)?.paragraph ?: return false+ for (s in para.spans) if (s.annotation != null || s.field != null || s.refName != null) return false+ }+ return true+ } private fun spanStyle(s: OdfSpan) = CellStyle( b = s.bold, i = s.italic, u = s.underline, st = s.strikethrough,- sz = s.fontSize, col = s.color, href = s.href,+ sz = s.fontSize, col = s.color, href = s.href, ff = s.fontFamily, bg = s.backgroundColor,+ sup = s.superscript, sub = s.subscript, ) - private fun paraStyle(p: OdfParagraph) = CellStyle(pstyle = p.style.name)+ private fun paraStyle(p: OdfParagraph) = CellStyle(+ pstyle = p.style.name, al = p.alignment?.let { alignName(it) }, ml = p.marginLeft, ti = p.textIndent,+ ll = p.listLevel, lt = p.listType.name, chk = p.listChecked, lh = p.lineHeightPercent, pbg = p.backgroundColor,+ ) fun toCells(doc: OdfDocument.TextDocument): List<String> { val cells = ArrayList<String>()@@ -75,6 +108,7 @@ object TextDocCodec { OdfSpan( text = curText.toString(), bold = cs.b, italic = cs.i, underline = cs.u, strikethrough = cs.st, fontSize = cs.sz, color = cs.col, href = cs.href,+ fontFamily = cs.ff, backgroundColor = cs.bg, superscript = cs.sup, subscript = cs.sub, ) ) }@@ -85,7 +119,19 @@ object TextDocCodec { flushSpan() val cs = newlineKey?.let { runCatching { json.decodeFromString<CellStyle>(it) }.getOrNull() } val style = cs?.pstyle?.let { runCatching { ParagraphStyle.valueOf(it) }.getOrNull() } ?: ParagraphStyle.BODY- blocks.add(OdfContentBlock.Paragraph(OdfParagraph(spans = if (spans.isEmpty()) listOf(OdfSpan("")) else spans, style = style)))+ val listType = cs?.lt?.let { runCatching { ListType.valueOf(it) }.getOrNull() } ?: ListType.BULLET+ blocks.add(OdfContentBlock.Paragraph(OdfParagraph(+ spans = if (spans.isEmpty()) listOf(OdfSpan("")) else spans,+ style = style,+ alignment = parseAlign(cs?.al),+ marginLeft = cs?.ml ?: 0f,+ textIndent = cs?.ti ?: 0f,+ listLevel = cs?.ll ?: 0,+ listType = listType,+ listChecked = cs?.chk ?: false,+ lineHeightPercent = cs?.lh,+ backgroundColor = cs?.pbg,+ ))) spans = ArrayList() curKey = null }@@ -107,4 +153,14 @@ object TextDocCodec { if (blocks.isEmpty()) blocks.add(OdfContentBlock.Paragraph(OdfParagraph(listOf(OdfSpan(""))))) return base.copy(content = blocks) }++ private fun alignName(a: TextAlign): String = when (a) {+ TextAlign.Start -> "start"; TextAlign.End -> "end"; TextAlign.Center -> "center"+ TextAlign.Justify -> "justify"; TextAlign.Left -> "left"; TextAlign.Right -> "right"; else -> "start"+ }++ private fun parseAlign(s: String?): TextAlign? = when (s) {+ "start" -> TextAlign.Start; "end" -> TextAlign.End; "center" -> TextAlign.Center+ "justify" -> TextAlign.Justify; "left" -> TextAlign.Left; "right" -> TextAlign.Right; else -> null+ } }