简体   繁体   English

Symfony2 + TinyMCE->极端文本区域错误

[英]Symfony2 + TinyMCE -> extreme textarea errors

I'm currently using Symfony2 and a bootstrap template. 我当前正在使用Symfony2和一个引导模板。 I would like to use one of TinyMCE's extreme textarea for a newsletter, but I am running into issues while getting it to work. 我想将TinyMCE的极端文本区域之一用作时事通讯,但是在使其工作时却遇到了问题。

Here's a link to the template where you can see the textareas : http://devoops.me/themes/devoops/#ajax/forms_layouts.html 这是模板的链接,您可以在其中查看文本区域: http : //devoops.me/themes/devoops/#ajax/forms_layouts.html

It comes with a general js script. 它带有一个通用的js脚本。 There is a start tinymce function : 有一个启动tinymce函数:

function TinyMCEStart(elem, mode) {
    var plugins = [];
    if (mode == 'extreme') {
        plugins = ["advlist anchor autolink autoresize autosave bbcode charmap code contextmenu directionality ",
            "emoticons fullpage fullscreen hr image insertdatetime layer legacyoutput",
            "link lists media nonbreaking noneditable pagebreak paste preview print save searchreplace",
            "tabfocus table template textcolor visualblocks visualchars wordcount"];
    }
    <!-- ADDED BY ME
    tinyMCE.baseURL = 'http://'+location.host+resourcesUrl+"/plugins/tinymce";
    tinyMCE.baseURI.setPath(resourcesUrl+"/plugins/tinymce");
    tinyMCE.documentBaseURL = 'http://'+location.host+'/ormindo/web/';
    -->
    tinyMCE.init({selector: elem,
        theme: "modern",
        plugins: plugins,
        //content_css: "css/style.css",
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media fullpage | forecolor backcolor emoticons",
        style_formats: [
            {title: 'Header 2', block: 'h2', classes: 'page-header'},
            {title: 'Header 3', block: 'h3', classes: 'page-header'},
            {title: 'Header 4', block: 'h4', classes: 'page-header'},
            {title: 'Header 5', block: 'h5', classes: 'page-header'},
            {title: 'Header 6', block: 'h6', classes: 'page-header'},
            {title: 'Bold text', inline: 'b'},
            {title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
            {title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
            {title: 'Example 1', inline: 'span', classes: 'example1'},
            {title: 'Example 2', inline: 'span', classes: 'example2'},
            {title: 'Table styles'},
            {title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
        ]
    });
}

While fiddling with the template, I have come to the conclusion that this code : 在摆弄模板时,我得出以下结论:

<textarea class="form-control" rows="5" id="wysiwig_full"></textarea>
<script type="text/javascript">
$(document).ready(function() {
    TinyMCEStart('#wysiwig_full', 'extreme');
});
</script>

is enough to get a textarea turned into a TinyMCE extreme text editor at any time. 足以随时将文本区域转换为TinyMCE极端文本编辑器。

The problem lies in getting this to work with a Symfony-generated form. 问题在于使它与Symfony生成的表单一起使用。 I tried a first approach, not the prettiest but at least it does something (script is added with {% javascripts %}) : 我尝试了第一种方法,不是最漂亮的方法,但至少它做了点什么(脚本中添加了{%javascripts%}):

$(document).ready(function() {   
// Create Wysiwig editor for textarea
TinyMCEStart('#ormindo_newsletterbundle_newslettermessage_contents', 'extreme');
});

This does work, alas only once. 确实可以,但是只有一次。 If I load another ajax content and come back, the textarea will be a standard one. 如果我加载另一个ajax内容并返回,则textarea将是标准的内容。 Inspecting the page while doing so does not yield any javascript error. 这样做时检查页面不会产生任何javascript错误。

Trying to break things down, I copy/pasted the raw html generated by Symfony (or should I say Twig) into the associated twig file. 为了分解问题,我将Symfony(或者我应该说Twig)生成的原始html复制/粘贴到关联的twig文件中。

I now had a twig file looking like this : 我现在有一个树枝文件,看起来像这样:

<div class="form-group">
<textarea id="ormindo_newsletterbundle_newslettermessage_contents" name="ormindo_newsletterbundle_newslettermessage[contents]" class="form-control"></textarea>
</div>
<script type="text/javascript">
$(document).ready(function() {
    TinyMCEStart('#ormindo_newsletterbundle_newslettermessage_contents', 'extreme');
});
</script>

This still did not work. 这仍然没有用。 However, removing the <div class="form-group"></div> tag made it work flawlessly. 但是,删除<div class="form-group"></div>标记可以使其正常工作。 I have been trying several things, trying different TinyMCE version, hunting coding errors, to no avail. 我一直在尝试几种方法,尝试使用不同的TinyMCE版本,寻找编码错误,但无济于事。

Considering myself a web-dev novice, I fear this is the kind of error where I need an external point of view. 考虑到自己是一名Web开发新手,我担心这是需要外部观点的错误。 Any kind of help will be greatly appreciated. 任何帮助将不胜感激。

How are you generating the symfony 2 form? 您如何生成symfony 2表单? As far as I know it is a simple matter to generate the tinymce, you just set an id/class for the form input that you wish to turn into a tiny mce, and then you set your javascript to look for that id/class and transform it. 据我所知,生成tinymce很简单,您只需为要转换为微小mce的表单输入设置一个id / class,然后设置javascript以查找该id / class,然后转换它。

To give a special id for a form field in symfony you do the following: 要为symfony中的表单字段提供特殊的ID,请执行以下操作:

{{form_widget(form.text,{'id':'wysiwig_full'})}}

That will render a the field called text with the id wysiwig_full. 这将渲染一个名为id为wysiwig_full的文本字段。 Replace text with whatever the name of your field is. 用您的字段名称替换文本。

I was absolutely unable to find the source of my issues. 我绝对找不到问题的根源。 Being quite fed up, I switched up to CKeditor, and it worked flawlessly out of the box, I just had to change the editor path var because I have a folder containing all my plugins. 厌倦了,我切换到了CKeditor,它开箱即用地工作了,我只需要更改编辑器路径var,因为我有一个包含所有插件的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM