Skip to content

Commit 1d88c5a

Browse files
committed
Collapse long code blocks by default and fix the Expand toggle
Code blocks taller than 5 lines now start collapsed, and the codeheader Expand/Shrink link toggles them. The theme put no max-height on .bbc_code, so core's expander had nothing to act on; own the behaviour in theme.js to match the CSP-safe pattern used for the rest of the theme's interactions.
1 parent 1bdf428 commit 1d88c5a

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

‎css/index.css‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,17 @@ pre {
14921492
overflow: auto;
14931493
}
14941494

1495+
/* Code blocks taller than 5 lines start collapsed (see theme.js); the
1496+
codeheader's Expand/Shrink link toggles them. */
1497+
.bbc_code.sid_code_collapsed {
1498+
mask-image: linear-gradient(to bottom, #000 calc(100% - 1.5rem), transparent);
1499+
-webkit-mask-image: linear-gradient(to bottom, #000 calc(100% - 1.5rem), transparent);
1500+
}
1501+
1502+
.codeheader .smf_expand_code {
1503+
cursor: pointer;
1504+
}
1505+
14951506
code {
14961507
padding: .12em .35em;
14971508
}

‎scripts/theme.js‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,69 @@ $(function() {
5757
$(this).toggleClass('original_size');
5858
});
5959

60+
// --- Code blocks: collapse-by-default + working Expand/Shrink ---------
61+
//
62+
// SMF renders each code block as <div class="codeheader">…<a
63+
// class="smf_expand_code">…</a></div><code class="bbc_code">…</code>.
64+
// Core's own toggle does nothing here because the theme puts no height
65+
// limit on .bbc_code, so there is never anything to expand. We own the
66+
// behaviour: anything taller than 5 lines starts collapsed, and the
67+
// Expand link toggles it.
68+
var CODE_MAX_LINES = 5;
69+
70+
function sidCollapseCode(code, btn) {
71+
var cs = getComputedStyle(code);
72+
var lineHeight = parseFloat(cs.lineHeight) || 20;
73+
var padTop = parseFloat(cs.paddingTop) || 0;
74+
code.style.maxHeight = Math.round(lineHeight * CODE_MAX_LINES + padTop) + 'px';
75+
code.style.overflow = 'auto';
76+
code.classList.add('sid_code_collapsed');
77+
if (btn)
78+
btn.textContent = btn.getAttribute('data-expand-txt') || btn.textContent;
79+
}
80+
81+
function sidExpandCode(code, btn) {
82+
code.style.maxHeight = '';
83+
code.classList.remove('sid_code_collapsed');
84+
if (btn)
85+
btn.textContent = btn.getAttribute('data-shrink-txt') || btn.textContent;
86+
}
87+
88+
$('code.bbc_code').each(function() {
89+
var code = this;
90+
var header = code.previousElementSibling;
91+
var btn = header ? header.querySelector('.smf_expand_code') : null;
92+
93+
// Source-line count: "longer than 5 lines" means 5 real lines.
94+
var lines = code.textContent.replace(/\s+$/, '').split('\n').length;
95+
if (lines <= CODE_MAX_LINES) {
96+
// Short enough to show in full; no expander needed.
97+
if (btn)
98+
btn.classList.add('hidden');
99+
return;
100+
}
101+
102+
if (btn)
103+
btn.classList.remove('hidden');
104+
sidCollapseCode(code, btn);
105+
});
106+
107+
// Toggle on click. Delegated so it also covers posts loaded via quick
108+
// edit / ajax. We drive everything off our own class, independent of
109+
// whatever core may or may not bind to the same link.
110+
$(document).on('click', '.smf_expand_code', function(e) {
111+
e.preventDefault();
112+
var btn = this;
113+
var header = btn.closest('.codeheader');
114+
var code = header ? header.nextElementSibling : null;
115+
if (!code || !code.classList.contains('bbc_code'))
116+
return;
117+
if (code.classList.contains('sid_code_collapsed'))
118+
sidExpandCode(code, btn);
119+
else
120+
sidCollapseCode(code, btn);
121+
});
122+
60123
// Admin dashboard tidy-up.
61124
var adminContent = document.getElementById('admin_content');
62125
if (adminContent) {

0 commit comments

Comments
 (0)