-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
218 lines (196 loc) · 4.4 KB
/
main.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
/* eslint-disable no-var */
var css = require('sheetify')
var html = require('nanohtml')
var titleCase = require('capitalize-title')
document.title = 'Capitalize titles in the browser'
var meta = html`
<meta content="width=device-width, initial-scale=1" name="viewport">
`
document.head.appendChild(meta)
var globalStyle = css`
:root {
--color-background: #ecdb83;
--color-border: #fff;
--color-text: #333;
--font-monospace: menlo, monaco, monospace;
--font-serif: 'hoefler text', 'bitstream charter', georgia, serif;
}
* {
box-sizing: border-box;
}
:host {
background-color: var(--color-background);
color: var(--color-text);
font-family: var(--font-serif);
margin: 0;
padding: 1rem;
}
`
document.body.classList.add(`${globalStyle}`)
var formStyle = css`
:host {
margin-left: auto;
margin-right: auto;
max-width: 40em;
}
:host label {
display: block;
margin-bottom: .5rem;
}
:host input,
:host textarea {
-webkit-appearance: none;
border: transparent;
display: block;
font-family: var(--font-monospace);
font-size: 1.25rem;
height: 3rem;
padding-left: .5rem;
padding-right: .5rem;
width: 100%;
}
:host input {
border-radius: .75rem;
margin-bottom: 1rem;
}
:host textarea {
border-bottom-left-radius: .75rem;
border-bottom-right-radius: 0;
border-right: hidden;
border-top-left-radius: .75rem;
border-top-right-radius: 0;
line-height: 2.25;
overflow-y: hidden;
resize: none;
z-index: 1;
}
:host div {
display: flex;
margin-bottom: 2rem;
}
:host button {
background-color: var(--color-background);
border-bottom-right-radius: .75rem;
border-top-right-radius: .75rem;
border: .25rem solid var(--color-border);
color: var(--color-text);
cursor: pointer;
font-family: var(--font-monospace);
font-size: 1rem;
line-height: 1;
margin: 0;
padding: .5rem 1rem;
}
:host summary {
border-radius: .75rem;
}
:host summary:hover {
cursor: pointer;
}
:host ul {
line-height: 1.5;
padding-left: 1rem;
}
:host code {
background-color: #fff;
border-radius: .25rem;
color: var(--color-text);
font-family: var(--font-monospace);
font-size: .875rem;
padding-left: .25rem;
padding-right: .25rem;
white-space: nowrap;
}
`
var headerStyle = css`
:host {
margin-bottom: 4rem;
text-align: center;
}
:host h1 {
font-size: 3rem;
margin-bottom: .5rem;
margin-top: 0;
}
:host h2 {
font-size: 1.75rem;
margin-bottom: 0;
margin-top: 0;
}
`
var footerStyle = css`
:host {
bottom: 1rem;
position: absolute;
}
:host a {
color: var(--color-text);
}
`
var header = html`
<header class=${headerStyle}>
<h1>caps</h1>
<h2><i>Capitalize titles in the browser</i></h2>
</header>
`
var input = html`<input autofocus oninput=${format} onkeyup=${capitalize} type="text">`
var output = html`<textarea readonly></textarea>`
var copyButton = html`<button onclick=${copyOutput}>Copy</button>`
var form = html`
<div class=${formStyle}>
<label>Input:</label>
${input}
<label>Output:</label>
<div>
${output}
${copyButton}
</div>
<details>
<summary>Instructions:</summary>
<ul>
<li>You can use the key binding <code>Ctrl + v</code> to copy the output.</li>
<li>Deselect the current field with <code>Esc</code>.</li>
</ul>
</details>
</div>
`
var footer = html`
<footer class=${footerStyle}>
<nav>
<a href="https://github.com/frekyll/caps">View on GitHub</a>
</nav>
</footer>
`
var container = html`
<div>
${header}
${form}
${footer}
</div>
`
document.body.appendChild(container)
function format (e) {
e.target.value = e.target.value
.replace(/[\u2013]/g, '-') // En dash
.replace(/[\u2014]/g, '--') // Em dash
.replace(/[\u2018\u2019]/g, "'") // Single quotes
.replace(/[\u201C\u201D]/g, '"') // Double quotes
.replace(/[\u2022]/g, '*') // Bullet
}
function capitalize (e) {
output.innerText = titleCase(e.target.value)
}
function copyOutput (e) {
output.focus()
output.select()
output.select(0, 99999)
document.execCommand('copy')
}
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape') {
this.activeElement.blur()
}
if (e.ctrlKey && e.key === 'v') {
copyOutput()
}
})