mirror of
https://github.com/discourse/discourse.git
synced 2025-06-11 13:16:04 +08:00

Modernizes the renderGlimmer API to use Glimmer components, replacing deprecated ember-cli-htmlbars templates. Adds deprecation warnings for invalid parameters and improves compatibility with the new Glimmer Post Stream API.
55 lines
1.5 KiB
Plaintext
55 lines
1.5 KiB
Plaintext
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
import LazyVideo from "../discourse/components/lazy-video";
|
|
import getVideoAttributes from "../lib/lazy-video-attributes";
|
|
|
|
function initLazyEmbed(api) {
|
|
api.decorateCookedElement(
|
|
(cooked, helper) => {
|
|
if (cooked.classList.contains("d-editor-preview")) {
|
|
return;
|
|
}
|
|
|
|
const lazyContainers = cooked.querySelectorAll(".lazy-video-container");
|
|
|
|
lazyContainers.forEach((container) => {
|
|
const siteSettings = api.container.lookup("service:site-settings");
|
|
const videoAttributes = getVideoAttributes(container);
|
|
|
|
if (siteSettings[`lazy_${videoAttributes.providerName}_enabled`]) {
|
|
const onLoadedVideo = () => {
|
|
const postId = cooked.closest("article")?.dataset?.postId;
|
|
if (postId) {
|
|
api.preventCloak(parseInt(postId, 10));
|
|
}
|
|
};
|
|
|
|
const lazyVideo = document.createElement("p");
|
|
lazyVideo.classList.add("lazy-video-wrapper");
|
|
|
|
helper.renderGlimmer(
|
|
lazyVideo,
|
|
<template>
|
|
<LazyVideo
|
|
@videoAttributes={{@data.param}}
|
|
@onLoadedVideo={{@data.onLoadedVideo}}
|
|
/>
|
|
</template>,
|
|
{ param: videoAttributes, onLoadedVideo }
|
|
);
|
|
|
|
container.replaceWith(lazyVideo);
|
|
}
|
|
});
|
|
},
|
|
{ onlyStream: true }
|
|
);
|
|
}
|
|
|
|
export default {
|
|
name: "discourse-lazy-videos",
|
|
|
|
initialize() {
|
|
withPluginApi(initLazyEmbed);
|
|
},
|
|
};
|