view.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function cacheDataURLFrom(text, toCache) {
  2. var urlMap = {};
  3. var attachmentCount = 0;
  4. var test = /\[(.*?)\]\((data:.*?)\)/g;
  5. var newText = text.replace(test, function (_, fileName, dataURL) {
  6. var fakeURL = location.href + '/' + ++attachmentCount +'/' + fileName;
  7. urlMap[fakeURL] = dataURL;
  8. return '[' + fileName + '](' + fakeURL + ')';
  9. });
  10. return Promise.all(
  11. Object.entries(urlMap).map(function([fakeURL, dataURL]) {
  12. return fetch(dataURL).then(function(res) {
  13. return toCache.put(fakeURL, new Response(res.body, res));
  14. });
  15. })
  16. ).then(function() {
  17. return newText;
  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. navigator.serviceWorker.register('./sw.js').then(function () {
  52. return navigator.serviceWorker.ready;
  53. }).then(function() {
  54. return axios.post('/api/get-msg', {
  55. guid: guid
  56. });
  57. }).then(function(response) {
  58. var data = response.data;
  59. app.status = data.status;
  60. if (app.status !== 1) return null;
  61. origText = data.text;
  62. $(window).on('navigate unload', function() {
  63. caches.delete(guid);
  64. });
  65. return caches.open(guid);
  66. }).then(function(toCache) {
  67. return toCache && cacheDataURLFrom(origText, toCache);
  68. }).then(function(text) {
  69. app.text = text;
  70. }).catch(function(error) {
  71. throw error;
  72. });
  73. },
  74. watch: {
  75. status: function(status) {
  76. if (status !== 1) return;
  77. // 获取成功,等待装载并设定编辑器状态
  78. this.$nextTick(() => {
  79. var easyMDE = this.$refs.preview.getMDEInstance();
  80. easyMDE.togglePreview();
  81. easyMDE.codemirror.setOption('readOnly', true);
  82. });
  83. }
  84. }
  85. })