Frontend Visual Theme Spec and Retirement of Host-Consumed Semantic CSS
This commit is contained in:
+57
-25
@@ -4,57 +4,80 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record FrontendSemanticPresentationSpec(
|
||||
List<String> semanticKeys,
|
||||
List<FrontendVisualThemeSpec> themes,
|
||||
String defaultThemeId,
|
||||
List<FrontendHostProjectionSpec> hostProjections) {
|
||||
|
||||
public FrontendSemanticPresentationSpec(
|
||||
final List<String> semanticKeys,
|
||||
final List<FrontendVisualThemeSpec> themes,
|
||||
final String defaultThemeId) {
|
||||
this(semanticKeys, themes, defaultThemeId, List.of());
|
||||
this(themes, defaultThemeId, List.of());
|
||||
}
|
||||
|
||||
public FrontendSemanticPresentationSpec {
|
||||
semanticKeys = List.copyOf(Objects.requireNonNull(semanticKeys, "semanticKeys"));
|
||||
themes = List.copyOf(Objects.requireNonNull(themes, "themes"));
|
||||
defaultThemeId = Objects.requireNonNull(defaultThemeId, "defaultThemeId").trim();
|
||||
hostProjections = List.copyOf(Objects.requireNonNull(hostProjections, "hostProjections"));
|
||||
if (semanticKeys.isEmpty() && themes.isEmpty()) {
|
||||
if (themes.isEmpty()) {
|
||||
if (!defaultThemeId.isEmpty()) {
|
||||
throw new IllegalArgumentException("defaultThemeId must be blank when no themes are declared");
|
||||
}
|
||||
if (!hostProjections.isEmpty()) {
|
||||
throw new IllegalArgumentException("hostProjections must be empty when no themes are declared");
|
||||
}
|
||||
} else {
|
||||
defaultThemeId = requireText(defaultThemeId, "defaultThemeId");
|
||||
if (themes.isEmpty()) {
|
||||
throw new IllegalArgumentException("themes must not be empty");
|
||||
}
|
||||
if (themes.stream().map(FrontendVisualThemeSpec::themeId).noneMatch(defaultThemeId::equals)) {
|
||||
final String resolvedDefaultThemeId = defaultThemeId;
|
||||
if (themes.stream().map(FrontendVisualThemeSpec::themeId).noneMatch(resolvedDefaultThemeId::equals)) {
|
||||
throw new IllegalArgumentException("defaultThemeId must reference one of the declared themes");
|
||||
}
|
||||
}
|
||||
final List<String> normalizedSemanticKeys = semanticKeys.stream()
|
||||
.map(key -> requireText(key, "semanticKeys entry"))
|
||||
.toList();
|
||||
final var hostIds = new java.util.LinkedHashSet<String>();
|
||||
for (final FrontendHostProjectionSpec hostProjection : hostProjections) {
|
||||
if (!hostIds.add(hostProjection.hostId())) {
|
||||
throw new IllegalArgumentException("duplicate host projection for hostId: " + hostProjection.hostId());
|
||||
}
|
||||
for (final FrontendHostProjectionEntrySpec tokenProjection : hostProjection.tokenProjections()) {
|
||||
if (!normalizedSemanticKeys.contains(tokenProjection.semanticKey())) {
|
||||
|
||||
final List<String> derivedSemanticKeys = themes.stream()
|
||||
.filter(theme -> theme.themeId().equals(resolvedDefaultThemeId))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new IllegalStateException("defaultThemeId must resolve to a declared theme"))
|
||||
.tokenStyles()
|
||||
.stream()
|
||||
.map(FrontendTokenStyleSpec::semanticKey)
|
||||
.toList();
|
||||
|
||||
for (final FrontendVisualThemeSpec theme : themes) {
|
||||
final List<String> themeSemanticKeys = theme.tokenStyles().stream()
|
||||
.map(FrontendTokenStyleSpec::semanticKey)
|
||||
.toList();
|
||||
if (!themeSemanticKeys.equals(derivedSemanticKeys)) {
|
||||
throw new IllegalArgumentException(
|
||||
"host projection for hostId '" + hostProjection.hostId() + "' references unknown semantic key: "
|
||||
+ tokenProjection.semanticKey());
|
||||
"theme '" + theme.themeId() + "' must publish the same semantic keys as the default theme");
|
||||
}
|
||||
}
|
||||
|
||||
final var hostIds = new java.util.LinkedHashSet<String>();
|
||||
for (final FrontendHostProjectionSpec hostProjection : hostProjections) {
|
||||
if (!hostIds.add(hostProjection.hostId())) {
|
||||
throw new IllegalArgumentException(
|
||||
"duplicate host projection for hostId: " + hostProjection.hostId());
|
||||
}
|
||||
final List<String> projectedSemanticKeys = hostProjection.tokenProjections().stream()
|
||||
.map(FrontendHostProjectionEntrySpec::semanticKey)
|
||||
.toList();
|
||||
if (!projectedSemanticKeys.equals(derivedSemanticKeys)) {
|
||||
throw new IllegalArgumentException(
|
||||
"host projection for hostId '" + hostProjection.hostId()
|
||||
+ "' must publish the same semantic keys as the default theme");
|
||||
}
|
||||
for (final FrontendHostProjectionEntrySpec tokenProjection : hostProjection.tokenProjections()) {
|
||||
if (!derivedSemanticKeys.contains(tokenProjection.semanticKey())) {
|
||||
throw new IllegalArgumentException(
|
||||
"host projection for hostId '" + hostProjection.hostId() + "' references unknown semantic key: "
|
||||
+ tokenProjection.semanticKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
semanticKeys = normalizedSemanticKeys;
|
||||
}
|
||||
|
||||
public static FrontendSemanticPresentationSpec empty() {
|
||||
return new FrontendSemanticPresentationSpec(List.of(), List.of(), "", List.of());
|
||||
return new FrontendSemanticPresentationSpec(List.of(), "", List.of());
|
||||
}
|
||||
|
||||
public FrontendVisualThemeSpec defaultTheme() {
|
||||
@@ -68,7 +91,16 @@ public record FrontendSemanticPresentationSpec(
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return semanticKeys.isEmpty() && themes.isEmpty() && defaultThemeId.isEmpty() && hostProjections.isEmpty();
|
||||
return themes.isEmpty() && defaultThemeId.isEmpty() && hostProjections.isEmpty();
|
||||
}
|
||||
|
||||
public List<String> semanticKeys() {
|
||||
if (themes.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
return defaultTheme().tokenStyles().stream()
|
||||
.map(FrontendTokenStyleSpec::semanticKey)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public java.util.Optional<FrontendHostProjectionSpec> hostProjection(final String hostId) {
|
||||
|
||||
+18
-3
@@ -12,7 +12,6 @@ final class FrontendSemanticPresentationSpecTest {
|
||||
@Test
|
||||
void shouldExposeHostProjectionByHostId() {
|
||||
final var presentation = new FrontendSemanticPresentationSpec(
|
||||
List.of("demo-key"),
|
||||
List.of(new FrontendVisualThemeSpec(
|
||||
"demo-theme",
|
||||
"Demo Theme",
|
||||
@@ -37,7 +36,6 @@ final class FrontendSemanticPresentationSpecTest {
|
||||
@Test
|
||||
void shouldRejectDuplicateHostIds() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new FrontendSemanticPresentationSpec(
|
||||
List.of("demo-key"),
|
||||
List.of(new FrontendVisualThemeSpec(
|
||||
"demo-theme",
|
||||
"Demo Theme",
|
||||
@@ -66,7 +64,6 @@ final class FrontendSemanticPresentationSpecTest {
|
||||
@Test
|
||||
void shouldRejectProjectionForUnknownSemanticKey() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new FrontendSemanticPresentationSpec(
|
||||
List.of("demo-key"),
|
||||
List.of(new FrontendVisualThemeSpec(
|
||||
"demo-theme",
|
||||
"Demo Theme",
|
||||
@@ -82,4 +79,22 @@ final class FrontendSemanticPresentationSpecTest {
|
||||
"variable",
|
||||
List.of()))))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectThemeSetThatDriftsFromDefaultThemeVocabulary() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new FrontendSemanticPresentationSpec(
|
||||
List.of(
|
||||
new FrontendVisualThemeSpec(
|
||||
"demo-theme",
|
||||
"Demo Theme",
|
||||
new FrontendEditorPaletteSpec("#111111", "#eeeeee", "#333333", "#777777", "#111111", "#222222", "#eeeeee"),
|
||||
List.of(new FrontendTokenStyleSpec("demo-key", "#eeeeee", false, false, false))),
|
||||
new FrontendVisualThemeSpec(
|
||||
"demo-theme-2",
|
||||
"Demo Theme 2",
|
||||
new FrontendEditorPaletteSpec("#111111", "#eeeeee", "#333333", "#777777", "#111111", "#222222", "#eeeeee"),
|
||||
List.of(new FrontendTokenStyleSpec("other-key", "#eeeeee", false, false, false)))),
|
||||
"demo-theme",
|
||||
List.of()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user