Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 实现瞬间九宫格样式 #455

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 89 additions & 2 deletions src/css/theme/moments/content.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
}

&:hover {
box-shadow: 0 15px 32px rgba(0, 0, 0, 0.15);
box-shadow: 0 2px 16px rgba(0, 0, 0, 0.15);
}

& .moment-content {
Expand All @@ -87,6 +87,93 @@
}
}

& .moment-medium {
width: 66%;
font-size: 0;
display: table;

& a {
display: inline-block;
border-radius: 8px;
overflow: hidden;
float: left;
position: relative;
}

& img,
& video {
aspect-ratio: 1 / 1;
object-fit: cover;
}

&.medium-1 {
& a {
width: 100%;
}
}

&.medium-2,
&.medium-4 {
& a {
width: 46%;
margin: 2%;
}
}

&.medium-3 {
& a {
width: 29.33%;
margin: 2%;

&:nth-child(1) {
width: 62.67%;
}
}
}

&.medium-5,
&.medium-8 {
& a {
width: 29.33%;
margin: 2%;

&:nth-child(n + 1):nth-child(-n + 2) {
width: 46%;
margin: 2%;
}
}
}

&.medium-6 {
& a {
width: 29.33%;
margin: 2.2% 2%;

&:nth-child(1) {
width: 62.67%;
}
}
}

&.medium-7 {
& a {
width: 29.33%;
margin: 2%;

&:nth-child(n + 4):nth-child(-n + 7) {
width: 21%;
}
}
}

&.medium-9 {
& a {
width: 29.33%;
margin: 2%;
}
}
}

& .moment-footer {
margin-top: 10px;
border-top: 1px dashed #fff;
Expand Down Expand Up @@ -170,7 +257,7 @@
&::after {
display: none;
}

& .moment-content {
& img {
max-width: 50%;
Expand Down
82 changes: 50 additions & 32 deletions src/module/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ declare const SearchWidget: any;
declare const CommentWidget: any;

export class Utils {

/**
* 按需加载 header 头部动画 css
*/
Expand Down Expand Up @@ -62,7 +61,7 @@ export class Utils {
subMenuElement.style.left = `${subMenuLeft}px`;
});
}

/**
* TODO 重试评论注册,临时解决 PJAX 情况下评论组件注册失败的问题
*/
Expand Down Expand Up @@ -103,19 +102,15 @@ export class Utils {
if (!CommentWidget) {
throw reject("Failed to fetch data");
}
CommentWidget.init(
`#${id}`,
"/plugins/PluginCommentWidget/assets/static/style.css",
{
group: group,
kind: kind,
name: name,
colorScheme: 'light'
}
);
CommentWidget.init(`#${id}`, "/plugins/PluginCommentWidget/assets/static/style.css", {
group: group,
kind: kind,
name: name,
colorScheme: "light",
});
resolve("success");
});
}
};
}

/**
Expand Down Expand Up @@ -213,34 +208,57 @@ export class Utils {
}

@documentFunction()
public wrapImageWithBox() {
const contentElement = document.querySelector(".fancybox-content") as HTMLElement;
public wrapWithFancybox() {
const contentElements = document.querySelectorAll(".fancybox-content") as NodeListOf<HTMLElement>;
contentElements?.forEach((contentElement) => {
if (contentElement.classList.contains("gallery")) {
return;
}
this.wrapImageWithBox(contentElement);
this.wrapVideoWithBox(contentElement);
import("@fancyapps/ui").then(async (module) => {
await import("@fancyapps/ui/dist/fancybox/fancybox.css");
await module.Fancybox.bind(contentElement, '[data-fancybox="gallery"]');
});
});
}

private wrapVideoWithBox(contentElement: HTMLElement) {
const videoElements = contentElement?.querySelectorAll("video") as NodeListOf<HTMLElement>;
if (!videoElements) {
return;
}
videoElements.forEach((videoElement) => {
const videoWrapper = this.buildFancybox(videoElement);
videoWrapper.classList.add("video-wrapper");
});
}

private wrapImageWithBox(contentElement: HTMLElement) {
const imageElements = contentElement?.querySelectorAll("img:not(.avatar)") as NodeListOf<HTMLElement>;
if (!imageElements) {
return;
}
imageElements.forEach((imageElement) => {
if (imageElement.classList.contains("gallery-img")) {
return;
}
imageElement.classList.add("gallery-img");
const imageWrapper = document.createElement("a");
imageWrapper.setAttribute("data-fancybox", "gallery");
if (imageElement.getAttribute("data-src")) {
imageWrapper.setAttribute("href", imageElement.getAttribute("data-src") || "");
} else {
imageWrapper.setAttribute("href", imageElement.getAttribute("src") || "");
}
const imageWrapper = this.buildFancybox(imageElement);
imageWrapper.classList.add("image-wrapper");
imageElement.parentNode?.insertBefore(imageWrapper, imageElement);
imageWrapper.appendChild(imageElement);
});
import("@fancyapps/ui").then(async (module) => {
await import("@fancyapps/ui/dist/fancybox/fancybox.css");
await module.Fancybox.bind(contentElement, '[data-fancybox="gallery"]');
});
}

private buildFancybox(element: HTMLElement) {
const wrapper = document.createElement("a");
wrapper.setAttribute("data-fancybox", "gallery");
if (element.getAttribute("data-src")) {
wrapper.setAttribute("href", element.getAttribute("data-src") || "");
} else {
wrapper.setAttribute("href", element.getAttribute("src") || "");
}
element.parentNode?.insertBefore(wrapper, element);
element.classList.add("gallery");
wrapper.appendChild(element);
return wrapper;
}

/**
* 注册 highlight (代码高亮) 功能
* 考虑到,此功能属于常用功能,因此将其注册到 Sakura 主题中,而不是采取插件的方式。
Expand Down
2 changes: 1 addition & 1 deletion templates/assets/dist/css/main.min.css

Large diffs are not rendered by default.

Loading