Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

## [Unreleased]

### Changed

- The "Template config" tab is a syntax-highlighted YAML editor ([CodeJar](https://github.com/antonmedv/codejar)) that keeps indentation on Enter and indents with Tab.

### Fixed

- Live-edit renders run the pattern's scripts in a fresh page, so scripts with top-level `const`/`let`/`class` or custom element definitions no longer break on re-render.
- Resetting the live config no longer adds a browser history entry.
- Code blocks in the pattern tabs no longer have a fixed width that caused horizontal scrolling on small screens.

## [1.6.0+ll.2](https://github.com/lincolnloop/django-pattern-library/releases/tag/v1.6.0+ll.2) - 2026-11-04

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions docs/guides/defining-template-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ You can define a list or a dict or anything that [`PyYAML`](http://pyyaml.org/wi

Click "Toggle live mode" in the "Template config" tab of the pattern library UI to make the config editable. Change the YAML there and the pattern re-renders in the preview with the edited `context` and `tags`, half a second after you stop typing. Use "Reset" to go back to the config from the YAML file, or toggle live mode off to also go back to the read-only config. Edits are only a preview: they are not saved to the YAML file, and are lost when you leave live mode or reload the page.

In the editor, Enter keeps the current indentation and Tab indents; press Esc then Tab to move focus out of it.

Live-editing needs the pattern library to be served by Django. In a [static export](./static-site-export.md) of the UI, the "Toggle live mode" button is hidden.

## Modifying template contexts with Python
Expand Down
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"webpack-cli": "^6.0.1"
},
"dependencies": {
"codejar": "^5.0.0",
"highlight.js": "^11.11.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { CodeJar } from 'codejar';
import hljs from 'highlight.js/lib/core';

export default function () {
const liveMode = document.querySelector('.js-config-live-mode');
const source = document.querySelector('.js-config-source');
const form = document.querySelector('.js-config-form');
const editor = document.querySelector('.js-config-editor');
const mount = document.querySelector('.js-config-mount');
const actions = document.querySelector('.js-config-actions');
const iframe = document.querySelector('.js-iframe');
let jar;
let timeout;

const highlight = (el) => {
el.innerHTML = hljs.highlight(el.textContent, {
language: 'yaml',
ignoreIllegals: true,
}).value;
};
const show = (code) => {
mount.textContent = code;
highlight(mount);
};
show(editor.value);

fetch(form.action, { method: 'OPTIONS' })
.then((response) => {
liveMode.hidden = !response.headers.has(
Expand All @@ -18,38 +34,72 @@ export default function () {
liveMode.addEventListener('click', () => {
const on = liveMode.getAttribute('aria-pressed') !== 'true';
liveMode.setAttribute('aria-pressed', on);
source.hidden = on;
form.hidden = !on;
mount.setAttribute('aria-readonly', !on);
actions.hidden = !on;
if (on) {
editor.focus();
// YAML forbids tab indentation, and auto-closed quotes don't type over
jar = CodeJar(mount, highlight, {
tab: ' ',
indentOn: /:$/,
addClosing: false,
});
mount.style.outline = ''; // CodeJar hides the focus ring
jar.onUpdate((code) => {
// Also fires on keys that don't edit, like arrows
if (code === editor.value) {
return;
}
editor.value = code;
clearTimeout(timeout);
timeout = setTimeout(() => form.requestSubmit(), 500);
});
mount.focus();
} else {
jar.destroy();
mount.removeAttribute('contenteditable');
form.reset();
}
});

// Write the render into the iframe in place rather than navigating it, so
// live edits don't pile up as back-button history entries
// Esc then Tab leaves the editor instead of indenting
mount.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
jar?.updateOptions({ catchTab: false });
}
});
mount.addEventListener('focus', () => {
jar?.updateOptions({ catchTab: true });
});

// Fresh about:blank per render: document.open() keeps the old window, so
// page scripts would re-run in one scope. replace() skips history entries
let pendingHtml;
iframe.addEventListener('load', () => {
const html = pendingHtml;
pendingHtml = undefined;
if (html === undefined) {
return;
}
const doc = iframe.contentDocument;
doc.open();
doc.write(html);
doc.close();
});

form.addEventListener('submit', (e) => {
e.preventDefault();
fetch(form.action, { method: 'POST', body: new FormData(form) })
.then((response) => response.text())
.then((html) => {
const doc = iframe.contentDocument;
doc.open();
doc.write(html);
doc.close();
pendingHtml = html;
iframe.contentWindow.location.replace('about:blank');
})
.catch(() => {});
});

editor.addEventListener('input', () => {
clearTimeout(timeout);
timeout = setTimeout(() => form.requestSubmit(), 500);
});

form.addEventListener('reset', () => {
clearTimeout(timeout);
iframe.src = iframe.src;
iframe.contentWindow.location.replace(iframe.src);
show(editor.defaultValue);
});
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
.code {
width: 600px;
overflow: scroll;

@media only screen and (min-width: 1200px) {
width: 80vw;
}

&--editor {
display: block;
margin-bottom: 16px;
padding: 10px;
overflow: auto;
font-family: monospace;
font-size: inherit;
resize: vertical;
}
// CodeJar sets this wrapping inline in live mode; match it when read-only
.code-editor {
white-space: pre-wrap;
overflow-wrap: break-word;
}
101 changes: 0 additions & 101 deletions pattern_library/static/pattern_library/src/scss/hljs/_a11y-light.scss

This file was deleted.

8 changes: 4 additions & 4 deletions pattern_library/templates/pattern_library/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ <h4 class="heading heading--iframe-size js-iframe-size">
</div>

<div id="tab-2" class="tabbed-content__item">
<pre class="js-config-source"><code class="code yaml">{{ pattern_config }}</code></pre>
<form class="js-config-form" id="pattern-config-form" method="post" action="{% url 'pattern_library:render_pattern' pattern_template_name=pattern_template_name %}" hidden>
<form class="js-config-form" id="pattern-config-form" method="post" action="{% url 'pattern_library:render_pattern' pattern_template_name=pattern_template_name %}">
{% csrf_token %}
<textarea class="code code--editor js-config-editor" name="config" spellcheck="false" rows="20" aria-label="Template config (YAML)">{{ pattern_config }}</textarea>
<textarea class="js-config-editor" name="config" hidden>{{ pattern_config }}</textarea>
<pre class="hljs code-editor js-config-mount" role="textbox" aria-multiline="true" aria-readonly="true" aria-label="Template config (YAML)" tabindex="0"></pre>
</form>
<button class="button button--config js-config-live-mode" type="button" aria-pressed="false" hidden>Toggle live mode</button>
<span class="js-config-actions" hidden>
Expand All @@ -63,7 +63,7 @@ <h4 class="heading heading--iframe-size js-iframe-size">
</div>

<div id="tab-3" class="tabbed-content__item">
<pre><code class="code django">{{ pattern_source }}</code></pre>
<pre><code class="django">{{ pattern_source }}</code></pre>
</div>

{% block custom_tab_content %}
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ def test_index_has_config_editor(self):
)
self.assertContains(response, "Toggle live mode")
self.assertContains(response, "js-config-editor")
self.assertContains(response, "js-config-mount")
self.assertContains(response, 'aria-label="Template config (YAML)"')
self.assertContains(response, "csrfmiddlewaretoken")
self.assertContains(response, "title: Get started")

Expand Down
Loading