-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedback-form.js
276 lines (219 loc) · 6.89 KB
/
feedback-form.js
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* Feedback form
*/
const openFeedbackButton = document.querySelector("#open-feedback-modal");
const closeFeedbackButton = document.querySelector("#close-feedback-modal");
const feedbackModal = document.querySelector("#feedback-modal");
const feedbackForm = document.querySelector("#feedback-form");
const captureSection = document.querySelector("#capture-section");
const captureButton = document.querySelector("#capture-section button");
const previewSection = document.querySelector("#preview-section");
const previewImage = document.querySelector("#preview-section img");
const deleteScreenshotButton = document.querySelector("#delete-screenshot");
// const isDisplayMediaSupported = () => {
// return Boolean(navigator.mediaDevices?.getDisplayMedia);
// };
const startCapture = async () => {
let captureStream = null;
try {
captureStream = await navigator.mediaDevices.getDisplayMedia({
preferCurrentTab: true,
});
} catch (err) {
console.error(err);
}
return captureStream;
};
const stopCapture = (video) => {
if (!video.srcObject) return;
const tracks = video.srcObject.getTracks();
tracks.forEach((track) => {
track.stop();
});
video.srcObject = null;
};
const createVideoElement = (captureStream) => {
const video = document.createElement("video");
video.autoplay = true;
video.muted = true;
video.playsInline = true;
video.srcObject = captureStream;
video.setAttribute(
"style",
"position:fixed;top:0;left:0;pointer-events:none;visibility:hidden;",
);
return video;
};
const videoToCanvas = (video) => {
const videoTrackSettings = video.srcObject?.getTracks()[0].getSettings();
const canvas = document.createElement("canvas");
canvas.width = videoTrackSettings?.width ?? 0;
canvas.height = videoTrackSettings?.height ?? 0;
const ctx = canvas.getContext("2d");
ctx?.drawImage(video, 0, 0);
return canvas;
};
openFeedbackButton.addEventListener("click", () => {
feedbackModal.showModal();
});
closeFeedbackButton.addEventListener("click", () => {
feedbackModal.close();
});
captureButton.addEventListener("click", async () => {
feedbackModal.close();
const captureStream = await startCapture();
if (!captureStream) {
feedbackModal.showModal();
return;
}
// using video instead of ImageCapture for better browser support
const video = createVideoElement(captureStream);
document.body.appendChild(video);
// workaround to support Safari browsers
await new Promise((r) => {
setTimeout(r, 500);
});
const canvas = videoToCanvas(video);
const screenshot = canvas.toDataURL("image/jpeg", 0.7);
// clean up
stopCapture(video);
canvas.remove();
previewImage.src = screenshot;
editorImage.src = screenshot;
captureSection.classList.add("hidden");
previewSection.classList.remove("hidden");
feedbackModal.showModal();
});
deleteScreenshotButton.addEventListener("click", () => {
previewImage.src = "data:,";
editorImage.src = "data:,";
captureSection.classList.remove("hidden");
previewSection.classList.add("hidden");
});
feedbackForm.addEventListener("submit", (evt) => {
evt.preventDefault();
const formData = new FormData(feedbackForm);
const data = {
datetime: new Date().toISOString(),
userAgent: window.navigator.userAgent,
pageTitle: window.document.title,
pageURL: window.location.href,
description: formData.get("feedback-description"),
screenshot: previewImage.src !== "data:," ? previewImage.src : null,
};
window.alert(
`End of feedback screenshot form flow. Data: ${JSON.stringify(data)}`,
);
feedbackForm.reset();
feedbackModal.close();
});
/**
* Screenshot editor modal
*/
const openEditorButton = document.querySelector("#edit-screenshot");
const closeEditorButton = document.querySelector("#close-editor-modal");
const cancelEditorButton = document.querySelector("#cancel-editor-modal");
const updateScreenshotButton = document.querySelector("#update-screenshot");
const editorModal = document.querySelector("#editor-modal");
const editorImage = document.querySelector("#editor-modal img");
const editorCanvas = document.querySelector("#editor-modal canvas");
let isDown = false;
let startX;
let startY;
let rectWidth;
let rectHeight;
let rectangles = [];
const redraw = () => {
const ctx = editorCanvas.getContext("2d");
ctx.clearRect(0, 0, editorCanvas.width, editorCanvas.height);
rectangles.forEach((rect) => {
ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
});
};
openEditorButton.addEventListener("click", () => {
editorModal.showModal();
feedbackModal.close();
const aspectRatio = editorImage.naturalWidth / editorImage.naturalHeight;
const container = document.querySelector("#canvas-container");
const canvasWidth = container.getBoundingClientRect().width;
const canvasHeight = canvasWidth / aspectRatio;
editorCanvas.width = canvasWidth;
editorCanvas.height = canvasHeight;
});
editorCanvas.addEventListener("mousedown", (evt) => {
evt.preventDefault();
evt.stopPropagation();
const offset = editorCanvas.getBoundingClientRect();
startX = evt.clientX - offset.left;
startY = evt.clientY - offset.top;
isDown = true;
});
editorCanvas.addEventListener("mouseup", (evt) => {
evt.preventDefault();
evt.stopPropagation();
isDown = false;
rectangles.push({
x: startX,
y: startY,
width: rectWidth,
height: rectHeight,
});
});
editorCanvas.addEventListener("mouseout", (evt) => {
evt.preventDefault();
evt.stopPropagation();
if (!isDown) return;
isDown = false;
redraw();
});
editorCanvas.addEventListener("mousemove", (evt) => {
evt.preventDefault();
evt.stopPropagation();
if (!isDown) return;
redraw();
// draw current
const offset = editorCanvas.getBoundingClientRect();
const currentX = evt.clientX - offset.left;
const currentY = evt.clientY - offset.top;
rectWidth = currentX - startX;
rectHeight = currentY - startY;
const ctx = editorCanvas.getContext("2d");
ctx.fillRect(startX, startY, rectWidth, rectHeight);
});
closeEditorButton.addEventListener("click", () => {
editorModal.close();
feedbackModal.showModal();
rectangles = [];
});
cancelEditorButton.addEventListener("click", () => {
editorModal.close();
feedbackModal.showModal();
rectangles = [];
});
updateScreenshotButton.addEventListener("click", () => {
const canvas = document.createElement("canvas");
canvas.width = previewImage.naturalWidth;
canvas.height = previewImage.naturalHeight;
const ctx = canvas.getContext("2d");
// combine
ctx?.drawImage(editorImage, 0, 0);
ctx?.drawImage(
editorCanvas,
0,
0,
editorCanvas.width,
editorCanvas.height,
0,
0,
canvas.width,
canvas.height,
);
const updatedScreenshot = canvas.toDataURL("image/jpeg", 0.7);
previewImage.src = updatedScreenshot;
editorImage.src = updatedScreenshot;
// cleanup
canvas.remove();
editorModal.close();
feedbackModal.showModal();
rectangles = [];
});