6.7 KiB
Contributing to ngx-pinch-zoom
For New Maintainers
This document provides comprehensive information for maintaining and developing this Angular library.
Project Overview
ngx-pinch-zoom is an Angular library providing touch-enabled image zoom and pan functionality. The library is built with Angular 20+ and uses the modern signals API for reactivity.
Key Features
- Touch and mouse support for pinch-to-zoom
- Wheel zoom support
- Double-tap to zoom
- Pan and drag functionality
- Configurable zoom limits and behaviors
- Standalone component (no NgModule required)
- Built with Angular signals for optimal performance
Architecture
Project Structure
ngx-pinch-zoom/
├── projects/ngx-pinch-zoom/ # Library source code
│ └── src/lib/
│ ├── pinch-zoom.component.ts # Main component with signals API
│ ├── ivypinch.ts # Core zoom logic
│ ├── touches.ts # Touch/mouse event handling
│ ├── interfaces.ts # TypeScript interfaces
│ └── properties.ts # Default configuration
├── src/ # Demo application
├── angular.json # Angular CLI configuration
├── package.json # Dependencies and scripts
└── tsconfig.json # TypeScript configuration
Core Components
-
PinchZoomComponent (
pinch-zoom.component.ts)- Main Angular component
- Uses signals for all inputs and outputs
- Implements modern Angular patterns (inject, computed, effect)
- Standalone component
-
IvyPinch (
ivypinch.ts)- Core zoom/pan logic
- Handles transformations and calculations
- No Angular dependencies (plain TypeScript class)
-
Touches (
touches.ts)- Event handling for touch and mouse interactions
- Gesture detection (pinch, pan, tap, double-tap)
- Cross-browser compatibility
Development Setup
Prerequisites
- Node.js 18.19.1+, 20.11.1+, or 22.0.0+
- npm 9.0.0+ or 10.0.0+
Installation
# Install dependencies
npm install
# Build the library
npm run build-lib
# Start demo app
npm start
Available Scripts
npm run build-lib # Build the library
npm run build # Build the demo app
npm start # Serve the demo app
npm run format # Format code with Prettier
npm run format:check # Check code formatting
Building the Library
The library is built using Angular's ng-packagr:
npm run build-lib
Output is generated in dist/ngx-pinch-zoom/ with:
- ESM bundles
- TypeScript declaration files
- package.json for npm publishing
- README.md
Angular Signals Migration
This library has been migrated to use Angular signals (Angular 20+). Here's what changed:
Before (Traditional Approach)
@Input() disabled: boolean;
@Output() zoomChanged = new EventEmitter<number>();
get scale(): number {
return this.pinchZoom.scale;
}
After (Signals Approach)
disabled = input<boolean>(false);
zoomChanged = output<number>();
scale = computed<number>(() => {
return this.currentScale();
});
Key Benefits
- Better Performance: Signals provide fine-grained reactivity
- Simpler State Management: No need for manual change detection
- Type Safety: Improved TypeScript inference
- Modern Angular: Aligns with Angular's future direction
Making Changes
Code Style
- Use Prettier for formatting (config in
.prettierrc) - Follow TypeScript strict mode rules
- Use meaningful variable names
- Add JSDoc comments for public APIs
TypeScript Configuration
The project uses strict TypeScript settings:
{
"strict": true,
"strictNullChecks": true,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
Adding New Features
-
Update the component (
pinch-zoom.component.ts):- Add input signals for configuration
- Add computed signals for derived state
- Update the component template if needed
-
Update core logic (
ivypinch.ts):- Add properties to the
Propertiesinterface - Implement the functionality
- Test calculations thoroughly
- Add properties to the
-
Document the feature:
- Update README.md with the new property
- Add usage examples
- Document default values
Testing Changes
Currently, the test infrastructure has been simplified. To test changes:
- Build the library:
npm run build-lib - Run the demo app:
npm start - Manually test in the browser
- Test on both desktop (mouse) and touch devices
Future improvement: Add Jest or Vitest for unit testing.
Common Tasks
Updating Dependencies
# Update Angular to latest version
npx ng update @angular/core @angular/cli
# Update other dependencies
npm update
Publishing to npm
- Update version in
package.json - Update version in
projects/ngx-pinch-zoom/package.json - Build the library:
npm run build-lib - Publish:
cd dist/ngx-pinch-zoom && npm publish
Debugging
-
Component Issues:
- Check signal reactivity with Angular DevTools
- Use
effect(() => console.log(signalValue()))
-
Touch/Mouse Issues:
- Add console.logs in
touches.tsevent handlers - Check browser dev tools for event listeners
- Add console.logs in
-
Transform Issues:
- Inspect
ivypinch.tstransformElement method - Check CSS transform values in browser inspector
- Inspect
Browser Compatibility
The library supports all modern browsers:
- Chrome/Edge (latest 2 versions)
- Firefox (latest 2 versions)
- Safari (latest 2 versions)
- Mobile browsers (iOS Safari, Chrome Mobile)
Performance Considerations
- Signals: Use computed signals for derived state
- Event Listeners: Properly clean up in ngOnDestroy
- Transform: Use CSS transforms (hardware accelerated)
- Change Detection: OnPush strategy compatible (signals don't require manual CD)
Troubleshooting
Build Failures
TypeScript errors: Ensure strict mode compliance
# Check TypeScript version
npx tsc --version # Should be ~5.8.0
Dependency conflicts: Clear cache and reinstall
rm -rf node_modules package-lock.json
npm install
Runtime Issues
Signals not updating: Check if effects are running Touch events not working: Verify viewport meta tag in host app Zoom calculations off: Check element positioning and getBoundingClientRect
Getting Help
- Create an issue on GitHub
- Check existing issues for similar problems
- Review the README.md for usage examples
Code of Conduct
- Be respectful and constructive
- Test your changes before submitting
- Document new features
- Keep the codebase clean and maintainable