-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
132 lines (109 loc) · 3.8 KB
/
index.ts
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
// Last submitted HTMLFormElement that will cause a browser navigation.
let submittedForm: HTMLFormElement | null = null
function shouldResumeField(field: HTMLInputElement | HTMLTextAreaElement): boolean {
return !!field.id && field.value !== field.defaultValue && field.form !== submittedForm
}
type PersistOptions = {
selector?: string
keyPrefix?: string
storage?: Pick<Storage, 'getItem' | 'setItem'>
}
// Write all ids and values of the selected fields on the page into sessionStorage.
export function persistResumableFields(id: string, options?: PersistOptions): void {
const selector = options?.selector ?? '.js-session-resumable'
const keyPrefix = options?.keyPrefix ?? 'session-resume:'
let storage
try {
storage = options?.storage ?? sessionStorage
} catch {
// Ignore browser private mode error and return early
return
}
const key = `${keyPrefix}${id}`
const resumables = []
for (const el of document.querySelectorAll(selector)) {
if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
resumables.push(el)
}
}
let fields = resumables.filter(field => shouldResumeField(field)).map(field => [field.id, field.value])
if (fields.length) {
try {
const previouslyStoredFieldsJson = storage.getItem(key)
if (previouslyStoredFieldsJson !== null) {
const previouslyStoredFields: string[][] = JSON.parse(previouslyStoredFieldsJson)
const fieldsNotReplaced: string[][] = previouslyStoredFields.filter(function (oldField) {
return !fields.some(field => field[0] === oldField[0])
})
fields = fields.concat(fieldsNotReplaced)
}
storage.setItem(key, JSON.stringify(fields))
} catch {
// Ignore browser private mode error.
}
}
}
type RestoreOptions = {keyPrefix?: string; storage?: Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>}
export function restoreResumableFields(id: string, options?: RestoreOptions): void {
const keyPrefix = options?.keyPrefix ?? 'session-resume:'
let storage
try {
storage = options?.storage ?? sessionStorage
} catch {
// Ignore browser private mode error and return early
return
}
const key = `${keyPrefix}${id}`
let fields
try {
fields = storage.getItem(key)
} catch {
// Ignore browser private mode error.
}
if (!fields) return
const changedFields: Array<HTMLInputElement | HTMLTextAreaElement> = []
const storedFieldsNotFound: string[][] = []
for (const [fieldId, value] of JSON.parse(fields)) {
const resumeEvent = new CustomEvent('session:resume', {
bubbles: true,
cancelable: true,
detail: {targetId: fieldId, targetValue: value}
})
if (document.dispatchEvent(resumeEvent)) {
const field = document.getElementById(fieldId)
if (field && (field instanceof HTMLInputElement || field instanceof HTMLTextAreaElement)) {
if (field.value === field.defaultValue) {
field.value = value
changedFields.push(field)
}
} else {
storedFieldsNotFound.push([fieldId, value])
}
}
}
// Some fields we want to restore are not always immediately present in the
// DOM and may be added later. This holds onto the values until
// they're needed.
if (storedFieldsNotFound.length === 0) {
try {
storage.removeItem(key)
} catch {
// Ignore browser private mode error.
}
} else {
storage.setItem(key, JSON.stringify(storedFieldsNotFound))
}
setTimeout(function () {
for (const el of changedFields) {
el.dispatchEvent(new CustomEvent('change', {bubbles: true, cancelable: true}))
}
}, 0)
}
export function setForm(event: Event): void {
submittedForm = event.target as HTMLFormElement
setTimeout(function () {
if (event.defaultPrevented) {
submittedForm = null
}
}, 0)
}