Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "trim empty lines" option #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const getConfig = () => {
'realLineNumbers',
'transparentBackground',
'target',
'shutterAction'
'shutterAction',
'trimEmptyLines'
]);

const selection = editor && editor.selection;
Expand Down
11 changes: 11 additions & 0 deletions webview/src/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
};