繁体   English   中英

TinyMCE Spellchecker Rails 不工作

[英]TinyMCE Spellchecker Rails not working

我目前正在使用https://github.com/spohlenz/tinymce-rails 中的 tinymce-rails gem,但在初始化拼写检查器时遇到了问题。 否则 TinyMCE 编辑器工作正常。

Javascript:

tinyMCE.init({
  mode: "specific_textareas",
  editor_selector: "tinymce",
  theme: "advanced",
  theme_advanced_toolbar_location: "top",
  theme_advanced_toolbar_align: "left",
  theme_advanced_statusbar_location: "bottom",
  theme_advanced_buttons1: "bold,italic,underline,bullist,numlist",
  theme_advanced_buttons3: "tablecontrols,fullscreen,spellchecker",
  plugins: "table,autoresize,fullscreen,spellchecker",
  width: "100%",
  height: 400,
  autoresize_min_height: 400,
  autoresize_max_height: 800,
  language:"en",
  spellchecker_languages: "+English=en"
});

erb <%= f.text_area :completed, :class => "tinymce", :size => 500 %>生成以下内容:

<body id="tinymce" class="mceContentBody " contenteditable="true" onload="window.parent.tinyMCE.get('report_completed').onLoad.dispatch();" spellcheck="false" style="overflow-y: hidden; padding-bottom: 50px;" dir="ltr">

我注意到拼写检查字段是错误的,但我不确定为什么,或者这是否与问题有关。

这篇文章这篇文章表明它比将它添加到插件列表和设置语言要复杂一些。

aspell文章都建议使用aspell来检查拼写,将:spellchecker_rpc_url => "/users/spellchecker",到 TinyMCE 配置中,并编写一些自定义控制器代码。

我希望这些链接可以帮助您。

可能重复: TinyMCE 4.0.5 拼写检查不起作用

根据我在其他地方找到的内容,拼写检查器插件由 Google 服务提供支持 - 该服务已停用。 所以此时似乎没有集成的 TinyMCE 拼写检查器解决方案。

但是,您可以通过执行以下操作来启用浏览器的内置拼写检查器:

tinymce.init({
    browser_spellcheck : true,
});

请务必从工具栏和插件列表中删除拼写检查器。

我已经设法轻松地让 as-you-type-check 工作,只需将它添加到我的config/tinymce.yml

browser_spellcheck:
  - true

我最终使用了@James Chevalier 链接之一中的建议,并按照TinyMCE v4 文档编写了以下内容:

# config/routes.rb

post 'tinymce/spellcheck'
# app/controllers/tinymce_controller.rb

class TinymceController < ApplicationController
  skip_forgery_protection

  respond_to :json

  def spellcheck
    suggestions = check_spelling_new(
      spellcheck_params[:text],
      spellcheck_params[:lang]
    )

    render json: {words: suggestions}
  end

  private

  def spellcheck_params
    params.permit(:method, :lang, :text)
  end

  def check_spelling_new(text, lang)
    suggestions = {}

    spell_check_response = `echo "#{text}" | aspell -a -l #{lang}`

    if spell_check_response.present?
      spelling_errors = spell_check_response.split(' ').slice(1..-1)

      spelling_errors.length.times do |i|
        spelling_errors[i].strip!

        if spelling_errors[i].to_s.start_with?('&')
          match_data = spelling_errors[i + 1]
          suggestion_count = spelling_errors[i + 2].to_i

          suggestions[match_data] ||= []

          suggestion_count.times do |k|
            suggestions[match_data] << spelling_errors[i + k + 4].gsub(',', '')
          end
        end
      end
    end

    suggestions
  end
end

// tinymce.js

tinymce.init({
  ...,
  spellchecker_rpc_url: '.../tinymce/spellcheck'
});

当然,您必须安装aspell和所需的词典。

我不建议为新项目这样做,按照@wloescher 和@Ruby Racer 所说的去做。 我只为一个必须支持它的遗留项目完成了这项工作。

暂无
暂无

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

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