eval code examples in documentation
This commit is contained in:
16
src/API.ts
16
src/API.ts
@ -41,6 +41,7 @@ export class UserAPI {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
private variables: { [key: string]: any } = {};
|
private variables: { [key: string]: any } = {};
|
||||||
|
public codeExamples: { [key: string]: string } = {};
|
||||||
private counters: { [key: string]: any } = {};
|
private counters: { [key: string]: any } = {};
|
||||||
private _drunk: DrunkWalk = new DrunkWalk(-100, 100, false);
|
private _drunk: DrunkWalk = new DrunkWalk(-100, 100, false);
|
||||||
public randomGen = Math.random;
|
public randomGen = Math.random;
|
||||||
@ -52,22 +53,19 @@ export class UserAPI {
|
|||||||
MidiConnection: MidiConnection = new MidiConnection();
|
MidiConnection: MidiConnection = new MidiConnection();
|
||||||
load: samples;
|
load: samples;
|
||||||
|
|
||||||
constructor(public app: Editor) {
|
constructor(public app: Editor) {}
|
||||||
//this.load = samples("github:tidalcycles/Dirt-Samples/master");
|
|
||||||
}
|
|
||||||
|
|
||||||
_executeCodeExample = (code: string) => {
|
_playDocExample = (code?: string) => {
|
||||||
console.log("Executing documentation example");
|
this.play();
|
||||||
this.app.universes[
|
console.log("Executing documentation example: " + this.app.selectedExample);
|
||||||
this.app.selected_universe as string
|
this.app.universes[this.app.selected_universe as string].global.candidate =
|
||||||
].global.candidate = code as string;
|
code ? code : (this.app.selectedExample as string);
|
||||||
tryEvaluate(
|
tryEvaluate(
|
||||||
this.app,
|
this.app,
|
||||||
this.app.universes[this.app.selected_universe as string].global
|
this.app.universes[this.app.selected_universe as string].global
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
_all_samples = (): object => {
|
_all_samples = (): object => {
|
||||||
return soundMap.get();
|
return soundMap.get();
|
||||||
};
|
};
|
||||||
|
|||||||
@ -37,35 +37,27 @@ const injectAvailableSamples = (application: Editor): string => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const documentation_factory = (application: Editor) => {
|
export const documentation_factory = (application: Editor) => {
|
||||||
function convert(template: string): string {
|
let counter = 0; // Counter to generate unique IDs for code snippets
|
||||||
// Escape special characters in the string
|
|
||||||
const escapedString = template
|
|
||||||
.replace(/\\/g, "\\\\")
|
|
||||||
.replace(/'/g, "\\'")
|
|
||||||
.replace(/"/g, '\\"');
|
|
||||||
|
|
||||||
// Split the template string by line breaks
|
// Initialize a data structure to store code examples by their unique IDs
|
||||||
const lines = escapedString.split("\n");
|
application.api.codeExamples = {};
|
||||||
|
|
||||||
// Concatenate the lines using the + operator
|
|
||||||
const concatenatedString = lines.map((line) => `'${line}'`).join(" +\n");
|
|
||||||
|
|
||||||
return concatenatedString;
|
|
||||||
}
|
|
||||||
|
|
||||||
const makeExample = (
|
const makeExample = (
|
||||||
description: string,
|
description: string,
|
||||||
code: string,
|
code: string,
|
||||||
open: boolean = false
|
open: boolean = false
|
||||||
): string => {
|
): string => {
|
||||||
|
const codeId = `codeExample${counter++}`;
|
||||||
|
|
||||||
|
// Store the code snippet in the data structure
|
||||||
|
application.api.codeExamples[codeId] = code;
|
||||||
|
|
||||||
return `
|
return `
|
||||||
<details ${open ? "open" : ""}>
|
<details ${open ? "open" : ""}>
|
||||||
<summary>${description}<button class="inline-block pl-2" onclick="()=>_executeCodeExample(${convert(
|
<summary>${description}
|
||||||
code
|
<button class="inline-block pl-2" onclick="app.api._playDocExample(app.api.codeExamples['${codeId}'])">▶</button>
|
||||||
)})()">▶</button>
|
<button class="inline-block pl-2" onclick="stop()">❚❚</button>
|
||||||
<button class="inline-block pl-2" onclick="(()=>stop)()">❚❚</button>
|
|
||||||
</summary>
|
</summary>
|
||||||
|
|
||||||
\`\`\`javascript
|
\`\`\`javascript
|
||||||
${code}
|
${code}
|
||||||
\`\`\`
|
\`\`\`
|
||||||
@ -99,7 +91,7 @@ ${makeExample(
|
|||||||
"Drums and arpeggios",
|
"Drums and arpeggios",
|
||||||
`
|
`
|
||||||
bpm(80)
|
bpm(80)
|
||||||
mod(0.25) :: sound('sawtooth')
|
mod(0.25) && sound('sawtooth')
|
||||||
.note(seqbar(
|
.note(seqbar(
|
||||||
[60, 67, 63].pick() - 12, [60, 67, 63].pick() - 12,
|
[60, 67, 63].pick() - 12, [60, 67, 63].pick() - 12,
|
||||||
[60, 67, 63].pick() - 12 + 5, [60, 67, 63].pick() - 12 + 5,
|
[60, 67, 63].pick() - 12 + 5, [60, 67, 63].pick() - 12 + 5,
|
||||||
|
|||||||
@ -78,6 +78,7 @@ export class Editor {
|
|||||||
userPlugins: Extension[] = [];
|
userPlugins: Extension[] = [];
|
||||||
state: EditorState;
|
state: EditorState;
|
||||||
api: UserAPI;
|
api: UserAPI;
|
||||||
|
selectedExample: string | null = "";
|
||||||
docs: { [key: string]: string } = {};
|
docs: { [key: string]: string } = {};
|
||||||
|
|
||||||
// Audio stuff
|
// Audio stuff
|
||||||
@ -677,13 +678,8 @@ export class Editor {
|
|||||||
const converted_markdown = converter.makeHtml(
|
const converted_markdown = converter.makeHtml(
|
||||||
this.docs[this.currentDocumentationPane]
|
this.docs[this.currentDocumentationPane]
|
||||||
);
|
);
|
||||||
function wrapCodeWithPre(inputString: string): string {
|
|
||||||
let newString = inputString.replace(/<code>/g, "<pre><code>");
|
|
||||||
newString = newString.replace(/<\/code>/g, "</code></pre>");
|
|
||||||
return newString;
|
|
||||||
}
|
|
||||||
document.getElementById("documentation-content")!.innerHTML =
|
document.getElementById("documentation-content")!.innerHTML =
|
||||||
wrapCodeWithPre(converted_markdown);
|
converted_markdown;
|
||||||
}
|
}
|
||||||
|
|
||||||
changeToLocalBuffer(i: number) {
|
changeToLocalBuffer(i: number) {
|
||||||
|
|||||||
Reference in New Issue
Block a user