-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
159 lines (154 loc) · 6.06 KB
/
main.js
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
const { Text } = require("scenegraph")
const { alert, error } = require("./lib/dialogs.js");
const clipboard = require("clipboard")
const application = require("application")
/**
* Textオブジェクトからフォントのデータを取得
* @param {*} item
*/
function getFontStyle (item) {
return `
font-style: ${item.fontFamily};
font-weight: ${item.fontStyle};
font-size: ${item.fontSize}px;
color: #${item.fill.value.toString(16).slice(2)};
letter-spacing: ${item.charSpacing / 1000}em;
line-height: ${item.lineSpacing / item.fontSize};
`
}
/**
* フォントのデータを全て取得
* @param {*} selection
* @param {*} documentRoot
*/
function getFontDataAll (selection, documentRoot) {
try {
if (selection.items.length == 0) {
if (application.appLanguage == 'ja') {
error('Convert font data to CSS', 'コピーに失敗しました! テキストを選択してください。')
} else {
error('Convert font data to CSS', 'Copy failed! Please select text.')
}
return
}
for (let i = 0; i < selection.items.length; i++) {
let item = selection.items[i]
if (item instanceof Text) {
const results = getFontStyle(item)
console.log(item.styleRanges)
console.log(results)
clipboard.copyText(results)
if (application.appLanguage == 'ja') {
alert('Convert font data to CSS', '全てのフォントスタイルのコピーに成功しました!')
} else {
alert('Convert font data to CSS', 'Successfully copied all font style!')
}
} else {
if (application.appLanguage == 'ja') {
error('Convert font data to CSS', 'コピーに失敗しました! テキストを選択してください。')
} else {
error('Convert font data to CSS', 'Copy failed! Please select text.')
}
}
}
} catch (e) {
if (application.appLanguage == 'ja') {
error('Convert font data to CSS', 'エラーが発生しました。', e)
} else {
error('Convert font data to CSS', 'An error has occured', e)
}
}
}
/**
* フォントのletter-spacingを計算して取得
* @param {*} selection
* @param {*} documentRoot
*/
function getCharSpacing (selection, documentRoot) {
try {
if (selection.items.length == 0) {
if (application.appLanguage == 'ja') {
error('Convert font data to CSS', 'コピーに失敗しました! テキストを選択してください。')
} else {
error('Convert font data to CSS', 'Copy failed! Please select text.')
}
return
}
for (let i = 0; i < selection.items.length; i++) {
let item = selection.items[i]
if (item instanceof Text) {
const result = item.charSpacing / 1000
const resultText = `letter-spacing: ${result}em;`
console.log(resultText)
clipboard.copyText(resultText)
if (application.appLanguage == 'ja') {
alert('Convert font data to CSS', 'letter-spacingのコピーに成功しました!')
} else {
alert('Convert font data to CSS', 'Successfully copied all font style!')
}
} else {
if (application.appLanguage == 'ja') {
error('Convert font data to CSS', 'コピーに失敗しました! テキストを選択してください。')
} else {
error('Convert font data to CSS', 'Copy failed! Please select text.')
}
}
}
} catch (e) {
if (application.appLanguage == 'ja') {
error('Convert font data to CSS', 'エラーが発生しました。', e)
} else {
error('Convert font data to CSS', 'An error has occured', e)
}
}
}
/**
* フォントのline-heightを計算して取得
* @param {*} selection
* @param {*} documentRoot
*/
function getLineHeight (selection, documentRoot) {
try {
if (selection.items.length == 0) {
if (application.appLanguage == 'ja') {
error('Convert font data to CSS', 'コピーに失敗しました! テキストを選択してください。')
} else {
error('Convert font data to CSS', 'Copy failed! Please select text.')
}
return
}
for (let i = 0; i < selection.items.length; i++) {
let item = selection.items[i]
if (item instanceof Text) {
const result = item.lineSpacing / item.fontSize;
const resultText = `line-height: ${result};`
console.log(resultText)
clipboard.copyText(resultText)
if (application.appLanguage == 'ja') {
alert('Convert font data to CSS', 'line-heightのコピーに成功しました!')
} else {
alert('Convert font data to CSS', 'Successfully copied line-height!')
}
} else {
if (application.appLanguage == 'ja') {
error('Convert font data to CSS', 'コピーに失敗しました! テキストを選択してください。')
} else {
error('Convert font data to CSS', 'Copy failed! Please select text.')
}
}
}
} catch (e) {
if (application.appLanguage == 'ja') {
error('Convert font data to CSS', 'エラーが発生しました。', e)
} else {
error('Convert font data to CSS', 'An error has occured', e)
}
}
}
module.exports = {
commands: {
getFontDataAll: getFontDataAll,
getCharSpacing: getCharSpacing,
getLineHeight: getLineHeight
}
}