Skip to content

Commit

Permalink
Functional custom definitor blocks arrived to Snavanced!
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandrito123 committed Nov 23, 2024
1 parent a893717 commit 3b43079
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 231 deletions.
4 changes: 2 additions & 2 deletions snap.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<META property="og:title" content="Snavanced!">
<META property="og:description" content="Build Your Own Blocks...">
<META property="og:url" content="http://alessandrito123.github.io/Snavanced/snap.html">
<META property="og:image" content="/mascot.png">
<META property="og:image" content="mascot.png">

<TITLE>Snavanced!</TITLE>

Expand Down Expand Up @@ -46,7 +46,7 @@
<SCRIPT type="text/javascript" src="src/api.js"></SCRIPT>
<SCRIPT type="text/javascript" src="src/sha512.js"></SCRIPT>
<SCRIPT type="text/javascript" src="src/motors.js"></SCRIPT>
<SCRIPT type="text/javascript" src="src/bmpn.js"></SCRIPT>
<SCRIPT type="text/javascript" src="src/bpmn.js"></SCRIPT>
<SCRIPT type="text/javascript" src="src/charts.js"></SCRIPT>
<SCRIPT type="text/javascript" src="src/desktop.js"></SCRIPT>
<SCRIPT type="text/javascript" src="src/compilers.js"></SCRIPT>
Expand Down
114 changes: 46 additions & 68 deletions src/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,7 @@ function () {return this.isStatic;});

SyntaxElementMorph.prototype.topBlock = function (
) {if (this.parent && ((this.parent).topBlock)) {
return this.parent.topBlock();}; return this;};
return this.parent.topBlock();}; return this;};

// SyntaxElementMorph reachable variables

