view.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. function replaceDataURLFrom(text) {
  2. var blobMap = {};
  3. var promises = [];
  4. var test = /\]\((data:.*?)\)/g
  5. for (var match = test.exec(text); match; match = test.exec(text)) {
  6. promises.push(
  7. fetch(match[1]).then(function(res) {
  8. return Promise.all([res.url, res.blob()]);
  9. }).then(function([dataURL, blob]) {
  10. blobMap[dataURL] = URL.createObjectURL(blob);
  11. })
  12. );
  13. };
  14. return Promise.all(promises).then(function() {
  15. return text.replace(test, function(_, dataURL) {
  16. return '](' + blobMap[dataURL] + ')';
  17. });
  18. });
  19. }
  20. Vue.component('vue-easymde', VueEasyMDE.VueEasyMDE)
  21. var origText;
  22. // 查看消息需要用到的JS
  23. app = new Vue({
  24. el: '#app',
  25. data: {
  26. status: -1,
  27. text: null,
  28. easyMDEOpts: {
  29. maxHeight: '70vh',
  30. onToggleFullScreen: function() { $('.navbar').toggle(); },
  31. spellChecker: false,
  32. toolbar: [
  33. {
  34. name: 'copy no-disable',
  35. action: function() {
  36. if (!origText) return;
  37. navigator.clipboard.writeText(origText);
  38. },
  39. className: 'fa fa-clipboard',
  40. title: 'Copy to clipboard',
  41. },
  42. 'preview',
  43. 'fullscreen',
  44. ],
  45. renderingConfig: { codeSyntaxHighlighting: true },
  46. },
  47. },
  48. mounted: function() {
  49. // 从URL获取GUID,然后透过POST获取消息
  50. var guid = reg_guid.exec(location.href)[0];
  51. axios.post('/api/get-msg', {
  52. guid: guid
  53. }).then(function(response) {
  54. var data = response.data;
  55. app.status = data.status;
  56. if (app.status !== 1) return null;
  57. origText = data.text;
  58. return replaceDataURLFrom(data.text);
  59. }).then(function(text) {
  60. app.text = text;
  61. }).catch(function(error) {
  62. throw error;
  63. });
  64. },
  65. watch: {
  66. status: function(status) {
  67. if (status !== 1) return;
  68. // 获取成功,等待装载并设定编辑器状态
  69. this.$nextTick(() => {
  70. var easyMDE = this.$refs.preview.getMDEInstance();
  71. easyMDE.togglePreview();
  72. easyMDE.codemirror.setOption('readOnly', true);
  73. });
  74. }
  75. }
  76. })