Skip to content

Editor preview #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/Resources/translations/messages.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@
<source>rss.description</source>
<target>Most recent posts published on the Symfony Demo blog</target>
</trans-unit>
<trans-unit id="label.write">
<source>label.write</source>
<target>Write</target>
</trans-unit>
<trans-unit id="label.preview">
<source>label.preview</source>
<target>Preview</target>
</trans-unit>
</body>
</file>
</xliff>
11 changes: 11 additions & 0 deletions app/Resources/views/admin/blog/edit.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
{% extends 'admin/layout.html.twig' %}

{% block javascripts %}
{{ parent() }}
<script>
$('a[data-toggle="tab"]').on('click', function (e) {
if ('#previewId' == $(e.target).attr('href')) {
SymfonyDemo.preview('#post_content', '#previewId', '{{ path('preview') }}');
}
});
</script>
{% endblock %}

{% block body_id 'admin_post_edit' %}

{% block main %}
Expand Down
11 changes: 11 additions & 0 deletions app/Resources/views/admin/blog/new.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
{% extends 'admin/layout.html.twig' %}

{% block javascripts %}
{{ parent() }}
<script>
$('a[data-toggle="tab"]').on('click', function (e) {
if ('#previewId' == $(e.target).attr('href')) {
SymfonyDemo.preview('#post_content', '#previewId', '{{ path('preview') }}');
}
});
</script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create a template file to do that and avoid duplicated code in edit.html.twig.

{% endblock %}

{% block body_id 'admin_post_new' %}

{% block main %}
Expand Down
13 changes: 13 additions & 0 deletions app/Resources/views/form/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@
</span>
</div>
{% endblock %}

{% block editor_widget %}
<ul class="nav nav-tabs">
<li class="active"><a href="#writeId" data-toggle="tab">{{ 'label.write'|trans }}</a></li>
<li><a href="#previewId" data-toggle="tab">{{ 'label.preview'|trans }}</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="writeId">
{{ block('textarea_widget') }}
</div>
<div class="tab-pane" style="min-height: 200px;" id="previewId"></div>
</div>
{% endblock %}
23 changes: 23 additions & 0 deletions src/AppBundle/Controller/Admin/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\JsonResponse;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elkuku, Why do you import this class? You don't use it in the previewAction.

use Symfony\Component\HttpFoundation\Request;

/**
Expand Down Expand Up @@ -189,4 +190,26 @@ public function deleteAction(Request $request, Post $post)

return $this->redirectToRoute('admin_post_index');
}

/**
* Converts a markdown string to HTML.
*
* @Route("/preview", name="preview")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can name this route admin_post_content_preview to be consistent with this action and others routes?

*
* This method uses the JsonResponse class which takes care of setting the
* correct content type for the response.
*/
public function previewAction(Request $request)
{
$response = [];
$text = $request->request->get('text');

if (!$text) {
$response['data'] = 'Nothing to preview...';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO should we return an empty string in this case.

} else {
$response['data'] = $this->get('markdown')->toHtml($text);
}

return new JsonResponse($response);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use $this->json() method to simplify this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool

}
}
3 changes: 2 additions & 1 deletion src/AppBundle/Form/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use AppBundle\Entity\Post;
use AppBundle\Form\Type\DateTimePickerType;
use AppBundle\Form\Type\EditorType;
use AppBundle\Form\Type\TagsInputType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
Expand Down Expand Up @@ -50,7 +51,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('summary', TextareaType::class, [
'label' => 'label.summary',
])
->add('content', null, [
->add('content', EditorType::class, [
'attr' => ['rows' => 20],
'label' => 'label.content',
])
Expand Down
33 changes: 33 additions & 0 deletions src/AppBundle/Form/Type/EditorType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;

/**
* Defines a custom form field type used to display a markdown editor with preview functionality.
*
* See http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html
*
* @author Nikolai Plath <[email protected]>
*/
class EditorType extends AbstractType
Copy link
Member

@yceruto yceruto Apr 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the only one value here is to override the block name used to render the form type, then use the block_name option instead (bonus plus to this PR) and remove entirely this form type.

For example:

// AppBundle\Form\PostType

->add('content', null, [
    // ...
    'block_name' => 'editor',
])
{# form/fields.html.twig #}

{% block _post_editor_widget %}
    ...
    {{ block('textarea_widget') }}
    ...
{% endblock %}

http://symfony.com/doc/current/reference/forms/types/form.html#block-name
http://symfony.com/doc/current/form/form_customization.html#form-customization-sidebar
http://symfony.com/doc/current/form/form_themes.html#form-template-blocks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool 😜

{
/**
* {@inheritdoc}
*/
public function getParent()
{
return TextareaType::class;
}
}
21 changes: 21 additions & 0 deletions web/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,25 @@
.modal('show');
}
});

window.SymfonyDemo = {
/**
* Render a Markdown input to formatted HTML
*
* @param {String} text
* @param {String} preview
* @param {String} previewUrl
*/
preview: function (text, preview, previewUrl) {
var out = $(preview);

out.html('Loading preview...');
Copy link
Member

@yceruto yceruto Apr 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid the translation issue, you can set this text by default into tab panel preview and translate it then, or use an icon loader instead, in any case this line should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a CSS loading animation to avoid the translations..


$.post(
previewUrl,
{text: $(text).val()},
function (r) { out.html(r.data); }
);
}
}
})(window.jQuery);