|
/* |
|
* Sample plugin code for Adobe XD. |
|
* |
|
* Visit http://adobexdplatform.com/ for API docs and more sample code. |
|
*/ |
|
|
|
const fs = require("uxp").storage.localFileSystem; |
|
var {Rectangle, Color} = require("scenegraph"); |
|
|
|
// global settings object |
|
let settings = {}; |
|
|
|
// file I/O is async, so these functions will be async as well |
|
async function loadSettings() { |
|
// get the plugin's data folder, where we can both |
|
// save and load information |
|
const folder = await fs.getDataFolder(); |
|
try { |
|
const file = await folder.getEntry("settings.json"); |
|
settings = JSON.parse(await file.read()); |
|
console.log(`Loaded settings, ${JSON.stringify(settings)}`) |
|
} catch (err) { |
|
console.log("No settings; using default"); |
|
settings = { h: 20, v: 20 }; |
|
} |
|
} |
|
|
|
async function saveSettings() { |
|
const folder = await fs.getDataFolder(); |
|
try { |
|
// Have to set `overwrite: true`, or we'd get |
|
// an error when we tried to create the file |
|
const file = await folder.createFile("settings.json", { |
|
overwrite: true |
|
}); |
|
await file.write(JSON.stringify(settings)); |
|
console.log("Saved settings"); |
|
} catch (err) { |
|
/* oops? */ |
|
console.log("Hmm. Something went wrong."); |
|
} |
|
} |
|
|
|
function createRectangle(selection, hPadding, vPadding) { |
|
var textNode = selection.items[0]; |
|
var bounds = textNode.boundsInParent; |
|
console.log(bounds); |
|
|
|
var shape = new Rectangle(); |
|
shape.width = bounds.width + 2 * hPadding |
|
shape.height = bounds.height + 2 * vPadding; |
|
shape.stroke = new Color("blue"); |
|
|
|
selection.insertionParent.addChild(shape); |
|
shape.placeInParentCoordinates( |
|
{x: 0, y: 0}, |
|
{x: bounds.x - hPadding, y: bounds.y - vPadding} |
|
); |
|
} |
|
|
|
let dialog; |
|
|
|
function createDialog() { |
|
dialog = document.createElement("dialog"); |
|
|
|
// We add a checkbox that lets us save settings |
|
dialog.innerHTML = ` |
|
<style> |
|
form { |
|
width: 400px; |
|
} |
|
.row { |
|
display: flex; |
|
align-items: center; |
|
} |
|
</style> |
|
<form method="dialog"> |
|
<h1>Create Button Frame</h1> |
|
<hr /> |
|
<div class="row"> |
|
<label class="row"> |
|
<span>V:</span> |
|
<input type="text" uxp-quiet="true" id="txtV" placeholder="V padding" /> |
|
</label> |
|
<label class="row"> |
|
<span>H:</span> |
|
<input type="text" uxp-quiet="true" id="txtH" placeholder="H padding" /> |
|
</label> |
|
</div> |
|
<footer> |
|
<label class="row"> |
|
<input type="checkbox" id="save"/> |
|
<span>Save as default</span> |
|
</label> |
|
<button id="cancel" type="reset" uxp-variant="secondary">Cancel</button> |
|
<button id="ok" type="submit" uxp-variant="cta">Apply</button> |
|
</footer> |
|
</form>`; |
|
|
|
// make sure the cancel button works :-) |
|
dialog.querySelector("#cancel").addEventListener("click", () => { |
|
dialog.close("reasonCanceled"); |
|
}) |
|
|
|
dialog.querySelector("#txtV").value = settings.v; |
|
dialog.querySelector("#txtH").value = settings.h; |
|
|
|
document.appendChild(dialog); |
|
} |
|
|
|
async function showDialog() { |
|
if (!dialog) { |
|
// load our defaults first |
|
await loadSettings(); |
|
|
|
// then create the dialog |
|
createDialog(); |
|
} |
|
|
|
const response = await dialog.showModal(); |
|
|
|
if (response !== 'reasonCanceled') { |
|
// user wants to create the rectangle! |
|
const { selection } = require("scenegraph"); |
|
|
|
// You can extract the values the user entered from the DOM |
|
// using querySelector and friends |
|
// NOTE: we should, of course, do validation here |
|
// but that's out of scope for now. |
|
const hPadding = Number(dialog.querySelector("#txtH").value); |
|
const vPadding = Number(dialog.querySelector("#txtV").value); |
|
|
|
// and now allow saving of our settings! |
|
const saveAsDefault = dialog.querySelector("#save").checked; |
|
if (saveAsDefault) { |
|
settings = { |
|
h: hPadding, |
|
v: vPadding |
|
}; |
|
await saveSettings(); |
|
} |
|
|
|
createRectangle(selection, hPadding, vPadding); |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = { |
|
commands: { |
|
myPluginCommand: showDialog |
|
} |
|
}; |