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

[3851] - Feat/send changes of adding block #3951

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
72097b8
fixes #3913
walterbender Jun 23, 2024
200dddb
fix:resolves grid position on hamburger opening(#3914) (#3924)
BeNikk Jun 28, 2024
71fe874
Merge branch 'master' into collaboration
apsinghdev Jul 1, 2024
423b467
Fix input boxes (#3927)
apsinghdev Jul 2, 2024
0d082eb
bump version
walterbender Jul 2, 2024
eb7c23a
enhancement: scale on hover (#3926)
Anas-2357 Jul 2, 2024
6354fa8
calculate frequencies from ratio
walterbender Jul 2, 2024
474f811
Merge branch 'master' of github.com:sugarlabs/musicblocks
walterbender Jul 2, 2024
ad292d9
Merge branch 'master' into collaboration
apsinghdev Jul 3, 2024
5848e62
Add close button to extended-menu pie menu (fixes #3933) (#3934)
BeNikk Jul 3, 2024
10ab05e
Bump braces and gulp (#3903)
dependabot[bot] Jul 3, 2024
5e27904
add support for solfege accidentals in phrasemaker
walterbender Jul 4, 2024
3c57c4b
Merge branch 'master' of github.com:sugarlabs/musicblocks
walterbender Jul 4, 2024
530f330
Merge branch 'collaboration' of https://github.com/sugarlabs/musicblo…
apsinghdev Jul 4, 2024
f73597e
Merge branch 'master' into collaboration
apsinghdev Jul 4, 2024
5022721
Merge branch 'collaboration' of https://github.com/sugarlabs/musicblo…
apsinghdev Jul 7, 2024
55570e8
Add support for cents (#3937)
walterbender Jul 7, 2024
f88955e
Ratio transpose and Ratio interval (#3936)
walterbender Jul 7, 2024
97e811a
fix note convert
walterbender Jul 7, 2024
cba02d3
remove extra comma throwing off indexing
walterbender Jul 7, 2024
306888e
Merge branch 'master' into collaboration
apsinghdev Jul 9, 2024
2d99514
Merge branch 'collaboration' of https://github.com/sugarlabs/musicblo…
apsinghdev Jul 10, 2024
944bc2e
Hyperscore link update (#3939) (#3947)
therealharshit Jul 12, 2024
aaf0f6e
Update Hyperscore example link in guide.html (#3944)
GeetenParab Jul 12, 2024
c41d9f5
Update README.md (#3943)
akilesh1706 Jul 12, 2024
69f8ee8
left_and_back_images (#3950)
khadar1020 Jul 15, 2024
472ec5c
Updated Dockerfile (#3949)
akilesh1706 Jul 15, 2024
cc0b986
Merge branch 'master' into collaboration
apsinghdev Jul 16, 2024
fd710bc
add temp collaborate button
apsinghdev Jul 16, 2024
14e8d82
Write collaboration as a class
apsinghdev Jul 16, 2024
87ae06d
bind collaboration with activity
apsinghdev Jul 16, 2024
87b9abb
Add emitter on new block creation
apsinghdev Jul 16, 2024
79f475c
connect collaborare UI with functionality
apsinghdev Jul 16, 2024
9269c32
Set local update flag
apsinghdev Jul 16, 2024
7e0ceb6
Render received data
apsinghdev Jul 16, 2024
35b8fc7
Initiate collaboration
apsinghdev Jul 16, 2024
99178ab
Make socket connection
apsinghdev Jul 16, 2024
c8873ac
Listen for emit message
apsinghdev Jul 16, 2024
2d89a50
Handle error
apsinghdev Jul 16, 2024
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
13 changes: 8 additions & 5 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@

FROM python:latest

# First stage: Build stage
FROM python:latest AS build

WORKDIR /app

COPY . .

COPY . .
# Second stage: Final stage
FROM python:latest

WORKDIR /app

EXPOSE 3000
COPY --from=build /app /app

EXPOSE 3000

CMD ["python", "-m", "http.server", "3000", "--bind", "0.0.0.0"]
2 changes: 1 addition & 1 deletion documentation/back_block.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: 1 addition & 1 deletion documentation/left_block.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: 1 addition & 1 deletion guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,7 @@ While a bit fanciful, this example, which can be run by clicking on
the link below, takes musical paint in a novel direction.

[RUN
LIVE](https://walterbender.github.io/musicblocks/index.html?id=1523896294964170&run=True&run=True)
LIVE](https://sugarlabs.github.io/musicblocks/index.html?id=1523896294964170&run=True&run=True)

## <a name="ENSEMBLE">3.8 Ensemble</a>

Expand Down
3 changes: 2 additions & 1 deletion guide/guide.html
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ <h2 id="2-making-sounds"><a name="NOTES">2. Making Sounds</a></h2>
<a href="#3-programming-with-music">Next Section (3. Programming with Music)</a>
</p>
<p>

Music Blocks incorporates many common elements of music, such as
<a href="#22-pitch-blocks">pitch</a>, <a href="#21-note-value-blocks">rhythm</a>,
<a href="#341-set-volume-and-crescendo">volume</a>, and, to some degree,
Expand Down Expand Up @@ -2406,7 +2407,7 @@ <h3 id="37-interactions"><a name="INTERACTIONS">3.7 Interactions</a></h3>
</p>
<p>
<a
href="https://walterbender.github.io/musicblocks/index.html?id=1523896294964170&amp;run=True&amp;run=True"
href="https://sugarlabs.github.io/musicblocks/index.html?id=1523896294964170&run=True&run=True"
>RUN LIVE</a
>
</p>
Expand Down
10 changes: 10 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,16 @@
>
<a style="color: transparent;">space&nbsp;&nbsp;&nbsp;</a>
</li>
<li>
<a
id="collaborateIcon"
class="tooltipped"
data-position="bottom"
><i class="material-icons md-48"
>groups</i
></a
>
</li>
<li>
<a
id="restoreIcon"
Expand Down
40 changes: 40 additions & 0 deletions js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,39 @@ class Activity {
})
}

// Initialize the collaboration
this._initiateCollaboration = () => {
this.collaboration.startCollaboration();
this.blocks.isLocalUpdate = true;
};

// Render the project from received data
this.renderProjectFromData = (update) => {
const data = JSON.parse(update);
document.body.style.cursor = "wait";
// this.doLoadAnimation();
this.blocks.isLocalUpdate = false;

// Render the blocks
for (const name in this.palettes.dict) {
this.palettes.dict[name].hideMenu(true);
}

this.stage.removeAllEventListeners("trashsignal");

// Wait for the old blocks to be removed.
// eslint-disable-next-line no-unused-vars
const __listener = (event) => {
this.blocks.loadNewBlocks(data);
this.stage.removeAllEventListeners("trashsignal");
};

this.stage.addEventListener("trashsignal", __listener, false);
this.sendAllToTrash(false, false);
this._allClear(false);
this.refreshCanvas();
};

/**
* Sets up plugin and palette boilerplate.
* This function initializes various properties related to the plugin objects,
Expand Down Expand Up @@ -3347,6 +3380,11 @@ class Activity {
this.refreshCanvas();
};

// Start the collaboration
const initiateCollaboration = (activity) => {
activity._initiateCollaboration();
}

this.handleKeyDown = (event) => {

if (event.ctrlKey && event.key === "z") {
Expand Down Expand Up @@ -5377,6 +5415,7 @@ class Activity {

this.pasteBox = new PasteBox(this);
this.languageBox = new LanguageBox(this);
this.collaboration = new Collaboration(this);

// Show help on startup if first-time user.
if (this.firstTimeUser) {
Expand Down Expand Up @@ -5425,6 +5464,7 @@ class Activity {
if (_THIS_IS_MUSIC_BLOCKS_) {
this.toolbar.renderChooseKeyIcon(chooseKeyMenu);
}
this.toolbar.renderCollaborateIcon(initiateCollaboration);
this.toolbar.renderJavaScriptIcon(toggleJSWindow);
this.toolbar.renderLanguageSelectIcon(this.languageBox);
this.toolbar.renderWrapIcon();
Expand Down
5 changes: 4 additions & 1 deletion js/artwork.js

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

23 changes: 23 additions & 0 deletions js/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ class Blocks {

this.selectedBlocks = [];

this.isLocalUpdate = false;

/**
* We stage deletion of prototype action blocks on the palette so
* as to avoid palette refresh race conditions.
Expand Down Expand Up @@ -3066,6 +3068,14 @@ class Blocks {
this.visible = true;
};

// Emit a message when a new block is created
this.emitAddedBlock = () => {
if(this.activity.collaboration.hasCollaborationStarted){
const update = this.activity.collaboration.convertBlockListToHtml();
this.activity.collaboration.socket.emit("new-block-added", update);
}
};

/**
* Make a new block with connections
* @param - name - new variable
Expand Down Expand Up @@ -3168,6 +3178,15 @@ class Blocks {

/** and we need to load the images into the container. */
myBlock.imageLoad();

/** Signal that a new block is created */
if (this.activity.collaboration.hasCollaborationStarted) {
if(this.isLocalUpdate){
setTimeout(() => {
this.emitAddedBlock();
}, 200);
};
};
return myBlock;
};

Expand Down Expand Up @@ -6525,6 +6544,10 @@ class Blocks {
}
}
}

if(this.activity.collaboration.hasCollaborationStarted){
this.isLocalUpdate = true;
}
};

/**
Expand Down
83 changes: 53 additions & 30 deletions js/collaboration/collaboration.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-vars */
// Copyright (c) 2024 Ajeet Pratap Singh
//
// This program is free software; you can redistribute it and/or
Expand All @@ -14,42 +15,64 @@

/* eslint-disable no-undef */

const RETRIES = 5;
const DELAY_DURATION = 2000;
let attempts = 0;

function stopConnection(socket) {
if(attempts >= RETRIES){
console.log("Maximum calls to make connection exceeded. Stopped making calls");
socket.disconnect();
class Collaboration {
constructor(activity) {
this.activity = activity;
this.RETRIES = 5;
this.DELAY_DURATION = 2000;
this.attempts = 0;
this.socket = null;
this.blockList = this.activity.blocks.blockList;
this.PORT = "http://localhost:8080/";
this.hasCollaborationStarted = false;
this.updatedProjectHtml = null;
}
}

function makeConnection(){
// Convert the blockList into html
convertBlockListToHtml = () => {
this.updatedProjectHtml = this.activity.prepareExport();
return this.updatedProjectHtml;
};

// connect to the local server
const socket = io("http://localhost:8080/");
socket.on("connect", () => {
try{
console.log("connected to the server");
} catch(error){
console.log("Connection failed", error);
// Stop making calls to the socket server
stopConnection = (socket) => {
if (this.attempts >= this.RETRIES) {
console.log("Maximum calls to make connection exceeded. Stopped making calls");
socket.disconnect();
}
});
};

socket.on("connect_error", (error) => {
attempts++;
console.log("Failed to connect to the socket server. Retrying in few seconds...");
setTimeout(stopConnection(socket), DELAY_DURATION);
});
}
// Make calls to the socket server
makeConnection = () => {
// connect to the local server
const socket = io(this.PORT);
socket.on("connect", () => {
this.socket = socket;
try {
console.log("connected to the server");
this.hasCollaborationStarted = true;
} catch (error) {
console.log("Connection failed", error);
}
});

function startCollaboration(){
document.addEventListener("DOMContentLoaded", () => {
makeConnection();
// initialiseDoc();
});
}
socket.on("connect_error", () => {
this.attempts++;
console.log("Failed to connect to the socket server. Retrying in few seconds...");
setTimeout(this.stopConnection(socket), this.DELAY_DURATION);
});

startCollaboration();
socket.on("new-block-added", (update) => {
this.activity.textMsg("New block added. rendering...");
setTimeout(this.activity.hideMsgs, 10000);
this.activity.renderProjectFromData(update);
});
};

// Start the collaboration
startCollaboration = () => {
this.makeConnection();
};
}

17 changes: 14 additions & 3 deletions js/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ class Toolbar {
["mb-logo", _("About Music Blocks")],
["play", _("Play")],
["stop", _("Stop")],
["record",_("Record")],
["record", _("Record")],
["Full Screen", _("Full screen")],
["FullScreen", _("Full screen")],
["Toggle Fullscreen", _("Toggle Fullscreen")],
["Toggle Fullscreen", _("Toggle Fullscreen")],
["newFile", _("New project")],
["load", _("Load project from file")],
["saveButton", _("Save project")],
Expand All @@ -71,6 +71,7 @@ class Toolbar {
["chooseKeyIcon", _("Set Pitch Preview")],
["toggleJavaScriptIcon", _("JavaScript Editor")],
["restoreIcon", _("Restore")],
["collaborateIcon", _("Collaborate")],
["beginnerMode", _("Switch to beginner mode")],
["advancedMode", _("Switch to advanced mode")],
["languageSelectIcon", _("Select language")],
Expand Down Expand Up @@ -132,6 +133,7 @@ class Toolbar {
_("Set Pitch Preview"),
_("JavaScript Editor"),
_("Restore"),
_("Collaborate"),
_("Switch to beginner mode"),
_("Switch to advanced mode"),
_("Select language"),
Expand Down Expand Up @@ -177,7 +179,7 @@ class Toolbar {
["record", _("Record")],
["Full Screen", _("Full Screen")],
["FullScreen", _("Full Screen")],
["Toggle Fullscreen", _("Toggle Fullscreen")],
["Toggle Fullscreen", _("Toggle Fullscreen")],
["newFile", _("New project")],
["load", _("Load project from file")],
["saveButton", _("Save project")],
Expand All @@ -196,6 +198,7 @@ class Toolbar {
["mergeWithCurrentIcon", _("Merge with current project")],
["toggleJavaScriptIcon", _("JavaScript Editor")],
["restoreIcon", _("Restore")],
["collaborateIcon", _("Collaborate")],
["beginnerMode", _("Switch to beginner mode")],
["advancedMode", _("Switch to advanced mode")],
["languageSelectIcon", _("Select language")],
Expand Down Expand Up @@ -252,6 +255,7 @@ class Toolbar {
_("Merge with current project"),
_("JavaScript Editor"),
_("Restore"),
_("Collaboration"),
_("Switch to beginner mode"),
_("Switch to advanced mode"),
_("Select language"),
Expand Down Expand Up @@ -892,6 +896,13 @@ class Toolbar {
};
}

renderCollaborateIcon(onclick) {
const collaborateIcon = docById("collaborateIcon");
collaborateIcon.onclick = () => {
onclick(this.activity);
}
}

/**
* Renders the choose key icon with the provided onclick handler.
*
Expand Down
12 changes: 11 additions & 1 deletion js/turtledefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
_, ADVANCEDBUTTON, BIGGERBUTTON, CARTESIANBUTTON, CLEARBUTTON,
COLLAPSEBLOCKSBUTTON, COLLAPSEBUTTON, GOHOMEBUTTON, HELPBUTTON,
HIDEBLOCKSBUTTON, LANGUAGEBUTTON, LOADBUTTON, MENUBUTTON,
NEWBUTTON, PLANETBUTTON, PLUGINSDELETEBUTTON, RESTORETRASHBUTTON,
NEWBUTTON, PLANETBUTTON, PLUGINSDELETEBUTTON, RESTORETRASHBUTTON,COLLABORATEBUTTON
RHYTHMPALETTEICON, RUNBUTTON, SAVEBUTTON, SCROLLUNLOCKBUTTON,
SHORTCUTSBUTTON, SLOWBUTTON, SMALLERBUTTON, STATSBUTTON,
STEPBUTTON, STOPTURTLEBUTTON, WRAPTURTLEBUTTON, _THIS_IS_TURTLE_BLOCKS_,
Expand Down Expand Up @@ -728,6 +728,16 @@ const createHelpContent = (activity) => {
base64Encode(RESTORETRASHBUTTON))

]);

HELPCONTENT.push([
_("Collaborate"),
_("Start collaboration on a project"),
"data:image/svg+xml;base64," +
window.btoa(
base64Encode(COLLABORATEBUTTON))

]);

HELPCONTENT.push([
_("Switch mode"),
_("Switch between beginner and advance modes."),
Expand Down
Loading