Skip to content

Commit

Permalink
Cookbook の翻訳を追従 (#394)
Browse files Browse the repository at this point in the history
* Debugging in VS Code Cookbook Recipe

vuejs/docs@572b3cc#diff-b0e55b2e7066b952316301f7d1ca92884bcf4587ecb8899b43161ec6754e3fb3

* fix: change the language on some code blocks to match the code

vuejs/docs@0391e2e#diff-b0e55b2e7066b952316301f7d1ca92884bcf4587ecb8899b43161ec6754e3fb3

* docs: use local copies of images rather than external URLs

vuejs/docs@4b0baab#diff-b0e55b2e7066b952316301f7d1ca92884bcf4587ecb8899b43161ec6754e3fb3

* docs: add automatic global registration to cookbook

vuejs/docs@6de305a#diff-b0e55b2e7066b952316301f7d1ca92884bcf4587ecb8899b43161ec6754e3fb3

* fix: markdown syntax

* docs: translate cookbook > editable svg icon systems

* docs: translate cookbook > debugging in vscode

* docs: translate cookbook > automatic global registration of base components
  • Loading branch information
naokie committed Jun 17, 2021
1 parent 53fb728 commit 1d7aa24
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const sidebar = {
children: [
'/cookbook/',
'/cookbook/editable-svg-icons',
// '/cookbook/debugging-in-vscode'
'/cookbook/debugging-in-vscode',
'/cookbook/automatic-global-registration-of-base-components'
]
}
],
Expand Down
Binary file added src/.vuepress/public/images/breakpoint_hit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/.vuepress/public/images/breakpoint_set.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/.vuepress/public/images/config_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions src/cookbook/automatic-global-registration-of-base-components.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# ベースコンポーネントの自動グローバル登録

## 基本的な例

