{"code":"(window.webpackJsonpGUI=window.webpackJsonpGUI||[]).push([[54],{1505:function(e,t,a){(e.exports=a(9)(!1)).push([e.i,\".sa-swap-local-global-hint {\\n padding-bottom: 1rem;\\n}\\n.sa-swap-local-global-stage {\\n /* resolves scratch class style conflict */\\n display: block;\\n}\\n\",\"\"])},1579:function(e,t,a){\"use strict\";a.r(t),a.d(t,\"resources\",(function(){return n}));var s=a(1505);const n={\"userscript.js\":async function(e){let{addon:t,msg:a,console:s}=e;const n=await t.tab.traps.getBlockly(),i=t.tab.traps.vm,l=e=>e.isStage?n.ScratchMsgs.translate(\"SENSING_OF_STAGE\",\"Stage\"):e.getName(),o=e=>i.editingTarget.lookupVariableById(e),r=()=>i.editingTarget.isStage,c=(e,t)=>{\"string\"==typeof t&&(t=e.getVariableById(t)),e.variableMap_.deleteVariable(t)};let d=null;const p=()=>{d&&(d(),d=null)},u=function(e){const t=this.getEventWorkspace_();e?(d=m(t,this.varId),c(t,this.varId)):(t.createVariable(this.varName,this.varType,this.varId,this.isLocal,this.isCloud),p())},g=function(e){const t=this.getEventWorkspace_();e?(t.createVariable(this.varName,this.varType,this.varId,this.isLocal,this.isCloud),p()):(d=m(t,this.varId),c(t,this.varId))},b=()=>n.Events.fireNow_(),m=(e,t)=>{const a=i.runtime._monitorState.get(t),s=o(t);return()=>{b();const n=o(t);if(n&&(n.value=s.value),a){a.visible&&i.runtime.monitorBlocks.changeBlock({id:t,element:\"checkbox\",value:!0});const e=!i.runtime.getTargetForStage().variables[t];let l=a;if(e){const e=i.editingTarget;l=l.set(\"targetId\",e.id),l=l.set(\"spriteName\",e.getName())}else l=l.set(\"targetId\",null),l=l.set(\"spriteName\",null);if(n.name!==s.name){const e=i.runtime.monitorBlocks,a=e.getBlock(t);a&&(l=l.set(\"params\",e._getBlockParams(a)))}i.runtime.requestAddMonitor(l)}n.name!==s.name&&((e,t)=>{const a=e.getVariableById(t);for(const t of e.getAllBlocks())t.updateVarName(a)})(e,t)}},h=(e,t,s)=>{const o=\"☁ \",d=e.name,p=e.getId(),h=e.type,v=e.isLocal,f=e.isCloud;if(v===t&&f===s)return;if(s&&t)return void alert(a(\"cant-convert-cloud\"));const k=i.editingTarget;if(v!==t)if(t){if(r())return void alert(a(\"cant-convert-stage\"));const e=(e=>i.runtime.targets.filter((e=>e.isOriginal)).filter((t=>Object.values(t.blocks._blocks).find((t=>t.fields.LIST&&t.fields.LIST.id===e||t.fields.VARIABLE&&t.fields.VARIABLE.id===e)))))(p);if(!e.every((e=>e===k)))return void(e.length>1?alert(a(\"cant-convert-to-local\",{sprites:e.map(l).join(\", \")})):alert(a(\"cant-convert-used-elsewhere\",{sprite:l(e[0])})))}else{const e=((e,t)=>i.runtime.targets.filter((a=>a.isOriginal&&a.lookupVariableByNameAndType(e,t,!0))))(d,h).filter((e=>e!==k));if(e.length>0)return void alert(a(\"cant-convert-conflict\",{sprites:e.map(l).join(\", \")}))}let y=d;f!==s&&(s?y=o+d:d.startsWith(o)&&(y=d.replace(o,\"\")));const C=e.workspace,E=m(C,p);n.Events.setGroup(!0);try{c(C,e),C.createVariable(y,h,p,t,s)}finally{n.Events.setGroup(!1)}b();const S=C.undoStack_,_=S[S.length-1],I=S[S.length-2];_ instanceof n.Events.VarCreate&&I instanceof n.Events.VarDelete&&(_.run=g,I.run=u),E()},v=e=>{if(t.self.disabled)return;const s=document.querySelector('[class^=\"prompt_body_\"]');if(!s)return;const n=s.parentElement.querySelector('[class^=\"modal_header-item_\"]');n&&(\"\"===e.type?n.textContent=a(\"edit-variable-header\"):n.textContent=a(\"edit-list-header\"));const l=document.createElement(\"div\");t.tab.displayNoneWhileDisabled(l);const o=(e,t)=>{const a=document.createElement(\"label\"),s=document.createElement(\"input\");\"checkbox\"===t?s.type=\"checkbox\":(s.name=\"variableScopeOption\",s.type=\"radio\",s.value=t),a.appendChild(s);const n=document.createElement(\"span\");return n.textContent=e,a.appendChild(n),{outer:a,label:n,input:s}},c=t.tab.scratchClass(\"prompt_disabled-label\"),d=document.createElement(\"div\");d.className=t.tab.scratchClass(\"prompt_info-message\",\"prompt_cloud-option\",{others:\"sa-swap-local-global-stage\"}),d.appendChild(Object.assign(document.createElement(\"span\"),{textContent:t.tab.scratchMessage(\"gui.gui.variablePromptAllSpritesMessage\")}));const p=document.createElement(\"div\");p.className=t.tab.scratchClass(\"prompt_options-row\",\"prompt_cloud-option\");const u=o(t.tab.scratchMessage(\"gui.gui.variableScopeOptionAllSprites\"),\"global\"),g=o(t.tab.scratchMessage(\"gui.gui.variableScopeOptionSpriteOnly\"),\"local\");u.input.checked=!e.isLocal,g.input.checked=e.isLocal,p.appendChild(u.outer),p.appendChild(g.outer);const b=document.createElement(\"div\");b.className=t.tab.scratchClass(\"prompt_cloud-option\");const m=o(t.tab.scratchMessage(\"gui.gui.cloudVariableOption\"),\"checkbox\");m.input.checked=e.isCloud,i.runtime.canAddCloudVariable()||e.isCloud||(m.input.disabled=!0,b.classList.add(c)),b.appendChild(m.outer);const h=()=>{const e=m.input.checked;g.input.disabled=e,g.label.classList.toggle(c,e),e&&u.input.click()};m.input.addEventListener(\"change\",h),h();let v=!1;return r()?l.appendChild(d):(v=!0,l.appendChild(p)),\"\"===e.type&&(()=>{const e=document.querySelector('[class^=\"gui_blocks-wrapper_\"]');let a=e[t.tab.traps.getInternalKey(e)];for(;;){var s,n;if(!a)return!1;const e=null===(s=a.stateNode)||void 0===s||null===(n=s.props)||void 0===n?void 0:n.canUseCloud;if(\"boolean\"==typeof e)return e;a=a.child}})()&&(v=!0,l.appendChild(b)),v&&l.prepend(Object.assign(document.createElement(\"div\"),{textContent:a(\"edit\"),className:\"sa-swap-local-global-hint\"})),s.insertBefore(l,s.lastChild),{isLocal:()=>g.input.checked,isCloud:()=>m.input.checked}},f=n.Variables.renameVariable;n.Variables.renameVariable=function(e,a,s){const n=f.call(this,e,a,(function(){s&&s(...arguments),!t.self.disabled&&i&&h(a,i.isLocal(),i.isCloud())})),i=v(a);return n},t.tab.createBlockContextMenu(((e,s)=>{if(!t.self.disabled&&(\"data\"===s.getCategory()||\"data-lists\"===s.getCategory())){const t=s.workspace.getVariableById(s.getVars()[0]);t&&(e.length>0&&(e[0].text===n.ScratchMsgs.translate(\"RENAME_VARIABLE\")?e[0].text=a(\"edit-variable-option\"):e[0].text===n.ScratchMsgs.translate(\"RENAME_LIST\")&&(e[0].text=a(\"edit-list-option\"))),e.push({enabled:!0,separator:!0,text:a(\"to-\".concat(t.isLocal?\"global\":\"local\")),callback:()=>h(t,!t.isLocal,t.isCloud)}))}return e}),{flyout:!0,blocks:!0})},\"style.css\":a.n(s).a}}}]);","name":"js/pentapod/addon-entry-swap-local-global.1fa8de8f13233ee85304.js","input":"(window[\"webpackJsonpGUI\"] = window[\"webpackJsonpGUI\"] || []).push([[54],{\n\n/***/ 1505:\n/***/ (function(module, exports, __webpack_require__) {\n\nexports = module.exports = __webpack_require__(9)(false);\n// imports\n\n\n// module\nexports.push([module.i, \".sa-swap-local-global-hint {\\n padding-bottom: 1rem;\\n}\\n.sa-swap-local-global-stage {\\n /* resolves scratch class style conflict */\\n display: block;\\n}\\n\", \"\"]);\n\n// exports\n\n\n/***/ }),\n\n/***/ 1579:\n/***/ (function(module, __webpack_exports__, __webpack_require__) {\n\n\"use strict\";\n// ESM COMPAT FLAG\n__webpack_require__.r(__webpack_exports__);\n\n// EXPORTS\n__webpack_require__.d(__webpack_exports__, \"resources\", function() { return /* binding */ resources; });\n\n// CONCATENATED MODULE: ./src/addons/addons/swap-local-global/userscript.js\n/* harmony default export */ var userscript = (async function (_ref) {\n let {\n addon,\n msg,\n console\n } = _ref;\n const ScratchBlocks = await addon.tab.traps.getBlockly();\n const vm = addon.tab.traps.vm;\n const getTargetName = target => {\n if (target.isStage) {\n // Stage always has an internal name of \"Stage\", but we want a translatable name\n return ScratchBlocks.ScratchMsgs.translate(\"SENSING_OF_STAGE\", \"Stage\");\n }\n return target.getName();\n };\n const getTargetsThatUseVariable = id => vm.runtime.targets.filter(target => target.isOriginal).filter(target => Object.values(target.blocks._blocks).find(block => block.fields.LIST && block.fields.LIST.id === id || block.fields.VARIABLE && block.fields.VARIABLE.id === id));\n\n // https://github.com/scratchfoundation/scratch-vm/blob/7c6f1e44fb0a9b0d0279225cd4c62fbe59b6af54/src/engine/blocks.js#L388-L394\n const getTargetsWithLocalVariableNamed = (name, type) => vm.runtime.targets.filter(target => target.isOriginal && target.lookupVariableByNameAndType(name, type, true));\n const getVmVariable = id => vm.editingTarget.lookupVariableById(id);\n const isStageSelected = () => vm.editingTarget.isStage;\n const deleteVariableWithoutDeletingBlocks = (workspace, variable) => {\n // variable can be an ID or an actual Blockly variable object\n if (typeof variable === \"string\") {\n variable = workspace.getVariableById(variable);\n }\n workspace.variableMap_.deleteVariable(variable);\n };\n const syncBlockVariableNameWithActualVariableName = (workspace, id) => {\n const variable = workspace.getVariableById(id);\n for (const block of workspace.getAllBlocks()) {\n block.updateVarName(variable);\n }\n };\n let _undoRedoPreserveStateCallback = null;\n const finishUndoRedoState = () => {\n if (_undoRedoPreserveStateCallback) {\n _undoRedoPreserveStateCallback();\n _undoRedoPreserveStateCallback = null;\n }\n };\n\n // https://github.com/scratchfoundation/scratch-blocks/blob/0d6012df1e18e66d82c1247f1f6d772a719982a7/core/variable_events.js#L194\n const customUndoVarDelete = function customUndoVarDelete(forward) {\n const workspace = this.getEventWorkspace_();\n if (forward) {\n _undoRedoPreserveStateCallback = beginPreservingState(workspace, this.varId);\n deleteVariableWithoutDeletingBlocks(workspace, this.varId);\n } else {\n workspace.createVariable(this.varName, this.varType, this.varId, this.isLocal, this.isCloud);\n finishUndoRedoState();\n }\n };\n\n // https://github.com/scratchfoundation/scratch-blocks/blob/0d6012df1e18e66d82c1247f1f6d772a719982a7/core/variable_events.js#L131\n const customUndoVarCreate = function customUndoVarCreate(forward) {\n const workspace = this.getEventWorkspace_();\n if (forward) {\n workspace.createVariable(this.varName, this.varType, this.varId, this.isLocal, this.isCloud);\n finishUndoRedoState();\n } else {\n _undoRedoPreserveStateCallback = beginPreservingState(workspace, this.varId);\n deleteVariableWithoutDeletingBlocks(workspace, this.varId);\n }\n };\n const flushBlocklyEventQueue = () => ScratchBlocks.Events.fireNow_();\n const beginPreservingState = (workspace, id) => {\n // oldMonitorState is an instance of https://github.com/scratchfoundation/scratch-vm/blob/develop/src/engine/monitor-record.js or undefined\n const oldMonitorState = vm.runtime._monitorState.get(id);\n const oldVmVariable = getVmVariable(id);\n return () => {\n flushBlocklyEventQueue();\n const newVmVariable = getVmVariable(id);\n if (newVmVariable) {\n newVmVariable.value = oldVmVariable.value;\n }\n\n // Update the variable monitor (state is maintained separately)\n if (oldMonitorState) {\n if (oldMonitorState.visible) {\n vm.runtime.monitorBlocks.changeBlock({\n id,\n element: \"checkbox\",\n value: true\n });\n }\n const isLocal = !vm.runtime.getTargetForStage().variables[id];\n let newMonitorState = oldMonitorState;\n if (isLocal) {\n const target = vm.editingTarget;\n newMonitorState = newMonitorState.set(\"targetId\", target.id);\n newMonitorState = newMonitorState.set(\"spriteName\", target.getName());\n } else {\n newMonitorState = newMonitorState.set(\"targetId\", null);\n newMonitorState = newMonitorState.set(\"spriteName\", null);\n }\n if (newVmVariable.name !== oldVmVariable.name) {\n const monitorBlocks = vm.runtime.monitorBlocks;\n const block = monitorBlocks.getBlock(id);\n if (block) {\n newMonitorState = newMonitorState.set(\"params\", monitorBlocks._getBlockParams(block));\n }\n }\n vm.runtime.requestAddMonitor(newMonitorState);\n }\n if (newVmVariable.name !== oldVmVariable.name) {\n syncBlockVariableNameWithActualVariableName(workspace, id);\n }\n };\n };\n const convertVariable = (oldBlocklyVariable, newLocal, newCloud) => {\n const CLOUD_PREFIX = \"☁ \";\n const name = oldBlocklyVariable.name;\n const id = oldBlocklyVariable.getId();\n const type = oldBlocklyVariable.type;\n const isLocal = oldBlocklyVariable.isLocal;\n const isCloud = oldBlocklyVariable.isCloud;\n if (isLocal === newLocal && isCloud === newCloud) {\n return;\n }\n\n // Cloud variables must always be global\n if (newCloud && newLocal) {\n alert(msg(\"cant-convert-cloud\"));\n return;\n }\n const editingTarget = vm.editingTarget;\n if (isLocal !== newLocal) {\n if (newLocal) {\n // Stage cannot have local variables\n if (isStageSelected()) {\n alert(msg(\"cant-convert-stage\"));\n return;\n }\n // Variables used by unfocused sprites cannot be made local\n // That includes cases where the variable is used by multiple sprites and where it's only used by an unfocused sprite\n const targets = getTargetsThatUseVariable(id);\n if (!targets.every(i => i === editingTarget)) {\n if (targets.length > 1) {\n alert(msg(\"cant-convert-to-local\", {\n sprites: targets.map(getTargetName).join(\", \")\n }));\n } else {\n alert(msg(\"cant-convert-used-elsewhere\", {\n sprite: getTargetName(targets[0])\n }));\n }\n return;\n }\n } else {\n // Global variables must not conflict with any local variables\n const targets = getTargetsWithLocalVariableNamed(name, type).filter(target => target !== editingTarget);\n if (targets.length > 0) {\n alert(msg(\"cant-convert-conflict\", {\n sprites: targets.map(getTargetName).join(\", \")\n }));\n return;\n }\n }\n }\n let newName = name;\n if (isCloud !== newCloud) {\n if (newCloud) {\n newName = CLOUD_PREFIX + name;\n } else if (name.startsWith(CLOUD_PREFIX)) {\n newName = name.replace(CLOUD_PREFIX, \"\");\n }\n }\n const workspace = oldBlocklyVariable.workspace;\n const finishPreservingState = beginPreservingState(workspace, id);\n ScratchBlocks.Events.setGroup(true);\n try {\n deleteVariableWithoutDeletingBlocks(workspace, oldBlocklyVariable);\n workspace.createVariable(newName, type, id, newLocal, newCloud);\n } finally {\n ScratchBlocks.Events.setGroup(false);\n }\n\n // 2 items will be added to the queue: a variable create and delete\n // We override their undo handlers to make undo/redo work properly\n flushBlocklyEventQueue();\n const stack = workspace.undoStack_;\n const createEvent = stack[stack.length - 1];\n const deleteEvent = stack[stack.length - 2];\n if (createEvent instanceof ScratchBlocks.Events.VarCreate && deleteEvent instanceof ScratchBlocks.Events.VarDelete) {\n createEvent.run = customUndoVarCreate;\n deleteEvent.run = customUndoVarDelete;\n }\n finishPreservingState();\n };\n const canUserUseCloudVariables = () => {\n const blocksWrapper = document.querySelector('[class^=\"gui_blocks-wrapper_\"]');\n let internalNode = blocksWrapper[addon.tab.traps.getInternalKey(blocksWrapper)];\n while (true) {\n var _internalNode$stateNo, _internalNode$stateNo2;\n if (!internalNode) {\n return false;\n }\n const canUseCloud = (_internalNode$stateNo = internalNode.stateNode) === null || _internalNode$stateNo === void 0 ? void 0 : (_internalNode$stateNo2 = _internalNode$stateNo.props) === null || _internalNode$stateNo2 === void 0 ? void 0 : _internalNode$stateNo2.canUseCloud;\n if (typeof canUseCloud === \"boolean\") {\n return canUseCloud;\n }\n internalNode = internalNode.child;\n }\n };\n const addMoreOptionsToPrompt = variable => {\n if (addon.self.disabled) {\n return;\n }\n const promptBody = document.querySelector('[class^=\"prompt_body_\"]');\n if (!promptBody) {\n return;\n }\n const headerTitle = promptBody.parentElement.querySelector('[class^=\"modal_header-item_\"]');\n if (headerTitle) {\n if (variable.type === \"\") {\n headerTitle.textContent = msg(\"edit-variable-header\");\n } else {\n headerTitle.textContent = msg(\"edit-list-header\");\n }\n }\n const root = document.createElement(\"div\");\n addon.tab.displayNoneWhileDisabled(root);\n const createLabeledInput = (text, value) => {\n const outer = document.createElement(\"label\");\n const input = document.createElement(\"input\");\n if (value === \"checkbox\") {\n input.type = \"checkbox\";\n } else {\n input.name = \"variableScopeOption\";\n input.type = \"radio\";\n input.value = value;\n }\n outer.appendChild(input);\n const label = document.createElement(\"span\");\n label.textContent = text;\n outer.appendChild(label);\n return {\n outer,\n label,\n input\n };\n };\n const promptDisabledClass = addon.tab.scratchClass(\"prompt_disabled-label\");\n const noLocalsInStageSection = document.createElement(\"div\");\n noLocalsInStageSection.className = addon.tab.scratchClass(\"prompt_info-message\", \"prompt_cloud-option\", {\n others: \"sa-swap-local-global-stage\"\n });\n noLocalsInStageSection.appendChild(Object.assign(document.createElement(\"span\"), {\n textContent: addon.tab.scratchMessage(\"gui.gui.variablePromptAllSpritesMessage\")\n }));\n const scopeSection = document.createElement(\"div\");\n scopeSection.className = addon.tab.scratchClass(\"prompt_options-row\", \"prompt_cloud-option\");\n const forAllSprites = createLabeledInput(addon.tab.scratchMessage(\"gui.gui.variableScopeOptionAllSprites\"), \"global\");\n const forThisSpriteOnly = createLabeledInput(addon.tab.scratchMessage(\"gui.gui.variableScopeOptionSpriteOnly\"), \"local\");\n forAllSprites.input.checked = !variable.isLocal;\n forThisSpriteOnly.input.checked = variable.isLocal;\n scopeSection.appendChild(forAllSprites.outer);\n scopeSection.appendChild(forThisSpriteOnly.outer);\n const cloudSection = document.createElement(\"div\");\n cloudSection.className = addon.tab.scratchClass(\"prompt_cloud-option\");\n const cloudCheckbox = createLabeledInput(addon.tab.scratchMessage(\"gui.gui.cloudVariableOption\"), \"checkbox\");\n cloudCheckbox.input.checked = variable.isCloud;\n if (!vm.runtime.canAddCloudVariable() && !variable.isCloud) {\n cloudCheckbox.input.disabled = true;\n cloudSection.classList.add(promptDisabledClass);\n }\n cloudSection.appendChild(cloudCheckbox.outer);\n const updateDisabledInputs = () => {\n const thisSpriteOnlyDisabled = cloudCheckbox.input.checked;\n forThisSpriteOnly.input.disabled = thisSpriteOnlyDisabled;\n forThisSpriteOnly.label.classList.toggle(promptDisabledClass, thisSpriteOnlyDisabled);\n if (thisSpriteOnlyDisabled) {\n forAllSprites.input.click();\n }\n };\n cloudCheckbox.input.addEventListener(\"change\", updateDisabledInputs);\n updateDisabledInputs();\n let isAnythingConfigurable = false;\n if (isStageSelected()) {\n root.appendChild(noLocalsInStageSection);\n } else {\n isAnythingConfigurable = true;\n root.appendChild(scopeSection);\n }\n if (variable.type === \"\" && canUserUseCloudVariables()) {\n isAnythingConfigurable = true;\n root.appendChild(cloudSection);\n }\n if (isAnythingConfigurable) {\n root.prepend(Object.assign(document.createElement(\"div\"), {\n textContent: msg(\"edit\"),\n className: \"sa-swap-local-global-hint\"\n }));\n }\n promptBody.insertBefore(root, promptBody.lastChild);\n return {\n isLocal: () => forThisSpriteOnly.input.checked,\n isCloud: () => cloudCheckbox.input.checked\n };\n };\n\n // https://github.com/scratchfoundation/scratch-blocks/blob/c5014f61e2e538e99601a9e0cb39e339e44c3910/core/variables.js#L470\n const originalRenameVariable = ScratchBlocks.Variables.renameVariable;\n ScratchBlocks.Variables.renameVariable = function (workspace, variable, opt_callback) {\n const ret = originalRenameVariable.call(this, workspace, variable, function () {\n if (opt_callback) {\n opt_callback(...arguments);\n }\n if (!addon.self.disabled && prompt) {\n convertVariable(variable, prompt.isLocal(), prompt.isCloud());\n }\n });\n const prompt = addMoreOptionsToPrompt(variable);\n return ret;\n };\n addon.tab.createBlockContextMenu((items, block) => {\n if (!addon.self.disabled && (block.getCategory() === \"data\" || block.getCategory() === \"data-lists\")) {\n const variable = block.workspace.getVariableById(block.getVars()[0]);\n if (variable) {\n if (items.length > 0) {\n if (items[0].text === ScratchBlocks.ScratchMsgs.translate(\"RENAME_VARIABLE\")) {\n items[0].text = msg(\"edit-variable-option\");\n } else if (items[0].text === ScratchBlocks.ScratchMsgs.translate(\"RENAME_LIST\")) {\n items[0].text = msg(\"edit-list-option\");\n }\n }\n items.push({\n enabled: true,\n separator: true,\n text: msg(\"to-\".concat(variable.isLocal ? \"global\" : \"local\")),\n callback: () => convertVariable(variable, !variable.isLocal, variable.isCloud)\n });\n }\n }\n return items;\n }, {\n flyout: true,\n blocks: true\n });\n});\n// EXTERNAL MODULE: ./node_modules/css-loader!./src/addons/addons/swap-local-global/style.css\nvar style = __webpack_require__(1505);\nvar style_default = /*#__PURE__*/__webpack_require__.n(style);\n\n// CONCATENATED MODULE: ./src/addons/addons/swap-local-global/_runtime_entry.js\n/* generated by pull.js */\n\n\nconst resources = {\n \"userscript.js\": userscript,\n \"style.css\": style_default.a\n};\n\n/***/ })\n\n}]);","inputSourceMap":null}