2018-10-08 09:49:57 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function createLivePreview() {
|
2018-10-08 10:38:01 +00:00
|
|
|
let data;
|
2018-10-08 09:49:57 +00:00
|
|
|
let previewer;
|
2018-10-08 10:38:01 +00:00
|
|
|
let hidden;
|
|
|
|
let node;
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
node = $('#preview-label');
|
|
|
|
if (hidden !== undefined) {
|
|
|
|
node.classList.toggle('hidden', hidden);
|
|
|
|
}
|
|
|
|
}, {once: true});
|
|
|
|
prefs.subscribe(['editor.livePreview'], (key, value) => {
|
|
|
|
if (value && data && data.id && data.enabled) {
|
|
|
|
previewer = createPreviewer;
|
|
|
|
previewer.update(data);
|
|
|
|
}
|
|
|
|
if (!value && previewer) {
|
|
|
|
previewer.disconnect();
|
|
|
|
previewer = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return {update, show};
|
2018-10-08 09:49:57 +00:00
|
|
|
|
2018-10-08 10:38:01 +00:00
|
|
|
function show(state) {
|
|
|
|
if (hidden === !state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hidden = !state;
|
|
|
|
if (node) {
|
|
|
|
node.classList.toggle('hidden', hidden);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function update(_data) {
|
|
|
|
data = _data;
|
2018-10-08 09:49:57 +00:00
|
|
|
if (!previewer) {
|
|
|
|
if (!data.id || !data.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
previewer = createPreviewer();
|
|
|
|
}
|
|
|
|
previewer.update(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createPreviewer() {
|
|
|
|
const port = chrome.runtime.connect({
|
|
|
|
name: 'livePreview'
|
|
|
|
});
|
|
|
|
port.onDisconnet.addListener(err => {
|
|
|
|
throw err;
|
|
|
|
});
|
2018-10-08 10:38:01 +00:00
|
|
|
return {update, disconnect};
|
2018-10-08 09:49:57 +00:00
|
|
|
|
|
|
|
function update(data) {
|
|
|
|
port.postMessage(data);
|
|
|
|
}
|
2018-10-08 10:38:01 +00:00
|
|
|
|
|
|
|
function disconnect() {
|
|
|
|
port.disconnect();
|
|
|
|
}
|
2018-10-08 09:49:57 +00:00
|
|
|
}
|
|
|
|
}
|