Skip to content

Commit

Permalink
added spell check, fixed flash, fixed path issues
Browse files Browse the repository at this point in the history
git-svn-id: svn://rubyforge.org//var/svn/fckeditorp/trunk/fckeditor@22 13c73f65-831a-0410-84ff-c4c9c9a2e73e
  • Loading branch information
srutherford committed Aug 26, 2006
1 parent 086b94e commit 3da68c5
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 27 deletions.
17 changes: 16 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Credits to:
"Implementation details for FCKEditor integration with Ruby on Rails" - http://public.ok2life.com/welcome/index/49.html
"FCKEditor On Rails" - http://www.underpantsgnome.com/projects/fckeditor-on-rails/

And for the spell checker, which Ken Pratt created a patch for:

Ken Pratt: www.kenpratt.net
http://softiesonrails.com/articles/2006/02/14/fckeditor-with-spell-check-on-rails

-------

This plugin installs the FCKeditor editor into your rails app, along with some helpers to use in your views.
Expand Down Expand Up @@ -61,12 +66,22 @@ To use a remote form you need to do something like this

If you forget to put in the :before it won't work

Spell Check
-----------

Basically ensure you have aspell installed and available in your path. If running on Windows then the expected install path is:

'c:\program files\aspell\bin\aspell'

If you install elsewhere then you will need to update spell_check.rb in the plugin to mirror your path. Also make sure you have this line in your fckcustom.js file:

FCKConfig.SpellChecker = 'SpellerPages';

Issues / TODO's
------

1) There is a problem with uploading Flash files
2) There are no progress indicators for the AJAX requests from the resource manager
2) ISpell is not yet implemented
3) No real docs....


44 changes: 34 additions & 10 deletions app/controllers/fckeditor_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ def command
upload_file
end

render :inline => RXML, :type => :rxml
render :inline => RXML, :type => :rxml unless params[:Command] == 'FileUpload'
end

def get_folders_and_files(include_files = true)
@url = UPLOADED + params[:CurrentFolder]
@folders = Array.new
@files = {}
@current_folder = UPLOADED_ROOT + params[:CurrentFolder]
@url = upload_directory_path
@current_folder = current_directory_path
Dir.entries(@current_folder).each do |entry|
next if entry =~ /^\./
path = @current_folder + entry
Expand All @@ -58,7 +58,7 @@ def get_folders_and_files(include_files = true)

def create_folder
begin
@url = UPLOADED_ROOT + params[:CurrentFolder]
@url = current_directory_path
path = @url + params[:NewFolderName]
if !(File.stat(@url).writable?)
@errorNumber = 103
Expand All @@ -76,28 +76,52 @@ def create_folder
end

def upload_file
new_file = params[:NewFile]
@new_file = params[:NewFile]
@url = upload_directory_path
begin
ftype = new_file.content_type.strip
ftype = @new_file.content_type.strip
if ! MIME_TYPES.include?(ftype)
@errorNumber = 202
puts "#{ftype} is invalid MIME type"
raise "#{ftype} is invalid MIME type"
else
dir = UPLOADED_ROOT + (params[:CurrentFolder] ? params[:CurrentFolder] : "/")
path = dir + new_file.original_filename
path = current_directory_path + "/" + @new_file.original_filename
File.open(path,"wb",0664) do |fp|
FileUtils.copy_stream(new_file, fp)
FileUtils.copy_stream(@new_file, fp)
end
@errorNumber = 0
end
rescue => e
@errorNumber = 110 if @errorNumber.nil?
end
render :inline => 'page << "window.parent.frames[\'frmUpload\'].OnUploadCompleted(#{@errorNumber}, \'#{new_file}\');"', :type => :rjs
render :inline => 'page << "window.parent.frames[\'frmUpload\'].OnUploadCompleted(#{@errorNumber}, \'#{@new_file}\');"', :type => :rjs
end

def upload
self.upload_file
end

include ActionView::Helpers::TextHelper
def check_spelling
require 'cgi'
require 'fckeditor_spell_check'

@original_text = params[:textinputs] ? params[:textinputs].first : ''
plain_text = strip_tags(CGI.unescape(@original_text))
@words = FckeditorSpellCheck.check_spelling(plain_text)

render :file => "#{Fckeditor::PLUGIN_VIEWS_PATH}/fckeditor/spell_check.rhtml"
end

private
def current_directory_path
base_dir = "#{UPLOADED_ROOT}/#{params[:Type]}"
Dir.mkdir(base_dir,0775) unless File.exists?(base_dir)
"#{base_dir}#{params[:CurrentFolder]}"
end

