diff --git a/app/assets/javascripts/app.js b/app/assets/javascripts/app.js index 72f747c..9de75ee 100644 --- a/app/assets/javascripts/app.js +++ b/app/assets/javascripts/app.js @@ -40,6 +40,7 @@ $(function(){ $(this).parent().attr("lang", "(language unknown)"); } }); + updateTimestamps(); setInterval(updateTimestamps, 1000*10); }); diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index ab60f77..89cdb16 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -12,5 +12,6 @@ //= require jquery //= require jquery_ujs //= require app +//= require editor //= require ago -//= require highlight +//= require highlight \ No newline at end of file diff --git a/app/assets/javascripts/editor.js b/app/assets/javascripts/editor.js new file mode 100644 index 0000000..f82b7f7 --- /dev/null +++ b/app/assets/javascripts/editor.js @@ -0,0 +1,55 @@ +$(function() { + + $('.md_editor .field_container .preview-button').click(function() { + if ($(this).data('preview') == 'true') { + edit($(this)); + } else { + preview($(this)); + } + }); + + function edit(target) { + target.data('preview', 'false'); + target.text('Preview'); + target.parent().find('.preview').hide(); + target.parent().find('.editor_field').show(); + } + + function preview(target) { + target.data('preview', 'true'); + target.text('Edit'); + var prev = target.parent().find('.preview'); + var editor = target.parent().find('.editor_field') + prev.html("(Loading ...)"); + prev.show(); + editor.hide() + if (target.parent().parent().hasClass('mini')) { + var url = '/tools/render_mini_markdown'; + } else { + var url = '/tools/render_markdown'; + } + $.ajax(url, { + type: 'post', + data: {text: editor.val()}, + dataType: 'html', + headers: { + 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') + }, + success: function(data) { + prev.html(data); + prev.find('pre code').each(function(i, e) { + if ($(this).attr("class")) { + $(this).parent().attr("lang", $(this).attr("class").replace("hljs", "").trim()); + } else { + $(this).parent().attr("lang", "(language unknown)"); + } + hljs.highlightBlock(e); + }); + }, + error: function(xhr, status, err) { + prev.html('(Error: ' + status + ')
' + err); + } + }); + } + +}); \ No newline at end of file diff --git a/app/assets/stylesheets/style.css.scss b/app/assets/stylesheets/style.css.scss index a76c73e..0d8d20f 100644 --- a/app/assets/stylesheets/style.css.scss +++ b/app/assets/stylesheets/style.css.scss @@ -216,7 +216,7 @@ span.no-about { display: block; } -.post, .thread, .thread-reply { +.post, .thread, .thread-reply, .preview { margin-bottom: 50px; .post-content, .thread-content { @@ -224,11 +224,8 @@ span.no-about { clear: both; word-wrap: break-word; overflow: hidden; - - img { - max-width: 100%; - } } + .post-title, .thread-title { margin-bottom: 10px; word-wrap: break-word; @@ -297,7 +294,9 @@ pre { // code blocks code { - padding: 0; + display: block; + padding: 0.5em; + color: #C5C8C6; //hljs box-shadow: 0 0 16px #222 inset; background: #3f3f3f !important; border: none; @@ -388,6 +387,28 @@ blockquote p { } } +.md_editor { + .field_container { + position: relative; + + .preview-button { + position: absolute; + top: 1em; + left: 1em; + z-index: 10; + } + .editor_field { + padding-top: 4em; + min-height: 5em; + } + + .preview { + display: none; + padding: 4em 1em 1em; + } + } +} + #comments { margin: 50px 0 0 40px; } @@ -457,10 +478,6 @@ textarea { padding: 1em; min-height: 3.5em; resize: vertical; - - &.comment { - height: 75px; - } } tr.spacer { diff --git a/app/controllers/tools_controller.rb b/app/controllers/tools_controller.rb new file mode 100644 index 0000000..ae86f76 --- /dev/null +++ b/app/controllers/tools_controller.rb @@ -0,0 +1,19 @@ +class ToolsController < ApplicationController + + def render_markdown + if current_user + render text: render_md(params[:text]) + else + render text: "Error: You are not logged in!" + end + end + + def render_mini_markdown + if current_user + render text: render_mini_md(params[:text]) + else + render text: "Error: You are not logged in!" + end + end + +end \ No newline at end of file diff --git a/app/models/forumthread.rb b/app/models/forumthread.rb index c6070a7..eb328a5 100644 --- a/app/models/forumthread.rb +++ b/app/models/forumthread.rb @@ -15,16 +15,11 @@ class Forumthread < ActiveRecord::Base end def author - @author ||= if self.user_author.present? - user_author - else - User.first - end + @author ||= (user_author || User.first) end def editor - # can be nil - @editor ||= user_editor + @editor ||= (self.user_editor || User.first) end def edited? diff --git a/app/views/application/_md_editor.html.erb b/app/views/application/_md_editor.html.erb new file mode 100644 index 0000000..0344485 --- /dev/null +++ b/app/views/application/_md_editor.html.erb @@ -0,0 +1,11 @@ +
"> + <%= render partial: 'mdhelp' %> +
+
Preview
+ <% options = (defined?(options) && options || {}) %> + <% options[:class] = "#{options[:class]} editor_field" %> + <% options[:placeholder] ||= "Text" %> + <%= text_area_tag name, content, options %> +
(Loading...)
+
+
\ No newline at end of file diff --git a/app/views/blogposts/edit.html.erb b/app/views/blogposts/edit.html.erb index 3d8b246..6240262 100644 --- a/app/views/blogposts/edit.html.erb +++ b/app/views/blogposts/edit.html.erb @@ -1,8 +1,7 @@

