Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
fix: do not connect to ws server from html export (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed May 25, 2018
1 parent 7962ad9 commit a7c2121
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions src/preview/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,39 +90,54 @@ function main(): void {
const initialData = getInitialData();
const store = PreviewStore.from(initialData ? initialData.data : undefined);

const connection = new WebSocket(`ws://${window.location.host}`);
const connection =
store.mode === PreviewDocumentMode.Live
? new WebSocket(`ws://${window.location.host}`)
: undefined;

const onElementClick = (e: MouseEvent, payload) => {
if (!connection) {
return;
}

e.preventDefault();
connection.send(
JSON.stringify({
type: PreviewMessageType.SelectElement,
id: uuid.v4(),
payload
})
);
};

const onOutsideClick = e => {
if (!connection) {
return;
}

e.preventDefault();

connection.send(
JSON.stringify({
type: PreviewMessageType.UnselectElement,
id: uuid.v4()
})
);
};

const render = () => {
renderer.render({
getChildren: createChildrenGetter(store),
getComponent: createComponentGetter(store),
getProperties: createPropertiesGetter(store),
getSlots: createSlotGetter(store),
onElementClick: (e: MouseEvent, payload) => {
e.preventDefault();
connection.send(
JSON.stringify({
type: PreviewMessageType.SelectElement,
id: uuid.v4(),
payload
})
);
},
onOutsideClick: e => {
e.preventDefault();

connection.send(
JSON.stringify({
type: PreviewMessageType.UnselectElement,
id: uuid.v4()
})
);
},
onElementClick,
onOutsideClick,
store
});
};

if (store.mode === PreviewDocumentMode.Live) {
if (connection) {
listen(store, connection, {
onReplacement(): void {
render();
Expand Down

0 comments on commit a7c2121

Please sign in to comment.