Add indent to editor
This commit is contained in:
parent
6b83a1ff0d
commit
edfee1f643
3 changed files with 108 additions and 60 deletions
|
@ -86,9 +86,7 @@ a {
|
|||
input, textarea {
|
||||
min-width: 100%;
|
||||
max-width: 100%;
|
||||
|
||||
// Hack for 4k on Chrome
|
||||
font-size: $base_font_size;
|
||||
font-size: 1em;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -134,6 +132,3 @@ a {
|
|||
.comment-body {
|
||||
word-wrap: break-word;
|
||||
}
|
||||
.highlight {
|
||||
background: #fff897;
|
||||
}
|
|
@ -154,6 +154,14 @@
|
|||
width: 100%;
|
||||
}
|
||||
|
||||
textarea,
|
||||
pre {
|
||||
-moz-tab-size : 4;
|
||||
-o-tab-size : 4;
|
||||
tab-size : 4;
|
||||
}
|
||||
|
||||
|
||||
@media (min-width: 700px) {
|
||||
#common-content {
|
||||
display: flex;
|
||||
|
|
|
@ -1250,11 +1250,16 @@
|
|||
keyEvent = "keypress";
|
||||
}
|
||||
|
||||
function doCommand(command) {
|
||||
var fakeButton = {};
|
||||
fakeButton.textOp = bindCommand(command);
|
||||
doClick(fakeButton);
|
||||
}
|
||||
|
||||
util.addEvent(inputBox, keyEvent, function (key) {
|
||||
|
||||
// Check to see if we have a button key and, if so execute the callback.
|
||||
if ((key.ctrlKey || key.metaKey) && !key.altKey && !key.shiftKey) {
|
||||
|
||||
var keyCode = key.charCode || key.keyCode;
|
||||
var keyCodeStr = String.fromCharCode(keyCode).toLowerCase();
|
||||
|
||||
|
@ -1306,11 +1311,18 @@
|
|||
doClick(buttons.undo);
|
||||
}
|
||||
break;
|
||||
}
|
||||
switch (keyCode) {
|
||||
case 221:
|
||||
doCommand("doIndent");
|
||||
break;
|
||||
case 219:
|
||||
doCommand("doReverseIndent");
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (key.preventDefault) {
|
||||
key.preventDefault();
|
||||
}
|
||||
|
@ -1321,15 +1333,22 @@
|
|||
}
|
||||
});
|
||||
|
||||
// Auto-indent on shift-enter
|
||||
util.addEvent(inputBox, "keyup", function (key) {
|
||||
if (key.shiftKey && !key.ctrlKey && !key.metaKey) {
|
||||
util.addEvent(inputBox, "keydown", function (key) {
|
||||
var keyCode = key.charCode || key.keyCode;
|
||||
// Character 13 is Enter
|
||||
if (keyCode === 13) {
|
||||
var fakeButton = {};
|
||||
fakeButton.textOp = bindCommand("doAutoindent");
|
||||
doClick(fakeButton);
|
||||
doCommand("doAutoindent");
|
||||
key.preventDefault();
|
||||
}
|
||||
|
||||
// Tab
|
||||
if (keyCode === 9) {
|
||||
key.preventDefault();
|
||||
if (key.shiftKey) {
|
||||
doCommand("doReverseIndent");
|
||||
}
|
||||
else {
|
||||
doCommand("doIndent");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -1907,10 +1926,37 @@
|
|||
}
|
||||
};
|
||||
|
||||
// When making a list, hitting shift-enter will put your cursor on the next line
|
||||
commandProto.doIndent = function(chunk, postProcessing) {
|
||||
if (!chunk.selection) {
|
||||
chunk.before += " ";
|
||||
}
|
||||
else {
|
||||
var selectedLines = chunk.selection.split("\n");
|
||||
for (var i = 1; i < selectedLines.length; i++) {
|
||||
selectedLines[i] = " " + selectedLines[i];
|
||||
}
|
||||
chunk.selection = selectedLines.join("\n");
|
||||
|
||||
// Indent the current line
|
||||
chunk.before = chunk.before.replace(/(^|\n)([^\^\n]*)$/, "$1 $2")
|
||||
}
|
||||
}
|
||||
|
||||
commandProto.doReverseIndent = function(chunk, postProcessing) {
|
||||
if (chunk.selection) {
|
||||
var selectedLines = chunk.selection.split("\n");
|
||||
for (var i = 1; i < selectedLines.length; i++) {
|
||||
selectedLines[i] = selectedLines[i].replace(/^([ ]{0,4})(.*)$/, "$2");
|
||||
}
|
||||
chunk.selection = selectedLines.join("\n");
|
||||
}
|
||||
// Reindent the current line
|
||||
chunk.before = chunk.before.replace(/(^|\n)([ ]{0,4})(.*)$/, "$1$3");
|
||||
}
|
||||
|
||||
// Hitting enter will put your cursor on the next line
|
||||
// at the current indent level.
|
||||
commandProto.doAutoindent = function (chunk, postProcessing) {
|
||||
|
||||
var commandMgr = this,
|
||||
fakeSelection = false;
|
||||
|
||||
|
@ -1930,26 +1976,32 @@
|
|||
fakeSelection = true;
|
||||
}
|
||||
|
||||
if (/(\n|^)[ ]{0,3}([*+-]|\d+[.])[ \t]+.*\n$/.test(chunk.before)) {
|
||||
if (/(\n|^)[ ]{0,3}([*+-]|\d+[.])[ \t]+.*$/.test(chunk.before)) {
|
||||
var oldChunk = {...chunk};
|
||||
if (commandMgr.doList) {
|
||||
chunk.before += "\n";
|
||||
commandMgr.doList(chunk);
|
||||
if (oldChunk.before == chunk.before + chunk.startTag) {
|
||||
chunk.startTag = "";
|
||||
chunk.selection = "";
|
||||
chunk.before += "\n";
|
||||
}
|
||||
}
|
||||
if (/(\n|^)[ ]{0,3}>[ \t]+.*\n$/.test(chunk.before)) {
|
||||
if (commandMgr.doBlockquote) {
|
||||
}
|
||||
else if (/(\n|^)[ ]{0,3}>[ \t]+.*$/.test(chunk.before)) {
|
||||
chunk.before += "\n";
|
||||
commandMgr.doBlockquote(chunk);
|
||||
}
|
||||
}
|
||||
if (/(\n|^)(\t|[ ]{4,}).*\n$/.test(chunk.before)) {
|
||||
if (commandMgr.doCode) {
|
||||
commandMgr.doCode(chunk);
|
||||
}
|
||||
else {
|
||||
var currentLine = chunk.before.split("\n").pop();
|
||||
var indent = currentLine.match(/^\s*/)[0];
|
||||
chunk.before += "\n" + indent;
|
||||
}
|
||||
|
||||
if (fakeSelection) {
|
||||
chunk.after = chunk.selection + chunk.after;
|
||||
chunk.selection = "";
|
||||
}
|
||||
chunk.selection = "";
|
||||
};
|
||||
|
||||
commandProto.doBlockquote = function (chunk, postProcessing) {
|
||||
|
@ -2205,16 +2257,12 @@
|
|||
|
||||
return itemText;
|
||||
};
|
||||
|
||||
chunk.findTags(/(\n|^)*[ ]{0,3}([*+-]|\d+[.])\s+/, null);
|
||||
|
||||
if (chunk.before && !/\n$/.test(chunk.before) && !/^\n/.test(chunk.startTag)) {
|
||||
chunk.before += chunk.startTag;
|
||||
chunk.startTag = "";
|
||||
}
|
||||
|
||||
if (chunk.startTag) {
|
||||
|
||||
var hasDigits = /\d+[.]/.test(chunk.startTag);
|
||||
chunk.startTag = "";
|
||||
chunk.selection = chunk.selection.replace(/\n[ ]{4}/g, "\n");
|
||||
|
@ -2231,7 +2279,6 @@
|
|||
}
|
||||
|
||||
var nLinesUp = 1;
|
||||
|
||||
chunk.before = chunk.before.replace(previousItemsRegex,
|
||||
function (itemText) {
|
||||
if (/^\s*([*+-])/.test(itemText)) {
|
||||
|
@ -2254,14 +2301,12 @@
|
|||
nLinesDown = /[^\n]\n\n[^\n]/.test(itemText) ? 1 : 0;
|
||||
return getPrefixedItem(itemText);
|
||||
});
|
||||
|
||||
chunk.trimWhitespace(true);
|
||||
chunk.skipLines(nLinesUp, nLinesDown, true);
|
||||
chunk.startTag = prefix;
|
||||
var spaces = prefix.replace(/./g, " ");
|
||||
this.wrap(chunk, SETTINGS.lineLength - spaces.length);
|
||||
chunk.selection = chunk.selection.replace(/\n/g, "\n" + spaces);
|
||||
|
||||
};
|
||||
|
||||
commandProto.doHeading = function (chunk, postProcessing) {
|
||||
|
|
Loading…
Reference in a new issue