Fix error when opening the first document if project view was active

This commit is contained in:
Francesco Abbate 2020-11-16 18:12:31 +01:00
parent edaa8fb42b
commit 7a856be6e4
1 changed files with 22 additions and 3 deletions

View File

@ -396,14 +396,33 @@ function RootView:get_active_node()
return self.root_node:get_node_for_view(core.active_view) return self.root_node:get_node_for_view(core.active_view)
end end
-- Get un unlocked node with at least one view.
local function get_node_unlocked(node)
if not node.locked and #node.views > 0 then
return node
end
if node.type ~= "leaf" then
local a = get_node_unlocked(node.a)
if a then return a end
return get_node_unlocked(node.b)
end
end
function RootView:get_document_view()
local node = get_node_unlocked(self.root_node)
if node then
return node.views[1]
end
end
function RootView:open_doc(doc) function RootView:open_doc(doc)
local node = self:get_active_node() local node = self:get_active_node()
if node.locked and core.last_active_view then if node.locked then
core.set_active_view(core.last_active_view) local default_view = self:get_document_view()
assert(default_view, "Cannot find an unlocked node to open the document.")
core.set_active_view(default_view)
node = self:get_active_node() node = self:get_active_node()
end end
assert(not node.locked, "Cannot open doc on locked node")
for i, view in ipairs(node.views) do for i, view in ipairs(node.views) do
if view.doc == doc then if view.doc == doc then
node:set_active_view(node.views[i]) node:set_active_view(node.views[i])