refactor(new_editor): streamline internal layout and command routing
This commit is contained in:
@@ -79,6 +79,46 @@ bool ExtractNodeRecursive(
|
||||
return false;
|
||||
}
|
||||
|
||||
HierarchyNode CloneNodeRecursive(
|
||||
const HierarchyNode& source,
|
||||
const std::function<std::string()>& allocateNodeId) {
|
||||
HierarchyNode duplicate = {};
|
||||
duplicate.nodeId = allocateNodeId();
|
||||
duplicate.label = source.label;
|
||||
duplicate.children.reserve(source.children.size());
|
||||
for (const HierarchyNode& child : source.children) {
|
||||
duplicate.children.push_back(CloneNodeRecursive(child, allocateNodeId));
|
||||
}
|
||||
return duplicate;
|
||||
}
|
||||
|
||||
bool DuplicateNodeRecursive(
|
||||
std::vector<HierarchyNode>& nodes,
|
||||
std::string_view nodeId,
|
||||
const std::function<std::string()>& allocateNodeId,
|
||||
std::string& duplicatedNodeId) {
|
||||
for (std::size_t index = 0u; index < nodes.size(); ++index) {
|
||||
if (nodes[index].nodeId == nodeId) {
|
||||
HierarchyNode duplicate = CloneNodeRecursive(nodes[index], allocateNodeId);
|
||||
duplicatedNodeId = duplicate.nodeId;
|
||||
nodes.insert(
|
||||
nodes.begin() + static_cast<std::ptrdiff_t>(index + 1u),
|
||||
std::move(duplicate));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (DuplicateNodeRecursive(
|
||||
nodes[index].children,
|
||||
nodeId,
|
||||
allocateNodeId,
|
||||
duplicatedNodeId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void BuildTreeItemsRecursive(
|
||||
const std::vector<HierarchyNode>& nodes,
|
||||
std::uint32_t depth,
|
||||
@@ -178,6 +218,22 @@ std::string HierarchyModel::CreateChild(
|
||||
return parent->children.back().nodeId;
|
||||
}
|
||||
|
||||
std::string HierarchyModel::DuplicateNode(std::string_view nodeId) {
|
||||
if (nodeId.empty() || !ContainsNode(nodeId)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string duplicatedNodeId = {};
|
||||
const auto allocateNodeId = [this]() {
|
||||
return AllocateNodeId();
|
||||
};
|
||||
if (!DuplicateNodeRecursive(m_roots, nodeId, allocateNodeId, duplicatedNodeId)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return duplicatedNodeId;
|
||||
}
|
||||
|
||||
bool HierarchyModel::DeleteNode(std::string_view nodeId) {
|
||||
if (nodeId.empty()) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user