Skip to content

Commit

Permalink
feat: Manual refresh for preview. (#237)
Browse files Browse the repository at this point in the history
Allows users to manually refresh the preview.

fixes #133
  • Loading branch information
Zoramite committed Aug 13, 2021
1 parent b59521a commit a6fd369
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/ts/editor/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ export const EVENT_ONBOARDING_UPDATE = 'live.onboarding.update';

/**
* Custom event name for when the editor wants to refresh the file.
*
* Can be used to clear specific caches when a refresh occurs.
*/
export const EVENT_REFRESH_FILE = 'live.refresh.file';

/**
* Custom event name for when the editor wants to refresh the preview.
*/
export const EVENT_REFRESH_PREVIEW = 'live.refresh.preview';

/**
* Custom event name for triggering a render.
*/
Expand Down
27 changes: 18 additions & 9 deletions src/ts/editor/ui/parts/preview/frame.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {BasePart, UiPartComponent, UiPartConfig} from '..';
import {EVENT_FILE_SAVE_COMPLETE, EVENT_REFRESH_PREVIEW} from '../../../events';
import {TemplateResult, classMap, html, styleMap} from '@blinkk/selective-edit';

import {DataStorage} from '../../../../utility/dataStorage';
import {DeviceData} from '../../../api';
import {EVENT_FILE_SAVE_COMPLETE} from '../../../events';
import {EditorState} from '../../../state';

/**
Expand Down Expand Up @@ -44,14 +44,12 @@ export class PreviewFramePart extends BasePart implements UiPartComponent {

// Watch for a save file event and reload the iframe.
document.addEventListener(EVENT_FILE_SAVE_COMPLETE, () => {
// Find and reload the iframe if it is visible.
const iframe = document.querySelector('.le__part__preview__frame iframe');
if (iframe) {
// Needs to be manually set in JS since the html src does not change the
// value and will not trigger a refresh like it will in JS.
(iframe as HTMLIFrameElement).src =
this.config.state.file?.url || (iframe as HTMLIFrameElement).src;
}
this.refresh();
});

// Watch for preview refresh event and reload the iframe.
document.addEventListener(EVENT_REFRESH_PREVIEW, () => {
this.refresh();
});
}

Expand Down Expand Up @@ -138,6 +136,17 @@ export class PreviewFramePart extends BasePart implements UiPartComponent {
};
}

refresh() {
// Find and reload the iframe if it is visible.
const iframe = document.querySelector('.le__part__preview__frame iframe');
if (iframe) {
// Needs to be manually set in JS since the html src does not change the
// value and will not trigger a refresh like it will in JS.
(iframe as HTMLIFrameElement).src =
this.config.state.file?.url || (iframe as HTMLIFrameElement).src;
}
}

template(device?: DeviceData, isRotated?: boolean): TemplateResult {
const sizing = this.getIframeSize(device, isRotated);

Expand Down
24 changes: 22 additions & 2 deletions src/ts/editor/ui/parts/preview/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {TemplateResult, classMap, html, repeat} from '@blinkk/selective-edit';
import {DataStorage} from '../../../../utility/dataStorage';
import {DeviceData} from '../../../api';
import {EditorState} from '../../../state';
import {EVENT_REFRESH_PREVIEW} from '../../../events';

const STORAGE_DEVICE_KEY = 'live.preview.device';
const STORAGE_DEVICE_MODE_KEY = 'live.preview.isDeviceMode';
Expand Down Expand Up @@ -123,8 +124,8 @@ export class PreviewToolbarPart extends BasePart implements UiPartComponent {
</div>`
: html`<div class="le__part__preview__toolbar__label">Preview</div>`}
<div class="le__part__preview__toolbar__icons">
${this.templateIconDeviceRotate()} ${this.templateIconDeviceMode()}
${this.templateIconBreakout()}
${this.templateIconRefresh()} ${this.templateIconDeviceRotate()}
${this.templateIconDeviceMode()} ${this.templateIconBreakout()}
</div>
</div>`;
}
Expand Down Expand Up @@ -225,4 +226,23 @@ export class PreviewToolbarPart extends BasePart implements UiPartComponent {
<span class="material-icons">screen_rotation</span>
</div>`;
}

templateIconRefresh(): TemplateResult {
if (!this.config.state.file?.url) {
return html``;
}

return html`<div
class="le__clickable le__tooltip--top"
data-tip="Refresh preview"
@click=${() => {
// Notify that the preview refresh is happening.
document.dispatchEvent(new CustomEvent(EVENT_REFRESH_PREVIEW));
this.render();
}}
>
<span class="material-icons">refresh</span>
</div>`;
}
}

0 comments on commit a6fd369

Please sign in to comment.