Browse Source

Editor: publishing actions

imwald
Nuša Pukšič 2 weeks ago
parent
commit
e665eb7a33
  1. 20
      assets/controllers/editor/header_controller.js
  2. 18
      assets/controllers/editor/json-panel_controller.js
  3. 29
      assets/controllers/editor/layout_controller.js
  4. 4
      assets/styles/03-components/form.css
  5. 9
      templates/editor/layout.html.twig

20
assets/controllers/editor/header_controller.js

@ -0,0 +1,20 @@
import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
connect() {
// For debug
// console.log("Header controller connected");
}
saveDraft(event) {
event.preventDefault();
const btn = document.querySelector('[data-editor--layout-target="saveDraftSubmit"]');
if (btn) btn.click();
}
publish(event) {
event.preventDefault();
const btn = document.querySelector('[data-editor--layout-target="publishSubmit"]');
if (btn) btn.click();
}
}

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

@ -92,6 +92,12 @@ export default class extends Controller {
const nostrController = this.getNostrPublishController(); const nostrController = this.getNostrPublishController();
if (nostrController && nostrController.hasJsonTextareaTarget && this.hasJsonTextareaTarget) { if (nostrController && nostrController.hasJsonTextareaTarget && this.hasJsonTextareaTarget) {
this.jsonTextareaTarget.value = nostrController.jsonTextareaTarget.value; this.jsonTextareaTarget.value = nostrController.jsonTextareaTarget.value;
// Update CodeMirror document to match textarea
if (this.cmView) {
this.cmView.dispatch({
changes: {from: 0, to: this.cmView.state.doc.length, insert: this.jsonTextareaTarget.value}
});
}
this.updateJsonContentFromMarkdown(); this.updateJsonContentFromMarkdown();
this.formatJson(); this.formatJson();
this.isDirty = false; this.isDirty = false;
@ -108,6 +114,12 @@ export default class extends Controller {
const json = nostrController.jsonTextareaTarget.value; const json = nostrController.jsonTextareaTarget.value;
if (json && this.hasJsonTextareaTarget) { if (json && this.hasJsonTextareaTarget) {
this.jsonTextareaTarget.value = json; this.jsonTextareaTarget.value = json;
// Update CodeMirror document to match textarea
if (this.cmView) {
this.cmView.dispatch({
changes: {from: 0, to: this.cmView.state.doc.length, insert: this.jsonTextareaTarget.value}
});
}
this.updateJsonContentFromMarkdown(); this.updateJsonContentFromMarkdown();
this.formatJson(); this.formatJson();
} }
@ -124,6 +136,12 @@ export default class extends Controller {
setTimeout(() => { setTimeout(() => {
if (nostrController.hasJsonTextareaTarget && this.hasJsonTextareaTarget) { if (nostrController.hasJsonTextareaTarget && this.hasJsonTextareaTarget) {
this.jsonTextareaTarget.value = nostrController.jsonTextareaTarget.value; this.jsonTextareaTarget.value = nostrController.jsonTextareaTarget.value;
// Update CodeMirror document to match textarea
if (this.cmView) {
this.cmView.dispatch({
changes: {from: 0, to: this.cmView.state.doc.length, insert: this.jsonTextareaTarget.value}
});
}
this.formatJson(); this.formatJson();
this.isDirty = false; this.isDirty = false;
this.updateDirtyHint(); this.updateDirtyHint();

29
assets/controllers/editor/layout_controller.js

@ -6,7 +6,8 @@ export default class extends Controller {
'modeTab', 'editPane', 'markdownPane', 'jsonPane', 'previewPane', 'modeTab', 'editPane', 'markdownPane', 'jsonPane', 'previewPane',
'previewBody', 'previewTitle', 'previewBody', 'previewTitle',
'previewSummary', 'previewImage', 'previewImagePlaceholder', 'previewAuthor', 'previewDate', 'previewSummary', 'previewImage', 'previewImagePlaceholder', 'previewAuthor', 'previewDate',
'markdownEditor', 'markdownTitle', 'markdownCode', 'status' 'markdownEditor', 'markdownTitle', 'markdownCode', 'status',
'saveDraftSubmit', 'publishSubmit'
]; ];
connect() { connect() {
@ -214,12 +215,15 @@ export default class extends Controller {
} }
saveDraft() { saveDraft() {
console.log('Saving draft...'); // Only for mobile actions, not header
alert('[Editor] saveDraft called');
// Mark as draft - set checkbox to true // Mark as draft - set checkbox to true
const draftCheckbox = this.element.querySelector('input[name*="[isDraft]"]'); const draftCheckbox = this.element.querySelector('input[name*="[isDraft]"]');
if (draftCheckbox) { if (draftCheckbox) {
draftCheckbox.checked = true; draftCheckbox.checked = true;
} else {
console.warn('[Editor] Draft checkbox not found');
} }
// Submit the form // Submit the form
@ -227,16 +231,22 @@ export default class extends Controller {
if (form) { if (form) {
this.updateStatus('Saving draft...'); this.updateStatus('Saving draft...');
form.requestSubmit(); form.requestSubmit();
console.log('[Editor] Form submitted for draft');
} else {
console.error('[Editor] Form not found for saveDraft');
} }
} }
publish() { publish() {
console.log('Publishing article...'); // Only for mobile actions, not header
alert('[Editor] publish called');
// Mark as NOT draft - set checkbox to false // Mark as NOT draft - set checkbox to false
const draftCheckbox = this.element.querySelector('input[name*="[isDraft]"]'); const draftCheckbox = this.element.querySelector('input[name*="[isDraft]"]');
if (draftCheckbox) { if (draftCheckbox) {
draftCheckbox.checked = false; draftCheckbox.checked = false;
} else {
console.warn('[Editor] Draft checkbox not found');
} }
// Find the Nostr publish controller and trigger publish // Find the Nostr publish controller and trigger publish
@ -246,10 +256,19 @@ export default class extends Controller {
); );
if (nostrController) { if (nostrController) {
console.log('[Editor] Nostr publish controller found, calling publish()');
nostrController.publish(); nostrController.publish();
} else { } else {
console.error('Nostr publish controller not found'); // Fallback: submit the form
alert('Could not find publishing controller. Please try again.'); const form = this.element.querySelector('form');
if (form) {
this.updateStatus('Publishing...');
form.requestSubmit();
console.log('[Editor] Form submitted for publish');
} else {
console.error('[Editor] Form not found for publish');
alert('Could not find publishing controller or form. Please try again.');
}
} }
} }

4
assets/styles/03-components/form.css

@ -54,8 +54,8 @@ input:focus, textarea:focus, select:focus {
.help-text { .help-text {
font-size: 0.8rem; font-size: 0.8rem;
color: var(--color-text); color: var(--color-text-mid);
font-weight: lighter; margin-bottom: var(--spacing-1);
} }
#editor_actions { #editor_actions {

9
templates/editor/layout.html.twig

@ -13,7 +13,7 @@
{% endblock %} {% endblock %}
{% block header %} {% block header %}
<header class="editor-header"> <header class="editor-header" data-controller="editor--header" data-editor--header-target="header">
<div class="editor-header-left"> <div class="editor-header-left">
<a href="{{ path('home') }}" class="btn">← Back</a> <a href="{{ path('home') }}" class="btn">← Back</a>
<div class="editor-title"> <div class="editor-title">
@ -26,10 +26,10 @@
<span class="editor-status text-muted" data-editor--layout-target="status"> <span class="editor-status text-muted" data-editor--layout-target="status">
{% if article.id %}Editing{% else %}New article{% endif %} {% if article.id %}Editing{% else %}New article{% endif %}
</span> </span>
<button type="button" class="btn btn-secondary" data-action="editor--layout#saveDraft"> <button type="button" class="btn btn-secondary" data-action="editor--header#saveDraft">
Save draft Save draft
</button> </button>
<button type="button" class="btn btn-primary" data-action="editor--layout#publish"> <button type="button" class="btn btn-primary" data-action="editor--header#publish">
Publish Publish
</button> </button>
</div> </div>
@ -265,6 +265,9 @@
</div> </div>
</div> </div>
</div> </div>
<button type="submit" name="_action" value="save_draft" style="display:none;" data-editor--layout-target="saveDraftSubmit"></button>
<button type="submit" name="_action" value="publish" style="display:none;" data-editor--layout-target="publishSubmit"></button>
</div> </div>
</div> </div>
{# Right sidebar (last grid column) #} {# Right sidebar (last grid column) #}

Loading…
Cancel
Save