Skip to content

Commit 7f8ca26

Browse files
authored
Merge pull request #47 from disberd/toc_cell_hiding_refactor
2 parents 7f609b5 + d0eea5c commit 7f8ca26

7 files changed

Lines changed: 71 additions & 78 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ This file contains the changelog for the PlutoExtras package. It follows the [Ke
77
### Fixed
88
- Fixed problems with `ExtendedTableOfContents` with recent PlutoUI versions, also resolving an issue with `hide-heading` icon not appearing next to the corresponding heading in the ToC.
99
- Fixed styling issue with `BondsTable` in #19
10+
- The `show_output_when_hidden` function now works correctly with `ExtendedTableOfContents`, not incorrectly hiding the cell input when cell should be shown normally (Fixes #46)
1011

1112
### Added
1213
- Added possibility of providing any valid object with a `MIME"text/html"` representation as description of the `@NTBond` macro.
1314
- Added the possibility of simplifying application of `PlutoUI.Experimental.transformed_value` to the fields of an `@NTBond` using the `@tv` decorator (see the example notebook for details).
1415

1516
### Changed
1617
- Changed the hiding behavior of the `Popout` container so that it stays displayed if the mouse is hovering over its contents even if not popped out
18+
- The internal logic for hiding cells in the notebook based on the ToC rows hiding status is now relying purely on JS attributes attached to cells rather than complex dynamic CSS, simplifying logic and allowing to fix some issues.
1719

1820

1921
## [0.7.15] - 2025-04-22

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PlutoExtras"
22
uuid = "ed5d0301-4775-4676-b788-cf71e66ff8ed"
33
authors = ["Alberto Mengali <disberd@gmail.com>"]
4-
version = "0.7.16-DEV"
4+
version = "0.7.16"
55

66
[deps]
77
AbstractPlutoDingetjes = "6e696c72-6542-2067-7265-42206c756150"

src/PlutoExtras.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ include("combine_htl/PlutoCombineHTL.jl")
2424

2525
include("basic_widgets.jl")
2626
include("latex_equations.jl")
27-
module ExtendedToc include("extended_toc.jl") end
27+
module ExtendedToc include("extended_toc/extended_toc.jl") end
2828

2929
include("structbond/StructBondModule.jl")
3030
using .StructBondModule
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,25 +159,29 @@ div.toc-row-separator.active.noshow {
159159
}
160160

161161
/* ==========================================================================
162-
Always Show Output Styles
162+
Main Cell Hidings
163163
========================================================================== */
164164

165+
pluto-notebook[hide-enabled] pluto-cell[toc-hidden] {
166+
display: none;
167+
}
168+
165169
/* This style permits to have a cell whose output is still being shown when
166170
the cell is hidden. This is useful for having hidden cells that still are
167171
sending HTML as output (like ToC and BondTable) */
168172

169-
pluto-notebook[hide-enabled] pluto-cell[always-show-output] {
170-
display: block !important;
173+
pluto-notebook[hide-enabled] pluto-cell[always-show-output][toc-hidden] {
174+
display: block;
171175
}
172176

173-
pluto-notebook[hide-enabled] pluto-cell[always-show-output]:not(.code_differs) > pluto-input,
174-
pluto-notebook[hide-enabled] pluto-cell[always-show-output]:not(.code_differs) > pluto-shoulder,
175-
pluto-notebook[hide-enabled] pluto-cell[always-show-output]:not(.code_differs) > pluto-trafficlight,
176-
pluto-notebook[hide-enabled] pluto-cell[always-show-output]:not(.code_differs) > pluto-runarea,
177-
pluto-notebook[hide-enabled] pluto-cell[always-show-output]:not(.code_differs) > button {
177+
pluto-notebook[hide-enabled] pluto-cell[always-show-output][toc-hidden]:not(.code_differs) > pluto-input,
178+
pluto-notebook[hide-enabled] pluto-cell[always-show-output][toc-hidden]:not(.code_differs) > pluto-shoulder,
179+
pluto-notebook[hide-enabled] pluto-cell[always-show-output][toc-hidden]:not(.code_differs) > pluto-trafficlight,
180+
pluto-notebook[hide-enabled] pluto-cell[always-show-output][toc-hidden]:not(.code_differs) > pluto-runarea,
181+
pluto-notebook[hide-enabled] pluto-cell[always-show-output][toc-hidden]:not(.code_differs) > button {
178182
display: none;
179183
}
180184

181-
pluto-notebook[hide-enabled] pluto-cell[always-show-output]:not(.code_differs) {
185+
pluto-notebook[hide-enabled] pluto-cell[always-show-output][toc-hidden]:not(.code_differs) {
182186
margin-top: 0px;
183187
}
Lines changed: 52 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -275,50 +275,6 @@ if (force_hide_enabled) {
275275
toggle_notebook_attribute('hide-enabled',true)
276276
}
277277

278-
// Hide cell blocks functionality
279-
// For each from and to, we have to specify `pluto-cell[id]` in the part before the comm and just `[id]` in the part after the comma to ensure the specificity of the two comma-separated selectors is the same (the part after the comma has the addition of `~ pluto-cell`, so it has inherently +1 element specificity)
280-
function hide_from_to_string(from_id, to_id) {
281-
if (_.isEmpty(from_id) && _.isEmpty(to_id)) {return ''}
282-
283-
const from_preselector = _.isEmpty(from_id) ? '' : `pluto-cell[id='${from_id}'], pluto-notebook[hide-enabled] [id='${from_id}'] ~ `
284-
const to_style = _.isEmpty(to_id) ? '' : `pluto-notebook[hide-enabled] pluto-cell[id='${to_id}'], pluto-notebook[hide-enabled] [id='${to_id}'] ~ pluto-cell {
285-
display: block;
286-
}
287-
`
288-
const style_string = `pluto-notebook[hide-enabled] ${from_preselector}pluto-cell {
289-
display: none;
290-
}
291-
${to_style}
292-
`
293-
return style_string
294-
}
295-
296-
function hide_from_to_list_string(vector) {
297-
let out = ``
298-
for (const lims of vector) {
299-
const from = lims[0]
300-
const to = lims[1]
301-
302-
out = `${out}\t${hide_from_to_string(from,to)}`
303-
}
304-
out = `${out}\tpluto-cell[always-show] {
305-
display: block !important;
306-
}
307-
`
308-
return out
309-
}
310-
function hide_from_to_list(vector) {
311-
const str = hide_from_to_list_string(vector)
312-
return html`<style>${str}</style>`
313-
}
314-
function hide_list_style(vector) {
315-
let style = document.getElementById('hide-cells-style')
316-
if (style == null) {
317-
style = document.head.appendChild(html`<style id='hide-cells-style'></style>`)
318-
}
319-
style.innerHTML = hide_from_to_list_string(vector)
320-
}
321-
322278
// Mutation observer functionality
323279
function toggle_state(name) {
324280
return (e) => {
@@ -330,31 +286,62 @@ function toggle_state(name) {
330286
}
331287
}
332288

333-
function update_hidden(e) {
334-
let new_hide = []
289+
// This function will return a vector of cells indices (intended to index the list of cells in their order as return by `document.querySelectorAll('pluto-cell')`) to identify start and end index of cells to hide because their corresponding ToC row is hidden
290+
function tocrows_hidden_indices() {
291+
let hidden_start_stop_idxs = [] // This will track the start and end index of cells to hide because their corresponding ToC row is hidden. The cell at the end idx should already be considered as `shown`
292+
let current_hidden_status = false // This signals whether the current is hidden or not
335293
if (hide_preamble) {
336-
new_hide.push(['', get_link_id(toc.querySelector('div.toc-row'))])
294+
hidden_start_stop_idxs.push(-1)
295+
current_hidden_status = true
337296
}
338-
let tracking_hidden = null
339-
let divs = toc.querySelectorAll('div.toc-row')
297+
const divs = toc.querySelectorAll('div.toc-row')
298+
const cells = document.querySelectorAll('pluto-cell')
299+
let running_idx = 0 // This will track the index of the main cell
340300
for (const div of divs) {
341-
if (tracking_hidden != null) {
342-
const hidden = div.classList.contains('hidden') || div.classList.contains('parent-hidden')
343-
if (!hidden) {
344-
new_hide.push([tracking_hidden, get_link_id(div)])
345-
tracking_hidden = null
346-
}
347-
} else {
348-
const hidden = div.classList.contains('hidden')
349-
if (hidden) {
350-
tracking_hidden = get_link_id(div)
351-
}
301+
const hidden = div.classList.contains('hidden') || div.classList.contains('parent-hidden')
302+
const linked_cell_id = get_link_id(div)
303+
// We iterate through the main cells till we find the one with the linked cell id
304+
while (running_idx < cells.length && cells[running_idx].id !== linked_cell_id) {
305+
running_idx++
306+
}
307+
if (running_idx >= cells.length) {
308+
console.warn(`Cell with id ${linked_cell_id} not found`)
309+
continue
352310
}
311+
if (hidden != current_hidden_status) {
312+
hidden_start_stop_idxs.push(running_idx)
313+
current_hidden_status = hidden
314+
}
315+
}
316+
// If the number of elements is odd, we just add as last element the number of cells as we want consistent start and stop indices
317+
if (hidden_start_stop_idxs.length % 2 !== 0) {
318+
hidden_start_stop_idxs.push(cells.length)
353319
}
354-
if (tracking_hidden != null) {
355-
new_hide.push([tracking_hidden, ""])
320+
return hidden_start_stop_idxs
321+
}
322+
323+
// This function takes as input a vector of start/stop indices of hidden cells (based on toc rows) and apply the `toc-hidden` cell attribute (we don't use a class as pluto constantly rewrites the cell class) to identify that a cell must be hidden
324+
function hide_main_cells(indices) {
325+
const cells = document.querySelectorAll('pluto-cell')
326+
let current_hidden_status = false
327+
let next_target = indices.shift()
328+
for (let i = 0; i < cells.length; i++) {
329+
while (i > next_target) {
330+
next_target = indices.shift()
331+
current_hidden_status = !current_hidden_status
332+
}
333+
if (i == next_target) {
334+
next_target = indices.shift()
335+
current_hidden_status = !current_hidden_status
336+
}
337+
cells[i].toggleAttribute('toc-hidden', current_hidden_status)
356338
}
357-
hide_list_style(new_hide)
339+
}
340+
341+
window.toc_utils.tocrows_hidden_indices = tocrows_hidden_indices
342+
343+
function update_hidden_tocrows() {
344+
hide_main_cells(tocrows_hidden_indices())
358345
}
359346

360347
// Reposition the hide_container using the floating-ui library
@@ -431,7 +418,7 @@ function process_row(div, history, old_state, new_state) {
431418
const hide_span = hide_container.insertAdjacentElement('afterbegin', html`<span class='toc-icon toc-hide'>`)
432419
hide_span.addEventListener('click', (e) => {
433420
toggle_state('hidden')(e)
434-
update_hidden(e)
421+
update_hidden_tocrows()
435422
})
436423
if (div.directChildren == undefined) {
437424
collapse_span.classList.toggle('no-children', true)
@@ -466,7 +453,7 @@ const observer = new MutationObserver(() => {
466453
}
467454
window.toc_state = new_state
468455
toc.classList.toggle('file-state-differs', stateDiffersFile(new_state))
469-
update_hidden()
456+
update_hidden_tocrows()
470457
})
471458

472459
observer.observe(toc, {childList: true})

test/notebooks/structbondmodule.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ end
2727
# ╔═╡ 949ac1ef-c502-4e28-81ff-f99b0d19aa03
2828
@frompackage "../.." begin
2929
using ^.StructBondModule
30-
using ^: ExtendedTableOfContents, Editable
30+
using ^: ExtendedTableOfContents, Editable, show_output_when_hidden
3131
using >.HypertextLiteral
3232
using >.PlutoUI
3333
end
@@ -368,7 +368,7 @@ BondTable([
368368
nt_bond,
369369
bl,
370370
blc
371-
]; description = "My Bonds")
371+
]; description = "My Bonds") |> show_output_when_hidden
372372

373373
# ╔═╡ 08d711c0-e2cc-4444-94ca-0c4c3cfe901f
374374
nt

0 commit comments

Comments
 (0)