-
Notifications
You must be signed in to change notification settings - Fork 62
Add support for setting a custom theme + add GitHub theme option. #98
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,10 +18,13 @@ | |
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| * DEALINGS IN THE SOFTWARE. | ||
| * | ||
| * Contributions: | ||
| * Leandro Silva | Grafluxe, 2016 | ||
| */ | ||
|
|
||
| /*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */ | ||
| /*global define, brackets, $, window, _hideSettings */ | ||
| /*global define, brackets, $, _hideSettings, _confirmFile, setCustomTheme */ | ||
|
|
||
| define(function (require, exports, module) { | ||
| "use strict"; | ||
|
|
@@ -39,7 +42,9 @@ define(function (require, exports, module) { | |
| WorkspaceManager = brackets.getModule("view/WorkspaceManager"), | ||
| CommandManager = brackets.getModule("command/CommandManager"), | ||
| Menus = brackets.getModule("command/Menus"), | ||
| _ = brackets.getModule("thirdparty/lodash"); | ||
| _ = brackets.getModule("thirdparty/lodash"), | ||
| Dialogs = brackets.getModule("widgets/Dialogs"), | ||
| FileSystem = brackets.getModule("filesystem/FileSystem"); | ||
|
|
||
| // Templates | ||
| var panelHTML = require("text!templates/panel.html"), | ||
|
|
@@ -63,13 +68,15 @@ define(function (require, exports, module) { | |
| viewMenu, | ||
| toggleCmd, | ||
| visible = false, | ||
| realVisibility = false; | ||
| realVisibility = false, | ||
| docLoading = false; | ||
|
|
||
| // Prefs | ||
| var _prefs = PreferencesManager.getExtensionPrefs("markdown-preview"); | ||
| _prefs.definePreference("useGFM", "boolean", false); | ||
| _prefs.definePreference("theme", "string", "clean"); | ||
| _prefs.definePreference("syncScroll", "boolean", true); | ||
| _prefs.definePreference("customTheme", "string", ""); | ||
|
|
||
| // (based on code in brackets.js) | ||
| function _handleLinkClick(e) { | ||
|
|
@@ -92,9 +99,13 @@ define(function (require, exports, module) { | |
| } | ||
|
|
||
| function _calcScrollPos() { | ||
| var scrollInfo = currentEditor._codeMirror.getScrollInfo(); | ||
| var scrollPercentage = scrollInfo.top / (scrollInfo.height - scrollInfo.clientHeight); | ||
| var scrollTop = ($iframe[0].contentDocument.body.scrollHeight - $iframe[0].clientHeight) * scrollPercentage; | ||
| var scrollInfo = currentEditor._codeMirror.getScrollInfo(), | ||
| scrollPercentage = scrollInfo.top / (scrollInfo.height - scrollInfo.clientHeight), | ||
| scrollTop; | ||
|
|
||
| if ($iframe[0].contentDocument.body) { | ||
| scrollTop = ($iframe[0].contentDocument.body.scrollHeight - $iframe[0].clientHeight) * scrollPercentage; | ||
| } | ||
|
|
||
| return Math.round(scrollTop); | ||
| } | ||
|
|
@@ -139,12 +150,27 @@ define(function (require, exports, module) { | |
| $iframe[0].contentDocument.body.innerHTML = bodyText; | ||
| } else { | ||
| // Make <base> tag for relative URLS | ||
| var baseUrl = window.location.protocol + "//" + FileUtils.getDirectoryPath(doc.file.fullPath); | ||
| var baseUrl = window.location.protocol + "//" + FileUtils.getDirectoryPath(doc.file.fullPath), | ||
| themeURI; | ||
|
|
||
| if (docLoading) { | ||
| return; | ||
| } | ||
|
|
||
| docLoading = true; | ||
|
|
||
| if (_prefs.get("theme") === "custom") { | ||
| themeURI = require.toUrl(_prefs.get("customTheme")); | ||
|
|
||
| _confirmFile(themeURI); | ||
| } else { | ||
| themeURI = require.toUrl("./themes/" + _prefs.get("theme") + ".css"); | ||
| } | ||
|
|
||
| // Assemble the HTML source | ||
| var htmlSource = _.template(previewHTML)({ | ||
| baseUrl : baseUrl, | ||
| themeUrl : require.toUrl("./themes/" + _prefs.get("theme") + ".css"), | ||
| themeUrl : themeURI, | ||
| scrollTop : scrollPos, | ||
| bodyText : bodyText | ||
| }); | ||
|
|
@@ -164,6 +190,7 @@ define(function (require, exports, module) { | |
|
|
||
| // Make sure iframe is showing | ||
| $iframe.show(); | ||
| docLoading = false; | ||
| }); | ||
| } | ||
| } | ||
|
|
@@ -208,7 +235,7 @@ define(function (require, exports, module) { | |
| } | ||
| } | ||
|
|
||
| function _showSettings(e) { | ||
| function _showSettings() { | ||
| _hideSettings(); | ||
|
|
||
| $settings = $(settingsHTML) | ||
|
|
@@ -228,8 +255,12 @@ define(function (require, exports, module) { | |
| $settings.find("#markdown-preview-theme") | ||
| .val(_prefs.get("theme")) | ||
| .change(function (e) { | ||
| _prefs.set("theme", e.target.value); | ||
| _updateSettings(); | ||
| if (e.target.value === "custom" && !_prefs.get("customTheme")) { | ||
| setCustomTheme(); | ||
| } else { | ||
| _prefs.set("theme", e.target.value); | ||
| _updateSettings(); | ||
| } | ||
| }); | ||
|
|
||
| var $syncScroll = $settings.find("#markdown-preview-sync-scroll"); | ||
|
|
@@ -324,6 +355,103 @@ define(function (require, exports, module) { | |
| toggleCmd.setChecked(visible); | ||
| } | ||
|
|
||
| // Support custom theme | ||
| function _onSelectCSS(err, fi) { | ||
| if (err) { | ||
| console.error(err); | ||
| Dialogs.showModalDialog( | ||
| "", | ||
| "Markdown Preview", | ||
| "There was an error with your selection." | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (fi.length === 1) { | ||
| _prefs.set("theme", "custom"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once a custom theme is set, there is no way to change it to a different theme (short of renaming the file to force a reset). There should be a way to switch to a different CSS file. Here is one possible suggestion: keep the Custom menu item, but add a new menu item whenever a custom CSS file is selected. This way the theme menu is populated with all of the built-in themes, and all themes selected by the user. If you do this, the Custom menu item should have an ellipses at the end. |
||
| _prefs.set("customTheme", fi[0]); | ||
| _updateSettings(); | ||
| } else { | ||
| _showSettings(); | ||
| } | ||
| } | ||
|
|
||
| function _onSelectModule(id) { | ||
| if (id === "cancel") { | ||
| _showSettings(); | ||
| return; | ||
| } | ||
|
|
||
| FileSystem.showOpenDialog( | ||
| false, | ||
| false, | ||
| "Select a CSS file", | ||
| brackets.app.getUserDocumentsDirectory(), | ||
| ["css"], | ||
| _onSelectCSS | ||
| ); | ||
| } | ||
|
|
||
| function setCustomTheme() { | ||
| _hideSettings(); | ||
|
|
||
| Dialogs.showModalDialog( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This dialog is not necessary. Selecting the 'Custom' theme should go directly to the file system open dialog. |
||
| "", | ||
| "Markdown Preview", | ||
| "Select a CSS file to style your theme with.", | ||
| [ | ||
| { | ||
| id: "select", | ||
| text: "Select A File" | ||
| }, | ||
| { | ||
| id: "cancel", | ||
| text: "Cancel" | ||
| } | ||
| ] | ||
| ).done(_onSelectModule); | ||
| } | ||
|
|
||
| function _resetCustomTheme() { | ||
| _prefs.set("customTheme", ""); | ||
| _prefs.set("theme", "clean"); | ||
|
|
||
| docLoading = false; | ||
| window.requestAnimationFrame(_updateSettings); | ||
| } | ||
|
|
||
| function _confirmFile(fi) { | ||
| var resetMsg = "<br><br>Please reset your custom theme path by reselecting \"custom\" from the theme dropdown."; | ||
|
|
||
| _hideSettings(); | ||
|
|
||
| if (fi.slice(-4) !== ".css") { | ||
| Dialogs.showModalDialog( | ||
| "", | ||
| "Markdown Preview", | ||
| "Your file must be of type CSS. " + resetMsg | ||
| ); | ||
|
|
||
| _resetCustomTheme(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| FileSystem.resolve(fi, function (err) { | ||
| if (err) { | ||
| console.error(err); | ||
|
|
||
| Dialogs.showModalDialog( | ||
| "", | ||
| "Markdown Preview", | ||
| "There was an error loading your file. " + resetMsg | ||
| ); | ||
|
|
||
| _resetCustomTheme(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Debounce event callback to avoid excess overhead | ||
| // Update preview 300 ms ofter document change | ||
| // Sync scroll 1ms after document scroll (just enough to ensure | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,5 +8,6 @@ | |
| "license": "MIT", | ||
| "engines": { | ||
| "brackets": ">=1.1" | ||
| } | ||
| }, | ||
| "contributors": ["Leandro Silva (http://grafluxe.com)"] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
|
|
||
| #panel-markdown-preview-frame { | ||
| border: none; | ||
| background-color: #fff; | ||
| } | ||
|
|
||
| #markdown-settings-toggle { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should be removed.