Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy / paste scripts #3286

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 98 additions & 2 deletions src/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3210,6 +3210,7 @@ BlockMorph.prototype.localizeBlockSpec = function (spec) {
BlockMorph.prototype.userMenu = function () {
var menu = new MenuMorph(this),
world = this.world(),
ide = this.parentThatIsA(IDE_Morph),
myself = this,
hasLine = false,
proc = this.activeProcess(),
Expand Down Expand Up @@ -3502,7 +3503,6 @@ BlockMorph.prototype.userMenu = function () {
"duplicate",
() => {
var dup = this.fullCopy(),
ide = this.parentThatIsA(IDE_Morph),
blockEditor = this.parentThatIsA(BlockEditorMorph);
dup.pickUp(world);
// register the drop-origin, so the block can
Expand Down Expand Up @@ -3589,6 +3589,61 @@ BlockMorph.prototype.userMenu = function () {
);
}
menu.addLine();
if ((this instanceof CommandBlockMorph)) {
menu.addItem(
'copy below',
() => {
ide.clipboard = {
type: 'xml',
content: this.toXMLString()
};
},
'copy block and below'
);
}
menu.addItem(
'copy block',
() => {
let block = this.fullCopy();
if (block instanceof CommandBlockMorph || block instanceof HatBlockMorph) {
var nb = block.nextBlock();
if (nb) {
nb.destroy();
}
}

block.parent = ide;
ide.clipboard = {
type: 'xml',
content: block.toXMLString()
};
block.destroy();
},
'copy this block'
);
menu.addItem(
'cut block',
() => {
let block = this.fullCopy();
if (this instanceof CommandBlockMorph || this instanceof HatBlockMorph) {
var nb = block.nextBlock();
if (nb) {
nb.destroy();
}
}

block.parent = ide;
ide.clipboard = {
type: 'xml',
content: block.toXMLString()
};
block.destroy();

this.userDestroy();
},
'copy this block and delete it'
);
menu.addLine();
menu.addItem(
"script pic...",
() => {
Expand Down Expand Up @@ -8658,6 +8713,16 @@ ScriptsMorph.prototype.userMenu = function () {
);
}
}
if (ide.clipboard) {
menu.addLine();
menu.addItem(
"paste",
() => {
ide.userPaste();
},
'Retrieve script\nfrom clipboard'
);
}
return menu;
};

Expand Down Expand Up @@ -15573,7 +15638,13 @@ CommentMorph.prototype.fixLayout = function () {
// CommentMorph menu:

CommentMorph.prototype.userMenu = function () {
var menu = new MenuMorph(this);
var menu = new MenuMorph(this),
ide = this.parentThatIsA(IDE_Morph),
blockEditor = this.parentThatIsA(BlockEditorMorph);

if (!ide && blockEditor) {
ide = blockEditor.target.parentThatIsA(IDE_Morph);
}

menu.addItem(
"duplicate",
Expand Down Expand Up @@ -15621,6 +15692,31 @@ CommentMorph.prototype.userMenu = function () {
},
'save a picture\nof this comment'
);
menu.addLine();
menu.addItem(
'copy comment',
() => {
ide.clipboard = {
type: 'comment',
content: this.text(),
width: this.textWidth(),
};
},
'copy this comment'
);
menu.addItem(
'cut comment',
() => {
ide.clipboard = {
type: 'comment',
content: this.text(),
width: this.textWidth(),
};

this.userDestroy()
},
'copy this comment and delete it'
);
return menu;
};

Expand Down
105 changes: 105 additions & 0 deletions src/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -3961,6 +3961,111 @@ IDE_Morph.prototype.removeBlock = function (aBlock, justThis) {
aBlock.destroy(justThis);
};

// IDE_Morph copy / paste at hand

IDE_Morph.prototype.copyUnderHand = function (event) {
var world = this.world();
var underHand,
hand,
mouseOverList;

hand = world.hand;
mouseOverList = hand.mouseOverList;

underHand = mouseOverList[0].parentThatIsA(BlockMorph) ||
mouseOverList[0].parentThatIsA(CommentMorph);

if (underHand && !underHand.isTemplate && !(underHand instanceof PrototypeHatBlockMorph)) {
let content = underHand.fullCopy();

if ((event === 'ctrl shift c') && (content instanceof CommandBlockMorph || content instanceof HatBlockMorph)) {
var nb = content.nextBlock();
if (nb) {
nb.destroy();
}
}

this.userCopy(content);

}
}

IDE_Morph.prototype.cutUnderHand = function () {
var world = this.world();
var underHand,
hand,
mouseOverList;

hand = world.hand;
mouseOverList = hand.mouseOverList;

underHand = mouseOverList[0].parentThatIsA(BlockMorph) ||
mouseOverList[0].parentThatIsA(CommentMorph);

if (underHand && !underHand.isTemplate && !(underHand instanceof PrototypeHatBlockMorph)) {
let content = underHand.fullCopy();

if (content instanceof CommandBlockMorph || content instanceof HatBlockMorph) {
var nb = content.nextBlock();
if (nb) {
nb.destroy();
}
}

this.userCopy(content);

underHand.userDestroy();
}
}

IDE_Morph.prototype.userCopy = function (content) {
if (content instanceof CommentMorph) {
this.clipboard = {
type: 'comment',
content: content.text(),
width: content.textWidth(),
}
return
}

if (content instanceof BlockMorph) {
content = content.fullCopy();

content.parent = this;
this.clipboard = {
type: 'xml',
content: content.toXMLString()
};
content.destroy();

return
}

}

IDE_Morph.prototype.userPaste = function (event) {
var world = this.world();

if (this.clipboard) {
switch (this.clipboard.type) {
case 'xml':
this.droppedText(this.clipboard.content);
break;
case 'comment':
let comment = new CommentMorph(this.clipboard.content);
if (this.clipboard.width) comment.setTextWidth(this.clipboard.width);
comment.pickUp(world);
world.hand.grabOrigin = {
origin: this.palette,
position: this.palette.center()
};
break;
default:
break;
}
}
}

// IDE_Morph menus

IDE_Morph.prototype.userMenu = function () {
Expand Down
19 changes: 17 additions & 2 deletions src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9347,10 +9347,10 @@ StageMorph.prototype.processKeyEvent = function (event, action) {
(event.shiftKey ? 'shift ' : '') + keyName;
}
}
action.call(this, keyName);
action.call(this, keyName, event);
};

StageMorph.prototype.fireKeyEvent = function (key) {
StageMorph.prototype.fireKeyEvent = function (key, event) {
var evt = key.toLowerCase(),
procs = [],
ide = this.parentThatIsA(IDE_Morph);
Expand All @@ -9374,6 +9374,21 @@ StageMorph.prototype.fireKeyEvent = function (key) {
if (!ide.isAppMode) {ide.currentSprite.scripts.redrop(); }
return;
}
if (evt === 'ctrl shift c' || (evt === 'ctrl c')) {
ide.copyUnderHand(evt);
if (event != null) {
event.preventDefault();
}
return;
}
if (evt === 'ctrl x') {
ide.cutUnderHand();
return;
}
if (evt === 'ctrl v' || (evt === 'ctrl shift v')) {
ide.userPaste();
return;
}
if (evt === 'ctrl n') {
if (!ide.isAppMode) {ide.createNewProject(); }
return;
Expand Down