refactor, fix user annotation bug

This commit is contained in:
zmagod 2017-04-24 16:22:25 +02:00
parent 55c8c498e2
commit 66f43803c0
13 changed files with 55 additions and 50 deletions

View file

@ -13,10 +13,6 @@ var TinyMCE = (function() {
});
}
function getTinyMceAssetsUploadPath() {
return $('textarea.tinymce').attr('tiny_mce_upload_path');
}
// returns a public API for TinyMCE editor
return Object.freeze({
init : function() {

View file

@ -30,7 +30,8 @@
{text: "<%= I18n.t 'tiny_mce.insert_btn' %>",
onclick: insertImage,
subtype: 'primary'},
{text: "<%= I18n.t 'general.cancel' %>", onclick: ed.windowManager.close}
{text: "<%= I18n.t 'general.cancel' %>",
onclick: ed.windowManager.close}
],
}, {
plugin_url: url
@ -42,7 +43,10 @@
iframe = win.find('iframe')[0];
form = createElement('form', {
action: ed.getParam('customimageuploader_form_url',
'/tinymce_assets'),
'<%= Rails.application
.routes
.url_helpers
.tiny_mce_assets_path %>'),
target: iframe._id,
method: 'POST',
enctype: 'multipart/form-data',
@ -108,7 +112,7 @@
function insertImage() {
if(getInputValue('file') == '') {
return handleError("<%= I18n.t 'tiny_mce.error_message'");
return handleError("<%= I18n.t 'tiny_mce.error_message' %>");
}
throbber = new top.tinymce.ui.Throbber(win.getEl());
@ -142,7 +146,7 @@
var doc = target.contentDocument || target.contentWindow.document;
handleResponse(doc.getElementsByTagName("body")[0].innerHTML);
} else {
handleError("Didn't get a response from the server");
handleError("<%= I18n.t 'tiny_mce.server_not_respond' %>");
}
}
@ -157,7 +161,9 @@
ed.windowManager.close();
}
} catch(e) {
handleError('Got a bad response from the server');
// hack that get's the server error message
var error_json = JSON.parse($(ret).text());
handleError(error_json['error'][0]);
}
}
@ -215,14 +221,14 @@
// Add a button that opens a window
editor.addButton('customimageuploader', {
tooltip: ed.translate('Insert an image from your computer'),
tooltip: "<%= I18n.t 'tiny_mce.upload_window_label' %>",
icon: 'image',
onclick: showDialog
});
// Adds a menu item to the tools menu
editor.addMenuItem('customimageuploader', {
text: ed.translate('Insert an image from your computer'),
text: "<%= I18n.t 'tiny_mce.upload_window_label' %>",
icon: 'image',
context: 'insert',
onclick: showDialog

View file

@ -43,7 +43,7 @@ class ResultTextsController < ApplicationController
respond_to do |format|
if (@result.save and @result_text.save) then
#link tiny_mce_assets to the result text
# link tiny_mce_assets to the result text
link_tiny_mce_assets(@result_text.text, @result_text)
result_annotation_notification

View file

@ -54,13 +54,12 @@ class StepsController < ApplicationController
respond_to do |format|
if @step.save
# Post process all assets
@step.assets.each do |asset|
asset.post_process_file(@protocol.team)
end
#link tiny_mce_assets to the step
# link tiny_mce_assets to the step
link_tiny_mce_assets(@step.description, @step)
create_annotation_notifications(@step)

View file

@ -3,19 +3,18 @@ class TinyMceAssetsController < ApplicationController
def create
image = params.fetch(:file) { render_404 }
tiny_img = TinyMceAsset.new(image: image,
reference: @obj,
editing: true,
team_id: current_team.id)
tiny_img = TinyMceAsset.new(image: image, reference: @obj)
if tiny_img.save
render json: {
image: {
url: view_context.image_url(tiny_img.url(:original)),
token: Base62.encode(tiny_img.id)
}
image: {
url: view_context.image_url(tiny_img.url(:large)),
token: Base62.encode(tiny_img.id)
}
}, content_type: 'text/html'
else
render_404
render json: {
error: tiny_img.errors.full_messages
}, status: :unprocessable_entity
end
end
@ -24,7 +23,7 @@ class TinyMceAssetsController < ApplicationController
def find_object
obj_type = params.fetch(:object_type) { render_404 }
obj_id = params.fetch(:object_id) { render_404 }
render_404 unless ['step', 'result_text'].include? obj_type
render_404 unless %w(step result_text).include? obj_type
@obj = obj_type.classify.constantize.find_by_id(obj_id)
end
end

View file

@ -103,6 +103,7 @@ module ApplicationHelper
end
def smart_annotation_parser(text, team = nil)
team = nil unless team.kind_of? Team
new_text = smart_annotation_filter_resources(text)
new_text = smart_annotation_filter_users(new_text, team)
new_text
@ -187,10 +188,9 @@ module ApplicationHelper
if user &&
team &&
UserTeam.user_in_team(user, team).any?
user_t, = user
.user_teams
.where('user_teams.team_id = ?', team)
.first
user_t = user.user_teams
.where('user_teams.team_id = ?', team)
.first
end
user_description = %(<div class='col-xs-4'>
<img src='#{Rails.application.routes.url_helpers

View file

@ -17,7 +17,7 @@ module InputSanitizeHelper
def custom_auto_link(text, options = {})
simple_format = options.fetch(:simple_format) { true }
team = options[:team],
team = options.fetch(:team) { nil },
wrapper_tag = options.fetch(:wrapper_tag) { {} }
tags = options.fetch(:tags) { [] }
text = if simple_format

View file

@ -1,24 +1,25 @@
require 'nokogiri'
module TinyMceHelper
def parse_tiny_mce_asset_to_token(text, ref = nil)
ids = []
html = Nokogiri::HTML(text)
html.search('img').each do |img|
next unless img['data-token']
img_id = Base62.decode(img['data-token'])
ids << img_id
token = "[~tiny_mce_id:#{img_id}]"
img.replace(token)
next unless ref
tiny_img = TinyMceAsset.find_by_id(img_id)
tiny_img.reference = ref unless tiny_img.step || tiny_img.result_text
tiny_img.editing = false
tiny_img.save
end
destroy_removed_tiny_mce_assets(ids, ref) if ref
html
end
def generate_image_tag_from_token(text, ref = nil)
regex = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/
new_text = text.gsub(regex) do |el|
text.gsub(regex) do |el|
match = el.match(regex)
img = TinyMceAsset.find_by_id(match[1])
next unless img
@ -27,14 +28,20 @@ module TinyMceHelper
end
def link_tiny_mce_assets(text, ref)
ids = []
regex = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/
text.gsub(regex) do |img|
match = img.match(regex)
tiny_img = TinyMceAsset.find_by_id(match[1])
next unless tiny_img
ids << tiny_img.id
tiny_img.public_send("#{ref.class.to_s.underscore}=".to_sym, ref)
tiny_img.editing = false
tiny_img.save!
end
destroy_removed_tiny_mce_assets(ids, ref)
end
def destroy_removed_tiny_mce_assets(ids, ref)
ref.tiny_mce_assets.where.not('id IN (?)', ids).destroy_all
end
end

View file

@ -26,7 +26,7 @@ class Step < ActiveRecord::Base
has_many :report_elements, inverse_of: :step,
dependent: :destroy
has_many :tiny_mce_assets, inverse_of: :step, dependent: :destroy
accepts_nested_attributes_for :checklists,
reject_if: :all_blank,
allow_destroy: true

View file

@ -5,19 +5,23 @@ class TinyMceAsset < ActiveRecord::Base
belongs_to :step, inverse_of: :tiny_mce_assets
belongs_to :result_text, inverse_of: :tiny_mce_assets
belongs_to :team
has_attached_file :image,
styles: { medium: [Constants::MEDIUM_PIC_FORMAT, :jpg] },
convert_options: { medium: '-quality 70 -strip' }
styles: { large: [Constants::LARGE_PIC_FORMAT, :jpg] },
convert_options: { large: '-quality 100 -strip' }
validates_attachment_content_type :image,
content_type: %r{^image/#{ Regexp.union(
Constants::WHITELISTED_IMAGE_TYPES
) }}
validates_attachment :image,
presence: true,
size: {
less_than: Constants::FILE_MAX_SIZE_MB.megabytes
}
validates :estimated_size, presence: true
# When using S3 file upload, we can limit file accessibility with url signing
def presigned_url(style = :medium,
def presigned_url(style = :large,
download: false,
timeout: Constants::URL_SHORT_EXPIRE_TIME)
if stored_on_s3?
@ -40,7 +44,7 @@ class TinyMceAsset < ActiveRecord::Base
image.options[:storage].to_sym == :s3
end
def url(style = :medium, timeout: Constants::URL_SHORT_EXPIRE_TIME)
def url(style = :large, timeout: Constants::URL_SHORT_EXPIRE_TIME)
if image.is_stored_on_s3?
presigned_url(style, timeout: timeout)
else
@ -50,8 +54,6 @@ class TinyMceAsset < ActiveRecord::Base
private
# If team is provided, its space_taken
# is updated as well
def update_estimated_size
return if image_file_size.blank?
es = image_file_size * Constants::ASSET_ESTIMATED_SIZE_FACTOR
@ -61,6 +63,6 @@ class TinyMceAsset < ActiveRecord::Base
def set_reference
obj_type = "#{@reference.class.to_s.underscore}=".to_sym
self.public_send(obj_type, @reference) if @reference
public_send(obj_type, @reference) if @reference
end
end

View file

@ -1572,6 +1572,7 @@ en:
upload_window_label: 'Choose an image'
insert_btn: 'Insert'
error_message: 'You must choose a file'
server_not_respond: "Didn't get a response from the server"
general:
save: "Save"
update: "Update"

View file

@ -2,9 +2,7 @@ class AddAttachmentImageToTinyMceAssets < ActiveRecord::Migration
def change
create_table :tiny_mce_assets do |t|
t.attachment :image
t.boolean :editing, default: false
t.integer :estimated_size, default: 0, null: false
t.references :team, index: true
t.references :step, index: true
t.references :result_text, index: true
t.timestamps null: false

View file

@ -565,18 +565,15 @@ ActiveRecord::Schema.define(version: 20170420075905) do
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.boolean "editing", default: false
t.integer "estimated_size", default: 0, null: false
t.integer "team_id"
t.integer "estimated_size", default: 0, null: false
t.integer "step_id"
t.integer "result_text_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "tiny_mce_assets", ["result_text_id"], name: "index_tiny_mce_assets_on_result_text_id", using: :btree
add_index "tiny_mce_assets", ["step_id"], name: "index_tiny_mce_assets_on_step_id", using: :btree
add_index "tiny_mce_assets", ["team_id"], name: "index_tiny_mce_assets_on_team_id", using: :btree
create_table "tokens", force: :cascade do |t|
t.string "token", null: false