Edit post

<%= form_for @post do |f|%> <%= f.text_field :title %> - <%= render partial: "mdhelp" %> - <%= f.text_area :content%> + <%= render partial: "md_editor", locals: {name: "blogpost[content]", content: @post.content, mini: false} %>

<%= f.submit "Update Post", class: "btn blue left" %>

<% end %>

<%= button_to "Delete post", @post, method: "delete", data: {confirm: "Delete post & comments forever?"}, class: "btn red right" %>

diff --git a/app/views/blogposts/new.html.erb b/app/views/blogposts/new.html.erb index eb3ac1b..2a2266a 100644 --- a/app/views/blogposts/new.html.erb +++ b/app/views/blogposts/new.html.erb @@ -1,8 +1,7 @@

New Post

<%= form_for @post do |f|%> <%= f.text_field :title, placeholder: "Title" %> - <%= render partial: "mdhelp" %> - <%= f.text_area :content, placeholder: "Text" %> + <%= render partial: "md_editor", locals: {name: "blogpost[content]", content: @post.content, mini: false} %>

<%= f.submit "Create Post", class: "btn blue left" %>

<% end %> diff --git a/app/views/comments/_new.html.erb b/app/views/comments/_new.html.erb index e29d5d8..d6dd47b 100644 --- a/app/views/comments/_new.html.erb +++ b/app/views/comments/_new.html.erb @@ -1,8 +1,7 @@ <% if current_user %>

New comment

<%= form_for [@post, @comment] do |f| %> - <%= render partial: "mdhelp" %> - <%= f.text_area :content, placeholder: "Comment", class: "comment" %> + <%= render partial: "md_editor", locals: {name: "comment[content]", content: @comment.content, mini: true} %>

<%= f.submit class: "btn blue" %>

<% end %> <% end %> \ No newline at end of file diff --git a/app/views/comments/edit.html.erb b/app/views/comments/edit.html.erb index 2a02537..61efc2b 100644 --- a/app/views/comments/edit.html.erb +++ b/app/views/comments/edit.html.erb @@ -1,8 +1,7 @@

Edit comment

<%= form_for [@comment.blogpost, @comment] do |f| %> - <%= render partial: "mdhelp" %> - <%= f.text_area :content, placeholder: "Comment" %> + <%= render partial: "md_editor", locals: {name: "comment[content]", content: @comment.content, mini: true} %>

<%= f.submit "Update Comment", class: "btn blue left" %>

<% end %>

<%= button_to "Delete comment", [@comment.blogpost, @comment] , method: "delete", data: {confirm: "Delete comment forever?"}, class: "btn red right" %>

diff --git a/app/views/forumthreads/edit.html.erb b/app/views/forumthreads/edit.html.erb index ee87009..044a8ad 100644 --- a/app/views/forumthreads/edit.html.erb +++ b/app/views/forumthreads/edit.html.erb @@ -24,8 +24,7 @@ <% end %> <%= f.text_field :title, placeholder: "Title" %> - <%= render partial: "mdhelp" %> - <%= f.text_area :content, placeholder: "Text" %> + <%= render partial: "md_editor", locals: {name: "forumthread[content]", content: @thread.content, mini: false} %>

<%= f.submit "Update thread", class: "btn blue left" %>

