2017-04-21 22:09:04 +08:00
|
|
|
module TinyMceHelper
|
|
|
|
def parse_tiny_mce_asset_to_token(text, ref = nil)
|
2017-04-24 22:22:25 +08:00
|
|
|
ids = []
|
2017-04-21 22:09:04 +08:00
|
|
|
html = Nokogiri::HTML(text)
|
|
|
|
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)
|
|
|
|
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.save
|
|
|
|
end
|
2017-04-24 22:22:25 +08:00
|
|
|
destroy_removed_tiny_mce_assets(ids, ref) if ref
|
2017-04-21 22:09:04 +08:00
|
|
|
html
|
|
|
|
end
|
|
|
|
|
2017-05-08 18:56:33 +08:00
|
|
|
def generate_image_tag_from_token(text)
|
2017-06-06 13:58:46 +08:00
|
|
|
return unless text
|
2017-04-21 22:09:04 +08:00
|
|
|
regex = /\[~tiny_mce_id:([0-9a-zA-Z]+)\]/
|
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-04-24 18:28:27 +08:00
|
|
|
next unless img
|
2017-05-09 19:59:59 +08:00
|
|
|
image_tag img.url,
|
|
|
|
class: 'img-responsive',
|
|
|
|
data: { token: Base62.encode(img.id) }
|
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-04-24 18:28:27 +08:00
|
|
|
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
|
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
|
|
|
|
|
|
|
|
def destroy_removed_tiny_mce_assets(ids, ref)
|
|
|
|
ref.tiny_mce_assets.where.not('id IN (?)', ids).destroy_all
|
2017-04-24 18:28:27 +08:00
|
|
|
end
|
2017-04-21 22:09:04 +08:00
|
|
|
end
|