diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-04-13 11:47:28 -0500 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2017-04-30 11:21:20 -0500 |
commit | 60ebd101d0fa3ddbfab86a676b81efba589f8393 (patch) | |
tree | ec72a89919f726599f8ceefc4d5d4de0539ab7b1 /app | |
parent | c205973763e9fddd291a65d241002a0769f84106 (diff) | |
download | gitlab-ce-60ebd101d0fa3ddbfab86a676b81efba589f8393.tar.gz |
Use blob viewers for snippets
Diffstat (limited to 'app')
-rw-r--r-- | app/assets/javascripts/blob/viewer/index.js | 4 | ||||
-rw-r--r-- | app/assets/javascripts/dispatcher.js | 6 | ||||
-rw-r--r-- | app/assets/javascripts/line_highlighter.js | 6 | ||||
-rw-r--r-- | app/controllers/concerns/renders_blob.rb | 4 | ||||
-rw-r--r-- | app/controllers/projects/blob_controller.rb | 2 | ||||
-rw-r--r-- | app/controllers/projects/snippets_controller.rb | 23 | ||||
-rw-r--r-- | app/controllers/snippets_controller.rb | 13 | ||||
-rw-r--r-- | app/helpers/blob_helper.rb | 14 | ||||
-rw-r--r-- | app/models/snippet.rb | 26 | ||||
-rw-r--r-- | app/models/snippet_blob.rb | 59 | ||||
-rw-r--r-- | app/views/projects/blob/_header.html.haml | 2 | ||||
-rw-r--r-- | app/views/projects/blob/viewers/_markup.html.haml | 3 | ||||
-rw-r--r-- | app/views/projects/snippets/show.html.haml | 2 | ||||
-rw-r--r-- | app/views/search/results/_snippet_blob.html.haml | 2 | ||||
-rw-r--r-- | app/views/shared/snippets/_blob.html.haml | 29 | ||||
-rw-r--r-- | app/views/snippets/show.html.haml | 2 |
16 files changed, 137 insertions, 60 deletions
diff --git a/app/assets/javascripts/blob/viewer/index.js b/app/assets/javascripts/blob/viewer/index.js index 7efa8537298..07d67d49aa5 100644 --- a/app/assets/javascripts/blob/viewer/index.js +++ b/app/assets/javascripts/blob/viewer/index.js @@ -6,7 +6,7 @@ export default class BlobViewer { this.copySourceBtn = document.querySelector('.js-copy-blob-source-btn'); this.simpleViewer = document.querySelector('.blob-viewer[data-type="simple"]'); this.richViewer = document.querySelector('.blob-viewer[data-type="rich"]'); - this.$blobContentHolder = $('#blob-content-holder'); + this.$fileHolder = $('.file-holder'); let initialViewerName = document.querySelector('.blob-viewer:not(.hidden)').getAttribute('data-type'); @@ -82,7 +82,7 @@ export default class BlobViewer { viewer.setAttribute('data-loaded', 'true'); - this.$blobContentHolder.trigger('highlight:line'); + this.$fileHolder.trigger('highlight:line'); this.toggleCopyButtonState(); }); diff --git a/app/assets/javascripts/dispatcher.js b/app/assets/javascripts/dispatcher.js index d3d75c4bf4a..15fe87f21ea 100644 --- a/app/assets/javascripts/dispatcher.js +++ b/app/assets/javascripts/dispatcher.js @@ -356,6 +356,10 @@ const ShortcutsBlob = require('./shortcuts_blob'); case 'users:show': new UserCallout(); break; + case 'snippets:show': + new LineHighlighter(); + new BlobViewer(); + break; } switch (path.first()) { case 'sessions': @@ -434,6 +438,8 @@ const ShortcutsBlob = require('./shortcuts_blob'); shortcut_handler = new ShortcutsNavigation(); if (path[2] === 'show') { new ZenMode(); + new LineHighlighter(); + new BlobViewer(); } break; case 'labels': diff --git a/app/assets/javascripts/line_highlighter.js b/app/assets/javascripts/line_highlighter.js index a6f7bea99f5..3ac6dedf131 100644 --- a/app/assets/javascripts/line_highlighter.js +++ b/app/assets/javascripts/line_highlighter.js @@ -57,9 +57,9 @@ require('vendor/jquery.scrollTo'); } LineHighlighter.prototype.bindEvents = function() { - const $blobContentHolder = $('#blob-content-holder'); - $blobContentHolder.on('click', 'a[data-line-number]', this.clickHandler); - $blobContentHolder.on('highlight:line', this.highlightHash); + const $fileHolder = $('.file-holder'); + $fileHolder.on('click', 'a[data-line-number]', this.clickHandler); + $fileHolder.on('highlight:line', this.highlightHash); }; LineHighlighter.prototype.highlightHash = function() { diff --git a/app/controllers/concerns/renders_blob.rb b/app/controllers/concerns/renders_blob.rb index d478c3bb6ca..9faf68e6d97 100644 --- a/app/controllers/concerns/renders_blob.rb +++ b/app/controllers/concerns/renders_blob.rb @@ -14,4 +14,8 @@ module RendersBlob html: view_to_html_string("projects/blob/_viewer", viewer: viewer, load_asynchronously: false) } end + + def override_max_blob_size(blob) + blob.override_max_size! if params[:override_max_size] == 'true' + end end diff --git a/app/controllers/projects/blob_controller.rb b/app/controllers/projects/blob_controller.rb index be5822b2cd4..9489bbddfc4 100644 --- a/app/controllers/projects/blob_controller.rb +++ b/app/controllers/projects/blob_controller.rb @@ -35,7 +35,7 @@ class Projects::BlobController < Projects::ApplicationController end def show - @blob.override_max_size! if params[:override_max_size] == 'true' + override_max_blob_size(@blob) respond_to do |format| format.html do diff --git a/app/controllers/projects/snippets_controller.rb b/app/controllers/projects/snippets_controller.rb index 5c9e0d4d1a1..66f913f8f9d 100644 --- a/app/controllers/projects/snippets_controller.rb +++ b/app/controllers/projects/snippets_controller.rb @@ -3,6 +3,7 @@ class Projects::SnippetsController < Projects::ApplicationController include ToggleAwardEmoji include SpammableActions include SnippetsActions + include RendersBlob before_action :module_enabled before_action :snippet, only: [:show, :edit, :destroy, :update, :raw, :toggle_award_emoji, :mark_as_spam] @@ -55,11 +56,23 @@ class Projects::SnippetsController < Projects::ApplicationController end def show - @note = @project.notes.new(noteable: @snippet) - @noteable = @snippet - - @discussions = @snippet.discussions - @notes = prepare_notes_for_rendering(@discussions.flat_map(&:notes)) + blob = @snippet.blob + override_max_blob_size(blob) + + respond_to do |format| + format.html do + @note = @project.notes.new(noteable: @snippet) + @noteable = @snippet + + @discussions = @snippet.discussions + @notes = prepare_notes_for_rendering(@discussions.flat_map(&:notes)) + render 'show' + end + + format.json do + render_blob_json(blob) + end + end end def destroy diff --git a/app/controllers/snippets_controller.rb b/app/controllers/snippets_controller.rb index 056910fad67..906833505d1 100644 --- a/app/controllers/snippets_controller.rb +++ b/app/controllers/snippets_controller.rb @@ -3,6 +3,7 @@ class SnippetsController < ApplicationController include SpammableActions include SnippetsActions include MarkdownPreview + include RendersBlob before_action :snippet, only: [:show, :edit, :destroy, :update, :raw, :download] @@ -60,6 +61,18 @@ class SnippetsController < ApplicationController end def show + blob = @snippet.blob + override_max_blob_size(blob) + + respond_to do |format| + format.html do + render 'show' + end + + format.json do + render_blob_json(blob) + end + end end def destroy diff --git a/app/helpers/blob_helper.rb b/app/helpers/blob_helper.rb index cc47654dc06..0b80747f001 100644 --- a/app/helpers/blob_helper.rb +++ b/app/helpers/blob_helper.rb @@ -119,7 +119,15 @@ module BlobHelper end def blob_raw_url - namespace_project_raw_path(@project.namespace, @project, @id) + if @snippet + if @snippet.project_id + raw_namespace_project_snippet_path(@project.namespace, @project, @snippet) + else + raw_snippet_path(@snippet) + end + elsif @blob + namespace_project_raw_path(@project.namespace, @project, @id) + end end # SVGs can contain malicious JavaScript; only include whitelisted @@ -212,8 +220,8 @@ module BlobHelper clipboard_button(target: ".blob-content[data-blob-id='#{blob.id}']", class: "btn btn-sm js-copy-blob-source-btn", title: "Copy source to clipboard") end - def open_raw_file_button(path) - link_to icon('file-code-o'), path, class: 'btn btn-sm has-tooltip', target: '_blank', rel: 'noopener noreferrer', title: 'Open raw', data: { container: 'body' } + def open_raw_blob_button + link_to icon('file-code-o'), blob_raw_url, class: 'btn btn-sm has-tooltip', target: '_blank', rel: 'noopener noreferrer', title: 'Open raw', data: { container: 'body' } end def blob_render_error_reason(viewer) diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 380835707e8..d8860718cb5 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -1,6 +1,5 @@ class Snippet < ActiveRecord::Base include Gitlab::VisibilityLevel - include Linguist::BlobHelper include CacheMarkdownField include Noteable include Participable @@ -87,47 +86,26 @@ class Snippet < ActiveRecord::Base ] end - def data - content + def blob + @blob ||= Blob.decorate(SnippetBlob.new(self), nil) end def hook_attrs attributes end - def size - 0 - end - def file_name super.to_s end - # alias for compatibility with blobs and highlighting - def path - file_name - end - - def name - file_name - end - def sanitized_file_name file_name.gsub(/[^a-zA-Z0-9_\-\.]+/, '') end - def mode - nil - end - def visibility_level_field :visibility_level end - def no_highlighting? - content.lines.count > 1000 - end - def notes_with_associations notes.includes(:author) end diff --git a/app/models/snippet_blob.rb b/app/models/snippet_blob.rb new file mode 100644 index 00000000000..d6cab74eb1a --- /dev/null +++ b/app/models/snippet_blob.rb @@ -0,0 +1,59 @@ +class SnippetBlob + include Linguist::BlobHelper + + attr_reader :snippet + + def initialize(snippet) + @snippet = snippet + end + + delegate :id, to: :snippet + + def name + snippet.file_name + end + + alias_method :path, :name + + def size + data.bytesize + end + + def data + snippet.content + end + + def rendered_markup + return unless Gitlab::MarkupHelper.gitlab_markdown?(name) + + Banzai.render_field(snippet, :content) + end + + def mode + nil + end + + def binary? + false + end + + def load_all_data!(repository) + # No-op + end + + def lfs_pointer? + false + end + + def lfs_oid + nil + end + + def lfs_size + nil + end + + def truncated? + false + end +end diff --git a/app/views/projects/blob/_header.html.haml b/app/views/projects/blob/_header.html.haml index b89cd460455..638f8cef3bd 100644 --- a/app/views/projects/blob/_header.html.haml +++ b/app/views/projects/blob/_header.html.haml @@ -16,7 +16,7 @@ .btn-group{ role: "group" }< = copy_blob_source_button(blob) if !blame && blob.rendered_as_text?(ignore_errors: false) - = open_raw_file_button(namespace_project_raw_path(@project.namespace, @project, @id)) + = open_raw_blob_button = view_on_environment_button(@commit.sha, @path, @environment) if @environment .btn-group{ role: "group" }< diff --git a/app/views/projects/blob/viewers/_markup.html.haml b/app/views/projects/blob/viewers/_markup.html.haml index b9a998d96ff..230305b488d 100644 --- a/app/views/projects/blob/viewers/_markup.html.haml +++ b/app/views/projects/blob/viewers/_markup.html.haml @@ -1,3 +1,4 @@ - blob = viewer.blob +- rendered_markup = blob.rendered_markup if blob.respond_to?(:rendered_markup) .file-content.wiki - = markup(blob.name, blob.data) + = markup(blob.name, blob.data, rendered: rendered_markup) diff --git a/app/views/projects/snippets/show.html.haml b/app/views/projects/snippets/show.html.haml index 7c6be003d4c..7a175f63eeb 100644 --- a/app/views/projects/snippets/show.html.haml +++ b/app/views/projects/snippets/show.html.haml @@ -4,7 +4,7 @@ .project-snippets %article.file-holder.snippet-file-content - = render 'shared/snippets/blob', raw_path: raw_namespace_project_snippet_path(@project.namespace, @project, @snippet) + = render 'shared/snippets/blob' .row-content-block.top-block.content-component-block = render 'award_emoji/awards_block', awardable: @snippet, inline: true diff --git a/app/views/search/results/_snippet_blob.html.haml b/app/views/search/results/_snippet_blob.html.haml index f2fe5742c12..c4a5131c1a7 100644 --- a/app/views/search/results/_snippet_blob.html.haml +++ b/app/views/search/results/_snippet_blob.html.haml @@ -39,7 +39,7 @@ .blob-content - snippet_chunks.each do |chunk| - unless chunk[:data].empty? - = highlight(snippet.file_name, chunk[:data], repository: nil, plain: snippet.no_highlighting?) + = highlight(snippet.file_name, chunk[:data], repository: nil, plain: snippet.blob.no_highlighting?) - else .file-content.code .nothing-here-block Empty file diff --git a/app/views/shared/snippets/_blob.html.haml b/app/views/shared/snippets/_blob.html.haml index 37c66ff2595..0c481120969 100644 --- a/app/views/shared/snippets/_blob.html.haml +++ b/app/views/shared/snippets/_blob.html.haml @@ -1,29 +1,24 @@ +- blob = @snippet.blob .js-file-title.file-title-flex-parent .file-header-content - = blob_icon @snippet.mode, @snippet.path + = blob_icon blob.mode, blob.path %strong.file-title-name - = @snippet.path + = blob.path - = copy_file_path_button(@snippet.path) + = copy_file_path_button(blob.path) + + %small + = number_to_human_size(blob.raw_size) .file-actions.hidden-xs + = render 'projects/blob/viewer_switcher', blob: blob + .btn-group{ role: "group" }< - = copy_blob_source_button(@snippet) - = open_raw_file_button(raw_path) + = copy_blob_source_button(blob) + = open_raw_blob_button - if defined?(download_path) && download_path = link_to icon('download'), download_path, class: "btn btn-sm has-tooltip", title: 'Download', data: { container: 'body' } -- if @snippet.content.empty? - .file-content.code - .nothing-here-block Empty file -- else - - if markup?(@snippet.file_name) - .file-content.wiki - - if gitlab_markdown?(@snippet.file_name) - = preserve(markdown_field(@snippet, :content)) - - else - = markup(@snippet.file_name, @snippet.content) - - else - = render 'shared/file_highlight', blob: @snippet += render 'projects/blob/content', blob: blob diff --git a/app/views/snippets/show.html.haml b/app/views/snippets/show.html.haml index e5711ca79c7..8a80013bbfd 100644 --- a/app/views/snippets/show.html.haml +++ b/app/views/snippets/show.html.haml @@ -3,7 +3,7 @@ = render 'shared/snippets/header' %article.file-holder.snippet-file-content - = render 'shared/snippets/blob', raw_path: raw_snippet_path(@snippet), download_path: download_snippet_path(@snippet) + = render 'shared/snippets/blob', download_path: download_snippet_path(@snippet) .row-content-block.top-block.content-component-block = render 'award_emoji/awards_block', awardable: @snippet, inline: true |