Skip to content

Commit 33bd5fb

Browse files
[PowerPoint] (Overview) Add active view scenario (#5224)
1 parent 93916d1 commit 33bd5fb

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

docs/powerpoint/powerpoint-add-ins.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,79 @@ reader.readAsDataURL(myFile.files[0]);
152152

153153
To see a full code sample that includes an HTML implementation, see [Create presentation](https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/document/create-presentation.yaml).
154154

155+
## Detect the presentation's active view and handle the ActiveViewChanged event
156+
157+
If you're building a [content add-in](../design/content-add-ins.md), you'll need to get the presentation's active view and handle the [Document.ActiveViewChanged](/javascript/api/office/office.eventtype#fields) event as part of your [Office.onReady](/javascript/api/office#office-office-onready-function(1)) call.
158+
159+
> [!NOTE]
160+
> In PowerPoint on the web, the `Document.ActiveViewChanged` event will never fire because **Slide Show** mode is treated as a new session. In this case, the add-in must fetch the active view on load, as shown in the following code sample.
161+
162+
Note the following about the code sample:
163+
164+
- The `getActiveFileView` function calls the [Document.getActiveViewAsync](/javascript/api/office/office.document#office-office-document-getactiveviewasync-member(1)) method to return whether the presentation's current view is "edit" (any of the view where you can edit slides, such as **Normal**, **Slide Sorter**, or **Outline**) or "read" (**Slide Show** or **Reading View**), represented by the [ActiveView](/javascript/api/office/office.activeview) enum.
165+
- The `registerActiveViewChanged` function calls the [Document.addHandlerAsync](/javascript/api/office/office.document#office-office-document-addhandlerasync-member(1)) method to register a handler for the `Document.ActiveViewChanged` event.
166+
- To display information, this example uses the `showNotification` function, which is included in the Visual Studio Office Add-ins project templates. If you aren't using Visual Studio to develop your add-in, you'll need to replace the `showNotification` function with your own code.
167+
168+
```js
169+
// General Office.onReady function. Called after the add-in loads and Office JS is initialized.
170+
Office.onReady(() => {
171+
// Get whether the current view is edit or read.
172+
const currentView = getActiveFileView();
173+
174+
// Register the active view changed handler.
175+
registerActiveViewChanged();
176+
177+
// Render the content based off of the current view.
178+
if (currentView === Office.ActiveView.Read) {
179+
// Handle read view.
180+
console.log('Current view is read.');
181+
// You can add any specific logic for the read view here.
182+
} else {
183+
// Handle edit view.
184+
console.log('Current view is edit.');
185+
// You can add any specific logic for the edit view here.
186+
}
187+
});
188+
189+
// Gets the active file view.
190+
function getActiveFileView() {
191+
console.log('Getting active file view...');
192+
Office.context.document.getActiveViewAsync(function (result) {
193+
if (result.status === Office.AsyncResultStatus.Succeeded) {
194+
console.log('Active view:', result.value);
195+
return result.value;
196+
} else {
197+
console.error('Error getting active view:', result.error.message);
198+
showNotification('Error:', result.error.message);
199+
return null;
200+
}
201+
});
202+
}
203+
204+
// Registers the ActiveViewChanged event.
205+
function registerActiveViewChanged() {
206+
console.log('Registering ActiveViewChanged event handler...');
207+
Office.context.document.addHandlerAsync(
208+
Office.EventType.ActiveViewChanged,
209+
activeViewHandler,
210+
function (result) {
211+
if (result.status === Office.AsyncResultStatus.Failed) {
212+
console.error('Failed to register active view changed handler:', result.error.message);
213+
showNotification('Error:', result.error.message);
214+
} else {
215+
console.log('Active view changed handler registered successfully.');
216+
}
217+
});
218+
}
219+
220+
// ActiveViewChanged event handler.
221+
function activeViewHandler(eventArgs) {
222+
console.log('Active view changed:', JSON.stringify(eventArgs));
223+
showNotification('Active view changed', `The active view has changed to: ${eventArgs.activeView}`);
224+
// You can add logic here based on the new active view.
225+
}
226+
```
227+
155228
## See also
156229

157230
- [Developing Office Add-ins](../develop/develop-overview.md)

0 commit comments

Comments
 (0)