Add indent to editor

This commit is contained in:
cuom1999 2022-10-27 20:32:31 -05:00
parent 6b83a1ff0d
commit edfee1f643
3 changed files with 108 additions and 60 deletions

View file

@ -86,9 +86,7 @@ a {
input, textarea { input, textarea {
min-width: 100%; min-width: 100%;
max-width: 100%; max-width: 100%;
font-size: 1em;
// Hack for 4k on Chrome
font-size: $base_font_size;
} }
} }
@ -134,6 +132,3 @@ a {
.comment-body { .comment-body {
word-wrap: break-word; word-wrap: break-word;
} }
.highlight {
background: #fff897;
}

View file

@ -154,6 +154,14 @@
width: 100%; width: 100%;
} }
textarea,
pre {
-moz-tab-size : 4;
-o-tab-size : 4;
tab-size : 4;
}
@media (min-width: 700px) { @media (min-width: 700px) {
#common-content { #common-content {
display: flex; display: flex;

View file

@ -1250,11 +1250,16 @@
keyEvent = "keypress"; keyEvent = "keypress";
} }
function doCommand(command) {
var fakeButton = {};
fakeButton.textOp = bindCommand(command);
doClick(fakeButton);
}
util.addEvent(inputBox, keyEvent, function (key) { util.addEvent(inputBox, keyEvent, function (key) {
// Check to see if we have a button key and, if so execute the callback. // Check to see if we have a button key and, if so execute the callback.
if ((key.ctrlKey || key.metaKey) && !key.altKey && !key.shiftKey) { if ((key.ctrlKey || key.metaKey) && !key.altKey && !key.shiftKey) {
var keyCode = key.charCode || key.keyCode; var keyCode = key.charCode || key.keyCode;
var keyCodeStr = String.fromCharCode(keyCode).toLowerCase(); var keyCodeStr = String.fromCharCode(keyCode).toLowerCase();
@ -1306,11 +1311,18 @@
doClick(buttons.undo); doClick(buttons.undo);
} }
break; break;
}
switch (keyCode) {
case 221:
doCommand("doIndent");
break;
case 219:
doCommand("doReverseIndent");
break;
default: default:
return; return;
} }
if (key.preventDefault) { if (key.preventDefault) {
key.preventDefault(); key.preventDefault();
} }
@ -1321,15 +1333,22 @@
} }
}); });
// Auto-indent on shift-enter util.addEvent(inputBox, "keydown", function (key) {
util.addEvent(inputBox, "keyup", function (key) {
if (key.shiftKey && !key.ctrlKey && !key.metaKey) {
var keyCode = key.charCode || key.keyCode; var keyCode = key.charCode || key.keyCode;
// Character 13 is Enter // Character 13 is Enter
if (keyCode === 13) { if (keyCode === 13) {
var fakeButton = {}; doCommand("doAutoindent");
fakeButton.textOp = bindCommand("doAutoindent"); key.preventDefault();
doClick(fakeButton); }
// 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. // at the current indent level.
commandProto.doAutoindent = function (chunk, postProcessing) { commandProto.doAutoindent = function (chunk, postProcessing) {
var commandMgr = this, var commandMgr = this,
fakeSelection = false; fakeSelection = false;
@ -1930,26 +1976,32 @@
fakeSelection = true; 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) { if (commandMgr.doList) {
chunk.before += "\n";
commandMgr.doList(chunk); 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); commandMgr.doBlockquote(chunk);
} }
} else {
if (/(\n|^)(\t|[ ]{4,}).*\n$/.test(chunk.before)) { var currentLine = chunk.before.split("\n").pop();
if (commandMgr.doCode) { var indent = currentLine.match(/^\s*/)[0];
commandMgr.doCode(chunk); chunk.before += "\n" + indent;
}
} }
if (fakeSelection) { if (fakeSelection) {
chunk.after = chunk.selection + chunk.after; chunk.after = chunk.selection + chunk.after;
chunk.selection = "";
} }
chunk.selection = "";
}; };
commandProto.doBlockquote = function (chunk, postProcessing) { commandProto.doBlockquote = function (chunk, postProcessing) {
@ -2205,16 +2257,12 @@
return itemText; return itemText;
}; };
chunk.findTags(/(\n|^)*[ ]{0,3}([*+-]|\d+[.])\s+/, null); chunk.findTags(/(\n|^)*[ ]{0,3}([*+-]|\d+[.])\s+/, null);
if (chunk.before && !/\n$/.test(chunk.before) && !/^\n/.test(chunk.startTag)) { if (chunk.before && !/\n$/.test(chunk.before) && !/^\n/.test(chunk.startTag)) {
chunk.before += chunk.startTag; chunk.before += chunk.startTag;
chunk.startTag = ""; chunk.startTag = "";
} }
if (chunk.startTag) { if (chunk.startTag) {
var hasDigits = /\d+[.]/.test(chunk.startTag); var hasDigits = /\d+[.]/.test(chunk.startTag);
chunk.startTag = ""; chunk.startTag = "";
chunk.selection = chunk.selection.replace(/\n[ ]{4}/g, "\n"); chunk.selection = chunk.selection.replace(/\n[ ]{4}/g, "\n");
@ -2231,7 +2279,6 @@
} }
var nLinesUp = 1; var nLinesUp = 1;
chunk.before = chunk.before.replace(previousItemsRegex, chunk.before = chunk.before.replace(previousItemsRegex,
function (itemText) { function (itemText) {
if (/^\s*([*+-])/.test(itemText)) { if (/^\s*([*+-])/.test(itemText)) {
@ -2254,14 +2301,12 @@
nLinesDown = /[^\n]\n\n[^\n]/.test(itemText) ? 1 : 0; nLinesDown = /[^\n]\n\n[^\n]/.test(itemText) ? 1 : 0;
return getPrefixedItem(itemText); return getPrefixedItem(itemText);
}); });
chunk.trimWhitespace(true); chunk.trimWhitespace(true);
chunk.skipLines(nLinesUp, nLinesDown, true); chunk.skipLines(nLinesUp, nLinesDown, true);
chunk.startTag = prefix; chunk.startTag = prefix;
var spaces = prefix.replace(/./g, " "); var spaces = prefix.replace(/./g, " ");
this.wrap(chunk, SETTINGS.lineLength - spaces.length); this.wrap(chunk, SETTINGS.lineLength - spaces.length);
chunk.selection = chunk.selection.replace(/\n/g, "\n" + spaces); chunk.selection = chunk.selection.replace(/\n/g, "\n" + spaces);
}; };
commandProto.doHeading = function (chunk, postProcessing) { commandProto.doHeading = function (chunk, postProcessing) {