多くのコンポーネントは割と一般的なもので、あるいは入力やボタンをラップするだけのものかもしれません。わたしたちは、このようなコンポーネントを [ベースコンポーネント](../style-guide/#base-component-names-strongly-recommended) と呼ぶことがあって、コンポーネント全体に渡ってとても頻繁に使われる傾向があります。


その結果、多くのコンポーネントはベースコンポーネントの長いリストに含まれていることがあります:

```js
import BaseButton from './BaseButton.vue'
import BaseIcon from './BaseIcon.vue'
import BaseInput from './BaseInput.vue'
export default {
components: {
BaseButton,
BaseIcon,
BaseInput
}
}
```

テンプレート内の比較的小さなマークアップをサポートするためだけのものです:

```html
<BaseInput v-model="searchText" @keydown.enter="search" />
<BaseButton @click="search">
<BaseIcon name="search" />
</BaseButton>
```

幸いなことに、webpack(または [Vue CLI](https://github.com/vuejs/vue-cli)、これは内部的に webpack を使っています)を使う場合、これらのとても汎用的なベースコンポーネントだけをグローバルに登録するのに `require.context` を使うことができます。このコード例は、アプリケーションのエントリファイル(例えば `src/main.js`)でベースコンポーネントをグローバルにインポートするのに使えるでしょう:

```js
import { createApp } from 'vue'
import upperFirst from 'lodash/upperFirst'
import camelCase from 'lodash/camelCase'
import App from './App.vue'

const app = createApp(App)

const requireComponent = require.context(
// コンポーネントフォルダの相対パス
'./components',
// サブフォルダ内を探すかどうか
false,
// ベースコンポーネントのファイル名とマッチさせるための正規表現
/Base[A-Z]\w+\.(vue|js)$/
)

requireComponent.keys().forEach(fileName => {
// コンポーネント設定の取得
const componentConfig = requireComponent(fileName)

// コンポーネントのパスカルケースでの名前を取得
const componentName = upperFirst(
camelCase(
// フォルダの深さに関わらずファイル名を取得
fileName
.split('/')
.pop()
.replace(/\.\w+$/, '')
)
)

app.component(
componentName,
// `.default` にあるコンポーネントのオプションを探す。
// コンポーネントが `export default` でエクスポートされていれば存在して、
// そうでなければモジュールのルートのフォールバックする。
componentConfig.default || componentConfig
)
})

app.mount('#app')
```
124 changes: 124 additions & 0 deletions src/cookbook/debugging-in-vscode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# VS Code でのデバッグ

あらゆるアプリケーションは、小さなものから大きなものまでエラーを理解する必要がある段階に到達します。このレシピでは、 VS Code ユーザがブラウザでアプリケーションをデバッグするためのワークフローをいくつか紹介します。

このレシピでは、 [Vue CLI](https://github.com/vuejs/vue-cli) アプリケーションをブラウザで実行しながら、 VS Code でデバッグする方法を紹介します。

## 必要なもの

VS Code と好みのブラウザがインストールされていて、対応するデバッガー拡張機能の最新バージョンがインストールされ、有効化されていることを確認してください:

- [Debugger for Chrome](https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome)
- [Debugger for Firefox](https://marketplace.visualstudio.com/items?itemName=hbenl.vscode-firefox-debug)

[Vue CLI ガイド](https://cli.vuejs.org/) の説明にしたがって、 [vue-cli](https://github.com/vuejs/vue-cli) をインストールしてプロジェクトを作成します。新しく作成したアプリケーションのディレクトリに移動して、 VS Code を開きます。

### ブラウザにソースコードを表示

VS Code で Vue コンポーネントをデバッグできるようにする前に、生成された Webpack の設定を更新してソースマップをビルドする必要があります。これはデバッガが圧縮ファイルの中のコードを、元のファイルの位置に一致させる方法を持つためです。これにより、アセットが Webpack で最適化された後でも、アプリケーションのデバッグが可能になります。

Vue CLI 2 を使っているならば、`config/index.js` 内の `devtool` プロパティを設定、または更新してください:

```json
devtool: 'source-map',
```

使っているのが Vue CLI 3 ならば、`vue.config.js` 内の `devtool` プロパティを設定、または更新してください:

```js
module.exports = {
configureWebpack: {
devtool: 'source-map',
},
}
```

### VS Code からアプリケーションを起動

::: info
ここではポートを `8080` と仮定します。そうでない場合(例えば `8080` が使われていて、Vue CLI が自動的に別のポートを選択したとき)は、適宜設定を変更してください。
:::

アクティビティバーのデバッグアイコンをクリックして、デバッグ表示に切り替え、歯車アイコンをクリックして launch.json ファイルを設定したら、環境として **Chrome/Firefox: Launch** を選択します。生成された launch.json の内容を対応する設定に置き換えます:

![Add Chrome Configuration](/images/config_add.png)

```json
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "vuejs: chrome",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}/src",
"breakOnLoad": true,
"sourceMapPathOverrides": {
"webpack:///src/*": "${webRoot}/*"
}
},
{
"type": "firefox",
"request": "launch",
"name": "vuejs: firefox",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}/src",
"pathMappings": [{ "url": "webpack:///src/", "path": "${webRoot}/" }]
}
]
}
```

## ブレークポイントの設定

1. **src/components/HelloWorld.vue**`line 90` にある `data` 関数が文字列を返す部分にブレークポイントを設定します。.

![Breakpoint Renderer](/images/breakpoint_set.png)

2. ルートフォルダでお気に入りのターミナルを開き、Vue CLI を使ってアプリケーションを配信します:

```
npm run serve
```

3. デバッグ表示に移動して、**'vuejs: chrome/firefox'** の設定を選択したら、F5 キーを押すか、緑色の再生ボタンをクリックします。

4. ブレークポイントに到達すると、新しいブラウザのインスタンスが `http://localhost:8080` を開きます。

![Breakpoint Hit](/images/breakpoint_hit.png)

## 代替パターン

### Vue Devtools

もっと複雑なデバッグの方法もあります。最も一般的で単純な方法は、優れた Vue.js devtools [Chrome 向け](https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd)[Firefox 向け](https://addons.mozilla.org/en-US/firefox/addon/vue-js-devtools/) を使うことです。Devtools を使うことのいくつかの利点は、データのプロパティを Live Edit(ライブエディット)して、その変更がすぐに反映されることです。その他の主な利点は、Vuex のタイムトラベルデバッグが可能になることです。

![Devtools Timetravel Debugger](/images/devtools-timetravel.gif)

Vue.js の本番向け・縮小化ビルド(CDN からの標準的なリンクなど)を使っているページでは、Devtools の検知がデフォルトで無効になっているため、Vue ペインが表示されないことに注意してください。縮小されていないバージョンに切り替えた場合は、それを表示するためにページのハード再読み込みが必要になるかもしれません。

### 単純な Debugger の記述

上記の例はすばらしいワークフローを持っています。しかし、[ネイティブの debugger 文](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/debugger) をコードの中で直接使うという別の方法もあります。この方法を選択した場合には、作業が終わったらこの debugger 文を削除することを忘れないようにするのが重要です。

```vue
<script>
export default {
data() {
return {
message: ''
}
},
mounted() {
const hello = 'Hello World!'
debugger
this.message = hello
}
};
</script>
```

## 謝辞

このレシピは、[Kenneth Auchenberg](https://twitter.com/auchenberg) 氏からの寄稿を元にしています。[元の記事](https://github.com/Microsoft/VSCode-recipes/tree/master/vuejs-cli)
64 changes: 32 additions & 32 deletions src/cookbook/editable-svg-icons.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# Editable SVG Icon Systems
# 編集可能な SVG アイコンシステム

## Base Example
## 基本的な例

There are many ways to create an SVG Icon System, but one method that takes advantage of Vue's capabilities is to create editable inline icons as components. Some of the advantages of this way of working is:
SVG アイコンシステムを作成する方法はいろいろありますが、 Vue の機能を生かした方法として、編集可能なインラインのアイコンをコンポーネントとして作成する方法があります。この方法のいくつかの利点としては:

- They are easy to edit on the fly
- They are animatable
- You can use standard props and defaults to keep them to a typical size or alter them if you need to
- They are inline, so no HTTP requests are necessary
- They can be made accessible dynamically
- 即座に編集することが簡単です
- アニメーションが可能です
- 標準的なプロパティとデフォルトを利用して標準サイズを保つことができ、必要に応じて変更することもできます
- インラインなので HTTP リクエストが不要です
- 動的にアクセスすることが可能です

First, we'll create a folder for all of the icons, and name them in a standardized fashion for easy retrieval:
まず、すべてのアイコンを入れるフォルダを作り、検索しやすいように一定のルールで命名します:

- `components/icons/IconBox.vue`
- `components/icons/IconCalendar.vue`
- `components/icons/IconEnvelope.vue`

Here's an example repo to get you going, where you can see the entire setup: [https://github.com/sdras/vue-sample-svg-icons/](https://github.com/sdras/vue-sample-svg-icons/)
ここにセットアップの全体像を見られるサンプルのリポジトリがあります: [https://github.com/sdras/vue-sample-svg-icons/](https://github.com/sdras/vue-sample-svg-icons/)

![Documentation site](https://s3-us-west-2.amazonaws.com/s.cdpn.io/28963/screendocs.jpg 'Docs demo')
![Documentation site](/images/editable-svg-icons.jpg 'Docs demo')

We'll create a base icon (`IconBase.vue`) component that uses a slot.
スロットを利用した基本となるアイコン(`IconBase.vue`)コンポーネントを作成します。

```html
<template>
Expand All @@ -40,9 +40,9 @@ We'll create a base icon (`IconBase.vue`) component that uses a slot.
</template>
```

You can use this base icon as is- the only thing you might need to update is the `viewBox` depending on the `viewBox` of your icons. In the base, we're making the `width`, `height`, `iconColor`, and name of the icon props so that it can be dynamically updated with props. The name will be used for both the `<title>` content and its `id` for accessibility.
アイコンの `viewBox` に応じて `viewBox` を更新する必要があるだけで、この基本となるアイコンをそのまま使うことができます。この基本では、 `width``height``iconColor` とアイコン名をプロパティにして、プロパティから動的に更新できるようにしています。名前は `<title>` のコンテンツと、アクセシビリティのための `id` の両方に使われます。

Our script will look like this, we'll have some defaults so that our icon will be rendered consistently unless we state otherwise:
スクリプト部分はこのようになります。いくつかのデフォルトがあり、特に指定しない限り、アイコンが一貫してレンダリングされるようにします:

```js
export default {
Expand All @@ -67,32 +67,32 @@ export default {
}
```

The `currentColor` property that's the default on the fill will make the icon inherit the color of whatever text surrounds it. We could also pass in a different color as a prop if we wish.
塗りつぶし色のデフォルト `currentColor` プロパティは、アイコンの周囲のテキスト色を継承します。必要なら、別の色をプロパティとして渡すこともできます。

We can use it like so, with the only contents of `IconWrite.vue` containing the paths inside the icon:
アイコンのパスを内包する `IconWrite.vue` だけを内容にすると、このように使えます:

```html
<icon-base icon-name="write"><icon-write /></icon-base>
```

Now, if we'd like to make many sizes for the icon, we can do so very easily:
さまざまなサイズのアイコンを作りたいとなったら、とても簡単にできます:

```html
<p>
<!-- you can pass in a smaller `width` and `height` as props -->
<!-- より小さい `width` `height` をプロパティとして渡せます -->
<icon-base width="12" height="12" icon-name="write"><icon-write /></icon-base>
<!-- or you can use the default, which is 18 -->
<!-- あるいはデフォルトも使えます。デフォルトは 18 です -->
<icon-base icon-name="write"><icon-write /></icon-base>
<!-- or make it a little bigger too :) -->
<!-- または少し大きくすることも :) -->
<icon-base width="30" height="30" icon-name="write"><icon-write /></icon-base>
</p>
```

<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/28963/Screen%20Shot%202018-01-01%20at%204.51.40%20PM.png" width="450" />
<img src="/images/editable-svg-icons-sizes.png" width="450" />

## Animatable Icons
## アニメーション可能なアイコン

Keeping icons in components comes in very handy when you'd like to animate them, especially on an interaction. Inline SVGs have the highest support for interaction of any method. Here's a very basic example of an icon that's animated on click:
アイコンをコンポーネントに入れておくと、特にインタラクションによってアニメーションさせたいときにとても便利です。インライン SVG は、いろいろなやり方の中で最もインタラクションをサポートします。これはクリックでアニメーションするアイコンのとても基本的な例です:

```html
<template>
Expand Down Expand Up @@ -141,27 +141,27 @@ export default {
}
```

We're applying `refs` to the groups of paths we need to move, and as both sides of the scissors have to move in tandem, we'll create a function we can reuse where we'll pass in the `refs`. The use of GreenSock helps resolve animation support and `transform-origin` issues across browser.
移動する必要のあるパスのグループに `refs` を適用します。また、はさみの両側は連動して動く必要があるので、 `refs` を渡す再利用可能な関数を作成します。 GreenSock を使うとアニメーションのサポートや、ブラウザ間の `transform-origin` の問題を解決することができます。

<common-codepen-snippet title="Editable SVG Icon System: Animated icon" slug="dJRpgY" :preview="false" :editable="false" version="2" theme="0" />

<p style="margin-top:-30px">Pretty easily accomplished! And easy to update on the fly.</p>

You can see more animated examples in the repo [here](https://github.com/sdras/vue-sample-svg-icons/)
他のアニメーションの例は、 [こちらの](https://github.com/sdras/vue-sample-svg-icons/) リポジトリで見ることができます。

## Additional Notes
## 補足

Designers may change their minds. Product requirements change. Keeping the logic for the entire icon system in one base component means you can quickly update all of your icons and have it propagate through the whole system. Even with the use of an icon loader, some situations require you to recreate or edit every SVG to make global changes. This method can save you that time and pain.
デザイナーの考えは変わるかもしれません。製品要件は変わります。アイコンシステム全体のロジックを 1 つの基本となるコンポーネントにまとめておけば、すべてのアイコンを素早く更新して、システム全体に広めることができます。アイコンローダーを使っても、場合によってはグローバルな変更を行うために、すべての SVG を再作成または編集しなければなりません。この方法ならば、そのような時間と苦痛からあなたを救うことができます。

## When To Avoid This Pattern
## このパターンを回避するケース

This type of SVG icon system is really useful when you have a number of icons that are used in different ways throughout your site. If you're repeating the same icon many times on one page (e.g. a giant table with a delete icon in each row), it might make more sense to have all of the sprites compiled into a sprite sheet and use `<use>` tags to load them.
このような SVG アイコンシステムは、サイト全体にさまざまな方法で使われているアイコンがたくさんある場合にとても便利です。1 つのページで同じアイコンを繰り返し使うならば(例えば、各行に削除アイコンのある巨大なテーブル)、すべてのスプライトをスプライトシートにコンパイルして `<use>` タグで読み込むほうが合理的でしょう。

## Alternative Patterns
## 代替パターン

Other tooling to help manage SVG icons includes:
SVG アイコンを管理するのに役立つ他のツールには、以下のものがあります:

- [svg-sprite-loader](https://github.com/kisenka/svg-sprite-loader)
- [svgo-loader](https://github.com/rpominov/svgo-loader)

These tools bundle SVGs at compile time, but make them a little harder to edit during runtime, because `<use>` tags can have strange cross-browser issues when doing anything more complex. They also leave you with two nested `viewBox` properties and thus two coordinate systems. This makes the implementation a little more complex.
これらのツールはコンパイル時に SVG をバンドルしますが、実行時にそれらを編集することが少し難しくなります。これは `<use>` タグが複雑な処理をする際に、おかしなクロスブラウザの問題を引き起こす可能性があるからです。2 つの入れ子になった `viewBox` プロパティがあるために、2 つの座標系が存在します。このため実装が少し複雑になります。
Loading

0 comments on commit 1d7aa24

Please sign in to comment.