From 47e40e6b09a01f67f6dd0d4ae993da7cff02bc10 Mon Sep 17 00:00:00 2001 From: MuTsunTsai Date: Sun, 4 Jul 2021 12:13:47 +0800 Subject: [PATCH] Add "trim empty lines" option --- package.json | 6 ++++++ src/extension.js | 3 ++- webview/src/code.js | 11 +++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 5f63377..4c1f870 100644 --- a/package.json +++ b/package.json @@ -121,6 +121,12 @@ ], "default": "save", "description": "The behavior of the shutter button" + }, + "codesnap.trimEmptyLines": { + "scope": "resource", + "type": "boolean", + "default": false, + "description": "Trim off empty lines at the beginning and at the end" } } } diff --git a/src/extension.js b/src/extension.js index 6f31aa4..c779db0 100644 --- a/src/extension.js +++ b/src/extension.js @@ -21,7 +21,8 @@ const getConfig = () => { 'realLineNumbers', 'transparentBackground', 'target', - 'shutterAction' + 'shutterAction', + 'trimEmptyLines' ]); const selection = editor && editor.selection; diff --git a/webview/src/code.js b/webview/src/code.js index ef5ea72..8f1e968 100644 --- a/webview/src/code.js +++ b/webview/src/code.js @@ -44,6 +44,16 @@ const stripInitialIndent = (node) => { initialSpans.forEach((span) => (span.textContent = span.textContent.slice(minIndent))); }; +const isEmptyLine = (node) => node.innerText.match(/^\s*$/); + +const trimEmptyLines = (node, config) => { + while(isEmptyLine(node.firstChild)) { + node.removeChild(node.firstChild); + if(config.realLineNumbers) config.startLine++; + } + while(isEmptyLine(node.lastChild)) node.removeChild(node.lastChild); +} + const getClipboardHtml = (clip) => { const html = clip.getData('text/html'); if (html) return html; @@ -62,5 +72,6 @@ export const pasteCode = (config, clipboard) => { snippetNode.style.lineHeight = code.style.lineHeight; snippetNode.innerHTML = code.innerHTML; stripInitialIndent(snippetNode); + if(config.trimEmptyLines) trimEmptyLines(snippetNode, config); setupLines(snippetNode, config); };