@difizen/mana-core
Version:
157 lines (156 loc) • 10.5 kB
JavaScript
;
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); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ThemeApplication = void 0;
var _manaCommon = require("@difizen/mana-common");
var _manaSyringe = require("@difizen/mana-syringe");
var _index = require("../application/index");
var _variableProtocol = require("./basic/variable-protocol");
var _variableRegistry = require("./basic/variable-registry");
var _colorProtocol = require("./color/color-protocol");
var _colorRegistry = require("./color/color-registry");
var _themeService = require("./theme-service");
var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _class, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _descriptor5;
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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 _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }
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 _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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 _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }
function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'transform-class-properties is enabled and runs after the decorators transform.'); }
var ThemeApplication = exports.ThemeApplication = (_dec = (0, _manaSyringe.singleton)({
contrib: _index.ApplicationContribution
}), _dec2 = (0, _manaSyringe.inject)(_themeService.ThemeService), _dec3 = (0, _manaSyringe.inject)(_variableRegistry.VariableRegistry), _dec4 = (0, _manaSyringe.contrib)(_variableProtocol.VariableContribution), _dec5 = (0, _manaSyringe.inject)(_colorRegistry.ColorRegistry), _dec6 = (0, _manaSyringe.contrib)(_colorProtocol.ColorContribution), _dec(_class = (_class2 = /*#__PURE__*/function () {
function ThemeApplication() {
var _this = this;
_classCallCheck(this, ThemeApplication);
this.toUpdate = new _manaCommon.DisposableCollection();
// dispose action when update
_initializerDefineProperty(this, "themeService", _descriptor, this);
_initializerDefineProperty(this, "variables", _descriptor2, this);
_initializerDefineProperty(this, "variableContributions", _descriptor3, this);
_initializerDefineProperty(this, "colors", _descriptor4, this);
_initializerDefineProperty(this, "colorContributions", _descriptor5, this);
this.update = function () {
_this.toUpdate.dispose();
_this.toUpdate = new _manaCommon.DisposableCollection();
_this.setThemeClassName();
for (var _i = 0, _arr = [].concat(_toConsumableArray(_this.colors.getCurrentCssVariables()), _toConsumableArray(_this.variables.getCurrentCssVariables())); _i < _arr.length; _i++) {
var cv = _arr[_i];
_this.setCssVariable(cv);
}
};
}
_createClass(ThemeApplication, [{
key: "onStart",
value: function onStart(application) {
var _this2 = this;
this.application = application;
this.application.onHostChanged(function () {
return _this2.update();
});
var _iterator = _createForOfIteratorHelper(this.variableContributions.getContributions()),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var contribution = _step.value;
contribution.registerVariables(this.variables);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
var _iterator2 = _createForOfIteratorHelper(this.colorContributions.getContributions()),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var _contribution = _step2.value;
_contribution.registerColors(this.colors);
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
this.themeService.onDidColorThemeChange(function () {
return _this2.update();
});
this.variables.onDidChange(function () {
return _this2.update();
});
this.themeService.onDidColorThemeChange(function () {
return _this2.update();
});
this.colors.onDidChange(function () {
return _this2.update();
});
this.update();
}
}, {
key: "setCssVariable",
value: function setCssVariable(cv) {
var host = this.application.host;
if (document) {
document.body.style.setProperty(cv.name, cv.value);
}
if (host) {
host.style.setProperty(cv.name, cv.value);
}
}
}, {
key: "setThemeClassName",
value: function setThemeClassName() {
var themeClass = this.themeService.themeClassName;
if (document) {
document.body.classList.add(themeClass);
this.toUpdate.push(_manaCommon.Disposable.create(function () {
return document.body.classList.remove(themeClass);
}));
}
var host = this.application.host;
if (host) {
host.classList.add(themeClass);
this.toUpdate.push(_manaCommon.Disposable.create(function () {
return host.classList.remove(themeClass);
}));
}
}
}]);
return ThemeApplication;
}(), (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "themeService", [_dec2], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
}), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "variables", [_dec3], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
}), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "variableContributions", [_dec4], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
}), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "colors", [_dec5], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
}), _descriptor5 = _applyDecoratedDescriptor(_class2.prototype, "colorContributions", [_dec6], {
configurable: true,
enumerable: true,
writable: true,
initializer: null
})), _class2)) || _class);