Don't show header if there's nothing useful to show

This commit is contained in:
Jake Howard 2022-09-19 16:34:02 +01:00
parent aadca46cd4
commit d5aac8ac50
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 25 additions and 6 deletions

View file

@ -62,6 +62,12 @@ window.addEventListener("load", () => {
document.querySelectorAll(".block-code").forEach((codeBlock) => {
const clipboardIcon = codeBlock.querySelector(".code-copy");
// There may not be an icon
if (!clipboardIcon) {
return;
}
clipboardIcon.addEventListener("click", (event) => {
event.preventDefault();
navigator.clipboard

View file

@ -4,6 +4,7 @@ from django.utils.safestring import mark_safe
from pygments import highlight
from pygments.formatters.html import HtmlFormatter
from pygments.lexers import find_lexer_class, get_all_lexers
from pygments.lexers.special import TextLexer
from wagtail.blocks import (
BooleanBlock,
CharBlock,
@ -27,6 +28,15 @@ class CodeStructValue(StructValue):
)
return mark_safe(highlight(self["source"], lexer, formatter))
def header_text(self) -> str:
if filename := self["filename"]:
return filename
if language := self["language"] and self["language"] != TextLexer.name:
return language
return ""
class CodeBlock(StructBlock):
filename = CharBlock(max_length=128, required=False)

View file

@ -1,7 +1,10 @@
<div class="code-header {% if value.always_show_header %}always-show{% endif %}">
<span title="{{ value.language }}">{% firstof value.filename value.language %}</span>
<span>
<i class="fa-regular fa-clipboard is-clickable code-copy" title="Copy to clipboard"></i>
</span>
</div>
{% if value.header_text %}
<div class="code-header {% if value.always_show_header %}always-show{% endif %}">
<span title="{{ value.language }}">{{ value.header_text }}</span>
<span>
<i class="fa-regular fa-clipboard is-clickable code-copy" title="Copy to clipboard"></i>
</span>
</div>
{% endif %}
{{ value.code }}