« get me outta code hell

generateAlbumStyleRules.js « dependencies « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/generateAlbumStyleRules.js
blob: c5acf374c5918f4e7f0a874a91e281134cbc8232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {empty} from '#sugar';

export default {
  extraDependencies: ['to'],

  data(album, track) {
    const data = {};

    data.hasWallpaper = !empty(album.wallpaperArtistContribs);
    data.hasBanner = !empty(album.bannerArtistContribs);

    if (data.hasWallpaper) {
      data.wallpaperPath = ['media.albumWallpaper', album.directory, album.wallpaperFileExtension];
      data.wallpaperStyle = album.wallpaperStyle;
    }

    if (data.hasBanner) {
      data.hasBannerStyle = !!album.bannerStyle;
      data.bannerStyle = album.bannerStyle;
    }

    data.albumDirectory = album.directory;

    if (track) {
      data.trackDirectory = track.directory;
    }

    return data;
  },

  generate(data, {to}) {
    const indent = parts =>
      (parts ?? [])
        .filter(Boolean)
        .join('\n')
        .split('\n')
        .map(line => ' '.repeat(4) + line)
        .join('\n');

    const rule = (selector, parts) =>
      (!empty(parts.filter(Boolean))
        ? [`${selector} {`, indent(parts), `}`]
        : []);

    const wallpaperRule =
      data.hasWallpaper &&
        rule(`body::before`, [
          `background-image: url("${to(...data.wallpaperPath)}");`,
          data.wallpaperStyle,
        ]);

    const bannerRule =
      data.hasBanner &&
        rule(`#banner img`, [
          data.bannerStyle,
        ]);

    const dataRule =
      rule(`:root`, [
        data.albumDirectory &&
          `--album-directory: ${data.albumDirectory};`,
        data.trackDirectory &&
          `--track-directory: ${data.trackDirectory};`,
      ]);

    return (
      [wallpaperRule, bannerRule, dataRule]
        .filter(Boolean)
        .flat()
        .join('\n'));
  },
};