Compare commits

...

9 Commits

4 changed files with 83 additions and 64 deletions
+63 -17
View File
@@ -2,20 +2,23 @@ import jsdoc from '../../../doc.json'; // doc.json is built with `npm run jsdoc-
const docs = jsdoc.docs.reduce((acc, obj) => Object.assign(acc, { [obj.longname]: obj }), {});
import { MiniRepl } from './MiniRepl';
export function JsDoc({ name, h = 3, hideDescription, punchcard, canvasHeight }) {
/**
*
* @param {Object} arguments
* @param {number} [arguments.h] Adds a heading (like 3 for h3). Deprecated, add the heading in MDX instead.
* @returns
*/
export function JsDoc({ name, h = undefined, hideDescription, punchcard, canvasHeight, hideMiniRepl }) {
const item = docs[name];
if (!item) {
console.warn('Not found: ' + name);
return <div />;
console.warn(`JsDoc for ${name} not found`);
return <div>Could not load docs for ${name}</div>;
}
const CustomHeading = `h${h}`;
const description =
item.description?.replaceAll(/\{@link ([a-zA-Z\.]+)?#?([a-zA-Z]*)\}/g, (_, a, b) => {
return `<a href="#${a.replaceAll('.', '').toLowerCase()}${b ? `-${b}` : ''}">${a}${b ? `#${b}` : ''}</a>`;
}) || '';
const CustomHeading = h ? `h${h}` : undefined;
const description = parseAndReplaceLink(item.description ?? '');
return (
<>
{!!h && <CustomHeading>{item.longname}</CustomHeading>}
{!!h && <CustomHeading id={name}>{item.longname}</CustomHeading>}
{!hideDescription && (
<>
{!!item.synonyms_text && (
@@ -26,19 +29,43 @@ export function JsDoc({ name, h = 3, hideDescription, punchcard, canvasHeight })
<div dangerouslySetInnerHTML={{ __html: description }} />
</>
)}
{item.params && item.params.length > 0 && <p className="font-bold">Arguments</p>}
<ul>
{item.params?.map((param, i) => (
<li key={i}>
{param.name} ({param.type?.names?.join('|')}): {param.description?.replace(/(<([^>]+)>)/gi, '')}
</li>
))}
{item.params?.map(({ name, type, description }, i) => {
// Check if the param is actually the parameter of an object
const objParamSeperator = name.indexOf('.');
const isObjectParam = objParamSeperator !== -1;
return (
<li key={i} className={isObjectParam ? 'list-none' : ''}>
{isObjectParam ? name.substring(objParamSeperator + 1) : name} : {type?.names?.join(' | ')}{' '}
{description ? <> - {removeHtmlTags(description)}</> : ''}
</li>
);
})}
</ul>
{item.returns && (
<p>
<span className="font-bold">Returns</span>{' '}
{item.returns
.map((returnValue) => {
const description = returnValue.description ? `- ${removeHtmlTags(returnValue.description)}` : '';
return `${returnValue.type?.names?.join(' | ')} ${description}`;
})
.join(' or ')}
</p>
)}
{item.examples?.length ? (
<div className="space-y-2">
{item.examples?.map((example, k) => (
<MiniRepl tune={example} key={k} {...{ punchcard, canvasHeight }} />
))}
{item.examples?.map((example, k) => {
const key = `${item.longname}_example_${k}`;
return hideMiniRepl ? (
<pre key={key}>{example}</pre>
) : (
<MiniRepl tune={example} key={key} {...{ punchcard, canvasHeight }} />
);
})}
</div>
) : (
<div />
@@ -46,3 +73,22 @@ export function JsDoc({ name, h = 3, hideDescription, punchcard, canvasHeight })
</>
);
}
/**
* Replaces the jsDocs link with something else.
* @param {string} string
* @param {(name:string,longname: string) => string} [replaceWith]
*/
export const parseAndReplaceLink = (string, replaceWith) => {
const jsDocsLinkRegex = /\{@link ([a-zA-Z\.#]+)\}/g;
const fun = replaceWith ?? ((name, longname) => `<a target="_blank" href="/reference#${longname}">${name}</a>`);
return string
.replaceAll(/<a href="https:\/\//g, `<a target="_blank" href="https://`)
.replaceAll(jsDocsLinkRegex, (_, longname) => {
const name = [...longname.matchAll(/[a-zA-Z]+[\.#]([a-zA-Z]+)/g)].at(0)?.at(1); //take the string after . or #
return fun(name ?? longname, longname);
});
};
const removeHtmlTags = (string) => string.replace(/(<([^>]+)>)/gi, '');
+10 -5
View File
@@ -10,11 +10,15 @@ import { loadModules, setVersionDefaultsFrom } from '../repl/util.mjs';
import Claviature from '@components/Claviature';
import useClient from '@src/useClient.mjs';
let prebaked, modulesLoading, audioReady;
if (typeof window !== 'undefined') {
prebaked = prebake();
modulesLoading = loadModules();
audioReady = initAudioOnFirstClick();
let prebaked, modulesLoading, audioReady, initicated;
export function initMiniRepl() {
if (typeof window !== 'undefined' && !initicated) {
prebaked = prebake();
modulesLoading = loadModules();
audioReady = initAudioOnFirstClick();
initicated = true;
}
}
export function MiniRepl({
@@ -44,6 +48,7 @@ export function MiniRepl({
const [activeNotes, setActiveNotes] = useState([]);
const init = useCallback(({ code, autodraw }) => {
initMiniRepl();
const drawContext = canvasId ? document.querySelector('#' + canvasId)?.getContext('2d') : getDrawContext();
const editor = new StrudelMirror({
@@ -3,6 +3,7 @@ import { memo, useEffect, useMemo, useState, Fragment } from 'react';
import jsdocJson from '../../../../../doc.json';
import { Textbox } from '@src/repl/components/panel/SettingsTab';
import { settingsMap, useSettings } from '@src/settings.mjs';
import { JsDoc } from '@src/docs/JsDoc';
import { computeAvailableFunctions } from '@src/jsDocsUtils.mjs';
const availableFunctions = computeAvailableFunctions(jsdocJson);
@@ -178,48 +179,7 @@ export const Reference = memo(function Reference() {
</span>
)}
</div>
{!!entry.synonyms_text && (
<p>
Synonyms: <code>{entry.synonyms_text}</code>
</p>
)}
<p
dangerouslySetInnerHTML={{
// If we have an external url make sure it opens in another tab to not interrupt the player
__html: entry.description.replaceAll(/<a href="https:\/\//g, `<a target="_blank" href="https://`),
}}
></p>
{entry.params && entry.params.length > 0 && <p className="font-bold">Arguments</p>}
<ul>
{entry.params?.map(({ name, type, description }, i) => {
// Check if the param is actually the parameter of an object
const objParamSeperator = name.indexOf('.');
const isObjectParam = objParamSeperator !== -1;
return (
<li key={i} className={isObjectParam ? 'list-none' : ''}>
{isObjectParam ? name.substring(objParamSeperator + 1) : name} : {type?.names?.join(' | ')}{' '}
{description ? <> - {getInnerText(description)}</> : ''}
</li>
);
})}
</ul>
{entry.returns && (
<p>
<span className="font-bold">Returns</span>{' '}
{entry.returns.map(({ type, description }) => (
<>
{type?.names?.join(' | ')} {description ? <> - {getInnerText(description)}</> : ''}
</>
))}
</p>
)}
{entry.examples?.map((example, j) => (
<pre className="bg-background border border-muted" key={j}>
{example}
</pre>
))}
<JsDoc name={entry.longname} hideMiniRepl></JsDoc>
</section>
)) || <p className="font-sans">Search or select a tag to get started.</p>}
{detailVisibleFunctions.length > 0 && <div className="h-screen" />}
+8
View File
@@ -15,6 +15,14 @@ const { code2hash, errorLogger, evalScope, hash2code, logger } = strudelCore;
export const supabase = createClient(
'https://pidxdsxphlhzjnzmifth.supabase.co',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBpZHhkc3hwaGxoempuem1pZnRoIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NTYyMzA1NTYsImV4cCI6MTk3MTgwNjU1Nn0.bqlw7802fsWRnqU5BLYtmXk_k-D1VFmbkHMywWc15NM',
{
// avoid "Uncaught (in promise) Error: Acquiring an exclusive Navigator LockManager lock ... immediately failed"
auth: {
persistSession: false,
autoRefreshToken: false,
detectSessionInUrl: false,
},
},
);
let dbLoaded;