@storybook/addon-knobs
Version:
Storybook addon for editing props
91 lines (89 loc) • 4.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var callArg = function callArg(fn) {
return fn();
};
var callAll = function callAll(fns) {
return fns.forEach(callArg);
};
var KnobStore = exports["default"] = /*#__PURE__*/function () {
function KnobStore() {
_classCallCheck(this, KnobStore);
_defineProperty(this, "store", {});
_defineProperty(this, "callbacks", []);
}
return _createClass(KnobStore, [{
key: "has",
value: function has(key) {
return this.store[key] !== undefined;
}
}, {
key: "set",
value: function set(key, value) {
this.store[key] = _objectSpread(_objectSpread({}, value), {}, {
used: true,
groupId: value.groupId
});
// debounce the execution of the callbacks for 50 milliseconds
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(callAll, 50, this.callbacks);
}
}, {
key: "update",
value: function update(key, options) {
this.store[key] = _objectSpread(_objectSpread({}, this.store[key]), options);
}
}, {
key: "get",
value: function get(key) {
var knob = this.store[key];
if (knob) {
knob.used = true;
}
return knob;
}
}, {
key: "getAll",
value: function getAll() {
return this.store;
}
}, {
key: "reset",
value: function reset() {
this.store = {};
}
}, {
key: "markAllUnused",
value: function markAllUnused() {
var _this = this;
Object.keys(this.store).forEach(function (knobName) {
_this.store[knobName].used = false;
});
}
}, {
key: "subscribe",
value: function subscribe(cb) {
this.callbacks.push(cb);
}
}, {
key: "unsubscribe",
value: function unsubscribe(cb) {
var index = this.callbacks.indexOf(cb);
this.callbacks.splice(index, 1);
}
}]);
}();