UNPKG

4.54 kBJavaScriptView Raw
1"use strict";
2
3require("core-js/modules/es.object.to-string");
4
5require("core-js/modules/es.promise");
6
7require("regenerator-runtime/runtime");
8
9var _global = require("global");
10
11var _axeCore = _interopRequireDefault(require("axe-core"));
12
13var _addons = _interopRequireDefault(require("@storybook/addons"));
14
15var _constants = require("./constants");
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
18
19function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
20
21function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
22
23if (module && module.hot && module.hot.decline) {
24 module.hot.decline();
25}
26
27var channel = _addons["default"].getChannel(); // Holds axe core running state
28
29
30var active = false; // Holds latest story we requested a run
31
32var activeStoryId;
33
34var getElement = function getElement() {
35 var storyRoot = _global.document.getElementById('story-root');
36
37 return storyRoot ? storyRoot.children : _global.document.getElementById('root');
38};
39/**
40 * Handle A11yContext events.
41 * Because the event are sent without manual check, we split calls
42 */
43
44
45var handleRequest = function handleRequest(storyId) {
46 var _getParams = getParams(storyId),
47 manual = _getParams.manual;
48
49 if (!manual) {
50 run(storyId);
51 }
52};
53
54var run = /*#__PURE__*/function () {
55 var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(storyId) {
56 var input, _input$element, element, config, _input$options, options, result;
57
58 return regeneratorRuntime.wrap(function _callee$(_context) {
59 while (1) {
60 switch (_context.prev = _context.next) {
61 case 0:
62 activeStoryId = storyId;
63 _context.prev = 1;
64 input = getParams(storyId);
65
66 if (active) {
67 _context.next = 13;
68 break;
69 }
70
71 active = true;
72 channel.emit(_constants.EVENTS.RUNNING);
73 _input$element = input.element, element = _input$element === void 0 ? getElement() : _input$element, config = input.config, _input$options = input.options, options = _input$options === void 0 ? {
74 restoreScroll: true
75 } : _input$options;
76
77 _axeCore["default"].reset();
78
79 if (config) {
80 _axeCore["default"].configure(config);
81 }
82
83 _context.next = 11;
84 return _axeCore["default"].run(element, options);
85
86 case 11:
87 result = _context.sent;
88
89 // It's possible that we requested a new run on a different story.
90 // Unfortunately, axe doesn't support a cancel method to abort current run.
91 // We check if the story we run against is still the current one,
92 // if not, trigger a new run using the current story
93 if (activeStoryId === storyId) {
94 channel.emit(_constants.EVENTS.RESULT, result);
95 } else {
96 active = false;
97 run(activeStoryId);
98 }
99
100 case 13:
101 _context.next = 18;
102 break;
103
104 case 15:
105 _context.prev = 15;
106 _context.t0 = _context["catch"](1);
107 channel.emit(_constants.EVENTS.ERROR, _context.t0);
108
109 case 18:
110 _context.prev = 18;
111 active = false;
112 return _context.finish(18);
113
114 case 21:
115 case "end":
116 return _context.stop();
117 }
118 }
119 }, _callee, null, [[1, 15, 18, 21]]);
120 }));
121
122 return function run(_x) {
123 return _ref.apply(this, arguments);
124 };
125}();
126/** Returns story parameters or default ones. */
127
128
129var getParams = function getParams(storyId) {
130 var _ref2 = _global.window.__STORYBOOK_STORY_STORE__.fromId(storyId) || {},
131 parameters = _ref2.parameters;
132
133 return parameters.a11y || {
134 config: {},
135 options: {
136 restoreScroll: true
137 }
138 };
139};
140
141channel.on(_constants.EVENTS.REQUEST, handleRequest);
142channel.on(_constants.EVENTS.MANUAL, run);
\No newline at end of file