function createThemePreview(selection) {
const rect = new Rectangle();
rect.name = "themePreview";
rect.width = 866;
rect.height = 537;
rect.cornerRadii = {
topLeft: 0,
topRight: 0,
bottomLeft: 0,
bottomRight: 0,
};
rect.fill = new Color("#000000");
rect.stroke = new Color("#707070");
rect.strokeWidth = 1;
rect.moveInParentCoordinates(0, 156);
var imagefill;
imageFill().then(
(success => {
console.log("image fill success");
console.log(success);
imagefill = success;
}),
(failure => {
console.log("image fill failed");
})
);
rect.fill = imagefill;
selection.insertionParent.addChild(rect);
return rect;
}
Throws Error
Plugin Error: Plugin 07b2dfeb is not permitted to make changes from the background. Return a Promise to continue execution asynchronously.
at convertPluginErrorToString (plugins/PluginErrorUtil.js:1:198)
at internalFormatPluginError (plugins/PluginErrorUtil.js:1:503)
at internalReportPluginError (plugins/PluginErrorUtil.js:1:610)
at Object.reportPluginError (plugins/PluginErrorUtil.js:1:1015)
at Object.checkAllowedToEdit (plugins/ScenegraphGuard.js:1:1094)
at Rectangle.<anonymous> (plugins/ScenegraphWrappers.js:1:2286)
at createThemePreview (C:\Users\PramUkesh\AppData\Local\Packages\Adobe.CC.XD_adky2gkssdxte\LocalState\develop\07b2dfeb\main.js:334:15)
You’re adding something to the scenegraph in an async operation – you must make sure that there’s a complete promise chain returned to the menu handler in order to make changes to the scenegraph. If not, XD locks it down immediately after invoking your plugin.
module.exports = {
commands: {
someCommand: function(selection) {
return new Promise((resolve, reject) => {
// you can make changes as long as this promises is neither resolved nor rejected
// once resolved, the scenegraph becomes locked to changes from code
// if rejected, changes to the scenegraph will be undone completely
});
}
}
};
There’s no other way – once you start doing anything that involves an asynchronous operation (file IO, network IO, renditions), you’ll need to be in the land of promises, and one unbroken chain all the way back to the menu handler.
If async/await is easier for you to use, that works too:
module.exports = {
commands: {
someCommand: async function(selection) {
const doSomethingAsync = await someOtherAsyncFunction();
// can still do things to the scenegraph
// throw if you want them to be undone
}
}
}
(Async/await is built on top of promises, so XD can work with async functions too.)
I tried promise function, still not able to fix “xxxx is not permitted to make changes from the background. Return a Promise to continue execution asynchronously.”
Can you please give an example to handle this problem
Hey steve,
Thank you for quick response. Still not luck to solve this problem. I’m not too aware of promise/await/async fx.
Can you please check this code?
// Get Rectangle and oval Shapes
async function fillImages(selection, allImages) {
var imageCount = allImages.length;
if (imageCount != 0 && imageCount >= selection.items.length) {
for (var i = 0; i < selection.items.length; i++) {
if (selection.items[i] instanceof scenegraph.Rectangle || selection.items[i] instanceof scenegraph.Ellipse) {
try {
var imgObj = await getImage(allImages[i]);
fillSelectionWithImage(selection.items[i], imgObj);
} catch (e) {
console.log(e);
}
} else {
console.log('please select elipse or rectangle');
}
}
} else {
console.log('too many selection');
}
}
// Check image
function getImage(selectedImage) {
return new Promise((resolve, reject) => {
if (selectedImage) {
try {
const image = selectedImage;
resolve(image);
} catch (e) {
reject('could not load image')
}
} else {
reject('had an error')
}
});
}
// Fill selected shape with image
function fillSelectionWithImage(selectedPath, image) {
const imageFill = new ImageFill(image);
selectedPath.fill = imageFill;
}
According to me, the logo should just fit in the container, independent of the size and shape of the container (be it square or vertical/horizontal rectangles)
Just picked height of rectangle/container and change the width of container respect to the ratio of original logotype. (Problem: If have group of container/rectangle, will stumbles )