Skip to content

Commit

Permalink
fix: normal collapse, collapsing top-level
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Dec 13, 2023
1 parent 8592e26 commit 676460a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
36 changes: 19 additions & 17 deletions examples/sample-p5-app/src/block_svg_patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ const originalToString = Blockly.BlockSvg.prototype.toString;
* or else the normal collapsed block string.
*/
Blockly.BlockSvg.prototype.toString = function() {
if (!this.llmSummary && !this.tryingToGetLlmSummary) {
this.tryingToGetLlmSummary = true;
getSummary(this).then(summary => {
this.tryingToGetLlmSummary = false;
this.llmSummary = summary;
this.renderEfficiently();
}).catch(reason => {
this.tryingToGetLlmSummary = false;
console.log(reason);
});
if (globalThis.doFanciness) {
if (!this.llmSummary && !this.tryingToGetLlmSummary) {
this.tryingToGetLlmSummary = true;
getSummary(this).then(summary => {
this.tryingToGetLlmSummary = false;
this.llmSummary = summary;
this.renderEfficiently();
}).catch(reason => {
this.tryingToGetLlmSummary = false;
console.log(reason);
});
}
}

return this.llmSummary ?? originalToString.call(this);
Expand Down Expand Up @@ -54,15 +56,15 @@ Blockly.BlockSvg.prototype.updateCollapsed_ = function() {
icon.updateCollapsed();
}

if (!collapsed) {
this.updateDisabled();
let i = 0;
while(this.getInput(collapsedInputName + i)) {
this.removeInput(collapsedInputName + i);
}
return;
this.updateDisabled();
let i = 0;
while(this.getInput(collapsedInputName + i)) {
this.removeInput(collapsedInputName + i);
i++;
}

if (!collapsed) return;

const text = this.toString();
const strings = Blockly.utils.string.wrap(text, 50).split('\n');
for (let i = 0; i < strings.length; i++) {
Expand Down
15 changes: 14 additions & 1 deletion examples/sample-p5-app/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ const fancyCollapseOption = {
return shouldExpand(scope) ? 'Expand ✨Fancily✨' : "Summarize ✨Fancily✨";
},
preconditionFn: () => {return 'enabled'},
weight: 100,
weight: 4,
id: 'FancyCollapse'
}
Blockly.ContextMenuRegistry.registry.register(fancyCollapseOption);
Expand All @@ -174,6 +174,9 @@ function shouldExpand(scope) {
const ws = scope.block.workspace;
const stacks = combineBlocks(ws, blockSelectionWeakMap.get(ws));
for (const stack of stacks) {
if (!stack.first.previousConnection && stack.first.isCollapsed()) {
return true;
}
for (const block of stack.blockList) {
if (block.type === 'collapse') {
return true;
Expand All @@ -184,7 +187,12 @@ function shouldExpand(scope) {
}

function fancyCollapse(stacks) {
globalThis.doFanciness = true;
for (const stack of stacks) {
if (!stack.first.previousConnection) {
stack.first.setCollapsed(true);
continue;
}
const prevParentConn = stack.first.previousConnection?.targetConnection;
const prevDanglerConn = stack.last.nextConnection?.targetConnection;
stack.first.previousConnection?.disconnect();
Expand All @@ -197,10 +205,15 @@ function fancyCollapse(stacks) {
prevDanglerConn?.connect(newBlock.nextConnection);
newBlock.setCollapsed(true);
}
globalThis.doFanciness = false;
}

function fancyExpand(stacks) {
for (const stack of stacks) {
if (!stack.first.previousConnection && stack.first.isCollapsed()) {
stack.first.setCollapsed(false);
continue;
}
for (const block of stack.blockList) {
const prevParentConn = block.previousConnection?.targetConnection;
const prevDanglerConn = block.nextConnection?.targetConnection;
Expand Down

0 comments on commit 676460a

Please sign in to comment.