Browse Source

Quill editor templates and controller

imwald
Nuša Pukšič 1 year ago
parent
commit
539f05a202
  1. 71
      src/Controller/ArticleController.php
  2. 20
      templates/pages/editor.html.twig

71
src/Controller/ArticleController.php

@ -4,16 +4,19 @@ namespace App\Controller; @@ -4,16 +4,19 @@ namespace App\Controller;
use App\Entity\Article;
use App\Entity\User;
use App\Enum\KindsEnum;
use App\Form\EditorType;
use App\Service\NostrClient;
use App\Util\CommonMark\Converter;
use Doctrine\ORM\EntityManagerInterface;
use League\CommonMark\Exception\CommonMarkException;
use PHPUnit\Exception;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\InvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Workflow\WorkflowInterface;
class ArticleController extends AbstractController
{
@ -67,4 +70,70 @@ class ArticleController extends AbstractController @@ -67,4 +70,70 @@ class ArticleController extends AbstractController
]);
}
/**
* Create new article
*/
#[Route('/article-editor/create', name: 'editor-create')]
#[Route('/article-editor/edit/{id}', name: 'editor-edit')]
public function newArticle(Request $request, EntityManagerInterface $entityManager, WorkflowInterface $articlePublishingWorkflow, Article $article = null): Response
{
if (!$article) {
$article = new Article();
$article->setKind(KindsEnum::LONGFORM);
$article->setCreatedAt(new \DateTimeImmutable());
$formAction = $this->generateUrl('editor-create');
} else {
$formAction = $this->generateUrl('editor-edit', ['id' => $article->getId()]);
}
$form = $this->createForm(EditorType::class, $article, ['action' => $formAction]);
$form->handleRequest($request);
// Step 3: Check if the form is submitted and valid
if ($form->isSubmitted() && $form->isValid()) {
$user = $this->getUser();
$currentPubkey = $user->getUserIdentifier();
if ($article->getPubkey() === null) {
$article->setPubkey($currentPubkey);
}
// Check which button was clicked
if ($form->get('actions')->get('submit')->isClicked()) {
// Save button was clicked, handle the "Publish" action
$this->addFlash('success', 'Product published!');
} elseif ($form->get('actions')->get('draft')->isClicked()) {
// Save and Publish button was clicked, handle the "Draft" action
$this->addFlash('success', 'Product saved as draft!');
} elseif ($form->get('actions')->get('preview')->isClicked()) {
// Preview button was clicked, handle the "Preview" action
$article->setSig(''); // clear the sig
$entityManager->persist($article);
$entityManager->flush();
return $this->redirectToRoute('article-preview', ['id' => $article->getId()]);
}
}
// load template with content editor
return $this->render('pages/editor.html.twig', [
'article' => $article,
'form' => $this->createForm(EditorType::class, $article)->createView(),
]);
}
/**
* Preview article
*/
#[Route('/article-preview/{id}', name: 'article-preview')]
public function preview($id, EntityManagerInterface $entityManager): Response
{
$repository = $entityManager->getRepository(Article::class);
$article = $repository->findOneBy(['id' => $id]);
return $this->render('pages/article.html.twig', [
'article' => $article,
'author' => $this->getUser(),
]);
}
}

20
templates/pages/editor.html.twig

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
{% extends 'base.html.twig' %}
{% form_theme form _self %}
{% block quill_widget %}
<div {{ stimulus_controller('quill') }} class="quill" data-id="{{ id }}" >
<div id="editor">
{{ value|raw }}
</div>
</div>
<input type="hidden" {{ block('widget_attributes') }} value="{{ value }}" />
{% endblock %}
{% block body %}
{{ form_start(form) }}
{{ form_end(form) }}
{% endblock %}
Loading…
Cancel
Save