Merge pull request #46 from NextStepWebs/development

Restructure to use gulp.js, Value improvements, Fix bugs
pull/50/head 1.5.1
Wes Cossick 9 years ago
commit 343b4a8027

3
.gitignore vendored

@ -1 +1,2 @@
localtesting/*
localtesting/*
node_modules/

@ -61,6 +61,7 @@ simplemde.value();
- **lineWrapping**: If set to `false`, disable line wrapping. Defaults to `true`.
- **indentWithTabs**: If set to `false`, indent using spaces instead of tabs. Defaults to `true`.
- **tabSize**: If set, customize the tab size. Defaults to `2`.
- **initialValue**: If set, will customize the initial value of the editor.
- **spellChecker**: If set to `false`, disable the spell checker. Defaults to `true`.
- **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`.
@ -73,10 +74,14 @@ var simplemde = new SimpleMDE({
status: false,
status: ['autosave', 'lines', 'words', 'cursor'], // Optional usage
toolbar: false,
toolbarTips: false,
toolbarGuideIcon: false,
autofocus: true,
lineWrapping: false,
indentWithTabs: false,
tabSize: 4,
initialValue: "Hello world!",
spellChecker: false,
autosave: {
enabled: true,
unique_id: "MyUniqueID",

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -0,0 +1,48 @@
var gulp = require('gulp'),
minifycss = require('gulp-minify-css'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat'),
header = require('gulp-header'),
pkg = require('./package.json');
var banner = ['/**',
' * <%= pkg.name %> v<%= pkg.version %>',
' * Copyright <%= pkg.company %>',
' * @link <%= pkg.homepage %>',
' * @license <%= pkg.license %>',
' */',
''].join('\n');
gulp.task('scripts', function() {
var js_files = [
'./src/js/codemirror/codemirror.js',
'./src/js/codemirror/continuelist.js',
'./src/js/codemirror/fullscreen.js',
'./src/js/codemirror/markdown.js',
'./src/js/codemirror/overlay.js',
'./src/js/codemirror/gfm.js',
'./src/js/codemirror/xml.js',
'./src/js/typo.js',
'./src/js/spell-checker.js',
'./src/js/marked.js',
'./src/js/simplemde.js'];
return gulp.src(js_files)
.pipe(header(banner, {pkg: pkg}))
.pipe(concat('simplemde.min.js'))
.pipe(gulp.dest('dist'))
.pipe(uglify())
.pipe(header(banner, {pkg: pkg}))
.pipe(gulp.dest('dist'));
});
gulp.task('styles', function() {
return gulp.src('./src/css/*.css')
.pipe(concat('simplemde.min.css'))
.pipe(gulp.dest('dist'))
.pipe(minifycss())
.pipe(header(banner, {pkg: pkg}))
.pipe(gulp.dest('dist'));
});
gulp.task('default', ['scripts', 'styles']);

@ -0,0 +1,28 @@
{
"name": "simplemde",
"version": "1.5.1",
"description": "A simple, beautiful, and embeddable JavaScript markdown editor. Features autosaving and spell checking.",
"keywords": ["embeddable", "markdown", "editor", "javascript", "wysiwyg"],
"homepage": "https://github.com/NextStepWebs/simplemde-markdown-editor",
"main": "gulpfile.js",
"license": "MIT",
"company": "Next Step Webs, Inc.",
"author": {
"name": "Wes Cossick",
"url": "http://www.WesCossick.com"
},
"bugs": {
"url": "https://github.com/NextStepWebs/simplemde-markdown-editor/issues"
},
"dependencies": {
"gulp": "*",
"gulp-minify-css": "*",
"gulp-uglify": "*",
"gulp-concat": "*",
"gulp-header": "*"
},
"repository": {
"type": "git",
"url": "https://github.com/NextStepWebs/simplemde-markdown-editor"
}
}

7
simplemde.min.css vendored

File diff suppressed because one or more lines are too long

13
simplemde.min.js vendored

File diff suppressed because one or more lines are too long

@ -1,20 +0,0 @@
# How to compile
Minify the JS in this order:
1. `codemirror/codemirror.js`
1. `codemirror/continuelist.js`
1. `codemirror/fullscreen.js`
1. `codemirror/markdown.js`
1. `codemirror/overlay.js`
1. `codemirror/gfm.js`
1. `codemirror/xml.js`
1. `typo/typo.js`
1. `spell-checker/spell-checker.js`
1. `marked.js`
1. `simplemde.js`
Minify the CSS in this order:
1. `codemirror/codemirror.css`
1. `simplemde.css`
1. `spell-checker/spell-checker.css`