<% end %> <%= button_to "Delete thread", @thread, :method => "delete", data: {confirm: "Delete thread & comments forever?"}, class: "btn red right" %> diff --git a/app/views/forumthreads/new.html.erb b/app/views/forumthreads/new.html.erb index b461c0e..ae13848 100644 --- a/app/views/forumthreads/new.html.erb +++ b/app/views/forumthreads/new.html.erb @@ -14,8 +14,7 @@ <% end %> <%= f.text_field :title, placeholder: "Title" %> - <%= render partial: "mdhelp" %> - <%= f.text_area :content, placeholder: "Text" %> + <%= render partial: "md_editor", locals: {name: "forumthread[content]", content: @thread.content, mini: false} %> <%= f.hidden_field :forum_id %>

<%= f.submit "Create thread", class: "btn blue left" %>

diff --git a/app/views/info/edit.html.erb b/app/views/info/edit.html.erb index 925a8a1..bb17379 100644 --- a/app/views/info/edit.html.erb +++ b/app/views/info/edit.html.erb @@ -1,8 +1,7 @@

Edit Info

<%= form_for @info do |f|%> <%= f.text_field :title%> - <%= render partial: "mdhelp" %> - <%= f.text_area :content %> + <%= render partial: "md_editor", locals: {name: "info[content]", content: @info.content, mini: false} %>

<%= f.submit "Update Info", class: "btn blue left" %>

<% end %>

<%= button_to "Delete Info", @info, method: "delete", data: {confirm: "Delete Info forever?"}, class: "btn red right" %>

diff --git a/app/views/info/new.html.erb b/app/views/info/new.html.erb index 15b6289..da2c124 100644 --- a/app/views/info/new.html.erb +++ b/app/views/info/new.html.erb @@ -1,8 +1,7 @@

New Info

<%= form_for @info, url: info_index_path do |f|%> <%= f.text_field :title, placeholder: "Title" %> - <%= render partial: "mdhelp" %> - <%= f.text_area :content, placeholder: "Text" %> + <%= render partial: "md_editor", locals: {name: "info[content]", content: @info.content, mini: false} %>

<%= f.submit "Create Info", class: "btn blue left" %>

<% end %> diff --git a/app/views/threadreplies/_new.html.erb b/app/views/threadreplies/_new.html.erb index d74af52..a66f9ff 100644 --- a/app/views/threadreplies/_new.html.erb +++ b/app/views/threadreplies/_new.html.erb @@ -1,4 +1,4 @@ <%= form_for [reply.thread, reply] do |f| %> - <%= f.text_area :content, placeholder: "Text" %> + <%= render partial: "md_editor", locals: {name: "threadreply[content]", content: reply.content, mini: false} %>

<%= f.submit "Reply", class: "btn blue" %>

<% end %> \ No newline at end of file diff --git a/app/views/threadreplies/edit.html.erb b/app/views/threadreplies/edit.html.erb index 943adcf..943083f 100644 --- a/app/views/threadreplies/edit.html.erb +++ b/app/views/threadreplies/edit.html.erb @@ -1,8 +1,7 @@ <%= link_to @reply.thread.forum.group, forumgroup_path(@reply.thread.forum.group) %> → <%= link_to @reply.thread.forum, @reply.thread.forum %> → <%= link_to @reply.thread %> → Edit reply

Edit reply

<%= form_for [@reply.thread, @reply] do |f| %> - <%= render partial: "mdhelp" %> - <%= f.text_area :content, placeholder: "Text" %> + <%= render partial: "md_editor", locals: {name: "threadreply[content]", content: @reply.content, mini: false} %>

<%= f.submit "Reply", class: "btn blue left" %>

<% end %>

<%= button_to "Delete reply", [@reply.thread, @reply], method: "delete", data: {confirm: "Delete reply forever?"}, class: "btn red right" %>

diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index d815509..3784df8 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -59,8 +59,7 @@ About you - <%= render partial: "mdhelp" %> - <%= f.text_area :about, placeholder: "Tell us something about you...", disabled: !can_edit? %> + <%= render partial: "md_editor", locals: {name: "user[about]", content: @user.about, mini: true, options: {disabled: !can_edit?, placeholder: "Tell us something about you ..."}} %> diff --git a/config/routes.rb b/config/routes.rb index 4a743c8..8ad1ba4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -27,6 +27,13 @@ Redstoner::Application.routes.draw do resources :threadreplies, path: 'replies' end + resources :tools do + collection do + post 'render_markdown' + post 'render_mini_markdown' + end + end + # get '/status' => 'status#show' get 'login' => 'sessions#new'