Skip to content

Commit 0b4f568

Browse files
Add ability to remove blank lines and bump version
1 parent dc3a034 commit 0b4f568

File tree

2 files changed

+85
-6
lines changed

2 files changed

+85
-6
lines changed

main.ts

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import {
33
App,
44
Editor,
5-
MarkdownView,
65
Plugin,
76
PluginSettingTab,
87
Setting,
@@ -24,6 +23,7 @@ export default class RemoveNewline extends Plugin {
2423
async onload() {
2524
await this.loadSettings();
2625

26+
// Command / Remove newlines / Selection
2727
this.addCommand({
2828
id: "remove-newlines-from-selection",
2929
name: "Remove newlines from selection",
@@ -41,7 +41,7 @@ export default class RemoveNewline extends Plugin {
4141
return false;
4242
},
4343
});
44-
44+
// Command / Remove newlines / Paste
4545
this.addCommand({
4646
id: "paste-without-newlines",
4747
name: "Paste without newlines",
@@ -54,6 +54,7 @@ export default class RemoveNewline extends Plugin {
5454
},
5555
});
5656

57+
// Context menu / Remove newlines / Selection
5758
this.registerEvent(
5859
this.app.workspace.on("editor-menu", (menu, editor, view) => {
5960
menu.addItem((item) => {
@@ -67,6 +68,7 @@ export default class RemoveNewline extends Plugin {
6768
})
6869
);
6970

71+
// Context menu / Remove newlines / Paste
7072
this.registerEvent(
7173
this.app.workspace.on("editor-menu", (menu, editor, view) => {
7274
menu.addItem((item) => {
@@ -80,6 +82,56 @@ export default class RemoveNewline extends Plugin {
8082
})
8183
);
8284

85+
// Context menu / Remove Blank Lines / Selection
86+
this.registerEvent(
87+
this.app.workspace.on("editor-menu", (menu, editor, view) => {
88+
menu.addItem((item) => {
89+
item.setTitle("Remove blank lines from selection")
90+
.setIcon("square-bottom-dashed-scissors")
91+
.setDisabled(!editor.somethingSelected())
92+
.onClick(() => this.removeBlankLinesFromSelection(editor));
93+
});
94+
})
95+
);
96+
97+
// Context menu / Remove Blank Lines / Paste
98+
this.registerEvent(
99+
this.app.workspace.on("editor-menu", (menu, editor, view) => {
100+
menu.addItem((item) => {
101+
item.setTitle("Paste without blank lines")
102+
.setIcon("clipboard-paste")
103+
//.setDisabled(this.clipboardHasText())
104+
.onClick(() => {
105+
void this.pasteWithoutBlankLines(editor);
106+
});
107+
});
108+
})
109+
);
110+
111+
// Command / Remove Blank Lines / Paste
112+
this.addCommand({
113+
id: "paste-without-blank-lines",
114+
name: "Paste without blank lines",
115+
editorCallback: (editor) => {
116+
void this.pasteWithoutBlankLines(editor);
117+
},
118+
});
119+
120+
// Command / Remove Blank Lines / Selection
121+
this.addCommand({
122+
id: "remove-blank-lines-from-selection",
123+
name: "Remove blank lines from selection",
124+
editorCheckCallback: (checking, editor, ctx) => {
125+
if (editor.somethingSelected()) {
126+
if (!checking) {
127+
this.removeBlankLinesFromSelection(editor);
128+
}
129+
return true;
130+
}
131+
return false;
132+
},
133+
});
134+
83135
this.addSettingTab(new RemoveNewlineSettingsTab(this.app, this));
84136
}
85137

@@ -112,6 +164,17 @@ export default class RemoveNewline extends Plugin {
112164
return text;
113165
}
114166

167+
removeBlankLines = (text: string): string => {
168+
169+
text = text.replace(/(\r\n|\r|\n){2,}/g, "\r\n");
170+
171+
// if (this.settings.fixWhitespace) {
172+
// text = text.replace(/\s{2,}/g, " ");
173+
// }
174+
175+
return text;
176+
}
177+
115178
removeNewlinesFromSelection = (editor: Editor): void => {
116179
let selection = editor.getSelection();
117180

@@ -127,6 +190,22 @@ export default class RemoveNewline extends Plugin {
127190

128191
editor.replaceSelection(selection);
129192
}
193+
194+
removeBlankLinesFromSelection = (editor: Editor): void => {
195+
let selection = editor.getSelection();
196+
197+
selection = this.removeBlankLines(selection);
198+
199+
editor.replaceSelection(selection);
200+
}
201+
202+
pasteWithoutBlankLines = async (editor: Editor): Promise<void> => {
203+
let selection = await navigator.clipboard.readText();
204+
205+
selection = this.removeBlankLines(selection);
206+
207+
editor.replaceSelection(selection);
208+
}
130209
}
131210

132211
class RemoveNewlineSettingsTab extends PluginSettingTab {
@@ -143,7 +222,7 @@ class RemoveNewlineSettingsTab extends PluginSettingTab {
143222
containerEl.empty();
144223

145224
new Setting(containerEl)
146-
.setName("Fix whitespace")
225+
.setName("Fix whitespace when removing newlines")
147226
.setDesc(
148227
"Remove two or more whitespace characters in a row from the selection after removing the newlines. (Recommended)"
149228
)
@@ -157,7 +236,7 @@ class RemoveNewlineSettingsTab extends PluginSettingTab {
157236
);
158237

159238
new Setting(containerEl)
160-
.setName("Fix hyphenation")
239+
.setName("Fix hyphenation when removing newlines")
161240
.setDesc(
162241
"If on, removes hyphens from the end of a line and also does not put a space where the newline was."
163242
)

manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"id": "remove-newlines",
33
"name": "Remove Newlines",
4-
"version": "1.0.4",
4+
"version": "1.0.5",
55
"minAppVersion": "0.15.0",
6-
"description": "Remove newlines from text selections and also paste content from the clipboard without newlines.",
6+
"description": "Remove newlines or blank lines from selected or pasted text.",
77
"author": "Elias Jaffe",
88
"authorUrl": "https://github.com/HandcartCactus",
99
"isDesktopOnly": false

0 commit comments

Comments
 (0)