@ -65,6 +65,7 @@
top: 0;
left: 0;
opacity: 1;
z-index: 9;
}
.editor-toolbar a {

@ -1,3 +1,3 @@
.CodeMirror .cm-spell-error:not(.cm-url):not(.cm-comment) {
background: rgba(255, 0, 0, .15);
}
background: rgba(255, 0, 0, .15);
}

@ -27,7 +27,7 @@
wrap.style.height = "auto";
wrap.className += " CodeMirror-fullscreen";
document.documentElement.style.overflow = "hidden";
document.getElementsByClassName("editor-toolbar")[0].className += " fullscreen";
wrap.previousSibling.className += " fullscreen";
cm.refresh();
}
@ -38,7 +38,7 @@
var info = cm.state.fullScreenRestore;
wrap.style.width = info.width; wrap.style.height = info.height;
window.scrollTo(info.scrollLeft, info.scrollTop);
document.getElementsByClassName("editor-toolbar")[0].className = document.getElementsByClassName("editor-toolbar")[0].className.replace(/\s*fullscreen\b/, "");
wrap.previousSibling.className = wrap.previousSibling.className.replace(/\s*fullscreen\b/, "");
cm.refresh();
}
});

@ -17,7 +17,7 @@ var shortcuts = {
* Fix shortcut. Mac use Command, others use Ctrl.
*/
function fixShortcut(name) {
if (isMac) {
if(isMac) {
name = name.replace('Ctrl', 'Cmd');
} else {
name = name.replace('Cmd', 'Ctrl');
@ -33,11 +33,11 @@ function createIcon(options, enableTooltips) {
options = options || {};
var el = document.createElement('a');
enableTooltips = (enableTooltips == undefined) ? true : enableTooltips;
if (options.title && enableTooltips) {
if(options.title && enableTooltips) {
el.title = options.title;
if (isMac) {
if(isMac) {
el.title = el.title.replace('Ctrl', '⌘');
el.title = el.title.replace('Alt', '⌥');
}
@ -61,28 +61,28 @@ function createSep() {
function getState(cm, pos) {
pos = pos || cm.getCursor('start');
var stat = cm.getTokenAt(pos);
if (!stat.type) return {};
if(!stat.type) return {};
var types = stat.type.split(' ');
var ret = {},
data, text;
for (var i = 0; i < types.length; i++) {
for(var i = 0; i < types.length; i++) {
data = types[i];
if (data === 'strong') {
if(data === 'strong') {
ret.bold = true;
} else if (data === 'variable-2') {
} else if(data === 'variable-2') {
text = cm.getLine(pos.line);
if (/^\s*\d+\.\s/.test(text)) {
if(/^\s*\d+\.\s/.test(text)) {
ret['ordered-list'] = true;
} else {
ret['unordered-list'] = true;
}
} else if (data === 'atom') {
} else if(data === 'atom') {
ret.quote = true;
} else if (data === 'em') {
} else if(data === 'em') {
ret.italic = true;
} else if (data === 'quote') {
} else if(data === 'quote') {
ret.quote = true;
}
}
@ -96,13 +96,12 @@ function getState(cm, pos) {
function toggleFullScreen(editor) {
var cm = editor.codemirror;
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
var toolbarButton = editor.toolbarElements.fullscreen;
if (!/active/.test(toolbarButton.className)) {
if(!/active/.test(toolbarButton.className)) {
toolbarButton.className += " active";
}
else {
} else {
toolbarButton.className = toolbarButton.className.replace(/\s*active\s*/g, '');
}
}
@ -211,18 +210,18 @@ function redo(editor) {
* Preview action.
*/
function togglePreview(editor) {
var toolbar_div = document.getElementsByClassName('editor-toolbar')[0];
var toolbar = editor.toolbarElements.preview;
var parse = editor.constructor.markdown;
var cm = editor.codemirror;
var wrapper = cm.getWrapperElement();
var toolbar_div = wrapper.previousSibling;
var toolbar = editor.toolbarElements.preview;
var parse = editor.constructor.markdown;
var preview = wrapper.lastChild;
if (!/editor-preview/.test(preview.className)) {
if(!/editor-preview/.test(preview.className)) {
preview = document.createElement('div');
preview.className = 'editor-preview';
wrapper.appendChild(preview);
}
if (/editor-preview-active/.test(preview.className)) {
if(/editor-preview-active/.test(preview.className)) {
preview.className = preview.className.replace(
/\s*editor-preview-active\s*/g, ''
);
@ -244,13 +243,13 @@ function togglePreview(editor) {
}
function _replaceSelection(cm, active, start, end) {
if (/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
return;
var text;
var startPoint = cm.getCursor('start');
var endPoint = cm.getCursor('end');
if (active) {
if(active) {
text = cm.getLine(startPoint.line);
start = text.slice(0, startPoint.ch);
end = text.slice(startPoint.ch);
@ -271,9 +270,9 @@ function _replaceSelection(cm, active, start, end) {
function _toggleLine(cm, name) {
if (/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
return;
var stat = getState(cm);
var startPoint = cm.getCursor('start');
var endPoint = cm.getCursor('end');
@ -287,10 +286,10 @@ function _toggleLine(cm, name) {
'unordered-list': '* ',
'ordered-list': '1. '
};
for (var i = startPoint.line; i <= endPoint.line; i++) {
for(var i = startPoint.line; i <= endPoint.line; i++) {
(function(i) {
var text = cm.getLine(i);
if (stat[name]) {
if(stat[name]) {
text = text.replace(repl[name], '$1');
} else {
text = map[name] + text;
@ -308,9 +307,9 @@ function _toggleLine(cm, name) {
}
function _toggleBlock(editor, type, start_chars, end_chars) {
if (/editor-preview-active/.test(editor.codemirror.getWrapperElement().lastChild.className))
if(/editor-preview-active/.test(editor.codemirror.getWrapperElement().lastChild.className))
return;
end_chars = (typeof end_chars === 'undefined') ? start_chars : end_chars;
var cm = editor.codemirror;
var stat = getState(cm);
@ -322,14 +321,14 @@ function _toggleBlock(editor, type, start_chars, end_chars) {
var startPoint = cm.getCursor('start');
var endPoint = cm.getCursor('end');
if (stat[type]) {
if(stat[type]) {
text = cm.getLine(startPoint.line);
start = text.slice(0, startPoint.ch);
end = text.slice(startPoint.ch);
if (type == "bold") {
if(type == "bold") {
start = start.replace(/(\*\*|__)(?![\s\S]*(\*\*|__))/, "");
end = end.replace(/(\*\*|__)/, "");
} else if (type == "italic") {
} else if(type == "italic") {
start = start.replace(/(\*|_)(?![\s\S]*(\*|_))/, "");
end = end.replace(/(\*|_)/, "");
}
@ -341,19 +340,19 @@ function _toggleBlock(editor, type, start_chars, end_chars) {
ch: 99999999999999
});
if (type == "bold") {
if(type == "bold") {
startPoint.ch -= 2;
endPoint.ch -= 2;
} else if (type == "italic") {
} else if(type == "italic") {
startPoint.ch -= 1;
endPoint.ch -= 1;
}
} else {
text = cm.getSelection();
if (type == "bold") {
if(type == "bold") {
text = text.split("**").join("");
text = text.split("__").join("");
} else if (type == "italic") {
} else if(type == "italic") {
text = text.split("*").join("");
text = text.split("_").join("");
}
@ -373,9 +372,9 @@ function wordCount(data) {
var pattern = /[a-zA-Z0-9_\u0392-\u03c9]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af]+/g;
var m = data.match(pattern);
var count = 0;
if (m === null) return count;
for (var i = 0; i < m.length; i++) {
if (m[i].charCodeAt(0) >= 0x4E00) {
if(m === null) return count;
for(var i = 0; i < m.length; i++) {
if(m[i].charCodeAt(0) >= 0x4E00) {
count += m[i].length;
} else {
count += 1;
@ -390,59 +389,50 @@ var toolbar = [{
action: toggleBold,
className: "fa fa-bold",
title: "Bold (Ctrl+B)",
},
{
}, {
name: "italic",
action: toggleItalic,
className: "fa fa-italic",
title: "Italic (Ctrl+I)",
},
"|",
{
"|", {
name: "quote",
action: toggleBlockquote,
className: "fa fa-quote-left",
title: "Quote (Ctrl+')",
},
{
}, {
name: "unordered-list",
action: toggleUnorderedList,
className: "fa fa-list-ul",
title: "Generic List (Ctrl+L)",
},
{
}, {
name: "ordered-list",
action: toggleOrderedList,
className: "fa fa-list-ol",
title: "Numbered List (Ctrl+Alt+L)",
},
"|",
{
"|", {
name: "link",
action: drawLink,
className: "fa fa-link",
title: "Create Link (Ctrl+K)",
},
{
}, {
name: "quote",
action: drawImage,
className: "fa fa-picture-o",
title: "Insert Image (Ctrl+Alt+I)",
},
"|",
{
"|", {
name: "preview",
action: togglePreview,
className: "fa fa-eye",
title: "Toggle Preview (Ctrl+P)",
},
{
}, {
name: "fullscreen",
action: toggleFullScreen,
className: "fa fa-arrows-alt",
title: "Toggle Fullscreen (F11)",
},
{
}, {
name: "guide",
action: "http://nextstepwebs.github.io/simplemde-markdown-editor/markdown-guide",
className: "fa fa-question-circle",
@ -456,14 +446,14 @@ var toolbar = [{
function SimpleMDE(options) {
options = options || {};
if (options.element) {
if(options.element) {
this.element = options.element;
}
if (options.toolbar !== false)
if(options.toolbar !== false)
options.toolbar = options.toolbar || SimpleMDE.toolbar;
if (!options.hasOwnProperty('status')) {
if(!options.hasOwnProperty('status')) {
options.status = ['autosave', 'lines', 'words', 'cursor'];
}
@ -471,6 +461,13 @@ function SimpleMDE(options) {
// If user has passed an element, it should auto rendered
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
if(options.initialValue) {
this.value(options.initialValue);
}
}
/**
@ -482,7 +479,7 @@ SimpleMDE.toolbar = toolbar;
* Default markdown render.
*/
SimpleMDE.markdown = function(text) {
if (window.marked) {
if(window.marked) {
// use marked as markdown parser
return marked(text);
}
@ -492,11 +489,11 @@ SimpleMDE.markdown = function(text) {
* Render editor to the given element.
*/
SimpleMDE.prototype.render = function(el) {
if (!el) {
if(!el) {
el = this.element || document.getElementsByTagName('textarea')[0];
}
if (this._rendered && this._rendered === el) {
if(this._rendered && this._rendered === el) {
// Already rendered.
return;
}
@ -507,7 +504,7 @@ SimpleMDE.prototype.render = function(el) {
var self = this;
var keyMaps = {};
for (var key in shortcuts) {
for(var key in shortcuts) {
(function(key) {
keyMaps[fixShortcut(key)] = function(cm) {
shortcuts[key](self);
@ -522,17 +519,17 @@ SimpleMDE.prototype.render = function(el) {
toggleFullScreen(cm);
};
keyMaps["Esc"] = function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
if(cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
};
var mode = "spell-checker";
var backdrop = "gfm";
if (options.spellChecker === false) {
if(options.spellChecker === false) {
mode = "gfm";
backdrop = undefined;
}
this.codemirror = CodeMirror.fromTextArea(el, {
mode: mode,
backdrop: backdrop,
@ -546,13 +543,13 @@ SimpleMDE.prototype.render = function(el) {
lineWrapping: (options.lineWrapping === false) ? false : true
});
if (options.toolbar !== false) {
if(options.toolbar !== false) {
this.createToolbar();
}
if (options.status !== false) {
if(options.status !== false) {
this.createStatusbar();
}
if (options.autosave != undefined && options.autosave.enabled === true) {
if(options.autosave != undefined && options.autosave.enabled === true) {
this.autosave();
}
@ -562,48 +559,48 @@ SimpleMDE.prototype.render = function(el) {
SimpleMDE.prototype.autosave = function() {
var content = this.value();
var simplemde = this;
if(this.options.autosave.unique_id == undefined || this.options.autosave.unique_id == ""){
if(this.options.autosave.unique_id == undefined || this.options.autosave.unique_id == "") {
console.log("SimpleMDE: You must set a unique_id to use the autosave feature");
return;
}
if(simplemde.element.form != null && simplemde.element.form != undefined){
simplemde.element.form.addEventListener("submit", function(){
if(simplemde.element.form != null && simplemde.element.form != undefined) {
simplemde.element.form.addEventListener("submit", function() {
localStorage.setItem(simplemde.options.autosave.unique_id, "");
});
}
if(this.options.autosave.loaded !== true){
if(this.options.autosave.loaded !== true) {
if(localStorage.getItem(this.options.autosave.unique_id) != null)
this.codemirror.setValue(localStorage.getItem(this.options.autosave.unique_id));
this.options.autosave.loaded = true;
}
if(localStorage) {
localStorage.setItem(this.options.autosave.unique_id, content);
}
var el = document.getElementById("autosaved");
if(el != null && el != undefined && el != ""){
if(el != null && el != undefined && el != "") {
var d = new Date();
var hh = d.getHours();
var m = d.getMinutes();
var dd = "am";
var h = hh;
if (h >= 12) {
h = hh-12;
if(h >= 12) {
h = hh - 12;
dd = "pm";
}
if (h == 0) {
if(h == 0) {
h = 12;
}
m = m<10?"0"+m:m;
el.innerHTML = "Autosaved: "+h+":"+m+" "+dd;
m = m < 10 ? "0" + m : m;
el.innerHTML = "Autosaved: " + h + ":" + m + " " + dd;
}
setTimeout(function() {
simplemde.autosave();
}, this.options.autosave.delay || 10000);
@ -612,7 +609,7 @@ SimpleMDE.prototype.autosave = function() {
SimpleMDE.prototype.createToolbar = function(items) {
items = items || this.options.toolbar;
if (!items || items.length === 0) {
if(!items || items.length === 0) {
return;
}
@ -625,25 +622,25 @@ SimpleMDE.prototype.createToolbar = function(items) {
var toolbar_data = {};
self.toolbar = items;
for (var i = 0; i < items.length; i++) {
for(var i = 0; i < items.length; i++) {
if(items[i].name == "guide" && self.options.toolbarGuideIcon === false)
continue;
(function(item) {
var el;
if (item === '|') {
if(item === '|') {
el = createSep();
} else {
el = createIcon(item, self.options.toolbarTips);
}
// bind events, special for info
if (item.action) {
if (typeof item.action === 'function') {
if(item.action) {
if(typeof item.action === 'function') {
el.onclick = function(e) {
item.action(self);
};
} else if (typeof item.action === 'string') {
} else if(typeof item.action === 'string') {
el.href = item.action;
el.target = '_blank';
}
@ -652,17 +649,17 @@ SimpleMDE.prototype.createToolbar = function(items) {
bar.appendChild(el);
})(items[i]);
}
self.toolbarElements = toolbar_data;
var cm = this.codemirror;
cm.on('cursorActivity', function() {
var stat = getState(cm);
for (var key in toolbar_data) {
for(var key in toolbar_data) {
(function(key) {
var el = toolbar_data[key];
if (stat[key]) {
if(stat[key]) {
el.className += ' active';
} else if(key != "fullscreen") {
el.className = el.className.replace(/\s*active\s*/g, '');
@ -680,41 +677,41 @@ SimpleMDE.prototype.createStatusbar = function(status) {
status = status || this.options.status;
options = this.options;
if (!status || status.length === 0) return;
if(!status || status.length === 0) return;
var bar = document.createElement('div');
bar.className = 'editor-statusbar';
var pos, cm = this.codemirror;
for (var i = 0; i < status.length; i++) {
for(var i = 0; i < status.length; i++) {
(function(name) {
var el = document.createElement('span');
el.className = name;
if (name === 'words') {
if(name === 'words') {
el.innerHTML = '0';
cm.on('update', function() {
el.innerHTML = wordCount(cm.getValue());
});
} else if (name === 'lines') {
} else if(name === 'lines') {
el.innerHTML = '0';
cm.on('update', function() {
el.innerHTML = cm.lineCount();
});
} else if (name === 'cursor') {
} else if(name === 'cursor') {
el.innerHTML = '0:0';
cm.on('cursorActivity', function() {
pos = cm.getCursor();
el.innerHTML = pos.line + ':' + pos.ch;
});
} else if (name === 'autosave') {
if (options.autosave != undefined && options.autosave.enabled === true) {
} else if(name === 'autosave') {
if(options.autosave != undefined && options.autosave.enabled === true) {
el.setAttribute("id", "autosaved");
}
}
bar.appendChild(el);
})(status[i]);
}
var cmWrapper = this.codemirror.getWrapperElement();
cmWrapper.parentNode.insertBefore(bar, cmWrapper.nextSibling);
return bar;
@ -724,11 +721,11 @@ SimpleMDE.prototype.createStatusbar = function(status) {
* Get or set the text content.
*/
SimpleMDE.prototype.value = function(val) {
if (val) {
if(val === undefined) {
return this.codemirror.getValue();
} else {
this.codemirror.getDoc().setValue(val);
return this;
} else {
return this.codemirror.getValue();
}
};
Loading…
Cancel
Save