Skip to content

Commit

Permalink
Removing unused modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
albertinad committed Jul 20, 2015
1 parent 93b95c2 commit 3af22ac
Showing 1 changed file with 48 additions and 50 deletions.
98 changes: 48 additions & 50 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*
* Copyright (c) 2012 Glenn Ruehle
*
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -21,7 +21,7 @@
*/

/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, brackets, $, window, marked, _hideSettings */
/*global define, brackets, $, window, _hideSettings */

define(function (require, exports, module) {
"use strict";
Expand All @@ -36,26 +36,24 @@ define(function (require, exports, module) {
MainViewManager = brackets.getModule("view/MainViewManager"),
PopUpManager = brackets.getModule("widgets/PopUpManager"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
Resizer = brackets.getModule("utils/Resizer"),
StringUtils = brackets.getModule("utils/StringUtils"),
WorkspaceManager = brackets.getModule("view/WorkspaceManager"),
_ = brackets.getModule("thirdparty/lodash");

// Templates
var panelHTML = require("text!templates/panel.html"),
previewHTML = require("text!templates/preview.html"),
settingsHTML = require("text!templates/settings.html");

// Local modules
var marked = require("lib/marked");

// jQuery objects
var $icon,
$iframe,
$panel,
$settingsToggle,
$settings;

// Other vars
var currentDoc,
currentEditor,
Expand All @@ -68,7 +66,7 @@ define(function (require, exports, module) {
_prefs.definePreference("useGFM", "boolean", false);
_prefs.definePreference("theme", "string", "clean");
_prefs.definePreference("syncScroll", "boolean", true);

// (based on code in brackets.js)
function _handleLinkClick(e) {
// Check parents too, in case link has inline formatting tags
Expand All @@ -84,27 +82,27 @@ define(function (require, exports, module) {
}
node = node.parentElement;
}

// Close settings dropdown, if open
_hideSettings();
}

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;

return Math.round(scrollTop);
}

function _editorScroll() {
if (_prefs.get("syncScroll") && $iframe) {
var scrollTop = _calcScrollPos();

$iframe[0].contentDocument.body.scrollTop = scrollTop;
}
}

function _loadDoc(doc, isReload) {
if (doc && visible && $iframe) {
var docText = doc.getText(),
Expand All @@ -117,22 +115,22 @@ define(function (require, exports, module) {
if (yamlMatch) {
docText = docText.substr(yamlMatch[0].length);
}

if (isReload) {
scrollPos = $iframe.contents()[0].body.scrollTop;
} else if (_prefs.get("syncScroll")) {
scrollPos = _calcScrollPos();
}

// Parse markdown into HTML
bodyText = marked(docText);

// Show URL in link tooltip
bodyText = bodyText.replace(/(href=\"([^\"]*)\")/g, "$1 title=\"$2\"");

// Convert protocol-relative URLS
bodyText = bodyText.replace(/src="\/\//g, "src=\"http://");

if (isReload) {
$iframe[0].contentDocument.body.innerHTML = bodyText;
} else {
Expand Down Expand Up @@ -166,104 +164,104 @@ define(function (require, exports, module) {
}
}
}

function _documentChange(e) {
_loadDoc(e.target, true);
}

function _resizeIframe() {
if (visible && $iframe) {
var iframeWidth = panel.$panel.innerWidth();
$iframe.attr("width", iframeWidth + "px");
}
}

function _updateSettings() {
// Format
var useGFM = _prefs.get("useGFM");
marked.setOptions({
breaks: useGFM,
gfm: useGFM
});

// Re-render
_loadDoc(currentDoc);
}

function _documentClicked(e) {
if (!$settings.is(e.target) &&
!$settingsToggle.is(e.target) &&
$settings.has(e.target).length === 0) {
_hideSettings();
}
}

function _hideSettings() {
if ($settings) {
$settings.remove();
$settings = null;
$(window.document).off("mousedown", _documentClicked);
}
}

function _showSettings(e) {
_hideSettings();

$settings = $(settingsHTML)
.css({
right: 12,
top: $settingsToggle.position().top + $settingsToggle.outerHeight() + 12
})
.appendTo($panel);

$settings.find("#markdown-preview-format")
.prop("selectedIndex", _prefs.get("useGFM") ? 1 : 0)
.change(function (e) {
_prefs.set("useGFM", e.target.selectedIndex === 1);
_updateSettings();
});

$settings.find("#markdown-preview-theme")
.val(_prefs.get("theme"))
.change(function (e) {
_prefs.set("theme", e.target.value);
_updateSettings();
});

var $syncScroll = $settings.find("#markdown-preview-sync-scroll");

$syncScroll.change(function (e) {
_prefs.set("syncScroll", e.target.checked);
_editorScroll();
});

if (_prefs.get("syncScroll")) {
$syncScroll.attr("checked", true);
}

PopUpManager.addPopUp($settings, _hideSettings, true);
$(window.document).on("mousedown", _documentClicked);
}

function _setPanelVisibility(isVisible) {
if (isVisible === realVisibility) {
return;
}

realVisibility = isVisible;
if (isVisible) {
if (!panel) {
$panel = $(panelHTML);
$iframe = $panel.find("#panel-markdown-preview-frame");

panel = WorkspaceManager.createBottomPanel("markdown-preview-panel", $panel);
$panel.on("panelResizeUpdate", function (e, newSize) {
$iframe.attr("height", newSize);
});
$iframe.attr("height", $panel.height());

window.setTimeout(_resizeIframe);

$settingsToggle = $("#markdown-settings-toggle")
.click(function (e) {
if ($settings) {
Expand All @@ -272,7 +270,7 @@ define(function (require, exports, module) {
_showSettings(e);
}
});

$iframe.hide();
}
_loadDoc(DocumentManager.getCurrentDocument());
Expand All @@ -288,17 +286,17 @@ define(function (require, exports, module) {
function _currentDocChangedHandler() {
var doc = DocumentManager.getCurrentDocument(),
ext = doc ? FileUtils.getFileExtension(doc.file.fullPath).toLowerCase() : "";

if (currentDoc) {
currentDoc.off("change", _documentChange);
currentDoc = null;
}

if (currentEditor) {
currentEditor.off("scroll", _editorScroll);
currentEditor = null;
}

if (doc && /md|markdown|litcoffee|txt/.test(ext)) {
currentDoc = doc;
currentDoc.on("change", _documentChange);
Expand All @@ -312,23 +310,23 @@ define(function (require, exports, module) {
_setPanelVisibility(false);
}
}

function _toggleVisibility() {
visible = !visible;
_setPanelVisibility(visible);
}

// Debounce event callback to avoid excess overhead
// Update preview 300 ms ofter document change
// Sync scroll 1ms after document scroll (just enough to ensure
// the document scroll isn't blocked).
_documentChange = _.debounce(_documentChange, 300);
_editorScroll = _.debounce(_editorScroll, 1);

// Insert CSS for this extension
ExtensionUtils.loadStyleSheet(module, "styles/MarkdownPreview.css");
// Add toolbar icon

// Add toolbar icon
$icon = $("<a>")
.attr({
id: "markdown-preview-icon",
Expand All @@ -339,16 +337,16 @@ define(function (require, exports, module) {
})
.click(_toggleVisibility)
.appendTo($("#main-toolbar .buttons"));

// Add a document change handler
MainViewManager.on("currentFileChange", _currentDocChangedHandler);

// currentDocumentChange is *not* called for the initial document. Use
// appReady() to set initial state.
AppInit.appReady(function () {
_currentDocChangedHandler();
});

// Listen for resize events
WorkspaceManager.on("workspaceUpdateLayout", _resizeIframe);
$("#sidebar").on("panelCollapsed panelExpanded panelResizeUpdate", _resizeIframe);
Expand Down

0 comments on commit 3af22ac

Please sign in to comment.