highlight viewing pattern

This commit is contained in:
Jade (Rose) Rowland 2024-01-06 17:33:24 -05:00
parent 1bc86c359e
commit 02b9bd82cf
3 changed files with 43 additions and 15 deletions

View File

@ -18,7 +18,8 @@ import {
settingsMap, settingsMap,
updateUserCode, updateUserCode,
useSettings, useSettings,
useActivePattern, getViewingPattern,
setViewingPattern,
} from '../settings.mjs'; } from '../settings.mjs';
import { Header } from './Header'; import { Header } from './Header';
import Loader from './Loader'; import Loader from './Loader';
@ -34,7 +35,7 @@ export const ReplContext = createContext(null);
const { latestCode } = settingsMap.get(); const { latestCode } = settingsMap.get();
let modulesLoading, presets, drawContext, clearCanvas, isIframe, viewingPatternID; let modulesLoading, presets, drawContext, clearCanvas, isIframe;
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
initAudioOnFirstClick(); initAudioOnFirstClick();
modulesLoading = loadModules(); modulesLoading = loadModules();
@ -76,7 +77,11 @@ export function Repl({ embedded = false }) {
updateUserCode(code); updateUserCode(code);
// setPending(false); // setPending(false);
setLatestCode(code); setLatestCode(code);
const viewingPatternID = getViewingPattern();
window.location.hash = '#' + code2hash(code); window.location.hash = '#' + code2hash(code);
if (viewingPatternID != null) {
setActivePattern(viewingPatternID);
}
}, },
bgFill: false, bgFill: false,
}); });
@ -144,7 +149,6 @@ export function Repl({ embedded = false }) {
// payload = {reset?: boolean, code?: string, evaluate?: boolean, patternID?: string } // payload = {reset?: boolean, code?: string, evaluate?: boolean, patternID?: string }
const handleUpdate = async (payload) => { const handleUpdate = async (payload) => {
const { reset = false, code = null, evaluate = true, patternID = null } = payload; const { reset = false, code = null, evaluate = true, patternID = null } = payload;
console.log(payload);
if (reset) { if (reset) {
clearCanvas(); clearCanvas();
resetLoadedSounds(); resetLoadedSounds();
@ -153,15 +157,10 @@ export function Repl({ embedded = false }) {
} }
if (code != null) { if (code != null) {
editorRef.current.setCode(code); editorRef.current.setCode(code);
viewingPatternID = patternID; setViewingPattern(patternID);
} }
console.log(isDirty);
if (evaluate) { if (evaluate) {
editorRef.current.evaluate(); editorRef.current.evaluate();
if (viewingPatternID != null) {
setActivePattern(viewingPatternID);
}
} }
}; };
const handleShuffle = async () => { const handleShuffle = async () => {

View File

@ -9,8 +9,8 @@ import {
importPatterns, importPatterns,
newUserPattern, newUserPattern,
renameActivePattern, renameActivePattern,
setActivePattern,
useActivePattern, useActivePattern,
useViewingPattern,
useSettings, useSettings,
} from '../../settings.mjs'; } from '../../settings.mjs';
import * as tunes from '../tunes.mjs'; import * as tunes from '../tunes.mjs';
@ -19,10 +19,14 @@ function classNames(...classes) {
return classes.filter(Boolean).join(' '); return classes.filter(Boolean).join(' ');
} }
function PatternButton({ isActive, onClick, label }) { function PatternButton({ showOutline, onClick, label, showHiglight }) {
return ( return (
<a <a
className={classNames('mr-4 hover:opacity-50 cursor-pointer inline-block', isActive ? 'outline outline-1' : '')} className={classNames(
'mr-4 hover:opacity-50 cursor-pointer inline-block',
showOutline ? 'outline outline-1' : '',
showHiglight ? 'bg-red-500' : '',
)}
onClick={onClick} onClick={onClick}
> >
{label} {label}
@ -30,11 +34,17 @@ function PatternButton({ isActive, onClick, label }) {
); );
} }
function PatternButtons({ patterns, activePattern, onClick }) { function PatternButtons({ patterns, activePattern, onClick, viewingPattern }) {
return ( return (
<div className="font-mono text-sm"> <div className="font-mono text-sm">
{Object.entries(patterns).map(([key, data]) => ( {Object.entries(patterns).map(([key, data]) => (
<PatternButton key={key} label={key} isActive={key === activePattern} onClick={() => onClick(key, data)} /> <PatternButton
key={key}
label={key}
showHiglight={key === viewingPattern}
showOutline={key === activePattern}
onClick={() => onClick(key, data)}
/>
))} ))}
</div> </div>
); );
@ -43,6 +53,7 @@ function PatternButtons({ patterns, activePattern, onClick }) {
export function PatternsTab({ context }) { export function PatternsTab({ context }) {
const { userPatterns } = useSettings(); const { userPatterns } = useSettings();
const activePattern = useActivePattern(); const activePattern = useActivePattern();
const viewingPattern = useViewingPattern();
const isExample = useMemo(() => activePattern && !!tunes[activePattern], [activePattern]); const isExample = useMemo(() => activePattern && !!tunes[activePattern], [activePattern]);
const onPatternClick = (key, data) => { const onPatternClick = (key, data) => {
const { code } = data; const { code } = data;
@ -76,7 +87,12 @@ export function PatternsTab({ context }) {
</div> </div>
</div> </div>
)} )}
<PatternButtons onClick={onPatternClick} patterns={userPatterns} activePattern={activePattern} /> <PatternButtons
onClick={onPatternClick}
patterns={userPatterns}
activePattern={activePattern}
viewingPattern={viewingPattern}
/>
<div className="pr-4 space-x-4 border-b border-foreground mb-2 h-8 flex overflow-auto max-w-full items-center"> <div className="pr-4 space-x-4 border-b border-foreground mb-2 h-8 flex overflow-auto max-w-full items-center">
<button <button
className="hover:opacity-50" className="hover:opacity-50"

View File

@ -28,6 +28,19 @@ export const defaultSettings = {
export const settingsMap = persistentMap('strudel-settings', defaultSettings); export const settingsMap = persistentMap('strudel-settings', defaultSettings);
//pattern that the use is currently viewing in the window
const $viewingPattern = persistentAtom('viewingPattern', '', { listen: false });
export function setViewingPattern(key) {
$viewingPattern.set(key);
}
export function getViewingPattern() {
return $viewingPattern.get();
}
export function useViewingPattern() {
return useStore($viewingPattern);
}
// active pattern is separate, because it shouldn't sync state across tabs // active pattern is separate, because it shouldn't sync state across tabs
// reason: https://github.com/tidalcycles/strudel/issues/857 // reason: https://github.com/tidalcycles/strudel/issues/857
const $activePattern = persistentAtom('activePattern', '', { listen: false }); const $activePattern = persistentAtom('activePattern', '', { listen: false });