Skip to content
Snippets Groups Projects
Verified Commit 86051d03 authored by Mikolai Gütschow's avatar Mikolai Gütschow Committed by Sebastian
Browse files

remove redundant cursorPosition

parent 70e8e5c0
No related branches found
No related tags found
1 merge request!366Add support for HTML blog posts
...@@ -114,15 +114,13 @@ fun HtmlText( ...@@ -114,15 +114,13 @@ fun HtmlText(
val formattedString = remember(html) { val formattedString = remember(html) {
buildAnnotatedString { buildAnnotatedString {
var cursorPosition = 0 // todo: couldn't this be replaced with this.length? fun append(str: String) {
fun appendAndUpdateCursor(str: String) { this.append(str)
append(str)
cursorPosition += str.length
lastCharWasNewline = str.last() == '\n' lastCharWasNewline = str.last() == '\n'
} }
fun ensureNewline() { fun ensureNewline() {
if (!lastCharWasNewline) appendAndUpdateCursor("\n") if (!lastCharWasNewline) append("\n")
} }
fun pushIndent(indent: TextUnit) { fun pushIndent(indent: TextUnit) {
...@@ -131,37 +129,35 @@ fun HtmlText( ...@@ -131,37 +129,35 @@ fun HtmlText(
var combinedIndent = indent var combinedIndent = indent
if (indentStack.isNotEmpty()) { if (indentStack.isNotEmpty()) {
val prev = indentStack.top() val prev = indentStack.top()
if (prev.start < cursorPosition) { if (prev.start < length) {
println("pushIndent: ${prev.start}-$cursorPosition")
addStyle( addStyle(
style = ParagraphStyle(textIndent = TextIndent(prev.indent, prev.indent)), style = ParagraphStyle(textIndent = TextIndent(prev.indent, prev.indent)),
start = prev.start, start = prev.start,
end = cursorPosition end = length
) )
// ensureNewline() // ensureNewline()
} }
combinedIndent = (prev.indent.value + indent.value).sp combinedIndent = (prev.indent.value + indent.value).sp
} }
indentStack.push(IndentInfo(combinedIndent, cursorPosition)) indentStack.push(IndentInfo(combinedIndent, length))
} }
fun popIndent() { fun popIndent() {
check(indentStack.isNotEmpty()) { "nothing to pop from" } check(indentStack.isNotEmpty()) { "nothing to pop from" }
val prev = indentStack.pop() val prev = indentStack.pop()
if (prev.start < cursorPosition) { if (prev.start < length) {
println("popIndent: ${prev.start}-$cursorPosition")
addStyle( addStyle(
style = ParagraphStyle(textIndent = TextIndent(prev.indent, prev.indent)), style = ParagraphStyle(textIndent = TextIndent(prev.indent, prev.indent)),
start = prev.start, start = prev.start,
end = cursorPosition end = length
) )
ensureNewline() ensureNewline()
} }
if (indentStack.isNotEmpty()) { if (indentStack.isNotEmpty()) {
val next = indentStack.pop() val next = indentStack.pop()
indentStack.push(next.copy(start = cursorPosition)) indentStack.push(next.copy(start = length))
} }
} }
...@@ -204,8 +200,8 @@ fun HtmlText( ...@@ -204,8 +200,8 @@ fun HtmlText(
), ),
"q" to HtmlNode( "q" to HtmlNode(
// todo: quotation marks should be properly localized // todo: quotation marks should be properly localized
start = { appendAndUpdateCursor("\"") }, start = { append("\"") },
end = { appendAndUpdateCursor("\"") } end = { append("\"") }
), ),
"a" to HtmlNode( "a" to HtmlNode(
start = { node -> start = { node ->
...@@ -252,11 +248,11 @@ fun HtmlText( ...@@ -252,11 +248,11 @@ fun HtmlText(
listNumbering.incrementCurrent() listNumbering.incrementCurrent()
if (listType == UNORDERED) { if (listType == UNORDERED) {
val bulletType = listNesting.size - 1 val bulletType = listNesting.size - 1
appendAndUpdateCursor(listBullets[bulletType % listBullets.size]) append(listBullets[bulletType % listBullets.size])
appendAndUpdateCursor(" ") append(" ")
pushStringAnnotation("bullet", listNesting.size.toString()) pushStringAnnotation("bullet", listNesting.size.toString())
} else if (listType == ORDERED) { } else if (listType == ORDERED) {
appendAndUpdateCursor("${listNumbering.top()}. ") append("${listNumbering.top()}. ")
pushStringAnnotation("bullet", listNesting.size.toString()) pushStringAnnotation("bullet", listNesting.size.toString())
} }
}, },
...@@ -267,7 +263,7 @@ fun HtmlText( ...@@ -267,7 +263,7 @@ fun HtmlText(
), ),
// misc // misc
"br" to HtmlNode(start = { appendAndUpdateCursor("\n") }, end = {}), "br" to HtmlNode(start = { append("\n") }, end = {}),
"p" to HtmlNode( "p" to HtmlNode(
start = { pushIndent(0.sp) }, start = { pushIndent(0.sp) },
end = { popIndent() } end = { popIndent() }
...@@ -310,7 +306,7 @@ fun HtmlText( ...@@ -310,7 +306,7 @@ fun HtmlText(
// remove whitespace if first character in new line // remove whitespace if first character in new line
.let { if (lastCharWasNewline) it.trimStart() else it } .let { if (lastCharWasNewline) it.trimStart() else it }
if (text.isNotBlank()) { if (text.isNotBlank()) {
appendAndUpdateCursor(text) append(text)
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment