From bfda8350aeab1497190dab436c43642835fa3867 Mon Sep 17 00:00:00 2001 From: Wes Cossick Date: Sat, 19 Sep 2015 20:42:20 -0500 Subject: [PATCH] Auto download Font Awesome --- README.md | 7 +------ src/js/simplemde.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 986e18b..cd9d103 100644 --- a/README.md +++ b/README.md @@ -21,12 +21,6 @@ SimpleMDE is also available on [jsDelivr](http://www.jsdelivr.com/#!simplemde). ``` -SimpleMDE depends on Font Awesome (load via MaxCDN for best performance). - -```HTML - -``` - And then load SimpleMDE on the first textarea on a page ```HTML @@ -65,6 +59,7 @@ simplemde.value("This text will appear in the editor"); ## Configuration +- **autoDownloadFontAwesome**: If set to `true`, force downloads Font Awesome (used for icons). If set to false, prevents downloading. Defaults to `undefined`, which will intelligently check whether Font Awesome has already been included, then download accordingly. - **autofocus**: If set to `true`, autofocuses the editor. Defaults to `false`. - **autosave**: *Saves the text that's being written. It will forget the text when the form is submitted.* - **enabled**: If set to `true`, autosave the text. Defaults to `false`. diff --git a/src/js/simplemde.js b/src/js/simplemde.js index d9e1597..11a7f70 100644 --- a/src/js/simplemde.js +++ b/src/js/simplemde.js @@ -733,10 +733,44 @@ var toolbar = ["bold", "italic", "heading", "|", "quote", "unordered-list", "ord * Interface of SimpleMDE. */ function SimpleMDE(options) { + // Handle options parameter options = options || {}; + // Used later to refer to it's parent options.parent = this; + + + // Check if Font Awesome needs to be auto downloaded + var autoDownloadFA = true; + + if(options.autoDownloadFontAwesome === false){ + autoDownloadFA = false; + } + + if(options.autoDownloadFontAwesome !== true){ + var styleSheets = document.styleSheets; + for(var i = 0; i < styleSheets.length; i++) { + if(!styleSheets[i].href) + continue; + + if(styleSheets[i].href.indexOf("//maxcdn.bootstrapcdn.com/font-awesome/") > -1){ + autoDownloadFA = false; + } + } + } + + if(autoDownloadFA){ + console.log("autodownloading"); + + var link = document.createElement("link"); + link.rel = "stylesheet"; + link.href = "https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css"; + document.getElementsByTagName("head")[0].appendChild(link); + }else{ + console.log("not autodownloading"); + } + // Find the textarea to use if(options.element) { @@ -747,6 +781,7 @@ function SimpleMDE(options) { return; } + // Handle toolbar and status bar if(options.toolbar !== false) options.toolbar = options.toolbar || SimpleMDE.toolbar; @@ -755,6 +790,7 @@ function SimpleMDE(options) { options.status = ['autosave', 'lines', 'words', 'cursor']; } + // Add default preview rendering function if(!options.previewRender) { options.previewRender = function(plainText) { @@ -763,15 +799,19 @@ function SimpleMDE(options) { } } + // Set default options for parsing config options.parsingConfig = options.parsingConfig || {}; + // Update this options this.options = options; + // Auto render this.render(); + // The codemirror component is only available after rendering // so, the setter for the initialValue can only run after // the element has been rendered