Commit aaafd230 authored by evazion's avatar evazion Committed by GitHub

Merge pull request #4291 from BrokenEagle/rework-version-views

Rework version views
parents 01e42b70 8ff00cfc
require 'dtext'
module ApplicationHelper
def listing_type(*fields, member_check: true, types: [:revert, :standard])
(fields.reduce(false) { |acc, field| acc || params.dig(:search, field).present? } && (!member_check || CurrentUser.is_member?) ? types[0] : types[1])
end
def diff_list_html(new, old, latest, ul_class: ["diff-list"], li_class: [])
diff = SetDiff.new(new, old, latest)
render "diff_list", diff: diff, ul_class: ul_class, li_class: li_class
end
def diff_body_html(record, previous, field)
return h(record[field]).gsub(/\r?\n/, '<span class="paragraph-mark">¶</span><br>').html_safe if previous.blank?
pattern = Regexp.new('(?:<.+?>)|(?:\w+)|(?:[ \t]+)|(?:\r?\n)|(?:.+?)')
DiffBuilder.new(record[field], previous[field], pattern).build
end
def status_diff_html(record)
previous = record.previous
return "New" if previous.blank?
statuses = []
record.class.status_fields.each do |field, status|
if record.has_attribute?(field)
statuses += [status] if record[field] != previous[field]
else
statuses += [status] if record.send(field)
end
end
statuses.join("<br>").html_safe
end
def wordbreakify(string)
lines = string.scan(/.{1,10}/)
wordbreaked_string = lines.map {|str| h(str)}.join("<wbr>")
......
module ArtistCommentaryVersionsHelper
def artist_commentary_versions_listing_type
params.dig(:search, :post_id).present? ? :revert : :standard
end
end
module ArtistVersionsHelper
def artist_versions_listing_type
(params.dig(:search, :artist_id).present? && CurrentUser.is_member?) ? :revert : :standard
end
def artist_version_other_names_diff(artist_version)
new_names = artist_version.other_names
old_names = artist_version.previous.try(:other_names)
......
module NoteVersionsHelper
def note_versions_listing_type
((params.dig(:search, :post_id).present? || params.dig(:search, :note_id).present?) && CurrentUser.is_member?) ? :revert : :standard
end
def note_version_body_diff_info(note_version)
def note_version_position_diff(note_version)
previous = note_version.previous
if previous.nil?
return ""
end
html = ""
if note_version.body == previous.body
html += '<span class="inactive">(body not changed)</span>'
html = "#{note_version.x},#{note_version.y}"
if previous.nil?
html
else
"#{previous.x},#{previous.y} -> " + html
end
html.html_safe
end
def note_version_position_diff(note_version)
def note_version_size_diff(note_version)
previous = note_version.previous
html = "#{note_version.width}x#{note_version.height}"
html += " #{note_version.x},#{note_version.y}"
if previous.nil?
html
elsif note_version.x == previous.x && note_version.y == previous.y && note_version.width == previous.width && note_version.height == previous.height
html
else
html = '<span style="text-decoration: underline;">' + html + '</span>'
html.html_safe
"#{previous.width}x#{previous.height} -> " + html
end
end
end
module PoolVersionsHelper
def pool_versions_listing_type
params.dig(:search, :pool_id).present? ? :revert : :standard
end
def pool_version_status_diff(pool_version)
cur = pool_version
prev = pool_version.previous
return "New" if prev.blank?
status = []
status += ["Renamed"] if cur.name != prev.name
status += ["DescChanged"] if cur.description != prev.description
status += ["Deleted"] if cur.is_deleted? && !prev.is_deleted?
status += ["Undeleted"] if !cur.is_deleted? && prev.is_deleted?
status += ["Activated"] if cur.is_active? && !prev.is_active?
status += ["Deactivated"] if !cur.is_active? && prev.is_active?
status.join(" ")
end
def pool_page_diff(pool_version, other_version)
pattern = Regexp.new('(?:<.+?>)|(?:\w+)|(?:[ \t]+)|(?:\r?\n)|(?:.+?)')
DiffBuilder.new(pool_version.description, other_version.description, pattern).build
end
end
module PostVersionsHelper
def post_versions_listing_type
params.dig(:search, :post_id).present? ? :revert : :standard
end
def post_version_diff(post_version)
diff = post_version.diff(post_version.previous)
html = '<span class="diff-list">'
......
module WikiPageVersionsHelper
def wiki_page_versions_listing_type
params.dig(:search, :wiki_page_id).present? ? :page : :global
end
def wiki_page_version_status_diff(wiki_page_version)
cur = wiki_page_version
prev = wiki_page_version.previous
return "New" if prev.blank?
status = []
status += ["Renamed"] if cur.title != prev.title
status += ["Deleted"] if cur.is_deleted? && !prev.is_deleted?
status += ["Undeleted"] if !cur.is_deleted? && prev.is_deleted?
status.join(" ")
end
def wiki_other_names_diff(new_version, old_version)
new_names = new_version.other_names
old_names = old_version.other_names
......@@ -23,9 +6,4 @@ module WikiPageVersionsHelper
diff_list_html(new_names, old_names, latest_names, ul_class: ["wiki-other-names-diff-list list-inline"], li_class: ["wiki-other-name"])
end
def wiki_body_diff(thispage, otherpage)
pattern = Regexp.new('(?:<.+?>)|(?:\w+)|(?:[ \t]+)|(?:\r?\n)|(?:.+?)')
DiffBuilder.new(thispage.body, otherpage.body, pattern).build
end
end
......@@ -39,3 +39,19 @@
color: var(--diff-list-obsolete-removed-color);
}
}
.diff-body {
del {
background: var(--wiki-page-versions-diff-del-background);
text-decoration: none;
}
ins {
background: var(--wiki-page-versions-diff-ins-background);
text-decoration: none;
}
span.paragraph-mark {
opacity: 0.25;
}
}
body.a-index {
div#p-revert-listing {
display: flex;
table.striped {
flex: 1;
}
}
div#p-revert-listing > article.post-preview {
margin-top: 2em;
}
}
div#c-artist-commentary-versions {
#a-index {
div.commentary-body-section {
padding: 0.5em;
margin-bottom: 0.5em;
border: var(--footer-border);
}
td.original-column,
td.translated-column {
padding-top: 0.5em;
vertical-align: top;
}
}
}
div#c-pool-versions {
#a-diff {
del {
background: var(--wiki-page-versions-diff-del-background);
text-decoration: none;
}
ins {
background: var(--wiki-page-versions-diff-ins-background);
text-decoration: none;
}
span.paragraph-mark {
opacity: 0.25;
}
}
}
\ No newline at end of file
......@@ -10,9 +10,4 @@ body.c-post-versions.a-index {
.advanced-search-link {
margin: 0 1em;
}
#p-revert-listing {
display: flex;
table#post-versions-table { flex: 1; }
}
}
div#c-wiki-page-versions {
#a-diff {
del, .wiki-other-names-diff-list .removed {
ul.wiki-other-names-diff-list li.removed {
background: var(--wiki-page-versions-diff-del-background);
text-decoration: none;
}
ins, .wiki-other-names-diff-list .added {
ul.wiki-other-names-diff-list li.added {
background: var(--wiki-page-versions-diff-ins-background);
text-decoration: none;
}
.wiki-other-names-diff-list .obsolete {
ul.wiki-other-names-diff-list li.obsolete {
text-decoration: dotted underline;
}
span.paragraph-mark {
opacity: 0.25;
}
}
#a-index {
......
......@@ -20,6 +20,14 @@ class DiffBuilder
output.each { |q| q.replace(escape_html[q]) }
diffs.reverse_each do |hunk|
old_cr = hunk[0].try(:old_element)
new_cr = hunk[1].try(:new_element)
if old_cr && new_cr && old_cr.match?(/^\r?\n$/) && new_cr.match?(/^\r?\n$/)
hunk_position = hunk[0].old_position
output[hunk_position] = '<del><span class="paragraph-mark">¶</span></del><ins><span class="paragraph-mark">¶</span></ins><br>'
next
end
newchange = hunk.max {|a, b| a.old_position <=> b.old_position}
newstart = newchange.old_position
oldstart = hunk.min {|a, b| a.old_position <=> b.old_position}.old_position
......
......@@ -7,4 +7,24 @@ class ArtistCommentaryVersion < ApplicationRecord
q = q.search_attributes(params, :post, :updater, :original_title, :original_description, :translated_title, :translated_description)
q.apply_default_order(params)
end
def previous
@previous ||= begin
ArtistCommentaryVersion.where("post_id = ? and updated_at < ?", post_id, updated_at).order("updated_at desc").limit(1).to_a
end
@previous.first
end
def self.status_fields
{
original_title: "OrigTitle",
original_description: "OrigDesc",
translated_title: "TransTitle",
translated_description: "TransDesc",
}
end
def unchanged_empty?(field)
self[field].strip.empty? && (previous.nil? || previous[field].strip.empty?)
end
end
......@@ -24,6 +24,46 @@ class ArtistVersion < ApplicationRecord
extend SearchMethods
def previous
ArtistVersion.where("artist_id = ? and created_at < ?", artist_id, created_at).order("created_at desc").first
@previous ||= begin
ArtistVersion.where("artist_id = ? and created_at < ?", artist_id, created_at).order("created_at desc").limit(1).to_a
end
@previous.first
end
def self.status_fields
{
name: "Renamed",
urls_changed: "URLs",
other_names_changed: "OtherNames",
group_name: "GroupName",
was_deleted: "Deleted",
was_undeleted: "Undeleted",
was_banned: "Banned",
was_unbanned: "Unbanned",
}
end
def other_names_changed
((other_names - previous.other_names) | (previous.other_names - other_names)).length > 0
end
def urls_changed
((urls - previous.urls) | (previous.urls - urls)).length > 0
end
def was_deleted
!is_active && previous.is_active
end
def was_undeleted
is_active && !previous.is_active
end
def was_banned
is_banned && !previous.is_banned
end
def was_unbanned
!is_banned && previous.is_banned
end
end
......@@ -13,6 +13,35 @@ class NoteVersion < ApplicationRecord
end
def previous
NoteVersion.where("note_id = ? and updated_at < ?", note_id, updated_at).order("updated_at desc").first
@previous ||= begin
NoteVersion.where("note_id = ? and updated_at < ?", note_id, updated_at).order("updated_at desc").limit(1).to_a
end
@previous.first
end
def self.status_fields
{
body: "Body",
was_moved: "Moved",
was_resized: "Resized",
was_deleted: "Deleted",
was_undeleted: "Undeleted",
}
end
def was_moved
x != previous.x || y != previous.y
end
def was_resized
width != previous.width || height != previous.height
end
def was_deleted
!is_active && previous.is_active
end
def was_undeleted
is_active && !previous.is_active
end
end
......@@ -108,7 +108,41 @@ class PoolArchive < ApplicationRecord
end
def previous
PoolArchive.where("pool_id = ? and version < ?", pool_id, version).order("version desc").first
@previous ||= begin
PoolArchive.where("pool_id = ? and version < ?", pool_id, version).order("version desc").limit(1).to_a
end
@previous.first
end
def self.status_fields
{
name: "Renamed",
description: "Description",
was_deleted: "Deleted",
was_undeleted: "Undeleted",
was_activated: "Activated",
was_deactivated: "Deactivated",
}
end
def was_deleted
is_deleted && !previous.is_deleted
end
def was_undeleted
!is_deleted && previous.is_deleted
end
def was_activated
is_active && !previous.is_active
end
def was_deactivated
!is_active && previous.is_active
end
def text_field_changed
previous.present? && (name_changed || description_changed)
end
def pretty_name
......
......@@ -92,19 +92,31 @@ class PostArchive < ApplicationRecord
end
def previous
# HACK: if all the post versions for this post have already been preloaded,
# we can use that to avoid a SQL query.
if association(:post).loaded? && post && post.association(:versions).loaded?
post.versions.sort_by(&:version).reverse.find { |v| v.version < version }
else
PostArchive.where("post_id = ? and version < ?", post_id, version).order("version desc").first
@previous ||= begin
# HACK: if all the post versions for this post have already been preloaded,
# we can use that to avoid a SQL query.
if association(:post).loaded? && post && post.association(:versions).loaded?
ver = [post.versions.sort_by(&:version).reverse.find { |v| v.version < version }]
else
ver = PostArchive.where("post_id = ? and version < ?", post_id, version).order("version desc").limit(1).to_a
end
end
@previous.first
end
def visible?
post&.visible?
end
def self.status_fields
{
tags: "Tags",
rating: "Rating",
parent_id: "Parent",
source: "Source",
}
end
def diff(version = nil)
if post.nil?
latest_tags = tag_array
......
......@@ -23,7 +23,32 @@ class WikiPageVersion < ApplicationRecord
end
def previous
WikiPageVersion.where("wiki_page_id = ? and id < ?", wiki_page_id, id).order("id desc").first
@previous ||= begin
WikiPageVersion.where("wiki_page_id = ? and id < ?", wiki_page_id, id).order("id desc").limit(1).to_a
end
@previous.first
end
def self.status_fields
{
body: "Body",
other_names_changed: "OtherNames",
title: "Renamed",
was_deleted: "Deleted",
was_undeleted: "Undeleted",
}
end
def other_names_changed
((other_names - previous.other_names) | (previous.other_names - other_names)).length > 0
end
def was_deleted
is_deleted && !previous.is_deleted
end
def was_undeleted
!is_deleted && previous.is_deleted
end
def category_name
......
<div id="p-<%= artist_commentary_versions_listing_type %>-listing">
<div id="p-<%= listing_type(:post_id) %>-listing">
<%= table_for @commentary_versions, {class: "striped autofit", width: "100%"} do |t| %>
<% t.column "Post", width: "5%" do |commentary_version| %>
<% if artist_commentary_versions_listing_type == :revert %>
<%= link_to commentary_version.post_id, post_path(commentary_version.post_id) %>
<% else %>
<%= PostPresenter.preview(commentary_version.post, :tags => "status:any") %>
<% if listing_type(:post_id) == :revert %>
<%= PostPresenter.preview(@commentary_versions.first.post, show_deleted: true) %>
<% end %>
<%= table_for @commentary_versions, {class: "striped", width: "100%"} do |t| %>
<% if listing_type(:post_id) == :standard %>
<% t.column "Post", width: "1%" do |commentary_version| %>
<%= PostPresenter.preview(commentary_version.post, :tags => "status:any") %>
<% end %>
<% end %>
<% if listing_type(:post_id) == :standard %>
<% t.column "Version", width: "3%" do |commentary_version| %>
<%= link_to "#{commentary_version.post_id}.#{commentary_version.id}»", artist_commentary_versions_path(search: {post_id: commentary_version.post_id}, anchor: "artist-commentary-version-#{commentary_version.id}") %>
<% end %>
<% end %>
<% if artist_commentary_versions_listing_type == :standard %>
<% t.column "Version" do |commentary_version| %>
<%= link_to "#{commentary_version.post_id}.#{commentary_version.id}»", artist_commentary_versions_path(search: {post_id: commentary_version.post_id}) %>
<% t.column "Original", width: "40%", td: {class: "diff-body"} do |commentary_version| %>
<% if !commentary_version.unchanged_empty?(:original_title) %>
<b>Title:</b>
<div class="commentary-body-section">
<%= diff_body_html(commentary_version, commentary_version.previous, :original_title) %>
</div>
<% end %>
<% if !commentary_version.unchanged_empty?(:original_description) %>
<b>Description:</b>
<div class="commentary-body-section">
<%= diff_body_html(commentary_version, commentary_version.previous, :original_description) %>
</div>
<% end %>
<% end %>
<% t.column "Original" do |commentary_version| %>
<%= format_commentary_title(commentary_version.original_title) %>
<%= format_commentary_description(commentary_version.original_description) %>
<% t.column "Translated", width: "40%", td: {class: "diff-body"} do |commentary_version| %>
<% if !commentary_version.unchanged_empty?(:translated_title) %>
<b>Title:</b>
<div class="commentary-body-section">
<%= diff_body_html(commentary_version, commentary_version.previous, :translated_title) %>
</div>
<% end %>
<% if !commentary_version.unchanged_empty?(:translated_description) %>
<b>Description:</b>
<div class="commentary-body-section">
<%= diff_body_html(commentary_version, commentary_version.previous, :translated_description) %>
</div>
<% end %>
<% end %>
<% t.column "Translated" do |commentary_version| %>
<%= format_commentary_title(commentary_version.translated_title) %>
<%= format_commentary_description(commentary_version.translated_description) %>
<% t.column "Changes", width: "3%" do |commentary_version| %>
<%= status_diff_html(commentary_version) %>
<% end %>
<% t.column "Edited by", width: "10%" do |commentary_version| %>
<% t.column "Updated", width: "10%" do |commentary_version| %>
<div>
<%= compact_time commentary_version.updated_at %>
</div>
by
<%= link_to_user commentary_version.updater %>
<%= link_to "»", artist_commentary_versions_path(search: params[:search].merge({ updater_id: commentary_version.updater_id })) %>
<% end %>
<% t.column "Date", width: "10%" do |commentary_version| %>
<%= compact_time commentary_version.updated_at %>
<% end %>
<% if artist_commentary_versions_listing_type == :revert %>
<% if listing_type(:post_id) == :revert %>
<% t.column column: "control", width: "7%" do |commentary_version| %>
<%= link_to "Revert to", revert_artist_commentary_path(commentary_version.post_id, :version_id => commentary_version.id), :remote => true, :method => :put, :data => {:confirm => "Are you sure you want to revert to this version?"} %>
<% end %>
......
<div id="p-<%= artist_versions_listing_type %>-listing">
<div id="p-<%= listing_type(:artist_id) %>-listing">
<%= table_for @artist_versions, {class: "striped autofit", width: "100%"} do |t| %>
<% t.column "Name" do |artist_version| %>
<%= link_to artist_version.name, artist_path(artist_version.artist_id) %>
<%= link_to "»", artist_versions_path(search: {artist_id: artist_version.artist_id}) %>
<% if !artist_version.is_active? %>
(deleted)
<% end %>
<% if artist_version.group_name.present? %>
<p>(group: <%= artist_version.group_name %>)</p>
<% end %>
<%= link_to "»", artist_versions_path(search: {artist_id: artist_version.artist_id}, anchor: "artist-version-#{artist_version.id}") %>
<% end %>
<% t.column "Other Names" do |artist_version| %>
<% if artist_version.group_name.present? %>
<p><b>Group:</b><br>&ensp;<%= artist_version.group_name %></p>
<% end %>
<%= artist_version_other_names_diff(artist_version) %>
<% end %>
<% t.column "URLs", td: {class: "col-expand"} do |artist_version| %>
<%= artist_version_urls_diff(artist_version) %>
<% end %>
<% t.column "Changes" do |artist_version| %>
<%= status_diff_html(artist_version) %>
<% end %>
<% t.column "Updated" do |artist_version| %>
<%= link_to_user artist_version.updater %>
<%= link_to "»", artist_versions_path(search: { updater_name: artist_version.updater.name }) %>
......@@ -26,7 +24,7 @@
<%= compact_time(artist_version.updated_at) %>
</p>
<% end %>
<% if artist_versions_listing_type == :revert %>
<% if listing_type(:artist_id) == :revert %>
<% t.column column: "control" do |artist_version| %>
<%= link_to "Revert to", revert_artist_path(artist_version.artist_id, version_id: artist_version.id), method: :put, "data-confirm": "Are you sure you want to revert to this version?" %>
<% end %>
......
<div id="p-<%= note_versions_listing_type %>-listing">
<div id="p-<%= listing_type(:post_id, :note_id) %>-listing">
<%= table_for @note_versions, {class: "striped autofit", width: "100%"} do |t| %>
<% t.column "Post", width: "5%" do |note_version| %>
<%= link_to note_version.post_id, post_path(note_version.post_id) %>
<% if !params.dig(:search, :post_id).present? %>
<%= link_to "»", note_versions_path(search: {post_id: note_version.post_id}) %>
<%= link_to "»", note_versions_path(search: {post_id: note_version.post_id}, anchor: "note-version-#{note_version.id}") %>
<% end %>
<% end %>
<% t.column "Note", width: "5%" do |note_version| %>
<%= link_to "#{note_version.note_id}.#{note_version.version}", post_path(note_version.post_id, anchor: "note-#{note_version.note_id}") %>
<% if !params.dig(:search, :note_id).present? %>
<%= link_to "»", note_versions_path(search: {note_id: note_version.note_id}) %>
<%= link_to "»", note_versions_path(search: {note_id: note_version.note_id}, anchor: "note-version-#{note_version.id}") %>
<% end %>
<% end %>
<% t.column "Body", td: {class: "col-expand"} do |note_version| %>
<%= h(note_version.body) %>
<% unless note_version.is_active? %>
<span class="inactive">(deleted)</span>
<% end %>
<%= note_version_body_diff_info(note_version) %>
<% t.column "Body", td: {class: "col-expand diff-body"} do |note_version| %>
<%= diff_body_html(note_version, note_version.previous, :body) %>
<% end %>
<% t.column "Position", width: "5%" do |note_version| %>
<% t.column "Position (X,Y)", width: "5%", column: "position" do |note_version| %>
<%= note_version_position_diff(note_version) %>
<% end %>
<% t.column "Edited By", width: "10%" do |note_version| %>
<%= link_to_user note_version.updater %>
<% t.column "Size (WxH)", width: "5%", column: "size" do |note_version| %>
<%= note_version_size_diff(note_version) %>
<% end %>
<% t.column "Date", width: "10%" do |note_version| %>
<%= compact_time note_version.updated_at %>
<% t.column "Changes", width: "3%" do |note_version| %>
<%= status_diff_html(note_version) %>
<% end %>
<% t.column "Updated", width: "10%" do |note_version| %>
<div>
<%= compact_time note_version.updated_at %>
</div>
by
<%= link_to_user note_version.updater %>
<%= link_to "»", note_versions_path(search: params[:search].merge({ updater_id: note_version.updater_id })) %>