Skip to content

Commit

Permalink
Merge pull request #37
Browse files Browse the repository at this point in the history
重构时间线页面
  • Loading branch information
fordes123 authored Jan 15, 2025
2 parents af16d5e + 73764eb commit 249da40
Show file tree
Hide file tree
Showing 9 changed files with 1,360 additions and 1,608 deletions.
2,675 changes: 1,158 additions & 1,517 deletions assets/css/style.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/css/style.min.css

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ function themeConfig($form)
$form->addInput(
new Typecho_Widget_Helper_Form_Element_Text('icp', NULL, NULL, _t('备案号'), _t('有就填没有就不填不要乱填'))
);

//时间轴文章数量
$form->addInput(
new Typecho_Widget_Helper_Form_Element_Text('timeline_num', NULL, 20, _t('时间轴文章数量'), _t('不宜过大'))
);
}

/**
Expand Down
72 changes: 72 additions & 0 deletions libs/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,78 @@ public static function formatNumber($number)
}
return $number;
}

/**
* 生成分页导航
* @param string $baseUrl 基础URL
* @param int $currentPage 当前页码
* @param int $totalPages 总页数
* @return string 分页HTML
*/
public static function pagination($baseUrl, $currentPage, $totalPages)
{
$html = '<nav class="navigation pagination" aria-label="Posts Navigation"><div class="nav-links">';
if ($currentPage > 1) {
$html .= '<a class="prev page-numbers" href="' . $baseUrl . ($currentPage - 1) . '">上一页</a>';
}

for ($i = 1; $i <= $totalPages; $i++) {
if ($i == $currentPage) {
$html .= '<span aria-current="page" class="page-numbers current">' . $i . '</span>';
} else {
if ($i == 1 || $i == $totalPages || ($i >= $currentPage - 2 && $i <= $currentPage + 2)) {
$html .= '<a class="page-numbers" href="' . $baseUrl . $i . '">' . $i . '</a>';
} elseif ($i == $currentPage - 3 || $i == $currentPage + 3) {
$html .= '<span class="page-numbers dots">...</span>';
}
}
}

if ($currentPage < $totalPages) {
$html .= '<a class="next page-numbers" href="' . $baseUrl . ($currentPage + 1) . '">下一页</a>';
}

$html .= '</div></nav>';
return $html;
}

/**
* 分页查询文章
* @param int $pageSize 每页文章数
* @param int $currentPage 当前页码
* @return array 包含文章cid列表和分页信息的数组
*/
public static function page($pageSize, $currentPage)
{
$db = Typecho_Db::get();
$totalPosts = $db->fetchObject($db->select(array('COUNT(cid)' => 'num'))
->from('table.contents')
->where('type = ?', 'post')
->where('status = ?', 'publish')
->order('modified', Typecho_Db::SORT_DESC))->num;

$totalPages = ceil($totalPosts / $pageSize);
if ($currentPage > $totalPages) {
$currentPage = $totalPages;
} elseif ($currentPage < 1) {
$currentPage = 1;
}

$offset = ($currentPage - 1) * $pageSize;
$result = $db->fetchAll($db->select('cid')
->from('table.contents')
->where('type = ?', 'post')
->where('status = ?', 'publish')
->order('modified', Typecho_Db::SORT_DESC)
->limit($pageSize)
->offset($offset));

return array(
'data' => array_column($result, 'cid'),
'currentPage' => $currentPage,
'totalPages' => $totalPages
);
}
}

