getKfContent.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Created by zhangbo21 on 14-9-2.
  3. */
  4. /*
  5. * getKfContent : 将image的src从base64替换为文件名
  6. * param : callback -- 回调函数 其参数为替换之后的内容
  7. * return : void
  8. * */
  9. /* eslint-disable */
  10. UE.Editor.prototype.getKfContent = function(callback){
  11. var me = this;
  12. var actionUrl = me.getActionUrl(me.getOpt('scrawlActionName')),
  13. params = UE.utils.serializeParam(me.queryCommandValue('serverparam')) || '',
  14. url = UE.utils.formatUrl(actionUrl + (actionUrl.indexOf('?') == -1 ? '?':'&') + params);
  15. // 找到所有的base64
  16. var count = 0;
  17. var imgs =me.body.getElementsByTagName('img');
  18. var base64Imgs = [];
  19. UE.utils.each(imgs, function(item){
  20. var imgType = item.getAttribute('src').match(/^[^;]+/)[0];
  21. if ( imgType === 'data:image/png') {
  22. base64Imgs.push(item);
  23. }
  24. });
  25. if (base64Imgs.length == 0){
  26. execCallback();
  27. } else {
  28. UE.utils.each(base64Imgs, function(item){
  29. var opt ={};
  30. opt[me.getOpt('scrawlFieldName')]= item.getAttribute('src').replace(/^[^,]+,/, '');
  31. opt.onsuccess = function(xhr){
  32. var json = UE.utils.str2json(xhr.responseText),
  33. url = me.options.scrawlUrlPrefix + json.url;
  34. item.setAttribute('src', url);
  35. item.setAttribute('_src', url);
  36. count++;
  37. execCallback();
  38. }
  39. opt.onerror = function(err){
  40. console.error(err);
  41. count++;
  42. execCallback();
  43. }
  44. UE.ajax.request(url, opt);
  45. });
  46. }
  47. function execCallback(){
  48. if (count >= base64Imgs.length) {
  49. me.sync();
  50. callback(me.getContent());
  51. }
  52. }
  53. };