1
1
// IWSY
2
2
3
- const IWSY = ( player , text ) => {
3
+ const IWSY = ( playerElement , text ) => {
4
4
5
+ let player = playerElement ;
5
6
let script = text ;
6
7
let clicked = false ;
7
8
@@ -104,10 +105,9 @@ const IWSY = (player, text) => {
104
105
105
106
// Create a block
106
107
const createBlock = block => {
107
- const r = player . getBoundingClientRect ( ) ;
108
108
let rect = {
109
- width : r . width ,
110
- height : r . height ,
109
+ width : player . clientWidth ,
110
+ height : player . clientHeight ,
111
111
left : 0 ,
112
112
top : 0
113
113
}
@@ -634,39 +634,6 @@ const IWSY = (player, text) => {
634
634
}
635
635
} ;
636
636
637
- // Go to a specified step number
638
- const gotoStep = ( target ) => {
639
- if ( ! script . stepping ) {
640
- script . stepping = true ;
641
- script . scanTarget = target ;
642
- script . singleStep = true ;
643
- script . runMode = `manual` ;
644
- scan ( ) ;
645
- }
646
- } ;
647
-
648
- // Run the presentation
649
- const run = then => {
650
- if ( ! script . stepping ) {
651
- initScript ( ) ;
652
- script . runMode = `auto` ;
653
- script . speed = `normal` ;
654
- script . singleStep = false ;
655
- script . then = then ;
656
- doStep ( script . steps [ 0 ] ) ;
657
- }
658
- } ;
659
-
660
- // Stop the run
661
- const stop = ( ) => {
662
- script . stop = true ;
663
- } ;
664
-
665
- // Set a step callback
666
- const onStep = onStepCB => {
667
- script . onStepCB = onStepCB ;
668
- } ;
669
-
670
637
// Chain to another presentation
671
638
const chain = step => {
672
639
step . next ( ) ;
@@ -686,68 +653,6 @@ const IWSY = (player, text) => {
686
653
}
687
654
} ;
688
655
689
- // Replace the script
690
- const setScript = newScript => {
691
- removeBlocks ( ) ;
692
- script = newScript ;
693
- initScript ( ) ;
694
- } ;
695
-
696
- // Show a block
697
- const block = blockIndex => {
698
- player . innerHTML = `` ;
699
- const w = player . getBoundingClientRect ( ) . width / 1000 ;
700
- const h = player . getBoundingClientRect ( ) . height / 1000 ;
701
- script . blocks . forEach ( ( block , index ) => {
702
- const defaults = block . defaults ;
703
- const element = document . createElement ( `div` ) ;
704
- player . appendChild ( element ) ;
705
- if ( script . speed === `scan` ) {
706
- element . style . display = `none` ;
707
- }
708
- element . style . position = `absolute` ;
709
- element . style . opacity = `0.5` ;
710
- let val = defaults . left ;
711
- if ( ! isNaN ( val ) ) {
712
- val *= w ;
713
- }
714
- element . style . left = val ;
715
- val = defaults . top ;
716
- if ( ! isNaN ( val ) ) {
717
- val *= h ;
718
- }
719
- element . style . top = val ;
720
- val = defaults . width ;
721
- if ( ! isNaN ( val ) ) {
722
- val = `${ val * w - 2 } px` ;
723
- } else {
724
- val = `calc(${ val } - 2px)`
725
- }
726
- element . style . width = val ;
727
- val = defaults . height ;
728
- if ( ! isNaN ( val ) ) {
729
- val = `${ val * h - 2 } px` ;
730
- } else {
731
- val = `calc(${ val } - 2px)`
732
- }
733
- element . style . height = val ;
734
- element . style [ `font-size` ] = `${ h * 40 } px`
735
- element . innerHTML = defaults . name ;
736
- if ( index == blockIndex ) {
737
- element . style . background = `#ddffdd` ;
738
- element . style . border = `1px solid #00ff00` ;
739
- element . style [ `font-weight` ] = `bold`
740
- element . style [ `z-index` ] = 10 ;
741
- element . style . color = `#006600` ;
742
- } else {
743
- element . style . border = `1px solid #ff0000` ;
744
- element . style [ `text-align` ] = `right` ;
745
- element . style [ `z-index` ] = 0 ;
746
- element . style . color = `#ff0000` ;
747
- }
748
- } ) ;
749
- } ;
750
-
751
656
// Set up Showdown
752
657
const setupShowdown = ( ) => {
753
658
if ( typeof showdown === `undefined` ) {
@@ -858,14 +763,6 @@ const IWSY = (player, text) => {
858
763
document . head . appendChild ( style ) ;
859
764
} ;
860
765
861
- // Remove all the CSS styles
862
- const removeStyles = ( ) => {
863
- const styles = document . getElementsByClassName ( "iwsy-css" ) ;
864
- for ( const style of styles ) {
865
- style . parentNode . removeChild ( style ) ;
866
- }
867
- } ;
868
-
869
766
// Initialize the script
870
767
const initScript = ( ) => {
871
768
document . onkeydown = null ;
@@ -965,16 +862,140 @@ const IWSY = (player, text) => {
965
862
}
966
863
} ;
967
864
865
+ ///////////////////////////////////////////////////////////////////////////////
866
+ // These are all the exported functions
867
+
868
+ // Set the script
869
+ const setScript = newScript => {
870
+ removeBlocks ( ) ;
871
+ script = newScript ;
872
+ initScript ( ) ;
873
+ } ;
874
+
875
+ // Go to a specified step number
876
+ const gotoStep = ( target ) => {
877
+ if ( ! script . stepping ) {
878
+ script . stepping = true ;
879
+ script . scanTarget = target ;
880
+ script . singleStep = true ;
881
+ script . runMode = `manual` ;
882
+ scan ( ) ;
883
+ }
884
+ } ;
885
+
886
+ // Show a block
887
+ const block = blockIndex => {
888
+ player . innerHTML = `` ;
889
+ const w = player . getBoundingClientRect ( ) . width / 1000 ;
890
+ const h = player . getBoundingClientRect ( ) . height / 1000 ;
891
+ script . blocks . forEach ( ( block , index ) => {
892
+ const defaults = block . defaults ;
893
+ const element = document . createElement ( `div` ) ;
894
+ player . appendChild ( element ) ;
895
+ if ( script . speed === `scan` ) {
896
+ element . style . display = `none` ;
897
+ }
898
+ element . style . position = `absolute` ;
899
+ element . style . opacity = `0.5` ;
900
+ let val = defaults . left ;
901
+ if ( ! isNaN ( val ) ) {
902
+ val *= w ;
903
+ }
904
+ element . style . left = val ;
905
+ val = defaults . top ;
906
+ if ( ! isNaN ( val ) ) {
907
+ val *= h ;
908
+ }
909
+ element . style . top = val ;
910
+ val = defaults . width ;
911
+ if ( ! isNaN ( val ) ) {
912
+ val = `${ val * w - 2 } px` ;
913
+ } else {
914
+ val = `calc(${ val } - 2px)`
915
+ }
916
+ element . style . width = val ;
917
+ val = defaults . height ;
918
+ if ( ! isNaN ( val ) ) {
919
+ val = `${ val * h - 2 } px` ;
920
+ } else {
921
+ val = `calc(${ val } - 2px)`
922
+ }
923
+ element . style . height = val ;
924
+ element . style [ `font-size` ] = `${ h * 40 } px`
925
+ element . innerHTML = defaults . name ;
926
+ if ( index == blockIndex ) {
927
+ element . style . background = `#ddffdd` ;
928
+ element . style . border = `1px solid #00ff00` ;
929
+ element . style [ `font-weight` ] = `bold`
930
+ element . style [ `z-index` ] = 10 ;
931
+ element . style . color = `#006600` ;
932
+ } else {
933
+ element . style . border = `1px solid #ff0000` ;
934
+ element . style [ `text-align` ] = `right` ;
935
+ element . style [ `z-index` ] = 0 ;
936
+ element . style . color = `#ff0000` ;
937
+ }
938
+ } ) ;
939
+ } ;
940
+
941
+ // Run the presentation
942
+ const run = ( mode , then ) => {
943
+ if ( mode === `fullscreen` ) {
944
+ if ( document . fullscreenElement ) {
945
+ document . exitFullscreen ( ) ;
946
+ } else {
947
+ player . requestFullscreen ( ) ;
948
+ document . onfullscreenchange = ( ) => {
949
+ if ( document . fullscreenElement ) {
950
+ player = document . fullscreenElement ;
951
+ runPresentation ( then ) ;
952
+ } else {
953
+ player = playerElement ;
954
+ }
955
+ } ;
956
+ }
957
+ } else {
958
+ runPresentation ( then ) ;
959
+ }
960
+ }
961
+
962
+ const runPresentation = then => {
963
+ if ( ! script . stepping ) {
964
+ initScript ( ) ;
965
+ script . runMode = `auto` ;
966
+ script . speed = `normal` ;
967
+ script . singleStep = false ;
968
+ script . then = then ;
969
+ doStep ( script . steps [ 0 ] ) ;
970
+ }
971
+ } ;
972
+
973
+ // Stop the run
974
+ const stop = ( ) => {
975
+ script . stop = true ;
976
+ } ;
977
+
978
+ // Set a step callback
979
+ const onStep = onStepCB => {
980
+ script . onStepCB = onStepCB ;
981
+ } ;
982
+
983
+ // Remove all the CSS styles
984
+ const removeStyles = ( ) => {
985
+ const styles = document . getElementsByClassName ( "iwsy-css" ) ;
986
+ for ( const style of styles ) {
987
+ style . parentNode . removeChild ( style ) ;
988
+ }
989
+ } ;
990
+
991
+ ///////////////////////////////////////////////////////////////////////////
992
+
968
993
document . removeEventListener ( `click` , init ) ;
969
994
if ( script . runMode === `auto` ) {
970
995
document . addEventListener ( `click` , onClick ) ;
971
996
}
972
997
setupShowdown ( ) ;
973
998
initScript ( ) ;
974
- IWSY . plugins = { } ;
975
- if ( script . steps . length > 0 ) {
976
- doStep ( script . steps [ 0 ] ) ;
977
- }
978
999
return {
979
1000
setScript,
980
1001
gotoStep,
@@ -984,4 +1005,4 @@ const IWSY = (player, text) => {
984
1005
onStep,
985
1006
removeStyles
986
1007
} ;
987
- } ;
1008
+ } ;
0 commit comments