-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.html
155 lines (141 loc) · 3.81 KB
/
ui.html
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
<!-- Plugin UI HTML -->
<h3>TV Show Preview Tool</h3>
<p>TV Show</p>
<input id="tv-show" value="Golden Girls" autofocus>
<div class="buttons">
<div class="buttons-section-1">
<button class="constructive" id="create">Apply</button>
<button class="destructive" id="cancel">Cancel</button>
</div>
<div class="buttons-section-2">
<button class="secondary" id="random">Random</button>
</div>
</div>
<div>
<p>Supported layer names: #ShowName, #ShowDescription, #Genre1, #Genre2, #RunTime, #Status, #Rating, #CoverImage</p>
</div>
<!-- Plugin UI Javscript -->
<script>
const randomShow = () => {
const randomShows = [
"Friends",
"Lost",
"Big Bang Theory",
"Succession",
"Stranger Things",
"Foundation",
"Curb your enthusiasm",
"Seinfeld",
"Squid game",
"Sopranos",
"Westworld",
"True Detective",
"Deadwood",
"Star Trek",
"Black Mirror",
"The Office",
"Parks and Recreation"
]
return randomShows[Math.round((randomShows.length - 1) * Math.random())]
}
document.getElementById("create").onclick = () => {
initiatePopulation(document.getElementById("tv-show").value)
}
document.getElementById('random').onclick = () => {
const show = randomShow()
document.getElementById("tv-show").value = show
initiatePopulation(show)
}
const initiatePopulation = query => {
fetch(`https://api.tvmaze.com/search/shows?q=${query}`)
.then(response => {
response.json().then(results => {
const show = results[0].show
const layerMap = {
"#ShowName": show.name,
"#ShowDescription": show.summary.replace(/<[^>]*>?/gm, ""),
"#Genre1": show.genres[0],
"#Genre2": show.genres[1],
"#RunTime": `${show.averageRuntime}m`,
"#Status": show.status,
"#Rating": show.rating.average ? show.rating.average.toString() : "-",
"#CoverImage": show.image.medium
}
window.parent.postMessage({ pluginMessage: { type: "populateShow", data: layerMap } }, "*")
loadImage(layerMap["#CoverImage"], "#CoverImage")
});
})
}
const loadImage = (imageUrl, layerName) => {
fetch(imageUrl)
.then(response => response.blob())
.then(
blob =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(new Uint8Array(reader.result));
reader.readAsArrayBuffer(blob);
})
)
.then(imageBytes => {
const pluginMessage = {
type: "populateImage",
data: {
imageBytes: imageBytes,
key: layerName
}
}
parent.postMessage({ pluginMessage: pluginMessage }, "*");
});
}
document.getElementById('cancel').onclick = () => {
const pluginMessage = { type: "cancel" }
parent.postMessage({ pluginMessage: pluginMessage }, "*")
}
</script>
<!-- Plugin UI styles -->
<style>
html {
padding: 0px;
margin: 0px;
}
body {
font-family: Arial, Helvetica, sans-serif;
padding: 16px;
margin: 0px;
}
p {
color: rgba(0, 0, 0, 0.54);
text-transform: uppercase;
font-size: 12px;
letter-spacing: 0.5px;
}
input {
padding: 8px 4px;
margin: 8px 0px;
border-radius: 2px;
border: 1px solid rgba(0, 0, 0, 0.2);
width: 100%;
}
button {
padding: 8px 16px;
border-radius: 2px;
border: 0px;
background-color: black;
color: white;
border-radius: 16px;
}
button.destructive {
color: black;
background-color: transparent;
}
button.secondary {
color: black;
background-color: rgba(0, 0, 0, 0.1);
}
.buttons {
margin-top: 20px;
display: flex;
justify-content: space-between;
}
</style>