简体   繁体   English

如何将Bootstrap-WYSIWYG集成到我的Rails应用程序中?

[英]How can i integrate Bootstrap-WYSIWYG in my Rails app?

There is a pretty bootstrap editor - Bootstrap WYSIWYG , which i want to use in my blog based on RoR 4.0. 有一个漂亮的自举编辑器-Bootstrap WYSIWYG ,我想在基于RoR 4.0的博客中使用它。 The problem is that Bootstrap WYSIWYG does not work with anything except DIV tag (as far as i know from a bit searching). 问题是, Bootstrap WYSIWYG除DIV标记外均不起作用(据我从一点搜索中了解到)。

This code works fine: 这段代码可以正常工作:

<div id="editor" contenteditable="true">some editable content</div>

And this one doesnt: 而这个没有:

<%= f.text_area :content, id: "editor", contenteditable: "true" %>
<textarea id="editor" contenteditable="true">

So, the question is - how to connect this two things together? 因此,问题是-如何将这两件事连接在一起?

have you tried putting a hidden field, working with the div and when the editor have changes update the hidden field value? 您是否尝试过放置隐藏字段,使用div以及当编辑器进行更改时更新了隐藏字段值? Hope this helps 希望这可以帮助

In order to integrate Bootstrap-WYSIWYG in a Ruby on Rails app you should do the following: 为了将Bootstrap-WYSIWYG集成到Ruby on Rails应用程序中,您应该执行以下操作:

(most of the times - you have more than one editor in a rails form, in this example I will show how to do it without errors) (大多数情况下-您在Rails表单中有多个编辑器,在此示例中,我将展示如何做到无错)

I will use the editor in the admin namespace, therefore I have created a folder editor inside my views: "admin/shared/editor" to keep everything tide and content oriented. 我将在admin名称空间中使用该编辑器,因此,我在视图内部创建了一个文件夹编辑器: “ admin / shared / editor”,以使所有内容和内容保持潮流。

Firstly, for every attribute of a model I would like to use the Bootstrap WYSIWYG editor I will render a partial with the field that has integrated the editor, so you might have smth like this: 首先,对于模型的每个属性,我都想使用Bootstrap WYSIWYG编辑器,我将使用集成了该编辑器的字段来呈现局部视图,因此您可能会遇到如下问题:

/admin/posts/_form.html.haml : /admin/posts/_form.html.haml

= form_for @post do |f|    
  = render partial: 'admin/shared/editor/field', locals: { f: f, content: "summary" }
  = f.button class: "form_with_editor"

were you pass as a local parameter the form and the attribute of the model you would like to apply the editor (in this case => summary ). 如果将您希望应用编辑器的模型的形式和属性作为本地参数传递(在这种情况下=> summary )。 Note also that you should add a class to the submit button of the form: .form_with_editor , that will be used later for the button click listener. 还要注意,您应该将一个类添加到以下形式的提交按钮: .form_with_editor ,稍后将用于按钮单击侦听器。

Now inside admin/shared/editor/_field.html.haml: 现在位于admin / shared / editor / _field.html.haml中:

.btn-toolbar.btn-editor{"data-role" => "editor-toolbar", "data-target" => "#editor_area_for_#{content}"}
  = render partial: 'admin/shared/editor/toolbar'

.wysiwyg{id: "editor_area_for_#{content}", style: "overflow:auto; height:444px;max-height:444px", data: {"for-content" => "hidden_#{content}"}}
= f.hidden_field content.to_sym, id: "hidden_#{content}"

This editor works with a div and not with a textarea, therefore, we will be using a div with class .wysiwyg and a dynamic id which in this case evaluates to: #editor_area_for_summary 该编辑器仅适用于div而不适用于textarea,因此,我们将使用具有.wysiwyg类和动态ID的div,在这种情况下,其结果为: #editor_area_for_summary

The class: .wysiwyg is used in order to select all the divs with this class when we initialize the editor in our doc ready function. 当在doc ready函数中初始化编辑器时,将使用类: .wysiwyg来选择该类的所有div。

The toolbar partial contains all the markup for the custom toolbar of the editor, you can customize it as you wish and use it in all your fields. 工具栏部分包含编辑器的自定义工具栏的所有标记,您可以根据需要对其进行自定义,并在所有字段中使用它。

In order to copy the contents of the div to the form input and post them to the server, you have to use a hidden input field : 为了将div的内容复制到表单输入并将其发布到服务器,您必须使用隐藏的输入字段

= f.hidden_field content.to_sym, id: "hidden_#{content}"

Note: it also gets a dynamic id(which evaluates to: "hidden_summary") and a name -> :summary 注意:它还会获取一个动态id(其值为:“ hidden_​​summary”)和一个名称->:summary

Now in order to make all these to work together we need some javascript on your doc ready function: 现在,为了使所有这些协同工作,我们需要在您的doc ready函数上使用一些javascript:

/assets/javascripts/doc_ready.js: /assets/javascripts/doc_ready.js:

$( document ).ready(function() {

  // EDITOR stuff
  $('.wysiwyg').wysiwyg();

  // Handling the form submission
  $('.form_with_editor').click(function(){
    $('.wysiwyg').each(function() {
      var editor_div_content = $(this).html();
      var hidden_input = $(this).attr("data-for-content");
      $("#" + hidden_input).val(editor_div_content);
    });
  });

  // Fill the editor divs with content
  $('.wysiwyg').each(function() {
      var hidden_input = $(this).attr("data-for-content");
      var editor_div_content =  $("#" + hidden_input).val();
      $(this).html(editor_div_content);
    });


});

On the first part we apply the function wysiwyg() of the editor in every div that has this class. 在第一部分中,我们在具有此类的每个div中应用编辑器的wysiwyg()函数。

On the second part we have an on click handler of the form button we get the html content of the div and we set it as the value of our hidden input field. 在第二部分,我们有一个表单按钮的单击处理程序 ,我们获取div的html内容,并将其设置为隐藏输入字段的值。

And on the last part we do the opposite, we get the value of the hidden field and place it on the editors div when the document is ready, with this one we display content on the editor that is already stored in the database. 而在最后一部分,我们做相反的事情,我们获取隐藏字段的值,并在文档准备好后将其放置在编辑器div上,通过此操作,我们在已存储在数据库中的编辑器上显示内容。


I hope this one helps :) 我希望这可以帮助:)

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

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