-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcode.ts
171 lines (159 loc) · 7.18 KB
/
code.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// This plugin will open a modal to prompt the user to enter a number, and
// it will then create that many rectangles on the screen.
// This file holds the main code for the plugins. It has access to the *document*.
// You can access browser APIs in the <script> tag inside "ui.html" which has a
// full browser enviroment (see documentation).
// This shows the HTML page in "ui.html".
figma.showUI(__html__)
// Calls to "parent.postMessage" from within the HTML page will trigger this
// callback. The callback will be passed the "pluginMessage" property of the
// posted message.
const storageKey = 'settingsData'
const defaultDisplayType = 'display-type-tile'
const defaultSymbolType = 'symbol-type-sfsymbols'
const defaultSettingsData = { clickAction: 'create', displayType: defaultDisplayType, symbolType: defaultSymbolType, windowHeight: 600, fontSize: 40 }
var settingsData = JSON.parse(JSON.stringify(defaultSettingsData));
var textObjectLength = 0
var windowWidth = 370
init()
function init(){
figma.clientStorage.getAsync(storageKey).then(result => {
if (result){
Object.keys(defaultSettingsData).forEach((key) => {
let data = JSON.parse(result)
settingsData[key] = data[key]
if(!settingsData[key]){
settingsData[key] = defaultSettingsData[key]
}
});
figma.clientStorage.setAsync(storageKey, JSON.stringify(settingsData))
} else {
figma.clientStorage.setAsync(storageKey, JSON.stringify(defaultSettingsData))
settingsData = defaultSettingsData
}
figma.ui.resize(windowWidth, parseInt(settingsData.windowHeight))
figma.ui.postMessage({ settings : true, data : settingsData })
})
}
function pasteFunction(nodeObjectsArray, copiedText, symbolType, symbolStyle){
if (nodeObjectsArray.length){
for (let i = 0; i < nodeObjectsArray.length; i++) {
if(nodeObjectsArray[i].type == 'TEXT'){
updateText(nodeObjectsArray[i], copiedText, symbolType, symbolStyle)
textObjectLength++
}
}
if (textObjectLength == 0){
// none
createTextAndPaste(copiedText, symbolType, symbolStyle)
textObjectLength++
figma.notify('Copy & Create symbol glyph object!')
}else{
figma.notify('Copy & Paste symbol glyph to selected text objects!')
}
}else{
createTextAndPaste(copiedText, symbolType, symbolStyle)
figma.notify('Create symbol glyph object!')
}
return textObjectLength
}
function createFunction(copiedText, symbolType, symbolStyle){
// console.log('createFunction')
createTextAndPaste(copiedText, symbolType, symbolStyle)
return textObjectLength
}
async function updateText(selectedItem, pasteValue, symbolType, symbolStyle) {
let selectedItemFontName = selectedItem.getRangeFontName(0, 1)
let textStyleId = selectedItem.getRangeTextStyleId(0, 1)
if(selectedItemFontName.family == 'SF Pro Display' || selectedItemFontName.family == 'SF Compact Display'){
if(symbolType == "material-icons"){
let tempFontName = fontFamilyAndStyle(symbolType, symbolStyle)
await figma.loadFontAsync({ family: tempFontName.family, style: tempFontName.style })
selectedItem.setRangeFontName(0, selectedItem.characters.length, tempFontName)
}else if(symbolType == "font-awesome"){
let tempFontName = fontFamilyAndStyle(symbolType, symbolStyle)
await figma.loadFontAsync({ family: tempFontName.family, style: tempFontName.style })
selectedItem.setRangeFontName(0, selectedItem.characters.length, tempFontName)
}else{
await figma.loadFontAsync({ family: selectedItemFontName.family, style: selectedItemFontName.style })
}
}else{
let tempFontName = fontFamilyAndStyle(symbolType, symbolStyle)
await figma.loadFontAsync({ family: tempFontName.family, style: tempFontName.style })
selectedItem.setRangeFontName(0, selectedItem.characters.length, tempFontName)
}
if(textStyleId){
selectedItem.setRangeTextStyleId(0, selectedItem.characters.length, textStyleId)
}else{
selectedItem.setRangeFontSize(0, selectedItem.characters.length, selectedItem.getRangeFontSize(0, 1))
selectedItem.setRangeTextCase(0, selectedItem.characters.length, selectedItem.getRangeTextCase(0, 1))
selectedItem.setRangeTextDecoration(0, selectedItem.characters.length, selectedItem.getRangeTextDecoration(0, 1))
selectedItem.setRangeLetterSpacing(0, selectedItem.characters.length, selectedItem.getRangeLetterSpacing(0, 1))
selectedItem.setRangeLineHeight(0, selectedItem.characters.length, selectedItem.getRangeLineHeight(0, 1))
}
if(selectedItem.getRangeFillStyleId(0, 1)){
selectedItem.setRangeFillStyleId(0, selectedItem.characters.length, selectedItem.getRangeFillStyleId(0, 1))
}else{
selectedItem.setRangeFills(0, selectedItem.characters.length, selectedItem.getRangeFills(0, 1))
}
selectedItem.characters = pasteValue
}
async function createTextAndPaste(pasteValue, symbolType, symbolStyle) {
let tempFontName = fontFamilyAndStyle(symbolType, symbolStyle)
await figma.loadFontAsync({ family: tempFontName.family, style: tempFontName.style })
const newTextNode = figma.createText()
newTextNode.fontName = tempFontName
newTextNode.fontSize = Number(settingsData.fontSize)
newTextNode.characters = pasteValue
newTextNode.x = figma.viewport.center.x - (newTextNode.width / 2)
newTextNode.y = figma.viewport.center.y - (newTextNode.height / 2)
figma.currentPage.appendChild(newTextNode)
figma.currentPage.selection = [newTextNode]
return newTextNode;
}
function fontFamilyAndStyle(symbolType, symbolStyle){
let tempFontName = {family: '', style: ''}
if(symbolType == "material-icons"){
tempFontName.family = 'Material Icons'
tempFontName.style = "Regular"
}else if(symbolType == "sf-symbols"){
tempFontName.family = 'SF Pro Display'
tempFontName.style = "Regular"
}else if(symbolType == "font-awesome"){
if(symbolStyle == 'brands'){
tempFontName.family = 'Font Awesome 5 Brands'
tempFontName.style = "Regular"
}else if(symbolStyle == 'solid'){
tempFontName.family = 'Font Awesome 5 Free'
tempFontName.style = "Solid"
}else if(symbolStyle == 'regular'){
tempFontName.family = 'Font Awesome 5 Free'
tempFontName.style = "Regular"
}
}
return tempFontName
}
figma.ui.onmessage = message => {
if (message.copied) {
// console.log(settingsData.clickAction)
if (settingsData.clickAction == 'copy'){
figma.notify('Copy symbol glyph to clipboard!')
}
if (settingsData.clickAction == 'paste'){
let num = pasteFunction(figma.currentPage.selection, message.copiedGlyph, message.symbolType, message.symbolStyle)
textObjectLength = 0
}
if (settingsData.clickAction == 'create'){
// console.log(settingsData.clickAction)
let num = createFunction(message.copiedGlyph, message.symbolType, message.symbolStyle)
textObjectLength = 0
figma.notify('Create symbol glyph object!')
}
}else if(message.updatedSettingsData){
if(settingsData.windowHeight != message.updatedSettingsData.windowHeight){
figma.ui.resize(370, parseInt(message.updatedSettingsData.windowHeight))
}
settingsData = message.updatedSettingsData
figma.clientStorage.setAsync(storageKey, JSON.stringify(message.updatedSettingsData))
}
}