upgradedocs.ui.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. async function upgradeDoc(docID, options)
  2. {
  3. const newData = await zui.fetchData(options.docFetcher, [{docID, version: 0}]);
  4. if(!newData || !newData.data) return false;
  5. const oldContent = newData.data.content;
  6. let content = '';
  7. if(options.type === 'html')
  8. {
  9. const snap = await zui.Editor.htmlToSnap(oldContent);
  10. if(!snap) return false;
  11. }
  12. else
  13. {
  14. try
  15. {
  16. content = await zui.Editor.convertToHtml(oldContent, undefined, options.downloadUrl);
  17. }
  18. catch(error)
  19. {
  20. console.warn('Convert doc to html failed:', error);
  21. }
  22. }
  23. const docData = {content};
  24. let result;
  25. await $.post(zui.formatString(options.migrateUrl, {docID: docID}), docData, (res) =>
  26. {
  27. result = res.result === 'success' ? true : res.message;
  28. }, 'json');
  29. return result;
  30. }
  31. async function upgradeWikis(idList, options)
  32. {
  33. let result;
  34. await $.post(options.wikiUrl, {wikis: idList.join(',')}, (res) =>
  35. {
  36. result = res.result === 'success' ? true : res.message;
  37. }, 'json');
  38. return result;
  39. }
  40. async function upgradeDocs(idList, options)
  41. {
  42. await zui.Editor.loadModule();
  43. const {
  44. onProgress,
  45. wikiUrl = $.createLink('upgrade', 'ajaxUpgradeWikis'),
  46. migrateUrl = $.createLink('upgrade', 'ajaxUpgradeDoc', 'docID={docID}'),
  47. docFetcher = $.createLink('upgrade', 'ajaxUpgradeDoc', 'docID={docID}'),
  48. downloadUrl = $.createLink('file', 'ajaxQuery', 'fileID={gid}'),
  49. } = options || {};
  50. const migrateOptions =
  51. {
  52. migrateUrl: migrateUrl,
  53. docFetcher: docFetcher,
  54. downloadUrl: downloadUrl,
  55. };
  56. let current = 0;
  57. const total = idList.doc.length + idList.html.length + idList.wiki.length;
  58. const types = ['doc', 'html'];
  59. for (const type of types)
  60. {
  61. for(const id of idList[type])
  62. {
  63. onProgress(current, total);
  64. await upgradeDoc(id, $.extend({type: type}, migrateOptions));
  65. await zui.delay(50);
  66. current++;
  67. }
  68. }
  69. if(idList.wiki.length)
  70. {
  71. await upgradeWikis(idList.wiki, {wikiUrl: wikiUrl});
  72. current += idList.wiki.length;
  73. }
  74. onProgress(current, total);
  75. return idList;
  76. }
  77. window.startUpgradeDocs = function(event, idList, upgradingDocsText, nextText)
  78. {
  79. const $btn = $('#upgradeDocsBtn');
  80. if($btn.hasClass('is-finished')) return;
  81. event.preventDefault();
  82. const $progressBar = $('#upgradeDocsProgress').addClass('active').find('.progress-bar');
  83. $btn.attr('disabled', 'disabled').addClass('disabled').removeClass('primary').find('.text').text(upgradingDocsText);
  84. upgradeDocs(idList,
  85. {
  86. onProgress: (current, total) =>
  87. {
  88. $progressBar.css('width', (100 * current / total) + '%');
  89. $btn.find('.text').text(`${upgradingDocsText} (${current}/${total})`);
  90. }
  91. }).then(() =>
  92. {
  93. $btn.removeAttr('disabled').removeClass('disabled').addClass('primary is-finished').find('.text').text(nextText);
  94. $('#upgradeDocsProgress').removeClass('active')
  95. });
  96. };