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

Js2coffee Integration (issues/126) #193

Open
wants to merge 7 commits into
base: master
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
3 changes: 3 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = function(grunt) {
'devbridge-autocomplete/dist/jquery.autocomplete.min.js',
'lib/jquery-deparam.js' : 'jquery-deparam/jquery-deparam.js',
'lib/jquery-turtle.js': 'jquery-turtle/jquery-turtle.js',
'lib/js2coffee.js': 'js2coffee/dist/js2coffee.js',
'lib/lodash.js': 'lodash/lodash.js',
'lib/p5.js': 'p5/index.js',
'lib/pencil-tracer.js': 'pencil-tracer/pencil-tracer.js',
Expand Down Expand Up @@ -105,6 +106,7 @@ module.exports = function(grunt) {
'content/lib/iced-coffee-script.js',
'content/lib/jquery.js',
'content/lib/jquery-turtle.js',
'content/lib/js2coffee.js',
'content/lib/lodash.js',
'content/lib/seedrandom.js',
'content/lib/socket.io.js',
Expand Down Expand Up @@ -154,6 +156,7 @@ module.exports = function(grunt) {
'content/lib/iced-coffee-script.js',
'content/lib/jquery.js',
'content/lib/jquery-turtle.js',
'content/lib/js2coffee.js',
'content/lib/lodash.js',
'content/lib/seedrandom.js',
'content/lib/socket.io.js',
Expand Down
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"jquery": "latest",
"jquery-deparam": "latest",
"jquery-turtle": "PencilCode/jquery-turtle#master",
"js2coffee": "js2coffee/js2coffee#master",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a tagged version instad of #master?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

js2coffee is not available as a bower package. So, I had to add the tagged version

"lodash": "latest",
"p5": "https://github.com/processing/p5.js/releases/download/0.4.6/p5.js",
"pencil-tracer": "yjerem/pencil-tracer#master",
Expand Down
7 changes: 7 additions & 0 deletions content/lib/js2coffee.js

Large diffs are not rendered by default.

24 changes: 23 additions & 1 deletion content/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var $ = require('jquery'),
pencilTracer = require('pencil-tracer'),
icedCoffeeScript = require('iced-coffee-script'),
drawProtractor = require('draw-protractor'),
cache = require('cache');
js2coffee = require('js2coffee'),
cache = require('cache');


eval(see.scope('controller'));
Expand Down Expand Up @@ -1952,6 +1953,27 @@ function createNewFileIntoPosition(position, filename, text, meta) {
}


function changeEditorLanguage(pane, lang) {
var mpp = model.pane[pane];
var text = view.getPaneEditorData(pane).data;

try{
var result;
if(lang == 'text/javascript') {
result = icedCoffeeScript.compile(text, {bare: true});
} else if(lang == 'text/coffeescript') {
result = js2coffee.build(text).code;
}
view.changePaneEditorText(pane, result);
} catch(err) {
}

}

view.on('editorLanguageChanged', function(pane, lang) {
changeEditorLanguage(pane, lang);
});

function loadFileIntoPosition(position, filename, isdir, forcenet, cb) {
var pane = paneatpos(position);
var mpp = model.pane[pane];
Expand Down
44 changes: 10 additions & 34 deletions content/src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -2208,44 +2208,15 @@ function changeEditorText(paneState, text) {
if (text.length && text.charAt(text.length - 1) != '\n') {
text += '\n';
}
paneState.changeHandler.suppressChange = true;
var saved = {}, editor = paneState.editor, session = editor.session;
saved.selection = session.selection.toJSON()
saved.atend = session.selection.getCursor().row >= session.getLength() - 1;
saved.folds = session.getAllFolds().map(function(fold) {
return {
start : fold.start,
end : fold.end,
placeholder : fold.placeholder
};
});
saved.scrollTop = session.getScrollTop()
saved.scrollLeft = session.getScrollLeft()
var editor = paneState.editor, session = editor.session;

session.changeHandler.suppressChange = true;

// Now set the text and restore everything.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was all this logic deleted as part of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method had not used anyway according to my knowledge. And also it seems like a copy of another method. So, I saw them as useless things. Anyway, I'l check it again and let you know

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidbau Do you have any idea about what these line are. And the need of them here. Appreciate if some one can help me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, so the call to set text is used via external API. The idea is that a tutorial website could set the text on a pencilcode program as part of a tutorial.

All the care with selections etc means that, if a student already has some code, setting the text will modify the code but keep the selection in the same place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidbau is it still required to keep records of selections and scrolled points even after changing the language. And would that be practical.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, when changing languages we shouldn't keep selections and things like that. There should probably be an additional argument to this function to control whether it can discard selection. Switching language would set it to "true".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure @davidbau . But, I couldn't find any where else using this method. That's why I changed this. Anyway, it's better to keep a switch.

session.setValue(text);

session.selection.fromJSON(saved.selection);
try {
saved.folds.forEach(function(fold){
session.addFold(fold.placeholder,
ace.require('ace/range').Range.fromPoints(fold.start, fold.end));
});
} catch(e) { }

session.setScrollTop(saved.scrollTop);
session.setScrollLeft(saved.scrollLeft);

// If the cursor used to be at the end, keep it at the end.
if (session.selection.isEmpty() && saved.atend) {
session.selection.moveCursorTo(session.getLength() - 1, 0);
}

// TODO: detect the case where some text is added and we should
// scroll down to make the changes visible.

paneState.changeHandler.suppressChange = false;
paneState.changeHandler();
session.changeHandler.suppressChange = false;
session.changeHandler();
}

function editorMimeType(paneState) {
Expand Down Expand Up @@ -2650,6 +2621,7 @@ function setupAceEditor(pane, elt, editor, mode, text) {
// Publish the update event for hosting frame.
if (hasSubscribers()) publish('update', [session.getValue()]);
});
editor.getSession().changeHandler = changeHandler;
$(elt).data('changeHandler', changeHandler);
editor.getSession().on('change', changeHandler);
// Fold any blocks with a line that ends with "# fold" or "// fold"
Expand Down Expand Up @@ -2736,6 +2708,10 @@ function setPaneEditorLanguageType(pane, type) {
paneState.dropletEditor.setMode(
dropletModeForMimeType(type),
dropletOptionsForMimeType(type));

//Modify the code to the changed language
fireEvent('editorLanguageChanged',[pane,type]);

paneState.editor.getSession().setMode(modeForMimeType(type));
paneState.meta = filetype.effectiveMeta(paneState.meta);
paneState.meta.type = type;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"html2canvas": "./content/lib/html2canvas.js",
"iced-coffee-script": "./content/lib/iced-coffee-script.js",
"jquery": "./content/lib/jquery.js",
"js2coffee": "./content/lib/js2coffee.js",
"palette": "./content/src/palette.js",
"pencil-tracer": "./content/lib/pencil-tracer.js",
"see": "./content/lib/see.js",
Expand Down