-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
738 lines (666 loc) · 24.1 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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
<html>
<head>
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.3.0/milligram.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.3.0/milligram.min.css">
<style>
body {
margin: 10px;
}
.section {
margin-left: 10px;
margin-top: 20px;
}
.section .heading {
margin-bottom: 5px;
}
.checkbox-wrapper {
padding-left: 10px;
font-weight: normal;
}
.checkbox-wrapper.checked {
text-decoration: line-through;
}
.checkbox-wrapper input {
margin: 0.5rem;
margin-top: 0;
vertical-align: middle;
}
.button.button-delete {
padding: 0.5rem;
margin: 0;
color: salmon;
height: 1rem;
line-height: 1rem;
}
.button.button-delete:hover {
color: red;
}
input::placeholder {
color: #ddd
}
.editable-text.editing {
min-width: 400px
}
.editable-text.editing.wide {
min-width: 650px
}
.error {
color: red;
}
.copy-button {
padding: 1rem;
color: #9b4dca;
}
.copy-button:hover .icon,
.copy-button:hover .icon:before {
border-color: #606c76;
}
/* from https://cssicon.space/old/ */
.copy.icon {
width: 8px;
height: 11px;
border: solid 1px #9b4dca;
border-radius: 1px;
position: relative;
}
.copy.icon:before {
content: "";
display: block;
position: absolute;
left: -3px;
top: -3px;
width: 8px;
height: 11px;
border-top: solid 1px #9b4dca;
border-left: solid 1px #9b4dca;
border-radius: 1px 0 0 0;
}
</style>
</head>
<body>
<div id="app_container"></div>
<script type="text/babel">
const APP_STATE = {
INTRO: 1,
CHECKLIST: 2,
}
const generateId = () => {
return `${new Date().getTime()}-${Math.floor(Math.random() * 99999999)}`;
}
const checklistKey = name => `checklist_${name}`
const reverseChecklistKey = key => key.slice('checklist_'.length)
const loadChecklist = name => {
const raw = localStorage.getItem(checklistKey(name))
if (raw) {
try {
return JSON.parse(raw)
} catch (e) {
console.error("Failed to load checklist", e)
}
}
}
const saveChecklist = (name, checklist) => {
checklist.editedTime = new Date()
localStorage.setItem(checklistKey(name), JSON.stringify(checklist))
}
const deleteChecklist = (name) => {
localStorage.removeItem(checklistKey(name))
}
const getNewChecklist = (name) => {
return {
id: generateId(),
version: '1',
title: name,
sections: defaultSections,
createdTime: new Date(),
editedTime: new Date(),
}
}
const defaultSections = [
{
id: generateId(),
title: "Things to Know",
items: [
{ label: "You can add, edit, and remove both items and sections" },
{ label: "Double click text to edit or delete" },
{ label: "All data is stored in YOUR browser - there is no server or database" },
{ label: "Can access this page online or locally and see the same data" },
].map(item => ({...item, id: generateId() }))
}
]
const copyChecklist = (checklist) => {
const content = [checklist.title]
checklist.sections.forEach(section => {
content.push('', '', section.title, '')
section.items.forEach(item => {
content.push(`${item.checked ? '[X]' : ''}\t${item.label}`)
})
})
navigator.clipboard.writeText(content.join("\n"))
}
const getExistingChecklists = () => {
const existingChecklists = [];
for (let i = 0, n = localStorage.length; i < n; i++) {
const key = localStorage.key(i)
const name = reverseChecklistKey(key)
const checklist = loadChecklist(name)
if (checklist) {
checklist.name = name
existingChecklists.push(checklist)
}
}
return existingChecklists
}
const existingChecklists = getExistingChecklists()
const DeleteButton = (props) => (
<button
is-delete-button="true"
className="button button-clear button-delete"
onClick={(e) => {
e.stopPropagation()
e.preventDefault()
props.onClick()
}}
>
X
</button>
)
const EditableText = (props) => {
const [text, setText] = React.useState(props.text)
const finishEditing = (editNextItem) => {
if (text === "" && props.onDelete) {
props.onDelete()
} else {
props.onChange && props.onChange(text)
}
props.setIsEditing(false, editNextItem)
}
if (props.isEditing) {
return (
<React.Fragment>
<input
className={`editable-text editing ${text.length > 30 ? 'wide' : ''}`}
value={text}
onChange={(e) => setText(e.target.value)}
onFocus={(e) => {
// select all the content when we start editing
e.target.select()
}}
onBlur={(blurEvent) => {
finishEditing()
// handle delete explicitly since blur stops an immediate click event from firing
if (blurEvent.relatedTarget && blurEvent.relatedTarget.attributes['is-delete-button']) {
props.onDelete()
}
}}
onKeyDown={(e) => (e.keyCode === 13 && finishEditing(true))}
autoFocus={true}
onPaste={(e) => {
e.preventDefault()
const content = (event.clipboardData || window.clipboardData).getData('text');
props.onPaste && props.onPaste(content)
}}
/>
{props.onDelete ? (
<DeleteButton
onClick={() => {
// do nothing - this will cause a blur and the blur handler will do the delete
// so we don't have to race these two events or let blur clobber the click
//props.onDelete()
//props.setIsEditing(false)
}}
/>
) : null }
</React.Fragment>
)
}
return (
<React.Fragment>
<span onDoubleClick={() => (props.setIsEditing(true))}>{props.text}</span>
{props.alwaysShowDelete && props.onDelete ? (
<DeleteButton onClick={() => props.onDelete() } />
) : null }
</React.Fragment>
)
}
const Section = (props) => {
const [isEditingTitle, setIsEditingTitle] = React.useState(false)
const [editingIndex, setEditingIndex] = React.useState()
const doAddItem = () => {
setEditingIndex(props.items.length)
props.addItem()
}
return (
<div className="section">
<h4 className="heading">
<EditableText
text={props.title || "Untitled Section"}
onChange={props.editSectionTitle}
onDelete={() => props.deleteSection()}
isEditing={isEditingTitle}
setIsEditing={setIsEditingTitle}
/>
</h4>
{props.items.map((item, i) => (
<label key={item.id || i} className={`checkbox-wrapper${item.checked ? ' checked' : ''}`}>
<input
type="checkbox"
checked={!!item.checked}
onChange={(e) => {
props.setItemChecked(i, !item.checked)
}}
/>
<EditableText
text={item.label}
onChange={newLabel => props.editItem(i, {...item, label: newLabel})}
onDelete={() => props.deleteItem(i)}
isEditing={i === editingIndex}
setIsEditing={(isEditing, editNext) => {
let nextIndex = undefined
if (!isEditing && editNext) {
nextIndex = editingIndex + 1
// if out of items but asked for next, create a new one
if (nextIndex >= props.items.length) {
doAddItem()
}
} else if (isEditing) {
nextIndex = i
}
setEditingIndex(nextIndex)
}}
alwaysShowDelete={item.checked}
onPaste={(content) => props.onPaste(i, content)}
/>
</label>
))}
<button className="button button-clear" onClick={() => doAddItem()}>
+ Add Item
</button>
</div>
)
}
const Checklist = (props) => {
const [isEditingTitle, setIsEditingTitle] = React.useState(false)
const getItemChecker = (sectionIndex) => {
return (itemIndex, isChecked) => {
const newSections = [...props.sections]
const newSection = {...newSections[sectionIndex]}
const newItem = {
...newSection.items[itemIndex],
checked: isChecked,
}
newSection.items[itemIndex] = newItem
newSections[sectionIndex] = newSection
props.updateSections(newSections)
}
}
const getItemDelete = (sectionIndex) => {
return (itemIndex) => {
const newSections = [...props.sections]
const newSection = {...newSections[sectionIndex]}
newSection.items.splice(itemIndex, 1)
newSections[sectionIndex] = newSection
props.updateSections(newSections)
}
}
const getItemAdd = (sectionIndex) => {
return () => {
const newSections = [...props.sections]
const newSection = {...newSections[sectionIndex]}
newSection.items.push({
id: generateId(),
label: "New Item",
})
newSections[sectionIndex] = newSection
props.updateSections(newSections)
}
}
const getItemEdit = (sectionIndex) => {
return (itemIndex, itemPartial) => {
const newSections = [...props.sections]
const newSection = {...newSections[sectionIndex]}
newSection.items[itemIndex] = {
...newSection.items[itemIndex],
...itemPartial,
}
newSections[sectionIndex] = newSection
props.updateSections(newSections)
}
}
const deleteSection = (sectionIndex) => {
props.updateSections([
...props.sections.slice(0, sectionIndex),
...props.sections.slice(sectionIndex + 1),
])
}
const addSection = () => {
props.updateSections([
...props.sections,
{
id: generateId(),
title: "New Section",
items: [
{ label: "New Item" }
],
}
])
}
const editSection = (sectionIndex, section) => {
const newSections = [...props.sections]
newSections[sectionIndex] = section
props.updateSections(newSections)
}
return (
<React.Fragment>
<h2>
<EditableText
text={props.title}
onChange={props.updateChecklistTitle}
isEditing={isEditingTitle}
setIsEditing={setIsEditingTitle}
/>
{props.doCopy ? (
<button
className="button button-clear copy-button"
title="copy to clipboard"
onClick={() => {
props.doCopy()
}}
>
<div className="icon copy"></div>
</button>
) : null}
</h2>
{props.sections.map((section, sectionIndex) => (
<Section
key={sectionIndex}
title={section.title}
items={section.items}
setItemChecked={getItemChecker(sectionIndex)}
addItem={getItemAdd(sectionIndex)}
deleteItem={getItemDelete(sectionIndex)}
editItem={getItemEdit(sectionIndex)}
deleteSection={() => deleteSection(sectionIndex)}
editSectionTitle={
(newTitle) => editSection(sectionIndex, {...section, title: newTitle})
}
onPaste={(itemIndex, content) => props.doPaste(sectionIndex, itemIndex, content)}
/>
))}
<button className="button button-clear" onClick={addSection}>
+ Add Section
</button>
</React.Fragment>
)
}
const Breadcrumb = (props) => (
<div>
<a href={props.url}>Back to Checklists</a>
</div>
)
const AppIntroPage = (props) => {
const [error, setError] = React.useState('')
const [checklistName, setChecklistName] = React.useState('')
return (
<div className='container'>
<div className='row'>
<div className='column'>
<h2>Welcome to One Page Checklist</h2>
<p>
The point of this project is to have a dead-simple way to show
a checklist on your screen. I originally created this to make it
easy to show visually where I was in a demo while recording a
video but you can use it for whatever you like.
</p>
<p>
You can visit this page online or open it from a file locally (no
webserver necessary - you can literally download this one file and
open it on your computer) but each checklist lives on YOUR computer in
your browser's local storage (there is NO server anywhere). You can
create or view a checklist by entering a name below.
</p>
<p>
You can make as many checklists as you want and they will always be available
for you (on THIS browser) unless you clear your local storage. No accounts,
no passwords, no servers, no databases, no data breaches, no stress.
</p>
<p>
This project is open source and contained in a single file
- if you're reading this you already have the entire source
but you can also access the git repository at
{" "}<a href="https://github.com/collingreen/onepagechecklist">https://github.com/collingreen/onepagechecklist</a>
</p>
</div>
</div>
<div className='row'>
<div className='column'>
<form onSubmit={(e) => {
e.preventDefault()
return false
}}>
<fieldset>
<label htmlFor='checklist_name'>Create New Checklist</label>
<div className='container'>
<div className='row'>
<input
id='checklist_name'
type='text'
placeholder='my next great checklist'
className='column'
value={checklistName}
onChange={(e) => setChecklistName(e.target.value)}
/>
<div className='column column-10'/>
<button
className='column button button-outline'
onClick={e => {
if (checklistName.trim() !== '') {
props.createChecklist(checklistName)
} else {
setError('Enter a name for your new checklist')
}
}}
>Create/View Checklist</button>
</div>
{error ? <div className="error">{error}</div> : null}
</div>
</fieldset>
</form>
</div>
</div>
<div className='row'>
<div className='column'>
<label>Your Existing Checklists:</label>
<table>
<thead>
<tr>
<th>Checklist</th>
<th>Last Updated</th>
<th>Created</th>
<th></th>
</tr>
</thead>
<tbody>
{props.existingChecklists.map(checklist => (
<tr key={checklist.name}>
<td>
<a href={`?name=${checklist.name}`}>{checklist.title}</a>
</td>
<td>{checklist.editedTime}</td>
<td>{checklist.createdTime}</td>
<td>
<DeleteButton onClick={() => {
const text = `Are you sure you want to delete the '${checklist.name}' checklist? This cannot be undone.`
if (window.confirm(text)) {
deleteChecklist(checklist.name)
window.location.reload()
}
}}/>
</td>
</tr>
))}
{props.existingChecklists.length === 0 && (
<tr>
<td colSpan="4">You have not yet created any checklists!</td>
</tr>
)}
</tbody>
</table>
</div>
</div>
</div>
)
}
const ChecklistPage = ({name}) => {
const [checklist, setChecklist] = React.useState()
React.useEffect(() => {
// look for checklist contents in localstorage
const startingChecklist = loadChecklist(name) || getNewChecklist(name)
setChecklist(startingChecklist)
}, [name])
const updateSections = (newSections) => {
const newChecklist = {...checklist}
newChecklist.sections = newSections
saveChecklist(name, newChecklist)
setChecklist(newChecklist)
}
const updateChecklistTitle = (newName) => {
const newChecklist = {...checklist, title: newName}
saveChecklist(name, newChecklist)
setChecklist(newChecklist)
}
const checkboxRegex = /\[([ xXyY]?)\]\s*(.*)/
const parseLine = (line) => {
const trimmed = line.trim()
let checked = false
let content = ''
const result = trimmed.match(checkboxRegex)
if (result) {
checked = ['x','X','y','Y'].includes(result[1])
content = result[2]
} else {
content = trimmed
}
return { checked, content }
}
const doPaste = (sectionIndex, itemIndex, rawContent) => {
if (!checklist) {
return
}
let items = []
let title = ''
let mightBeHeader = false
const sections = []
const lines = rawContent.split("\n")
lines.forEach(line => {
if (line.trim() === '') {
if (items.length === 1) {
if (mightBeHeader) {
// was just a single line - use as header
sections.push({id: generateId(), title, items: []})
title = items[0].label
items = []
mightBeHeader = true
} else {
title = items[0].label
items = []
mightBeHeader = true
}
} else if (items.length >= 2) {
sections.push({id: generateId(), title, items})
items = []
title = ''
mightBeHeader = false
}
return
}
// TODO: parse more formats
const { checked, content } = parseLine(line)
items.push({
id: generateId(),
label: content,
checked
})
})
if (items.length !== 0) {
sections.push({id: generateId(), title, items})
}
// add items and sections to the checklist, starting in the right spot
if (sections.length === 0) {
return
}
const firstSection = sections[0]
const clonedSections = [...checklist.sections]
if (sections[0].title !== '') {
clonedSections.splice(sectionIndex + 1, 0, ...sections)
} else {
const targetSection = clonedSections[sectionIndex]
targetSection.items.splice(itemIndex + 1, 0, ...sections[0].items)
clonedSections.splice(sectionIndex + 1, 0, ...sections.slice(1))
}
updateSections(clonedSections)
}
const currentUrl = new URL(window.location)
currentUrl.search = ''
currentUrl.hash = ''
const indexUrl = currentUrl.href
if (!checklist) {
return (
<div>
<h2>Loading checklist. Please wait a moment...</h2>
</div>
)
}
return (
<div>
<Breadcrumb url={indexUrl}/>
<Checklist
name={name}
title={checklist.title || name}
sections={checklist.sections}
updateSections={updateSections}
updateChecklistTitle={updateChecklistTitle}
doCopy={() => copyChecklist(checklist)}
doPaste={doPaste}
/>
</div>
)
}
const App = (props) => {
// decide what to do based on url
const params = new URLSearchParams(location.search)
const name = params.get('name') || 'default'
const [appState, setAppState] = React.useState(APP_STATE.INTRO);
React.useEffect(() => {
setAppState(name === 'default' ? APP_STATE.INTRO : APP_STATE.CHECKLIST)
}, [name])
if (appState === APP_STATE.INTRO) {
return (
<AppIntroPage
existingChecklists={existingChecklists}
createChecklist={newName => {
const newUrl = new URL(window.location)
newUrl.search = `name=${newName}`
// TODO: push url change instead of reload
window.location.assign(newUrl.href)
}}
/>
)
} else {
return (
<ChecklistPage
name={name}
/>
)
}
}
const domContainer = document.querySelector('#app_container')
ReactDOM.render(React.createElement(App), domContainer)
</script>
</body>
</html>