2
2
import {
3
3
App ,
4
4
Editor ,
5
- MarkdownView ,
6
5
Plugin ,
7
6
PluginSettingTab ,
8
7
Setting ,
@@ -24,6 +23,7 @@ export default class RemoveNewline extends Plugin {
24
23
async onload ( ) {
25
24
await this . loadSettings ( ) ;
26
25
26
+ // Command / Remove newlines / Selection
27
27
this . addCommand ( {
28
28
id : "remove-newlines-from-selection" ,
29
29
name : "Remove newlines from selection" ,
@@ -41,7 +41,7 @@ export default class RemoveNewline extends Plugin {
41
41
return false ;
42
42
} ,
43
43
} ) ;
44
-
44
+ // Command / Remove newlines / Paste
45
45
this . addCommand ( {
46
46
id : "paste-without-newlines" ,
47
47
name : "Paste without newlines" ,
@@ -54,6 +54,7 @@ export default class RemoveNewline extends Plugin {
54
54
} ,
55
55
} ) ;
56
56
57
+ // Context menu / Remove newlines / Selection
57
58
this . registerEvent (
58
59
this . app . workspace . on ( "editor-menu" , ( menu , editor , view ) => {
59
60
menu . addItem ( ( item ) => {
@@ -67,6 +68,7 @@ export default class RemoveNewline extends Plugin {
67
68
} )
68
69
) ;
69
70
71
+ // Context menu / Remove newlines / Paste
70
72
this . registerEvent (
71
73
this . app . workspace . on ( "editor-menu" , ( menu , editor , view ) => {
72
74
menu . addItem ( ( item ) => {
@@ -80,6 +82,56 @@ export default class RemoveNewline extends Plugin {
80
82
} )
81
83
) ;
82
84
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
+
83
135
this . addSettingTab ( new RemoveNewlineSettingsTab ( this . app , this ) ) ;
84
136
}
85
137
@@ -112,6 +164,17 @@ export default class RemoveNewline extends Plugin {
112
164
return text ;
113
165
}
114
166
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
+
115
178
removeNewlinesFromSelection = ( editor : Editor ) : void => {
116
179
let selection = editor . getSelection ( ) ;
117
180
@@ -127,6 +190,22 @@ export default class RemoveNewline extends Plugin {
127
190
128
191
editor . replaceSelection ( selection ) ;
129
192
}
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
+ }
130
209
}
131
210
132
211
class RemoveNewlineSettingsTab extends PluginSettingTab {
@@ -143,7 +222,7 @@ class RemoveNewlineSettingsTab extends PluginSettingTab {
143
222
containerEl . empty ( ) ;
144
223
145
224
new Setting ( containerEl )
146
- . setName ( "Fix whitespace" )
225
+ . setName ( "Fix whitespace when removing newlines " )
147
226
. setDesc (
148
227
"Remove two or more whitespace characters in a row from the selection after removing the newlines. (Recommended)"
149
228
)
@@ -157,7 +236,7 @@ class RemoveNewlineSettingsTab extends PluginSettingTab {
157
236
) ;
158
237
159
238
new Setting ( containerEl )
160
- . setName ( "Fix hyphenation" )
239
+ . setName ( "Fix hyphenation when removing newlines " )
161
240
. setDesc (
162
241
"If on, removes hyphens from the end of a line and also does not put a space where the newline was."
163
242
)
0 commit comments