@storybook/addon-knobs
Version:
Storybook addon for editing props
139 lines (135 loc) • 9.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _global = require("global");
var _escapeHtml = _interopRequireDefault(require("escape-html"));
var _KnobStore = _interopRequireDefault(require("./KnobStore"));
var _shared = require("./shared");
var _excluded = ["value"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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 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 _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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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); } /* eslint no-underscore-dangle: 0 */ // navigator exists in Jest but not the browser. global exists in the browser but not Jest
var navigator = _global.navigator || _global.global.navigator;
// This is used by _mayCallChannel to determine how long to wait to before triggering a panel update
var PANEL_UPDATE_INTERVAL = 400;
function escapeStrings(obj) {
if (typeof obj === 'string') {
return (0, _escapeHtml["default"])(obj);
}
if (obj == null || _typeof(obj) !== 'object') {
return obj;
}
if (Array.isArray(obj)) {
var newArray = obj.map(escapeStrings);
var didChange = newArray.some(function (newValue, key) {
return newValue !== obj[key];
});
return didChange ? newArray : obj;
}
return Object.entries(obj).reduce(function (acc, _ref) {
var _ref2 = _slicedToArray(_ref, 2),
key = _ref2[0],
oldValue = _ref2[1];
var newValue = escapeStrings(oldValue);
return newValue === oldValue ? acc : _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, key, newValue));
}, obj);
}
var KnobManager = exports["default"] = /*#__PURE__*/function () {
function KnobManager() {
_classCallCheck(this, KnobManager);
_defineProperty(this, "knobStore", new _KnobStore["default"]());
_defineProperty(this, "options", {});
_defineProperty(this, "calling", false);
}
return _createClass(KnobManager, [{
key: "setChannel",
value: function setChannel(channel) {
this.channel = channel;
}
}, {
key: "setOptions",
value: function setOptions(options) {
this.options = options;
}
}, {
key: "getKnobValue",
value: function getKnobValue(_ref3) {
var value = _ref3.value;
return this.options.escapeHTML ? escapeStrings(value) : value;
}
}, {
key: "knob",
value: function knob(name, options) {
this._mayCallChannel();
var knobName = options.groupId ? "".concat(name, "_").concat(options.groupId) : name;
var knobStore = this.knobStore;
var existingKnob = knobStore.get(knobName);
// We need to return the value set by the knob editor via this.
// Normally the knobs are reset and so re-use is safe as long as the types match
// when in storyshots, though the change event isn't called and so the knobs aren't reset, making this code fail
// so always create a new knob when in storyshots
if (existingKnob && options.type === existingKnob.type && navigator && (
// userAgent is not set in react-native
!navigator.userAgent || !navigator.userAgent.includes('jsdom'))) {
var value = options.value,
restOptions = _objectWithoutProperties(options, _excluded);
knobStore.update(knobName, restOptions);
return this.getKnobValue(existingKnob);
}
var knobInfo = _objectSpread(_objectSpread({}, options), {}, {
name: knobName,
label: name
});
knobInfo.defaultValue = options.value;
knobStore.set(knobName, knobInfo);
return this.getKnobValue(knobStore.get(knobName));
}
}, {
key: "_mayCallChannel",
value: function _mayCallChannel() {
var _this = this;
// Re rendering of the story may cause changes to the knobStore. Some new knobs maybe added and
// Some knobs may go unused. So we need to update the panel accordingly. For example remove the
// unused knobs from the panel. This function sends the `setKnobs` message to the channel
// triggering a panel re-render.
if (!this.channel) {
// to prevent call to undefined channel and therefore throwing TypeError
return;
}
if (this.calling) {
// If a call to channel has already registered ignore this call.
// Once the previous call is completed all the changes to knobStore including the one that
// triggered this, will be added to the panel.
// This avoids emitting to the channel within very short periods of time.
return;
}
this.calling = true;
var timestamp = +new Date();
setTimeout(function () {
_this.calling = false;
// emit to the channel and trigger a panel re-render
if (_this.channel) _this.channel.emit(_shared.SET, {
knobs: _this.knobStore.getAll(),
timestamp: timestamp
});
}, PANEL_UPDATE_INTERVAL);
}
}]);
}();