@@ -33,7 +33,8 @@ import { Editor, Level } from '../../types';
3333 * @returns Object containing all command methods
3434 */
3535export function createCommandMethods ( editor : Editor ) {
36- return {
36+ // Create methods object that allows self-reference
37+ const methods = {
3738 wrapText : ( before : string , after = before , defaultText ) => {
3839 const range = editor . state . selection . ranges [ 0 ] ;
3940 const selectedText = editor . state . sliceDoc ( range . from , range . to ) ;
@@ -79,39 +80,39 @@ export function createCommandMethods(editor: Editor) {
7980 } ,
8081
8182 insertBold : ( text ?: string ) => {
82- editor . wrapText ( '**' , '**' , text || 'bold text' ) ;
83+ methods . wrapText ( '**' , '**' , text || 'bold text' ) ;
8384 } ,
8485
8586 insertItalic : ( text ?: string ) => {
86- editor . wrapText ( '*' , '*' , text || 'italic text' ) ;
87+ methods . wrapText ( '*' , '*' , text || 'italic text' ) ;
8788 } ,
8889
8990 insertCode : ( text ?: string ) => {
90- editor . wrapText ( '`' , '`' , text || 'code' ) ;
91+ methods . wrapText ( '`' , '`' , text || 'code' ) ;
9192 } ,
9293
9394 insertStrikethrough : ( text ?: string ) => {
94- editor . wrapText ( '~~' , '~~' , text || 'strikethrough text' ) ;
95+ methods . wrapText ( '~~' , '~~' , text || 'strikethrough text' ) ;
9596 } ,
9697
9798 insertHeading : ( level : Level , text ?: string ) => {
9899 const headingText = '#' . repeat ( level ) ;
99- editor . wrapText ( `${ headingText } ` , '' , text || 'heading' ) ;
100+ methods . wrapText ( `${ headingText } ` , '' , text || 'heading' ) ;
100101 } ,
101102
102103 insertBlockquote : ( text ?: string ) => {
103- editor . wrapText ( '> ' , '' , text || 'quote' ) ;
104+ methods . wrapText ( '> ' , '' , text || 'quote' ) ;
104105 } ,
105106
106107 insertCodeBlock : ( language ?: string , code ?: string ) => {
107108 const lang = language || '' ;
108109 const codeText = code || '' ;
109110 const block = `\`\`\`${ lang } \n${ codeText } \n\`\`\`` ;
110- editor . appendBlock ( block ) ;
111+ methods . appendBlock ( block ) ;
111112 } ,
112113
113114 insertHorizontalRule : ( ) => {
114- editor . appendBlock ( '---' ) ;
115+ methods . appendBlock ( '---' ) ;
115116 } ,
116117
117118 insertOrderedList : ( ) => {
@@ -121,7 +122,7 @@ export function createCommandMethods(editor: Editor) {
121122 if ( / ^ \d + \. \s / . test ( lineText ) ) {
122123 return ;
123124 }
124- editor . replaceLines ( ( lineItem ) => {
125+ methods . replaceLines ( ( lineItem ) => {
125126 if ( lineItem . trim ( ) === '' ) {
126127 return lineItem ;
127128 }
@@ -136,7 +137,7 @@ export function createCommandMethods(editor: Editor) {
136137 if ( / ^ [ - * + ] \s / . test ( lineText ) ) {
137138 return ;
138139 }
139- editor . replaceLines ( ( lineItem ) => {
140+ methods . replaceLines ( ( lineItem ) => {
140141 if ( lineItem . trim ( ) === '' ) {
141142 return lineItem ;
142143 }
@@ -149,11 +150,11 @@ export function createCommandMethods(editor: Editor) {
149150 const line = editor . state . doc . line ( cursor . line ) ;
150151 const lineText = line . text . trim ( ) ;
151152 if ( / ^ \d + \. \s / . test ( lineText ) ) {
152- editor . replaceLines ( ( lineItem ) => {
153+ methods . replaceLines ( ( lineItem ) => {
153154 return lineItem . replace ( / ^ \d + \. \s / , '' ) ;
154155 } ) ;
155156 } else {
156- editor . insertOrderedList ( ) ;
157+ methods . insertOrderedList ( ) ;
157158 }
158159 } ,
159160
@@ -162,22 +163,22 @@ export function createCommandMethods(editor: Editor) {
162163 const line = editor . state . doc . line ( cursor . line ) ;
163164 const lineText = line . text . trim ( ) ;
164165 if ( / ^ [ - * + ] \s / . test ( lineText ) ) {
165- editor . replaceLines ( ( lineItem ) => {
166+ methods . replaceLines ( ( lineItem ) => {
166167 return lineItem . replace ( / ^ [ - * + ] \s / , '' ) ;
167168 } ) ;
168169 } else {
169- editor . insertUnorderedList ( ) ;
170+ methods . insertUnorderedList ( ) ;
170171 }
171172 } ,
172173
173174 insertLink : ( url : string , text ?: string ) => {
174175 const linkText = text || url ;
175- editor . wrapText ( '[' , `](${ url } )` , linkText ) ;
176+ methods . wrapText ( '[' , `](${ url } )` , linkText ) ;
176177 } ,
177178
178179 insertImage : ( url : string , alt ?: string ) => {
179180 const altText = alt || '' ;
180- editor . wrapText ( '` , altText ) ;
181+ methods . wrapText ( '` , altText ) ;
181182 } ,
182183
183184 insertTable : ( rows = 3 , cols = 3 ) => {
@@ -192,11 +193,11 @@ export function createCommandMethods(editor: Editor) {
192193 table . push ( `| ${ '---' . repeat ( cols ) . split ( '' ) . join ( ' | ' ) } |` ) ;
193194 }
194195 }
195- editor . appendBlock ( table . join ( '\n' ) ) ;
196+ methods . appendBlock ( table . join ( '\n' ) ) ;
196197 } ,
197198
198199 indent : ( ) => {
199- editor . replaceLines ( ( line ) => {
200+ methods . replaceLines ( ( line ) => {
200201 if ( line . trim ( ) === '' ) {
201202 return line ;
202203 }
@@ -205,7 +206,7 @@ export function createCommandMethods(editor: Editor) {
205206 } ,
206207
207208 outdent : ( ) => {
208- editor . replaceLines ( ( line ) => {
209+ methods . replaceLines ( ( line ) => {
209210 if ( line . trim ( ) === '' ) {
210211 return line ;
211212 }
@@ -261,4 +262,6 @@ export function createCommandMethods(editor: Editor) {
261262 return / ^ [ - * + ] \s / . test ( lineText ) ;
262263 } ,
263264 } ;
265+
266+ return methods ;
264267}
0 commit comments