forked from liferay/liferay-portal
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LPS-115484 As a developer I can declare theme's customizable CSS variables #17
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2ae1e1f
LPS-115484 Add an API to retrieve configurable CSS variables for a theme
izaera 4f30d70
LPS-115484 Implement the API to retrieve configurable CSS variables f…
izaera 51f0e0c
LPS-115484 Add tests
izaera 6068dcb
LPS-115484 Read the css-variables.json file path from package.json
izaera 2066d40
LPS-115484 Use an AtomicReference for CSS variable descriptions
izaera ff4655d
LPS-115484 Remove CSS variable localization support
izaera 7246433
LPS-115484 Make path to CSS variables file configurable
izaera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
Bundle-Name: Liferay Frontend CSS Variables API | ||
Bundle-SymbolicName: com.liferay.frontend.css.variables.api | ||
Bundle-Version: 1.0.0 | ||
Export-Package: com.liferay.frontend.css.variables | ||
Export-Package:\ | ||
com.liferay.frontend.css.variables,\ | ||
com.liferay.frontend.css.variables.theme |
26 changes: 26 additions & 0 deletions
26
...ariables-api/src/main/java/com/liferay/frontend/css/variables/CSSVariableDescription.java
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,26 @@ | ||
/** | ||
* Copyright (c) 2000-present Liferay, Inc. All rights reserved. | ||
* | ||
* This library is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation; either version 2.1 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
* details. | ||
*/ | ||
|
||
package com.liferay.frontend.css.variables; | ||
|
||
/** | ||
* @author Iván Zaera Avellón | ||
*/ | ||
public interface CSSVariableDescription { | ||
|
||
public CSSVariableType getCSSVariableType(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
public String getName(); | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...d-css-variables-api/src/main/java/com/liferay/frontend/css/variables/CSSVariableType.java
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,24 @@ | ||
/** | ||
* Copyright (c) 2000-present Liferay, Inc. All rights reserved. | ||
* | ||
* This library is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation; either version 2.1 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
* details. | ||
*/ | ||
|
||
package com.liferay.frontend.css.variables; | ||
|
||
/** | ||
* @author Iván Zaera Avellón | ||
*/ | ||
public enum CSSVariableType { | ||
|
||
COLOR, STRING | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...n/java/com/liferay/frontend/css/variables/theme/ThemeCSSVariableDescriptionsRegistry.java
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,30 @@ | ||
/** | ||
* Copyright (c) 2000-present Liferay, Inc. All rights reserved. | ||
* | ||
* This library is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation; either version 2.1 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
* details. | ||
*/ | ||
|
||
package com.liferay.frontend.css.variables.theme; | ||
|
||
import com.liferay.frontend.css.variables.CSSVariableDescription; | ||
import com.liferay.portal.kernel.model.Theme; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author Iván Zaera Avellón | ||
*/ | ||
public interface ThemeCSSVariableDescriptionsRegistry { | ||
|
||
public Map<String, CSSVariableDescription> getCSSVariableDescriptions( | ||
Theme theme); | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...css-variables-api/src/main/resources/com/liferay/frontend/css/variables/theme/packageinfo
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 @@ | ||
version 1.0.0 |
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
45 changes: 45 additions & 0 deletions
45
...main/java/com/liferay/frontend/css/variables/web/internal/CSSVariableDescriptionImpl.java
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,45 @@ | ||
/** | ||
* Copyright (c) 2000-present Liferay, Inc. All rights reserved. | ||
* | ||
* This library is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation; either version 2.1 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
* details. | ||
*/ | ||
|
||
package com.liferay.frontend.css.variables.web.internal; | ||
|
||
import com.liferay.frontend.css.variables.CSSVariableDescription; | ||
import com.liferay.frontend.css.variables.CSSVariableType; | ||
|
||
/** | ||
* @author Iván Zaera Avellón | ||
*/ | ||
public class CSSVariableDescriptionImpl implements CSSVariableDescription { | ||
|
||
public CSSVariableDescriptionImpl( | ||
CSSVariableType cssVariableType, String name) { | ||
|
||
_cssVariableType = cssVariableType; | ||
_name = name; | ||
} | ||
|
||
@Override | ||
public CSSVariableType getCSSVariableType() { | ||
return _cssVariableType; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return _name; | ||
} | ||
|
||
private final CSSVariableType _cssVariableType; | ||
private final String _name; | ||
|
||
} |
172 changes: 172 additions & 0 deletions
172
...main/java/com/liferay/frontend/css/variables/web/internal/theme/ThemeBundleInspector.java
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,172 @@ | ||
/** | ||
* Copyright (c) 2000-present Liferay, Inc. All rights reserved. | ||
* | ||
* This library is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU Lesser General Public License as published by the Free | ||
* Software Foundation; either version 2.1 of the License, or (at your option) | ||
* any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
* details. | ||
*/ | ||
|
||
package com.liferay.frontend.css.variables.web.internal.theme; | ||
|
||
import com.liferay.frontend.css.variables.CSSVariableDescription; | ||
import com.liferay.frontend.css.variables.CSSVariableType; | ||
import com.liferay.frontend.css.variables.web.internal.CSSVariableDescriptionImpl; | ||
import com.liferay.petra.string.StringBundler; | ||
import com.liferay.portal.kernel.json.JSONArray; | ||
import com.liferay.portal.kernel.json.JSONException; | ||
import com.liferay.portal.kernel.json.JSONFactory; | ||
import com.liferay.portal.kernel.json.JSONObject; | ||
import com.liferay.portal.kernel.util.LocaleUtil; | ||
import com.liferay.portal.kernel.util.StringUtil; | ||
import com.liferay.portal.kernel.util.Validator; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import java.net.URL; | ||
|
||
import java.util.Dictionary; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
import org.osgi.framework.Bundle; | ||
|
||
/** | ||
* @author Iván Zaera Avellón | ||
*/ | ||
public class ThemeBundleInspector { | ||
|
||
public static boolean isTheme(Bundle bundle) { | ||
izaera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
boolean theme = false; | ||
|
||
URL url = bundle.getResource("WEB-INF/liferay-look-and-feel.xml"); | ||
|
||
if (url != null) { | ||
try (InputStream inputStream = url.openStream()) { | ||
String xml = StringUtil.read(inputStream); | ||
|
||
if (xml.contains("</theme>")) { | ||
theme = true; | ||
} | ||
} | ||
catch (IOException ioException) { | ||
throw new RuntimeException(ioException); | ||
} | ||
} | ||
|
||
return theme; | ||
} | ||
|
||
/** | ||
* @throws IllegalArgumentException if the bundle is not a theme bundle | ||
*/ | ||
public ThemeBundleInspector(Bundle bundle, JSONFactory jsonFactory) | ||
throws IllegalArgumentException { | ||
|
||
_bundle = bundle; | ||
_jsonFactory = jsonFactory; | ||
|
||
if (!isTheme(bundle)) { | ||
throw new IllegalArgumentException( | ||
StringBundler.concat( | ||
"Bundle ", _bundle.getSymbolicName(), " is not a theme")); | ||
} | ||
} | ||
|
||
public Map<String, CSSVariableDescription> getCSSVariableDescriptions() | ||
throws JSONException { | ||
|
||
JSONObject jsonObject = _parseJSONObject(_getTokensPath()); | ||
|
||
if (jsonObject == null) { | ||
return null; | ||
} | ||
|
||
JSONArray variablesJSONArray = jsonObject.getJSONArray("variables"); | ||
|
||
if (variablesJSONArray == null) { | ||
return null; | ||
} | ||
|
||
Map<String, CSSVariableDescription> cssVariableDescriptions = | ||
new HashMap<>(); | ||
|
||
for (int i = 0; i < variablesJSONArray.length(); i++) { | ||
JSONObject cssVariableDefinitionJSONObject = | ||
variablesJSONArray.getJSONObject(i); | ||
|
||
String name = cssVariableDefinitionJSONObject.getString("name"); | ||
|
||
cssVariableDescriptions.put( | ||
name, | ||
new CSSVariableDescriptionImpl( | ||
_getCSSVariableType(cssVariableDefinitionJSONObject), | ||
name)); | ||
} | ||
|
||
if (cssVariableDescriptions.isEmpty()) { | ||
return null; | ||
} | ||
|
||
return cssVariableDescriptions; | ||
} | ||
|
||
public String getSymbolicName() { | ||
return _bundle.getSymbolicName(); | ||
} | ||
|
||
private CSSVariableType _getCSSVariableType(JSONObject jsonObject) { | ||
String type = jsonObject.getString("type"); | ||
|
||
if (type.equals("color")) { | ||
return CSSVariableType.COLOR; | ||
} | ||
|
||
return CSSVariableType.STRING; | ||
} | ||
|
||
private String _getTokensPath() { | ||
Locale defaultLocale = LocaleUtil.getDefault(); | ||
|
||
Dictionary<String, String> headers = _bundle.getHeaders( | ||
defaultLocale.toString()); | ||
|
||
String tokensPath = headers.get("Tokens-Path"); | ||
|
||
if (Validator.isNull(tokensPath)) { | ||
tokensPath = "WEB-INF/tokens.json"; | ||
} | ||
|
||
if (tokensPath.charAt(0) == '/') { | ||
tokensPath = tokensPath.substring(1); | ||
} | ||
|
||
return tokensPath; | ||
} | ||
|
||
private JSONObject _parseJSONObject(String fileName) throws JSONException { | ||
URL resource = _bundle.getResource(fileName); | ||
|
||
if (resource == null) { | ||
return null; | ||
} | ||
|
||
try (InputStream is = resource.openStream()) { | ||
return _jsonFactory.createJSONObject(StringUtil.read(is)); | ||
} | ||
catch (IOException ioException) { | ||
throw new JSONException("Unable to parse " + fileName, ioException); | ||
} | ||
} | ||
|
||
private final Bundle _bundle; | ||
private final JSONFactory _jsonFactory; | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be a TokenDescription now...