|
|
@@ -1,26 +1,26 @@
|
|
|
-function replaceDataURLFrom(text) {
|
|
|
- var blobMap = {};
|
|
|
- var promises = [];
|
|
|
- var test = /\]\((data:.*?)\)/g
|
|
|
- for (var match = test.exec(text); match; match = test.exec(text)) {
|
|
|
- promises.push(
|
|
|
- fetch(match[1]).then(function(res) {
|
|
|
- return Promise.all([res.url, res.blob()]);
|
|
|
- }).then(function([dataURL, blob]) {
|
|
|
- blobMap[dataURL] = URL.createObjectURL(blob);
|
|
|
- })
|
|
|
- );
|
|
|
- };
|
|
|
- return Promise.all(promises).then(function() {
|
|
|
- return text.replace(test, function(_, dataURL) {
|
|
|
- return '](' + blobMap[dataURL] + ')';
|
|
|
- });
|
|
|
+function cacheDataURLFrom(text, toCache) {
|
|
|
+ var urlMap = {};
|
|
|
+ var attachmentCount = 0;
|
|
|
+ var test = /\[(.*?)\]\((data:.*?)\)/g;
|
|
|
+ var newText = text.replace(test, function (_, fileName, dataURL) {
|
|
|
+ var fakeURL = location.href + '/' + ++attachmentCount +'/' + fileName;
|
|
|
+ urlMap[fakeURL] = dataURL;
|
|
|
+ return '[' + fileName + '](' + fakeURL + ')';
|
|
|
+ });
|
|
|
+ return Promise.all(
|
|
|
+ Object.entries(urlMap).map(function([fakeURL, dataURL]) {
|
|
|
+ return fetch(dataURL).then(function(res) {
|
|
|
+ return toCache.put(fakeURL, new Response(res.body, res));
|
|
|
+ });
|
|
|
+ })
|
|
|
+ ).then(function() {
|
|
|
+ return newText;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
Vue.component('vue-easymde', VueEasyMDE.VueEasyMDE)
|
|
|
|
|
|
-var origText;
|
|
|
+var origText
|
|
|
|
|
|
// 查看消息需要用到的JS
|
|
|
app = new Vue({
|
|
|
@@ -51,14 +51,23 @@ app = new Vue({
|
|
|
mounted: function() {
|
|
|
// 从URL获取GUID,然后透过POST获取消息
|
|
|
var guid = reg_guid.exec(location.href)[0];
|
|
|
- axios.post('/api/get-msg', {
|
|
|
- guid: guid
|
|
|
+ navigator.serviceWorker.register('./sw.js').then(function () {
|
|
|
+ return navigator.serviceWorker.ready;
|
|
|
+ }).then(function() {
|
|
|
+ return axios.post('/api/get-msg', {
|
|
|
+ guid: guid
|
|
|
+ });
|
|
|
}).then(function(response) {
|
|
|
var data = response.data;
|
|
|
app.status = data.status;
|
|
|
if (app.status !== 1) return null;
|
|
|
origText = data.text;
|
|
|
- return replaceDataURLFrom(data.text);
|
|
|
+ $(window).on('navigate unload', function() {
|
|
|
+ caches.delete(guid);
|
|
|
+ });
|
|
|
+ return caches.open(guid);
|
|
|
+ }).then(function(toCache) {
|
|
|
+ return toCache && cacheDataURLFrom(origText, toCache);
|
|
|
}).then(function(text) {
|
|
|
app.text = text;
|
|
|
}).catch(function(error) {
|