From 517ba97c292808aff693c86ffeeec0e47ebe6e62 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Sat, 2 Sep 2023 21:29:42 +0200 Subject: [PATCH] add separate example buffer for playing examples --- src/API.ts | 53 ++++++++++++++++++++++++++++++++++---------- src/AppSettings.ts | 1 + src/Documentation.ts | 2 +- src/TransportNode.js | 6 ++++- src/main.ts | 7 +++++- 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/API.ts b/src/API.ts index b2c555c..8599bfe 100644 --- a/src/API.ts +++ b/src/API.ts @@ -75,19 +75,45 @@ export class UserAPI { }; _playDocExample = (code?: string) => { - this.play(); - console.log("Executing documentation example: " + this.app.selectedExample); - this.app.universes[this.app.selected_universe as string].global.candidate = - code ? code : (this.app.selectedExample as string); - tryEvaluate( - this.app, - this.app.universes[this.app.selected_universe as string].global - ); + /** + * Play an example from the documentation. The example is going + * to be stored in the example buffer belonging to the universe. + * This buffer is going to be cleaned everytime the user press + * pause or leaves the documentation window. + * + * @param code - The code example to play (identifier) + */ + + let current_universe = this.app.universes[this.app.selected_universe]; + this.app.exampleIsPlaying = true; + if (!current_universe.example) { + current_universe.example = { + candidate: "", + committed: "", + evaluations: 0, + }; + current_universe.example.candidate! = code + ? code + : (this.app.selectedExample as string); + } else { + current_universe.example.candidate! = code + ? code + : (this.app.selectedExample as string); + } + }; + + _stopDocExample = () => { + let current_universe = this.app.universes[this.app.selected_universe]; + if (current_universe?.example !== undefined) { + this.app.exampleIsPlaying = false; + current_universe.example.candidate! = ""; + current_universe.example.committed! = ""; + } }; _playDocExampleOnce = (code?: string) => { this.play(); - console.log("Executing documentation example: " + this.app.selectedExample); + this.app.exampleIsPlaying = true; evaluateOnce(this.app, code as string); }; @@ -406,7 +432,7 @@ export class UserAPI { player.updateLastCallTime(); - if(id !== "") { + if (id !== "") { // Sync named patterns to z0 by default player.sync("z0"); } @@ -1050,7 +1076,7 @@ export class UserAPI { return final_pulses.some((p) => p == true); }; - oneuclid = (pulses: number, length: number, rotate: number=0): boolean => { + oneuclid = (pulses: number, length: number, rotate: number = 0): boolean => { /** * Returns true if the current beat is in the given euclid sequence. * @param pulses - The number of pulses in the cycle @@ -1059,7 +1085,10 @@ export class UserAPI { * @returns True if the current beat is in the given euclid sequence */ const cycle = this._euclidean_cycle(pulses, length, rotate); - const beats = cycle.reduce((acc:number[], x:boolean, i:number) => { if(x) acc.push(i+1); return acc; }, []); + const beats = cycle.reduce((acc: number[], x: boolean, i: number) => { + if (x) acc.push(i + 1); + return acc; + }, []); return this.oncount(beats, length); }; diff --git a/src/AppSettings.ts b/src/AppSettings.ts index 0ddd270..fc6d92c 100644 --- a/src/AppSettings.ts +++ b/src/AppSettings.ts @@ -15,6 +15,7 @@ export interface Universe { locals: { [key: number]: File }; init: File; notes: File; + example?: File; } export interface File { diff --git a/src/Documentation.ts b/src/Documentation.ts index c92f009..1c0cb7e 100644 --- a/src/Documentation.ts +++ b/src/Documentation.ts @@ -33,7 +33,7 @@ export const makeExampleFactory = (application: Editor): Function => {
${description} - + \`\`\`javascript diff --git a/src/TransportNode.js b/src/TransportNode.js index 64c2fef..589fe29 100644 --- a/src/TransportNode.js +++ b/src/TransportNode.js @@ -21,7 +21,11 @@ export class TransportNode extends AudioWorkletNode { const futureTimeStamp = this.app.clock.convertTicksToTimeposition(this.app.clock.tick); this.app.clock.time_position = futureTimeStamp; - tryEvaluate(this.app, this.app.global_buffer); + if (this.app.exampleIsPlaying) { + tryEvaluate(this.app, this.app.example_buffer); + } else { + tryEvaluate(this.app, this.app.global_buffer); + } } }; diff --git a/src/main.ts b/src/main.ts index 6f1e8f2..1c21a68 100644 --- a/src/main.ts +++ b/src/main.ts @@ -73,6 +73,7 @@ export class Editor { chosenLanguage: Compartment; currentDocumentationPane: string = "introduction"; exampleCounter: number = 0; + exampleIsPlaying: boolean = false; settings = new AppSettings(); editorExtensions: Extension[] = []; @@ -680,6 +681,10 @@ export class Editor { return this.universes[this.selected_universe.toString()].notes; } + get example_buffer() { + return this.universes[this.selected_universe.toString()].example; + } + get global_buffer() { return this.universes[this.selected_universe.toString()].global; } @@ -746,10 +751,10 @@ export class Editor { if (document.getElementById("app")?.classList.contains("hidden")) { document.getElementById("app")?.classList.remove("hidden"); document.getElementById("documentation")?.classList.add("hidden"); + this.exampleIsPlaying = false; } else { document.getElementById("app")?.classList.add("hidden"); document.getElementById("documentation")?.classList.remove("hidden"); - // Load and convert Markdown content from the documentation file this.updateDocumentationContent(); }