- commit
- 1f6e7cfd6820a324dd612e2634c1fe589de06525
- parent
- f917348013be86f57c8ad212f2a6093e18bf9270
- author
- Dami <dami.etoile@gmail.com>
| M | diagramzip/ | 15 | ++++++++++++ |
| M | diagramzip/ | 7 | +++++- |
| M | diagramzip/ | 37 | +++++++++++++++++++++++++++++- |
@@ -16,3 +16,18 @@ export function exampleVariant(index, context) {
1616 },1717 }1818 }19+20+export function refreshMatchingExampleMetadata(state, exampleState) {21+ if (state?.source !== exampleState?.source || state?.meta?.title?.trim()) return state22+23+ return {24+ ...state,25+ meta: {26+ ...state.meta,27+ title: exampleState.meta.title,28+ description: state.meta?.description?.trim()29+ ? state.meta.description30+ : exampleState.meta.description,31+ },32+ }33+}@@ -1,5 +1,6 @@
11 import { diagramTypeFromQuery, diagramTypes, isKnownDiagramType, urlWithDiagramType } from './diagram-types.js'22 import { exampleStateFor } from './examples.js'3+import { refreshMatchingExampleMetadata } from './example-variants.js'34 import { createSourceEditor } from './source-editor.js'45 import { stateForTypeChange } from './type-drafts.js'56 import {@@ -470,7 +471,11 @@ async function loadInitialState() {
470471 const requestedType = diagramTypeFromQuery(location.search)471472 const draft = loadDraft(LOCAL_DRAFT_KEY)472473 if (requestedType) {473- if (draft?.state.type === requestedType) return draft.state474+ if (draft?.state.type === requestedType) {475+ const state = refreshMatchingExampleMetadata(draft.state, exampleStateFor(requestedType))476+ if (state !== draft.state) storeDraft(state)477+ return state478+ }474479 return exampleStateFor(requestedType)475480 }476481 if (draft) return draft.state@@ -1,6 +1,10 @@
11 import assert from 'node:assert/strict'22 import test from 'node:test'3-import { exampleVariant, grayCode } from '../src/example-variants.js'3+import {4+ exampleVariant,5+ grayCode,6+ refreshMatchingExampleMetadata,7+} from '../src/example-variants.js'48 59 test('changes one example axis at a time across the catalog', () => {610 for (let index = 1; index < 30; index++) {@@ -19,3 +23,34 @@ test('names every example while varying descriptions and padding', () => {
1923 assert.ok(variants.every(variant => variant.presentation.background === ''))2024 assert.ok(variants.every(variant => variant.presentation.frame === false))2125 })26+27+test('refreshes metadata for an untitled draft matching its bundled example', () => {28+ const state = {29+ type: 'd2',30+ source: 'a -> b',31+ meta: { title: '', description: '' },32+ presentation: { padding: 24 },33+ }34+ const example = {35+ type: 'd2',36+ source: 'a -> b',37+ meta: { title: 'Connection styles', description: 'D2 syntax.' },38+ }39+40+ assert.deepEqual(refreshMatchingExampleMetadata(state, example), {41+ ...state,42+ meta: { title: 'Connection styles', description: 'D2 syntax.' },43+ })44+})45+46+test('preserves named and edited drafts', () => {47+ const example = {48+ source: 'a -> b',49+ meta: { title: 'Connection styles', description: 'D2 syntax.' },50+ }51+ const named = { source: example.source, meta: { title: 'Mine', description: '' } }52+ const edited = { source: 'a -> b: edited', meta: { title: '', description: '' } }53+54+ assert.equal(refreshMatchingExampleMetadata(named, example), named)55+ assert.equal(refreshMatchingExampleMetadata(edited, example), edited)56+})