def upload_directory_path
uploaded = "#{UPLOADED}/#{params[:Type]}"
"#{uploaded}#{params[:CurrentFolder]}"
end
end
52 changes: 52 additions & 0 deletions app/views/fckeditor/spell_check.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<%= stylesheet_link_tag "/javascripts/fckeditor/editor/dialog/fck_spellerpages/spellerpages/spellerStyle" %>
<%= javascript_include_tag "fckeditor/editor/dialog/fck_spellerpages/spellerpages/wordWindow" %>
<script type="text/javascript" language="javascript">
// <![CDATA[
var suggs = new Array();
var words = new Array();
var textinputs = new Array();
var error;

textinputs[0] = decodeURIComponent('<%= @original_text %>');
words[0] = [];
suggs[0] = [];

<% @words.each_with_index do |pair, i| %>
words[0][<%= i %>] = '<%= pair[0] %>';
suggs[0][<%= i %>] = [<%= pair[1] %>];
<% end %>

var wordWindowObj = new wordWindow();
wordWindowObj.originalSpellings = words;
wordWindowObj.suggestions = suggs;
wordWindowObj.textInputs = textinputs;

function init_spell() {
// check if any error occured during server-side processing
if( error ) {
alert( error );
} else {
// call the init_spell() function in the parent frameset
if (parent.frames.length) {
parent.init_spell( wordWindowObj );
} else {
alert('This page was loaded outside of a frameset. It might not display properly');
}
}
}
</script>
</head>
<body onload="init_spell();">

<script type="text/javascript" language="javascript">
// <![CDATA[
wordWindowObj.writeBody();
// ]]>
</script>

</body>
</html>
16 changes: 15 additions & 1 deletion init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@
ActionView::Base.send(:include, Fckeditor::Helper)

# require the controller
require 'fckeditor_controller'
require 'fckeditor_controller'

# add a route for spellcheck
class ActionController::Routing::RouteSet
alias draw_without_spelling draw
def draw_with_spelling
draw_without_spelling do |map|
map.connect 'fckeditor/check_spelling', :controller => 'fckeditor', :action => 'check_spelling'
map.connect 'fckeditor/command', :controller => 'fckeditor', :action => 'command'
map.connect 'fckeditor/upload', :controller => 'fckeditor', :action => 'upload'
yield map
end
end
alias draw draw_with_spelling
end
18 changes: 8 additions & 10 deletions lib/fckeditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ module Fckeditor

module Version
MAJOR = 0
MINOR = 1
RELEASE = 5
MINOR = 2
RELEASE = 1
end

def self.version
Expand Down Expand Up @@ -40,7 +40,7 @@ def fckeditor_textarea(object, field, options = {})
inputs +
javascript_tag( "FCKeditorAPI = null;\n" +
"__FCKeditorNS = null;\n" +
"var oFCKeditor = new FCKeditor('#{id}', '#{width}', '#{height}', '#{toolbarSet}', '#{value}');\n"+
"var oFCKeditor = new FCKeditor('#{id}', '#{width}', '#{height}', '#{toolbarSet}');\n"+
"oFCKeditor.Config['CustomConfigurationsPath'] = '/javascripts/fckcustom.js';\n"+
"oFCKeditor.ReplaceTextarea();\n")
end
Expand All @@ -58,23 +58,21 @@ def fckeditor_div_id(object, field)
def fckeditor_before_js(object, field)
textarea_id = fckeditor_element_id(object, field)
form_field = "#{object}_#{field}"
"var oEditor = FCKeditorAPI.GetInstance('"+textarea_id+"'); $('"+form_field+"').value = oEditor.GetXHTML();"
end
"var oEditor = FCKeditorAPI.GetInstance('"+textarea_id+"'); $('"+form_field+"').value = oEditor.GetXHTML()"
end
end
end

module ActionView::Helpers::AssetTagHelper
alias_method :rails_javascript_include_tag, :javascript_include_tag

# Adds a new option to Rails' built-in <tt>javascript_include_tag</tt>
# helper - <tt>:unobtrusive</tt>. Works in the same way as <tt>:defaults</tt> - specifying
# <tt>:unobtrusive</tt> will make sure the necessary javascript
# helper - <tt>:fckeditor</tt>. Works in the same way as <tt>:defaults</tt> - specifying
# <tt>:fckeditor</tt> will make sure the necessary javascript
# libraries and behaviours file +script+ tags are loaded. Will happily
# work along side <tt>:defaults</tt>.
#
# <%= javascript_include_tag :defaults, :unobtrusive %>
#
# This replaces the old +unobtrusive_javascript_files+ helper.
# <%= javascript_include_tag :defaults, :fckeditor %>
def javascript_include_tag(*sources)
main_sources, application_source = [], []
if sources.include?(:fckeditor)
Expand Down
40 changes: 40 additions & 0 deletions lib/fckeditor_spell_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Interfaces with aspell and returns results ready for FCKeditor-friendly javascript
class FckeditorSpellCheck

