# フォーマット拡張

フォーマット拡張プラグインの主な目的は、Eagle でまだサポートされていないファイル形式のプレビューを可能にすることです。他のタイプのプラグインと違って、フォーマット拡張プラグインは `manifest.json` の中で `main` 属性を定義する必要はありませんが、代わりに `preview` 属性を設定する必要があります。以下は例のコードです：

```json
"preview": {}
```

`preview` の中で拡張したいファイルの拡張子を定義することができます。例えば、Eagle で icns アイコン形式をサポートさせたい場合、 `"icns": {}` と入力できます：

```json
"preview" : {
    "icns": {}
}
```

また、複数の拡張子を設定する必要がある場合は、コンマ `,` を使用して異なる拡張子を区切って定義できます。例えば：

```json
"preview" : {
    "icns,ico": {}
}
```

フォーマット拡張プラグインは2つの部分に分けられます：

1. `"thumbnail.path"`：拡張したいファイル形式のサムネイルを解析するための `.js` ファイルを提供します。
2. `"viewer.path"`：拡張したい形式のプレビュー用の `.html` ファイルを提供します。

```json
"preview": {
    "icns": {
        "thumbnail": {
            "path": "thumbnail/icns.js",
            "size": 400,
            "allowZoom": false
        },
        "viewer": {
            "path": "viewer/icns.html"
        }
    }
}
```

他の `manifest.json` フィールドを設定した後、最終的なコードは以下のようになります：

```json
{
    "id": "LARSKLB8OTOC2",
    "version": "1.0.0",
    "platform": "all",
    "arch": "all",
    "name": "Preview Plugin",
    "logo": "/logo.png",
    "keywords": [
        "icns"
    ],
    "devTools": false,
    "preview": {
        "icns": {
            "thumbnail": {
                "path": "thumbnail/icns.js",
                "size": 400,
                "allowZoom": false
            },
            "viewer": {
                "path": "viewer/icns.html"
            }
        }
    }
}
```

```javascript
const fs = require('fs');
const icns = require('./../js/icns-util.js');
const imageSize = require('./../js/image-size.js');

module.exports = async ({ src, dest, item }) => {
    return new Promise(async (resolve, reject) => {
        try {
            // 1. 解析してサムネイルファイルをdestに生成
            await icns.icns2png(src, dest);
            let size = await imageSize(dest);

            // 2. 結果が正しいかチェック
            if (!fs.existsSync(dest) || size.width === 0) {
                return reject(new Error(`icnsファイルのサムネイル生成に失敗しました。`));
            }

            // 3. アイテムの寸法を更新
            item.height = size?.height || item.height;
            item.width = size?.width || item.width;

            // 4. 結果を返す
            return resolve(item);
        }
        catch (err) {
            return reject(err);
        }
    });
}
```

```html
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>ICNS Viewer</title>
    <style>
        html, body {
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
        #viewer {
            pointer-events: none;
            object-fit: contain;
            object-position: center;
            width: 100%;
            height: 100%;
            max-width: 100vw;
            max-height: 100vh;
        }
    </style>
</head>
<body>
    <img id="viewer"/>
    <script>
        const urlParams = new URLSearchParams(window.location.search);
        const id = urlParams.get('id');
        const filePath = urlParams.get('path');
        const width = urlParams.get('width');
        const height = urlParams.get('height');
        const theme = urlParams.get('theme');
        const lang = urlParams.get('lang');

        const viewer = document.querySelector('#viewer');

        // 1. Load the thumbnail image first
        // 👍 Avoid loading for too long, and UI has no content
        viewer.src = filePath.replace(".icns", "_thumbnail.png");

        // 2. Load the file and replace thumbnail
        (async function() {
            const icns = require('./../js/icns-util.js');
            let buffer = await icns.icns2buffer(filePath);
            let base64 = `data:image/png;base64,${buffer.toString('base64')}`;
            viewer.src = base64;
        })();
    </script>
</body>
</html>
```

{% hint style="warning" %}
注意：現在、フォーマット拡張プラグインは、Eagle Plugin API および DevTools のデバッグ機能をサポートしていません。
{% endhint %}

{% hint style="info" %}
**完全なサンプルコード：**\
<https://github.com/eagle-app/eagle-plugin-examples/tree/main/Preview>
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developer.eagle.cool/plugin-api/ja-jp/get-started/plugin-types/preview.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
