Skip to content

feat: expose setZoom and getZoom API on SuperDoc#1498

Closed
tupizz wants to merge 5 commits intosuperdoc-dev:mainfrom
tupizz:feat/expose-zoom-api
Closed

feat: expose setZoom and getZoom API on SuperDoc#1498
tupizz wants to merge 5 commits intosuperdoc-dev:mainfrom
tupizz:feat/expose-zoom-api

Conversation

@tupizz
Copy link
Copy Markdown
Contributor

@tupizz tupizz commented Dec 12, 2025

Demo

CleanShot.2026-02-19.at.13.33.58.mp4

Summary

Add public methods to programmatically control zoom level on SuperDoc instances:

  • setZoom(percent) - Set zoom level as a percentage (e.g., 150 for 150%)
  • getZoom() - Get current zoom level as percentage
  • zoomChange event - Emitted when zoom changes

This allows developers to implement custom zoom controls (e.g., Ctrl+scroll, pinch-to-zoom, custom UI buttons) without relying on the toolbar.

Implementation

Both setZoom and getZoom go through superdocStore.activeZoom — the same centralized state used by the toolbar's zoom dropdown. The Vue watcher on activeZoom propagates changes to all downstream systems:

  • PresentationEditor.setGlobalZoom() — all editors
  • pdfViewer.updateScale() — PDF viewer
  • Whiteboard page sizes/offsets

This ensures overlays, comment highlights, hit-testing, and PDF rendering all stay in sync with the zoom level.

Usage

const superdoc = new SuperDoc({
  selector: '#editor',
  document: file,
});

// Set zoom to 150%
superdoc.setZoom(150);

// Get current zoom
const zoom = superdoc.getZoom(); // Returns 100, 150, etc.

// Listen for zoom changes
superdoc.on('zoomChange', ({ zoom }) => {
  console.log(`Zoom changed to ${zoom}%`);
});

// Example: Ctrl+scroll zoom
window.addEventListener('wheel', (event) => {
  if (event.ctrlKey) {
    event.preventDefault();
    const currentZoom = superdoc.getZoom();
    const delta = event.deltaY > 0 ? -10 : 10;
    superdoc.setZoom(Math.max(25, Math.min(400, currentZoom + delta)));
  }
}, { passive: false });

Test plan

  • setZoom(150) updates superdocStore.activeZoom to 150
  • getZoom() returns current activeZoom from store
  • getZoom() reflects value set by setZoom() (round-trip)
  • zoomChange event fires with correct payload
  • Invalid values (negative, NaN, zero, Infinity, string) warn and don't update
  • Programmatic API uses the same store path as toolbar zoom
  • Dev app has zoom +/- buttons for manual testing

References

Closes #928
Closes SD-1221

Add public methods to programmatically control zoom level:
- `setZoom(percent)` - Set zoom level (e.g., 150 for 150%)
- `getZoom()` - Get current zoom level as percentage

This allows developers to implement custom zoom controls
without relying on the toolbar UI.

Usage:
```javascript
// Set zoom to 150%
superdoc.setZoom(150);

// Get current zoom
const zoom = superdoc.getZoom(); // Returns 100, 150, etc.

// Listen for zoom changes
superdoc.on('zoomChange', ({ zoom }) => {
  console.log(`Zoom changed to ${zoom}%`);
});
```

Closes superdoc-dev#928
Copilot AI review requested due to automatic review settings December 12, 2025 13:33
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/superdoc/src/core/SuperDoc.js Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds programmatic zoom control to SuperDoc by exposing setZoom() and getZoom() public API methods, along with a zoomChange event. This enables developers to implement custom zoom controls (e.g., keyboard shortcuts, custom UI buttons, gesture controls) without relying on the built-in toolbar.

  • Adds getZoom() method to retrieve current zoom level as a percentage
  • Adds setZoom(percent) method to programmatically set zoom level for all documents
  • Emits zoomChange event when zoom is changed programmatically

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/superdoc/src/core/SuperDoc.js
Comment thread packages/superdoc/src/core/SuperDoc.js
Comment thread packages/superdoc/src/core/SuperDoc.js
Comment thread packages/superdoc/src/core/SuperDoc.js
Add 8 test cases covering:
- getZoom returns 100 when no presentation editor is available
- getZoom returns correct percentage from presentation editor
- getZoom rounds to nearest integer
- setZoom calls presentation editor with correct multiplier
- setZoom emits zoomChange event
- setZoom updates all editors in multi-document mode
- setZoom handles invalid values gracefully
- setZoom handles missing presentation editor
@tupizz tupizz force-pushed the feat/expose-zoom-api branch from 476c981 to 84f915c Compare December 12, 2025 13:39
@tupizz
Copy link
Copy Markdown
Contributor Author

tupizz commented Dec 12, 2025

@edoversb 🙏🏻

# Conflicts:
#	packages/superdoc/src/core/SuperDoc.test.js
- setZoom: also update superdocStore.activeZoom so toolbar UI stays
  in sync with programmatic zoom changes
- getZoom: fall back to store value when PE is not yet available
- onToolbarCommand: route toolbar zoom through setZoom() instead of
  direct store mutation, ensuring consistent behavior
- dev app: add zoom +/- buttons for manual testing
@tupizz tupizz force-pushed the feat/expose-zoom-api branch from 512decf to df86c8b Compare February 19, 2026 17:49
Copy link
Copy Markdown
Contributor

@andrii-harbour andrii-harbour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Copy Markdown
Collaborator

@harbournick harbournick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@harbournick
Copy link
Copy Markdown
Collaborator

merged via #2137

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IT: How to control the doc's scale by code but not UI

5 participants