49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import * as plugins from './smartntml.plugins.js';
|
|
|
|
import type * as litTypes from 'lit';
|
|
export type { TemplateResult } from 'lit';
|
|
|
|
export class Smartntml {
|
|
// STATIC
|
|
public static async createAndInit() {
|
|
const smartntml = new Smartntml();
|
|
await smartntml.init();
|
|
return smartntml;
|
|
}
|
|
|
|
private static smartntmlSingletonDeferred: plugins.smartpromise.Deferred<Smartntml>;
|
|
public static async createAndInitSingleton() {
|
|
if (this.smartntmlSingletonDeferred) {
|
|
return this.smartntmlSingletonDeferred.promise;
|
|
}
|
|
this.smartntmlSingletonDeferred = plugins.smartpromise.defer();
|
|
const smartntmlInstance = await this.createAndInit();
|
|
this.smartntmlSingletonDeferred.resolve(smartntmlInstance);
|
|
return this.smartntmlSingletonDeferred.promise;
|
|
}
|
|
|
|
// INSTANCE
|
|
private render: typeof litTypes.render;
|
|
html: typeof litTypes.html;
|
|
|
|
constructor() {
|
|
this.init();
|
|
}
|
|
|
|
public async init() {
|
|
const lit = await import('lit');
|
|
this.render = lit.render;
|
|
this.html = lit.html;
|
|
}
|
|
|
|
public async renderTemplateResult(templateResult: litTypes.TemplateResult, stripCommentsArg = true) {
|
|
const element = document.createElement('div');
|
|
this.render(templateResult, element);
|
|
let stringResult = element.innerHTML;
|
|
if (stripCommentsArg) {
|
|
stringResult = stringResult.replace(/<!--(.*?)-->/g, '');
|
|
}
|
|
return stringResult;
|
|
}
|
|
}
|