2018-03-12 00:45:54 +00:00
|
|
|
import cledit from './cledit';
|
|
|
|
import store from '../store';
|
|
|
|
import utils from './utils';
|
|
|
|
import editorSvc from './editorSvc';
|
2018-05-04 18:07:28 +00:00
|
|
|
import fileSvc from './fileSvc';
|
2018-03-12 00:45:54 +00:00
|
|
|
|
|
|
|
const origin = utils.queryParams.origin;
|
|
|
|
const fileName = utils.queryParams.fileName;
|
|
|
|
const contentText = utils.queryParams.contentText;
|
|
|
|
const contentProperties = utils.queryParams.contentProperties;
|
|
|
|
|
|
|
|
export default {
|
2018-03-15 13:51:39 +00:00
|
|
|
setReady() {
|
|
|
|
if (origin && window.parent) {
|
2018-04-16 00:22:29 +00:00
|
|
|
// Wait for the editor to init
|
|
|
|
setTimeout(() => window.parent.postMessage({ type: 'ready' }, origin), 1);
|
2018-03-15 13:51:39 +00:00
|
|
|
}
|
|
|
|
},
|
2018-03-14 00:42:26 +00:00
|
|
|
closed: false,
|
2018-03-12 00:45:54 +00:00
|
|
|
close() {
|
2018-03-14 00:42:26 +00:00
|
|
|
if (!this.closed && origin && window.parent) {
|
2018-03-12 00:45:54 +00:00
|
|
|
window.parent.postMessage({ type: 'close' }, origin);
|
|
|
|
}
|
2018-03-14 00:42:26 +00:00
|
|
|
this.closed = true;
|
2018-03-12 00:45:54 +00:00
|
|
|
},
|
|
|
|
init() {
|
|
|
|
if (!origin || !window.parent) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
store.commit('setLight', true);
|
2018-03-15 13:51:39 +00:00
|
|
|
|
2018-05-04 18:07:28 +00:00
|
|
|
return fileSvc.createFile({
|
2018-03-15 13:51:39 +00:00
|
|
|
name: fileName || utils.getHostname(origin),
|
|
|
|
text: contentText || '\n',
|
2018-03-14 00:42:26 +00:00
|
|
|
properties: contentProperties,
|
|
|
|
parentId: 'temp',
|
2018-05-04 18:07:28 +00:00
|
|
|
}, true)
|
2018-03-12 00:45:54 +00:00
|
|
|
.then((file) => {
|
|
|
|
const fileItemMap = store.state.file.itemMap;
|
|
|
|
|
|
|
|
// Sanitize file creations
|
2018-03-14 00:42:26 +00:00
|
|
|
const lastCreated = {};
|
|
|
|
Object.entries(store.getters['data/lastCreated']).forEach(([id, createdOn]) => {
|
|
|
|
if (fileItemMap[id] && fileItemMap[id].parentId === 'temp') {
|
|
|
|
lastCreated[id] = createdOn;
|
2018-03-12 00:45:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-14 00:42:26 +00:00
|
|
|
// Track file creation from other site
|
|
|
|
lastCreated[file.id] = {
|
2018-03-12 00:45:54 +00:00
|
|
|
created: Date.now(),
|
|
|
|
};
|
|
|
|
|
2018-03-14 00:42:26 +00:00
|
|
|
// Keep only the last 10 temp files created by other sites
|
|
|
|
Object.entries(lastCreated)
|
|
|
|
.sort(([, createdOn1], [, createdOn2]) => createdOn2 - createdOn1)
|
2018-03-12 00:45:54 +00:00
|
|
|
.splice(10)
|
2018-03-14 00:42:26 +00:00
|
|
|
.forEach(([id]) => {
|
|
|
|
delete lastCreated[id];
|
2018-05-04 18:07:28 +00:00
|
|
|
fileSvc.deleteFile(id);
|
2018-03-12 00:45:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Store file creations and open the file
|
2018-03-14 00:42:26 +00:00
|
|
|
store.dispatch('data/setLastCreated', lastCreated);
|
2018-03-12 00:45:54 +00:00
|
|
|
store.commit('file/setCurrentId', file.id);
|
|
|
|
|
|
|
|
const onChange = cledit.Utils.debounce(() => {
|
|
|
|
const currentFile = store.getters['file/current'];
|
|
|
|
if (currentFile.id !== file.id) {
|
|
|
|
// Close editor if file has changed for some reason
|
|
|
|
this.close();
|
2018-03-14 00:42:26 +00:00
|
|
|
} else if (!this.closed && editorSvc.previewCtx.html != null) {
|
2018-03-12 00:45:54 +00:00
|
|
|
const content = store.getters['content/current'];
|
|
|
|
const properties = utils.computeProperties(content.properties);
|
|
|
|
window.parent.postMessage({
|
|
|
|
type: 'fileChange',
|
2018-03-15 13:51:39 +00:00
|
|
|
payload: {
|
2018-03-12 00:45:54 +00:00
|
|
|
id: file.id,
|
|
|
|
name: currentFile.name,
|
|
|
|
content: {
|
2018-03-15 13:51:39 +00:00
|
|
|
text: content.text.slice(0, -1), // Remove trailing LF
|
2018-03-12 00:45:54 +00:00
|
|
|
properties,
|
|
|
|
yamlProperties: content.properties,
|
|
|
|
html: editorSvc.previewCtx.html,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, origin);
|
|
|
|
}
|
|
|
|
}, 25);
|
|
|
|
|
|
|
|
// Watch preview refresh and file name changes
|
|
|
|
editorSvc.$on('previewCtx', onChange);
|
2018-03-15 13:51:39 +00:00
|
|
|
store.watch(() => store.getters['file/current'].name, onChange);
|
2018-03-12 00:45:54 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|