将所见即所得编辑器集成到最佳实践文本区域中

8
我正在使用best_in_place gem来进行客户信息的即时编辑。
我的问题是,如何集成一个所见即所得的编辑器以便以HTML的形式来编辑内容呢? 我目前正在使用这个编辑器:https://github.com/Nerian/bootstrap-wysihtml5-rails/ 由于我不擅长Javascript和Coffeescript,所以我可能做错了什么。
下面是我的视图代码:

<%= best_in_place @client, :info, :type => :textarea, :nil => "Click here to add content!", :html_attrs => { class: "wysihtml5" }, :sanitize => false %>

And clients.js.cofee

$(document).ready ->

# Activating Best In Place
jQuery(".best_in_place").best_in_place()

# Loading editor
$(".wysihtml5").each (i, elem) ->
$(elem).wysihtml5()

有人知道如何解决这个问题吗?

谢谢


你最终解决了这个问题吗? - dgilperez
5个回答

1

尝试这样做,inner_class: "wysihtml5" 如果存在,激活 WisiHTML5 编辑器,按钮“保存”和“取消”必须存在(默认事件处理程序已禁用以确保正确工作)

在 show.html.erb 中:

  <%= best_in_place @client, :info, type: :textarea, 
                                    nil: "Click here to add content!", 
                                    inner_class: "wysihtml5", 
                                    sanitize: false,
                                    ok_button: 'Save',
                                    cancel_button: 'Cancel',
                                    display_with: lambda { |v| v.html_safe }
  %>

在咖啡脚本文件中:
jQuery ->
  # WisiHTML5 Edit
  $('.best_in_place')
    # append
    .best_in_place()
    # init
    .on 'best_in_place:activate', () -> 
      if $(this).attr('data-inner-class') == 'wysihtml5'
        html = $(this).attr('data-original-content')
        area = $('textarea', this)
        area.addClass('span7').unbind('blur')
        area.wysihtml5().data('wysihtml5').editor.on 'load', (e)->
          this.setValue(html, true)
    # update
    .on 'best_in_place:success', (e, data) ->
      if $(this).attr('data-inner-class') == 'wysihtml5'
        attr = $(this).attr('data-attribute')
        data = jQuery.parseJSON(data)
        if data[attr]
          $(this).attr('data-original-content', data[attr])
          $(this).html(data[attr])

在show.json.jbuilder文件中:
json.extract! @client, :info, ... <any other attributes>

0

这可能是Coffeescript的缩进问题。只需在$(document).ready ->下面的行前加一个制表符即可。

$(document).ready ->
  # Activating Best In Place
  jQuery(".best_in_place").best_in_place()

  # Loading editor
  $(".wysihtml5").each (i, elem) ->
  $(elem).wysihtml5()

为进一步验证,请将代码复制到咖啡脚本框架@ http://js2coffee.org/ 中:生成的JavaScript应如下所示:

$(document).ready(function() {
  jQuery(".best_in_place").best_in_place();
  $(".wysihtml5").each(function(i, elem) {});
  return $(elem).wysihtml5();
});

也就是说,插件应该在文档对象模型加载完成后调用。

0
你需要迭代每个类名为"wysihtml5"的元素,代码应该是这样的。
$(function(){
  jQuery(".best_in_place").best_in_place();
  $.each('.wysihtml5', function(i, elem){
    $(elem).wysihtml5();
  });
});

并且使用 Js2Coffee 转换为 CoffeeScript 后应该能正常工作。

$ ->
 jQuery(".best_in_place").best_in_place()
 $.each ".wysihtml5", (i, elem) ->
   $(elem).wysihtml5()   

0

我正在使用CK-Editor,我相信这可能与你的所谓所见即所得编辑器有些类似。

以下是一个关于这个问题的示例和解决方案: 长篇文章述说了他如何转向这种编辑器:

http://blog.amps211.com/this-is-me-professing-my-love-for-jquery-and-how-i-got-ckeditor-working-with-jeditable/

编辑器更新的文件 http://blog.amps211.com/wp-content/uploads/2009/10/jquery.generateId.js http://blog.amps211.com/wp-content/uploads/2009/10/jquery.jeditable.ckeditor.js


0

我自己也在尝试这个,但是很明显这并不容易。

最终我是这样让它工作的:

你需要在文本框元素上调用wysihtml5(),并将其绑定到best_in_place:activate事件。这是我的application.js.coffee代码:

$(".best_in_place").each (index, element) ->
  $element  = $(element)

  $element
  .best_in_place()
  .on 'best_in_place:activate', () ->
    $element.find(".wysihtml5").each ->
      $this = $(this)
      $this.wysihtml5()

我使用帮助器的 inner_class 选项添加了 wysihtml5 CSS 类:

<%= best_in_place @client, :info, type: :textarea, nil: "Click here to add content!", inner_class: 'wysihtml5' }, sanitize: false, display_as: :info_html %>

请注意,我正在使用display_as: :info_html,因为我在模型上有该字段来存储处理过的HTML,但如果不是这种情况,您可以使用display_with: lambda { |v| v.html_safe }
现在来到棘手的部分:当前版本(3.0.3)的best_in_placewysihtml5不兼容,因为文本区域被模拟成可编辑的iframe,这导致了可编辑操作被取消。我需要自己对gem进行一些修改,希望能够合并(您可以在此处看到完整的讨论),但同时您可以使用我的fork。如果您这样做,那么您需要为视图助手提供这个额外选项:skip_blur: true

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接