Skip to content
Browse files

super in-place editing

  • Loading branch information...
1 parent 03e2534 commit 29a1086f37be2b219cdc8a2d7c678148fe23eccd @releu releu committed Mar 3, 2013
View
32 app/assets/javascripts/cache.js.coffee
@@ -61,4 +61,34 @@ $ ->
# show controlls
if $comment.data('author') == window.current_user.username || window.current_user.admin
$comment.find('div.comment-controls').removeClass('hidden')
-
+
+ $comments.on 'ajax:success', 'a.comment-control-edit', (e, data) ->
+ $link = $(@)
+ $comment = $(e.delegateTarget)
+ $comment.find('li.comment-control-active').removeClass('hidden')
+ $comment.find('li.comment-control-inactive').addClass('hidden')
+ $body = $comment.find('div.comment-body')
+ $body
+ .prop('contenteditable', true)
+ .html(data.content)
+ .focus()
+ false
+
+ $comments.on 'ajax:before', 'a.comment-control-save', (e, data) ->
+ $link = $(@)
+ $comment = $(e.delegateTarget)
+ content = $comment.find('div.comment-body').text()
+ $link.data 'params', 'content=' + content
+ true
+
+
+ $comments.on 'ajax:success', 'a.comment-control-save', (e, data) ->
+ $link = $(@)
+ $comment = $(e.delegateTarget)
+ $comment.find('li.comment-control-inactive').removeClass('hidden')
+ $comment.find('li.comment-control-active').addClass('hidden')
+ $body = $comment.find('div.comment-body')
+ $body
+ .prop('contenteditable', false)
+ .html(data.html)
+ false
View
24 app/assets/stylesheets/styles.less
@@ -20,6 +20,17 @@
background-image: linear-gradient(@from, @to);
}
+.inputable {
+ padding: 4px;
+ outline: none;
+ margin: 0;
+ border: 1px solid #bbb;
+ &:focus {
+ border: 1px solid #7a7a7a;
+ }
+ .rounded-corners(3px);
+}
+
.button {
position: relative;
@@ -554,6 +565,10 @@ a.secondary {
margin-bottom: 5px;
}
+.comment-body[contenteditable=true] {
+ .inputable;
+}
+
.comment-controls {
position: absolute;
top: 0;
@@ -592,14 +607,7 @@ a.secondary {
textarea {
width: 550px;
- padding: 4px;
- outline: none;
- margin: 0;
- border: 1px solid #bbb;
- &:focus {
- border: 1px solid #7a7a7a;
- }
- .rounded-corners(3px)
+ .inputable;
}
.form-actions {
View
63 app/controllers/posts/comments_controller.rb
@@ -3,60 +3,39 @@ class Posts::CommentsController < ApplicationController
cache_sweeper :comment_sweeper
def create
- @post = find_post(params[:post_id])
- @new_comment = @post.comments.build(params[:comment]) do |comment|
- comment.user = current_user
- end
-
- if @new_comment.save
- @comment = @post.comments.build do |comment|
- comment.user = current_user
- end
- render json: { form: render_form(:build), comment: render_comment }
- else
- render_json_error("We are sorry, but comment couldn't be saved.")
- end
- end
-
- def build
- @post = find_post(params[:post_id])
- @comment = @post.comments.build(params[:comment]) do |comment|
- comment.user = current_user
- end
- render json: { form: render_form(:build) }
- end
-
- def preview
- @post = find_post(params[:post_id])
- @comment = @post.comments.build(params[:comment]) do |comment|
- comment.user = current_user
- end
- render json: { form: render_form(:preview) }
+ # @post = find_post(params[:post_id])
+ # @new_comment = @post.comments.build(params[:comment]) do |comment|
+ # comment.user = current_user
+ # end
+ #
+ # if @new_comment.save
+ # @comment = @post.comments.build do |comment|
+ # comment.user = current_user
+ # end
+ # render json: { form: render_form(:build), comment: render_comment }
+ # else
+ # render_json_error("We are sorry, but comment couldn't be saved.")
+ # end
end
def edit
@comment = Comment.find(params[:id])
authorize! :edit, @comment
- render json: { form: render_form(:edit) }
+ render json: { content: @comment.content }
end
def update
- @new_comment = Comment.find(params[:id])
- authorize! :edit, @new_comment
-
- if @new_comment.update_attributes(params[:comment])
- render json: { comment: render_comment }
- else
- render_json_error("We are sorry, but comment couldn't be updated.")
- end
+ @comment = Comment.find(params[:id])
+ authorize! :edit, @comment
+ @comment.update_attribute(:content, params[:content])
+ render json: { html: Markdown.markdown(@comment.content, flavored: true) }
end
def destroy
- comment = Comment.find(params[:id])
+ @comment = Comment.find(params[:id])
authorize! :destroy, comment
-
- comment.mark_deleted
- render json: { message: 'Comment was deleted.' }
+ @comment.mark_deleted
+ render json: {}
end
protected
View
16 app/views/posts/_comments.html.slim
@@ -19,14 +19,22 @@ section class="comments"
== Markdown.markdown comment.content, flavored: true
div class="comment-controls hidden"
ul
- li
- = link_to '#', class: 'comment-control-edit secondary' do
+ li class="comment-control-inactive"
+ = link_to [:edit, @post, comment], class: 'comment-control-edit secondary', remote: true do
i class="icon-pencil"
span Edit
- li
- = link_to '#', class: 'comment-control-remove secondary' do
+ li class="comment-control-inactive"
+ = link_to [@post, comment], class: 'comment-control-remove secondary' do
i class="icon-trash"
span Remove
+ li class="comment-control-active hidden"
+ = link_to [@post, comment], class: 'comment-control-save secondary', remote: true, method: :put do
+ i class="icon-save"
+ span Save
+ li class="comment-control-active hidden"
+ = link_to [@post, comment], class: 'comment-control-cancel secondary', remote: true do
+ i class="icon-share-alt"
+ span Cancel
div class="clear"
- else
p class="comments-empty" No comments yet.

0 comments on commit 29a1086

Please sign in to comment.
Something went wrong with that request. Please try again.