从我在 Sketch 中预定义的阴影在对象上实现阴影
Implement shadow on the object from my predefine shadows in Sketch
我不是 javascript 的专家,但我想通过我的预定义阴影在对象上实现阴影,但代码不起作用:
var selection = context.selection;
for (var i=0; i < selection.count(); i++){
var layer = selection.objectAtIndex(i)
if (layer.isEmpty) UI.message('No layers selected!');
else {
var sha_0 = [];
var sha_1 = [
color: '#00000024',
x: 0,
y: 1,
blur: 1,
spread: 0
]
var sha_2 = [
color: '#00036024',
x: 0,
y: 1,
blur: 1,
spread: 0
]
var options = ['0dp', '1dp', '2dp'];
var selectDialog = sketch.UI.getSelectionFromUser("Please select shadow depth:", options);
if (selected == 0) {
layer.forEach(function (e) {
e.style.shadows = sha_0;
});
} else if (selected == 1) {
layer.forEach(function (e) {
e.style.shadows = sha_1;
});
} else if (selected == 2) {
layer.forEach(function (e) {
e.style.shadows = sha_2;
});
}
}
}
您需要调用的属于 UI 库的方法是异步的。他们依赖于用户在代码可以继续之前执行一些操作。在 Javascript 中,这需要回调函数或使用 async
/await
和 Promises。您正在使用的特定方法:sketch.UI.getSelectionFromUser
被描述为支持新的 UI.getInputFromUser
方法 as you can find here.
例如:
UI.getInputFromUser(
"What's your favorite design tool?",
{
type: UI.INPUT_TYPE.selection,
possibleValues: ['Sketch', 'Paper'],
},
(err, value) => {
if (err) {
// most likely the user canceled the input
return
}
}
)
因此,您的代码将如下所示:
// selected layers via document object per docs
var selection = document.selectedLayers;
// iterate directly over the selection layers
selection.forEach(layer => {
// These shadows are objects, not arrays, so changed [] to {}
var sha_0 = [];
var sha_1 = [{
color: '#00000024',
x: 0,
y: 1,
blur: 1,
spread: 0
}]
var sha_2 = [{
color: '#00036024',
x: 0,
y: 1,
blur: 1,
spread: 0
}]
var options = ['0dp', '1dp', '2dp'];
// Use the newer getInputFromUser method
UI.getInputFromUser(
"Please select shadow depth:",
{
type: UI.INPUT_TYPE.selection,
possibleValues: options,
},
(err, value) => {
if (err) {
// most likely the user canceled the input
return
}
switch (value) {
case options[0]:
layer.style.shadows = sha_0;
break;
case options[1]:
layer.style.shadows = sha_1;
break;
case options[2]:
layer.style.shadows = sha_2;
break;
}
}
)
})
您的代码可能会进一步简化,但这应该会让您再次前进。
我不是 javascript 的专家,但我想通过我的预定义阴影在对象上实现阴影,但代码不起作用:
var selection = context.selection;
for (var i=0; i < selection.count(); i++){
var layer = selection.objectAtIndex(i)
if (layer.isEmpty) UI.message('No layers selected!');
else {
var sha_0 = [];
var sha_1 = [
color: '#00000024',
x: 0,
y: 1,
blur: 1,
spread: 0
]
var sha_2 = [
color: '#00036024',
x: 0,
y: 1,
blur: 1,
spread: 0
]
var options = ['0dp', '1dp', '2dp'];
var selectDialog = sketch.UI.getSelectionFromUser("Please select shadow depth:", options);
if (selected == 0) {
layer.forEach(function (e) {
e.style.shadows = sha_0;
});
} else if (selected == 1) {
layer.forEach(function (e) {
e.style.shadows = sha_1;
});
} else if (selected == 2) {
layer.forEach(function (e) {
e.style.shadows = sha_2;
});
}
}
}
您需要调用的属于 UI 库的方法是异步的。他们依赖于用户在代码可以继续之前执行一些操作。在 Javascript 中,这需要回调函数或使用 async
/await
和 Promises。您正在使用的特定方法:sketch.UI.getSelectionFromUser
被描述为支持新的 UI.getInputFromUser
方法 as you can find here.
例如:
UI.getInputFromUser(
"What's your favorite design tool?",
{
type: UI.INPUT_TYPE.selection,
possibleValues: ['Sketch', 'Paper'],
},
(err, value) => {
if (err) {
// most likely the user canceled the input
return
}
}
)
因此,您的代码将如下所示:
// selected layers via document object per docs
var selection = document.selectedLayers;
// iterate directly over the selection layers
selection.forEach(layer => {
// These shadows are objects, not arrays, so changed [] to {}
var sha_0 = [];
var sha_1 = [{
color: '#00000024',
x: 0,
y: 1,
blur: 1,
spread: 0
}]
var sha_2 = [{
color: '#00036024',
x: 0,
y: 1,
blur: 1,
spread: 0
}]
var options = ['0dp', '1dp', '2dp'];
// Use the newer getInputFromUser method
UI.getInputFromUser(
"Please select shadow depth:",
{
type: UI.INPUT_TYPE.selection,
possibleValues: options,
},
(err, value) => {
if (err) {
// most likely the user canceled the input
return
}
switch (value) {
case options[0]:
layer.style.shadows = sha_0;
break;
case options[1]:
layer.style.shadows = sha_1;
break;
case options[2]:
layer.style.shadows = sha_2;
break;
}
}
)
})
您的代码可能会进一步简化,但这应该会让您再次前进。