From 6bbe22aaa2c4f3b5e3a87c3aa087c9ebcd61c4e3 Mon Sep 17 00:00:00 2001 From: Benoit Schweblin Date: Fri, 29 Sep 2017 19:43:26 +0100 Subject: [PATCH] Fixed multiple window issue --- .gitignore | 1 + index.js | 2 +- package.json | 1 - server/index.js | 61 ++++++++++++++++++++++---------------- src/services/localDbSvc.js | 14 ++++----- src/store/modules/data.js | 13 +++----- 6 files changed, 49 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 8b457db9..38ad1968 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ npm-debug.log* yarn-debug.log* yarn-error.log* .vscode +stackedit_v4 diff --git a/index.js b/index.js index 671f6a43..db99bd26 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,7 @@ var path = require('path'); var express = require('express'); var app = express(); -require('./server')(app); +require('./server')(app, process.env.SERVE_V4); var port = parseInt(process.env.PORT || 8080, 10); if(port === 443) { diff --git a/package.json b/package.json index b082fec7..6bab7ccd 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "raw-loader": "^0.5.1", "request": "^2.82.0", "serve-static": "^1.12.6", - "stackedit": "^4.3.17", "vue": "^2.3.3", "vuex": "^2.3.1" }, diff --git a/server/index.js b/server/index.js index e701d360..2b31e4bb 100644 --- a/server/index.js +++ b/server/index.js @@ -2,15 +2,18 @@ var compression = require('compression'); var serveStatic = require('serve-static'); var path = require('path'); -module.exports = function (app) { +module.exports = function (app, serveV4) { // Use gzip compression if (process.env.NODE_ENV === 'production') { - // Force HTTPS on stackedit.io app.all('*', function(req, res, next) { + // Force HTTPS on stackedit.io if (req.headers.host === 'stackedit.io' && !req.secure && req.headers['x-forwarded-proto'] !== 'https') { return res.redirect('https://stackedit.io' + req.url); } - /\.(eot|ttf|woff|svg)$/.test(req.url) && res.header('Access-Control-Allow-Origin', '*'); + // Enable CORS for fonts + if (/\.(eot|ttf|woff|svg)$/.test(req.url)) { + res.header('Access-Control-Allow-Origin', '*'); + } next(); }); @@ -18,10 +21,12 @@ module.exports = function (app) { } app.get('/oauth2/githubToken', require('./github').githubToken); - app.post('/pdfExport', require('stackedit/app/pdf').export); - app.post('/sshPublish', require('stackedit/app/ssh').publish); - app.post('/picasaImportImg', require('stackedit/app/picasa').importImg); - app.get('/downloadImport', require('stackedit/app/download').importPublic); + if (serveV4) { + app.post('/pdfExport', require('../stackedit_v4/app/pdf').export); + app.post('/sshPublish', require('../stackedit_v4/app/ssh').publish); + app.post('/picasaImportImg', require('../stackedit_v4/app/picasa').importImg); + app.get('/downloadImport', require('../stackedit_v4/app/download').importPublic); + } // Serve callback.html in /app app.get('/oauth2/callback', function(req, res) { @@ -30,29 +35,35 @@ module.exports = function (app) { // Serve static resources if (process.env.NODE_ENV === 'production') { - // Serve landing.html in / - app.get('/', function(req, res) { - res.sendFile(require.resolve('stackedit/views/landing.html')); - }); - // Serve editor.html in /viewer - app.get('/editor', function(req, res) { - res.sendFile(require.resolve('stackedit/views/editor.html')); - }); - // Serve viewer.html in /viewer - app.get('/viewer', function(req, res) { - res.sendFile(require.resolve('stackedit/views/viewer.html')); - }); + if (serveV4) { + // Serve landing.html in / + app.get('/', function(req, res) { + res.sendFile(require.resolve('../stackedit_v4/views/landing.html')); + }); + // Serve editor.html in /viewer + app.get('/editor', function(req, res) { + res.sendFile(require.resolve('../stackedit_v4/views/editor.html')); + }); + // Serve viewer.html in /viewer + app.get('/viewer', function(req, res) { + res.sendFile(require.resolve('../stackedit_v4/views/viewer.html')); + }); + } + // Serve index.html in /app app.get('/app', function(req, res) { res.sendFile(path.join(__dirname, '../dist/index.html')); }); - app.use(serveStatic(path.join(__dirname, '../dist'))); // v5 - app.use(serveStatic(path.dirname(require.resolve('stackedit/public/cache.manifest')))); // v4 + app.use(serveStatic(path.join(__dirname, '../dist'))); - // Error 404 - app.use(function(req, res) { - res.status(404).sendFile(require.resolve('stackedit/views/error_404.html')); - }); + if (serveV4) { + app.use(serveStatic(path.dirname(require.resolve('../stackedit_v4/public/cache.manifest')))); + + // Error 404 + app.use(function(req, res) { + res.status(404).sendFile(require.resolve('../stackedit_v4/views/error_404.html')); + }); + } } }; diff --git a/src/services/localDbSvc.js b/src/services/localDbSvc.js index 6bac8ad0..c0c4f781 100644 --- a/src/services/localDbSvc.js +++ b/src/services/localDbSvc.js @@ -98,15 +98,14 @@ const localDbSvc = { connection: new Connection(), /** - * Return a promise that is resolved once the synchronization between the store and the localDb - * is finished. Effectively, open a transaction, then read and apply all changes from the DB - * since the previous transaction, then write all the changes from the store. + * Return a promise that will be resolved once the synchronization between the store and the + * localDb will be finished. Effectively, open a transaction, then read and apply all changes + * from the DB since the previous transaction, then write all the changes from the store. */ sync() { return new Promise((resolve, reject) => { - const storeItemMap = { ...store.getters.allItemMap }; this.connection.createTx((tx) => { - this.readAll(storeItemMap, tx, () => { + this.readAll(tx, (storeItemMap) => { this.writeAll(storeItemMap, tx); if (!store.state.ready) { store.commit('setReady'); @@ -120,7 +119,7 @@ const localDbSvc = { /** * Read and apply all changes from the DB since previous transaction. */ - readAll(storeItemMap, tx, cb) { + readAll(tx, cb) { let lastTx = this.lastTx; const dbStore = tx.objectStore(dbStoreName); const index = dbStore.index('tx'); @@ -142,6 +141,7 @@ const localDbSvc = { changes.push(item); cursor.continue(); } else { + const storeItemMap = { ...store.getters.allItemMap }; changes.forEach((item) => { this.readDbItem(item, storeItemMap); // If item is an old delete marker, remove it from the DB @@ -150,7 +150,7 @@ const localDbSvc = { } }); this.lastTx = lastTx; - cb(); + cb(storeItemMap); } }; }, diff --git a/src/store/modules/data.js b/src/store/modules/data.js index a9666285..69c832bd 100644 --- a/src/store/modules/data.js +++ b/src/store/modules/data.js @@ -29,17 +29,12 @@ module.mutations.setItem = (state, value) => { : value.data; const item = { ...emptyItem, - ...value, data, - hash: Date.now(), }; - if (item.id === 'settings' || item.id === 'templates') { - // Use a real hash for synced types - item.hash = utils.hash(utils.serializeObject({ - ...item, - hash: undefined, - })); - } + item.hash = utils.hash(utils.serializeObject({ + ...item, + hash: undefined, + })); Vue.set(state.itemMap, item.id, item); };