Expand Down Expand Up @@ -3615,10 +3615,7 @@ BlockMorph.prototype.userMenu = function () {
if (this.parent.parentThatIsA(RingMorph)) {
menu.addLine();
menu.addItem("unquote", 'unringify');
if (this instanceof ReporterBlockMorph ||
(!(top instanceof HatBlockMorph) || !(top instanceof DefinitorBlockMorph))) {
menu.addItem("quote", 'ringify');
};
menu.addItem("quote", 'ringify');
return menu;
};
if (contains(
Expand All @@ -3633,17 +3630,11 @@ BlockMorph.prototype.userMenu = function () {
'showMessageUsers'
);
};
if (this.parent instanceof ReporterSlotMorph
|| (this.parent instanceof CommandSlotMorph)
|| (this instanceof DefinitorBlockMorph)
|| (this instanceof HatBlockMorph)
|| (this instanceof CommandBlockMorph
&& (top instanceof HatBlockMorph))) {
return menu;
};
if (!hasLine) {menu.addLine();};
menu.addItem("quote", 'ringify');
if (StageMorph.prototype.enableCodeMapping) {
if ((this.parent instanceof ReporterSlotMorph
) || (this.parent instanceof CommandSlotMorph)
) {return menu;}; if (!hasLine) {menu.addLine(
);}; menu.addItem("quote", 'ringify'); if (
StageMorph.prototype.enableCodeMapping) {
menu.addLine();
menu.addItem(
'header mapping...',
Expand Down Expand Up @@ -3844,10 +3835,7 @@ center = top.fullBounds().center(); if (this.parent === null) {return null;}; to
if (this instanceof ReporterBlockMorph) {
this.parent.replaceInput(this, ring, true); // don't vanish
ring.embed(this, null, true); // don't vanish
} else if (top) { // command
if (top instanceof HatBlockMorph) {
return;
}
} else if (top) {
top.parent.add(ring);
ring.embed(top);
ring.setCenter(center);
Expand Down Expand Up @@ -4316,7 +4304,7 @@ BlockMorph.prototype.components = function (parameterNames = []) {
if (this instanceof ReporterBlockMorph) {
return this.syntaxTree(parameterNames);
}
var seq = new List(this.blockSequence()).map((block, i) =>
var seq = new List(this.metaSequence()).map((block, i) =>
block.syntaxTree(i < 1 ? parameterNames : [])
);
return seq.length() === 1 ? seq.at(1) : seq;
Expand Down Expand Up @@ -5118,14 +5106,12 @@ BlockMorph.prototype.backupRender = function (ctx) {
this.drawMethodIcon(ctx);};};

BlockMorph.prototype.drawMethodIcon = function (ctx) {
var ext = this.methodIconExtent(),
w = ext.x, h = ext.y, r = w / 2,
x = this.edge + this.labelPadding,
y = this.edge,
isNormal =
this.color === SpriteMorph.prototype.blockColorFor(this.category);
if (this.isPredicate || this.isArrow) {
x = this.rounding;
var ext = this.methodIconExtent(), w = ext.x, h = ext.y,
r = w / 2, x = this.edge + this.labelPadding, y = (((this
) instanceof CustomDefinitorBlockMorph) ? (this.hatHeight
) : this.edge), isNormal = (this.color === (SpriteMorph
).prototype.blockColorFor(this.category)); if ((this
).isPredicate || this.isArrow) {x = this.rounding;
}; if (this instanceof CommandBlockMorph) {
y += this.corner;
}; ctx.fillStyle = (isNormal ? this.bright() : this.dark());
Expand Down Expand Up @@ -5476,9 +5462,8 @@ receiver.parentThatIsA(StageMorph
).toggleProcess(top, receiver);};};};

BlockMorph.prototype.focus = function () {
var scripts = this.parentThatIsA(ScriptsMorph),
world = this.world(),
focus;
var scripts = this.parentThatIsA(
ScriptsMorph), focus;
if (!scripts || !ScriptsMorph.prototype.enableKeyboard) {return; }
if (scripts.focus) {scripts.focus.stopEditing(); }
world.stopEditing();
Expand Down Expand Up @@ -5646,7 +5631,8 @@ BlockMorph.prototype.snap = function () {
top.addHighlight(top.removeHighlight());
};
// register generic hat blocks
if (this.selector === 'receiveCondition') {
if ((this instanceof CustomDefinitorBlockMorph
) || (this.selector === 'receiveCondition')) {
receiver = top.scriptTarget();
if (receiver) {
stage = receiver.parentThatIsA(StageMorph);
Expand Down Expand Up @@ -5699,10 +5685,12 @@ CommandBlockMorph.prototype.init = function () {

// CommandBlockMorph enumerating:

CommandBlockMorph.prototype.blockSequence = function () {
CommandBlockMorph.prototype.metaSequence = function () {
var sequence = [this], nb = this.nextBlock(); while (nb) {
sequence.push(nb); nb = nb.nextBlock();}; return sequence;};

CommandBlockMorph.prototype.blockSequence = CommandBlockMorph.prototype.metaSequence;

CommandBlockMorph.prototype.bottomBlock = function () {if ((this
).nextBlock()) {return this.nextBlock().bottomBlock();}; return this;};

Expand Down Expand Up @@ -5772,26 +5760,24 @@ CommandBlockMorph.prototype.dentCenter = function () {

CommandBlockMorph.prototype.attachTargets = function () {
var answer = [],
tp;
if (!((this instanceof HatBlockMorph) || (this instanceof DefinitorBlockMorph))) {
tp = this.topAttachPoint();
if (!(this.parent instanceof SyntaxElementMorph)) {
if (!this.parentThatIsA(CommandSlotMorph)) {
answer.push({
point: tp,
element: this,
loc: 'top',
loc: 'wrap',
type: 'block'
});
}
if (ScriptsMorph.prototype.enableNestedAutoWrapping ||
!this.parentThatIsA(CommandSlotMorph)) {
if (!((this instanceof HatBlockMorph) || (this instanceof DefinitorBlockMorph))) {
if (!(this.parent instanceof SyntaxElementMorph)) {
answer.push({
point: tp,
element: this,
loc: 'wrap',
loc: 'top',
type: 'block'
});
}
};
}
if (!this.isStop()) {
answer.push({
Expand Down Expand Up @@ -5836,12 +5822,8 @@ CommandBlockMorph.prototype.closestAttachTarget = function (newParent) {
thresh = Math.max(
this.corner * 2 + this.dent,
this.minSnapDistance
),
dist,
ref = [],
minDist = 1000,
wrap;

), dist, wrap, ref = [],
minDist = 1000;
if (!((this instanceof HatBlockMorph) || (this instanceof DefinitorBlockMorph))) {
ref.push(
{
Expand Down Expand Up @@ -5961,7 +5943,7 @@ CommandBlockMorph.prototype.snap = function (hand) {

// fix zebra coloring.
// this could probably be generalized into the fixBlockColor mechanism
target.element.blockSequence().forEach(cmd =>
target.element.metaSequence().forEach(cmd =>
cmd.fixBlockColor()
);
};
Expand Down Expand Up @@ -6309,17 +6291,8 @@ if (asABool(isPrototypeLike)) {this.category = 'custom'; this.setSpec(localize('

// HatBlockMorph enumerating:

HatBlockMorph.prototype.blockSequence = function () {
// override my inherited method so that I am not part of my sequence
var result = HatBlockMorph.uber.blockSequence.call(this);
result.shift(); return result;
};

// HatBlockMorph syntax analysis

HatBlockMorph.prototype.reify = function (
) {var nb = this.nextBlock(); if (!(nb)) {
return new Context;}; return nb.reify();};
HatBlockMorph.prototype.blockSequence = function () {if (this.selector === 'receiveCondition') {return (
this);} else {var result = HatBlockMorph.uber.blockSequence.call(this); result.shift(); return result;};};

// HatBlockMorph drawing:

Expand Down Expand Up @@ -6474,6 +6447,11 @@ function DefinitorBlockMorph() {this.init();};
DefinitorBlockMorph.prototype.init = function () {(DefinitorBlockMorph
).uber.init.call(this); this.fixLayout(); this.rerender();};

// DefinitorBlockMorph enumerating:

DefinitorBlockMorph.prototype.blockSequence = function () {if (this.isCustomBlock) {return this;
} else {var result = HatBlockMorph.uber.blockSequence.call(this); result.shift(); return result;};};

DefinitorBlockMorph.prototype.outlinePath = HatBlockMorph.prototype.outlinePath;
DefinitorBlockMorph.prototype.drawEdges = HatBlockMorph.prototype.drawEdges;

Expand Down Expand Up @@ -7976,7 +7954,7 @@ ScriptsMorph.prototype.recoverLastDrop = function (forRedrop) {

// fix zebra coloring.
// this could be generalized into the fixBlockColor mechanism
rec.lastDropTarget.element.blockSequence().forEach(cmd =>
rec.lastDropTarget.element.metaSequence().forEach(cmd =>
cmd.fixBlockColor()
);
cslot.fixLayout();
Expand Down Expand Up @@ -13692,7 +13670,8 @@ ScriptFocusMorph.prototype.fillInBlock = function (block) {
this.fixLayout();

// register generic hat blocks
if (block.selector === 'receiveCondition') {
if ((block instanceof CustomDefinitorBlockMorph
) || (block.selector === 'receiveCondition')) {
rcvr = this.editor.scriptTarget();
if (rcvr) {
stage = rcvr.parentThatIsA(StageMorph);
Expand All @@ -13708,7 +13687,7 @@ ScriptFocusMorph.prototype.fillInBlock = function (block) {
}

// experimental: if the inserted block has inputs, go to the first one
if (block.inputs && block.inputs().length) {
if (block.inputs && (block.inputs()).length) {
this.element = block;
this.atEnd = false;
this.nextElement();
Expand Down Expand Up @@ -13994,12 +13973,11 @@ ScriptFocusMorph.prototype.blockTypes = function () {
if (this.element instanceof ScriptsMorph) {
return ['hat', 'command', 'reporter', 'predicate', 'arrow', 'ring', 'definitor'];
}
if (this.element instanceof HatBlockMorph ||
this.element instanceof CommandSlotMorph) {
return ['command'];
if (this.element instanceof CommandSlotMorph) {
return ['command', 'hat', 'definitor'];
}
if (this.element instanceof DefinitorBlockMorph ||
this.element instanceof CommandSlotMorph) {
if (this.element instanceof HatBlockMorph ||
this.element instanceof DefinitorBlockMorph) {
return ['command'];
}
if (this.element instanceof CommandBlockMorph) {
Expand Down
Loading

0 comments on commit 3b43079

Please sign in to comment.