Skip to content

Commit

Permalink
Properly handle multiple sidebar columns
Browse files Browse the repository at this point in the history
This is a valid layout:

````

## Sidebar {.sidebar}

Sidebar controls

## Column

```{r}
plot(cars)
```

## Column

```{r}
plot(mtcars)
```

````
  • Loading branch information
dragonstyle committed Nov 2, 2023
1 parent 8d7a885 commit b368e19
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
28 changes: 4 additions & 24 deletions src/resources/filters/modules/dashboard/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
-- Copyright (C) 2020-2022 Posit Software, PBC

local document = require "modules/dashboard/document"
local sidebar = require "modules/dashboard/sidebar"

-- Layout classes
local kRowsClass = "rows"
Expand Down Expand Up @@ -153,28 +152,6 @@ local function orientContents(contents, toOrientation, options)
end
end

function inferOrientation(el)
-- force the global orientation to columns if there is a sidebar present
local hasSidebar = false
_quarto.ast.walk(el, {
Header = function(header)
if header.level == 1 and sidebar.isSidebar(header) then
hasSidebar = true
end
end,
Div = function(div)
if sidebar.isSidebar(div) then
hasSidebar = true
end
end
})
if hasSidebar then
return kOrientationColumns
else
return nil
end
end



return {
Expand All @@ -187,5 +164,8 @@ return {
orientContents = orientContents,
readOptions = readOptions,
makeOptions = makeOptions,
inferOrientation = inferOrientation
orientations = {
columns = kOrientationColumns,
rows = kOrientationRows
}
}
4 changes: 2 additions & 2 deletions src/resources/filters/modules/dashboard/page.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- page.lua
-- Copyright (C) 2020-2022 Posit Software, PBC
local document = require "modules/dashboard/document"
local layout = require "modules/dashboard/layout"
local sidebar = require "modules/dashboard/sidebar"

local kPageClass = "dashboard-page"

Expand Down Expand Up @@ -43,7 +43,7 @@ local function makePage(id, headerEl, contents, options)
local tabContentsOrientation = options[kOrientationAttr];

-- Infer orientation by seeing 'sidebar' on a row
local inferred = layout.inferOrientation(pandoc.Div(contents))
local inferred = sidebar.maybeUseSidebarOrientation(pandoc.Div(contents))
if inferred ~= nil then
tabContentsOrientation = inferred
end
Expand Down
30 changes: 28 additions & 2 deletions src/resources/filters/modules/dashboard/sidebar.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- sidebar.lua
-- Copyright (C) 2020-2022 Posit Software, PBC
local card = require "modules/dashboard/card"
local layout = require "modules/dashboard/layout"


-- left or right position (or both)
Expand Down Expand Up @@ -55,7 +56,8 @@ local function makeSidebar(sidebarEls, contentEls, options)
-- TODO: forward title
local sidebarEl = pandoc.Div(sidebarContentsFiltered, pandoc.Attr("", {kSidebarClass}, sidebarAttr(options)))

local sidebarContentsEl = pandoc.Div(contentEls, pandoc.Attr("", {kSidebarContentClass}))
local sidebarColumns = layout.orientContents(contentEls, layout.orientations.columns, {})
local sidebarContentsEl = pandoc.Div(sidebarColumns, pandoc.Attr("", {kSidebarContentClass}))
sidebarContainerEl.content:extend({sidebarEl, sidebarContentsEl})

return sidebarContainerEl
Expand All @@ -66,11 +68,35 @@ local function pageSidebarPlaceholder(contents, options)
return sidebarContainer
end

function maybeUseSidebarOrientation(el)
-- force the global orientation to columns if there is a sidebar present
local hasSidebar = false
_quarto.ast.walk(el, {
Header = function(header)
if header.level == 1 and isSidebar(header) then
hasSidebar = true
end
end,
Div = function(div)
if isSidebar(div) then
hasSidebar = true
end
end
})
if hasSidebar then
return layout.orientations.columns
else
return nil
end
end


return {
isSidebar = isSidebar,
readOptions = readOptions,
makeSidebar = makeSidebar,
pageSidebarPlaceholder = pageSidebarPlaceholder
pageSidebarPlaceholder = pageSidebarPlaceholder,
maybeUseSidebarOrientation = maybeUseSidebarOrientation
}


4 changes: 2 additions & 2 deletions src/resources/filters/quarto-post/dashboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function render_dashboard()
local layoutContentEls = organizer.ensureInLayoutContainers()

-- force the global orientation to columns if there is a sidebar present
local inferredOrientation = dashboard.layout.inferOrientation(el)
local inferredOrientation = dashboard.sidebar.maybeUseSidebarOrientation(el)
if inferredOrientation ~= nil then
dashboard.layout.setOrientation(inferredOrientation)
end
Expand Down Expand Up @@ -303,7 +303,7 @@ function render_dashboard()
lastLevel = level

-- force the global orientation to columns if there is a sidebar present
local inferredOrientation = dashboard.layout.inferOrientation(el)
local inferredOrientation = dashboard.sidebar.maybeUseSidebarOrientation(el)
if inferredOrientation ~= nil then
toOrientation = dashboard.layout.setOrientation(inferredOrientation)
else
Expand Down
25 changes: 25 additions & 0 deletions tests/docs/smoke-all/dashboard/6-sidebar-columns.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Sidebar Example
format:
dashboard:
orientation: rows
theme: journal
---

## Sidebar {.sidebar}

Sidebar controls

## Column

```{r}
plot(cars)
```

## Column

```{r}
plot(mtcars)
```


0 comments on commit b368e19

Please sign in to comment.