aesthetic-adapter-typestyle
Version:
TypeStyle support for Aesthetic.
131 lines (98 loc) • 4.21 kB
JavaScript
import { Adapter } from 'aesthetic';
import { purgeStyles, toArray } from 'aesthetic-utils';
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _inheritsLoose(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
subClass.__proto__ = superClass;
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
var TypeStyleAdapter = function (_Adapter) {
_inheritsLoose(TypeStyleAdapter, _Adapter);
function TypeStyleAdapter(typeStyle) {
var _this;
_this = _Adapter.call(this) || this;
_defineProperty(_assertThisInitialized(_this), "typeStyle", void 0);
_defineProperty(_assertThisInitialized(_this), "keyframes", {});
_defineProperty(_assertThisInitialized(_this), "handleCss", function (css) {
_this.typeStyle.cssRaw(css);
});
_defineProperty(_assertThisInitialized(_this), "handleFallback", function (ruleset, name, value) {
ruleset.addProperty(name, [].concat(value, toArray(ruleset.properties[name])));
});
_defineProperty(_assertThisInitialized(_this), "handleFontFace", function (sheet, fontFaces) {
fontFaces.forEach(function (face) {
_this.typeStyle.fontFace(face.toObject());
});
});
_defineProperty(_assertThisInitialized(_this), "handleGlobal", function (sheet, selector, ruleset) {
_this.typeStyle.cssRule(selector, ruleset.toObject());
});
_defineProperty(_assertThisInitialized(_this), "handleKeyframe", function (sheet, keyframe, animationName) {
_this.keyframes[animationName] = _this.typeStyle.keyframes(keyframe.toObject());
});
_defineProperty(_assertThisInitialized(_this), "handleMedia", function (ruleset, query, value) {
_this.handleNested(ruleset, "@media " + query, value);
});
_defineProperty(_assertThisInitialized(_this), "handleNested", function (ruleset, selector, value) {
var nest = ruleset.nested.get('$nest') || ruleset.createRuleset('$nest');
nest.addNested(selector.startsWith('@') ? selector : "&" + selector, value);
ruleset.addNested('$nest', nest);
});
_defineProperty(_assertThisInitialized(_this), "handleProperty", function (ruleset, name, value) {
if (name === 'animationName') {
ruleset.addProperty(name, _this.syntax.injectKeyframes(String(value), _this.keyframes).join(', '));
} else {
ruleset.addProperty(name, value);
}
});
_defineProperty(_assertThisInitialized(_this), "handleSupport", function (ruleset, query, value) {
_this.handleNested(ruleset, "@supports " + query, value);
});
_this.typeStyle = typeStyle;
_this.syntax.on('attribute', _this.handleNested).on('css', _this.handleCss).on('fallback', _this.handleFallback).on('font-face', _this.handleFontFace).on('global', _this.handleGlobal).on('keyframe', _this.handleKeyframe).on('media', _this.handleMedia).on('property', _this.handleProperty).on('pseudo', _this.handleNested).on('selector', _this.handleNested).on('support', _this.handleSupport);
return _this;
}
var _proto = TypeStyleAdapter.prototype;
_proto.flushStyles = function flushStyles() {
this.typeStyle.forceRenderStyles();
};
_proto.isParsedBlock = function isParsedBlock(block) {
return typeof block === 'string';
};
_proto.parseStyleSheet = function parseStyleSheet(styleSheet) {
return this.typeStyle.stylesheet(styleSheet);
};
_proto.purgeStyles = function purgeStyles$1() {
var element = this.typeStyle._tag;
if (element) {
purgeStyles(element, true);
}
};
_proto.transformToClassName = function transformToClassName(styles) {
return styles.join(' ');
};
return TypeStyleAdapter;
}(Adapter);
/**
* @copyright 2017-2019, Miles Johnson
* @license https://opensource.org/licenses/MIT
*/
export default TypeStyleAdapter;