a.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. message: '<loading>',
  85. onStop: function(loading) {
  86. loading.overlay.fadeOut(650);
  87. }
  88. });
  89. axios.post(basePath + 'api/get-temp', {
  90. text: app.text.replace(/\]\((blob:[^)]*)\)/g, function(_, blobURL) {
  91. return '](' + blobMap[blobURL] + ')';
  92. })
  93. }).then(function(response) {
  94. $('#enurl').loading('stop');
  95. var data = response.data;
  96. if (data.status == 1) {
  97. app.enurl = location.origin + location.pathname + data.guid;
  98. } else {
  99. modal.show('<failed>', '<error>');
  100. throw 'failed to get the tmp url!';
  101. }
  102. console.log(response);
  103. }).catch(function(error) {
  104. var msg;
  105. if (error.code) {
  106. if (error.code === 'ERR_NETWORK') msg = '<neterr>';
  107. if (error.code === 'ERR_BAD_REQUEST') msg = 'Bad Request';
  108. } else {
  109. msg = error.response ? error.response.data.message : '<unknerr>';
  110. }
  111. console.warn(error);
  112. $('#enurl').loading('stop');
  113. modal.show('<failed><br><br>' + msg, '<error>')
  114. })
  115. }
  116. }
  117. })