-
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathPreviewBrowser.vue
More file actions
216 lines (198 loc) · 4.94 KB
/
Copy pathPreviewBrowser.vue
File metadata and controls
216 lines (198 loc) · 4.94 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<template>
<div class="k-preview-browser">
<header v-if="label" class="k-preview-browser-header">
<k-headline class="k-preview-headline">
<k-icon type="git-branch" />
{{ label }}
</k-headline>
<k-button-group>
<template v-if="mode === 'changes'">
<p v-if="hasDiff === false" class="k-preview-browser-message">
{{ $t("lock.unsaved.empty") }}
</p>
<k-form-controls
v-else
:editor="editor"
:has-diff="hasDiff"
:is-locked="isLocked"
:is-processing="isProcessing"
:modified="modified"
size="xs"
@discard="$emit('discard', $event)"
@submit="$emit('submit', $event)"
/>
</template>
<k-button
v-if="mode === 'form'"
:aria-checked="isPinned"
:title="$t('preview.browser.pin')"
:theme="isPinned ? 'info' : 'passive'"
:variant="isPinned ? 'filled' : 'none'"
icon="pushpin"
role="switch"
size="sm"
@click="$emit('pin')"
/>
<k-button
v-if="open"
:link="open"
icon="open"
size="xs"
target="_blank"
@click="$emit('open')"
/>
</k-button-group>
</header>
<iframe ref="browser" :src="src" @load="onLoad" />
</div>
</template>
<script>
import { props } from "@/components/Forms/FormControls.vue";
export default {
mixins: [props],
props: {
isPinned: Boolean,
label: String,
src: String,
mode: String,
open: String
},
emits: ["discard", "navigate", "open", "pin", "scroll", "submit"],
computed: {
window() {
return this.$refs.browser.contentWindow;
}
},
methods: {
/**
* Handle link clicks inside the iframe
*/
onClick(e) {
const link = e.target.closest("a");
if (!link) {
return;
}
if (!link.href || link.onclick) {
return;
}
// open external links and Panel links in new tab
if (
link.href.startsWith(location.origin) === false ||
link.href.startsWith(this.$panel.urls.panel) === true
) {
link.target = "_blank";
return true;
}
// catch internal links and emit navigate event
e.preventDefault(e);
if (this.isPinned) {
// we only want to refresh the browser for the target
this.$emit("navigate", { browser: link.href });
} else {
// we want to refresh the whole view for the target
this.$emit("navigate", { view: link.href });
}
},
onLoad() {
const document = this.$refs.browser.contentDocument;
// if the browser got redirected during load
// navigate to the proper preview URL for this new URL
// (but only if the new URL doesn't already contain _version and _token)
if (this.src !== document.URL) {
const url = new URL(document.URL);
if (
url.searchParams.has("_token") === false ||
url.searchParams.has("_version") === false
) {
return this.$emit("navigate", { browser: url });
}
}
// attach event listeners to all links inside the iframe
document.addEventListener("click", this.onClick);
for (const link of document.querySelectorAll("a")) {
link.addEventListener("click", this.onClick);
}
document.addEventListener("scroll", (e) => this.$emit("scroll", e));
},
/**
* Refresh the iframe
* (e.g. for content updates)
*/
reload() {
this.window.location.reload();
},
/**
* Restore an iframe URL and scroll position
* (only when iframe browser is pinned)
*/
restore({ src, scroll }) {
// if the browser isn't pinned, we keep it as loaded with the view
if (!this.isPinned) {
return;
}
// restore scroll position once the iframe finished loading
this.$refs.browser.addEventListener(
"load",
() => this.window.scrollTo(0, scroll),
{ once: true }
);
// load restored URL in iframe
this.$refs.browser.src = src;
},
/**
* Returns the current iframe URL and scroll position,
* so that these can be restored, if needed
*/
store() {
return {
src: this.$refs.browser.src,
scroll: this.window.scrollY
};
}
}
};
</script>
<style>
:root {
--preview-browser-color-background: var(--input-color-back);
}
.k-preview-browser {
container-type: inline-size;
display: flex;
flex-direction: column;
border-radius: var(--rounded-lg);
box-shadow: var(--shadow-xl);
background: var(--preview-browser-color-background);
overflow: hidden;
border: 1px solid var(--color-border);
}
.k-preview-browser-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: var(--spacing-6);
background: var(--preview-browser-color-background);
border-bottom: 1px solid var(--color-border);
color: var(--color-text);
padding-inline: var(--spacing-2);
height: var(--input-height);
}
.k-preview-browser-header .k-preview-headline {
font-size: var(--text-xs);
}
.k-preview-browser-message {
font-size: var(--text-xs);
display: flex;
margin-inline-end: var(--spacing-1);
color: var(--color-text-dimmed);
}
.k-preview-browser iframe {
width: 100%;
flex-grow: 1;
}
@container (max-width: 30rem) {
.k-preview-browser-message {
display: none;
}
}
</style>