vue-files-preview-inno
Version:
A tool for previewing files such as doc, excel, pdf, image, markdown, txt, audio, and video and so on.
169 lines (168 loc) • 4.94 kB
JavaScript
import d from "./utils/url.mjs";
class h {
constructor(e) {
this.rendition = e, this._themes = {
default: {
rules: {},
url: "",
serialized: ""
}
}, this._overrides = {}, this._current = "default", this._injected = [], this.rendition.hooks.content.register(this.inject.bind(this)), this.rendition.hooks.content.register(this.overrides.bind(this));
}
/**
* Add themes to be used by a rendition
* @param {object | Array<object> | string}
* @example themes.register("light", "http://example.com/light.css")
* @example themes.register("light", { "body": { "color": "purple"}})
* @example themes.register({ "light" : {...}, "dark" : {...}})
*/
register() {
if (arguments.length !== 0) {
if (arguments.length === 1 && typeof arguments[0] == "object")
return this.registerThemes(arguments[0]);
if (arguments.length === 1 && typeof arguments[0] == "string")
return this.default(arguments[0]);
if (arguments.length === 2 && typeof arguments[1] == "string")
return this.registerUrl(arguments[0], arguments[1]);
if (arguments.length === 2 && typeof arguments[1] == "object")
return this.registerRules(arguments[0], arguments[1]);
}
}
/**
* Add a default theme to be used by a rendition
* @param {object | string} theme
* @example themes.register("http://example.com/default.css")
* @example themes.register({ "body": { "color": "purple"}})
*/
default(e) {
if (e) {
if (typeof e == "string")
return this.registerUrl("default", e);
if (typeof e == "object")
return this.registerRules("default", e);
}
}
/**
* Register themes object
* @param {object} themes
*/
registerThemes(e) {
for (var t in e)
e.hasOwnProperty(t) && (typeof e[t] == "string" ? this.registerUrl(t, e[t]) : this.registerRules(t, e[t]));
}
/**
* Register a theme by passing its css as string
* @param {string} name
* @param {string} css
*/
registerCss(e, t) {
this._themes[e] = { serialized: t }, (this._injected[e] || e == "default") && this.update(e);
}
/**
* Register a url
* @param {string} name
* @param {string} input
*/
registerUrl(e, t) {
var r = new d(t);
this._themes[e] = { url: r.toString() }, (this._injected[e] || e == "default") && this.update(e);
}
/**
* Register rule
* @param {string} name
* @param {object} rules
*/
registerRules(e, t) {
this._themes[e] = { rules: t }, (this._injected[e] || e == "default") && this.update(e);
}
/**
* Select a theme
* @param {string} name
*/
select(e) {
var t = this._current, r;
this._current = e, this.update(e), r = this.rendition.getContents(), r.forEach((i) => {
i.removeClass(t), i.addClass(e);
});
}
/**
* Update a theme
* @param {string} name
*/
update(e) {
var t = this.rendition.getContents();
t.forEach((r) => {
this.add(e, r);
});
}
/**
* Inject all themes into contents
* @param {Contents} contents
*/
inject(e) {
var t = [], r = this._themes, i;
for (var s in r)
r.hasOwnProperty(s) && (s === this._current || s === "default") && (i = r[s], (i.rules && Object.keys(i.rules).length > 0 || i.url && t.indexOf(i.url) === -1) && this.add(s, e), this._injected.push(s));
this._current != "default" && e.addClass(this._current);
}
/**
* Add Theme to contents
* @param {string} name
* @param {Contents} contents
*/
add(e, t) {
var r = this._themes[e];
!r || !t || (r.url ? t.addStylesheet(r.url) : r.serialized ? (t.addStylesheetCss(r.serialized, e), r.injected = !0) : r.rules && (t.addStylesheetRules(r.rules, e), r.injected = !0));
}
/**
* Add override
* @param {string} name
* @param {string} value
* @param {boolean} priority
*/
override(e, t, r) {
var i = this.rendition.getContents();
this._overrides[e] = {
value: t,
priority: r === !0
}, i.forEach((s) => {
s.css(e, this._overrides[e].value, this._overrides[e].priority);
});
}
removeOverride(e) {
var t = this.rendition.getContents();
delete this._overrides[e], t.forEach((r) => {
r.css(e);
});
}
/**
* Add all overrides
* @param {Content} content
*/
overrides(e) {
var t = this._overrides;
for (var r in t)
t.hasOwnProperty(r) && e.css(r, t[r].value, t[r].priority);
}
/**
* Adjust the font size of a rendition
* @param {number} size
*/
fontSize(e) {
this.override("font-size", e);
}
/**
* Adjust the font-family of a rendition
* @param {string} f
*/
font(e) {
this.override("font-family", e, !0);
}
destroy() {
this.rendition = void 0, this._themes = void 0, this._overrides = void 0, this._current = void 0, this._injected = void 0;
}
}
export {
h as default
};
//# sourceMappingURL=themes.mjs.map