From b574ce0e6c1ed9a76a9c7f09c0546baa2afaeb0b Mon Sep 17 00:00:00 2001 From: mattpass Date: Sun, 4 Oct 2020 08:48:04 +0100 Subject: [PATCH] Auto-indent lines you're pasting or moving --- assets/js/icecoder.js | 25 +++++++++++++++++++++++++ editor.php | 1 + 2 files changed, 26 insertions(+) diff --git a/assets/js/icecoder.js b/assets/js/icecoder.js index 63c59a0..5c89694 100644 --- a/assets/js/icecoder.js +++ b/assets/js/icecoder.js @@ -583,6 +583,7 @@ var ICEcoder = { } } + // Reset our array for next time and redo editor stats this.oppTagReplaceData = []; this.setEditorStats(); @@ -661,6 +662,20 @@ var ICEcoder = { this.mouseDownInCM = "editor"; }, + // On paste + cMonPaste: function(thisCM, cMinstance, evt, clipboardData) { + // Get text from clipboard, the number of lines (excluding last) + // and so the startLine and endLine and auto-indent for that range + const text = clipboardData.getData('Text'); + const num = text.split("\n").length - 1; + const startLine = thisCM.getCursor().line; + const endLine = startLine + num; + // Now auto-indent after a 0ms tickover + setTimeout(() => { + parent.ICEcoder.autoIndentLines(startLine, endLine); + }, 0); + }, + // On context menu cMonContextMenu: function(thisCM, cMinstance, evt) { // Set cursor @@ -1109,6 +1124,13 @@ var ICEcoder = { } }, + // Indent line range with smart indenting, falls back to indenting to prev line if no smart indenting for mode + autoIndentLines: function(startLine, endLine) { + for (let i = startLine; i <= endLine; i++) { + this.getcMInstance().indentLine(i, "smart"); + } + }, + // Move current line up/down moveLines: function(dir) { let thisCM, lineStart, lineEnd, swapLineNo, swapLine; @@ -1146,6 +1168,9 @@ var ICEcoder = { ); }) } + + // Auto-indent the lines we're moving (but not the swapped line) + this.autoIndentLines(lineStart.line - ("up" === dir ? 1 : -1), lineEnd.line + ("up" === dir ? -1 : 1)); }, // Highlight specified line diff --git a/editor.php b/editor.php index 10a88dd..c643b5d 100644 --- a/editor.php +++ b/editor.php @@ -269,6 +269,7 @@ function createNewCMInstanceEvents(num, pane) { window['cM'+num+pane].on("inputRead", function(thisCM) {parent.ICEcoder.cMonInputRead(thisCM, 'cM' + num + pane)}); window['cM'+num+pane].on("gutterClick", function(thisCM,line,gutter,evt) {parent.ICEcoder.cMonGutterClick(thisCM, line, gutter, evt, 'cM' + num + pane)}); window['cM'+num+pane].on("mousedown", function(thisCM,evt) {parent.ICEcoder.cMonMouseDown(thisCM, 'cM' + num + pane, evt)}); + window['cM'+num+pane].on("paste", function(thisCM,evt) {parent.ICEcoder.cMonPaste(thisCM, 'cM' + num + pane, evt, (evt.clipboardData || window.clipboardData))}); window['cM'+num+pane].on("contextmenu", function(thisCM,evt) {parent.ICEcoder.cMonContextMenu(thisCM, 'cM' + num + pane, evt)}); window['cM'+num+pane].on("dragover", function(thisCM) {parent.ICEcoder.cMonDragOver(thisCM, event, 'cM' + num + pane)}); window['cM'+num+pane].on("renderLine", function(thisCM, line, element) {parent.ICEcoder.cMonRenderLine(thisCM, 'cM' + num + pane, line, element)});