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

経過時間の表示の追加 #216

Open
wants to merge 4 commits into
base: main-2023
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion lib/posts-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
const util = require('./handler-util');

const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');
const relativeTime = require('dayjs/plugin/relativeTime');
require('dayjs/locale/ja');
dayjs.locale('ja');
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(relativeTime);
dayjs.tz.setDefault('Asia/Tokyo');

async function handle(req, res) {
switch (req.method) {
case 'GET':
Expand All @@ -17,6 +28,8 @@ async function handle(req, res) {
});
posts.forEach((post) => {
post.content = post.content.replace(/\n/g, '<br>');
post.relativeCreatedAt = dayjs(post.createdAt).tz().fromNow();
post.formattedCreatedAt = dayjs(post.createdAt).tz().format('YYYY年MM月DD日 HH時mm分ss秒');
});
res.end(pug.renderFile('./views/posts.pug', { posts, user: req.user }));
console.info(
Expand Down Expand Up @@ -89,4 +102,4 @@ function handleDelete(req, res) {
module.exports = {
handle,
handleDelete
};
};
8 changes: 7 additions & 1 deletion public/nn-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ function isPressedSubmitKey(event) {
if (event.metaKey) {
return true;
}
}
}

// ツールチップの有効化
const tooltipTriggerElements = document.querySelectorAll('[data-bs-toggle="tooltip"]');
tooltipTriggerElements.forEach((tooltipTriggerElement) => {
new bootstrap.Tooltip(tooltipTriggerElement);
});
5 changes: 3 additions & 2 deletions views/posts.pug
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ html(lang="ja")
h5.card-title #{post.postedBy}
if post.postedBy === 'admin'
i.bi-patch-check-fill.ms-1
small.card-text.text-muted.float-end #{post.createdAt}
- const tooltipTitle = `<small>${post.formattedCreatedAt}</small>`
small.card-text.text-muted.float-end(data-bs-toggle="tooltip" data-bs-placement="top" data-bs-html="true" title=tooltipTitle) #{post.relativeCreatedAt}
p.card-text.lead!= post.content
- const isDeletable = (user === post.postedBy || user === 'admin')
if isDeletable
Expand All @@ -49,4 +50,4 @@ html(lang="ja")
script(src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous")
script(src="nn-chat.js")
script(src="nn-chat.js")