add run groups

This commit is contained in:
Cleon 2023-01-07 16:31:28 +13:00
parent 29a555fd68
commit 03e713f8ee
2 changed files with 72 additions and 25 deletions

View file

@ -13,7 +13,12 @@ import { MarkerRunContextImplemention } from "./rci-markers";
export class Compiler{ export class Compiler{
public compile( logger: RunLogger, editor: Editor, view: MarkdownView ) : (() => void) | null { public compile(
logger: RunLogger,
editor: Editor,
view: MarkdownView,
runGroupTag?: string
) : (() => void) | null {
const fileCache = app.metadataCache.getFileCache(view.file); const fileCache = app.metadataCache.getFileCache(view.file);
@ -22,11 +27,14 @@ export class Compiler{
} }
// build context // build context
const context = this.build_run_context(logger, editor, view.file, fileCache ); const context = this.build_run_context( logger, editor, view.file, fileCache, runGroupTag );
if (context == null){ if (context == null){
return function() { return function() {
new Notice( 'No JavaScript blocks were found marked with "meld-build"' ); new Notice(
'No JavaScript blocks were found marked with "meld-build"'
+ ( runGroupTag !=undefined ? ` (Tag group=${runGroupTag})` : '' )
);
}; };
} }
@ -95,6 +103,7 @@ export class Compiler{
editor: Editor, editor: Editor,
file: TFile, file: TFile,
fileCache:CachedMetadata, fileCache:CachedMetadata,
runGroupTag?: string
) : TRunContext | null { ) : TRunContext | null {
const pzr = new Parser(); const pzr = new Parser();
@ -107,6 +116,7 @@ export class Compiler{
// runable code blocks // runable code blocks
const runableCodeBlocks = allCodeBlocks const runableCodeBlocks = allCodeBlocks
.filter( cb => CodeBlockInfoHelper.isRunable( cb.info ) ) .filter( cb => CodeBlockInfoHelper.isRunable( cb.info ) )
.filter( cb => runGroupTag == undefined || cb.info.params.contains( runGroupTag ) )
; ;
if ( runableCodeBlocks.length == 0 ){ if ( runableCodeBlocks.length == 0 ){
return null; return null;

View file

@ -50,31 +50,41 @@ export default class MeldBuildPlugin extends Plugin {
el: HTMLElement, el: HTMLElement,
ctx: MarkdownPostProcessorContext ctx: MarkdownPostProcessorContext
){ ){
const buttons : ToolbarButton[] = [];
const lines = source.split( '\n' ); const lines = source.split( '\n' );
const valueMap = new Map<string,string>();
lines.forEach( line => { lines.forEach( line => {
const pair = line.split( '=' ); const button = ToolbarButton.parse(line);
if( pair.length == 2 ){ if (!button){
valueMap.set( pair[0].trim(), pair[1].trim() ); return;
} }
buttons.push(button);
} ); } );
const runButtonLabel = valueMap.get('run'); if ( buttons.length == 0 ){
const showRunButton = runButtonLabel !== ''; // add default buttons
buttons.push(
const helpButtonLabel = valueMap.get('help'); new ToolbarButton( 'run' ),
const showHelpButton = helpButtonLabel !== ''; new ToolbarButton( 'help' )
);
if (showRunButton){
el.createEl('button', { text: runButtonLabel ?? 'Run'}, el => {
el.on('click', '*', async ev => await this.buildAndRunActiveView() );
});
} }
if (showHelpButton){ // render buttons
el.createEl('button', {text: helpButtonLabel ?? '❔', title: 'Help' }, el=>{ for (const button of buttons) {
el.on( 'click', '*', ev => this.openHelp() );
} ); if ( button.id == 'run' ){
el.createEl('button', { text: button.label ?? 'Run ▶️' }, el => {
el.on('click', '*', async ev => await this.buildAndRunActiveView( button.params.first() ) );
});
continue;
}
if ( button.id == 'help' ){
el.createEl('button', {text: button.label ?? '❔', title: 'Help' }, el=>{
el.on( 'click', '*', ev => this.openHelp() );
} );
continue;
}
} }
} }
@ -98,16 +108,16 @@ export default class MeldBuildPlugin extends Plugin {
}); });
} }
private async buildAndRunActiveView(){ private async buildAndRunActiveView( runGroupTag?:string ){
const view = app.workspace.getActiveViewOfType( MarkdownView ); const view = app.workspace.getActiveViewOfType( MarkdownView );
if (!view){ if (!view){
new Notice( 'Unable to run, no active Markdown View found' ); new Notice( 'Unable to run, no active Markdown View found' );
return; return;
} }
await this.buildAndRun( view.editor, view ); await this.buildAndRun( view.editor, view, runGroupTag );
} }
private async buildAndRun( editor:Editor, view: MarkdownView | MarkdownFileInfo ){ private async buildAndRun( editor:Editor, view: MarkdownView | MarkdownFileInfo, runGroupTag?:string ){
if ( !( view instanceof MarkdownView ) ){ if ( !( view instanceof MarkdownView ) ){
return; return;
} }
@ -115,7 +125,7 @@ export default class MeldBuildPlugin extends Plugin {
try{ try{
//await view.save(); //await view.save();
const compiler = new Compiler(); const compiler = new Compiler();
const runner = compiler.compile(logger, editor, view); const runner = compiler.compile( logger, editor, view, runGroupTag );
runner?.(); runner?.();
}catch(e){ }catch(e){
console.debug('here'); console.debug('here');
@ -158,3 +168,30 @@ export default class MeldBuildPlugin extends Plugin {
await this.saveData(this.settings); await this.saveData(this.settings);
} }
} }
class ToolbarButton{
id:string;
label?: string;
params: string[];
constructor( id:string, label?:string, params?:string[] ){
this.id = id;
this.label = label;
this.params = params ?? [];
}
public static parse( line:string ) : ToolbarButton|null{
const pair = line.split( '=' );
if( pair.length == 2 ){
const buttonParts = pair[0].split( '|' ).map( e => e.trim() );
const id = buttonParts.shift() ?? '';
const params = buttonParts;
const label = pair[1].trim();
return new ToolbarButton( id, label, params );
}
return null;
}
}