From 4d465050fe3682dd53d1de010e8352e3f17fbaa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C5=A1a=20Puk=C5=A1i=C4=8D?= Date: Fri, 26 Dec 2025 22:05:02 +0100 Subject: [PATCH] Editor: restore --- .../editor/json-panel_controller.js | 62 +++++++++++++------ assets/styles/05-utilities/utilities.css | 2 + templates/editor/panels/_metadata.html.twig | 2 +- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/assets/controllers/editor/json-panel_controller.js b/assets/controllers/editor/json-panel_controller.js index c075c4b..6e5506a 100644 --- a/assets/controllers/editor/json-panel_controller.js +++ b/assets/controllers/editor/json-panel_controller.js @@ -46,20 +46,28 @@ export default class extends Controller { this.cmView = this.textarea._codemirror; } - // Restore from localStorage if available - const savedJson = localStorage.getItem('editorState'); - if (savedJson) { - try { - JSON.parse(savedJson); // Validate JSON - this.jsonTextareaTarget.value = savedJson; - if (this.cmView) { - this.cmView.dispatch({ - changes: {from: 0, to: this.cmView.state.doc.length, insert: savedJson} - }); + // Prompt to restore from localStorage if available and textarea is empty + if (this.jsonTextareaTarget.value.trim() === '') { + const savedJson = localStorage.getItem('editorState'); + if (savedJson) { + let shouldRestore = window.confirm('A draft was found in your browser. Do you want to restore it?'); + if (shouldRestore) { + try { + const parsedJson = JSON.parse(savedJson); // Validate JSON + this.jsonTextareaTarget.value = savedJson; + if (this.cmView) { + this.cmView.dispatch({ + changes: {from: 0, to: this.cmView.state.doc.length, insert: savedJson} + }); + } + this.populateFormFieldsFromJson(parsedJson); + } catch (e) { + // Ignore corrupt JSON + localStorage.removeItem('editorState'); + } + } else { + localStorage.removeItem('editorState'); } - } catch (e) { - // Ignore corrupt JSON - localStorage.removeItem('editorState'); } } // Periodic save every 10 seconds @@ -89,12 +97,6 @@ export default class extends Controller { } this.textarea.style.display = ''; this.textarea._codemirror = null; - - // Clear periodic save interval - if (this._saveInterval) { - clearInterval(this._saveInterval); - this._saveInterval = null; - } } handleMarkdownInput() { @@ -257,4 +259,26 @@ export default class extends Controller { 'nostr--nostr-publish' ); } + + // Populate form fields (markdown, title, etc.) from JSON + populateFormFieldsFromJson(json) { + // Markdown content + const md = this.getMarkdownTextarea(); + if (md && json.content !== undefined) { + md.value = json.content; + md.dispatchEvent(new Event('input', { bubbles: true })); + } + // Title (example: input[name="editor[title]"]) + const titleInput = document.querySelector('input[name="editor[title]"]'); + if (titleInput && json.title !== undefined) { + titleInput.value = json.title; + titleInput.dispatchEvent(new Event('input', { bubbles: true })); + } + // Tags (example: input[name="editor[tags]"] or similar) + const tagsInput = document.querySelector('input[name="editor[tags]"]'); + if (tagsInput && json.tags !== undefined && Array.isArray(json.tags)) { + tagsInput.value = json.tags.join(', '); + tagsInput.dispatchEvent(new Event('input', { bubbles: true })); + } + } } diff --git a/assets/styles/05-utilities/utilities.css b/assets/styles/05-utilities/utilities.css index 8320faf..f410c2a 100644 --- a/assets/styles/05-utilities/utilities.css +++ b/assets/styles/05-utilities/utilities.css @@ -32,6 +32,8 @@ .flex-wrap{flex-wrap: wrap} .justify-content-between{justify-content:space-between!important} .justify-content-center{justify-content:center!important} +.justify-content-start{justify-content:flex-start!important} +.justify-content-end{justify-content:flex-end!important} .align-items-center{align-items:center!important} .align-items-start{align-items:flex-start!important} diff --git a/templates/editor/panels/_metadata.html.twig b/templates/editor/panels/_metadata.html.twig index 9e0b796..c87e393 100644 --- a/templates/editor/panels/_metadata.html.twig +++ b/templates/editor/panels/_metadata.html.twig @@ -61,7 +61,7 @@ {{ form_row(form.clientTag, { 'label': 'Add client tag (Decent Newsroom)', - 'row_attr': {'class': 'form-check mt-2 d-flex flex-row-reverse justify-content-between'}, + 'row_attr': {'class': 'form-check mt-2 d-flex flex-row-reverse justify-content-end'}, 'label_attr': {'class': 'form-check-label'}, 'attr': {'class': 'form-check-input'} }) }}