UNPKG

react-typestyle

Version:

A react integration of the TypeStyle ecosystem

81 lines 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var free_style_1 = require("free-style"); var formatting_1 = require("typestyle/lib/internal/formatting"); var utils_1 = require("./utils"); var Registry = (function () { function Registry(_a) { var plugins = (_a === void 0 ? {} : _a).plugins; this.listeners = []; this.plugins = plugins; this.freeStyle = free_style_1.create(undefined, true); this.update = this.update.bind(this); } Registry.prototype.clear = function () { var patch = this.freeStyle; this.freeStyle = free_style_1.create(undefined, true); return this.publish(patch, true); }; Registry.prototype.getStyles = function () { return this.freeStyle.getStyles(); }; Registry.prototype.mount = function (registry) { registry.on(this.update); return this.update(registry.freeStyle); }; Registry.prototype.off = function (listener) { this.listeners = this.listeners.filter(function (value) { return value !== listener; }); return this; }; Registry.prototype.on = function (listener) { this.listeners.push(listener); return this; }; Registry.prototype.sheet = function (object) { var _this = this; var patch = free_style_1.create(undefined, true); var classNames = {}; Object.keys(object).forEach(function (name) { classNames[name] = _this.registerStyle(patch, object[name]); }); this.update(patch); return classNames; }; Registry.prototype.style = function (object) { var patch = free_style_1.create(undefined, true); var className = this.registerStyle(patch, object); this.update(patch); return className; }; Registry.prototype.toString = function () { return this.getStyles(); }; Registry.prototype.unmount = function (registry) { registry.off(this.update); this.update(registry.freeStyle, true); return this; }; Registry.prototype.update = function (patch, subtractive) { if (subtractive) { this.freeStyle.unmerge(patch); } else { this.freeStyle.merge(patch); } return this.publish(patch, subtractive); }; Registry.prototype.publish = function (patch, subtractive) { this.listeners.forEach(function (listener) { listener(patch, subtractive); }); return this; }; Registry.prototype.registerStyle = function (freeStyle, style, props) { var _a = formatting_1.ensureStringObj(style), result = _a.result, debugName = _a.debugName; var processed = utils_1.processPlugins(this.plugins, result, props); return debugName ? freeStyle.registerStyle(processed, debugName) : freeStyle.registerStyle(processed); }; return Registry; }()); exports.default = Registry; //# sourceMappingURL=registry.js.map