1
- import $ from 'jquery' ;
2
1
import { GET } from '../modules/fetch.ts' ;
3
2
import { hideElem , loadElem , queryElemChildren , queryElems } from '../utils/dom.ts' ;
4
3
import { parseDom } from '../utils.ts' ;
4
+ import { fomanticQuery } from '../modules/fomantic/base.ts' ;
5
5
6
6
function getDefaultSvgBoundsIfUndefined ( text , src ) {
7
7
const defaultSize = 300 ;
8
8
const maxSize = 99999 ;
9
9
10
10
const svgDoc = parseDom ( text , 'image/svg+xml' ) ;
11
- const svg = svgDoc . documentElement ;
11
+ const svg = ( svgDoc . documentElement as unknown ) as SVGSVGElement ;
12
12
const width = svg ?. width ?. baseVal ;
13
13
const height = svg ?. height ?. baseVal ;
14
14
if ( width === undefined || height === undefined ) {
@@ -68,25 +68,27 @@ function createContext(imageAfter, imageBefore) {
68
68
}
69
69
70
70
class ImageDiff {
71
- async init ( containerEl ) {
71
+ containerEl : HTMLElement ;
72
+ diffContainerWidth : number ;
73
+
74
+ async init ( containerEl : HTMLElement ) {
72
75
this . containerEl = containerEl ;
73
76
containerEl . setAttribute ( 'data-image-diff-loaded' , 'true' ) ;
74
77
75
- // the only jQuery usage in this file
76
- $ ( containerEl ) . find ( '.ui.menu.tabular .item' ) . tab ( { autoTabActivation : false } ) ;
78
+ fomanticQuery ( containerEl ) . find ( '.ui.menu.tabular .item' ) . tab ( { autoTabActivation : false } ) ;
77
79
78
80
// the container may be hidden by "viewed" checkbox, so use the parent's width for reference
79
81
this . diffContainerWidth = Math . max ( containerEl . closest ( '.diff-file-box' ) . clientWidth - 300 , 100 ) ;
80
82
81
83
const imageInfos = [ {
82
84
path : containerEl . getAttribute ( 'data-path-after' ) ,
83
85
mime : containerEl . getAttribute ( 'data-mime-after' ) ,
84
- images : containerEl . querySelectorAll ( 'img.image-after' ) , // matches 3 <img>
86
+ images : containerEl . querySelectorAll < HTMLImageElement > ( 'img.image-after' ) , // matches 3 <img>
85
87
boundsInfo : containerEl . querySelector ( '.bounds-info-after' ) ,
86
88
} , {
87
89
path : containerEl . getAttribute ( 'data-path-before' ) ,
88
90
mime : containerEl . getAttribute ( 'data-mime-before' ) ,
89
- images : containerEl . querySelectorAll ( 'img.image-before' ) , // matches 3 <img>
91
+ images : containerEl . querySelectorAll < HTMLImageElement > ( 'img.image-before' ) , // matches 3 <img>
90
92
boundsInfo : containerEl . querySelector ( '.bounds-info-before' ) ,
91
93
} ] ;
92
94
@@ -102,8 +104,8 @@ class ImageDiff {
102
104
const bounds = getDefaultSvgBoundsIfUndefined ( text , info . path ) ;
103
105
if ( bounds ) {
104
106
for ( const el of info . images ) {
105
- el . setAttribute ( 'width' , bounds . width ) ;
106
- el . setAttribute ( 'height' , bounds . height ) ;
107
+ el . setAttribute ( 'width' , String ( bounds . width ) ) ;
108
+ el . setAttribute ( 'height' , String ( bounds . height ) ) ;
107
109
}
108
110
hideElem ( info . boundsInfo ) ;
109
111
}
@@ -151,7 +153,7 @@ class ImageDiff {
151
153
const boundsInfoBeforeHeight = this . containerEl . querySelector ( '.bounds-info-before .bounds-info-height' ) ;
152
154
if ( boundsInfoBeforeHeight ) {
153
155
boundsInfoBeforeHeight . textContent = `${ sizes . imageBefore . naturalHeight } px` ;
154
- boundsInfoBeforeHeight . classList . add ( 'red' , heightChanged ) ;
156
+ boundsInfoBeforeHeight . classList . toggle ( 'red' , heightChanged ) ;
155
157
}
156
158
}
157
159
@@ -205,7 +207,7 @@ class ImageDiff {
205
207
}
206
208
207
209
// extra height for inner "position: absolute" elements
208
- const swipe = this . containerEl . querySelector ( '.diff-swipe' ) ;
210
+ const swipe = this . containerEl . querySelector < HTMLElement > ( '.diff-swipe' ) ;
209
211
if ( swipe ) {
210
212
swipe . style . width = `${ sizes . maxSize . width * factor + 2 } px` ;
211
213
swipe . style . height = `${ sizes . maxSize . height * factor + 30 } px` ;
@@ -225,7 +227,7 @@ class ImageDiff {
225
227
const rect = swipeFrame . getBoundingClientRect ( ) ;
226
228
const value = Math . max ( 0 , Math . min ( e . clientX - rect . left , width ) ) ;
227
229
swipeBar . style . left = `${ value } px` ;
228
- this . containerEl . querySelector ( '.swipe-container' ) . style . width = `${ swipeFrame . clientWidth - value } px` ;
230
+ this . containerEl . querySelector < HTMLElement > ( '.swipe-container' ) . style . width = `${ swipeFrame . clientWidth - value } px` ;
229
231
} ;
230
232
const removeEventListeners = ( ) => {
231
233
document . removeEventListener ( 'mousemove' , onSwipeMouseMove ) ;
@@ -264,11 +266,11 @@ class ImageDiff {
264
266
overlayFrame . style . height = `${ sizes . maxSize . height * factor + 2 } px` ;
265
267
}
266
268
267
- const rangeInput = this . containerEl . querySelector ( 'input[type="range"]' ) ;
269
+ const rangeInput = this . containerEl . querySelector < HTMLInputElement > ( 'input[type="range"]' ) ;
268
270
269
271
function updateOpacity ( ) {
270
272
if ( sizes . imageAfter ) {
271
- sizes . imageAfter . parentNode . style . opacity = `${ rangeInput . value / 100 } ` ;
273
+ sizes . imageAfter . parentNode . style . opacity = `${ Number ( rangeInput . value ) / 100 } ` ;
272
274
}
273
275
}
274
276
@@ -278,7 +280,7 @@ class ImageDiff {
278
280
}
279
281
280
282
export function initImageDiff ( ) {
281
- for ( const el of queryElems ( '.image-diff:not([data-image-diff-loaded])' ) ) {
283
+ for ( const el of queryElems < HTMLImageElement > ( document , '.image-diff:not([data-image-diff-loaded])' ) ) {
282
284
( new ImageDiff ( ) ) . init ( el ) ; // it is async, but we don't need to await for it
283
285
}
284
286
}
0 commit comments