UNPKG

1.59 kBJavaScriptView Raw
1import "core-js/modules/es.array.from.js";
2import "core-js/modules/es.string.iterator.js";
3import "core-js/modules/es.set.js";
4import "core-js/modules/es.object.to-string.js";
5import "core-js/modules/es.array.iterator.js";
6import "core-js/modules/web.dom-collections.iterator.js";
7import "core-js/modules/es.array.join.js";
8import "core-js/modules/es.array.map.js";
9import "core-js/modules/es.array.concat.js";
10import global from 'global';
11import { addons } from '@storybook/addons';
12import { STORY_CHANGED } from '@storybook/core-events';
13import { EVENTS, HIGHLIGHT_STYLE_ID } from './constants';
14import { highlightStyle } from './highlight';
15var document = global.document;
16
17if (module && module.hot && module.hot.decline) {
18 module.hot.decline();
19}
20
21var channel = addons.getChannel();
22
23var highlight = function highlight(infos) {
24 var id = HIGHLIGHT_STYLE_ID;
25 resetHighlight(); // Make sure there are no duplicated selectors
26
27 var elements = Array.from(new Set(infos.elements));
28 var sheet = document.createElement('style');
29 sheet.setAttribute('id', id);
30 sheet.innerHTML = elements.map(function (target) {
31 return "".concat(target, "{\n ").concat(highlightStyle(infos.color), "\n }");
32 }).join(' ');
33 document.head.appendChild(sheet);
34};
35
36var resetHighlight = function resetHighlight() {
37 var id = HIGHLIGHT_STYLE_ID;
38 var sheetToBeRemoved = document.getElementById(id);
39
40 if (sheetToBeRemoved) {
41 sheetToBeRemoved.parentNode.removeChild(sheetToBeRemoved);
42 }
43};
44
45channel.on(STORY_CHANGED, resetHighlight);
46channel.on(EVENTS.HIGHLIGHT, highlight);
\No newline at end of file