Browse Source

Editor: restore

imwald
Nuša Pukšič 2 weeks ago
parent
commit
4d465050fe
  1. 62
      assets/controllers/editor/json-panel_controller.js
  2. 2
      assets/styles/05-utilities/utilities.css
  3. 2
      templates/editor/panels/_metadata.html.twig

62
assets/controllers/editor/json-panel_controller.js

@ -46,20 +46,28 @@ export default class extends Controller { @@ -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 { @@ -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 { @@ -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 }));
}
}
}

2
assets/styles/05-utilities/utilities.css

@ -32,6 +32,8 @@ @@ -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}

2
templates/editor/panels/_metadata.html.twig

@ -61,7 +61,7 @@ @@ -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'}
}) }}

Loading…
Cancel
Save