From dd41f41867dc5814301f8e1975ab4eb407db8c59 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Wed, 16 Apr 2014 21:56:24 +0600 Subject: [PATCH] [#369] add codemirror editor --- Gemfile | 2 + Gemfile.lock | 3 + .../projects/git/blobs/_editor.html.haml | 19 +++++ config/environments/production.rb | 4 +- config/environments/staging.rb | 4 +- lib/ext/git/grit.rb | 60 ++------------ .../assets/javascripts/codemirror_editor.js | 78 +++++++++++++++++++ .../assets/stylesheets/codemirror_editor.scss | 2 + 8 files changed, 118 insertions(+), 54 deletions(-) create mode 100644 vendor/assets/javascripts/codemirror_editor.js create mode 100644 vendor/assets/stylesheets/codemirror_editor.scss diff --git a/Gemfile b/Gemfile index 6038611d1..270406aab 100644 --- a/Gemfile +++ b/Gemfile @@ -86,6 +86,8 @@ gem 'therubyracer', '~> 0.12.1', platforms: [:mri, :rbx] gem 'therubyrhino', '~> 1.73.1', platforms: :jruby gem 'sitemap_generator' +gem 'codemirror-rails', '~> 4.0' + group :production do gem "airbrake", '~> 3.1.2' gem 'bluepill', '~> 0.0.60', require: false diff --git a/Gemfile.lock b/Gemfile.lock index ece5418b6..c9f60de51 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -122,6 +122,8 @@ GEM activesupport (>= 3.0) cocaine (0.5.3) climate_control (>= 0.0.3, < 1.0) + codemirror-rails (4.0) + railties (>= 3.0, < 5) coderay (1.1.0) coffee-rails (4.0.1) coffee-script (>= 2.2.0) @@ -523,6 +525,7 @@ DEPENDENCIES capistrano capistrano_colors charlock_holmes (~> 0.6.9) + codemirror-rails (~> 4.0) coffee-rails (~> 4.0.1) compass-rails (~> 1.1.6) creole diff --git a/app/views/projects/git/blobs/_editor.html.haml b/app/views/projects/git/blobs/_editor.html.haml index d8d6bb94a..075f2bd80 100644 --- a/app/views/projects/git/blobs/_editor.html.haml +++ b/app/views/projects/git/blobs/_editor.html.haml @@ -1,3 +1,6 @@ += javascript_include_tag 'codemirror_editor' += stylesheet_link_tag 'codemirror_editor' + %h3= t("layout.projects.files_in_project") .files .l= render 'whereami' @@ -16,3 +19,19 @@ = submit_tag t('layout.save'), title: t('layout.save'), data: {'disable-with' => t('layout.saving')} = t("layout.or") = link_to t("layout.cancel"), blob_path(@project, @treeish, @path), class: 'button' + +:javascript + $(function() { + var editor = CodeMirror.fromTextArea(document.getElementById('code'), { + lineNumbers: true, + mode: '#{@blob.raw_mime_type}' + }); + $(".CodeMirror").resizable({ + stop: function() { editor.refresh(); }, + resize: function() { + $(".CodeMirror-scroll").height($(this).height()); + $(".CodeMirror-scroll").width($(this).width()); + editor.refresh(); + } + }); + }); \ No newline at end of file diff --git a/config/environments/production.rb b/config/environments/production.rb index ce0b0eebc..39258a20b 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -63,7 +63,9 @@ Rosa::Application.configure do config.assets.digest = true # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) - config.assets.precompile += %w(login.css login.js reg_session.css tour.css tour.js gollum/editor/langs/*.js moment/ru.js) + config.assets.precompile += + %w(login.css login.js reg_session.css tour.css tour.js gollum/editor/langs/*.js moment/ru.js + codemirror_editor.js codemirror_editor.css) # Compress JavaScripts and CSS. config.assets.js_compressor = :uglifier diff --git a/config/environments/staging.rb b/config/environments/staging.rb index aee5535d7..730cc8e7b 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -62,7 +62,9 @@ Rosa::Application.configure do config.assets.digest = true # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added) - config.assets.precompile += %w(login.css login.js reg_session.css tour.css tour.js gollum/editor/langs/*.js moment/ru.js) + config.assets.precompile += + %w(login.css login.js reg_session.css tour.css tour.js gollum/editor/langs/*.js moment/ru.js + codemirror_editor.js codemirror_editor.css) # Compress JavaScripts and CSS. config.assets.js_compressor = :uglifier diff --git a/lib/ext/git/grit.rb b/lib/ext/git/grit.rb index ca23f9aa0..e484ebeb5 100644 --- a/lib/ext/git/grit.rb +++ b/lib/ext/git/grit.rb @@ -43,61 +43,17 @@ module Grit end end - # def file_mime_type - # @file_mime_type ||= data.file_type(:mime_type) - # end - # - # def text? - # file_mime_type =~ /^text\// # not binary? - # end - # - # def binary? - # not text? # file_mime_type !~ /^text\// - # # s = data.split(//); ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30 # works only for latin chars - # end - # - # def image? - # mime_type.match(/image/) - # end - - DEFAULT_RAW_MIME_TYPE = MIME::Types[DEFAULT_MIME_TYPE].first - - def mime_type_with_class_store - set_associated_mimes - @associated_mimes.first.simplified - end - alias_method_chain :mime_type, :class_store - attr_accessor :raw_mime_type def raw_mime_type - set_associated_mimes - @raw_mime_type = @associated_mimes.first || DEFAULT_RAW_MIME_TYPE - @raw_mime_type - end - - def raw_mime_types - set_associated_mimes - end - - protected - - # store all associated MIME::Types inside class - def set_associated_mimes - @associated_mimes ||= [] - if @associated_mimes.empty? - guesses = MIME::Types.type_for(self.name) rescue [DEFAULT_RAW_MIME_TYPE] - guesses = [DEFAULT_RAW_MIME_TYPE] if guesses.empty? - - @associated_mimes = guesses.sort{|a,b| mime_sort(a, b)} + return @raw_mime_type if @raw_mime_type.present? + if mime_type == 'text/rpm-spec' + @raw_mime_type = 'text/x-rpm-spec' + else + @raw_mime_type = Linguist::Language.detect(name, data).try(:lexer).try(:mimetypes).try(:first) + @raw_mime_type ||= DEFAULT_MIME_TYPE + @raw_mime_type.gsub!('application', 'text') + @raw_mime_type end - @associated_mimes - end - - # TODO make more clever function - def mime_sort(a,b) - return 0 if a.media_type == b.media_type and a.registered? == b.registered? - return -1 if a.media_type == 'text' and !a.registered? - return 1 end end diff --git a/vendor/assets/javascripts/codemirror_editor.js b/vendor/assets/javascripts/codemirror_editor.js new file mode 100644 index 000000000..e06c3f412 --- /dev/null +++ b/vendor/assets/javascripts/codemirror_editor.js @@ -0,0 +1,78 @@ +//= require codemirror + +//= require codemirror/modes/apl +//= require codemirror/modes/asterisk +//= require codemirror/modes/clike +//= require codemirror/modes/clojure +//= require codemirror/modes/cobol +//= require codemirror/modes/coffeescript +//= require codemirror/modes/commonlisp +//= require codemirror/modes/css +//= require codemirror/modes/d +//= require codemirror/modes/diff +//= require codemirror/modes/dtd +//= require codemirror/modes/ecl +//= require codemirror/modes/eiffel +//= require codemirror/modes/erlang +//= require codemirror/modes/fortran +//= require codemirror/modes/gas +//= require codemirror/modes/gfm +//= require codemirror/modes/gherkin +//= require codemirror/modes/go +//= require codemirror/modes/groovy +//= require codemirror/modes/haml +//= require codemirror/modes/haskell +//= require codemirror/modes/haxe +//= require codemirror/modes/htmlembedded +//= require codemirror/modes/htmlmixed +//= require codemirror/modes/http +//= require codemirror/modes/jade +//= require codemirror/modes/javascript +//= require codemirror/modes/jinja2 +//= require codemirror/modes/julia +//= require codemirror/modes/livescript +//= require codemirror/modes/lua +//= require codemirror/modes/markdown +//= require codemirror/modes/mirc +//= require codemirror/modes/mllike +//= require codemirror/modes/nginx +//= require codemirror/modes/ntriples +//= require codemirror/modes/octave +//= require codemirror/modes/pascal +//= require codemirror/modes/pegjs +//= require codemirror/modes/perl +//= require codemirror/modes/php +//= require codemirror/modes/pig +//= require codemirror/modes/properties +//= require codemirror/modes/puppet +//= require codemirror/modes/python +//= require codemirror/modes/q +//= require codemirror/modes/r +//= require codemirror/modes/rpm +//= require codemirror/modes/rst +//= require codemirror/modes/ruby +//= require codemirror/modes/rust +//= require codemirror/modes/sass +//= require codemirror/modes/scheme +//= require codemirror/modes/shell +//= require codemirror/modes/sieve +//= require codemirror/modes/smalltalk +//= require codemirror/modes/smarty +//= require codemirror/modes/smartymixed +//= require codemirror/modes/solr +//= require codemirror/modes/sparql +//= require codemirror/modes/sql +//= require codemirror/modes/stex +//= require codemirror/modes/tcl +//= require codemirror/modes/tiddlywiki +//= require codemirror/modes/tiki +//= require codemirror/modes/toml +//= require codemirror/modes/turtle +//= require codemirror/modes/vb +//= require codemirror/modes/vbscript +//= require codemirror/modes/velocity +//= require codemirror/modes/verilog +//= require codemirror/modes/xml +//= require codemirror/modes/xquery +//= require codemirror/modes/yaml +//= require codemirror/modes/z80 diff --git a/vendor/assets/stylesheets/codemirror_editor.scss b/vendor/assets/stylesheets/codemirror_editor.scss new file mode 100644 index 000000000..6af424b6a --- /dev/null +++ b/vendor/assets/stylesheets/codemirror_editor.scss @@ -0,0 +1,2 @@ +@import "codemirror"; +//@import "codemirror/themes/rubyblue";