Skip to content

Commit

Permalink
Merge e561589 into 7f3e1b4
Browse files Browse the repository at this point in the history
  • Loading branch information
saracarl authored Feb 3, 2021
2 parents 7f3e1b4 + e561589 commit de52b47
Show file tree
Hide file tree
Showing 39 changed files with 2,667 additions and 148,782 deletions.
59 changes: 59 additions & 0 deletions app/assets/stylesheets/sections/collection.scss
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,65 @@
}
}


// Configure buttons
.edit-buttons {
padding: 0;
margin: 1.5em 0;
list-style: none;

li {
margin: 0;
display: flex;
padding: 15px 0;
border-style: solid;
border-width: 1px 0 0 0;
border-color: rgba(#000, 0.15);

&:last-child {
border-width: 1px 0;
}
}

&_format {
flex: 1 1 auto;
padding-right: 15px;

h5 {
display: flex;
align-items: center;
margin: 0 0 0.25em 0;

input {
width: 19px;
height: 19px;
margin-right: 5px;
}
}
}

&_options {
display: flex;
flex: 0 0 300px;
padding-left: 15px;
flex-direction: column;
justify-content: center;
border-left: 1px solid rgba(#000, 0.15);

label {
margin: 3px 0;
display: flex;
align-items: center;

input {
margin-right: 3px;
}
}
}
}



//Edit transcription fields
.button.fields {
margin: $gapSize/2 $gapSize/2 0 0;
Expand Down
27 changes: 26 additions & 1 deletion app/controllers/collection_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CollectionController < ApplicationController
before_action :load_settings, :only => [:edit, :update, :upload]

# no layout if xhr request
layout Proc.new { |controller| controller.request.xhr? ? false : nil }, :only => [:new, :create]
layout Proc.new { |controller| controller.request.xhr? ? false : nil }, :only => [:new, :create, :edit_buttons]

def authorized?
unless user_signed_in?
Expand All @@ -27,6 +27,31 @@ def authorized?
end
end

def edit_buttons
@prefer_html = @collection.editor_buttons.where(:prefer_html => true).exists?
end

def update_buttons
@collection.editor_buttons.delete_all

prefer_html = (params[:prefer_html] == 'true')

EditorButton::BUTTON_MAP.keys.each do |key|
if params[key] == "1"
button_config = EditorButton.new
button_config.key = key
button_config.prefer_html = prefer_html
button_config.collection = @collection
button_config.save
end
end

flash[:notice] = 'Editor Buttons Updated'
ajax_redirect_to(edit_collection_path(@collection.owner, @collection))

end


def enable_document_sets
@collection.supports_document_sets = true
@collection.save!
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/iiif_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def iiif_collection_id_from_collection(collection)
if collection.is_a? DocumentSet
iiif_document_set_url(collection)
else
url_for({ :controller => 'iiif', :action => 'collection', :collection_id => collection.id, :only_path => false })
url_for({ :controller => 'iiif', :action => 'collection', :collection_id => collection.slug, :only_path => false })
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/helpers/abstract_xml_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def xml_to_html(xml_text, preserve_lb=true, flatten_links=false, collection=nil)
end

doc.elements.each("//expan") do |e|
orig = e.attributes['orig']
orig = e.attributes['abbr'] || e.attributes['orig']
span = REXML::Element.new("span")
span.add_attribute("class", "expanded-abbreviation")
e.children.each { |c| span.add(c) }
Expand Down
1 change: 1 addition & 0 deletions app/models/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Collection < ApplicationRecord
has_one :sc_collection, :dependent => :destroy
has_many :transcription_fields, :dependent => :destroy
has_many :bulk_exports, :dependent => :destroy
has_many :editor_buttons, :dependent => :destroy

belongs_to :next_untranscribed_page, foreign_key: 'next_untranscribed_page_id', class_name: "Page", optional: true
has_many :pages, through: :works
Expand Down
4 changes: 4 additions & 0 deletions app/models/document_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def subjects_disabled
self.collection.subjects_disabled
end

def editor_buttons
self.collection.editor_buttons
end

def articles
Article.joins(:pages).where(pages: {work_id: self.works.ids}).distinct
end
Expand Down
63 changes: 63 additions & 0 deletions app/models/editor_button.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class EditorButton < ApplicationRecord
belongs_to :collection

module Keys
ABBR = 'abbr'
ADD = 'add'
DATE = 'date'
DEL = 'del'
EXPAN = 'expan'
FIG = 'fig'
GAP = 'gap'
REG = 'reg'
STRIKE = 's'
SUB = 'sub'
SUP = 'sup'
UNCLEAR = 'unclear'
UNDERLINE = 'u'
end

BUTTON_MAP = {
Keys::ABBR => ['<abbr expan="">'],
Keys::ADD => ['<add>'],
Keys::DATE => ['<date when="">'],
Keys::DEL => ['<del>'],
Keys::EXPAN => ['<expan abbr="">'],
Keys::FIG => ['<fig rend="hr">'],
Keys::GAP => ['<gap>'],
Keys::REG => ['<reg orig="">'],
Keys::STRIKE => ['<hi rend="str">', '<s>'],
Keys::SUB => ['<hi rend="sub">', '<sub>'],
Keys::SUP => ['<hi rend="sup">', '<sup>'],
Keys::UNCLEAR => ['<unclear>'],
Keys::UNDERLINE => ['<hi rend="underline">', '<u>']
}


def open_tag
tags = BUTTON_MAP[self.key]
if self.prefer_html && tags.size > 1
tags[1].html_safe
else
tags[0].html_safe
end
end


def close_tag
('</' + open_tag.sub('<', '').sub(/\s.*/, '').sub('>','') + '>').html_safe
end

def cursor_offset
self.close_tag.length
end

def has_attribute
open_tag.match /\s/
end

def hotkey
"Ctrl-E"
end

end
6 changes: 6 additions & 0 deletions app/views/collection/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@
span =t('.revert_document_based_transcription')
=link_to t('.edit_fields'), transcription_field_edit_fields_path(:collection_id => @collection), class: 'button fields', 'aria-label' => t('.edit_fields')

-if !@collection.field_based
h3 =t('.editor_buttons')
p =t('.editor_buttons_description')
=link_to(collection_editor_button_edit_path(:collection_id => @collection.id), class: 'button', 'aria-label' => t('.edit_buttons'), :data => { litebox: { hash: 'edit-buttons' }})
span =t('.edit_buttons')

h3 =t('.document_sets')
p =t('.document_sets_description')
-if !@collection.supports_document_sets
Expand Down
43 changes: 43 additions & 0 deletions app/views/collection/edit_buttons.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
h2.nomargin =t('.heading')
p =t('.description')
=form_with(url: collection_editor_button_update_path, local: true) do |f|
=hidden_field_tag :collection_id, @collection.slug
-buttons = @collection.editor_buttons.to_a.map { |b| b.key}
ul.edit-buttons
-EditorButton::BUTTON_MAP.keys.each do |key|
li
.edit-buttons_format
h5
=t(".#{key}_label")
p.nomargin =t(".#{key}_description")
.edit-buttons_options
=f.label key
=check_box_tag key, '1', buttons.include?(key)
span
-if EditorButton::BUTTON_MAP[key].size > 1
code
=EditorButton::BUTTON_MAP[key][0]
=t('.tei')
br
code
=EditorButton::BUTTON_MAP[key][1]
=t('.html')
-else
code
=EditorButton::BUTTON_MAP[key][0]
li
.edit-buttons_format
h5 =t('.markup_preference_label')
p.nomargin =t('.markup_preference_description')
.edit-buttons_options
=f.label :prefer_html_true
=radio_button_tag :prefer_html, :true, @prefer_html
span =t('.prefer_html')
=f.label :prefer_html_false
=radio_button_tag :prefer_html, :false, !@prefer_html
span =t('.prefer_tei')


.toolbar
.toolbar_group.aright =f.button 'Save', class: 'big', 'data-start-export': ''

4 changes: 2 additions & 2 deletions app/views/export/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ p
=t('.iiif_api_description', link: link_to('here', 'https://github.com/benwbrum/fromthepage/wiki/FromThePage-Support-for-the-IIIF-Presentation-API-and-Web-Annotations')).html_safe

p
=t('.iiif_collection_api_endpoint', link: link_to(iiif_collection_url(@collection.id, only_path: false),
iiif_collection_url(@collection.id, only_path: false))).html_safe
=t('.iiif_collection_api_endpoint', link: link_to(iiif_collection_url(@collection.slug, only_path: false),
iiif_collection_url(@collection.slug, only_path: false))).html_safe

=render({ :partial => '/shared/collection_footer' })

Expand Down
102 changes: 102 additions & 0 deletions app/views/shared/_codemirror.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<%=stylesheet_link_tag "codemirror/lib/codemirror"%>
<%=stylesheet_link_tag "codemirror-buttons/buttons"%>
<%=javascript_include_tag "codemirror/lib/codemirror"%>
<%=javascript_include_tag "codemirror/mode/xml/xml"%>
<%=javascript_include_tag "codemirror/addon/display/display/panel"%>
<%=javascript_include_tag "codemirror/addon/hint/show-hint"%>
<%=javascript_include_tag "codemirror/addon/hint/xml-hint"%>
<%=javascript_include_tag "codemirror-buttons/buttons"%>

<script>
var button_config = [
<% @collection.editor_buttons.each do |button| %>
{
hotkey: '<%= button.hotkey %>',
class: '<%= "editor-buttons-#{button.key}" %>',
label: '<%= "#{button.key}" %>',
callback: function (cm) {
var selection = cm.getSelection();
cm.replaceSelection('<%= button.open_tag %>' + selection + '<%= button.close_tag %>');
if (!selection) {
var cursorPos = cm.getCursor();
cm.setCursor(cursorPos.line, cursorPos.ch - <%= button.cursor_offset %>);
}
<% if button.has_attribute %>
else {
var cursorPos = cm.getCursor();
cm.setCursor(cursorPos.line, cursorPos.ch - <%= button.cursor_offset %> - selection.length - 2);
}
<% end %>
}
},
<% end %>
];

var fromthepage_tags = {
"!top": ["abbr", "add", "cb", "date", "del", "expan", "figure", "gap", "hi", "ins", "milestone", "reg", "stamp", "sub", "sup", "table", "unclear", "u", "strike", "s", "span", "a"],
abbr: { attrs: { expan: null}},
add: { children: ["u", "s", "hi"] },
cb: { attrs: { "n": null } },
date: {attrs: { when: "YYYY-MM-DD" }, children: ["hi", "unclear", "gap"] },
expan: {attrs: { abbr: null }},
figure: { attrs: { "type": ["hr", "postmark", "seal"] }},
hi: { attrs: { "rend": ["bold", "italic", "str", "sub", "sup", "underline"]}},
ins: { children: ["u", "s", "hi"]},
milestone: { attrs: { "unit": ["column"], "n": null}},
reg: {attrs: { orig: null }},
row: { children: ["cell"] },
stamp: { attrs: {"type": ["clerical", "postage", "revenue"]}},
sup: { children: ["u", "s"] },
table: { attrs: {"rend": ["rules"]}, children: ["row"] }
}


function completeAfter(cm, pred) {
var cur = cm.getCursor();
if (!pred || pred()) setTimeout(function() {
if (!cm.state.completionActive)
cm.showHint({completeSingle: false});
}, 100);
return CodeMirror.Pass;
}

function completeIfAfterLt(cm) {
return completeAfter(cm, function() {
var cur = cm.getCursor();
return cm.getRange(CodeMirror.Pos(cur.line, cur.ch - 1), cur) == "<";
});
}

function completeIfInTag(cm) {
return completeAfter(cm, function() {
var tok = cm.getTokenAt(cm.getCursor());
if (tok.type == "string" && (!/['"]/.test(tok.string.charAt(tok.string.length - 1)) || tok.string.length == 1)) return false;
var inner = CodeMirror.innerMode(cm.getMode(), tok.state).state;
return inner.tagName;
});
}

var myCodeMirror = CodeMirror.fromTextArea(
document.getElementById("<%= textarea %>"),
{
lineNumbers: true,
mode: "xml",
extraKeys: {
"'<'": completeAfter,
"'/'": completeIfAfterLt,
"' '": completeIfInTag,
"'='": completeIfInTag,
"Ctrl-Space": "autocomplete",
"Tab": "autocomplete"
},
lineWrapping: true,
hintOptions: {schemaInfo: fromthepage_tags},
buttons: button_config
});

<% if has_title %>
myCodeMirror.setSize("100%", "91.5%");
<% else %>
myCodeMirror.setSize("100%", "100%");
<% end %>
</script>
Loading

0 comments on commit de52b47

Please sign in to comment.