/
preview.ts
182 lines (158 loc) · 5.58 KB
/
preview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import {BasePart, LazyUiParts, UiPartComponent, UiPartConfig} from '.';
import {DeviceData, EditorPreviewSettings, WorkspaceData} from '../../api';
import {EditorState, StatePromiseKeys} from '../../state';
import {PreviewFramePart, PreviewFramePartConfig} from './preview/frame';
import {PreviewToolbarPart, PreviewToolbarPartConfig} from './preview/toolbar';
import {TemplateResult, classMap, html} from '@blinkk/selective-edit';
import {DataStorage} from '../../../utility/dataStorage';
import {interpolatePreviewUrl} from '../../preview';
import {templateLoading} from '../../template';
export interface PreviewPartConfig extends UiPartConfig {
/**
* State class for working with editor state.
*/
state: EditorState;
/**
* Storage class for working with settings.
*/
storage: DataStorage;
}
export class PreviewPart extends BasePart implements UiPartComponent {
config: PreviewPartConfig;
device?: DeviceData;
parts: LazyUiParts;
loginWindow?: Window | null;
loginTimer?: number;
constructor(config: PreviewPartConfig) {
super();
this.config = config;
this.parts = new LazyUiParts();
this.parts.register('frame', PreviewFramePart, {
state: this.config.state,
storage: this.config.storage,
} as PreviewToolbarPartConfig);
this.parts.register('toolbar', PreviewToolbarPart, {
state: this.config.state,
storage: this.config.storage,
} as PreviewFramePartConfig);
}
classesForPart(): Record<string, boolean> {
return {
le__panel: true,
le__part__preview: true,
'le__part__preview--device_mode':
(this.partToolbar.isDeviceMode || false) &&
Boolean(this.partToolbar.device),
};
}
get isExpanded(): boolean {
return this.partToolbar.isExpanded || false;
}
handleLoginClick() {
if (this.loginTimer) {
clearInterval(this.loginTimer);
}
const baseUrl = interpolatePreviewUrl(
this.config.state.project?.preview as EditorPreviewSettings,
this.config.state.workspace as WorkspaceData
);
// Open the base url in a new window to let it trigger any auth
// requirements before we try to load the preview config again.
this.loginWindow = window.open(
baseUrl,
'editor-preview-login',
'resizable,scrollbars,status,width=600,height=400'
);
// Use interval to check for when the window is closed to reload
// the preview config since it is on a different domain.
this.loginTimer = setInterval(() => {
if (this.loginWindow?.closed) {
clearInterval(this.loginTimer);
this.handleRefreshClick();
}
}, 1500) as unknown as number;
}
handleRefreshClick() {
// Reset the preview config before reloading it shows the loading UI.
this.config.state.previewConfig = undefined;
this.config.state.getPreviewConfig();
this.render();
}
get partFrame(): PreviewFramePart {
return this.parts.get('frame') as PreviewFramePart;
}
get partToolbar(): PreviewToolbarPart {
return this.parts.get('toolbar') as PreviewToolbarPart;
}
template(): TemplateResult {
const pieces: Array<TemplateResult> = [];
pieces.push(this.partToolbar.template());
if (this.config.state.file?.url) {
pieces.push(
this.partFrame.template(
this.partToolbar.device,
this.partToolbar.isRotated
)
);
} else if (this.config.state.previewConfig === undefined) {
pieces.push(this.templatePreviewConfigLoading());
} else if (this.config.state.previewConfig === null) {
// Check if we tried to load it and it failed.
if (this.config.state.project?.preview?.baseUrl) {
pieces.push(this.templatePreviewConfigLoadingError());
} else {
pieces.push(this.templatePreviewNotConfigured());
}
} else {
pieces.push(this.templatePreviewNotAvailable());
}
return html`<div class=${classMap(this.classesForPart())}>${pieces}</div>`;
}
templatePreviewConfigLoading(): TemplateResult {
return templateLoading({}, html`<div>Searching for file preview.</div>`);
}
templatePreviewConfigLoadingError(): TemplateResult {
return html`<div class="le__part__preview__message">
<h3>Trouble connecting to preview server</h3>
<p>
You may need to sign in to the preview server, or it may be offline.
Sign into the preview server then reload this page.
</p>
<div>
<button
class="le__button le__button--outline le__button--primary"
@click=${this.handleLoginClick.bind(this)}
>
Sign in to preview server
</button>
<button class="le__button" @click=${this.handleRefreshClick.bind(this)}>
Reload
</button>
</div>
</div>`;
}
templatePreviewNotAvailable(): TemplateResult {
// When waiting for the file to load do not show anything
// since the file load is already showing.
if (
!this.config.state.loadingFilePath &&
this.config.editor.state.inProgress(StatePromiseKeys.GetFile)
) {
return html``;
}
return html`<div class="le__part__preview__message">
<h3>No preview found for file</h3>
<p>This file doesn't have a preview associated with it.</p>
</div>`;
}
templatePreviewNotConfigured(): TemplateResult {
// TODO: Link to the documentation on setting up a preview server.
return html`<div class="le__part__preview__message">
<h3>No preview server</h3>
<p>
A preview server has not yet been set up for this project. Ask a
developer to set up a preview server for reactive previews.
</p>
</div>`;
}
}