-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathEmbeddingCodeModal.tsx
More file actions
187 lines (168 loc) · 5.57 KB
/
EmbeddingCodeModal.tsx
File metadata and controls
187 lines (168 loc) · 5.57 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
import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { getSourceURL } from "../../../../utils/embeddedCodeUtils";
import ButtonLikeAnchor from "../../../shared/ButtonLikeAnchor";
import BaseButton from "../../../shared/BaseButton";
import { ParseKeys } from "i18next";
/**
* This component renders the embedding code modal
*/
const EmbeddingCodeModal = ({
eventId,
}: {
eventId: string
}) => {
const { t } = useTranslation();
const [textAreaContent, setTextAreaContent] = useState("");
const [sourceURL, setSourceURL] = useState("");
const [currentSize, setCurrentSize] = useState("0x0");
const [showCopySuccess, setCopySuccess] = useState(false);
useEffect(() => {
const fetchData = async () => {
// get source url
const sourceURL = await getSourceURL();
setSourceURL(sourceURL);
};
fetchData();
}, []);
const copy = () => {
const copyText = document.getElementById("social_embed-textarea");
if (copyText) {
const text = copyText.innerText;
navigator.clipboard.writeText(text);
setCopySuccess(true);
}
};
const updateTextArea = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
// chosen frame size
const frameSize = e.currentTarget.textContent;
if (!frameSize) {
return;
}
// buttons containing possible frame sizes
const embedSizeButtons = document.getElementsByClassName("embedSizeButton");
// iterate through embedSizeButtons and mark the chosen size
if (frameSize) {
for (let i = 0; i < embedSizeButtons.length; i++) {
if (frameSize === embedSizeButtons[i].id) {
embedSizeButtons[i].classList.add("embedSizeButtonSelected");
} else {
embedSizeButtons[i].classList.remove("embedSizeButtonSelected");
}
}
}
// split frameSize to be used in iFrameString
const size = frameSize.split("x");
// build whole url
const url = sourceURL + "/play/" + eventId;
// code displayed in text area containing the iFrame to copy
const iFrameString = `<iframe allowfullscreen src="${url}"
style="border: 0; margin 0;" name="Player" scrolling="no"
width="${size[0]}" height="${size[1]}"></iframe>`
.replace(/\s\s+/g, " ");
// set state with new inputs
setTextAreaContent(iFrameString);
setCurrentSize(frameSize);
setCopySuccess(false);
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLElement>) => {
if ((e.ctrlKey || e.metaKey) && e.key === "c") {
e.preventDefault();
copy();
}
};
return (
<>
{/* embed size buttons */}
<div className="list-obj">
<div className="obj-container">
<span>
{t("EMBEDDING_CODE.DESCRIPTION")}
</span>
</div>
</div>
<div className="embedded-code-boxes">
<ButtonLikeAnchor
id="620x349"
className="embedSizeButton size_620x349"
onClick={e => updateTextArea(e)}
tooltipText={t("EMBEDDING_CODE.GENERATE_TOOLTIP", { size: "620x349" }) as ParseKeys}
aria-label={t("EMBEDDING_CODE.GENERATE_TOOLTIP", { size: "620x349" }) as ParseKeys}
>
<span className="span-embedded-code">620x349</span>
</ButtonLikeAnchor>
<ButtonLikeAnchor
id="540x304"
className="embedSizeButton size_540x304"
onClick={e => updateTextArea(e)}
tooltipText={t("EMBEDDING_CODE.GENERATE_TOOLTIP", { size: "540x304" }) as ParseKeys}
aria-label={t("EMBEDDING_CODE.GENERATE_TOOLTIP", { size: "540x304" }) as ParseKeys}
>
<span className="span-embedded-code">540x304</span>
</ButtonLikeAnchor>
<ButtonLikeAnchor
id="460x259"
className="embedSizeButton size_460x259"
onClick={e => updateTextArea(e)}
tooltipText={t("EMBEDDING_CODE.GENERATE_TOOLTIP", { size: "460x259" }) as ParseKeys}
aria-label={t("EMBEDDING_CODE.GENERATE_TOOLTIP", { size: "460x259" }) as ParseKeys}
>
<span className="span-embedded-code">460x259</span>
</ButtonLikeAnchor>
<ButtonLikeAnchor
id="380x214"
className="embedSizeButton size_380x214"
onClick={e => updateTextArea(e)}
tooltipText={t("EMBEDDING_CODE.GENERATE_TOOLTIP", { size: "380x214" }) as ParseKeys}
aria-label={t("EMBEDDING_CODE.GENERATE_TOOLTIP", { size: "380x214" }) as ParseKeys}
>
<span className="span-embedded-code">380x214</span>
</ButtonLikeAnchor>
<ButtonLikeAnchor
id="300x169"
className="embedSizeButton size_300x169"
onClick={e => updateTextArea(e)}
tooltipText={t("EMBEDDING_CODE.GENERATE_TOOLTIP", { size: "300x169" }) as ParseKeys}
aria-label={t("EMBEDDING_CODE.GENERATE_TOOLTIP", { size: "300x169" }) as ParseKeys}
>
<span className="span-embedded-code">300x169</span>
</ButtonLikeAnchor>
</div>
<span id="id_video" className="embedded-code-no-visible">
{eventId}
</span>
{/* text area containing current iFrame code to copy*/}
<div className="embedded-code-video">
<code
id="social_embed-textarea"
className="embedded-code-textarea"
aria-label={t("EMBEDDING_CODE.EMBEDD_CODE_TEXTAREA_ARIA")}
tabIndex={0}
onKeyDown={handleKeyDown}
>
{textAreaContent}
</code>
</div>
{/* copy confirmation */}
{showCopySuccess && (
<div className="copyConfirm" role="alert">
<span id="copy_confirm_pre">
{t("CONFIRMATIONS.EMBEDDING_CODE", { size: currentSize })}
</span>
</div>
)}
{/* copy button */}
<div className="embedded-code-copy-to-clipboard">
<BaseButton
className="cancel-btn"
onClick={() => copy()}
tooltipText="EMBEDDING_CODE.COPY_BUTTON_TOOLTIP"
aria-label={t("EMBEDDING_CODE.COPY_BUTTON_TOOLTIP")}
>
{t("COPY")}
</BaseButton>
</div>
</>
);
};
export default EmbeddingCodeModal;