【问题标题】:TinyMCE Spellchecker Rails not workingTinyMCE Spellchecker Rails 不工作
【发布时间】:2013-01-04 21:12:49
【问题描述】:

我目前正在使用 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">

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

【问题讨论】:

    标签: javascript ruby-on-rails tinymce


    【解决方案1】:

    This postthis post 建议这不仅仅是将其添加到插件列表和设置语言。

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

    希望这些链接对您有所帮助。

    【讨论】:

      【解决方案2】:

      可能重复:TinyMCE 4.0.5 spell check not working

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

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

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

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

      【讨论】:

      • 天哪,我爱你!您的解决方案与其他解决方案一样好,而且工作量减少了大约 1000 倍
      • 很高兴为您服务,@Gerry!
      【解决方案3】:

      只需将其添加到我的 config/tinymce.yml 中,我就可以轻松地进行 as-you-type-check 工作

      browser_spellcheck:
        - true
      

      【讨论】:

        【解决方案4】:

        我最终使用了@James Chevalier 的links 之一中的建议,并按照TinyMCE v4 docs 写了这个:

        # 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 所说的去做。我只为必须支持它的遗留项目这样做。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多