Skip to content

Commit

Permalink
fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin committed Apr 19, 2014
1 parent 06ec20c commit 070c3bb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
12 changes: 8 additions & 4 deletions app/assets/javascripts/rails_embed_editor/application.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ window.load_rails_embed_code_editor = () ->
editormode: container.data('editormode')
highlight: compute_range(container.data('highlight'), container.data('first-line'))
}
setup_editor(container[0], options)
setup_editor(container, options)


setup_editor = (element, options) ->
Expand All @@ -34,7 +34,7 @@ setup_editor = (element, options) ->
editormode: 'readonly'
}
options = $.extend({}, defaults, options);
editor = ace.edit(element)
editor = ace.edit(element[0])
editor.commands.removeCommand('replace')
editor.setTheme("ace/theme/" + options['theme']);
editor.getSession().setMode("ace/mode/" + options['mode']);
Expand Down Expand Up @@ -64,15 +64,19 @@ setup_editor = (element, options) ->
button.text('Save')
editor.setReadOnly(false)
else
last_line = editor.session.getLength() - options['first_line']+1
$.post('/rails_embed_editor/edit', {
content: editor.getValue()
content: editor.getValue() + '\n'
first_line: options['first_line']
last_line: options['last_line']
filename: options['filename']
}).success (data) ->
if data['success'] == true
button.text('Saved!')

console.log('last: ' + options['last_line'])
console.log('new: ' + last_line)
element.attr('data-last-line', last_line)
options['last_line'] = last_line
else
button.text('Error')
console.log('Error saving:' + data['message'])
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/rails_embed_editor/editor_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def edit
if filename.nil? or text.nil?
render :json => {:success => false, :params => params.to_json}
else
puts text.lines.map{|x| "#{x.chomp}\n"}.to_s
p text
manager = RailsEmbedEditor::FileManager.from_options(filename, params)
manager.save_text text
render :json => {:success => true}
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/rails_embed_editor/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module ApplicationHelper
def rails_embed_editor(filename, options ={})
options[:editormode] ||= RailsEmbedEditor::EditorMode::READ_ONLY
manager = RailsEmbedEditor::FileManager.from_options(filename, options)
text = manager.read_text
text = manager.read_text.chomp
first_line = manager.first_line
last_line = manager.last_line
content_tag('div', text, :class => 'rails_embed_code_editor',
Expand Down
9 changes: 7 additions & 2 deletions lib/rails_embed_editor/file_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def self.from_around(filename, line, radius=2)
def read_lines
lines = []
File.open(@filename, 'r') do |f|
lines = f.readlines[@first_line-1...@last_line]
all_lines = f.readlines
@first_line = 1 if @first_line <= 0
@last_line = all_lines.size if @first_line > all_lines.size
lines = all_lines[@first_line-1...@last_line]
end
lines
end
Expand All @@ -35,14 +38,16 @@ def read_text
end

def save_text(text)
save_lines(text.split(/\r?\n/).map { |x| "#{x}\n" })
save_lines(text.lines.map{|x| "#{x.chomp}\n"})
end

def save_lines(lines)
all_lines = []
File.open(@filename, 'r') do |f|
all_lines = f.readlines
end
@first_line = 1 if @first_line <= 0
@last_line = all_lines.size if @first_line > all_lines.size
all_lines = all_lines[0...@first_line-1] + lines + all_lines[@last_line..-1]
File.open(@filename, 'w') do |f|
all_lines.each do |line|
Expand Down

0 comments on commit 070c3bb

Please sign in to comment.