Skip to content

[TypeScript演習]Google拡張機能の課題追加 #80

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

aAnnaChiba
Copy link
Member

@aAnnaChiba aAnnaChiba commented Apr 25, 2025

内容

TypeScript 演習で、時間が余ったとき用の自由課題として、Google拡張機能の課題を追加しました。

問題案はQuestionProposal.mdに、答えのtsファイルはanswer.tsにあります。
問題の追加や、リファクタなど、指摘がありましたらレビューお願いします。

TODO

  • answer.tsをレビュー終了後に削除
  • QuestionProposal.mdをレビュー終了後に削除して、問題はスライドに移動

@aAnnaChiba aAnnaChiba self-assigned this Apr 25, 2025
@aAnnaChiba aAnnaChiba changed the title [WIP] [TypeScript演習]Google拡張機能の課題追加 [TypeScript演習]Google拡張機能の課題追加 Apr 25, 2025
@aYumaIto
Copy link
Contributor

aYumaIto commented Apr 25, 2025

スペルミスだと思いますが、 extention ではなく extension です。

以下を確認ください。

  • ディレクトリ名
  • typescript/src/chrome_extention/README.md
  • typescript/README.md


// TODO 削除ボタンの作成

bookmarkList.appendChild(title)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

複数の要素を描画するときに1つ1つ DOM に追加していくのはパフォーマンスに悪いので DocumentFragment を使用するようにしましょうか。
https://zenn.dev/toshihide2000/scraps/87957b8360cf84

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます 🙇
165516b で修正しましたが、このようなかたちであっていますか 🤔

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空の fragment が繰り返し append されるだけで無意味なコードになっちゃってます。

forEach の中で毎回画面が再描画されるのを避けようという話で、そのために

  1. 描画されていない(=documentのどこにも存在しない) fragment を作る(createDocumentFragment())
  2. forEach の中ではその fragment に append することで再描画を防ぐ
  3. 中身がすべて出来上がった後で、描画されている要素(bookmarkList) に fragment ごと append

とすることで1回の再描画で済まそう、ということです。

  const fragment = document.createDocumentFragment();
  // TODO forEachを使ってブックマークリストの要素を表示してみよう
  bookmarks.forEach((bookmark) => {
    // ...
    fragment.appendChild(title);
  });
  bookmarkList.appendChild(fragment);

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すいません、 QuenstionProposal.md の方だけ見てコメントしましたが、answer.ts の方は合ってますね。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

關さんのコメントと被りますが、 QuestionPropsal.mdforEach の中で bookmarkList.appendChild(fragment); が実行されているのでそこだけ修正ください。
answer.ts は OK です。

@@ -0,0 +1,130 @@
// // 問題
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

わざわざ全部コメントにしなくても良いんじゃないでしょうか。


*.log

.DS_Store
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit/optional) 細かいですが、テキストファイルの末尾には改行があるべきです。参考

min-width: 300px;
}
form {
margin-bottom: 10px;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit/optional) HTML/CSS研修でどう教えているかにも依りますが、margin-block-end 等の logical property を使うことが一般的には良いです。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTML/CSS 研修で論理プロパティについて触れていただいたので、ぜひ使っていただけると良いです。

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit/optional) JSXやXHTMLではないので、void element の / は不要です。

</form>
<ul id="bookmark-list"></ul>

<script src="dist/app.js"></script>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) 誤差レベルですが、 <head> タグの中で defer つけて書いたほうが読み込みが少し早くなります。


- [Q1. 型定義をする](#q1-型定義をする)
- [Q2. インターフェースの継承を使ってみる](#q2-インターフェースの継承を使ってみる)
- [Q3. オプショナルチェーンを使って、リファクタリングしてみる](#q3-オプショナルチェーンを使ってリファクタリングしてみる)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) ここのタイトルを修正し忘れているようです。

// TODO forEachを使ってブックマークリストの要素を表示してみよう
bookmarks.forEach((bookmark) => {
// タイトルとURLのリンクを作成
const title = document.createElement('li');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(want) このコメントと変数名だと、このタグにタイトルが入っていそうですが、実際はそうではないので違和感があります。item, listItem, li とかにリネームしたいです。

Comment on lines +134 to +141
// TODO: Q7. 新しいブックマークの作成をしてみる
const newBookmark: Bookmark = {
id: crypto.randomUUID(),
title,
url,
category: category || undefined,
createdAt: new Date(),
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(optional) 他の関数の分け方を見ると、ここも createBookmark(title, url, category) みたいに関数に分けたほうが自然に感じます。


// 初期化
const bookmarks = loadBookmarks();
document.addEventListener('DOMContentLoaded', () => renderBookmarks(bookmarks));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(want) 既に bookmarkList を取得した後なので、DOMContentLoaded イベントを待たずにここで即 renderBookmarks(bookmarks); と呼び出しても変わらないと思います。

defer にしとけばスクリプトの実行がDOMの構築後になりますし、これで特に問題ないはずです。

return {
root: "src",
build: {
outDir: "../dist",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(ask) tsconfig.jsonoutDir と階層がズレてるように見えますが、どういうことでしょう?

Comment on lines +4 to +5
export default defineConfig((opt) => {
return {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(want) opt が使われていないようです。

(optional/nit) return を省略した書き方にできます。ネストが1段減らせますが、前処理等が増える可能性を考えているならこのままでも良いです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants