mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-02 18:04:29 +08:00
add image upload functionality to result text
This commit is contained in:
parent
550f563666
commit
55c8c498e2
15 changed files with 73 additions and 13 deletions
|
@ -517,12 +517,15 @@ $("[data-action='new-step']").on("ajax:success", function(e, data) {
|
|||
applyCancelOnNew();
|
||||
toggleButtons(false);
|
||||
initializeCheckboxSorting();
|
||||
TinyMCE.init();
|
||||
|
||||
|
||||
$("#step_name").focus();
|
||||
$("#new-step-main-tab a").on("shown.bs.tab", function() {
|
||||
$("#step_name").focus();
|
||||
});
|
||||
|
||||
TinyMCE.refresh();
|
||||
|
||||
});
|
||||
|
||||
// Needed because server-side validation failure clears locations of
|
||||
|
|
|
@ -2,6 +2,7 @@ class ResultTextsController < ApplicationController
|
|||
include ResultsHelper
|
||||
include ActionView::Helpers::UrlHelper
|
||||
include ApplicationHelper
|
||||
include TinyMceHelper
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
before_action :load_vars, only: [:edit, :update, :download]
|
||||
|
@ -31,6 +32,7 @@ class ResultTextsController < ApplicationController
|
|||
|
||||
def create
|
||||
@result_text = ResultText.new(result_params[:result_text_attributes])
|
||||
@result_text.text = parse_tiny_mce_asset_to_token(@result_text.text)
|
||||
@result = Result.new(
|
||||
user: current_user,
|
||||
my_module: @my_module,
|
||||
|
@ -41,6 +43,8 @@ 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(@result_text.text, @result_text)
|
||||
|
||||
result_annotation_notification
|
||||
# Generate activity
|
||||
|
@ -81,6 +85,7 @@ class ResultTextsController < ApplicationController
|
|||
end
|
||||
|
||||
def edit
|
||||
@result_text.text = generate_image_tag_from_token(@result_text.text)
|
||||
respond_to do |format|
|
||||
format.json {
|
||||
render json: {
|
||||
|
@ -97,6 +102,8 @@ class ResultTextsController < ApplicationController
|
|||
update_params = result_params
|
||||
@result.last_modified_by = current_user
|
||||
@result.assign_attributes(update_params)
|
||||
@result_text.text = parse_tiny_mce_asset_to_token(@result_text.text,
|
||||
@result_text)
|
||||
success_flash = t("result_texts.update.success_flash",
|
||||
module: @my_module.name)
|
||||
if @result.archived_changed?(from: false, to: true)
|
||||
|
|
|
@ -60,7 +60,11 @@ class StepsController < ApplicationController
|
|||
asset.post_process_file(@protocol.team)
|
||||
end
|
||||
|
||||
#link tiny_mce_assets to the step
|
||||
link_tiny_mce_assets(@step.description, @step)
|
||||
|
||||
create_annotation_notifications(@step)
|
||||
|
||||
# Generate activity
|
||||
if @protocol.in_module?
|
||||
Activity.create(
|
||||
|
|
|
@ -3,7 +3,10 @@ class TinyMceAssetsController < ApplicationController
|
|||
|
||||
def create
|
||||
image = params.fetch(:file) { render_404 }
|
||||
tiny_img = TinyMceAsset.new(image: image, reference: @obj, editing: true)
|
||||
tiny_img = TinyMceAsset.new(image: image,
|
||||
reference: @obj,
|
||||
editing: true,
|
||||
team_id: current_team.id)
|
||||
if tiny_img.save
|
||||
render json: {
|
||||
image: {
|
||||
|
|
|
@ -7,10 +7,10 @@ module TinyMceHelper
|
|||
img_id = Base62.decode(img['data-token'])
|
||||
token = "[~tiny_mce_id:#{img_id}]"
|
||||
img.replace(token)
|
||||
byebug
|
||||
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
|
||||
html
|
||||
|
@ -21,7 +21,20 @@ module TinyMceHelper
|
|||
new_text = text.gsub(regex) do |el|
|
||||
match = el.match(regex)
|
||||
img = TinyMceAsset.find_by_id(match[1])
|
||||
next unless img
|
||||
image_tag img.url, data: { token: Base62.encode(img.id) }
|
||||
end
|
||||
end
|
||||
|
||||
def link_tiny_mce_assets(text, ref)
|
||||
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
|
||||
tiny_img.public_send("#{ref.class.to_s.underscore}=".to_sym, ref)
|
||||
tiny_img.editing = false
|
||||
tiny_img.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,5 +5,5 @@ class ResultText < ActiveRecord::Base
|
|||
length: { maximum: Constants::RICH_TEXT_MAX_LENGTH }
|
||||
validates :result, presence: true
|
||||
belongs_to :result, inverse_of: :result_text
|
||||
has_many :tiny_mce_assets, inverse_of: :result_text
|
||||
has_many :tiny_mce_assets, inverse_of: :result_text, dependent: :destroy
|
||||
end
|
||||
|
|
|
@ -25,7 +25,8 @@ class Step < ActiveRecord::Base
|
|||
has_many :tables, through: :step_tables
|
||||
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
|
||||
|
|
|
@ -268,10 +268,13 @@ class Team < ActiveRecord::Base
|
|||
project.project_my_modules.find_each do |my_module|
|
||||
my_module.protocol.steps.find_each do |step|
|
||||
step.assets.find_each { |asset| st += asset.estimated_size }
|
||||
step.tiny_mce_assets.find_each { |tiny| st += tiny.estimated_size }
|
||||
end
|
||||
my_module.results.find_each do |result|
|
||||
if result.is_asset then
|
||||
st += result.asset.estimated_size
|
||||
st += result.asset.estimated_size if result.is_asset
|
||||
if result.is_text
|
||||
tiny_assets = TinyMceAsset.where(result_text: result.result_text)
|
||||
tiny_assets.find_each { |tiny| st += tiny.estimated_size }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
class TinyMceAsset < ActiveRecord::Base
|
||||
attr_accessor :reference
|
||||
before_create :set_reference
|
||||
after_create :update_estimated_size
|
||||
|
||||
belongs_to :step
|
||||
belongs_to :result_text
|
||||
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' }
|
||||
|
@ -12,6 +14,8 @@ class TinyMceAsset < ActiveRecord::Base
|
|||
content_type: %r{^image/#{ Regexp.union(
|
||||
Constants::WHITELISTED_IMAGE_TYPES
|
||||
) }}
|
||||
validates :estimated_size, presence: true
|
||||
|
||||
# When using S3 file upload, we can limit file accessibility with url signing
|
||||
def presigned_url(style = :medium,
|
||||
download: false,
|
||||
|
@ -44,6 +48,17 @@ class TinyMceAsset < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
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
|
||||
update(estimated_size: es)
|
||||
Rails.logger.info "Asset #{id}: Estimated size successfully calculated"
|
||||
end
|
||||
|
||||
def set_reference
|
||||
obj_type = "#{@reference.class.to_s.underscore}=".to_sym
|
||||
self.public_send(obj_type, @reference) if @reference
|
||||
|
|
|
@ -3,7 +3,10 @@
|
|||
<%= f.text_field :name, style: "margin-top: 10px;" %><br />
|
||||
<%= f.fields_for :result_text do |ff| %>
|
||||
<div class="form-group">
|
||||
<%= ff.tiny_mce_editor(:text, value: @result.result_text.text) %>
|
||||
<%= ff.tiny_mce_editor(:text,
|
||||
value: @result.result_text.text,
|
||||
data: { object_type: 'result_text',
|
||||
object_id: @result.result_text.id }) %>
|
||||
</div>
|
||||
<% end %><br />
|
||||
<%= f.submit t("result_texts.edit.update"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.TEXT, true);" %>
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
<%= f.text_field :name, style: "margin-top: 10px;" %><br />
|
||||
<%= f.fields_for :result_text do |ff| %>
|
||||
<div class="form-group">
|
||||
<%= ff.tiny_mce_editor(:text) %>
|
||||
<%= ff.tiny_mce_editor(:text, data: { object_type: 'result_text',
|
||||
object_id: @result.result_text.id }) %>
|
||||
</div>
|
||||
<% end %><br />
|
||||
<%= f.submit t("result_texts.new.create"), class: 'btn btn-primary save-result', onclick: "processResult(event, ResultTypeEnum.TEXT, false);" %>
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
<div class="ql-editor">
|
||||
<%= custom_auto_link(result.result_text.text, simple_format: false) %>
|
||||
<%= custom_auto_link(generate_image_tag_from_token(result.result_text.text),
|
||||
simple_format: false,
|
||||
tags: %w(img)) %>
|
||||
</div>
|
||||
|
|
|
@ -56,7 +56,7 @@
|
|||
<div class="ql-editor">
|
||||
<%= custom_auto_link(generate_image_tag_from_token(step.description),
|
||||
simple_format: false,
|
||||
tags: ['img']) %>
|
||||
tags: %w(img)) %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
|
|
@ -3,6 +3,8 @@ class AddAttachmentImageToTinyMceAssets < ActiveRecord::Migration
|
|||
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
|
||||
|
|
|
@ -566,6 +566,8 @@ ActiveRecord::Schema.define(version: 20170420075905) do
|
|||
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 "step_id"
|
||||
t.integer "result_text_id"
|
||||
t.datetime "created_at", null: false
|
||||
|
@ -574,6 +576,7 @@ ActiveRecord::Schema.define(version: 20170420075905) do
|
|||
|
||||
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
|
||||
|
|
Loading…
Add table
Reference in a new issue