@@ -882,77 +882,93 @@ export default class MarkdownPreview extends React.Component {
882
882
883
883
const markdownPreviewIframe = document . querySelector ( '.MarkdownPreview' )
884
884
const rect = markdownPreviewIframe . getBoundingClientRect ( )
885
+ const config = { attributes : true , subtree : true }
886
+ const imgObserver = new MutationObserver ( ( mutationList ) => {
887
+ for ( const mu of mutationList ) {
888
+ if ( mu . target . className === 'carouselContent-enter-done' ) {
889
+ this . setImgOnClickEventHelper ( mu . target , rect )
890
+ break
891
+ }
892
+ }
893
+ } )
894
+
885
895
const imgList = markdownPreviewIframe . contentWindow . document . body . querySelectorAll ( 'img' )
886
896
for ( const img of imgList ) {
887
- img . onclick = ( ) => {
888
- const widthMagnification = document . body . clientWidth / img . width
889
- const heightMagnification = document . body . clientHeight / img . height
890
- const baseOnWidth = widthMagnification < heightMagnification
891
- const magnification = baseOnWidth ? widthMagnification : heightMagnification
892
-
893
- const zoomImgWidth = img . width * magnification
894
- const zoomImgHeight = img . height * magnification
895
- const zoomImgTop = ( document . body . clientHeight - zoomImgHeight ) / 2
896
- const zoomImgLeft = ( document . body . clientWidth - zoomImgWidth ) / 2
897
- const originalImgTop = img . y + rect . top
898
- const originalImgLeft = img . x + rect . left
899
- const originalImgRect = {
900
- top : `${ originalImgTop } px` ,
901
- left : `${ originalImgLeft } px` ,
902
- width : `${ img . width } px` ,
903
- height : `${ img . height } px`
904
- }
905
- const zoomInImgRect = {
906
- top : `${ baseOnWidth ? zoomImgTop : 0 } px` ,
907
- left : `${ baseOnWidth ? 0 : zoomImgLeft } px` ,
908
- width : `${ zoomImgWidth } px` ,
909
- height : `${ zoomImgHeight } px`
910
- }
911
- const animationSpeed = 300
897
+ const parentEl = img . parentElement
898
+ this . setImgOnClickEventHelper ( img , rect )
899
+ imgObserver . observe ( parentEl , config )
900
+ }
901
+ }
912
902
913
- const zoomImg = document . createElement ( 'img' )
914
- zoomImg . src = img . src
903
+ setImgOnClickEventHelper ( img , rect ) {
904
+ img . onclick = ( ) => {
905
+ const widthMagnification = document . body . clientWidth / img . width
906
+ const heightMagnification = document . body . clientHeight / img . height
907
+ const baseOnWidth = widthMagnification < heightMagnification
908
+ const magnification = baseOnWidth ? widthMagnification : heightMagnification
909
+
910
+ const zoomImgWidth = img . width * magnification
911
+ const zoomImgHeight = img . height * magnification
912
+ const zoomImgTop = ( document . body . clientHeight - zoomImgHeight ) / 2
913
+ const zoomImgLeft = ( document . body . clientWidth - zoomImgWidth ) / 2
914
+ const originalImgTop = img . y + rect . top
915
+ const originalImgLeft = img . x + rect . left
916
+ const originalImgRect = {
917
+ top : `${ originalImgTop } px` ,
918
+ left : `${ originalImgLeft } px` ,
919
+ width : `${ img . width } px` ,
920
+ height : `${ img . height } px`
921
+ }
922
+ const zoomInImgRect = {
923
+ top : `${ baseOnWidth ? zoomImgTop : 0 } px` ,
924
+ left : `${ baseOnWidth ? 0 : zoomImgLeft } px` ,
925
+ width : `${ zoomImgWidth } px` ,
926
+ height : `${ zoomImgHeight } px`
927
+ }
928
+ const animationSpeed = 300
929
+
930
+ const zoomImg = document . createElement ( 'img' )
931
+ zoomImg . src = img . src
932
+ zoomImg . style = `
933
+ position: absolute;
934
+ top: ${ baseOnWidth ? zoomImgTop : 0 } px;
935
+ left: ${ baseOnWidth ? 0 : zoomImgLeft } px;
936
+ width: ${ zoomImgWidth } ;
937
+ height: ${ zoomImgHeight } px;
938
+ `
939
+ zoomImg . animate ( [
940
+ originalImgRect ,
941
+ zoomInImgRect
942
+ ] , animationSpeed )
943
+
944
+ const overlay = document . createElement ( 'div' )
945
+ overlay . style = `
946
+ background-color: rgba(0,0,0,0.5);
947
+ cursor: zoom-out;
948
+ position: absolute;
949
+ top: 0;
950
+ left: 0;
951
+ width: 100%;
952
+ height: ${ document . body . clientHeight } px;
953
+ z-index: 100;
954
+ `
955
+ overlay . onclick = ( ) => {
915
956
zoomImg . style = `
916
957
position: absolute;
917
- top: ${ baseOnWidth ? zoomImgTop : 0 } px;
918
- left: ${ baseOnWidth ? 0 : zoomImgLeft } px;
919
- width: ${ zoomImgWidth } ;
920
- height: ${ zoomImgHeight } px;
958
+ top: ${ originalImgTop } px;
959
+ left: ${ originalImgLeft } px;
960
+ width: ${ img . width } px ;
961
+ height: ${ img . height } px;
921
962
`
922
- zoomImg . animate ( [
923
- originalImgRect ,
924
- zoomInImgRect
963
+ const zoomOutImgAnimation = zoomImg . animate ( [
964
+ zoomInImgRect ,
965
+ originalImgRect
925
966
] , animationSpeed )
926
-
927
- const overlay = document . createElement ( 'div' )
928
- overlay . style = `
929
- background-color: rgba(0,0,0,0.5);
930
- cursor: zoom-out;
931
- position: absolute;
932
- top: 0;
933
- left: 0;
934
- width: 100%;
935
- height: ${ document . body . clientHeight } px;
936
- z-index: 100;
937
- `
938
- overlay . onclick = ( ) => {
939
- zoomImg . style = `
940
- position: absolute;
941
- top: ${ originalImgTop } px;
942
- left: ${ originalImgLeft } px;
943
- width: ${ img . width } px;
944
- height: ${ img . height } px;
945
- `
946
- const zoomOutImgAnimation = zoomImg . animate ( [
947
- zoomInImgRect ,
948
- originalImgRect
949
- ] , animationSpeed )
950
- zoomOutImgAnimation . onfinish = ( ) => overlay . remove ( )
951
- }
952
-
953
- overlay . appendChild ( zoomImg )
954
- document . body . appendChild ( overlay )
967
+ zoomOutImgAnimation . onfinish = ( ) => overlay . remove ( )
955
968
}
969
+
970
+ overlay . appendChild ( zoomImg )
971
+ document . body . appendChild ( overlay )
956
972
}
957
973
958
974
this . getWindow ( ) . scrollTo ( 0 , 0 )
0 commit comments