diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dd7d42..d0fbf5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/guides/defining-template-context.md b/docs/guides/defining-template-context.md index b9ed1dd..fdf55dd 100644 --- a/docs/guides/defining-template-context.md +++ b/docs/guides/defining-template-context.md @@ -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 diff --git a/package-lock.json b/package-lock.json index 72b979a..e76d704 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "license": "BSD-3-Clause", "dependencies": { + "codejar": "^5.0.0", "highlight.js": "^11.11.1" }, "devDependencies": { @@ -2594,6 +2595,12 @@ "node": ">=6" } }, + "node_modules/codejar": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/codejar/-/codejar-5.0.0.tgz", + "integrity": "sha512-p0yJqjxcohip+OxJqjn8axh/i1Gp2Jqg8p135M0CE5Qu/sgtmMK1/ZVGv6YgSMxllfc3rovrMfwYG39PqaPRug==", + "license": "MIT" + }, "node_modules/color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", diff --git a/package.json b/package.json index e64c96b..169ef9e 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "webpack-cli": "^6.0.1" }, "dependencies": { + "codejar": "^5.0.0", "highlight.js": "^11.11.1" } } diff --git a/pattern_library/static/pattern_library/src/js/components/config-editor.js b/pattern_library/static/pattern_library/src/js/components/config-editor.js index 49e01da..ca13b91 100644 --- a/pattern_library/static/pattern_library/src/js/components/config-editor.js +++ b/pattern_library/static/pattern_library/src/js/components/config-editor.js @@ -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( @@ -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); }); } diff --git a/pattern_library/static/pattern_library/src/scss/components/_code.scss b/pattern_library/static/pattern_library/src/scss/components/_code.scss index 02c7057..3cc8a3e 100644 --- a/pattern_library/static/pattern_library/src/scss/components/_code.scss +++ b/pattern_library/static/pattern_library/src/scss/components/_code.scss @@ -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; } diff --git a/pattern_library/static/pattern_library/src/scss/hljs/_a11y-light.scss b/pattern_library/static/pattern_library/src/scss/hljs/_a11y-light.scss deleted file mode 100644 index d781942..0000000 --- a/pattern_library/static/pattern_library/src/scss/hljs/_a11y-light.scss +++ /dev/null @@ -1,101 +0,0 @@ -// a11y-light theme -// Based on the Tomorrow Night Eighties theme: https://github.com/isagalaev/highlight.js/blob/master/src/styles/tomorrow-night-eighties.css -// @author: ericwbailey - -// stylelint-disable scss/selector-class-pattern, scale-unlimited/declaration-strict-value - -/* Comment */ -.hljs-comment, -.hljs-quote { - color: #696969; -} - -/* Red */ -.hljs-variable, -.hljs-template-variable, -.hljs-tag, -.hljs-name, -.hljs-selector-id, -.hljs-selector-class, -.hljs-regexp, -.hljs-deletion { - color: #d91e18; -} - -/* Orange */ -.hljs-number, -.hljs-built_in, -.hljs-builtin-name, -.hljs-literal, -.hljs-type, -.hljs-params, -.hljs-meta, -.hljs-link { - color: #aa5d00; -} - -/* Yellow */ -.hljs-attribute { - color: #aa5d00; -} - -/* Green */ -.hljs-string, -.hljs-symbol, -.hljs-bullet, -.hljs-addition { - color: #008000; -} - -/* Blue */ -.hljs-title, -.hljs-section { - color: #007faa; -} - -/* Purple */ -.hljs-keyword, -.hljs-selector-tag { - color: #7928a1; -} - -.hljs { - display: block; - overflow-x: auto; - background: #fefefe; - color: #545454; - padding: 0.5em; -} - -.hljs-emphasis { - font-style: italic; -} - -.hljs-strong { - font-weight: bold; -} - -@media screen and (-ms-high-contrast: active) { - .hljs-addition, - .hljs-attribute, - .hljs-built_in, - .hljs-builtin-name, - .hljs-bullet, - .hljs-comment, - .hljs-link, - .hljs-literal, - .hljs-meta, - .hljs-number, - .hljs-params, - .hljs-string, - .hljs-symbol, - .hljs-type, - .hljs-quote { - color: highlight; - } - - .hljs-keyword, - .hljs-selector-tag { - font-weight: bold; - } -} diff --git a/pattern_library/templates/pattern_library/index.html b/pattern_library/templates/pattern_library/index.html index 399b977..473c88f 100644 --- a/pattern_library/templates/pattern_library/index.html +++ b/pattern_library/templates/pattern_library/index.html @@ -50,10 +50,10 @@
{{ pattern_config }}
-
@@ -63,7 +63,7 @@ {{ pattern_source }}
+ {{ pattern_source }}