Created
November 23, 2024 04:46
-
-
Save hiloki/b208b031ce441af912b780abb777eef4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.figma.com/plugin-docs/api/properties/global-fetch/ | |
// https://www.figma.com/developers/api#post-comments-endpoint | |
const token = "YOUR_PERSONAL_ACCESS_TOKEN"; // トークンを設定 | |
const fileKey = "FILE_KEY"; // 対象ファイルのキーを設定 | |
const message = "Hello, this is a comment for the selected layer."; // コメント内容 | |
const selection = figma.currentPage.selection; | |
if (selection.length === 0) { | |
figma.notify("コメントを投稿するレイヤーを選択してください。"); | |
} else { | |
const layer = selection[0]; // 最初の選択レイヤーを対象とする | |
// レイヤーの位置情報を取得 | |
const x = layer.absoluteBoundingBox?.x || layer.x; | |
const y = layer.absoluteBoundingBox?.y || layer.y; | |
if (x === undefined || y === undefined || !layer.id) { | |
figma.notify("選択したレイヤーの情報が不足しています。"); | |
} else { | |
// コメントを投稿するAPIリクエスト | |
const url = `https://api.figma.com/v1/files/${fileKey}/comments`; | |
fetch(url, { | |
method: "POST", | |
headers: { | |
"X-FIGMA-TOKEN": token, | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify({ | |
message: message, | |
client_meta: { | |
node_id: layer.id, // レイヤーIDを関連付け | |
node_offset: { x: 0, y: 0 }, // オフセット値(必要に応じて調整可能) | |
}, | |
}), | |
}) | |
.then((response) => { | |
if (!response.ok) { | |
return response.json().then((errorData) => { | |
console.error("APIエラーの詳細:", errorData); | |
throw new Error(`HTTPエラー: ${response.status}`); | |
}); | |
} | |
return response.json(); | |
}) | |
.then((data) => { | |
console.log("コメントが正常に投稿されました:", data); | |
figma.notify("コメントを選択したレイヤーに投稿しました!"); | |
}) | |
.catch((error) => { | |
console.error("エラーが発生しました:", error); | |
figma.notify("コメントの投稿中にエラーが発生しました。"); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment