forked from thepushkarp/craft-tweet-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
129 lines (116 loc) · 4.25 KB
/
index.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
<!DOCTYPE html>
<html>
<body>
<div
style="
width: inherit;
height: inherit;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
"
>
<div style="font-size: 20px; font-weight: bold; font-family: sans-serif">
Characters left
</div>
<svg width="100" height="100">
<circle
cx="50"
cy="50"
r="40"
stroke="#1DA1F2"
stroke-width="3"
fill="none"
stroke-dasharray="251.3274"
stroke-dashoffset="251.3274"
id="circle"
transform="rotate(-90 50 50)"
/>
<text
x="50%"
y="50%"
text-anchor="middle"
alignment-baseline="middle"
font-size="20"
font-weight="bold"
fill="#1DA1F2"
id="count"
font-family="sans-serif"
>
0
</text>
</svg>
<div
style="
position: absolute;
bottom: 0;
right: 0;
margin: 10px;
font-size: 12px;
font-family: sans-serif;
"
>
Tweet Helper made with ❤️ by
<span
style="color: #1da1f2; cursor: pointer"
onclick="openTwitterProfile()"
>
@thepushkarp
</span>
</div>
</div>
<script>
setInterval(async () => {
craft.env.setListener((env) => {
document.body.style.backgroundColor =
env.colorScheme === 'light' ? 'white' : '#202020';
document.body.style.color =
env.colorScheme === 'light' ? '#202020' : 'white';
});
const result = await craft.editorApi.getTextSelection();
if (result.status !== 'success') {
throw new Error(result.message);
}
const selectedText = result.data;
const tweetLength = selectedText.length;
const circle = document.getElementById('circle');
const count = document.getElementById('count');
const MAX_TWEET_LENGTH = 280;
const CIRCUMFERENCE = 251.3274;
const numCharsShortOfMaxLength = MAX_TWEET_LENGTH - tweetLength;
var offset;
count.innerHTML = numCharsShortOfMaxLength;
if (MAX_TWEET_LENGTH - tweetLength <= 0) {
circle.style.stroke = '#F42121';
count.style.fill = '#F42121';
circle.style['stroke-width'] = '4';
} else if (MAX_TWEET_LENGTH - tweetLength <= 20) {
circle.style.stroke = '#FFD400';
count.style.fill = '#FFD400';
circle.style['stroke-width'] = '4';
} else {
circle.style.stroke = '#1DA1F2';
count.style.fill = '#1DA1F2';
circle.style['stroke-width'] = '3';
}
if (numCharsShortOfMaxLength < 0) {
offset = 0;
} else {
offset =
CIRCUMFERENCE -
(tweetLength / MAX_TWEET_LENGTH) * CIRCUMFERENCE;
}
circle.style.strokeDashoffset = offset;
}, 10);
async function openTwitterProfile() {
const result = await craft.editorApi.openURL(
'https://twitter.com/thepushkarp'
);
if (result.status !== 'success') {
throw new Error(result.mess);
}
}
</script>
</body>
</html>