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

Added undo and redo in gear activity #1609

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions activities/Gears.activity/css/activity.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ body {
background-image: url(../icons/edit-clear.svg);
}

#main-toolbar #undo-button {
background-image: url(../icons/edit-undo.svg);
}

#main-toolbar #redo-button {
background-image: url(../icons/edit-redo.svg);
}

#main-toolbar #help-button {
background-image: url(../icons/help.svg);
}
Expand Down
16 changes: 16 additions & 0 deletions activities/Gears.activity/icons/edit-redo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions activities/Gears.activity/icons/edit-undo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions activities/Gears.activity/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<button class="toolbutton" id="chain-button" title="Chain"></button>
<button class="toolbutton" id="momentum-button" title="Momentum"></button>
<button class="toolbutton play" id="play-button" title="Play"></button>
<button class="toolbutton" id="undo-button" data-i18n="undo" title="Undo" style="opacity: 0.4;"></button>
<button class="toolbutton" id="redo-button" data-i18n="redo" title="Redo" style="opacity: 0.4;"></button>
<hr/>
<button class="toolbutton" id="clear-button" title="Clear"></button>
<button class="toolbutton pull-right" id="stop-button" title="Stop"></button>
Expand Down
95 changes: 93 additions & 2 deletions activities/Gears.activity/js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ define(["sugar-web/activity/activity","sugar-web/graphics/radiobuttonsgroup","ge

// Initialize the activity.
activity.setup();

var stopButton = document.getElementById("stop-button");
stopButton.addEventListener('click', function (event) {
console.log("writing...");
Expand Down Expand Up @@ -39,6 +39,97 @@ define(["sugar-web/activity/activity","sugar-web/graphics/radiobuttonsgroup","ge
}
datastoreObject.loadAsText(onLoaded);

// Create a redo array associated with history object of global scope
window.history = {};
window.$h = window.history;
history.undo = [];
history.redo = [];

document.getElementById('undo-button').addEventListener('click', function(){
if ($h.undo.length > 0 && gearSketch.selectedButton !== "playButton") {
var obj = $h.undo.pop();
if(obj.method === "delete" || obj.method === "add"){
$h.redo.push(obj);
gearSketch.board.clear();
// console.log($h.undo);
$h.undo.map((value, ind) => {
if(value.method === "add"){
gearSketch.board[value.type][value.item.id] = value.item;
} else if( value.method === "delete"){
// Search for the item with the id and delete it
for (var type in gearSketch.board) {
if (gearSketch.board.hasOwnProperty(type)) {
var items = gearSketch.board[type];
for (var itemId in items) {
if (items.hasOwnProperty(itemId) && itemId === value.item.id) {
delete gearSketch.board[type][itemId];
}
}
}
}
}
})
} else if(obj.method === "locationchange") {
for (var type in gearSketch.board) {
if (gearSketch.board.hasOwnProperty(type)) {
var items = gearSketch.board[type];
for (var itemId in items) {
if (items.hasOwnProperty(itemId) && itemId === obj.item) {
var earlierLocation = gearSketch.board[type][itemId].location;
gearSketch.board[type][itemId].location = obj.location;
obj.location = earlierLocation;
$h.redo.push(obj);
}
}
}
}
}
document.getElementById("redo-button").style.opacity = 1;
}
if($h.undo.length === 0) document.getElementById("undo-button").style.opacity = 0.4;
else document.getElementById("undo-button").style.opacity = 1;
})

document.getElementById('redo-button').addEventListener('click', function(){
if ($h.redo.length > 0 && gearSketch.selectedButton !== "playButton") {
var obj = $h.redo.pop();
if(obj.method === "delete"){
$h.undo.push(obj);
// Search for the item with the id and delete it
for (var type in gearSketch.board) {
if (gearSketch.board.hasOwnProperty(type)) {
var items = gearSketch.board[type];
for (var itemId in items) {
if (items.hasOwnProperty(itemId) && itemId === obj.item.id) {
delete gearSketch.board[type][itemId];
}
}
}
}
} else if(obj.method === "add"){
$h.undo.push(obj);
gearSketch.board[obj.type][obj.item.id] = obj.item;
} else if(obj.method === "locationchange") {
for (var type in gearSketch.board) {
if (gearSketch.board.hasOwnProperty(type)) {
var items = gearSketch.board[type];
for (var itemId in items) {
if (items.hasOwnProperty(itemId) && itemId === obj.item) {
var earlierLocation = gearSketch.board[type][itemId].location;
gearSketch.board[type][itemId].location = obj.location;
obj.location = earlierLocation;
$h.undo.push(obj);
}
}
}
}
}
document.getElementById("undo-button").style.opacity = 1;
}
if($h.redo.length === 0) document.getElementById("redo-button").style.opacity = 0.4;
else document.getElementById("redo-button").style.opacity = 1;
})

var radioButtons;
var gearButton = document.getElementById("gear-button");
var chainButton = document.getElementById("chain-button");
Expand Down Expand Up @@ -125,7 +216,7 @@ define(["sugar-web/activity/activity","sugar-web/graphics/radiobuttonsgroup","ge
}
gearSketch.playDemo();
});

// Full screen.
document.getElementById("fullscreen-button").addEventListener('click', function() {
document.getElementById("main-toolbar").style.opacity = 0;
Expand Down
36 changes: 33 additions & 3 deletions activities/Gears.activity/lib/gearsketch/gearsketch_main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.