ui.mjsannotatedui.mjssource32 lines · 1.7 KB · raw
1/*
2ui.mjs - <short description TODO>
3Copyright (C) 2022 Strudel contributors - see <https://codeberg.org/uzu/strudel/src/branch/main/packages/core/ui.mjs>
4This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program.  If not, see <https://www.gnu.org/licenses/>.
5*/
6
7export const backgroundImage = function (src, animateOptions = {}) {
8  const container = document.getElementById('code');
9  const bg = 'background-image:url(' + src + ');background-size:contain;';
10  container.style = bg;
11  const { className: initialClassName } = container;
12  const handleOption = (option, value) => {
13    ({
14      style: () => (container.style = bg + ';' + value),
15      className: () => (container.className = value + ' ' + initialClassName),
16    })[option]();
17  };
18  const funcOptions = Object.entries(animateOptions).filter(([_, v]) => typeof v === 'function');
19  const stringOptions = Object.entries(animateOptions).filter(([_, v]) => typeof v === 'string');
20  stringOptions.forEach(([option, value]) => handleOption(option, value));
21
22  if (funcOptions.length === 0) {
23    return;
24  }
25};
26
27export const cleanupUi = () => {
28  const container = document.getElementById('code');
29  if (container) {
30    container.style = '';
31  }
32};