Skip to content

Commit

Permalink
feat: Pass inline styles to JS (#19078)
Browse files Browse the repository at this point in the history
Publish node info only for dev mode
  • Loading branch information
Artur- committed Apr 3, 2024
1 parent c263d4f commit cc01569
Showing 1 changed file with 36 additions and 6 deletions.
Expand Up @@ -25,6 +25,7 @@
import com.vaadin.client.communication.ReconnectConfiguration;
import com.vaadin.client.flow.StateNode;
import com.vaadin.client.flow.binding.Binder;
import com.vaadin.client.flow.collection.JsArray;
import com.vaadin.client.flow.dom.DomApi;
import com.vaadin.client.flow.util.NativeFunction;
import com.vaadin.flow.internal.nodefeature.NodeFeatures;
Expand Down Expand Up @@ -168,12 +169,6 @@ private native void publishJavascriptMethods(String applicationId,
client.getByNodeId = $entry(function(nodeId) {
return ap.@ApplicationConnection::getDomElementByNodeId(*)(nodeId);
});
client.getNodeInfo = $entry(function(nodeId) {
return {
element: ap.@ApplicationConnection::getDomElementByNodeId(*)(nodeId),
javaClass: ap.@ApplicationConnection::getJavaClass(*)(nodeId)
};
});
client.getNodeId = $entry(function(element) {
return ap.@ApplicationConnection::getNodeId(*)(element);
});
Expand Down Expand Up @@ -241,6 +236,34 @@ private String getJavaClass(int id) {
.getValueOrDefault(null);
}

public static final class Styles extends JavaScriptObject {
protected Styles() {

}

public final native void set(String key, Object value)/*-{
this[key] = value;
}-*/;
}

private JavaScriptObject getElementStyleProperties(int id) {
StateNode node = registry.getStateTree().getNode(id);
Styles styles = JavaScriptObject.createObject().cast();
if (node != null) {
JsArray<String> names = node
.getMap(NodeFeatures.ELEMENT_STYLE_PROPERTIES)
.getPropertyNames();
for (int i = 0; i < names.length(); i++) {
String name = names.get(i);
styles.set(name,
node.getMap(NodeFeatures.ELEMENT_STYLE_PROPERTIES)
.getProperty(name).getValue());

}
}
return styles;
}

private int getNodeId(Element element) {
StateNode node = registry.getStateTree()
.getStateNodeForDomNode(DomApi.wrap(element));
Expand Down Expand Up @@ -280,6 +303,13 @@ private native void publishDevelopmentModeJavascriptMethods(
var registry = ap.@ApplicationConnection::registry;
return registry.@com.vaadin.client.Registry::getStateTree()().@com.vaadin.client.flow.StateTree::getRootNode()().@com.vaadin.client.flow.StateNode::getDebugJson()();
});
client.getNodeInfo = $entry(function(nodeId) {
return {
element: ap.@ApplicationConnection::getDomElementByNodeId(*)(nodeId),
javaClass: ap.@ApplicationConnection::getJavaClass(*)(nodeId),
styles: ap.@ApplicationConnection::getElementStyleProperties(*)(nodeId)
};
});
}-*/;

Expand Down

0 comments on commit cc01569

Please sign in to comment.