Skip to content

Commit cb5df01

Browse files
authored
Add files via upload
1 parent 92e4947 commit cb5df01

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
import { modifiedCreateAllInputs, modifiedUpdateDeclarationProcCode } from "./modified-funcs.js";
2+
3+
export default async function ({ addon, console }) {
4+
function createArrow(direction, callback) {
5+
const path = direction === "left" ? "M 17 13 L 9 21 L 17 30" : "M 9 13 L 17 21 L 9 30";
6+
7+
Blockly.WidgetDiv.DIV.insertAdjacentHTML(
8+
"beforeend",
9+
`
10+
<svg width="20px" height="40px"
11+
style="left: ${direction === "left" ? "calc(50% - 20px)" : "calc(50% + 20px)"}"
12+
class="blocklyTextShiftArrow">
13+
<path d="${path}" fill="none" stroke="#FF661A" stroke-width="2"></path>
14+
</svg>`
15+
);
16+
17+
Blockly.WidgetDiv.DIV.lastChild.addEventListener("click", callback);
18+
}
19+
20+
//https://github.com/scratchfoundation/scratch-blocks/blob/f210e042988b91bcdc2abeca7a2d85e178edadb2/blocks_vertical/procedures.js#L674
21+
function modifiedRemoveFieldCallback(field) {
22+
// Do not delete if there is only one input
23+
if (this.inputList.length === 1) {
24+
return;
25+
}
26+
var inputNameToRemove = null;
27+
for (var n = 0; n < this.inputList.length; n++) {
28+
var input = this.inputList[n];
29+
if (input.connection) {
30+
var target = input.connection.targetBlock();
31+
if (target.getField(field.name) == field) {
32+
inputNameToRemove = input.name;
33+
}
34+
} else {
35+
for (var j = 0; j < input.fieldRow.length; j++) {
36+
if (input.fieldRow[j] == field) {
37+
inputNameToRemove = input.name;
38+
}
39+
}
40+
}
41+
}
42+
if (inputNameToRemove) {
43+
Blockly.WidgetDiv.hide(true);
44+
this.removeInput(inputNameToRemove);
45+
this.onChangeFn(true); // this is the only part we changed. We added this boolean input, which lets us switch on the merging.
46+
this.updateDisplay_();
47+
}
48+
}
49+
50+
function addInputAfter(addInputFn, fnName) {
51+
return function () {
52+
const sourceBlock = selectedField?.sourceBlock_;
53+
const proc = sourceBlock ? (sourceBlock.parentBlock_ ? sourceBlock.parentBlock_ : sourceBlock) : this;
54+
55+
// if a label is added, scratch's code will directly append the label text to the procCode
56+
// We account for this with a hacky method of adding the delimiter at the end of the last label input
57+
if (fnName === "addLabelExternal") {
58+
const lastInput = proc.inputList[proc.inputList.length - 1];
59+
if (lastInput.type === Blockly.DUMMY_INPUT) {
60+
lastInput.fieldRow[0].setValue(lastInput.fieldRow[0].getValue() + " %l");
61+
}
62+
}
63+
64+
proc.onChangeFn(true);
65+
if (sourceBlock === null || sourceBlock === undefined || !addon.settings.get("InsertInputsAfter"))
66+
return addInputFn.call(this, ...arguments);
67+
68+
let newPosition = getFieldInputNameAndIndex(selectedField, proc.inputList).index + 1;
69+
70+
addInputFn.call(proc, ...arguments);
71+
72+
const lastInputName = proc.inputList[proc.inputList.length - 1].name;
73+
shiftInput(proc, lastInputName, newPosition);
74+
};
75+
}
76+
77+
function getFieldInputNameAndIndex(field, inputList) {
78+
for (const [i, input] of inputList.entries()) {
79+
const isTargetField = input.connection
80+
? input.connection.targetBlock()?.getField(field.name) === field
81+
: input.fieldRow.includes(field);
82+
83+
if (isTargetField) {
84+
return {
85+
name: input.name,
86+
index: i,
87+
};
88+
}
89+
}
90+
}
91+
92+
function shiftInput(procedureBlock, inputNameToShift, newPosition) {
93+
const initialInputListLength = procedureBlock.inputList.length;
94+
95+
// return if inputNameToShift and newPosition are not valid
96+
if (!(inputNameToShift && newPosition >= 0 && newPosition <= initialInputListLength)) {
97+
return false;
98+
}
99+
100+
const originalPosition = procedureBlock.inputList.findIndex((input) => input.name === inputNameToShift);
101+
const itemToMove = procedureBlock.inputList.splice(originalPosition, 1)[0];
102+
procedureBlock.inputList.splice(newPosition, 0, itemToMove);
103+
104+
Blockly.Events.disable();
105+
try {
106+
procedureBlock.onChangeFn(true);
107+
procedureBlock.updateDisplay_();
108+
} finally {
109+
Blockly.Events.enable();
110+
}
111+
112+
focusOnInput(procedureBlock.inputList[newPosition]);
113+
}
114+
115+
function focusOnInput(input) {
116+
if (!input) return;
117+
if (input.type === Blockly.DUMMY_INPUT) {
118+
input.fieldRow[0].showEditor_();
119+
} else if (input.type === Blockly.INPUT_VALUE) {
120+
const target = input.connection.targetBlock();
121+
target.getField("TEXT").showEditor_();
122+
}
123+
}
124+
125+
function shiftFieldCallback(sourceBlock, field, direction) {
126+
const proc = sourceBlock.parentBlock_ ? sourceBlock.parentBlock_ : sourceBlock;
127+
128+
// if inputList length is 1 there's nowhere to shift the input so we can simply return
129+
if (proc.inputList.length <= 1) return;
130+
131+
const { name, index } = getFieldInputNameAndIndex(field, proc.inputList);
132+
const newPosition = direction === "left" ? index - 1 : index + 1;
133+
shiftInput(proc, name, newPosition);
134+
}
135+
136+
function polluteProcedureDeclaration(procedureDeclaration, save_original = true) {
137+
procedureDeclaration.createAllInputs_ = modifiedCreateAllInputs;
138+
procedureDeclaration.onChangeFn = modifiedUpdateDeclarationProcCode;
139+
procedureDeclaration.removeFieldCallback = modifiedRemoveFieldCallback;
140+
141+
for (const inputFn of ["addLabelExternal", "addBooleanExternal", "addStringNumberExternal"]) {
142+
if (save_original) {
143+
originalAddFns[inputFn] = procedureDeclaration[inputFn];
144+
}
145+
procedureDeclaration[inputFn] = addInputAfter(procedureDeclaration[inputFn], inputFn);
146+
}
147+
}
148+
149+
function depolluteProcedureDeclaration(procedureDeclaration) {
150+
procedureDeclaration.createAllInputs_ = originalCreateAllInputs;
151+
procedureDeclaration.onChangeFn = originalUpdateDeclarationProcCode;
152+
procedureDeclaration.removeFieldCallback = originalRemoveFieldCallback;
153+
154+
for (const [inputFnName, originalFn] of Object.entries(originalAddFns)) {
155+
procedureDeclaration[inputFnName] = originalFn;
156+
}
157+
}
158+
159+
function getExistingProceduresDeclarationBlock() {
160+
// Blockly.getMainWorkspace is required for this to work.
161+
// for future reference "upgrading" to addon.tab.traps.getWorkspace() will cause bugs.
162+
return Blockly.getMainWorkspace()
163+
.getAllBlocks()
164+
.find((block) => block.type === "procedures_declaration");
165+
}
166+
167+
function enableAddon() {
168+
// pollute the procedures_declaration prototype with a modified version that prevents merging, and allows inserting after
169+
polluteProcedureDeclaration(Blockly.Blocks["procedures_declaration"]);
170+
171+
// if custom procedures modal is already open we also directly pollute the existing procedures_declaration block
172+
if (addon.tab.redux.state.scratchGui.customProcedures.active) {
173+
polluteProcedureDeclaration(getExistingProceduresDeclarationBlock(), false);
174+
}
175+
176+
Blockly.FieldTextInputRemovable.prototype.showEditor_ = function () {
177+
originalShowEditor.call(this);
178+
createArrow("left", () => shiftFieldCallback(this.sourceBlock_, this, "left"));
179+
createArrow("right", () => shiftFieldCallback(this.sourceBlock_, this, "right"));
180+
selectedField = this;
181+
};
182+
}
183+
184+
function disableAddon() {
185+
// depollute the procedures_declaration prototype
186+
depolluteProcedureDeclaration(Blockly.Blocks["procedures_declaration"]);
187+
188+
// if custom procedures modal is already open we also directly depollute the existing procedures_declaration block
189+
if (addon.tab.redux.state.scratchGui.customProcedures.active) {
190+
depolluteProcedureDeclaration(getExistingProceduresDeclarationBlock());
191+
}
192+
193+
Blockly.FieldTextInputRemovable.prototype.showEditor_ = originalShowEditor;
194+
Blockly.WidgetDiv.DIV.querySelectorAll(".blocklyTextShiftArrow").forEach((e) => e.remove());
195+
}
196+
197+
const Blockly = await addon.tab.traps.getBlockly();
198+
const originalCreateAllInputs = Blockly.Blocks["procedures_declaration"].createAllInputs_;
199+
const originalUpdateDeclarationProcCode = Blockly.Blocks["procedures_declaration"].onChangeFn;
200+
const originalRemoveFieldCallback = Blockly.Blocks["procedures_declaration"].removeFieldCallback;
201+
const originalShowEditor = Blockly.FieldTextInputRemovable.prototype.showEditor_;
202+
let originalAddFns = {};
203+
let selectedField = null;
204+
205+
addon.self.addEventListener("disabled", disableAddon);
206+
addon.self.addEventListener("reenabled", enableAddon);
207+
enableAddon();
208+
}

0 commit comments

Comments
 (0)