-
-
Notifications
You must be signed in to change notification settings - Fork 944
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add toolbar for basic markdown formatting
- Loading branch information
1 parent
faeec22
commit 491c831
Showing
13 changed files
with
907 additions
and
19 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
function toggleFormat(type){ | ||
var startPoint = cm.getCursor("start"); | ||
var endPoint = cm.getCursor("end"); | ||
if(type == "bold") { | ||
var start_chars = "**"; | ||
} else if(type == "italic") { | ||
var start_chars = "_"; | ||
} else if(type == "strikethrough") { | ||
var start_chars = "~~"; | ||
} | ||
text = cm.getSelection(); | ||
var start = start_chars; | ||
var end = start_chars; | ||
text = text.split("**").join(""); | ||
text = text.split("__").join(""); | ||
if(type == "bold") { | ||
text = text.split("**").join(""); | ||
text = text.split("__").join(""); | ||
} 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); | ||
startPoint.ch += start_chars.length; | ||
endPoint.ch = startPoint.ch + text.length; | ||
cm.setSelection(startPoint, endPoint); | ||
cm.focus(); | ||
} | ||
|
||
function getState(cm, pos) { | ||
pos = pos || cm.getCursor("start"); | ||
var stat = cm.getTokenAt(pos); | ||
if(!stat.type) return {}; | ||
|
||
var types = stat.type.split(" "); | ||
|
||
var ret = {}, | ||
data, text; | ||
for(var i = 0; i < types.length; i++) { | ||
data = types[i]; | ||
if(data === "strong") { | ||
ret.bold = true; | ||
} else if(data === "variable-2") { | ||
text = cm.getLine(pos.line); | ||
if(/^\s*\d+\.\s/.test(text)) { | ||
ret["ordered-list"] = true; | ||
} else { | ||
ret["unordered-list"] = true; | ||
} | ||
} else if(data === "atom") { | ||
ret.quote = true; | ||
} else if(data === "em") { | ||
ret.italic = true; | ||
} else if(data === "quote") { | ||
ret.quote = true; | ||
} else if(data === "strikethrough") { | ||
ret.strikethrough = true; | ||
} else if(data === "comment") { | ||
ret.code = true; | ||
} else if(data === "link") { | ||
ret.link = true; | ||
} else if(data === "tag") { | ||
ret.image = true; | ||
} else if(data.match(/^header(\-[1-6])?$/)) { | ||
ret[data.replace("header", "heading")] = true; | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
function toggleBlockquote() { | ||
_toggleLine(cm, "quote"); | ||
} | ||
|
||
function toggleUnorderedList(editor) { | ||
_toggleLine(cm, "unordered-list"); | ||
} | ||
|
||
function toggleOrderedList(editor) { | ||
_toggleLine(cm, "ordered-list"); | ||
} | ||
|
||
function _toggleLine(cm, name) { | ||
if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className)) | ||
return; | ||
|
||
var stat = getState(cm); | ||
var startPoint = cm.getCursor("start"); | ||
var endPoint = cm.getCursor("end"); | ||
var repl = { | ||
"quote": /^(\s*)\>\s+/, | ||
"unordered-list": /^(\s*)(\*|\-|\+)\s+/, | ||
"ordered-list": /^(\s*)\d+\.\s+/ | ||
}; | ||
var map = { | ||
"quote": "> ", | ||
"unordered-list": "* ", | ||
"ordered-list": "1. " | ||
}; | ||
for(var i = startPoint.line; i <= endPoint.line; i++) { | ||
(function(i) { | ||
var text = cm.getLine(i); | ||
if(stat[name]) { | ||
text = text.replace(repl[name], "$1"); | ||
} else { | ||
text = map[name] + text; | ||
} | ||
cm.replaceRange(text, { | ||
line: i, | ||
ch: 0 | ||
}, { | ||
line: i, | ||
ch: 99999999999999 | ||
}); | ||
})(i); | ||
} | ||
cm.focus(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,33 @@ | ||
var clkPref = function (opt) { | ||
currentValue = opt.value; | ||
if ( currentValue=='preview' ) { | ||
document.getElementById("htmlPreview").style.display = "none"; | ||
document.getElementById("markdown").style.display = "block"; | ||
} else if ( currentValue=='html' ) { | ||
document.getElementById("markdown").style.display = "none"; | ||
document.getElementById("htmlPreview").style.display = "block"; | ||
} | ||
currentValue = opt.value; | ||
if ( currentValue=='preview' ) { | ||
document.getElementById("htmlPreview").style.display = "none"; | ||
document.getElementById("markdown").style.display = "block"; | ||
} else if ( currentValue=='html' ) { | ||
document.getElementById("markdown").style.display = "none"; | ||
document.getElementById("htmlPreview").style.display = "block"; | ||
} | ||
} | ||
|
||
var changeTheme = function (opt) { | ||
currentValueTheme = opt.value; | ||
if ( currentValueTheme=='light' ) { | ||
cm.setOption("theme", "default"); | ||
} else if ( currentValueTheme=='dark' ) { | ||
cm.setOption("theme", "base16-dark"); | ||
} | ||
currentValueTheme = opt.value; | ||
if ( currentValueTheme=='light' ) { | ||
cm.setOption("theme", "default"); | ||
} else if ( currentValueTheme=='dark' ) { | ||
cm.setOption("theme", "base16-dark"); | ||
} | ||
} | ||
|
||
function showToolBar(){ | ||
if(document.getElementById("toolbarArea").style.display == "block"){ | ||
document.getElementById("angleToolBar").className = ""; | ||
document.getElementById("angleToolBar").className = "fa fa-angle-double-right"; | ||
document.getElementById("toolbarArea").style.display = "none"; | ||
document.getElementById("editArea").style.paddingTop = "24px"; | ||
}else{ | ||
document.getElementById("angleToolBar").className = ""; | ||
document.getElementById("angleToolBar").className = "fa fa-angle-double-down"; | ||
document.getElementById("toolbarArea").style.display = "block"; | ||
document.getElementById("editArea").style.paddingTop = "53px"; | ||
} | ||
} |