Skip to content

Commit 6f8f85c

Browse files
authored
Allow users to provide their own Butterchurn presets (#1309)
* Allow users to provide their own Butterchurn presets * Ensure entrypoints have anchor links
1 parent e82db4c commit 6f8f85c

File tree

6 files changed

+67
-20
lines changed

6 files changed

+67
-20
lines changed

packages/webamp-docs/docs/06_API/00_entrypoints.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ All bundles are minified ES modules with provided TypeScript types. For examples
66

77
The main bundles are exposed as the following [package entrypoints](https://nodejs.org/api/packages.html#package-entry-points):
88

9-
# `webamp/butterchurn`
9+
## `webamp/butterchurn`
1010

1111
**Since** [v2.2.0](../12_changelog.md#220)
1212

@@ -20,7 +20,7 @@ This all-inclusive minified bundle has everything you need to enable all Webamp
2020
import Webamp from "webamp/butterchurn";
2121
```
2222

23-
# `webamp/lazy`
23+
## `webamp/lazy`
2424

2525
:::warning
2626
Using this entrypoint requires that you have a rather sophisticated bundler setup.
@@ -38,7 +38,7 @@ import Webamp from "webamp/lazy";
3838

3939
For instructions on how to use this entrypoint, see the [Bundle Size Guide](../07_guides/03_bundle-size.md).
4040

41-
# `webamp`
41+
## `webamp`
4242

4343
:::warning
4444
In a future version of Webamp, this entrypoint will become an alias of `webamp/butterchurn`.

packages/webamp-docs/docs/06_API/02_webamp-constructor.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,15 @@ const webamp = new Webamp({
254254
// ...other config options
255255
});
256256
```
257+
258+
### `requireButterchurnPresets?: () => Promise<Preset[]>`
259+
260+
**Since** [unreleased](../12_changelog.md#unreleased)
261+
262+
Milkdrop (Butterchurn) presets to be used. If not specified, the default presets
263+
included in the bundle will be used.
264+
265+
Presets are expected to be in Butterchurn's JSON format. You can find these `.json` files in:
266+
267+
- The [Milkdrop Presets Collection](https://archive.org/details/milkdrops) at the Internet Archive.
268+
- The [`butterchurn-presets@3.0.0-beta.4`](https://www.npmjs.com/package/butterchurn-presets/v/3.0.0-beta.4) NPM package

packages/webamp-docs/docs/12_changelog.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# Changelog
22

3-
<!-- ## Unreleased
3+
## Unreleased
44

55
_These changes are not yet published to NPM with an official version number._
66

77
:::tip
88
If you want access to the changes in this section before they are officially released you can do `npm install webamp@main`.
9-
::: -->
9+
:::
10+
11+
### Improvements
12+
13+
- Added new [`requireButterchurnPresets`](./06_API/02_webamp-constructor.md#requirebutterchurnpresets---promisepreset) option when constructing a Webamp instance. This allows you to specify which Butterchurn presets to use for the Milkdrop visualizer. If you don't specify this option, Webamp will use the default Butterchurn presets.
1014

1115
## 2.2.0
1216

packages/webamp/js/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,18 @@ export interface Options {
741741
* https://developer.mozilla.org/en-US/docs/Web/API/Media_Session_API
742742
*/
743743
enableMediaSession?: boolean;
744+
745+
/**
746+
* Milkdrop (Butterchurn) presets to be used. If not specified, the default presets
747+
* included in the bundle will be used.
748+
*
749+
* Presets are expected to be in Butterchurn's JSON format. You can find these
750+
* `.json` files in:
751+
*
752+
* * The [Milkdrop Presets Collection](https://archive.org/details/milkdrops) at the Internet Archive.
753+
* * The `butterchurn-presets@3.0.0-beta.4` NPM package
754+
*/
755+
requireButterchurnPresets?: () => Promise<Preset[]>;
744756
}
745757

746758
/**

packages/webamp/js/webampLazy.tsx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
PlaylistTrack,
1717
PlayerMediaStatus,
1818
IMetadataApi,
19+
Preset,
1920
} from "./types";
2021
import getStore from "./store";
2122
import App from "./components/App";
@@ -44,14 +45,17 @@ export interface PrivateOptions {
4445
export interface InjectableDependencies {
4546
requireJSZip: () => Promise<JSZip>;
4647
requireMusicMetadata: () => Promise<IMetadataApi>;
48+
requireButterchurnPresets?: () => Promise<Preset[]>;
4749
}
4850

4951
class Webamp {
5052
static VERSION = "2.2.0";
5153
_actionEmitter: Emitter;
5254
_root: ReactDOM.Root | null;
5355
_disposable: Disposable;
54-
options: Options & PrivateOptions & InjectableDependencies; // TODO: Make this _private
56+
// TODO: Make this _private
57+
options: Options & PrivateOptions & InjectableDependencies;
58+
5559
media: IMedia; // TODO: Make this _private
5660
store: Store; // TODO: Make this _private
5761

@@ -84,6 +88,7 @@ class Webamp {
8488
zIndex,
8589
requireJSZip,
8690
requireMusicMetadata,
91+
requireButterchurnPresets,
8792
handleTrackDropEvent,
8893
handleAddUrlEvent,
8994
handleLoadListEvent,
@@ -93,11 +98,22 @@ class Webamp {
9398
__customMediaClass,
9499
} = this.options;
95100

101+
const butterchurnOptions = __butterchurnOptions;
102+
103+
if (requireButterchurnPresets != null) {
104+
if (butterchurnOptions == null) {
105+
throw new Error(
106+
"You must pass `__butterchurnOptions` if you are using `requireButterchurnPresets`."
107+
);
108+
}
109+
butterchurnOptions.getPresets = requireButterchurnPresets;
110+
}
111+
96112
// TODO: Make this much cleaner.
97113
let convertPreset = null;
98-
if (__butterchurnOptions != null) {
114+
if (butterchurnOptions != null) {
99115
const { importConvertPreset, presetConverterEndpoint } =
100-
__butterchurnOptions;
116+
butterchurnOptions;
101117

102118
if (importConvertPreset != null && presetConverterEndpoint != null) {
103119
convertPreset = async (file: File): Promise<Object> => {
@@ -148,14 +164,12 @@ class Webamp {
148164
this.store.dispatch({ type: "SET_Z_INDEX", zIndex });
149165
}
150166

151-
if (options.__butterchurnOptions) {
167+
if (butterchurnOptions) {
152168
this.store.dispatch({
153169
type: "ENABLE_MILKDROP",
154-
open: options.__butterchurnOptions.butterchurnOpen,
170+
open: butterchurnOptions.butterchurnOpen,
155171
});
156-
this.store.dispatch(
157-
Actions.initializePresets(options.__butterchurnOptions)
158-
);
172+
this.store.dispatch(Actions.initializePresets(butterchurnOptions));
159173
}
160174

161175
const handleOnline = () =>

packages/webamp/js/webampWithButterchurn.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Options } from "./types";
1+
import { Options, Preset } from "./types";
22
import { PrivateOptions } from "./webampLazy";
33
import Webamp from "./webamp";
44
// @ts-ignore
@@ -19,18 +19,23 @@ const DEFAULT_BUTTERCHURN_WINDOW_LAYOUT = {
1919
},
2020
};
2121

22+
const DEFAULT_REQUIRE_BUTTERCHURN_PRESETS = async () =>
23+
Object.entries(butterchurnPresets).map(([name, preset]) => {
24+
return { name, butterchurnPresetObject: preset as Object };
25+
});
26+
2227
export default class WebampWithButterchurn extends Webamp {
2328
constructor(options: Options & PrivateOptions) {
29+
const requireButterchurnPresets =
30+
options.requireButterchurnPresets ?? DEFAULT_REQUIRE_BUTTERCHURN_PRESETS;
2431
super({
2532
...options,
33+
requireButterchurnPresets,
2634
__butterchurnOptions: {
2735
importButterchurn: () => Promise.resolve(butterchurn),
28-
// @ts-ignore
29-
getPresets: () => {
30-
return Object.entries(butterchurnPresets).map(([name, preset]) => {
31-
return { name, butterchurnPresetObject: preset };
32-
});
33-
},
36+
// This should be considered deprecated, and users should instead supply
37+
// the top level `requireButterchurnPresets` option.
38+
getPresets: requireButterchurnPresets,
3439
butterchurnOpen: true,
3540
},
3641
windowLayout: options.windowLayout ?? DEFAULT_BUTTERCHURN_WINDOW_LAYOUT,

0 commit comments

Comments
 (0)