@@ -14,6 +14,7 @@ let queryList = []
1414let bl_projectId = null
1515store = null
1616let playAfterDragStop = [ ]
17+ let finishedSavingCB = [ ]
1718function mutationCallback ( ) {
1819 if ( typeof BL_UTILS == 'object' ) {
1920 if ( ! BL_UTILS . isDragging ( ) && playAfterDragStop . length > 0 ) {
@@ -70,6 +71,7 @@ var port
7071var isConnected = false ;
7172
7273function liveMessage ( message , res ) {
74+ if ( blockliveDeleted ) { return }
7375 reconnectIfNeeded ( )
7476 let msg = message
7577 if ( msg . meta == "blockly.event" || msg . meta == "sprite.proxy" || msg . meta == "vm.blockListen" || msg . meta == "vm.shareBlocks" || msg . meta == "vm.replaceBlocks" || msg . meta == "vm.updateBitmap" || msg . meta == "vm.updateSvg" || msg . meta == "version++" ) {
@@ -81,6 +83,7 @@ function liveMessage(message,res) {
8183let blockliveListener
8284
8385let registerChromePortListeners = ( ) => {
86+ if ( blockliveDeleted ) { return }
8487 port . onMessage . addListener ( ( ...args ) => { blockliveListener ( ...args ) } ) ;
8588 port . onDisconnect . addListener ( ( ) => {
8689 isConnected = false ;
@@ -89,6 +92,7 @@ let registerChromePortListeners = ()=> {
8992// registerChromePortListeners()
9093
9194function reconnectIfNeeded ( ) {
95+ if ( blockliveDeleted ) { return }
9296 if ( ! isConnected ) {
9397 port = chrome . runtime . connect ( exId ) ;
9498 isConnected = ( ! ! port ) ;
@@ -116,7 +120,9 @@ let onceProjectLoaded = []
116120let vm
117121let readyToRecieveChanges = false
118122
123+ let reloadAfterRestart = false
119124async function startBlocklive ( creatingNew ) {
125+ blockliveDeleted = false ;
120126 pauseEventHandling = true
121127 liveMessage ( { meta :"myId" , id :blId } )
122128 injectLoadingOverlay ( )
@@ -141,6 +147,7 @@ async function startBlocklive(creatingNew) {
141147 }
142148}
143149
150+ let blockliveDeleted = false ;
144151async function onTabLoad ( ) {
145152 // Get usable scratch id
146153 // await waitFor(()=>{!isNaN(parseFloat(location.pathname.split('/')[2]))})
@@ -205,14 +212,26 @@ async function joinExistingBlocklive(id) {
205212 pauseEventHandling = false ;
206213}
207214
215+ function unshareBlocklive ( ) {
216+ chrome . runtime . sendMessage ( exId , { meta :'leaveScratchId' , scratchId} ) ;
217+ removeBlockliveButtons ( ) ;
218+ blockliveDeleted = true ;
219+ port . disconnect ( )
220+ }
221+
208222function removeBlockliveButtons ( ) {
209223 try {
210224
211- document . querySelector ( "#app > div > div.gui_menu-bar-position_3U1T0.menu-bar_menu-bar_JcuHF.box_box_2jjDp > div.menu-bar_main-menu_3wjWH > blocklivecontainer" ) ?. remove ( )
212- document . querySelector ( "blocklive-init " ) ?. remove ( )
225+ // document.querySelector("#app > div > div.gui_menu-bar-position_3U1T0.menu-bar_menu-bar_JcuHF.box_box_2jjDp > div.menu-bar_main-menu_3wjWH > blocklivecontainer")?.remove()
226+ document . querySelector ( "#blRevert " ) ?. remove ( )
213227 document . querySelector ( "#noRefreshPanel" ) ?. remove ( )
214228 document . querySelector ( "#blUsersPanel" ) ?. remove ( )
215229 document . querySelector ( "#bl-chat" ) ?. remove ( )
230+
231+ blDropdown . style . display = 'none' ;
232+ blockliveButton . onclick = blActivateClick
233+ blId = null ;
234+
216235 } catch ( e ) { console . error ( e ) }
217236}
218237
@@ -294,6 +313,7 @@ setInterval(reconnectIfNeeded,1000)
294313/// other things
295314
296315 blockliveListener = async ( msg ) => {
316+ if ( blockliveDeleted ) { return }
297317 if ( typeof BL_UTILS != 'undefined' && BL_UTILS . isDragging ( ) ) {
298318 // dong add to list if its a move event on the current moving block
299319 if ( msg . meta == 'vm.blockListen' && msg . type == 'move' && msg . event . blockId == BL_UTILS . getDraggingId ( ) ) { return }
@@ -467,17 +487,22 @@ BL_UTILS.stageName = stageName
467487let lastProjectState = store . getState ( ) . scratchGui . projectState . loadingState
468488let lastTitle = store . getState ( ) . preview . projectInfo . title
469489let settingTitle = null
490+
470491store . subscribe ( function ( ) {
471492 // HANDLE PROJECT SAVE
472493 let state = store . getState ( ) . scratchGui . projectState . loadingState
473494 if ( lastProjectState != state ) { // If state changed
474495 lastProjectState = store . getState ( ) . scratchGui . projectState . loadingState
475-
496+ console . log ( 'state ' + state )
476497 if ( state . endsWith ( 'UPDATING' ) ) {
477498 console . log ( '🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢' )
478499 chrome . runtime . sendMessage ( exId , { meta :'projectSavedJSON' , blId, json :vm . toJSON ( ) , version :blVersion , } )
479500 // chrome.runtime.sendMessage(exId,{meta:'projectSaved',blId,scratchId,version:blVersion})
480501 }
502+ try {
503+ if ( state == 'SHOWING_WITH_ID' ) {
504+ finishedSavingCB . forEach ( ( func ) => { func ( ) } )
505+ } } catch ( e ) { console . error ( e ) }
481506 }
482507
483508 // HANDLE TITLE CHANGE
@@ -2127,7 +2152,8 @@ let shareDropdown = `
21272152
21282153<sharedWith style="display:flex;flex-direction: column;">
21292154 <text style="display:flex;align-self: left;padding-left:4px; padding-top:5px;padding-bottom:5px;font-size: large;">
2130- Shared With
2155+ Shared With
2156+ <unshare style="font-size:14px !important; align-self:center; margin-left:38px; justify-self:end; text-decoration:underline; color:blue; cursor:pointer; padding:2px; background-color:rgba(255,255,255,0.1); border-radius:5px;" onclick="unshareBlocklive()">Unshare</unshare>
21312157 </text>
21322158 <sharedList style="overflow: auto; max-height: 350px; display:flex; min-height: 20px; border-radius:10px;gap:5px;flex-direction: column; ">
21332159 <cell id="blModalExample" style="display:none; gap:10px;flex-direction: row; align-items: center;">
@@ -2168,7 +2194,10 @@ let shareDropdown = `
21682194
21692195 </cell>
21702196 </results>
2197+
2198+
21712199 </search>
2200+
21722201 </div>
21732202 </container>
21742203
@@ -2567,6 +2596,7 @@ function makeBlockliveButton() {
25672596}
25682597function makeRevertButton ( ) {
25692598 let button = document . createElement ( 'blocklive-init' )
2599+ button . id = 'blRevert'
25702600 button . className = 'button_outlined-button_1bS__ menu-bar_menu-bar-button_3IDN0 community-button_community-button_2Lo_g'
25712601
25722602
@@ -2637,6 +2667,17 @@ function addToCredits(text) {
26372667}
26382668
26392669let blActivateClick = async ( ) => {
2670+
2671+ if ( blockliveDeleted ) { reloadAfterRestart = true } //todo write code so that it can do this without restarting
2672+ if ( reloadAfterRestart ) {
2673+ finishedSavingCB . push ( ( ) => { location . reload ( ) } )
2674+ //stop spinny
2675+ document . querySelector ( 'loader.blockliveloader' ) . style . display = 'none'
2676+ reloadOnlineUsers ( )
2677+
2678+ blShareClick ( )
2679+ }
2680+
26402681 // change onclick
26412682 blockliveButton . onclick = undefined
26422683 // set spinny icon
@@ -2653,6 +2694,8 @@ let blActivateClick = async ()=>{
26532694 chrome . runtime . sendMessage ( exId , { json, meta :'create' , scratchId, title :store . getState ( ) . preview . projectInfo . title } , async ( response ) => {
26542695 blId = response . id
26552696
2697+
2698+
26562699 // ACTIVATE BLOKLIVE!!!
26572700 projectReplaceInitiated = true ;
26582701 pauseEventHandling = false
@@ -2674,6 +2717,7 @@ let blActivateClick = async ()=>{
26742717 reloadOnlineUsers ( )
26752718
26762719 blShareClick ( )
2720+
26772721 } )
26782722}
26792723let blShareClick = ( ) => { console . log ( 'clicked' ) ; blDropdown . style . display = ( blDropdown . style . display == 'none' ? 'flex' : 'none' ) ; refreshShareModal ( ) }
0 commit comments