Merge pull request #116 from medialab-prado/106-show-annotation-context

Show annotation context in annotation page
This commit is contained in:
Fernando Blat
2017-02-13 11:42:41 +01:00
committed by GitHub
9 changed files with 130 additions and 44 deletions

View File

@@ -14,7 +14,7 @@ class Legislation::Annotation < ActiveRecord::Base
validates :draft_version, presence: true
validates :author, presence: true
before_save :store_range
before_save :store_range, :store_context
after_create :create_first_comment
def store_range
@@ -24,6 +24,26 @@ class Legislation::Annotation < ActiveRecord::Base
self.range_end_offset = ranges.first["endOffset"]
end
def store_context
begin
html = draft_version.body_html
doc = Nokogiri::HTML(html)
selector_start = "/html/body/#{range_start}"
el_start = doc.at_xpath(selector_start)
selector_end = "/html/body/#{range_end}"
el_end = doc.at_xpath(selector_end)
remainder_el_start = el_start.text[0 .. range_start_offset-1] unless range_start_offset.zero?
remainder_el_end = el_end.text[range_end_offset .. -1]
self.context = "#{remainder_el_start}<span class=annotator-hl>#{quote}</span>#{remainder_el_end}"
rescue
"<span class=annotator-hl>#{quote}</span>"
end
end
def create_first_comment
comments.create(body: self.text, user: self.author)
end

View File

@@ -20,7 +20,7 @@
<% end %>
</span>
<div class="comment-section">
<%= annotation.quote %>
<%= annotation.context.try(:html_safe).presence || annotation.quote %>
</div>
<%= link_to legislation_process_draft_version_annotation_path(@process, @draft_version, annotation) do %>
<span class="icon-comments" aria-hidden="true"></span> <span><%= t('.comments_count', count: annotation.comments_count) %></span></a>

View File

@@ -15,7 +15,7 @@
<div class="comment-section">
<div class="row">
<div class="small-12 medium-10 column legislation-comment">
<%= @annotation.quote %>
<%= @annotation.context.try(:html_safe).presence || @annotation.quote %>
</div>
<div class="small-12 medium-2 column legislation-comment">
<span class="pull-right">