From a333dcd9f101ec162e52e2d2b0a177d135b6a5ca Mon Sep 17 00:00:00 2001 From: bat Date: Sun, 28 May 2023 06:58:53 +0000 Subject: [PATCH] Create from template --- components/header.js | 36 +++++++++++++++++++++++++++++++----- components/page-actions.js | 2 +- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/components/header.js b/components/header.js index 9a2d77c..e405b96 100644 --- a/components/header.js +++ b/components/header.js @@ -254,6 +254,22 @@ export class Header extends HTMLElement { return this.editing ? this.icons.check : this.icons.edit } + getTemplates() { + const text = this.storage.getItem( + 'settings:templates' + ) + if (text ?? undefined !== undefined) { + const data = JSON.parse(text) + return Object.entries( + data.templates + ).map(([id, data]) => ( + {...data, id} + )) + } else { + return [] + } + } + addPage() { const dialog = document.createElement('m-dialog') this.dialogWrap.replaceChildren(dialog) @@ -269,6 +285,13 @@ export class Header extends HTMLElement { el.innerText = this.text[value] return el })) + const templates = this.getTemplates() + select.append(...templates.map(t => { + const el = document.createElement('option') + el.value = t.id + el.innerText = t.name + return el + })) select.value = 'htmlCss' input.addEventListener('input', e => { const ext = e.target.value.match(/\.\w+$/) @@ -295,10 +318,9 @@ export class Header extends HTMLElement { } return } - const value = ( - select.value === 'singleFile' ? - '' : - JSON.stringify({ + let value + if (select.value === 'htmlCss') { + value = JSON.stringify({ type: 'm-file-group', files: [ { @@ -311,7 +333,11 @@ export class Header extends HTMLElement { }, ], }) - ) + } else { + value = this.storage.getItem( + select.value + ) ?? '' + } this.storage.setItem(newPath, value) this.storage.setItem( 'settings/page:' + newPath, '{}' diff --git a/components/page-actions.js b/components/page-actions.js index 4df7c09..451b0e2 100644 --- a/components/page-actions.js +++ b/components/page-actions.js @@ -349,7 +349,7 @@ export class PageActions extends HTMLElement { const name = key.slice( 'settings/page:'.length ) - settings[name] = {name} + templates[name] = {name} } } } catch (err) {