1- import type { NotePosition } from 'packages/core/src/config/APIConfigs' ;
21import type {
32 ButtonAction ,
43 ButtonConfig ,
4+ ButtonContext ,
55 CommandButtonAction ,
66 CreateNoteButtonAction ,
77 InlineJsButtonAction ,
@@ -66,18 +66,13 @@ export class ButtonActionRunner {
6666 * @param inline whether the button is inline
6767 * @param position the position of the button in the note
6868 */
69- async runButtonAction (
70- config : ButtonConfig ,
71- filePath : string ,
72- inline : boolean ,
73- position : NotePosition | undefined ,
74- ) : Promise < void > {
69+ async runButtonActions ( config : ButtonConfig , filePath : string , context : ButtonContext ) : Promise < void > {
7570 try {
7671 if ( config . action ) {
77- await this . plugin . api . buttonActionRunner . runAction ( config , config . action , filePath , inline , position ) ;
72+ await this . plugin . api . buttonActionRunner . runAction ( config , config . action , filePath , context ) ;
7873 } else if ( config . actions ) {
7974 for ( const action of config . actions ) {
80- await this . plugin . api . buttonActionRunner . runAction ( config , action , filePath , inline , position ) ;
75+ await this . plugin . api . buttonActionRunner . runAction ( config , action , filePath , context ) ;
8176 }
8277 } else {
8378 console . warn ( 'meta-bind | ButtonMDRC >> no action defined' ) ;
@@ -178,14 +173,13 @@ export class ButtonActionRunner {
178173 config : ButtonConfig | undefined ,
179174 action : ButtonAction ,
180175 filePath : string ,
181- inline : boolean ,
182- position : NotePosition | undefined ,
176+ buttonContext : ButtonContext ,
183177 ) : Promise < void > {
184178 if ( action . type === ButtonActionType . COMMAND ) {
185179 await this . runCommandAction ( action ) ;
186180 return ;
187181 } else if ( action . type === ButtonActionType . JS ) {
188- await this . runJSAction ( config , action , filePath ) ;
182+ await this . runJSAction ( config , action , filePath , buttonContext ) ;
189183 return ;
190184 } else if ( action . type === ButtonActionType . OPEN ) {
191185 await this . runOpenAction ( action , filePath ) ;
@@ -209,16 +203,16 @@ export class ButtonActionRunner {
209203 await this . runReplaceInNoteAction ( action , filePath ) ;
210204 return ;
211205 } else if ( action . type === ButtonActionType . REPLACE_SELF ) {
212- await this . runReplaceSelfAction ( action , filePath , inline , position ) ;
206+ await this . runReplaceSelfAction ( action , filePath , buttonContext ) ;
213207 return ;
214208 } else if ( action . type === ButtonActionType . REGEXP_REPLACE_IN_NOTE ) {
215- await this . runRegexpReplaceInNotAction ( action , filePath ) ;
209+ await this . runRegexpReplaceInNoteAction ( action , filePath ) ;
216210 return ;
217211 } else if ( action . type === ButtonActionType . INSERT_INTO_NOTE ) {
218212 await this . runInsertIntoNoteAction ( action , filePath ) ;
219213 return ;
220214 } else if ( action . type === ButtonActionType . INLINE_JS ) {
221- await this . runInlineJsAction ( config , action , filePath ) ;
215+ await this . runInlineJsAction ( config , action , filePath , buttonContext ) ;
222216 return ;
223217 }
224218
@@ -231,7 +225,12 @@ export class ButtonActionRunner {
231225 this . plugin . internal . executeCommandById ( action . command ) ;
232226 }
233227
234- async runJSAction ( config : ButtonConfig | undefined , action : JSButtonAction , filePath : string ) : Promise < void > {
228+ async runJSAction (
229+ config : ButtonConfig | undefined ,
230+ action : JSButtonAction ,
231+ filePath : string ,
232+ buttonContext : ButtonContext ,
233+ ) : Promise < void > {
235234 if ( ! this . plugin . settings . enableJs ) {
236235 throw new MetaBindJsError ( {
237236 errorLevel : ErrorLevel . CRITICAL ,
@@ -243,6 +242,7 @@ export class ButtonActionRunner {
243242 const configOverrides : Record < string , unknown > = {
244243 buttonConfig : structuredClone ( config ) ,
245244 args : structuredClone ( action . args ) ,
245+ buttonContext : structuredClone ( buttonContext ) ,
246246 } ;
247247 const unloadCallback = await this . plugin . internal . jsEngineRunFile ( action . file , filePath , configOverrides ) ;
248248 unloadCallback ( ) ;
@@ -334,28 +334,25 @@ export class ButtonActionRunner {
334334 async runReplaceSelfAction (
335335 action : ReplaceSelfButtonAction ,
336336 filePath : string ,
337- inline : boolean ,
338- position : NotePosition | undefined ,
337+ buttonContext : ButtonContext ,
339338 ) : Promise < void > {
340- if ( inline ) {
339+ if ( buttonContext . isInline ) {
341340 throw new Error ( 'Replace self action not supported for inline buttons' ) ;
342341 }
343342
344- const linePosition = position ?. getPosition ( ) ;
345-
346- if ( linePosition === undefined ) {
343+ if ( buttonContext . position === undefined ) {
347344 throw new Error ( 'Position of the button in the note is unknown' ) ;
348345 }
349346
350- if ( linePosition . lineStart > linePosition . lineEnd ) {
347+ if ( buttonContext . position . lineStart > buttonContext . position . lineEnd ) {
351348 throw new Error ( 'Position of the button in the note is invalid' ) ;
352349 }
353350
354351 const content = await this . plugin . internal . readFilePath ( filePath ) ;
355352
356353 let splitContent = content . split ( '\n' ) ;
357354
358- if ( linePosition . lineStart < 0 || linePosition . lineEnd > splitContent . length + 1 ) {
355+ if ( buttonContext . position . lineStart < 0 || buttonContext . position . lineEnd > splitContent . length + 1 ) {
359356 throw new Error ( 'Position of the button in the note is out of bounds' ) ;
360357 }
361358
@@ -364,15 +361,15 @@ export class ButtonActionRunner {
364361 : action . replacement ;
365362
366363 splitContent = [
367- ...splitContent . slice ( 0 , linePosition . lineStart ) ,
364+ ...splitContent . slice ( 0 , buttonContext . position . lineStart ) ,
368365 replacement ,
369- ...splitContent . slice ( linePosition . lineEnd + 1 ) ,
366+ ...splitContent . slice ( buttonContext . position . lineEnd + 1 ) ,
370367 ] ;
371368
372369 await this . plugin . internal . writeFilePath ( filePath , splitContent . join ( '\n' ) ) ;
373370 }
374371
375- async runRegexpReplaceInNotAction ( action : RegexpReplaceInNoteButtonAction , filePath : string ) : Promise < void > {
372+ async runRegexpReplaceInNoteAction ( action : RegexpReplaceInNoteButtonAction , filePath : string ) : Promise < void > {
376373 if ( action . regexp === '' ) {
377374 throw new Error ( 'Regexp cannot be empty' ) ;
378375 }
@@ -410,6 +407,7 @@ export class ButtonActionRunner {
410407 config : ButtonConfig | undefined ,
411408 action : InlineJsButtonAction ,
412409 filePath : string ,
410+ buttonContext : ButtonContext ,
413411 ) : Promise < void > {
414412 if ( ! this . plugin . settings . enableJs ) {
415413 throw new MetaBindJsError ( {
@@ -421,6 +419,7 @@ export class ButtonActionRunner {
421419
422420 const configOverrides : Record < string , unknown > = {
423421 buttonConfig : structuredClone ( config ) ,
422+ buttonContext : structuredClone ( buttonContext ) ,
424423 } ;
425424 const unloadCallback = await this . plugin . internal . jsEngineRunCode ( action . code , filePath , configOverrides ) ;
426425 unloadCallback ( ) ;
0 commit comments