smartntml/ts/index.ts

40 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-05-28 15:29:36 +00:00
import * as plugins from './smartntml.plugins.js';
2023-03-30 09:02:15 +00:00
import * as deesElement from '@designestate/dees-element';
2022-05-28 15:29:36 +00:00
export class Smartntml {
// STATIC
2023-03-30 09:02:15 +00:00
public static async create() {
2022-05-28 15:29:36 +00:00
const smartntml = new Smartntml();
return smartntml;
}
2022-05-28 18:03:13 +00:00
private static smartntmlSingletonDeferred: plugins.smartpromise.Deferred<Smartntml>;
2023-03-30 09:02:15 +00:00
public static async createSingleton() {
2022-05-28 18:03:13 +00:00
if (this.smartntmlSingletonDeferred) {
return this.smartntmlSingletonDeferred.promise;
}
2022-05-28 22:33:36 +00:00
this.smartntmlSingletonDeferred = plugins.smartpromise.defer();
2023-03-30 09:02:15 +00:00
const smartntmlInstance = await this.create();
2022-05-28 18:03:13 +00:00
this.smartntmlSingletonDeferred.resolve(smartntmlInstance);
return this.smartntmlSingletonDeferred.promise;
}
2022-05-28 15:29:36 +00:00
// INSTANCE
2023-03-30 09:02:15 +00:00
private render = deesElement.render;
public html = deesElement.html;
public unsafeHTML = deesElement.unsafeHTML;
2022-05-28 15:29:36 +00:00
constructor() {
}
2023-03-30 09:02:15 +00:00
public async renderTemplateResult(templateResult: deesElement.TemplateResult, stripCommentsArg = true) {
2022-05-28 15:29:36 +00:00
const element = document.createElement('div');
this.render(templateResult, element);
2022-05-29 13:02:48 +00:00
let stringResult = element.innerHTML;
if (stripCommentsArg) {
stringResult = stringResult.replace(/<!--(.*?)-->/g, '');
}
return stringResult;
2022-05-28 15:29:36 +00:00
}
}