@@ -14,7 +14,8 @@ export interface LineOrWordsChange {
14
14
export function DiffDocument ( p : { first : string , second : string } ) {
15
15
16
16
const [ margin , setMargin ] = React . useState < number | null > ( 4 ) ;
17
-
17
+ const [ force , setForce ] = React . useState < number > ( false ) ;
18
+ var formatter = toNumberFormat ( "N0" ) ;
18
19
return (
19
20
< div >
20
21
< div >
@@ -24,18 +25,31 @@ export function DiffDocument(p: { first: string, second: string }) {
24
25
validateKey = { isNumber } /> lines arround each change</ label >
25
26
</ div >
26
27
< div >
27
- < DiffDocumentSimple first = { p . first } second = { p . second } margin = { margin } />
28
+ { ( p . first . length * p . second . length > DiffDocument . maxSize * DiffDocument . maxSize ) && ! force ?
29
+ < div class = "alert alert-warning mt-2" role = "alert" >
30
+ The two strings are too big ({ formatter . format ( p . first . length ) } ch. and { formatter . format ( p . second . length ) } ch.) and could freeze your browser...
31
+ < br />
32
+ < a href = "#" className = "btn btn-sm btn-warning mt-3" onClick = { e => { e . preventDefault ( ) ; setForce ( true ) ; } } > Try anyway!</ a >
33
+ </ div > :
34
+ < DiffDocumentSimple first = { p . first } second = { p . second } margin = { margin } />
35
+ }
28
36
</ div >
29
37
</ div >
30
38
) ;
31
39
}
32
40
33
41
DiffDocument . defaultMarginLines = 4 as ( number | null ) ;
42
+ DiffDocument . maxSize = 300000 ;
43
+
44
+
34
45
35
46
export function DiffDocumentSimple ( p : { first : string , second : string , margin ?: number | null } ) {
36
47
48
+
49
+
50
+
37
51
const linesDiff = React . useMemo < Array < LineOrWordsChange > > ( ( ) => {
38
-
52
+
39
53
var diffs = diffLines ( p . first , p . second ) ;
40
54
var result : Array < LineOrWordsChange > = [ ] ;
41
55
0 commit comments