Merge pull request #80 from NextStepWebs/development

New side-by-side previewing, Better line break handling, Heading icon improvements, New icons, Mobile improvements
pull/75/head 1.7.0
Wes Cossick 9 years ago
commit fbf4af9c73

@ -1,9 +1,9 @@
# SimpleMDE - Markdown Editor
A drop-in JavaScript textarea replacement for writing beautiful and understandable markdown. The WYSIWYG-esque editor allows users who may be less experienced with Markdown to use familiar toolbar buttons and shortcuts. In addition, the syntax is rendered while editing to clearly show the expected result. Headings are larger, emphasized words are italicized, links are underlined, etc. SimpleMDE is one of the first editors to feature both built-in autosaving and spell checking.
A drop-in JavaScript textarea replacement for writing beautiful and understandable Markdown. The WYSIWYG-esque editor allows users who may be less experienced with Markdown to use familiar toolbar buttons and shortcuts. In addition, the syntax is rendered while editing to clearly show the expected result. Headings are larger, emphasized words are italicized, links are underlined, etc. SimpleMDE is one of the first editors to feature both built-in autosaving and spell checking.
[**Demo**](http://nextstepwebs.github.io/simplemde-markdown-editor)
[![Preview](http://i.imgur.com/b9hFHFT.png)](http://nextstepwebs.github.io/simplemde-markdown-editor)
[![Preview](http://i.imgur.com/zqWfJwO.png)](http://nextstepwebs.github.io/simplemde-markdown-editor)
## Why not a WYSIWYG editor or pure Markdown?
WYSIWYG editors that produce HTML are often complex and buggy. Markdown solves this problem in many ways, plus Markdown can be rendered natively on more platforms than HTML. However, Markdown is not a syntax that an average user will be familiar with, nor is it visually clear while editing. In otherwords, for an unfamiliar user, the syntax they write will make little sense until they click the preview button. SimpleMDE has been designed to bridge this gap for non-technical users who are less familiar with or just learning Markdown syntax.
@ -66,6 +66,7 @@ simplemde.value();
- **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`.
- **singleLineBreaks**: If set to `false`, disable parsing GFM single line breaks. 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`.
- **unique_id**: You must set a unique identifier so that SimpleMDE can autosave. Something that separates this from other textareas.
@ -85,6 +86,7 @@ var simplemde = new SimpleMDE({
tabSize: 4,
initialValue: "Hello world!",
spellChecker: false,
singleLineBreaks: false,
autosave: {
enabled: true,
unique_id: "MyUniqueID",
@ -95,15 +97,19 @@ var simplemde = new SimpleMDE({
#### Toolbar icons
Below are the built-in toolbar icons, which can be reorganized however you like. "Name" is the name of the icon, referenced in the JS. "Action" is either a function or a URL to open. "Class" is the class given to the icon. "Tooltip" is the small tooltip that appears via the `title=""` attribute. The `Ctrl` and `Alt` in the title tags will be changed automatically to their Mac equivalents when needed. Additionally, you can add a separator between any icons by adding `"|"` to the toolbar array.
Below are the built-in toolbar icons (only some of which are enabled by default), which can be reorganized however you like. "Name" is the name of the icon, referenced in the JS. "Action" is either a function or a URL to open. "Class" is the class given to the icon. "Tooltip" is the small tooltip that appears via the `title=""` attribute. The `Ctrl` and `Alt` in the title tags will be changed automatically to their Mac equivalents when needed. Additionally, you can add a separator between any icons by adding `"|"` to the toolbar array.
Name | Action | Class | Tooltip
:--- | :----- | :---- | :------
bold | toggleBold | fa fa-bold | Bold (Ctrl+B)
italic | toggleItalic | fa fa-italic | Italic (Ctrl+I)
strikethrough | toggleStrikethrough | fa fa-strikethrough | Strikethrough
heading | toggleHeadingSmaller | fa fa-header | Heading (Ctrl+H)
heading-smaller | toggleHeadingSmaller | fa fa-header | Smaller Heading (Ctrl+H)
heading-bigger | toggleHeadingBigger | fa fa-lg fa-header | Bigger Heading (Shift+Ctrl+H)
heading-1 | toggleHeading1 | fa fa-header fa-header-x fa-header-1 | Big Heading
heading-2 | toggleHeading2 | fa fa-header fa-header-x fa-header-2 | Medium Heading
heading-3 | toggleHeading3 | fa fa-header fa-header-x fa-header-3 | Small Heading
code | toggleCodeBlock | fa fa-code | Code (Ctrl+Alt+C)
quote | toggleBlockquote | fa fa-quote-left | Quote (Ctrl+')
unordered-list | toggleUnorderedList | fa fa-list-ul | Generic List (Ctrl+L)
@ -111,8 +117,9 @@ numbered-list | toggleOrderedList | fa fa-list-ol | Numbered List (Ctrl+Alt+L)
link | drawLink | fa fa-link | Create Link (Ctrl+K)
image | drawImage | fa fa-picture-o | Insert Image (Ctrl+Alt+I)
horizontal-rule | drawHorizontalRule | fa fa-minus | Insert Horizontal Line
fullscreen | toggleFullScreen | fa fa-arrows-alt | Toggle Fullscreen (F11)
preview | togglePreview | fa fa-eye | Toggle Preview (Ctrl+P)
side-by-side | toggleSideBySide | fa fa-columns | Toggle Side by Side (F9)
fullscreen | toggleFullScreen | fa fa-arrows-alt | Toggle Fullscreen (F11)
guide | [This link](http://nextstepwebs.github.io/simplemde-markdown-editor/markdown-guide) | fa fa-question-circle | Markdown Guide
Customize the toolbar using the `toolbar` option like:
@ -168,7 +175,7 @@ simplemde.codemirror.on("change", function(){
## How it works
SimpleMDE is an improvement of [lepture's Editor project](https://github.com/lepture/editor) and includes a great many number of changes. It is bundled with [CodeMirror](https://github.com/codemirror/codemirror) and depends on [Font Awesome](http://fortawesome.github.io/Font-Awesome/).
CodeMirror is the backbone of the project and parses much of the Markdown syntax as it's being written. This allows us to add styles to the Markdown that's being written. Additionally, a toolbar and status bar have been added to the top and bottom, respectively. Previews are rendered by [Marked](https://github.com/chjj/marked).
CodeMirror is the backbone of the project and parses much of the Markdown syntax as it's being written. This allows us to add styles to the Markdown that's being written. Additionally, a toolbar and status bar have been added to the top and bottom, respectively. Previews are rendered by [Marked](https://github.com/chjj/marked) using GFM.
## What's changed?
As mentioned earlier, SimpleMDE is an improvement of [lepture's Editor project](https://github.com/lepture/editor). So you might be wondering, what's changed? Quite a bit actually. Here's some notable changes:

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -3,7 +3,8 @@ var gulp = require('gulp'),
uglify = require('gulp-uglify'),
concat = require('gulp-concat'),
header = require('gulp-header'),
pkg = require('./package.json');
pkg = require('./package.json'),
prettify = require('gulp-jsbeautifier');
var banner = ['/**',
' * <%= pkg.name %> v<%= pkg.version %>',
@ -44,5 +45,17 @@ gulp.task('styles', function() {
.pipe(header(banner, {pkg: pkg}))
.pipe(gulp.dest('dist'));
});
gulp.task('prettify-js', function() {
gulp.src('./src/js/simplemde.js')
.pipe(prettify({js: {braceStyle: "collapse", indentChar: "\t", indentSize: 1, maxPreserveNewlines: 3, spaceBeforeConditional: false}}))
.pipe(gulp.dest('./src/js'));
});
gulp.task('prettify-css', function() {
gulp.src('./src/css/simplemde.css')
.pipe(prettify({css: {indentChar: "\t", indentSize: 1}}))
.pipe(gulp.dest('./src/css'));
});
gulp.task('default', ['scripts', 'styles']);
gulp.task('default', ['scripts', 'styles', 'prettify-js', 'prettify-css']);

@ -1,6 +1,6 @@
{
"name": "simplemde",
"version": "1.6.1",
"version": "1.7.0",
"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",
@ -19,7 +19,8 @@
"gulp-minify-css": "*",
"gulp-uglify": "*",
"gulp-concat": "*",
"gulp-header": "*"
"gulp-header": "*",
"gulp-jsbeautifier": "*"
},
"repository": {
"type": "git",

@ -13,7 +13,7 @@
}
.CodeMirror-fullscreen {
background:#fff;
background: #fff;
position: fixed !important;
top: 50px;
left: 0;
@ -23,6 +23,10 @@
z-index: 9;
}
.CodeMirror-sided {
width: 50% !important;
}
.editor-toolbar {
position: relative;
opacity: .6;
@ -39,7 +43,8 @@
border-top-right-radius: 4px;
}
.editor-toolbar:after, .editor-toolbar:before {
.editor-toolbar:after,
.editor-toolbar:before {
display: block;
content: ' ';
height: 1px;
@ -53,16 +58,19 @@
margin-top: 8px
}
.editor-toolbar:hover, .editor-wrapper input.title:focus, .editor-wrapper input.title:hover {
.editor-toolbar:hover,
.editor-wrapper input.title:focus,
.editor-wrapper input.title:hover {
opacity: .8
}
.editor-toolbar.fullscreen {
width: 100%;
height: 50px;
height: 40px;
overflow-x: auto;
overflow-y: hidden;
white-space: nowrap;
padding-top: 10px;
background: #fff;
border: 0;
position: fixed;
@ -72,6 +80,40 @@
z-index: 9;
}
.editor-toolbar.fullscreen::before {
width: 20px;
height: 50px;
background: -moz-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(125, 185, 232, 0.01) 100%);
background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 1)), color-stop(100%, rgba(125, 185, 232, 0.01)));
background: -webkit-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(125, 185, 232, 0.01) 100%);
background: -o-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(125, 185, 232, 0.01) 100%);
background: -ms-linear-gradient(left, rgba(255, 255, 255, 1) 0%, rgba(125, 185, 232, 0.01) 100%);
background: linear-gradient(to right, rgba(255, 255, 255, 1) 0%, rgba(125, 185, 232, 0.01) 100%);
filter: progid: DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#037db9e8', GradientType=1);
position: fixed;
top: 0;
left: 0;
margin: 0;
padding: 0;
}
.editor-toolbar.fullscreen::after {
width: 20px;
height: 50px;
background: -moz-linear-gradient(left, rgba(125, 185, 232, 0.01) 0%, rgba(254, 254, 255, 1) 99%, rgba(255, 255, 255, 1) 100%);
background: -webkit-gradient(linear, left top, right top, color-stop(0%, rgba(125, 185, 232, 0.01)), color-stop(99%, rgba(254, 254, 255, 1)), color-stop(100%, rgba(255, 255, 255, 1)));
background: -webkit-linear-gradient(left, rgba(125, 185, 232, 0.01) 0%, rgba(254, 254, 255, 1) 99%, rgba(255, 255, 255, 1) 100%);
background: -o-linear-gradient(left, rgba(125, 185, 232, 0.01) 0%, rgba(254, 254, 255, 1) 99%, rgba(255, 255, 255, 1) 100%);
background: -ms-linear-gradient(left, rgba(125, 185, 232, 0.01) 0%, rgba(254, 254, 255, 1) 99%, rgba(255, 255, 255, 1) 100%);
background: linear-gradient(to right, rgba(125, 185, 232, 0.01) 0%, rgba(254, 254, 255, 1) 99%, rgba(255, 255, 255, 1) 100%);
filter: progid: DXImageTransform.Microsoft.gradient( startColorstr='#037db9e8', endColorstr='#ffffff', GradientType=1);
position: fixed;
top: 0;
right: 0;
margin: 0;
padding: 0;
}
.editor-toolbar a {
display: inline-block;
text-align: center;
@ -85,7 +127,8 @@
cursor: pointer;
}
.editor-toolbar a.active, .editor-toolbar a:hover {
.editor-toolbar a.active,
.editor-toolbar a:hover {
background: #fcfcfc;
border-color: #95a5a6;
}
@ -104,15 +147,43 @@
margin: 0 6px;
}
.editor-toolbar a.icon-fullscreen {
position: absolute;
right: 10px;
.editor-toolbar a.fa-header-x:after {
font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
font-size: 65%;
vertical-align: text-bottom;
position: relative;
top: 2px;
}
.editor-toolbar a.fa-header-1:after {
content: "1";
}
.editor-toolbar a.fa-header-2:after {
content: "2";
}
.editor-toolbar.disabled-for-preview a:not(.fa-eye):not(.fa-arrows-alt) {
.editor-toolbar a.fa-header-3:after {
content: "3";
}
.editor-toolbar a.fa-header-bigger:after {
content: "▲";
}
.editor-toolbar a.fa-header-smaller:after {
content: "▼";
}
.editor-toolbar.disabled-for-preview a:not(.fa-eye):not(.fa-arrows-alt):not(.fa-columns) {
pointer-events: none;
background: #fff;
border: none;
}
@media only screen and (max-width: 700px) {
.editor-toolbar a.fa-columns {
display: none;
}
}
.editor-statusbar {
@ -139,8 +210,8 @@
.editor-preview {
padding: 10px;
position: absolute;
width:100%;
height:100%;
width: 100%;
height: 100%;
top: 0;
left: 0;
background: #fafafa;
@ -150,20 +221,44 @@
box-sizing: border-box;
}
.editor-preview-side {
padding: 10px;
position: fixed;
bottom: 0;
width: 50%;
top: 50px;
right: 0;
background: #fafafa;
z-index: 9999;
overflow: auto;
display: none;
box-sizing: border-box;
border: 1px solid #ddd;
}
.editor-preview-active-side {
display: block
}
.editor-preview-active {
display: block
}
.editor-preview>p {
.editor-preview>p,
.editor-preview-side>p {
margin-top: 0
}
.editor-preview pre {
.editor-preview pre,
.editor-preview-side pre {
background: #eee;
margin-bottom: 10px;
}
.editor-preview table td, table th {
.editor-preview table td,
.editor-preview table th,
.editor-preview-side table td,
.editor-preview-side table th {
border: 1px solid #ddd;
padding: 5px;
}

@ -86,12 +86,17 @@ function getState(cm, pos) {
ret.italic = true;
} else if(data === 'quote') {
ret.quote = true;
} else if(data === 'strikethrough') {
ret.strikethrough = true;
}
}
return ret;
}
// Saved overflow setting
var saved_overflow = "";
/**
* Toggle full screen of the editor.
*/
@ -99,18 +104,27 @@ function toggleFullScreen(editor) {
// Set fullscreen
var cm = editor.codemirror;
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
// Prevent scrolling on body during fullscreen active
if(cm.getOption("fullScreen")) {
saved_overflow = document.body.style.overflow;
document.body.style.overflow = "hidden";
} else {
document.body.style.overflow = saved_overflow;
}
// Update toolbar class
var wrap = cm.getWrapperElement();
if(!/fullscreen/.test(wrap.previousSibling.className)) {
wrap.previousSibling.className += " fullscreen";
} else {
wrap.previousSibling.className = wrap.previousSibling.className.replace(/\s*fullscreen\b/, "");
}
// Update toolbar button
var toolbarButton = editor.toolbarElements.fullscreen;
@ -119,6 +133,12 @@ function toggleFullScreen(editor) {
} else {
toolbarButton.className = toolbarButton.className.replace(/\s*active\s*/g, '');
}
// Hide side by side if needed
var sidebyside = cm.getWrapperElement().nextSibling;
if(/editor-preview-active-side/.test(sidebyside.className))
toggleSideBySide(editor);
}
@ -137,6 +157,14 @@ function toggleItalic(editor) {
_toggleBlock(editor, 'italic', '*');
}
/**
* Action for toggling strikethrough.
*/
function toggleStrikethrough(editor) {
_toggleBlock(editor, 'strikethrough', '~~');
}
/**
* Action for toggling code block.
*/
@ -168,6 +196,30 @@ function toggleHeadingBigger(editor) {
_toggleHeading(cm, 'bigger');
}
/**
* Action for toggling heading size 1
*/
function toggleHeading1(editor) {
var cm = editor.codemirror;
_toggleHeading(cm, undefined, 1);
}
/**
* Action for toggling heading size 2
*/
function toggleHeading2(editor) {
var cm = editor.codemirror;
_toggleHeading(cm, undefined, 2);
}
/**
* Action for toggling heading size 3
*/
function toggleHeading3(editor) {
var cm = editor.codemirror;
_toggleHeading(cm, undefined, 3);
}
/**
* Action for toggling ul.
@ -236,6 +288,59 @@ function redo(editor) {
cm.focus();
}
/**
* Toggle side by side preview
*/
function toggleSideBySide(editor) {
var cm = editor.codemirror;
var wrapper = cm.getWrapperElement();
var code = wrapper.firstChild;
var preview = wrapper.nextSibling;
var toolbarButton = editor.toolbarElements["side-by-side"];
if(/editor-preview-active-side/.test(preview.className)) {
preview.className = preview.className.replace(
/\s*editor-preview-active-side\s*/g, ''
);
toolbarButton.className = toolbarButton.className.replace(/\s*active\s*/g, '');
wrapper.className = wrapper.className.replace(/\s*CodeMirror-sided\s*/g, ' ');
} else {
/* When the preview button is clicked for the first time,
* give some time for the transition from editor.css to fire and the view to slide from right to left,
* instead of just appearing.
*/
setTimeout(function() {
if(!cm.getOption("fullScreen")) toggleFullScreen(editor);
preview.className += ' editor-preview-active-side'
}, 1);
toolbarButton.className += ' active';
wrapper.className += ' CodeMirror-sided';
}
// Hide normal preview if active
var previewNormal = wrapper.lastChild;
if(/editor-preview-active/.test(previewNormal.className)) {
previewNormal.className = previewNormal.className.replace(
/\s*editor-preview-active\s*/g, ''
);
var toolbar = editor.toolbarElements.preview;
var toolbar_div = wrapper.previousSibling;
toolbar.className = toolbar.className.replace(/\s*active\s*/g, '');
toolbar_div.className = toolbar_div.className.replace(/\s*disabled-for-preview*/g, '');
}
// Start preview with the current text
var parse = editor.constructor.markdown;
preview.innerHTML = parse(cm.getValue());
// Updates preview
cm.on('update', function() {
preview.innerHTML = parse(cm.getValue());
});
}
/**
* Preview action.
*/
@ -270,6 +375,11 @@ function togglePreview(editor) {
}
var text = cm.getValue();
preview.innerHTML = parse(text);
// Turn off side by side if needed
var sidebyside = cm.getWrapperElement().nextSibling;
if(/editor-preview-active-side/.test(sidebyside.className))
toggleSideBySide(editor);
}
function _replaceSelection(cm, active, start, end) {
@ -299,7 +409,7 @@ function _replaceSelection(cm, active, start, end) {
}
function _toggleHeading(cm, direction) {
function _toggleHeading(cm, direction, size) {
if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
return;
@ -309,21 +419,53 @@ function _toggleHeading(cm, direction) {
(function(i) {
var text = cm.getLine(i);
var currHeadingLevel = text.search(/[^#]/);
if (currHeadingLevel <= 0) {
if (direction == 'bigger') {
text = '###### ' + text;
if(direction !== undefined) {
if(currHeadingLevel <= 0) {
if(direction == 'bigger') {
text = '###### ' + text;
} else {
text = '# ' + text;
}
} else if(currHeadingLevel == 6 && direction == 'smaller') {
text = text.substr(7);
} else if(currHeadingLevel == 1 && direction == 'bigger') {
text = text.substr(2);
} else {
text = '# ' + text;
if(direction == 'bigger') {
text = text.substr(1);
} else {
text = '#' + text;
}
}
} else if ((currHeadingLevel == 6 && direction == 'smaller') || (currHeadingLevel == 1 && direction == 'bigger')) {
text = text.substr(7);
} else {
if (direction == 'bigger') {
text = text.substr(1);
if(size == 1) {
if(currHeadingLevel <= 0) {
text = '# ' + text;
} else if(currHeadingLevel == size) {
text = text.substr(currHeadingLevel + 1);
} else {
text = '# ' + text.substr(currHeadingLevel + 1);
}
} else if(size == 2) {
if(currHeadingLevel <= 0) {
text = '## ' + text;
} else if(currHeadingLevel == size) {
text = text.substr(currHeadingLevel + 1);
} else {
text = '## ' + text.substr(currHeadingLevel + 1);
}
} else {
text = '#' + text;
if(currHeadingLevel <= 0) {
text = '### ' + text;
} else if(currHeadingLevel == size) {
text = text.substr(currHeadingLevel + 1);
} else {
text = '### ' + text.substr(currHeadingLevel + 1);
}
}
}
cm.replaceRange(text, {
line: i,
ch: 0
@ -399,6 +541,9 @@ function _toggleBlock(editor, type, start_chars, end_chars) {
} else if(type == "italic") {
start = start.replace(/(\*|_)(?![\s\S]*(\*|_))/, "");
end = end.replace(/(\*|_)/, "");
} else if(type == "strikethrough") {
start = start.replace(/(\*\*|~~)(?![\s\S]*(\*\*|~~))/, "");
end = end.replace(/(\*\*|~~)/, "");
}
cm.replaceRange(start + end, {
line: startPoint.line,
@ -408,7 +553,7 @@ function _toggleBlock(editor, type, start_chars, end_chars) {
ch: 99999999999999
});
if(type == "bold") {
if(type == "bold" || type == "strikethrough") {
startPoint.ch -= 2;
endPoint.ch -= 2;
} else if(type == "italic") {
@ -423,6 +568,8 @@ function _toggleBlock(editor, type, start_chars, end_chars) {
} else if(type == "italic") {
text = text.split("*").join("");
text = text.split("_").join("");
} else if(type == "strikethrough") {
text = text.split("~~").join("");
}
cm.replaceSelection(start + text + end);
@ -465,6 +612,12 @@ var toolbarBuiltInButtons = {
className: "fa fa-italic",
title: "Italic (Ctrl+I)",
},
"strikethrough": {
name: "strikethrough",
action: toggleStrikethrough,
className: "fa fa-strikethrough",
title: "Strikethrough",
},
"heading": {
name: "heading",
action: toggleHeadingSmaller,
@ -474,15 +627,33 @@ var toolbarBuiltInButtons = {
"heading-smaller": {
name: "heading-smaller",
action: toggleHeadingSmaller,
className: "fa fa-header",
className: "fa fa-header fa-header-x fa-header-smaller",
title: "Smaller Heading (Ctrl+H)",
},
"heading-bigger": {
name: "heading-bigger",
action: toggleHeadingBigger,
className: "fa fa-lg fa-header",
className: "fa fa-header fa-header-x fa-header-bigger",
title: "Bigger Heading (Shift+Ctrl+H)",
},
"heading-1": {
name: "heading-1",
action: toggleHeading1,
className: "fa fa-header fa-header-x fa-header-1",
title: "Big Heading",
},
"heading-2": {
name: "heading-2",
action: toggleHeading2,
className: "fa fa-header fa-header-x fa-header-2",
title: "Medium Heading",
},
"heading-3": {
name: "heading-3",
action: toggleHeading3,
className: "fa fa-header fa-header-x fa-header-3",
title: "Small Heading",
},
"code": {
name: "code",
action: toggleCodeBlock,
@ -531,6 +702,12 @@ var toolbarBuiltInButtons = {
className: "fa fa-eye",
title: "Toggle Preview (Ctrl+P)",
},
"side-by-side": {
name: "side-by-side",
action: toggleSideBySide,
className: "fa fa-columns",
title: "Toggle Side by Side (F9)",
},
"fullscreen": {
name: "fullscreen",
action: toggleFullScreen,
@ -545,7 +722,7 @@ var toolbarBuiltInButtons = {
}
};
var toolbar = ["bold", "italic", "heading", "|", "quote", "unordered-list", "ordered-list", "|", "link", "image", "|", "preview", "fullscreen", "guide"];
var toolbar = ["bold", "italic", "heading", "|", "quote", "unordered-list", "ordered-list", "|", "link", "image", "|", "preview", "side-by-side", "fullscreen", "guide"];
/**
* Interface of SimpleMDE.
@ -555,6 +732,10 @@ function SimpleMDE(options) {
if(options.element) {
this.element = options.element;
} else if(options.element === null) {
// This means that the element option was specified, but no element was found
console.log("SimpleMDE: Error. No element was found.");
return;
}
if(options.toolbar !== false)
@ -587,7 +768,13 @@ SimpleMDE.toolbar = toolbar;
*/
SimpleMDE.markdown = function(text) {
if(window.marked) {
// use marked as markdown parser
// Update options
if(this.options.singleLineBreaks !== false) {
marked.setOptions({
breaks: true
});
}
return marked(text);
}
};
@ -623,7 +810,10 @@ SimpleMDE.prototype.render = function(el) {
keyMaps["Tab"] = "tabAndIndentContinueMarkdownList";
keyMaps["Shift-Tab"] = "shiftTabAndIndentContinueMarkdownList";
keyMaps["F11"] = function(cm) {
toggleFullScreen(cm);
toggleFullScreen(self);
};
keyMaps["F9"] = function(cm) {
toggleSideBySide(self);
};
keyMaps["Esc"] = function(cm) {
if(cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
@ -660,6 +850,8 @@ SimpleMDE.prototype.render = function(el) {
this.autosave();
}
this.createSidebyside();
this._rendered = this.element;
};
@ -713,15 +905,56 @@ SimpleMDE.prototype.autosave = function() {
}, this.options.autosave.delay || 10000);
};
SimpleMDE.prototype.createSidebyside = function() {
var cm = this.codemirror;
var wrapper = cm.getWrapperElement();
var preview = wrapper.nextSibling;
if(!/editor-preview-side/.test(preview.className)) {
preview = document.createElement('div');
preview.className = 'editor-preview-side';
wrapper.parentNode.insertBefore(preview, wrapper.nextSibling);
}
// Syncs scroll editor -> preview
var cScroll = false;
var pScroll = false;
cm.on('scroll', function(v) {
if(cScroll) {
cScroll = false;
return;
};
pScroll = true;
height = v.getScrollInfo().height - v.getScrollInfo().clientHeight;
ratio = parseFloat(v.getScrollInfo().top) / height;
move = (preview.scrollHeight - preview.clientHeight) * ratio;
preview.scrollTop = move;
});
// Syncs scroll preview -> editor
preview.onscroll = function(v) {
if(pScroll) {
pScroll = false;
return;
};
cScroll = true;
height = preview.scrollHeight - preview.clientHeight;
ratio = parseFloat(preview.scrollTop) / height;
move = (cm.getScrollInfo().height - cm.getScrollInfo().clientHeight) * ratio;
cm.scrollTo(0, move);
};
return true;
}
SimpleMDE.prototype.createToolbar = function(items) {
items = items || this.options.toolbar;
if(!items || items.length === 0) {
return;
}
for(var i = 0; i < items.length; i++) {
if(toolbarBuiltInButtons[items[i]] != undefined){
if(toolbarBuiltInButtons[items[i]] != undefined) {
items[i] = toolbarBuiltInButtons[items[i]];
}
}
@ -774,7 +1007,7 @@ SimpleMDE.prototype.createToolbar = function(items) {
var el = toolbar_data[key];
if(stat[key]) {
el.className += ' active';
} else if(key != "fullscreen") {
} else if(key != "fullscreen" && key != "side-by-side") {
el.className = el.className.replace(/\s*active\s*/g, '');
}
})(key);
@ -848,9 +1081,13 @@ SimpleMDE.prototype.value = function(val) {
*/
SimpleMDE.toggleBold = toggleBold;
SimpleMDE.toggleItalic = toggleItalic;
SimpleMDE.toggleStrikethrough = toggleStrikethrough;
SimpleMDE.toggleBlockquote = toggleBlockquote;
SimpleMDE.toggleHeadingSmaller = toggleHeadingSmaller;
SimpleMDE.toggleHeadingBigger = toggleHeadingBigger;
SimpleMDE.toggleHeading1 = toggleHeading1;
SimpleMDE.toggleHeading2 = toggleHeading2;
SimpleMDE.toggleHeading3 = toggleHeading3;
SimpleMDE.toggleCodeBlock = toggleCodeBlock;
SimpleMDE.toggleUnorderedList = toggleUnorderedList;
SimpleMDE.toggleOrderedList = toggleOrderedList;
@ -860,6 +1097,7 @@ SimpleMDE.drawHorizontalRule = drawHorizontalRule;
SimpleMDE.undo = undo;
SimpleMDE.redo = redo;
SimpleMDE.togglePreview = togglePreview;
SimpleMDE.toggleSideBySide = toggleSideBySide;
SimpleMDE.toggleFullScreen = toggleFullScreen;
/**
@ -871,6 +1109,9 @@ SimpleMDE.prototype.toggleBold = function() {
SimpleMDE.prototype.toggleItalic = function() {
toggleItalic(this);
};
SimpleMDE.prototype.toggleStrikethrough = function() {
toggleStrikethrough(this);
};
SimpleMDE.prototype.toggleBlockquote = function() {
toggleBlockquote(this);
};
@ -880,6 +1121,15 @@ SimpleMDE.prototype.toggleHeadingSmaller = function() {
SimpleMDE.prototype.toggleHeadingBigger = function() {
toggleHeadingBigger(this);
};
SimpleMDE.prototype.toggleHeading1 = function() {
toggleHeading1(this);
};
SimpleMDE.prototype.toggleHeading2 = function() {
toggleHeading2(this);
};
SimpleMDE.prototype.toggleHeading3 = function() {
toggleHeading3(this);
};
SimpleMDE.prototype.toggleCodeBlock = function() {
toggleCodeBlock(this);
};
@ -907,6 +1157,9 @@ SimpleMDE.prototype.redo = function() {
SimpleMDE.prototype.togglePreview = function() {
togglePreview(this);
};
SimpleMDE.prototype.toggleSideBySide = function() {
toggleSideBySide(this);
};
SimpleMDE.prototype.toggleFullScreen = function() {
toggleFullScreen(this);
};

Loading…
Cancel
Save