-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Editor preview #528
Changes from 4 commits
7eb1825
c0819ec
da4f281
2145286
e57b7c0
66d3af4
a4c024c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused import? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can name this route |
||
* | ||
* 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...'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool |
||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very cool 😜 |
||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParent() | ||
{ | ||
return TextareaType::class; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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...'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); |
There was a problem hiding this comment.
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
.