From d5aac8ac50b190c96a462048c27f5e7181e07039 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Mon, 19 Sep 2022 16:34:02 +0100 Subject: [PATCH] Don't show header if there's nothing useful to show --- static/src/js/base.js | 6 ++++++ website/contrib/code_block/blocks.py | 10 ++++++++++ .../code_block/templates/contrib/blocks/code.html | 15 +++++++++------ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/static/src/js/base.js b/static/src/js/base.js index 988f363..547e671 100644 --- a/static/src/js/base.js +++ b/static/src/js/base.js @@ -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 diff --git a/website/contrib/code_block/blocks.py b/website/contrib/code_block/blocks.py index e73f5f6..5f81942 100644 --- a/website/contrib/code_block/blocks.py +++ b/website/contrib/code_block/blocks.py @@ -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) diff --git a/website/contrib/code_block/templates/contrib/blocks/code.html b/website/contrib/code_block/templates/contrib/blocks/code.html index 53877b1..05ecce7 100644 --- a/website/contrib/code_block/templates/contrib/blocks/code.html +++ b/website/contrib/code_block/templates/contrib/blocks/code.html @@ -1,7 +1,10 @@ -
- {% firstof value.filename value.language %} - - - -
+{% if value.header_text %} +
+ {{ value.header_text }} + + + +
+{% endif %} + {{ value.code }}