a.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. var basePath = document.currentScript.src + '/../../';
  2. modal = new Vue({
  3. el: '#modal',
  4. data: {
  5. msg: '<none>',
  6. title: '<info>'
  7. },
  8. methods: {
  9. show: function(msg, title) {
  10. if (title) {
  11. this.title = title;
  12. }
  13. this.msg = msg;
  14. $('#mmodal').modal();
  15. }
  16. }
  17. })
  18. Vue.component('vue-easymde', VueEasyMDE.VueEasyMDE)
  19. var blobMap = {}
  20. app = new Vue({
  21. el: '#app',
  22. data: {
  23. text: null,
  24. enurl: null,
  25. copyicon: 'fa-clone',
  26. easyMDEOpts: {
  27. maxHeight: '47vh',
  28. onToggleFullScreen: function() { $('.navbar').toggle(); },
  29. sideBySideFullscreen: false,
  30. spellChecker: false,
  31. toolbar: [
  32. 'undo', 'redo', '|',
  33. 'bold', 'italic', 'strikethrough', 'heading', '|',
  34. 'quote', 'unordered-list', 'ordered-list', 'check-list', '|',
  35. 'link', 'image',
  36. {
  37. name: 'upload-image',
  38. action: EasyMDE.drawUploadedImage,
  39. className: 'fa fa-upload',
  40. title: 'Upload File',
  41. },
  42. {
  43. name: 'add-table',
  44. action: EasyMDE.drawTable,
  45. className: 'fa fa-table',
  46. title: 'Insert Table',
  47. },
  48. 'horizontal-rule', '|',
  49. 'preview', 'side-by-side', 'fullscreen', '|', 'guide'
  50. ],
  51. renderingConfig: { codeSyntaxHighlighting: true },
  52. uploadImage: true,
  53. imageAccept: '*',
  54. imageMaxSize: 268435456,
  55. imageUploadFunction: function(file, onSuccess, onError) {
  56. var url = URL.createObjectURL(file);
  57. var reader = new FileReader();
  58. reader.onerror = onError;
  59. reader.onload = function() {
  60. blobMap[url] = reader.result;
  61. var prefix = '[' + file.name + ']';
  62. if (file.type.startsWith('image')) {
  63. prefix = '!' + prefix;
  64. }
  65. var test = new RegExp('\\[(.+)(' + file.name + ')?\\]\\(blob:.*?\\1\\)');
  66. app.text = app.text.replace(test, prefix + '(' + url + ')');
  67. };
  68. onSuccess(url);
  69. reader.readAsDataURL(file);
  70. },
  71. },
  72. },
  73. methods: {
  74. copyTempURL: function() {
  75. navigator.clipboard.writeText(app.enurl).then(function() {
  76. app.copyicon = 'fa-check';
  77. setTimeout(function() {
  78. app.copyicon = 'fa-clone';
  79. }, 3000);
  80. })
  81. },
  82. getTempURL: function() {
  83. $('#enurl').loading({
  84. onStop: function(loading) {
  85. loading.overlay.fadeOut(650);
  86. }
  87. });
  88. axios.post(basePath + 'api/get-temp', {
  89. text: app.text.replace(/\]\((blob:[^)]*)\)/g, function(_, blobURL) {
  90. return '](' + blobMap[blobURL] + ')';
  91. })
  92. }).then(function(response) {
  93. $('#enurl').loading('stop');
  94. var data = response.data;
  95. if (data.status == 1) {
  96. app.enurl = location.origin + location.pathname + data.guid;
  97. } else {
  98. modal.show('<failed>', '<error>');
  99. throw 'failed to get the tmp url!';
  100. }
  101. console.log(response);
  102. }).catch(function(error) {
  103. var msg;
  104. if (error.code) {
  105. if (error.code === 'ERR_NETWORK') msg = '<neterr>';
  106. if (error.code === 'ERR_BAD_REQUEST') msg = 'Bad Request';
  107. } else {
  108. msg = error.response ? error.response.data.message : '<unknerr>';
  109. }
  110. console.warn(error);
  111. $('#enurl').loading('stop');
  112. modal.show('<failed><br><br>' + msg, '<error>')
  113. })
  114. }
  115. }
  116. })