@project161/yang-furigana
Version:
An easy way to use <ruby> tags in Angular
89 lines (81 loc) • 3.62 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) :
typeof define === 'function' && define.amd ? define('@project161/yang-furigana', ['exports', '@angular/core'], factory) :
(global = global || self, factory((global.project161 = global.project161 || {}, global.project161['yang-furigana'] = {}), global.ng.core));
}(this, (function (exports, core) { 'use strict';
var SPAN = 'span';
var YangFuriganaDirective = /** @class */ (function () {
function YangFuriganaDirective(el, renderer) {
this.el = el;
this.renderer = renderer;
}
YangFuriganaDirective.prototype.ngAfterViewInit = function () {
if (!this.isValidElement()) {
console.warn('Warning: yangFurigana works only on <span> elements without children.');
return;
}
var html = this.generateRubyTag();
this.renderer.setProperty(this.el.nativeElement, 'innerHTML', html);
};
/**
* Check if the element passed as argument is a <span> element and has no child tags
* @param el the Element to evaluate
*/
YangFuriganaDirective.prototype.isValidElement = function () {
return (this.el.nativeElement.nodeName.toLowerCase() === SPAN &&
this.el.nativeElement.childElementCount === 0);
};
/**
* Generate the template to be added to the HTML as result
*/
YangFuriganaDirective.prototype.generateRubyTag = function () {
var styleCode = '';
if (!!this.yangFuriganaClass) {
var encapsulationAttribute = this.el.nativeElement.attributes[0];
styleCode = " " + encapsulationAttribute.name + " class=\"" + this.yangFuriganaClass + "\"";
}
if (!!this.yangFuriganaStyle) {
styleCode += " style=\"" + this.yangFuriganaStyle + "\"";
}
var furiganaHTML = "<rp>(</rp><rt" + styleCode + ">" + this.yangFurigana + "</rt><rp>)</rp>";
return "<ruby>" + this.el.nativeElement.innerHTML + furiganaHTML + "</ruby>";
};
return YangFuriganaDirective;
}());
YangFuriganaDirective.decorators = [
{ type: core.Directive, args: [{
selector: '[yangFurigana]'
},] }
];
YangFuriganaDirective.ctorParameters = function () { return [
{ type: core.ElementRef },
{ type: core.Renderer2 }
]; };
YangFuriganaDirective.propDecorators = {
yangFurigana: [{ type: core.Input }],
yangFuriganaClass: [{ type: core.Input }],
yangFuriganaStyle: [{ type: core.Input }]
};
var YangFuriganaModule = /** @class */ (function () {
function YangFuriganaModule() {
}
return YangFuriganaModule;
}());
YangFuriganaModule.decorators = [
{ type: core.NgModule, args: [{
declarations: [YangFuriganaDirective],
imports: [],
exports: [YangFuriganaDirective]
},] }
];
/*
* Public API Surface of ng-furigana
*/
/**
* Generated bundle index. Do not edit.
*/
exports.YangFuriganaDirective = YangFuriganaDirective;
exports.YangFuriganaModule = YangFuriganaModule;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=project161-yang-furigana.umd.js.map