Skip to content

Commit

Permalink
Rework powerpoint refresh function
Browse files Browse the repository at this point in the history
  • Loading branch information
jgreywolf committed Nov 30, 2023
1 parent 360a393 commit f207f42
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
38 changes: 17 additions & 21 deletions packages/office/src/lib/client/clientApi/PowerPointService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,30 @@ export class PowerPointService extends OfficeService {
}

public async syncDiagrams(): Promise<void> {
OfficeExtension.config.extendedErrorLogging = true;
await PowerPoint.run(async (context) => {
const presentation = context.presentation;
const slides = presentation.slides.load("items");

await context.sync();

for(let slideIndex = 0; slideIndex < slides.items.length; slideIndex++) {``
const slide = slides.getItemAt(slideIndex);
const shapes = slide.shapes.load('items');
const slides = context.presentation.slides.load("items");
await context.sync();

for (let shapeIndex = 0; shapeIndex < shapes.items.length; shapeIndex++) {
const shape = shapes.getItemAt(shapeIndex).load('tags/items');

for (let i = 0; i < slides.items.length; i++) {
const currentSlide = slides.items[i];
const shapes = currentSlide.shapes.load("items");
await context.sync();

try{
const diagramTag = shape.tags.getItem(C.TokenSettingName.toUpperCase()).load('key, value');;

for (let shapeIndex = 0; shapeIndex < shapes.items.length; shapeIndex++) {
const currentShape = shapes.items[shapeIndex];
const tags = currentShape.tags.load("key, value");
await context.sync();
if(diagramTag) {
shape.delete();
await this.replaceExistingDiagram(diagramTag.value);

for (let tagIndex = 0; tagIndex < tags.items.length; tagIndex++) {
const currentTag = tags.items[tagIndex];

if (currentTag.key === C.TokenSettingName.toUpperCase()) {
currentShape.delete();
await this.replaceExistingDiagram(currentTag.value);
}
}
} catch (error) {
throw new RefreshError(`Error encountered while updating diagrams:`, error as Error);
}
}
}
});
}

Expand Down
18 changes: 9 additions & 9 deletions packages/office/src/lib/client/clientApi/WordService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class WordService extends OfficeService {
}

public async syncDiagrams() {
const contentControlsToUpdate = await this.getContentControls().catch((error) => {
const controlTagsToUpdate = await this.getContentControlTagsToUpdate().catch((error) => {
if(error instanceof ContentControlsNotFoundError) {
showUserMessage(
'No diagrams found in document',
Expand All @@ -63,10 +63,10 @@ export class WordService extends OfficeService {
}
});

if(contentControlsToUpdate) {
if(controlTagsToUpdate) {
try {
for (const control of contentControlsToUpdate) {
await this.replaceExistingDiagram(control);
for (const tag of controlTagsToUpdate) {
await this.replaceExistingDiagram(tag);
}
} catch (error) {
if(error instanceof RefreshError) {
Expand All @@ -81,8 +81,8 @@ export class WordService extends OfficeService {
}
}

private async getContentControls() {
const contentControlsList: string[] = [];
private async getContentControlTagsToUpdate() {
const tagList: string[] = [];

try {
await Word.run(async (context) => {
Expand All @@ -93,7 +93,7 @@ export class WordService extends OfficeService {
for (let i = 0; i < contentControls.items.length; i++) {
const tag = contentControls.items[i].tag;
if(tag.startsWith(C.TokenSettingName)) {
contentControlsList.push(contentControls.items[i].tag);
tagList.push(contentControls.items[i].tag);
}
}
});
Expand All @@ -102,11 +102,11 @@ export class WordService extends OfficeService {
throw new Error('unknown error getting content controls');
}

if(contentControlsList.length === 0) {
if(tagList.length === 0) {
throw new ContentControlsNotFoundError();
}

return contentControlsList;
return tagList;
}

private async replaceExistingDiagram(tag: string) {
Expand Down

0 comments on commit f207f42

Please sign in to comment.