代码段消失只有纯文本保留在tinymce

时间:2016-09-09 12:11:02

标签: laravel tinymce code-snippets

每次我在timymce编辑器中插入代码片段并发布文章。它看起来还不错。但如果我再次编辑文章,我的大部分代码都会消失,只剩下文字。我正在使用laravel框架。

例如我将此代码段放在我的文章中并发布

Here is the full code that you can copy paste. This is my article title.
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
 <head>
 <meta charset="<?php bloginfo('charset' ); ?>">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
 <meta name="description" content="<?php bloginfo('description'); ?>">

 <title><?php bloginfo('name'); ?> | <?php is_front_page() ? bloginfo('description') : wp_title(); ?><?php wp_title(); ?></title>

 <!-- Custom styles for this template -->
 <link href="<?php bloginfo('template_url'); ?>/css/bootstrap.css" rel="stylesheet">
 <link href="<?php bloginfo('stylesheet_url'); ?>" rel="stylesheet">
 <link href="/css/font-awesome.css" rel="stylesheet">
 <?php wp_head(); ?>
 </head>

 <body>

 <div class="blog-masthead">
 <div class="container">
 <nav class="blog-nav">
 <a class="blog-nav-item active" href="#">Home</a>
 <a class="blog-nav-item" href="#">New features</a>
 <a class="blog-nav-item" href="#">Press</a>
 <a class="blog-nav-item" href="#">New hires</a>
 <a class="blog-nav-item" href="#">About</a>
 </nav>
 </div>
 </div>

现在它会发布好的。但是,如果我再次编辑那篇文章,我所看到的就是这段代码:一旦我点击编辑,我所有以前的代码块都消失了,只留下了文字。

<pre class="language-markup"><code>
&gt;
 
 
 
 
 

 &lt;?php bloginfo('name'); ?&gt; | &lt;?php is_front_page() ? bloginfo('description') : wp_title(); ?&gt;&lt;?php wp_title(); ?&gt;

 
 
 
 
 
 

 
 Home
 New features
 Press
 New hires
 About
 
 
 </code></pre>

正如您所看到的,两个代码块应该看起来相同但不会发生。

这是我的tinymce编辑器配置:

<script src="http://code.jquery.com/jquery.js"></script>
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>
      var editor_config = {
        path_absolute : "{{ URL::to('/') }}/",
        selector: "textarea",
        //entity_encoding: "raw",
        // encoding: 'xml',
        plugins: [
          "advlist autolink lists link image charmap print preview hr anchor pagebreak",
          "searchreplace wordcount visualblocks visualchars code fullscreen",
          "insertdatetime media nonbreaking save table contextmenu directionality",
          "emoticons template paste textcolor colorpicker textpattern",
          "codesample",
          "spellchecker"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media | spellchecker | codesample",
        menubar: "tools",
        spellchecker_callback: function(method, text, success, failure) {
            var words = text.match(this.getWordCharPattern());
            if (method == "spellcheck") {
              var suggestions = {};
              for (var i = 0; i < words.length; i++) {
                suggestions[words[i]] = ["First", "Second"];
              }
              success(suggestions);
            }
          },
        relative_urls: false,
        file_browser_callback : function(field_name, url, type, win) {
          var x = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
          var y = window.innerHeight|| document.documentElement.clientHeight|| document.getElementsByTagName('body')[0].clientHeight;

          var cmsURL = editor_config.path_absolute + 'laravel-filemanager?field_name=' + field_name;
          if (type == 'image') {
            cmsURL = cmsURL + "&type=Images";
          } else {
            cmsURL = cmsURL + "&type=Files";
          }

          tinyMCE.activeEditor.windowManager.open({
            file : cmsURL,
            title : 'Filemanager',
            width : x * 0.8,
            height : y * 0.8,
            resizable : "yes",
            close_previous : "no"
          });
        }
      };

      tinymce.init(editor_config);
    <!--   -->
</script>

我花了几个小时到处寻找但没有成功。你的帮助会很高兴。可能我在配置中遗漏了一些东西。我想使用带有laravel的tinymce创建博客文章,并将使用包含在pre / code中的大量代码片段。谢谢。

这是我的创建页面textarea和编辑页面textarea,非常相似: 我在两个页面都包含了tinymce脚本,代码就是我上面发布的内容。

<div class="form-group">
     {!! Form::label("body", "Body:") !!}
     {!! Form::textarea("body", null, ['class' => 'form-control']) !!}  
 </div>

这是我的文章创建控制器:

public function store(Request $request)
    {
        $this->validate($request, $rules);

        $input = $request->all();
        $input['slug'] = str_slug($request->title);
        $input['user_id'] = Auth::user()->id;
        $input['meta_title'] = $request->title;
        if ($file = $request->file('photo_id')) {
            $name = Carbon::now(). '.' .$file->getClientOriginalName();
            $file->move('images', $name);
            $photo = Photo::create(['photo' => $name]);
            $input['photo_id'] = $photo->id;
        }
        $article = Article::create($input);
        if ($categoryIds = $request->category_id) {
            $article->category()->sync($categoryIds);
        }
        notify()->flash('<h3>New article has been created successfully</h3>', 'success', ['timer' => 2000]);
                return redirect()->back()->withInput();
    }

这是我的文章更新控制器:

public function update(Request $request, $id)
    {
        $this->validate($request, $rules);
        $article = Article::findOrFail($id);
        $input['slug'] = str_slug($request->title);
        $input['meta_title'] = $request->title;
        $input = $request->all();
        if ($file = $request->file('photo_id')) {
            if ($article->photo) {
            unlink('images/' . $article->photo->photo);
            $article->photo()->delete('photo');
            }
            $name = Carbon::now(). '.' .$file->getClientOriginalName();
            $file->move('images', $name);
            $photo = Photo::create(['photo' => $name]);
            $input['photo_id'] = $photo->id;
        }
        $article->update($input);
        if ($categoryIds = $request->category_id) {
            $article->category()->sync($categoryIds);
        }
        notify()->flash('<h3>You have successfully edit the article.</h3>', 'success', ['timer' => 2000]);
        return redirect()->back()->withInput();
    }

0 个答案:

没有答案