Skip to content
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

cherry pick to V3.3.x #4151

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

#include <string>
#include <vector>
#include "api/adapter/data/serializable.h"
#include "nlohmann/json.hpp"

namespace hippy::devtools {

class DomainMetas : public Serializable {
class DomainMetas {
public:
DomainMetas() = default;
explicit DomainMetas(uint32_t node_id) : node_id_(node_id) {}
Expand All @@ -44,7 +44,7 @@ class DomainMetas : public Serializable {
inline void SetNodeValue(std::string node_value) { node_value_ = node_value; }
inline void SetChildrenCount(uint64_t children_count) { children_count_ = children_count; }
inline void AddChild(const DomainMetas& meta) { children_.emplace_back(meta); }
std::string Serialize() const override;
nlohmann::json ToJson() const;

private:
uint32_t node_id_;
Expand Down
88 changes: 19 additions & 69 deletions devtools/devtools-backend/src/api/adapter/data/domain_metas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,78 +36,28 @@ constexpr char kLayoutX[] = "x";
constexpr char kLayoutY[] = "y";
constexpr char kStyle[] = "style";

std::string DomainMetas::Serialize() const {
std::string node_str = "{\"";
node_str += kNodeId;
node_str += "\":";
node_str += std::to_string(node_id_);
node_str += ",\"";
node_str += kParentId;
node_str += "\":";
node_str += std::to_string(parent_id_);
node_str += ",\"";
node_str += kRootId;
node_str += "\":";
node_str += std::to_string(root_id_);
node_str += ",\"";
node_str += kClassName;
node_str += "\":\"";
node_str += class_name_;
node_str += "\",\"";
node_str += kNodeName;
node_str += "\":\"";
node_str += node_name_;
node_str += "\",\"";
node_str += kLocalName;
node_str += "\":\"";
node_str += local_name_;
node_str += "\",\"";
node_str += kNodeValue;
node_str += "\":\"";
node_str += node_value_;
node_str += "\",\"";
node_str += kChildNodeCount;
node_str += "\":";
node_str += std::to_string(children_.size());
node_str += ",\"";
node_str += kStyle;
node_str += "\":";
node_str += style_props_;
node_str += ",\"";
node_str += kAttributes;
node_str += "\":";
node_str += total_props_;
node_str += ",\"";
node_str += kLayoutX;
node_str += "\":";
node_str += std::to_string(static_cast<int>(layout_x_));
node_str += ",\"";
node_str += kLayoutY;
node_str += "\":";
node_str += std::to_string(static_cast<int>(layout_y_));
node_str += ",\"";
node_str += kWidth;
node_str += "\":";
node_str += std::to_string(static_cast<int>(width_));
node_str += ",\"";
node_str += kHeight;
node_str += "\":";
node_str += std::to_string(static_cast<int>(height_));
node_str += ",\"";
node_str += kChildNodeCount;
node_str += "\":";
node_str += std::to_string(static_cast<int>(children_count_));
nlohmann::json DomainMetas::ToJson() const {
nlohmann::json jsonObject;
jsonObject[kNodeId] = node_id_;
jsonObject[kParentId] = parent_id_;
jsonObject[kRootId] = root_id_;
jsonObject[kClassName] = class_name_;
jsonObject[kNodeName] = node_name_;
jsonObject[kLocalName] = local_name_;
jsonObject[kNodeValue] = node_value_;
jsonObject[kChildNodeCount] = children_.size();
jsonObject[kStyle] = nlohmann::json::parse(style_props_, nullptr, false);
jsonObject[kAttributes] = nlohmann::json::parse(total_props_, nullptr, false);
jsonObject[kLayoutX] = static_cast<int>(layout_x_);
jsonObject[kLayoutY] = static_cast<int>(layout_y_);
jsonObject[kWidth] = static_cast<int>(width_);
jsonObject[kHeight] = static_cast<int>(height_);
jsonObject[kChildNodeCount] = static_cast<int>(children_count_);
if (!children_.empty()) {
node_str += ",\"children\": [";
for (auto& child : children_) {
node_str += child.Serialize();
node_str += ",";
jsonObject["children"].push_back(child.ToJson());
}
node_str = node_str.substr(0, node_str.length() - 1); // remove last ","
node_str += "]";
}
node_str += "}";
return node_str;
return jsonObject;
}

} // namespace hippy::devtools
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void CssDomain::RegisterCallback() {
return;
}
auto response_callback = [callback, provider = self->GetDataProvider()](const DomainMetas& data) {
auto model = CssModel::CreateModel(nlohmann::json::parse(data.Serialize(), nullptr, false));
auto model = CssModel::CreateModel(data.ToJson());
model.SetDataProvider(provider);
if (callback) {
callback(model);
Expand Down
6 changes: 3 additions & 3 deletions devtools/devtools-backend/src/module/domain/dom_domain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void DomDomain::RegisterCallback() {
auto dom_tree_adapter = self->GetDataProvider()->dom_tree_adapter;
if (dom_tree_adapter) {
auto response_callback = [callback, provider = self->GetDataProvider()](const DomainMetas& data) {
auto model = DomModel::CreateModel(nlohmann::json::parse(data.Serialize(), nullptr, false));
auto model = DomModel::CreateModel(data.ToJson());
model.SetDataProvider(provider);
if (callback) {
callback(model);
Expand Down Expand Up @@ -172,8 +172,8 @@ void DomDomain::GetBoxModel(const DomNodeDataRequest& request) {
}

void DomDomain::GetNodeForLocation(const DomNodeForLocationRequest& request) {
if (!dom_data_call_back_) {
ResponseErrorToFrontend(request.GetId(), kErrorFailCode, "GetNodeForLocation, dom_data_callback is null");
if (!location_for_node_call_back_) {
ResponseErrorToFrontend(request.GetId(), kErrorFailCode, "GetNodeForLocation, location_for_node_call_back is null");
return;
}
if (!request.HasSetXY()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "api/adapter/data/domain_metas.h"
#include "dom/dom_manager.h"
#include "dom/dom_node.h"
#include "dom/root_node.h"

namespace hippy::devtools {
/**
Expand All @@ -45,7 +46,7 @@ class DevToolsUtil {
uint32_t depth,
const std::shared_ptr<DomManager>& dom_manager);

static DomNodeLocation GetNodeIdByDomLocation(const std::shared_ptr<DomNode>& root_node, double x, double y);
static DomNodeLocation GetNodeIdByDomLocation(const std::shared_ptr<RootNode>& root_node, double x, double y);

static DomPushNodePathMetas GetPushNodeByPath(const std::shared_ptr<DomNode>& dom_node,
std::vector<std::map<std::string, int32_t>> path);
Expand All @@ -55,12 +56,12 @@ class DevToolsUtil {
static bool ShouldAvoidPostDomManagerTask(const std::string& event_name);

private:
static std::shared_ptr<DomNode> GetHitNode(const std::shared_ptr<DomNode>& root_node, const std::shared_ptr<DomNode>& node, double x, double y);
static bool IsLocationHitNode(const std::shared_ptr<DomNode>& root_node, const std::shared_ptr<DomNode>& dom_node, double x, double y);
static hippy::dom::DomArgument MakeLocationArgument(double x, double y);
static std::shared_ptr<DomNode> GetHitNode(const std::shared_ptr<RootNode>& root_node, double x, double y);
static std::string ParseNodeKeyProps(const std::string& node_key, const NodePropsUnorderedMap& node_props);
static std::string ParseNodeProps(const NodePropsUnorderedMap& node_props);
static std::string ParseNodeProps(const std::unordered_map<std::string, HippyValue>& node_props);
static std::string ParseDomValue(const HippyValue& value);
static std::string DumpHippyValue(const HippyValue& value);
static void AppendDomKeyValue(std::string& node_str,
bool& first_object,
const std::string& node_key,
Expand Down
Loading
Loading