フォーマット拡張

この記事では、フォーマット拡張プラグインの基本的な概念を説明します。

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

"preview": {}

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

"preview" : {
    "icns": {}
}

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

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

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

  1. "thumbnail.path":拡張したいファイル形式のサムネイルを解析するための .js ファイルを提供します。

  2. "viewer.path":拡張したい形式のプレビュー用の .html ファイルを提供します。

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

他の metadata.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"
            }
        }
    }
}
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);
        }
    });
}
<!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>

注意:現在、フォーマット拡張プラグインは、Eagle Plugin API および DevTools のデバッグ機能をサポートしていません。

最后更新于