preview.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { domReady } from '../src/utils/domReady';
  2. import '../tests/stubs';
  3. import '../../wagtail/admin/static_src/wagtailadmin/scss/core.scss';
  4. import './preview.scss';
  5. export const parameters = {
  6. controls: {
  7. hideNoControlsWarning: true,
  8. matchers: {
  9. color: /(background|color)$/i,
  10. date: /Date$/,
  11. },
  12. },
  13. };
  14. const cachedIcons = sessionStorage.getItem('WAGTAIL_ICONS');
  15. window.WAGTAIL_ICONS = cachedIcons ? JSON.parse(cachedIcons) : [];
  16. /**
  17. * Loads Wagtail’s icon sprite into the DOM, similarly to the admin.
  18. */
  19. const loadIconSprite = () => {
  20. const PATTERN_LIBRARY_SPRITE_URL = '/pattern-library/api/v1/sprite';
  21. window
  22. .fetch(PATTERN_LIBRARY_SPRITE_URL)
  23. .then((res) => res.text())
  24. .then((html) => {
  25. const sprite = document.createElement('div');
  26. sprite.innerHTML = html;
  27. const symbols = Array.from(sprite.querySelectorAll('symbol'));
  28. const icons = symbols.map((elt) => elt.id.replace('icon-', '')).sort();
  29. window.WAGTAIL_ICONS = icons;
  30. sessionStorage.setItem('WAGTAIL_ICONS', JSON.stringify(icons));
  31. if (document.body) {
  32. document.body.appendChild(sprite);
  33. } else {
  34. window.addEventListener('DOMContentLoaded', () => {
  35. document.body.appendChild(sprite);
  36. });
  37. }
  38. });
  39. };
  40. domReady().then(() => {
  41. // Add ready class to body to enable CSS transitions
  42. // Emulates what happens in Wagtail admin when initial content is loaded
  43. document.body.classList.add('ready');
  44. });
  45. loadIconSprite();