2017-04-21 22:09:04 +08:00
|
|
|
module TinyMceHelper
|
2017-10-03 00:06:42 +08:00
|
|
|
def parse_tiny_mce_asset_to_token(text, obj)
|
2017-04-24 22:22:25 +08:00
|
|
|
ids = []
|
2017-11-28 21:16:20 +08:00
|
|
|
html = Nokogiri::HTML(remove_pasted_tokens(text))
|
2017-04-21 22:09:04 +08:00
|
|
|
html.search('img').each do |img|
|
|
|
|
next unless img['data-token']
|
|
|
|
img_id = Base62.decode(img['data-token'])
|
2017-04-24 22:22:25 +08:00
|
|
|
ids << img_id
|
2017-04-21 22:09:04 +08:00
|
|
|
token = "[~tiny_mce_id:#{img_id}]"
|
|
|
|
img.replace(token)
|
2017-10-03 00:06:42 +08:00
|
|
|
next unless obj
|
2017-04-21 22:09:04 +08:00
|
|
|
tiny_img = TinyMceAsset.find_by_id(img_id)
|
2017-10-03 00:06:42 +08:00
|
|
|
tiny_img.reference = obj unless tiny_img.step || tiny_img.result_text
|
2017-04-21 22:09:04 +08:00
|
|
|
tiny_img.save
|
|
|
|
end
|
2017-10-03 00:06:42 +08:00
|
|
|
destroy_removed_tiny_mce_assets(ids, obj) if obj
|
2017-04-21 22:09:04 +08:00
|
|
|
html
|
|
|
|
end
|
|
|
|
|
2018-04-05 19:15:32 +08:00
|
|
|
# @param pdf_export_ready is needed for wicked_pdf in export report action
|
|
|
|
def generate_image_tag_from_token(text, obj, pdf_export_ready = false)
|
2017-06-06 13:58:46 +08:00
|
|
|
return unless text
|
2017-11-25 00:04:01 +08:00
|
|
|
regex = Constants::TINY_MCE_ASSET_REGEX
|
2017-04-24 22:22:25 +08:00
|
|
|
text.gsub(regex) do |el|
|
2017-04-21 22:09:04 +08:00
|
|
|
match = el.match(regex)
|
|
|
|
img = TinyMceAsset.find_by_id(match[1])
|
2017-10-03 00:06:42 +08:00
|
|
|
next unless img && img.team == current_team
|
|
|
|
next unless check_image_permissions(obj, img)
|
2018-04-05 19:15:32 +08:00
|
|
|
if pdf_export_ready
|
|
|
|
report_image_asset_url(img, :tiny_mce_asset, 'tiny-mce-pdf-ready')
|
|
|
|
else
|
2018-04-05 22:46:09 +08:00
|
|
|
image_tag(img.url,
|
2018-04-05 19:15:32 +08:00
|
|
|
class: 'img-responsive',
|
2018-04-05 22:46:09 +08:00
|
|
|
data: { token: Base62.encode(img.id) })
|
2018-04-05 19:15:32 +08:00
|
|
|
end
|
2017-04-21 22:09:04 +08:00
|
|
|
end
|
|
|
|
end
|
2017-04-24 18:28:27 +08:00
|
|
|
|
|
|
|
def link_tiny_mce_assets(text, ref)
|
2017-04-24 22:22:25 +08:00
|
|
|
ids = []
|
2017-11-25 00:04:01 +08:00
|
|
|
regex = Constants::TINY_MCE_ASSET_REGEX
|
2017-04-24 18:28:27 +08:00
|
|
|
text.gsub(regex) do |img|
|
|
|
|
match = img.match(regex)
|
|
|
|
tiny_img = TinyMceAsset.find_by_id(match[1])
|
|
|
|
next unless tiny_img
|
2017-04-24 22:22:25 +08:00
|
|
|
ids << tiny_img.id
|
2017-04-24 18:28:27 +08:00
|
|
|
tiny_img.public_send("#{ref.class.to_s.underscore}=".to_sym, ref)
|
|
|
|
tiny_img.save!
|
|
|
|
end
|
2017-04-24 22:22:25 +08:00
|
|
|
destroy_removed_tiny_mce_assets(ids, ref)
|
|
|
|
end
|
|
|
|
|
2017-11-22 23:47:21 +08:00
|
|
|
def replace_tiny_mce_assets(text, img_ids)
|
|
|
|
img_ids.each do |src_id, dest_id|
|
|
|
|
regex = /\[~tiny_mce_id:#{src_id}\]/
|
|
|
|
new_token = "[~tiny_mce_id:#{dest_id}]"
|
|
|
|
text.sub!(regex, new_token)
|
|
|
|
end
|
|
|
|
text
|
|
|
|
end
|
|
|
|
|
2017-04-24 22:22:25 +08:00
|
|
|
def destroy_removed_tiny_mce_assets(ids, ref)
|
2017-11-28 18:01:07 +08:00
|
|
|
# need to check if the array is empty because if we pass the empty array
|
|
|
|
# in the SQL query it will not work properly
|
|
|
|
if ids.empty?
|
|
|
|
ref.tiny_mce_assets.destroy_all
|
|
|
|
else
|
|
|
|
ref.tiny_mce_assets.where.not('id IN (?)', ids).destroy_all
|
|
|
|
end
|
2017-04-24 18:28:27 +08:00
|
|
|
end
|
2017-10-03 00:06:42 +08:00
|
|
|
|
|
|
|
def check_image_permissions(obj, img)
|
|
|
|
if obj.class == Step
|
2017-10-03 00:11:54 +08:00
|
|
|
img.step == obj
|
2017-10-03 00:06:42 +08:00
|
|
|
elsif obj.class == ResultText
|
2017-10-03 00:11:54 +08:00
|
|
|
img.result_text == obj
|
2017-10-03 00:06:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-28 21:16:20 +08:00
|
|
|
def remove_pasted_tokens(text)
|
|
|
|
regex = Constants::TINY_MCE_ASSET_REGEX
|
|
|
|
text.gsub(regex, ' ')
|
2017-10-03 00:06:42 +08:00
|
|
|
end
|
2017-04-21 22:09:04 +08:00
|
|
|
end
|