4
4
Command ,
5
5
setIcon ,
6
6
debounce ,
7
+ Editor ,
7
8
MarkdownView ,
8
9
SliderComponent ,
9
10
ToggleComponent ,
@@ -123,46 +124,67 @@ export default class cMenuPlugin extends Plugin {
123
124
124
125
generateCommands ( ) {
125
126
const applyCommand = (
126
- prefix : string ,
127
- selectedText : string ,
128
- suffix : string
127
+ command : commandPlot ,
128
+ editor : Editor ,
129
129
) => {
130
- suffix = suffix || prefix ;
131
- return `${ prefix } ${ selectedText } ${ suffix } ` ;
130
+ const selectedText = editor . getSelection ( ) ;
131
+ const curserStart = editor . getCursor ( "from" ) ;
132
+ const curserEnd = editor . getCursor ( "to" ) ;
133
+ const prefix = command . prefix ;
134
+ const suffix = command . suffix || prefix ;
135
+ const setCursor = ( mode : number ) => {
136
+ editor . setCursor ( curserStart . line + command . line * mode , curserEnd . ch + command . char * mode ) ;
137
+ } ;
138
+ const preStart = { line : curserStart . line - command . line , ch : curserStart . ch - prefix . length } ;
139
+ const pre = editor . getRange ( preStart , curserStart ) ;
140
+
141
+ if ( pre == prefix . trimStart ( ) ) {
142
+ const sufEnd = { line : curserStart . line + command . line , ch : curserEnd . ch + suffix . length } ;
143
+ const suf = editor . getRange ( curserEnd , sufEnd ) ;
144
+ if ( suf == suffix . trimEnd ( ) ) {
145
+ editor . replaceRange ( selectedText , preStart , sufEnd ) ; // codeblock leave blank lines
146
+ return setCursor ( - 1 ) ;
147
+ }
148
+ }
149
+ editor . replaceSelection ( `${ prefix } ${ selectedText } ${ suffix } ` ) ;
150
+ return setCursor ( 1 ) ;
151
+ } ;
152
+
153
+ type commandPlot = {
154
+ char : number ;
155
+ line : number ;
156
+ prefix : string ;
157
+ suffix : string ;
132
158
} ;
133
159
134
160
type commandsPlot = {
135
- [ key : string ] : {
136
- replacement : ( selectedText : string ) => string ;
137
- char : number ;
138
- line : number ;
139
- } ;
161
+ [ key : string ] : commandPlot ;
140
162
} ;
141
163
142
164
const commandsMap : commandsPlot = {
143
165
underline : {
144
- replacement : ( selectedText ) =>
145
- applyCommand ( "<u>" , selectedText , "</u>" ) ,
146
166
char : 3 ,
147
167
line : 0 ,
168
+ prefix : "<u>" ,
169
+ suffix : "</u>" ,
148
170
} ,
149
171
superscript : {
150
- replacement : ( selectedText ) =>
151
- applyCommand ( "<sup>" , selectedText , "</sup>" ) ,
152
172
char : 5 ,
153
173
line : 0 ,
174
+ prefix : "<sup>" ,
175
+ suffix : "</sup>" ,
154
176
} ,
155
177
subscript : {
156
- replacement : ( selectedText ) =>
157
- applyCommand ( "<sub>" , selectedText , "</sub>" ) ,
158
178
char : 5 ,
159
179
line : 0 ,
180
+ prefix : "<sub>" ,
181
+ suffix : "</sub>" ,
160
182
} ,
161
183
codeblock : {
162
- replacement : ( selectedText ) =>
163
- applyCommand ( "\n```\n" , selectedText , "\n```\n" ) ,
164
184
char : 5 ,
165
185
line : 1 ,
186
+ prefix : "\n```\n" ,
187
+ suffix : "\n```\n" ,
166
188
} ,
167
189
} ;
168
190
@@ -177,25 +199,7 @@ export default class cMenuPlugin extends Plugin {
177
199
if ( activeLeaf ) {
178
200
const view = activeLeaf ;
179
201
const editor = view . editor ;
180
- const selection = editor . getSelection ( ) ;
181
- const curserStart = editor . getCursor ( "from" ) ;
182
- const curserEnd = editor . getCursor ( "to" ) ;
183
- if ( selection ) {
184
- editor . replaceSelection ( commandsMap [ type ] . replacement ( selection ) ) ;
185
- editor . setCursor (
186
- curserStart . line + commandsMap [ type ] . line ,
187
- curserEnd . ch + commandsMap [ type ] . char
188
- ) ;
189
- } else {
190
- editor . replaceRange (
191
- commandsMap [ type ] . replacement ( selection ) ,
192
- curserStart
193
- ) ;
194
- editor . setCursor (
195
- curserStart . line + commandsMap [ type ] . line ,
196
- curserEnd . ch + commandsMap [ type ] . char
197
- ) ;
198
- }
202
+ applyCommand ( commandsMap [ type ] , editor ) ;
199
203
await wait ( 10 ) ;
200
204
//@ts -ignore
201
205
this . app . commands . executeCommandById ( "editor:focus" ) ;
0 commit comments