forked from adamschwartz/lazy.invoice.workers.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml.js
More file actions
executable file
·425 lines (355 loc) · 20.3 KB
/
html.js
File metadata and controls
executable file
·425 lines (355 loc) · 20.3 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
export default `<!doctype html>
<html lang="en" with-selection-styled>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Lazy Invoice · By Adam Schwartz · Powered by Cloudflare Workers®</title>
<meta name="description" content="Lazily generate PDF invoices—powered by Cloudflare Workers®.">
<meta name="keywords" content="Invoice, PDF, generation, generate, Cloudflare Workers®, app">
<meta name="author" content="Adam Schwartz">
<meta name="generator" content="Adam Schwartz">
<meta property="og:type" content="website">
<meta property="og:title" content="Lazy Invoice">
<meta property="og:description" content="Lazily generate PDF invoices—powered by Cloudflare Workers®.">
<meta property="og:url" content="https://lazy.invoice.workers.dev">
<meta name="twitter:site" content="@adamfschwartz">
<meta name="twitter:creator" content="@adamfschwartz">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Lazy Invoice">
<meta name="twitter:description" content="Lazily generate PDF invoices—powered by Cloudflare Workers®.">
<meta name="twitter:url" content="https://lazy.invoice.workers.dev">
<link href="https://ui.adam.workers.dev/?components=link,button,formField,input,checkbox,stack,row,dialog" rel="stylesheet">
<script>/* focus-visible polyfill */!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e():"function"==typeof define&&define.amd?define(e):e()}(0,function(){"use strict";"undefined"!=typeof document&&function(t){var e;function n(){e||(e=!0,t())}["interactive","complete"].indexOf(document.readyState)>=0?t():(e=!1,document.addEventListener("DOMContentLoaded",n,!1),window.addEventListener("load",n,!1))}(function(){var t=!0,e=!1,n=null,o={text:!0,search:!0,url:!0,tel:!0,email:!0,password:!0,number:!0,date:!0,month:!0,week:!0,time:!0,datetime:!0,"datetime-local":!0};function r(t){return!!(t&&t!==document&&"HTML"!==t.nodeName&&"BODY"!==t.nodeName&&"classList"in t&&"contains"in t.classList)}function i(t){""!==t.getAttribute("is-focus-visible")&&t.setAttribute("is-focus-visible","")}function u(e){t=!1}function c(){document.addEventListener("mousemove",s),document.addEventListener("mousedown",s),document.addEventListener("mouseup",s),document.addEventListener("pointermove",s),document.addEventListener("pointerdown",s),document.addEventListener("pointerup",s),document.addEventListener("touchmove",s),document.addEventListener("touchstart",s),document.addEventListener("touchend",s)}function s(e){"html"!==e.target.nodeName.toLowerCase()&&(t=!1,document.removeEventListener("mousemove",s),document.removeEventListener("mousedown",s),document.removeEventListener("mouseup",s),document.removeEventListener("pointermove",s),document.removeEventListener("pointerdown",s),document.removeEventListener("pointerup",s),document.removeEventListener("touchmove",s),document.removeEventListener("touchstart",s),document.removeEventListener("touchend",s))}document.addEventListener("keydown",function(e){r(document.activeElement)&&i(document.activeElement),t=!0},!0),document.addEventListener("mousedown",u,!0),document.addEventListener("pointerdown",u,!0),document.addEventListener("touchstart",u,!0),document.addEventListener("focus",function(e){var n,u,c;r(e.target)&&(t||(n=e.target,u=n.type,"INPUT"==(c=n.tagName)&&o[u]&&!n.readOnly||"TEXTAREA"==c&&!n.readOnly||n.isContentEditable))&&i(e.target)},!0),document.addEventListener("blur",function(t){var o;r(t.target)&&t.target.hasAttribute("is-focus-visible")&&(e=!0,window.clearTimeout(n),n=window.setTimeout(function(){e=!1,window.clearTimeout(n)},100),""===(o=t.target).getAttribute("is-focus-visible")&&o.removeAttribute("is-focus-visible"))},!0),document.addEventListener("visibilitychange",function(n){"hidden"==document.visibilityState&&(e&&(t=!0),c())},!0),c(),document.documentElement.setAttribute("js-focus-visible-polyfill-available","")})})</script>
<script>/* dialog */(()=>{const a="[dialog-autofocus], [dialog-close]",b=b=>c=>{b.contains(c.target)||(b.previousElementSibling===c.target?b.querySelector("[dialog-close-x]").focus():b.querySelector(a).focus())},c=a=>b=>{"Escape"===b.key&&a.close()};window.Dialog=class{constructor(a){this.el=a,this.trapFocus=b(a),this.checkForEscape=c(this)}open(){this.originalActiveElement=document.activeElement,document.documentElement.setAttribute("is-dialog",""),document.addEventListener("focus",this.trapFocus,!0),document.addEventListener("keyup",this.checkForEscape,!0),this.setupClosers(),this.el.querySelector(a).focus()}setupClosers(){const a=[];this.el.parentNode.matches("[dialog-close]")&&a.push(this.el.parentNode),this.el.querySelectorAll("[dialog-close]").forEach(b=>a.push(b)),a.forEach(a=>{""===a.getAttribute("dialog-close-handled")||(a.setAttribute("dialog-close-handled",""),a.addEventListener("click",b=>{(null===a.getAttribute("dialog-close-self-only")||b.target===a)&&this.close()}))})}close(){document.documentElement.removeAttribute("is-dialog"),document.removeEventListener("focus",this.trapFocus,!0),document.removeEventListener("keyup",this.checkForEscape,!0),this.originalActiveElement&&this.originalActiveElement.parentNode&&this.originalActiveElement.focus()}}})()</script>
<script>/* autoresize textarea */(function(){window.btoa&&window.getComputedStyle&&document.documentElement.querySelector&&document.documentElement.setAttribute&&((window.TextareaAutoResize={}).init=function(a){if(!("TEXTAREA"!==a.nodeName||a.getAttribute("textarea-auto-resize-initted-"))){a.setAttribute("textarea-auto-resize-initted",!0),a.style.overflow="hidden",a.style.overflowY="hidden",a.style.wordWrap="break-word";var b=a.value;a.value="",a.value=b;var c="position: absolute; top: -999px; right: auto; bottom: auto;left: 0; overflow: hidden; -webkit-box-sizing: content-box;-moz-box-sizing: content-box; box-sizing: content-box;min-height: 0 !important; height: 0 !important; padding: 0;word-wrap: break-word; border: 0;",d=document.createElement("textarea");d.setAttribute("tabindex",-1),d.setAttribute("aria-label","Text input clone"),d.setAttribute("style",c);var e=getComputedStyle(a),f="border-box"===e.getPropertyValue("box-sizing")||"border-box"===e.getPropertyValue("-moz-box-sizing")||"border-box"===e.getPropertyValue("-webkit-box-sizing"),g=f?{width:parseInt(e.getPropertyValue("border-right-width"),10)+parseInt(e.getPropertyValue("padding-right"),10)+parseInt(e.getPropertyValue("padding-left"),10)+parseInt(e.getPropertyValue("border-left-width"),10),height:parseInt(e.getPropertyValue("border-top-width"),10)+parseInt(e.getPropertyValue("padding-top"),10)+parseInt(e.getPropertyValue("padding-bottom"),10)+parseInt(e.getPropertyValue("border-bottom-width"),10)}:{width:0,height:0},h=parseInt(e.getPropertyValue("min-height"),10),i=parseInt(e.getPropertyValue("height"),10),j=Math.max(h,i)-g.height,k=parseInt(e.getPropertyValue("max-height"),10);k=k&&0<k?k:9e4,d.parentNode!==document.body&&document.body.appendChild(d),a.style.resize="none";var l,m,n=["font-family","font-size","font-weight","font-style","letter-spacing","line-height","text-transform","word-spacing","text-indent"],o=function(){l=a,e=getComputedStyle(a),n.forEach(function(a){c+=a+":"+e.getPropertyValue(a)+";"}),d.setAttribute("style",c)},p=function(){if(l!==a&&o(),!m){m=!0,d.value=a.value+"",d.style.overflowY=a.style.overflowY;var b=""===a.style.height?"auto":parseInt(a.style.height,10),c=getComputedStyle(a).getPropertyValue("width");if("px"===c.substr(c.length-2,2)){var e=parseInt(c,10)-g.width;d.style.width=e+"px"}var f,h=d.scrollHeight;h>k?(h=k,f="scroll"):h<j&&(h=j),h+=g.height,a.style.overflowY=f||"hidden",b!==h&&(a.style.height=h+"px"),setTimeout(function(){m=!1},1000)}},q=function(){m=!1,p()};window.addEventListener("resize",q),a.addEventListener("input",function(){q()}),setTimeout(p)}})})();</script>
<style>
html {
--font-family: Avenir, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
--color-rgb: 8, 10, 60;
--accent-color-rgb: 74, 76, 105;
}
@media(min-width: 84em) {
body {
font-size: 1.2em;
}
}
a {
color: rgb(var(--accent-color-rgb));
}
html {
--preview-background: #525659; /* Chrome PDF viewer background color */
background: var(--preview-background);
}
html, body {
overscroll-behavior: none;
}
body {
margin: 0;
}
html, body, .Surface {
height: 100%;
}
.Surface {
display: flex;
}
h1 {
margin: 0;
font-weight: bold;
font-size: 1.25em;
}
@media (max-width: 360px) {
h1 {
font-size: 1.15em;
}
}
h1 + .Button {
margin-left: auto;
}
.Button-is-help {
margin: -.5em 0;
padding: 0;
font-size: 1.1em;
font-weight: bold;
text-align: center;
width: 1.75em;
height: 1.75em;
line-height: 1.9;
border-radius: 50%;
}
.FormField--label {
font-weight: 500;
}
.Panel {
position: relative;
z-index: 1;
display: flex;
flex-direction: column;
width: 28em;
background: #efefef;
box-shadow: 0 0 1em rgba(0, 0, 0, .5);
}
.Panel--top, .Panel--main, .Panel--bottom {
box-sizing: border-box;
padding-left: 1.5em;
padding-right: 1.5em;
}
.Panel--top {
position: relative;
z-index: 2;
display: flex;
align-items: center;
padding-top: 1em;
padding-bottom: 1em;
background: #fff;
box-shadow: 0 1px rgba(0, 0, 0, .07), 0 3px 3px -2px rgba(0, 0, 0, .09);
flex-shrink: 0;
}
.Panel--main {
padding-top: 1.5em;
padding-bottom: 1.5em;
overflow: auto;
-webkit-overflow-scrolling: touch;
}
.Panel--deemphasized {
position: relative;
}
.Panel--deemphasized::after {
content: "";
display: block;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: #efefef;
opacity: .3;
}
.Panel--bottom {
position: relative;
z-index: 1;
margin-top: auto;
padding-top: 1em;
padding-bottom: 1em;
background: #fff;
box-shadow: 0 -1px rgba(0, 0, 0, .07), 0 -3px 3px -2px rgba(0, 0, 0, .09);
flex-shrink: 0;
}
.Preview {
flex: 1;
margin: 0;
padding: 0;
}
.Preview--iframe {
display: block;
border: 0;
height: 100%;
width: 100%;
}
.LazyInvoiceLogo > span {
display: inline-block;
}
.LazyInvoiceLogo--muted {
position: relative;
}
.LazyInvoiceLogo--muted::after {
content: "";
display: block;
position: absolute;
top: 3px;
right: 0;
bottom: 3px;
left: 0;
background: #fff;
opacity: .5;
}
@media (max-width: 319px) {
.LazyInvoiceLogo > .LazyInvoiceLogo--muted {
display: none;
}
}
.Panel--bottom .Button-is-primary .mobile-only {
display: none;
}
@media (max-width: 800px) {
.Panel {
width: 100%;
}
.Preview,
.Panel--bottom .Button-is-primary .desktop-only,
.Panel--bottom .Button-is-bordered {
display: none;
}
.Panel--bottom .Button-is-primary .mobile-only {
display: inline;
}
}
.Dialog--content .Link {
--underline-size: 1px;
--underline-color: rgba(var(--accent-color-rgb), .7);
display: inline-block;
line-height: 1.1;
}
</style>
</head>
<body>
<div class="Surface">
<div class="Panel">
<div class="Panel--top">
<h1><a href="https://lazy.invoice.workers.dev" class="Link Link-without-underline"><span class="LazyInvoiceLogo"><span>lazy.invoice</span><span class="LazyInvoiceLogo--muted">.workers.dev</span></span></a></h1>
<button data-js-dialog-open class="Button Button-is-help Button-is-bordered" type="button" aria-label="Help">?</button>
</div>
<main class="Panel--main" is-smooth-scrolling>
<form id="form" class="Stack" method="GET" action="https://generate.invoice.workers.dev">
<div class="FormField">
<div class="FormField--text">
<label class="FormField--label" for="company">Company <span class="Panel--deemphasized">name & address</span></label>
<p id="company-describedby" class="FormField--description">The company or individual sending the invoice.</p>
</div>
<textarea class="Input Input-textarea" id="company" aria-describedby="company-describedby" name="company" rows="3" autocomplete="off" autofocus></textarea>
</div>
<div class="FormField">
<div class="FormField--text">
<label class="FormField--label" for="customer">Customer <span class="Panel--deemphasized">name & address</span></label>
<p id="customer-describedby" class="FormField--description">The company or individual receiving the invoice.</p>
</div>
<textarea class="Input Input-textarea" id="customer" aria-describedby="customer-describedby" name="customer" rows="3" autocomplete="off"></textarea>
</div>
<div class="FormField">
<div class="FormField--text">
<label class="FormField--label" for="description">Description</label>
<p id="description-describedby" class="FormField--description">Default value is “Products and services”.</p>
</div>
<input class="Input" id="description" type="text" name="description" aria-describedby="description-describedby" autocomplete="off"/>
</div>
<div class="FormField">
<div class="FormField--text">
<label class="FormField--label" for="amount">Amount</label>
<p class="FormField--description">e.g. “$100” or “¥39000” or simply “500”.</p>
</div>
<input class="Input" id="amount" type="text" name="amount" autocomplete="off"/>
</div>
<div class="FormField">
<div class="FormField--text">
<label class="FormField--label" for="total">Total <span class="Panel--deemphasized">(optional)</span></label>
</div>
<input class="Input" id="total" type="text" name="total" autocomplete="off"/>
</div>
<div class="FormField">
<div class="FormField--text">
<label class="FormField--label" for="number">Invoice Number/ID <span class="Panel--deemphasized">(optional)</span></label>
</div>
<input class="Input" id="number" type="text" name="number" autocomplete="off"/>
</div>
<div class="FormField">
<div class="FormField--text">
<label class="FormField--label" for="date">Invoice date</label>
<p id="date-describedby" class="FormField--description">Default value is <em>today</em>.</p>
</div>
<input class="Input" id="date" type="text" name="date" aria-describedby="date-describedby" autocomplete="off"/>
</div>
</form>
</main>
<div class="Panel--bottom">
<div class="Row">
<button class="Button Button-is-primary" type="submit" form="form"><span class="desktop-only">Update preview</span><span class="mobile-only">Generate PDF</span></button>
<a data-js-permalink href="https://generate.invoice.workers.dev" class="Button Button-is-bordered" type="button">Permalink</a>
</div>
</div>
</div>
<figure class="Preview">
<iframe class="Preview--iframe" src="https://generate.invoice.workers.dev" border="0" title="Invoice PDF preview"></iframe>
</figure>
</div>
<div class="Dialog---backdrop" dialog-close dialog-close-self-only>
<div tabindex="0"></div>
<div class="Dialog" role="dialog" aria-labelledby="dialog-title" dialog>
<h2 class="Dialog--title" id="dialog-title">About</h2>
<div class="Dialog--content">
<p>Lazy Invoice is a simple tool for generating a PDF invoice for a single product or service. Just fill out the form and see the PDF update as you go.</p>
<p>Built by <a class="Link" href="https://adamschwartz.co" target="_blank" rel="noopener">Adam Schwartz</a> using <a class="Link" href="https://workers.cloudflare.com" target="_blank" rel="noopener">Cloudflare Workers</a>.</p>
</div>
<div class="Dialog--actions">
<div class="Row">
<button class="Button Button-is-primary" dialog-close dialog-close-x dialog-autofocus>Done</button>
<button class="Button Button-is-bordered" dialog-close dialog-close-x dialog-autofocus data-js-fill-example>View example</button>
</div>
</div>
<div class="Dialog--close-positioner">
<button class="Button Button-is-close" aria-label="Close" dialog-close>
<svg viewBox="0 0 8 8"><path stroke="currentColor" stroke-width=".75" d="M0 0 L8 8 M8 0 L0 8"/></svg>
</button>
</div>
</div>
<div tabindex="0"></div>
</div>
<script>
const Debounce = (fn, delay) => {
let timeout
return () => {
clearTimeout(timeout)
timeout = setTimeout(() => fn(), delay)
}
}
const form = document.querySelector('form')
const iframe = document.querySelector('iframe')
const openDialog = document.querySelector('[data-js-dialog-open]')
const fillExample = document.querySelector('[data-js-fill-example]')
const permalink = document.querySelector('[data-js-permalink]')
const mobileQuery = window.matchMedia('(max-width: 800px)')
const dialog = new Dialog(document.querySelector('[dialog]'))
openDialog.addEventListener('click', () => dialog.open())
fillExample.addEventListener('click', () => {
const data = {
company: 'Ginza Natsuno\\n4F Marunouchi 1-5-1\\nChiyoda-ku, Tokyo, Japan',
customer: 'Sukiyabashi Jiro\\n2-15, Ginza 4-chome\\nCity Chuo, Tokyo, Japan',
description: '4 pairs of rounded red sandalwood chopsticks',
amount: '4 × ¥8,000 per pair',
total: '¥32,000',
number: '373267' // Dreams ;)
}
const getFieldEl = id => form.querySelector(\`#\${ id }\`)
let pristine = true
for (property in data) {
const fieldEl = getFieldEl(property)
if (fieldEl && fieldEl.value !== '') pristine = false
}
if (pristine === false) {
const confirmValue = confirm('Are you sure you want to replace all of the entries in the form with the example values?')
if (!confirmValue) return
}
for (property in data) {
const fieldEl = getFieldEl(property)
if (fieldEl) fieldEl.value = data[property]
}
update()
})
let lastPreviewURL = iframe.src
const update = () => {
const data = new FormData(form)
const queryString = new URLSearchParams(data).toString()
const url = \`\${ form.action }?\${ queryString }\`
if (url === lastPreviewURL) return
lastPreviewURL = url
iframe.src = url
permalink.href = url
}
form.addEventListener('submit', event => {
if (mobileQuery.matches) return
event.preventDefault()
update()
})
const events = ['input', 'change']
const debouncedUpdateFn = Debounce(update, 380)
document.querySelectorAll('textarea, input').forEach(el => {
events.forEach(eventName => {
el.addEventListener(eventName, event => {
if (mobileQuery.matches) return
debouncedUpdateFn()
})
})
})
document.querySelectorAll('textarea').forEach(t => TextareaAutoResize.init(t))
</script>
</body>
</html>`