@nent/core
Version:
168 lines (162 loc) • 5.51 kB
JavaScript
/*!
* NENT 2022
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const index = require('./index-1829aebc.js');
const index$1 = require('./index-637e8c28.js');
const logging = require('./logging-37c154cf.js');
const remote = require('./remote-d40ac61b.js');
const elements = require('./elements-87859bcf.js');
const expressions = require('./expressions-531d82f5.js');
const interfaces = require('./interfaces-95d0415a.js');
const jsonata_worker = require('./jsonata.worker-5efab898.js');
const tokens = require('./tokens-4f8bcd42.js');
const utils = require('./utils-1f59272d.js');
const state = require('./state-246c735d.js');
const state$1 = require('./state-f97ff0e6.js');
require('./factory-0d7ddff9.js');
require('./index-96f3ab3f.js');
require('./values-b2399e33.js');
require('./promises-463f4e01.js');
require('./strings-fe5a8e44.js');
const workerPromise = Promise.resolve().then(function () { return require('./remarkable.worker-ea94c556.js'); }).then(m => m.worker);
const renderMarkdown = /*@__PURE__*/expressions.createWorkerProxy(workerPromise, 'stencil.remarkable.worker', 'renderMarkdown');
const ContentMarkdown = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.contentClass = 'rendered-content';
this.renderCache = {};
this.location = state.state.location;
this.contentElement = null;
/**
* Cross Origin Mode
*/
this.mode = 'cors';
/**
* Before rendering HTML, replace any data-tokens with their
* resolved values. This also commands this element to
* re-render it's HTML for data-changes. This can affect
* performance.
*/
this.resolveTokens = false;
/**
* If set, disables auto-rendering of this instance.
* To fetch the contents change to false or remove
* attribute.
*/
this.deferLoad = false;
/**
* Force render with data & route changes.
*/
this.noCache = false;
}
async getContentKey() {
return this.src
? await remote.resolveSrc(this.src)
: this.childScript
? 'script'
: null;
}
get canCache() {
return this.noCache === false && this.resolveTokens === false;
}
get childScript() {
return this.el.querySelector('script');
}
async componentWillLoad() {
if (this.resolveTokens || this.when != undefined) {
if (state$1.state.dataEnabled) {
this.subscribeToDataEvents();
}
else {
this.dataSubscription = state$1.onChange('dataEnabled', enabled => {
if (enabled) {
this.subscribeToDataEvents();
this.dataSubscription();
}
});
}
}
}
subscribeToDataEvents() {
this.dataSubscription = index$1.eventBus.on(interfaces.DATA_EVENTS.DataChanged, () => {
index.forceUpdate(this.el);
});
}
async componentWillRender() {
let shouldRender = !this.deferLoad;
if (shouldRender && this.when)
shouldRender = await expressions.evaluatePredicate(this.when);
if (shouldRender) {
if (this.contentElement && this.canCache)
return;
this.contentElement = await this.resolveContentElement();
}
else {
this.contentElement = null;
}
}
async resolveContentElement() {
var _a;
const key = await this.getContentKey();
if (key && this.renderCache[key])
return this.renderCache[key];
const content = this.src
? await this.getContentFromSrc()
: await this.getContentFromScript();
if (content == null)
return null;
const div = document.createElement('div');
div.innerHTML = (await renderMarkdown(content)) || '';
div.className = this.contentClass;
if (state$1.state.elementsEnabled)
elements.resolveChildElementXAttributes(div);
(_a = state.state.router) === null || _a === void 0 ? void 0 : _a.captureInnerLinks(div);
this.highlight(div);
if (key && this.canCache)
this.renderCache[key] = div;
return div;
}
async getContentFromSrc() {
try {
let content = await remote.resolveRemoteContent(window, this.src, this.mode, this.resolveTokens);
if (content && this.json) {
logging.debugIf(state$1.state.debug, `n-content-markdown: filtering: ${this.json}`);
const data = JSON.parse(content);
content = await jsonata_worker.filterData(this.json, data);
}
return content;
}
catch (err) {
logging.error(`n-content-markdown: unable to retrieve content from ${this.src}. ${err}`);
return null;
}
}
async getContentFromScript() {
const element = this.childScript;
if (!(element === null || element === void 0 ? void 0 : element.textContent))
return null;
let content = utils.dedent(element.textContent);
if (this.resolveTokens)
content = await tokens.resolveTokens(content);
return content;
}
highlight(container) {
const win = window;
const prism = win.Prism;
if (prism === null || prism === void 0 ? void 0 : prism.highlightAllUnder) {
prism.highlightAllUnder(container);
}
}
render() {
remote.replaceHtmlInElement(this.el, `.${this.contentClass}`, this.contentElement);
return index.h(index.Host, { hidden: this.contentElement == null });
}
disconnectedCallback() {
var _a;
(_a = this.dataSubscription) === null || _a === void 0 ? void 0 : _a.call(this);
}
get el() { return index.getElement(this); }
};
exports.n_content_markdown = ContentMarkdown;