Skip to content

Commit

Permalink
Add Back-end support for allowJavaScript toggle (#584)
Browse files Browse the repository at this point in the history
Related: mozilla/thimble.mozilla.org#1631
  • Loading branch information
peiying16 authored and gideonthomas committed Feb 11, 2017
1 parent 82172d6 commit b524178
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/bramble/client/StateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ define(function() {
wordWrap: {
get: function() { return getBool(storage, "wordWrap"); },
set: function(v) { storage.setItem(prefix("wordWrap"), v); }
},
allowJavaScript: {
get: function() { return getBool(storage, "allowJavaScript"); },
set: function(v) { storage.setItem(prefix("allowJavaScript"), v); }
}
});
}
Expand Down
7 changes: 6 additions & 1 deletion src/bramble/client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ define([
self.getSidebarVisible = function() { return _state.sidebarVisible; };
self.getRootDir = function() { return _root; };
self.getWordWrap = function() { return _state.wordWrap; };
self.getAllowJavaScript = function() { return _state.allowJavaScript; };
self.getTutorialExists = function() { return _tutorialExists; };
self.getTutorialVisible = function() { return _tutorialVisible; };
self.getLayout = function() {
Expand Down Expand Up @@ -262,6 +263,7 @@ define([
_state.previewMode = data.previewMode;
_state.theme = data.theme;
_state.wordWrap = data.wordWrap;
_state.allowJavaScript = data.allowJavaScript;

setReadyState(Bramble.READY);
}
Expand Down Expand Up @@ -297,6 +299,8 @@ define([
_state.sidebarVisible = data.visible;
} else if (eventName === "wordWrapChange") {
_state.wordWrap = data.wordWrap;
} else if (eventName === "allowJavaScriptChange") {
_state.allowJavaScript = data.allowJavaScript;
} else if (eventName === "tutorialVisibilityChange") {
_tutorialVisible = data.visible;
}
Expand Down Expand Up @@ -412,7 +416,8 @@ define([
firstPaneWidth: _state.firstPaneWidth,
secondPaneWidth: _state.secondPaneWidth,
previewMode: _state.previewMode,
wordWrap: _state.wordWrap
wordWrap: _state.wordWrap,
allowJavaScript: _state.allowJavaScript
}
};
_brambleWindow.postMessage(JSON.stringify(initMessage), _iframe.src);
Expand Down
19 changes: 16 additions & 3 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ define(function (require, exports, module) {
TAB_SIZE = "tabSize",
UPPERCASE_COLORS = "uppercaseColors",
USE_TAB_CHAR = "useTabChar",
WORD_WRAP = "wordWrap";
WORD_WRAP = "wordWrap",
ALLOW_JAVASCRIPT = "allowJavaScript";

var cmOptions = {};

Expand Down Expand Up @@ -125,7 +126,8 @@ define(function (require, exports, module) {
cmOptions[TAB_SIZE] = "tabSize";
cmOptions[USE_TAB_CHAR] = "indentWithTabs";
cmOptions[WORD_WRAP] = "lineWrapping";

cmOptions[ALLOW_JAVASCRIPT] = "allowJavaScript";

PreferencesManager.definePreference(CLOSE_BRACKETS, "boolean", false);
PreferencesManager.definePreference(CLOSE_TAGS, "Object", { whenOpening: true, whenClosing: true, indentTags: [] });
PreferencesManager.definePreference(DRAG_DROP, "boolean", false);
Expand All @@ -145,7 +147,8 @@ define(function (require, exports, module) {
PreferencesManager.definePreference(UPPERCASE_COLORS, "boolean", false);
PreferencesManager.definePreference(USE_TAB_CHAR, "boolean", false);
PreferencesManager.definePreference(WORD_WRAP, "boolean", true);

PreferencesManager.definePreference(ALLOW_JAVASCRIPT, "boolean", true);

var editorOptions = Object.keys(cmOptions);

/** Editor preferences */
Expand Down Expand Up @@ -321,6 +324,7 @@ define(function (require, exports, module) {
inputStyle : "textarea", // the "contenteditable" mode used on mobiles could cause issues
lineNumbers : currentOptions[SHOW_LINE_NUMBERS],
lineWrapping : currentOptions[WORD_WRAP],
allowJavaScript : currentOptions[ALLOW_JAVASCRIPT],
matchBrackets : { maxScanLineLength: 50000, maxScanLines: 1000 },
matchTags : { bothTags: true },
scrollPastEnd : !range && currentOptions[SCROLL_PAST_END],
Expand Down Expand Up @@ -2406,6 +2410,15 @@ define(function (require, exports, module) {
return PreferencesManager.get(WORD_WRAP, _buildPreferencesContext(fullPath));
};

Editor.setAllowJavaScript = function (value, fullPath) {
var options = fullPath && {context: fullPath};
return PreferencesManager.set(ALLOW_JAVASCRIPT, value, options);
};

Editor.getAllowJavaScript = function (fullPath) {
return PreferencesManager.get(ALLOW_JAVASCRIPT, _buildPreferencesContext(fullPath));
};

/**
* Runs callback for every Editor instance that currently exists
* @param {!function(!Editor)} callback
Expand Down
3 changes: 3 additions & 0 deletions src/editor/EditorOptionHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ define(function (require, exports, module) {
var SHOW_LINE_NUMBERS = "showLineNumbers",
STYLE_ACTIVE_LINE = "styleActiveLine",
WORD_WRAP = "wordWrap",
ALLOW_JAVASCRIPT = "allowJavaScript",
CLOSE_BRACKETS = "closeBrackets";

/**
Expand All @@ -50,6 +51,7 @@ define(function (require, exports, module) {
_optionMapping[SHOW_LINE_NUMBERS] = Commands.TOGGLE_LINE_NUMBERS;
_optionMapping[STYLE_ACTIVE_LINE] = Commands.TOGGLE_ACTIVE_LINE;
_optionMapping[WORD_WRAP] = Commands.TOGGLE_WORD_WRAP;
_optionMapping[ALLOW_JAVASCRIPT] = Commands.TOGGLE_ALLOW_JAVASCRIPT;
_optionMapping[CLOSE_BRACKETS] = Commands.TOGGLE_CLOSE_BRACKETS;

/**
Expand Down Expand Up @@ -99,6 +101,7 @@ define(function (require, exports, module) {
CommandManager.register(Strings.CMD_TOGGLE_LINE_NUMBERS, Commands.TOGGLE_LINE_NUMBERS, _getToggler(SHOW_LINE_NUMBERS));
CommandManager.register(Strings.CMD_TOGGLE_ACTIVE_LINE, Commands.TOGGLE_ACTIVE_LINE, _getToggler(STYLE_ACTIVE_LINE));
CommandManager.register(Strings.CMD_TOGGLE_WORD_WRAP, Commands.TOGGLE_WORD_WRAP, _getToggler(WORD_WRAP));
CommandManager.register(Strings.CMD_TOGGLE_ALLOW_JAVASCRIPT, Commands.TOGGLE_ALLOW_JAVASCRIPT, _getToggler(ALLOW_JAVASCRIPT));
CommandManager.register(Strings.CMD_TOGGLE_CLOSE_BRACKETS, Commands.TOGGLE_CLOSE_BRACKETS, _getToggler(CLOSE_BRACKETS));

AppInit.htmlReady(_init);
Expand Down
2 changes: 2 additions & 0 deletions src/extensions/default/bramble/lib/RemoteCommandHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ define(function (require, exports, module) {
break;
case "BRAMBLE_ENABLE_SCRIPTS":
HTMLRewriter.enableScripts();
PreferencesManager.set("allowJavaScript", true);
PostMessageTransport.reload();
break;
case "BRAMBLE_DISABLE_SCRIPTS":
HTMLRewriter.disableScripts();
PreferencesManager.set("allowJavaScript", false);
PostMessageTransport.reload();
break;
case "BRAMBLE_ENABLE_INSPECTOR":
Expand Down
11 changes: 10 additions & 1 deletion src/extensions/default/bramble/lib/RemoteEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ define(function (require, exports, module) {
wordWrap: PreferencesManager.get("wordWrap")
});
});

// Listen for changes to allow javascript
PreferencesManager.on("change", "allowJavaScript", function () {
sendEvent({
type: "bramble:allowJavaScriptChange",
allowJavaScript: PreferencesManager.get("allowJavaScript")
});
});
}

/**
Expand Down Expand Up @@ -156,7 +164,8 @@ define(function (require, exports, module) {
previewMode: UI.getPreviewMode(),
fontSize: ViewCommandHandlers.getFontSize(),
theme: Theme.getTheme(),
wordWrap: PreferencesManager.get("wordWrap")
wordWrap: PreferencesManager.get("wordWrap"),
allowJavaScript: PreferencesManager.get("allowJavaScript")
});
}

Expand Down
5 changes: 5 additions & 0 deletions src/extensions/default/bramble/lib/UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ define(function (require, exports, module) {
PreferencesManager.set("wordWrap", wordWrap);
}

var allowJavaScript = BrambleStartupState.ui("allowJavaScript");
if(typeof allowJavaScript === "boolean") {
PreferencesManager.set("allowJavaScript", allowJavaScript);
}

var sidebarWidth = BrambleStartupState.ui("sidebarWidth");
if(sidebarWidth) {
SidebarView.resize(sidebarWidth);
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/default/bramble/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ define(function (require, exports, module) {
firstPaneWidth: data.state.firstPaneWidth,
secondPaneWidth: data.state.secondPaneWidth,
previewMode: data.state.previewMode,
wordWrap: data.state.wordWrap
wordWrap: data.state.wordWrap,
allowJavaScript: data.state.allowJavaScript
});

RemoteEvents.start();
Expand Down

0 comments on commit b524178

Please sign in to comment.