-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
256 additions
and
40 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
"scalawind": minor | ||
--- | ||
|
||
add class optimization for padding and margin utilities |
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
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
68 changes: 68 additions & 0 deletions
68
packages/scalawind/src/generate/templates/classesValidation.hbs
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,68 @@ | ||
{{#if classesValidation}} | ||
def validate(classes: List[String]): Unit = { | ||
checkDuplication(classes) | ||
val optimizationSuggestions = checkOptimization(classes) | ||
if optimizationSuggestions.nonEmpty then | ||
report.errorAndAbort(s"[Optimization] ${optimizationSuggestions.mkString(", ")}") | ||
} | ||
|
||
def checkDuplication(classes: List[String]): Unit = { | ||
val duplicates = classes.groupBy(identity).collect { case (x, List(_, _, _*)) => x } | ||
if duplicates.nonEmpty then | ||
report.errorAndAbort(s"[Duplication] ${duplicates.mkString(", ")}") | ||
} | ||
|
||
def checkOptimization(classes: List[String]): List[String] = { | ||
val properties = List("p", "m") | ||
|
||
val suggestions = properties.flatMap { property => | ||
val propertySuggestions = scala.collection.mutable.ListBuffer.empty[String] | ||
|
||
val classMap = classes.map { | ||
case c if c.startsWith(s"${property}t-") => s"${property}t" -> c | ||
case c if c.startsWith(s"${property}b-") => s"${property}b" -> c | ||
case c if c.startsWith(s"${property}l-") => s"${property}l" -> c | ||
case c if c.startsWith(s"${property}r-") => s"${property}r" -> c | ||
case c if c.startsWith(s"${property}x-") => s"${property}x" -> c | ||
case c if c.startsWith(s"${property}y-") => s"${property}y" -> c | ||
case c if c.startsWith(s"${property}-") => property -> c | ||
case c => c -> c | ||
}.groupBy(_._1).view.mapValues(_.map(_._2)).toMap | ||
|
||
def checkAndSuggest(key1: String, key2: String, combined: String): Unit = { | ||
(classMap.get(key1), classMap.get(key2)) match | ||
case (Some(List(c1)), Some(List(c2))) if c1.substring(3) == c2.substring(3) => | ||
propertySuggestions += s"Use $combined${c1.substring(3)} instead of $c1 and $c2" | ||
case _ => () | ||
} | ||
|
||
def checkFourWay(): Unit = { | ||
(classMap.get(s"${property}t"), classMap.get(s"${property}b"), classMap.get(s"${property}l"), classMap.get(s"${property}r")) match | ||
case (Some(List(pt)), Some(List(pb)), Some(List(pl)), Some(List(pr))) if pt.substring(3) == pb.substring(3) && pl.substring(3) == pr.substring(3) && pt.substring(3) == pl.substring(3) => | ||
propertySuggestions += s"Use ${property}-${pt.substring(3)} instead of $pt, $pb, $pl and $pr" | ||
case _ => () | ||
} | ||
|
||
def checkPxPy(): Unit = { | ||
(classMap.get(s"${property}x"), classMap.get(s"${property}y")) match | ||
case (Some(List(px)), Some(List(py))) if px.substring(3) == py.substring(3) => | ||
propertySuggestions += s"Use ${property}-${px.substring(3)} instead of $px and $py" | ||
case _ => () | ||
} | ||
|
||
// Check for four-way combination first | ||
checkFourWay() | ||
|
||
// Only check for two-way combinations if no four-way combination is found | ||
if propertySuggestions.isEmpty then | ||
checkPxPy() | ||
if propertySuggestions.isEmpty then | ||
checkAndSuggest(s"${property}t", s"${property}b", s"${property}y-") | ||
checkAndSuggest(s"${property}l", s"${property}r", s"${property}x-") | ||
|
||
propertySuggestions.toList | ||
} | ||
|
||
suggestions | ||
} | ||
{{/if}} |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{{#if laminar}} | ||
import com.raquo.laminar.api.L.cls | ||
|
||
object clx: | ||
object clx { | ||
def apply(styles: String) = cls(styles) | ||
def :=(styles: String) = cls(styles) | ||
} | ||
{{/if}} |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{{#if scalajsReact}} | ||
import japgolly.scalajs.react.vdom.html_<^.* | ||
|
||
object clx: | ||
object clx { | ||
def apply(styles: String): TagMod = ^.cls := styles | ||
def :=(styles: String): TagMod = ^.cls := styles | ||
} | ||
{{/if}} |
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
Oops, something went wrong.