/**
Expand Down
37 changes: 36 additions & 1 deletion page.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit;
$this->need('post.php');
$this->need('header.php');
$this->need('sidebar.php');
$this->need('topbar.php');
?>

<main class="site-main">
<div class="container">
<div class="container">
<div class="row gx-3 gx-md-4">
<div class="post card card-md mb-3 mb-md-4">
<div class="post-other-style">
<div class="post-heading text-center pt-5 pt-md-5 pb-3 pb-xl-4">
<h1 class="post-title"> <?php $this->title(); ?></h1>
<div class="post-meta d-flex flex-fill justify-content-center align-items-center text-base mt-3 mt-md-3">
</div>
</div>
<div class="card-body">
<div class="post-content">
<div class="post-excerpt">
<?php if ($this->fields->text): ?>
<i class="excerpt-icon"></i>
<h4><?php echo $this->fields->text; ?></h4>
<?php endif; ?>
</div>
<div class="text-wrap text-break fs-6 mx-3">
<?php $this->content(); ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</main>
<?php $this->need('footer.php'); ?>
17 changes: 0 additions & 17 deletions post-modal.php

This file was deleted.

20 changes: 17 additions & 3 deletions post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@
$this->need('sidebar.php');
$this->need('topbar.php');
if ($this->fields->navigation == 2):
$this->need('post-modal.php');
endif;
?>
<div class="modal fade" id="openWxModal" tabindex="-1" aria-labelledby="openWxModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body d-flex flex-column justify-content-center align-items-center gap-3">
<?php if ($this->fields->url): ?>
<img src="<?php $this->fields->url(); ?>" alt="二维码" class="img-fluid mt-3">
<?php elseif ($this->fields->logo): ?>
<img src="<?php $this->fields->logo(); ?>" alt="logo" class="img-fluid mt-3">
<?php endif; ?>
<p class="modal-title mb-3 fs-5"><?php echo $this->title(); ?></p>
<p class="text-muted">Tips: 长按识别二维码, 或者去微信搜索:<b class="text-success"><?php echo $this->title(); ?></b></p>
<a class="btn btn-primary" href="weixin://" role="button">前往微信</a>
</div>
</div>
</div>
</div>
endif; ?>


<main class="site-main">
Expand Down
69 changes: 0 additions & 69 deletions templates_timeline.php

This file was deleted.

71 changes: 71 additions & 0 deletions timeline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* 目录/时间线
*
* @package custom
*/

if (!defined("__TYPECHO_ROOT_DIR__")) {
exit();
}
$this->need("header.php");
$this->need("sidebar.php");
$this->need("topbar.php");
?>

<main class="site-main">
<div class="container">
<div class="container">
<div class="row gx-3 gx-md-4">
<div class="post card card-md mb-3 mb-md-4">
<div class="post-other-style">
<div class="post-heading text-center pt-5 pt-md-5 pb-3 pb-xl-4">
<h1 class="post-title"> <?php $this->title(); ?></h1>
</div>
<div class="card-body">
<div class="post-content">
<?php
$pageSize = $this->options->pageSize;
$currentPage = isset($_GET['page']) ? intval($_GET['page']) : 1;
$result = Utils::page($pageSize, $currentPage);
?>
<div class="timeline">
<?php foreach ($result['data'] as $cid): ?>
<?php $post = Helper::widgetById('Contents', $cid); ?>
<div class="timeline-element">
<div>
<span class="timeline-element-icon">
<i class="badge badge-dot">
<img src="<?php $this->options->themeUrl('/assets/image/default.gif'); ?>" data-src="<?php echo Utils::favicon($post); ?>" class="media-content lazyload" />
</i>
</span>
<div class="timeline-element-content">
<h4 class="timeline-title">
<a href="<?php $post->permalink(); ?>"><?php $post->title(); ?></a>
</h4>
<p><?php if ($post->fields->text) $post->fields->text();
else $post->excerpt(80, '...'); ?></p>
<span class="timeline-element-date"><?php echo date('m-d, Y', $post->modified); ?></span>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<div class="post-actions row g-2 mt-4">
</div>
</div>
</div>
</div>
</div>

<?php
$pageLink = $this->permalink . '?page=';
$currentPage = $result['currentPage'];
$totalPages = $result['totalPages'];
echo Utils::pagination($pageLink, $currentPage, $totalPages); ?>
</div>
</div>
</main>
<?php $this->need('footer.php'); ?>

0 comments on commit 249da40

Please sign in to comment.