# call aspell and retrieve the results
def self.check_spelling(text)
if RUBY_PLATFORM =~ /mswin/i
aspell_program = 'c:\program files\aspell\bin\aspell' # windows
else
aspell_program = 'aspell' # every other OS on the planet
end

# call aspell
command = "\"#{aspell_program}\" -a --lang=en_US --encoding=utf-8 -H 2>&1"
output = IO.popen(command, 'r+') {|io| io.puts(text); io.close_write; io.read }

# return an array of results
output.split("\n").collect {|line| parse_aspell_line(line) }.compact
end

private

# return a 2-D array of results, where each element in the array has a pair
# of values: [ escaped word, comma separted list of escaped suggestions ]
def self.parse_aspell_line(line)
if line =~ /^&/
details = line.split(' ', 5)
[ escape(details[1]), get_suggestions(details[4]) ]
end
end

# quote and escape the individual suggestions
def self.get_suggestions(value)
value.split(', ').collect {|x| "'#{escape(x)}'"}.join(', ')
end

# properly escape single-quote for javascript
def self.escape(x)
x.gsub(/'/, '\\\\\'')
end
end
1 change: 1 addition & 0 deletions public/javascripts/fckcustom.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ FCKConfig.LinkUploadURL = '/fckeditor/upload';
FCKConfig.ImageUploadURL = '/fckeditor/upload?Type=Image';
FCKConfig.FlashUploadURL = '/fckeditor/upload?Type=Flash';
FCKConfig.AllowQueryStringDebug = false;
FCKConfig.SpellChecker = 'SpellerPages';

// ONLY CHANGE BELOW HERE
FCKConfig.SkinPath = FCKConfig.BasePath + 'skins/silver/';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ function spellChecker( textObject ) {
// this.popUpUrl = '/speller/spellchecker.html'; // by FredCK
this.popUpUrl = 'fck_spellerpages/spellerpages/spellchecker.html'; // by FredCK
this.popUpName = 'spellchecker';
// this.popUpProps = "menu=no,width=440,height=350,top=70,left=120,resizable=yes,status=yes"; // by FredCK
this.popUpProps = "menu=no,width=440,height=350,top=70,left=120,resizable=yes,status=yes"; // by FredCK
this.popUpProps = null ; // by FredCK
// this.spellCheckScript = '/speller/server-scripts/spellchecker.php'; // by FredCK
this.spellCheckScript = 'server-scripts/spellchecker.php'; // by FredCK
//this.spellCheckScript = 'server-scripts/spellchecker.php'; // by FredCK
//this.spellCheckScript = '/cgi-bin/spellchecker.pl';


this.spellCheckScript = '/fckeditor/check_spelling';

// values used to keep track of what happened to a word
this.replWordFlag = "R"; // single replace
this.ignrWordFlag = "I"; // single ignore
this.replAllFlag = "RA"; // replace all occurances
this.ignrAllFlag = "IA"; // ignore all occurances
this.fromReplAll = "~RA"; // an occurance of a "replace all" word
this.fromIgnrAll = "~IA"; // an occurance of a "ignore all" word

// properties set at run time
this.wordFlags = new Array();
this.currentTextIndex = 0;
Expand Down
5 changes: 3 additions & 2 deletions public/javascripts/fckeditor/fckconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ FCKConfig.FontFormats = 'p;div;pre;address;h1;h2;h3;h4;h5;h6' ;
FCKConfig.StylesXmlPath = FCKConfig.EditorPath + 'fckstyles.xml' ;
FCKConfig.TemplatesXmlPath = FCKConfig.EditorPath + 'fcktemplates.xml' ;

FCKConfig.SpellChecker = 'ieSpell' ; // 'ieSpell' | 'SpellerPages'
FCKConfig.IeSpellDownloadUrl = 'http://iespell.huhbw.com/ieSpellSetup220647.exe' ;
//FCKConfig.SpellChecker = 'ieSpell' ; // 'ieSpell' | 'SpellerPages'
FCKConfig.SpellChecker = 'SpellerPages'
//FCKConfig.IeSpellDownloadUrl = 'http://iespell.huhbw.com/ieSpellSetup220647.exe' ;

FCKConfig.MaxUndoLevels = 15 ;

Expand Down

0 comments on commit 3da68c5

Please sign in to comment.