简体   繁体   中英

TinyMCE Spellchecker Rails not working

I am currently using the tinymce-rails gem found here https://github.com/spohlenz/tinymce-rails and I am having trouble getting the spellchecker to initialize. The TinyMCE editor works fine otherwise.

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"
});

The erb <%= f.text_area :completed, :class => "tinymce", :size => 500 %> generates the following:

<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">

I note that the spellcheck field is false, but I'm not sure why, or if that is actually related to what the problem is.

This post , and this post suggest that it's a bit more involved than just adding it to the list of plugins & setting the language.

Both posts suggest using aspell to check spellings, adding the line :spellchecker_rpc_url => "/users/spellchecker", to the TinyMCE configuration, and writing some custom Controller code.

I hope those links help you out.

Possible duplicate: TinyMCE 4.0.5 spell check not working

According to what I've found elsewhere, the spellchecker plugin was powered by Google service - which has been retired. So at this time there does not appear to be an integrated TinyMCE spellchecker solution.

However, you CAN enable the browser's built-in spellchecker by doing the following:

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

Be sure to remove spellchecker from your toolbar and your plugins list.

I've managed to easily get as-you-type-check working, just by adding this to my config/tinymce.yml

browser_spellcheck:
  - true

I ended up using the suggestions in one of @James Chevalier's links and following the TinyMCE v4 docs to write this:

# 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'
});

You must have aspell and the required dictionaries installed, of course.

I don't recommend doing it for new projects, do what @wloescher and @Ruby Racer said. I've only done this for a legacy project that must support it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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