2023-07-14 01:57:45 +08:00
|
|
|
import require from "require";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { getResolverOption } from "discourse-common/resolver";
|
2020-05-06 00:15:03 +08:00
|
|
|
|
|
|
|
export const __DISCOURSE_RAW_TEMPLATES = {};
|
|
|
|
|
2021-05-08 02:41:06 +08:00
|
|
|
export function addRawTemplate(name, template, opts = {}) {
|
|
|
|
// Core templates should never overwrite themes / plugins
|
|
|
|
if (opts.core && __DISCOURSE_RAW_TEMPLATES[name]) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-06 00:15:03 +08:00
|
|
|
__DISCOURSE_RAW_TEMPLATES[name] = template;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeRawTemplate(name) {
|
|
|
|
delete __DISCOURSE_RAW_TEMPLATES[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findRawTemplate(name) {
|
|
|
|
if (getResolverOption("mobileView")) {
|
|
|
|
return (
|
|
|
|
__DISCOURSE_RAW_TEMPLATES[`javascripts/mobile/${name}`] ||
|
|
|
|
__DISCOURSE_RAW_TEMPLATES[`javascripts/${name}`] ||
|
|
|
|
__DISCOURSE_RAW_TEMPLATES[`mobile/${name}`] ||
|
|
|
|
__DISCOURSE_RAW_TEMPLATES[name]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
__DISCOURSE_RAW_TEMPLATES[`javascripts/${name}`] ||
|
|
|
|
__DISCOURSE_RAW_TEMPLATES[name]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-18 19:07:10 +08:00
|
|
|
export function buildRawConnectorCache() {
|
2020-05-06 00:15:03 +08:00
|
|
|
let result = {};
|
2023-08-18 19:07:10 +08:00
|
|
|
Object.keys(__DISCOURSE_RAW_TEMPLATES).forEach((resource) => {
|
|
|
|
const segments = resource.split("/");
|
|
|
|
const connectorIndex = segments.indexOf("connectors");
|
|
|
|
|
|
|
|
if (connectorIndex >= 0) {
|
|
|
|
const outletName = segments[connectorIndex + 1];
|
2022-11-29 18:24:35 +08:00
|
|
|
result[outletName] ??= [];
|
|
|
|
result[outletName].push({
|
|
|
|
template: __DISCOURSE_RAW_TEMPLATES[resource],
|
|
|
|
});
|
|
|
|
}
|
2023-08-18 19:07:10 +08:00
|
|
|
});
|
2020-05-06 00:15:03 +08:00
|
|
|
return result;
|
|
|
|
}
|
2022-11-29 18:24:35 +08:00
|
|
|
|
|
|
|
export function eagerLoadRawTemplateModules() {
|
2023-07-14 01:57:45 +08:00
|
|
|
for (const key of Object.keys(requirejs.entries)) {
|
|
|
|
if (key.includes("/raw-templates/")) {
|
2022-11-29 18:24:35 +08:00
|
|
|
require(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|