-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPaintCurrentUserAvatarCommandHandler.ts
More file actions
99 lines (80 loc) · 3.34 KB
/
PaintCurrentUserAvatarCommandHandler.ts
File metadata and controls
99 lines (80 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { NetworkRequestCommand } from "../../browser-commands/network-request/NetworkRequestCommand";
import { CommandHandler } from "../../commands-setup/CommandHandler";
import { executeCommand } from "../../commands-setup/executeCommand";
import { PaintCurrentUserAvatarCommand } from "./PaintCurrentUserAvatarCommand";
export class PaintCurrentUserAvatarCommandHandler
implements CommandHandler<PaintCurrentUserAvatarCommand>
{
private readonly avatarImageSize = 100;
constructor(private readonly figma: PluginAPI) {}
// `command` argument needed due to polymorphism.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
handle(command: PaintCurrentUserAvatarCommand): Promise<void> {
const currentUserAvatarUrl = this.figma.currentUser?.photoUrl;
const currentUserName = this.figma.currentUser?.name;
if (currentUserAvatarUrl === undefined || currentUserAvatarUrl === null) {
this.figma.notify("Sorry but you do not have an avatar to add 😅");
return Promise.resolve();
}
const responseType = "arraybuffer";
executeCommand(
new NetworkRequestCommand(currentUserAvatarUrl, responseType)
);
return new Promise((resolve) => {
this.figma.ui.onmessage = async (command) => {
this.ensureToOnlyReceiveNetworkRequestResponse(command);
await this.createAvatarBadge(
command.payload as ArrayBuffer,
currentUserName as string
);
resolve();
};
});
}
private ensureToOnlyReceiveNetworkRequestResponse(command: { type: string }) {
if (command.type !== "networkRequestResponse") {
const errorMessage =
"Unexpected command received while performing the request for painting the user avatar.";
throw new Error(errorMessage);
}
}
private async createAvatarBadge(
imageBuffer: ArrayBuffer,
userName: string
): Promise<void> {
const avatarImage = this.createAvatarImage(imageBuffer, userName);
const userNameText = await this.createAvatarText(userName);
const elementsToFocus = [avatarImage, userNameText];
this.figma.currentPage.selection = elementsToFocus;
this.figma.viewport.scrollAndZoomIntoView(elementsToFocus);
}
private createAvatarImage(
avatarImage: ArrayBuffer,
currentUserName: string
): EllipseNode {
const imageUint8Array = new Uint8Array(avatarImage);
const figmaImage = this.figma.createImage(imageUint8Array);
const imageWrapper = this.figma.createEllipse();
imageWrapper.x = this.figma.viewport.center.x;
imageWrapper.y = this.figma.viewport.center.y;
imageWrapper.resize(this.avatarImageSize, this.avatarImageSize);
imageWrapper.fills = [
{ type: "IMAGE", scaleMode: "FILL", imageHash: figmaImage.hash },
];
imageWrapper.name = `${currentUserName} avatar`;
this.figma.currentPage.appendChild(imageWrapper);
return imageWrapper;
}
private async createAvatarText(userName: string): Promise<TextNode> {
const userNameText = this.figma.createText();
userNameText.x = this.figma.viewport.center.x - userName.length / 2;
userNameText.y =
this.figma.viewport.center.y +
this.avatarImageSize +
this.avatarImageSize / 12;
await this.figma.loadFontAsync(userNameText.fontName as FontName);
userNameText.characters = userName;
userNameText.fontSize = 14;
return userNameText;
}
}