Skip to content

Commit d7af303

Browse files
committed
Update code to allow for external update
1 parent 1956cb1 commit d7af303

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

Editor/Inspector/FigmaInspector.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void OnPersonalAccessTokenGUI()
106106
}
107107
void OnAssetGUI()
108108
{
109-
void Update(string assetPath, bool downloadImages)
109+
async void Update(string assetPath, bool downloadImages)
110110
{
111111
string folder;
112112
string relativeFolder;
@@ -142,7 +142,7 @@ IEnumerable<string> FontsDirs()
142142
yield return fontsDir.stringValue;
143143
}
144144

145-
UpdateTitle(document, (Figma)target, title.stringValue, folder, relativeFolder, Event.current.modifiers == EventModifiers.Control, downloadImages, FontsDirs());
145+
await UpdateTitleAsync(document, (Figma)target, title.stringValue, folder, relativeFolder, Event.current.modifiers == EventModifiers.Control, downloadImages, FontsDirs());
146146
}
147147
}
148148

@@ -202,7 +202,31 @@ void OnFigmaGUI()
202202
#endregion
203203

204204
#region Support Methods
205-
static async void UpdateTitle(UIDocument document, Figma figma, string title, string folder, string relativeFolder, bool systemCopyBuffer, bool downloadImages, IEnumerable<string> fontDirs)
205+
static async Task UpdateDocumentAsync(UIDocument document, Figma figma, string title, bool downloadImages, bool systemCopyBuffer, IEnumerable<string> fontDirs)
206+
{
207+
string folder;
208+
string relativeFolder;
209+
string assetPath = AssetDatabase.GetAssetPath(document.visualTreeAsset);
210+
211+
if (assetPath.NullOrEmpty())
212+
throw new NotSupportedException();
213+
214+
if (assetPath.StartsWith("Packages"))
215+
{
216+
PackageInfo packageInfo = PackageInfo.FindForAssetPath(assetPath);
217+
folder = $"{packageInfo.resolvedPath}{Path.GetDirectoryName(assetPath.Replace(packageInfo.assetPath, ""))}";
218+
relativeFolder = Path.GetDirectoryName(assetPath);
219+
}
220+
else
221+
{
222+
folder = Path.GetDirectoryName(assetPath);
223+
relativeFolder = Path.GetRelativePath(Directory.GetCurrentDirectory(), folder);
224+
}
225+
226+
if (folder.NotNullOrEmpty())
227+
await UpdateTitleAsync(document, figma, title, folder, relativeFolder, systemCopyBuffer, downloadImages, fontDirs);
228+
}
229+
static async Task UpdateTitleAsync(UIDocument document, Figma figma, string title, string folder, string relativeFolder, bool systemCopyBuffer, bool downloadImages, IEnumerable<string> fontDirs)
206230
{
207231
if (!Directory.Exists(Path.Combine(folder, "Images"))) Directory.CreateDirectory(Path.Combine(folder, "Images"));
208232
if (!Directory.Exists(Path.Combine(folder, "Elements"))) Directory.CreateDirectory(Path.Combine(folder, "Elements"));
@@ -213,7 +237,7 @@ static async void UpdateTitle(UIDocument document, Figma figma, string title, st
213237
int progress = Progress.Start(processName, default, Progress.Options.Managed);
214238
Progress.RegisterCancelCallback(progress, () =>
215239
{
216-
if (cancellationToken is not null) cancellationToken.Cancel();
240+
cancellationToken.Cancel();
217241
return true;
218242
});
219243

0 commit comments

Comments
 (0)