Skip to content

Commit

Permalink
Merge pull request #431 from tidalcycles/themes
Browse files Browse the repository at this point in the history
Themes
  • Loading branch information
felixroos committed Feb 10, 2023
2 parents 0da25ea + a500f98 commit a253c26
Show file tree
Hide file tree
Showing 31 changed files with 953 additions and 112 deletions.
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ vite.config.js
!**/*.mjs
**/*.tsx
**/*.ts
**/*.json
**/*.json
**/dev-dist
**/dist
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ packages/mini/krill-parser.js
packages/xen/tunejs.js
paper
pnpm-lock.yaml
pnpm-workspace.yaml
pnpm-workspace.yaml
**/dev-dist
18 changes: 14 additions & 4 deletions packages/react/src/components/CodeMirror6.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ const highlightField = StateField.define({
try {
for (let e of tr.effects) {
if (e.is(setHighlights)) {
const { haps } = e.value;
const marks =
e.value
haps
.map((hap) =>
(hap.context.locations || []).map(({ start, end }) => {
const color = hap.context.color || '#FFCA28';
const color = hap.context.color || e.value.color || '#FFCA28';
let from = tr.newDoc.line(start.line).from + start.column;
let to = tr.newDoc.line(end.line).from + end.column;
const l = tr.newDoc.length;
Expand All @@ -79,9 +80,17 @@ const highlightField = StateField.define({
provide: (f) => EditorView.decorations.from(f),
});

const extensions = [javascript(), strudelTheme, highlightField, flashField];
const extensions = [javascript(), highlightField, flashField];

export default function CodeMirror({ value, onChange, onViewChanged, onSelectionChange, options, editorDidMount }) {
export default function CodeMirror({
value,
onChange,
onViewChanged,
onSelectionChange,
theme,
options,
editorDidMount,
}) {
const handleOnChange = useCallback(
(value) => {
onChange?.(value);
Expand All @@ -106,6 +115,7 @@ export default function CodeMirror({ value, onChange, onViewChanged, onSelection
<>
<_CodeMirror
value={value}
theme={theme || strudelTheme}
onChange={handleOnChange}
onCreateEditor={handleOnCreateEditor}
onUpdate={handleOnUpdate}
Expand Down
31 changes: 14 additions & 17 deletions packages/react/src/components/MiniRepl.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ import { Icon } from './Icon';
import styles from './MiniRepl.module.css';
import './style.css';
import { logger } from '@strudel.cycles/core';
import useEvent from '../hooks/useEvent.mjs';
import useKeydown from '../hooks/useKeydown.mjs';

const getTime = () => getAudioContext().currentTime;

export function MiniRepl({ tune, hideOutsideView = false, enableKeyboard, drawTime, punchcard, canvasHeight = 200 }) {
export function MiniRepl({
tune,
hideOutsideView = false,
enableKeyboard,
drawTime,
punchcard,
canvasHeight = 200,
theme,
highlightColor,
}) {
drawTime = drawTime || (punchcard ? [0, 4] : undefined);
const evalOnMount = !!drawTime;
const drawContext = useCallback(
Expand Down Expand Up @@ -61,6 +72,7 @@ export function MiniRepl({ tune, hideOutsideView = false, enableKeyboard, drawTi
pattern,
active: started && !activeCode?.includes('strudel disable-highlighting'),
getTime: () => scheduler.now(),
color: highlightColor,
});

// keyboard shortcuts
Expand Down Expand Up @@ -132,7 +144,7 @@ export function MiniRepl({ tune, hideOutsideView = false, enableKeyboard, drawTi
{error && <div className={styles.error}>{error.message}</div>}
</div>
<div className={styles.body}>
{show && <CodeMirror6 value={code} onChange={setCode} onViewChanged={setView} />}
{show && <CodeMirror6 value={code} onChange={setCode} onViewChanged={setView} theme={theme} />}
</div>
{drawTime && (
<canvas
Expand Down Expand Up @@ -161,18 +173,3 @@ export function MiniRepl({ tune, hideOutsideView = false, enableKeyboard, drawTi
function useLogger(onTrigger) {
useEvent(logger.key, onTrigger);
}

// TODO: dedupe
function useEvent(name, onTrigger, useCapture = false) {
useEffect(() => {
document.addEventListener(name, onTrigger, useCapture);
return () => {
document.removeEventListener(name, onTrigger, useCapture);
};
}, [onTrigger]);
}

// TODO: dedupe
function useKeydown(onTrigger) {
useEvent('keydown', onTrigger, true);
}
8 changes: 4 additions & 4 deletions packages/react/src/components/MiniRepl.module.css
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
.container {
@apply rounded-md overflow-hidden bg-[#222222];
@apply overflow-hidden;
}

.header {
@apply flex justify-between bg-slate-700 border-t border-slate-500;
@apply flex justify-between bg-lineHighlight border-t border-l border-r border-lineHighlight rounded-t-md overflow-hidden;
}

.buttons {
@apply flex;
}

.button {
@apply cursor-pointer w-16 flex items-center justify-center p-1 bg-slate-700 border-r border-slate-500 text-white hover:bg-slate-600;
@apply cursor-pointer w-16 flex items-center justify-center p-1 border-r border-lineHighlight text-foreground hover:bg-background;
}

.buttonDisabled {
@apply w-16 flex items-center justify-center p-1 bg-slate-600 text-slate-400 cursor-not-allowed;
@apply w-16 flex items-center justify-center p-1 opacity-50 cursor-not-allowed border-r border-lineHighlight;
}

.error {
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/components/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
font-size: 18px;
}

.cm-theme-light {
.cm-theme {
width: 100%;
}

.cm-line > * {
background: #00000095;
.cm-theme-light {
width: 100%;
}
12 changes: 12 additions & 0 deletions packages/react/src/hooks/useEvent.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useEffect } from 'react';

function useEvent(name, onTrigger, useCapture = false) {
useEffect(() => {
document.addEventListener(name, onTrigger, useCapture);
return () => {
document.removeEventListener(name, onTrigger, useCapture);
};
}, [onTrigger]);
}

export default useEvent;
10 changes: 5 additions & 5 deletions packages/react/src/hooks/useHighlighting.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useRef } from 'react';
import { setHighlights } from '../components/CodeMirror6';

function useHighlighting({ view, pattern, active, getTime }) {
function useHighlighting({ view, pattern, active, getTime, color }) {
const highlights = useRef([]);
const lastEnd = useRef(0);
useEffect(() => {
Expand All @@ -19,9 +19,9 @@ function useHighlighting({ view, pattern, active, getTime }) {
highlights.current = highlights.current.filter((hap) => hap.whole.end > audioTime); // keep only highlights that are still active
const haps = pattern.queryArc(...span).filter((hap) => hap.hasOnset());
highlights.current = highlights.current.concat(haps); // add potential new onsets
view.dispatch({ effects: setHighlights.of(highlights.current) }); // highlight all still active + new active haps
view.dispatch({ effects: setHighlights.of({ haps: highlights.current, color }) }); // highlight all still active + new active haps
} catch (err) {
view.dispatch({ effects: setHighlights.of([]) });
view.dispatch({ effects: setHighlights.of({ haps: [] }) });
}
frame = requestAnimationFrame(updateHighlights);
});
Expand All @@ -30,10 +30,10 @@ function useHighlighting({ view, pattern, active, getTime }) {
};
} else {
highlights.current = [];
view.dispatch({ effects: setHighlights.of([]) });
view.dispatch({ effects: setHighlights.of({ haps: [] }) });
}
}
}, [pattern, active, view]);
}, [pattern, active, view, color]);
}

export default useHighlighting;
10 changes: 6 additions & 4 deletions packages/react/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// import 'tailwindcss/tailwind.css';

export { default as CodeMirror, flash } from './components/CodeMirror6';
export * from './components/MiniRepl';
export { default as useHighlighting } from './hooks/useHighlighting';
export { default as CodeMirror, flash } from './components/CodeMirror6'; // !SSR
export * from './components/MiniRepl'; // !SSR
export { default as useHighlighting } from './hooks/useHighlighting'; // !SSR
export { default as useStrudel } from './hooks/useStrudel'; // !SSR
export { default as usePostMessage } from './hooks/usePostMessage';
export { default as useStrudel } from './hooks/useStrudel';
export { default as useKeydown } from './hooks/useKeydown';
export { default as useEvent } from './hooks/useEvent';
export { default as strudelTheme } from './themes/strudel-theme';
export { default as cx } from './cx';

0 comments on commit a253c26

Please sign in to comment.