@@ -1114,11 +1114,54 @@ export function superForm<
1114
1114
//#region Tainted
1115
1115
1116
1116
const Tainted = {
1117
+ defaultMessage : 'Leave page? Changes that you made may not be saved.' ,
1117
1118
state : writable < TaintedFields < T > | undefined > ( ) ,
1118
1119
message : options . taintedMessage ,
1119
- clean : clone ( form . data ) // Important to clone form.data, so it's not comparing the same object,
1120
+ clean : clone ( form . data ) , // Important to clone form.data, so it's not comparing the same object,
1121
+ forceRedirection : false
1120
1122
} ;
1121
1123
1124
+ async function Tainted_check ( nav : BeforeNavigate ) {
1125
+ if ( options . taintedMessage && ! Data . submitting && ! Tainted . forceRedirection ) {
1126
+ if ( Tainted_isTainted ( ) ) {
1127
+ const { taintedMessage } = options ;
1128
+ const isTaintedFunction = typeof taintedMessage === 'function' ;
1129
+
1130
+ // As beforeNavigate does not support Promise, we cancel the redirection until the promise resolve
1131
+ // if it's a custom function
1132
+ if ( isTaintedFunction ) nav . cancel ( ) ;
1133
+ // Does not display any dialog on page refresh or closing tab, will use default browser behaviour
1134
+ if ( nav . type === 'leave' ) return ;
1135
+
1136
+ const message =
1137
+ isTaintedFunction || taintedMessage === true ? Tainted . defaultMessage : taintedMessage ;
1138
+
1139
+ let shouldRedirect ;
1140
+ try {
1141
+ // - rejected => shouldRedirect = false
1142
+ // - resolved with false => shouldRedirect = false
1143
+ // - resolved with true => shouldRedirect = true
1144
+ shouldRedirect = isTaintedFunction ? await taintedMessage ( ) : window . confirm ( message ) ;
1145
+ } catch {
1146
+ shouldRedirect = false ;
1147
+ }
1148
+
1149
+ if ( shouldRedirect && nav . to ) {
1150
+ try {
1151
+ Tainted . forceRedirection = true ;
1152
+ await goto ( nav . to . url , { ...nav . to . params } ) ;
1153
+ return ;
1154
+ } finally {
1155
+ // Reset forceRedirection for multiple-tainted purpose
1156
+ Tainted . forceRedirection = false ;
1157
+ }
1158
+ } else if ( ! shouldRedirect && ! isTaintedFunction ) {
1159
+ nav . cancel ( ) ;
1160
+ }
1161
+ }
1162
+ }
1163
+ }
1164
+
1122
1165
function Tainted_enable ( ) {
1123
1166
options . taintedMessage = Tainted . message ;
1124
1167
}
@@ -1353,53 +1396,8 @@ export function superForm<
1353
1396
1354
1397
///// Store subscriptions ///////////////////////////////////////////////////
1355
1398
1356
- // Tainted check
1357
- const defaultMessage = 'Leave page? Changes that you made may not be saved.' ;
1358
- let forceRedirection = false ;
1359
-
1360
- async function taintedCheck ( nav : BeforeNavigate ) {
1361
- if ( options . taintedMessage && ! Data . submitting && ! forceRedirection ) {
1362
- if ( Tainted_isTainted ( ) ) {
1363
- const { taintedMessage } = options ;
1364
- const isTaintedFunction = typeof taintedMessage === 'function' ;
1365
-
1366
- // As beforeNavigate does not support Promise, we cancel the redirection until the promise resolve
1367
- // if it's a custom function
1368
- if ( isTaintedFunction ) nav . cancel ( ) ;
1369
- // Does not display any dialog on page refresh or closing tab, will use default browser behaviour
1370
- if ( nav . type === 'leave' ) return ;
1371
-
1372
- const message =
1373
- isTaintedFunction || taintedMessage === true ? defaultMessage : taintedMessage ;
1374
-
1375
- let shouldRedirect ;
1376
- try {
1377
- // - rejected => shouldRedirect = false
1378
- // - resolved with false => shouldRedirect = false
1379
- // - resolved with true => shouldRedirect = true
1380
- shouldRedirect = isTaintedFunction ? await taintedMessage ( ) : window . confirm ( message ) ;
1381
- } catch {
1382
- shouldRedirect = false ;
1383
- }
1384
-
1385
- if ( shouldRedirect && nav . to ) {
1386
- try {
1387
- forceRedirection = true ;
1388
- await goto ( nav . to . url , { ...nav . to . params } ) ;
1389
- return ;
1390
- } finally {
1391
- // Reset forceRedirection for multiple-tainted purpose
1392
- forceRedirection = false ;
1393
- }
1394
- } else if ( ! shouldRedirect && ! isTaintedFunction ) {
1395
- nav . cancel ( ) ;
1396
- }
1397
- }
1398
- }
1399
- }
1400
-
1401
1399
if ( browser ) {
1402
- beforeNavigate ( taintedCheck ) ;
1400
+ beforeNavigate ( Tainted_check ) ;
1403
1401
1404
1402
// Need to subscribe to catch page invalidation.
1405
1403
Unsubscriptions_add (
0 commit comments