Skip to content

Commit

Permalink
fix: remove comments from path and group when minify is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeltonholo committed Mar 3, 2024
1 parent 950b1f4 commit 475a620
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ fun AndroidVectorNode.asNode(minified: Boolean): ImageVectorNode = when (this) {
fun AndroidVectorNode.Path.asNode(minified: Boolean): ImageVectorNode.Path = ImageVectorNode.Path(
fillColor = fillColor.orEmpty(),
wrapper = pathData.asNodeWrapper(minified),
minified = minified,
)

fun AndroidVectorNode.Group.asNode(minified: Boolean): ImageVectorNode.Group = ImageVectorNode.Group(
clipPath = clipPath?.pathData?.asNodeWrapper(minified),
commands = commands.map { it.asNode(minified) },
minified = minified,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.tonholo.s2c.domain
import dev.tonholo.s2c.error.ErrorCode
import dev.tonholo.s2c.error.ExitProgramException
import dev.tonholo.s2c.extensions.EMPTY
import dev.tonholo.s2c.extensions.indented
import dev.tonholo.s2c.logger.debug
import dev.tonholo.s2c.logger.debugEndSection
import dev.tonholo.s2c.logger.debugSection
Expand All @@ -13,6 +14,7 @@ sealed interface ImageVectorNode {
data class Path(
val fillColor: String,
val wrapper: NodeWrapper,
val minified: Boolean,
) : ImageVectorNode {
override fun materialize(): String {
val realColor = fillColor.uppercase().removePrefix("#").let { color ->
Expand Down Expand Up @@ -45,9 +47,10 @@ sealed interface ImageVectorNode {
""
}

val comment = if (minified) "" else "// ${wrapper.normalizedPath}\n|"

return """
|// ${wrapper.normalizedPath}
|path$pathParamsString {
|${comment}path$pathParamsString {
| $pathNodes
|}
""".trimMargin()
Expand All @@ -57,6 +60,7 @@ sealed interface ImageVectorNode {
data class Group(
val clipPath: NodeWrapper?,
val commands: List<ImageVectorNode>,
val minified: Boolean,
) : ImageVectorNode {
override fun materialize(): String {
val indentSize = 4
Expand All @@ -73,8 +77,13 @@ sealed interface ImageVectorNode {
.replace("\n", "\n${" ".repeat(indentSize * 2)}")
.trimEnd()
}
"""(
| // ${clipPath.normalizedPath}

val clipPathComment = if (minified) {
""
} else {
"\n|${"// ${clipPath.normalizedPath}".indented(4)}"
}
"""($clipPathComment
| clipPathData = PathData {
| $clipPathData
| },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package dev.tonholo.s2c.domain

import dev.tonholo.s2c.extensions.indented
import dev.tonholo.s2c.extensions.toInt

sealed class PathNodes(
Expand Down Expand Up @@ -39,7 +40,7 @@ sealed class PathNodes(
if (minified) it.trim() else it
}

protected fun Set<String>.toParameters(forceInline: Boolean = false): String {
private fun Set<String>.toParameters(forceInline: Boolean = false): String {
val indentSize = if (minified || forceInline) 0 else 4
val separator = if (minified || forceInline) "" else "\n"
val scape = if (minified || forceInline) " " else "|"
Expand All @@ -50,8 +51,6 @@ sealed class PathNodes(
}
}

private fun String.indented(indentSize: Int) = " ".repeat(indentSize) + this

class MoveTo(
values: List<String>,
isRelative: Boolean,
Expand Down
4 changes: 3 additions & 1 deletion shared/src/commonMain/kotlin/dev/tonholo/s2c/domain/Svg.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ fun SvgNode.asNode(masks: List<SvgNode.Mask>, minified: Boolean = false): ImageV
fun SvgNode.Path.asNode(minified: Boolean = false): ImageVectorNode.Path = ImageVectorNode.Path(
fillColor = fill.orEmpty(),
wrapper = d.asNodeWrapper(minified),
minified = minified,
)

fun SvgNode.Group.asNode(
Expand All @@ -96,6 +97,7 @@ fun SvgNode.Group.asNode(

return ImageVectorNode.Group(
clipPath = clipPath,
commands = commands.mapNotNull { it.asNode(masks, minified) }
commands = commands.mapNotNull { it.asNode(masks, minified) },
minified = minified,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ fun String.camelCase(): String = replaceDividers()

fun String.pascalCase(): String = replaceDividers()
.replaceFirstChar { it.uppercaseChar() }

fun String.indented(indentSize: Int) = " ".repeat(indentSize) + this

0 comments on commit 475a620

Please sign in to comment.