« 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: 6bfcc62e5668a78521401524ec5e68ee75e1f11c (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {empty, stitchArrays} from '#sugar';

export default {
  extraDependencies: ['to'],

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

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

    if (data.hasWallpaper) {
      if (!empty(album.wallpaperParts)) {
        data.wallpaperMode = 'parts';

        data.wallpaperPaths =
          album.wallpaperParts.map(part =>
            (part.asset
              ? ['media.albumWallpaperPart', album.directory, part.asset]
              : null));

        data.wallpaperStyles =
          album.wallpaperParts.map(part => part.style);
      } else {
        data.wallpaperMode = 'one';
        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 oneWallpaperRule =
      data.wallpaperMode === 'one' &&
        rule(`body::before`, [
          `background-image: url("${to(...data.wallpaperPath)}");`,
          data.wallpaperStyle,
        ]);

    const wallpaperPartRules =
      data.wallpaperMode === 'parts' &&
        stitchArrays({
          path: data.wallpaperPaths,
          style: data.wallpaperStyles,
        }).map(({path, style}, index) =>
            rule(`.wallpaper-part:nth-child(${index + 1})`, [
              path && `background-image: url("${to(...path)}");`,
              style,
            ]));

    const nukeBasicWallpaperRule =
      data.wallpaperMode === 'parts' &&
        rule(`body::before`, ['display: none']);

    const wallpaperRules = [
      oneWallpaperRule,
      ...wallpaperPartRules || [],
      nukeBasicWallpaperRule,
    ];

    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 (
      [...wallpaperRules, bannerRule, dataRule]
        .filter(Boolean)
        .flat()
        .join('\n'));
  },
};