Skip to content

Commit 40bbf7f

Browse files
committed
Add TypeScript-based JS interop infrastructure
Introduces comprehensive TypeScript-based JavaScript interop for Blazouter, including five specialized services: NavigationInterop, DocumentInterop, StorageInterop, ViewportInterop, and ClipboardInterop. Adds TypeScript source project, compiled JS modules with type definitions, and global namespace exposure for C# interop. Updates documentation and sample app to demonstrate new APIs, and enhances RouterNavigationService with true browser navigation. All interop services are optional, type-safe, and backward compatible.
1 parent 410628a commit 40bbf7f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+6519
-22
lines changed

ATTRIBUTE_ROUTING.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,9 @@ Mix attribute-based routes with programmatic routes:
247247

248248
```csharp
249249
// In App.razor.cs or Routes.razor.cs
250-
using Blazouter.Extensions;
250+
using Blazouter.Enums;
251251
using Blazouter.Models;
252+
using Blazouter.Extensions;
252253

253254
public partial class App
254255
{
@@ -277,8 +278,8 @@ Create routes entirely from attributes:
277278

278279
```csharp
279280
// In App.razor.cs or Routes.razor.cs
280-
using Blazouter.Extensions;
281281
using Blazouter.Models;
282+
using Blazouter.Extensions;
282283

283284
public partial class App
284285
{

CHANGELOG.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,81 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [1.0.13] - 2025-11-26
11+
12+
### Added
13+
- **TypeScript-Based JavaScript Interop Infrastructure**: Comprehensive type-safe browser API access
14+
- TypeScript compilation pipeline with `.d.ts` generation for IntelliSense support
15+
- Global namespace exposure pattern for C# interop: `window.blazouterNavigation`, `window.blazouterDocument`, `window.blazouterStorage`, `window.blazouterViewport`, `window.blazouterClipboard`
16+
- Separate `Blazouter.TypeScript` project at `src/Blazouter.TypeScript/` for TypeScript source files
17+
- NPM package with TypeScript compiler configuration (tsconfig.json)
18+
- Compiled JavaScript modules with source maps and type definitions in `src/Blazouter/wwwroot/js/`
19+
20+
- **NavigationInterop Service**: Browser history and navigation API wrapper
21+
- History API: `GoBackAsync()`, `GoForwardAsync()`, `GoAsync()`, `GetHistoryLengthAsync()`, `GetHistoryStateAsync()`
22+
- URL helpers: `GetCurrentUrlAsync()`, `GetPathnameAsync()`
23+
- Hash navigation: `GetHashAsync()`, `SetHashAsync()`
24+
- Query parameters: `GetQueryStringAsync()`, `GetQueryParamAsync()`, `GetAllQueryParamsAsync()`
25+
- Page control: `ReloadAsync()`
26+
- Integration with `RouterNavigationService` providing `GoBackAsync()`, `GoForwardAsync()`, `CanGoBackAsync()`
27+
28+
- **DocumentInterop Service**: Document manipulation and SEO support
29+
- Title management: `SetTitleAsync()`, `GetTitleAsync()`
30+
- Meta tags: `SetMetaTagAsync()`, `GetMetaTagAsync()`, `RemoveMetaTagAsync()`
31+
- Open Graph support: `SetOpenGraphTagAsync()`
32+
- Canonical URLs: `SetCanonicalUrlAsync()`, `GetCanonicalUrlAsync()`
33+
- Scroll control: `ScrollToTopAsync()`, `ScrollToBottomAsync()`, `ScrollToElementAsync()`, `GetScrollPositionAsync()`, `SetScrollPositionAsync()`
34+
- Element management: `FocusElementAsync()`, `IsElementVisibleAsync()`
35+
- CSS classes: `AddClassAsync()`, `RemoveClassAsync()`, `ToggleClassAsync()`
36+
- Document state: `GetReadyStateAsync()`, `IsDocumentReadyAsync()`
37+
38+
- **StorageInterop Service**: Type-safe localStorage and sessionStorage access
39+
- LocalStorage operations: `SetLocalStorageAsync<T>()`, `GetLocalStorageAsync<T>()`, `RemoveLocalStorageAsync()`, `ClearLocalStorageAsync()`, `GetLocalStorageKeysAsync()`, `HasLocalStorageAsync()`
40+
- SessionStorage operations: `SetSessionStorageAsync<T>()`, `GetSessionStorageAsync<T>()`, `RemoveSessionStorageAsync()`, `ClearSessionStorageAsync()`, `GetSessionStorageKeysAsync()`, `HasSessionStorageAsync()`
41+
- Generic type support with automatic JSON serialization/deserialization
42+
43+
- **ViewportInterop Service**: Viewport, screen, and device detection
44+
- Viewport dimensions: `GetViewportSizeAsync()` returning `Size` record with width and height
45+
- Screen information: `GetScreenSizeAsync()`
46+
- Device pixel ratio: `GetPixelRatioAsync()`
47+
- Orientation: `IsPortraitAsync()`, `IsLandscapeAsync()`, `GetOrientationAsync()`
48+
- Device type detection: `IsMobileAsync()`, `IsTabletAsync()`, `IsDesktopAsync()`, `GetDeviceTypeAsync()`
49+
- Fullscreen API: `IsFullscreenAsync()`, `RequestFullscreenAsync()`, `ExitFullscreenAsync()`
50+
51+
- **ClipboardInterop Service**: Clipboard operations with permission support
52+
- Text operations: `CopyTextAsync()`, `ReadTextAsync()`
53+
- Feature detection: `IsClipboardSupportedAsync()`
54+
- Permission checks: `HasClipboardReadPermissionAsync()`, `HasClipboardWritePermissionAsync()`
55+
- Fallback support for older browsers in `copyText()` implementation
56+
57+
- **Service Organization**: All interop services in `Blazouter.Interops` namespace
58+
- Located at `src/Blazouter/Interops/` folder
59+
- `AddBlazouterInterop()` extension method for DI registration (optional, non-breaking)
60+
- Services injected as nullable dependencies for backward compatibility
61+
62+
- **Documentation**: Comprehensive guides and API references
63+
- `TYPESCRIPT_INTEGRATION.md`: Complete documentation with usage examples, API references, migration guide, and hosting-specific instructions for WebAssembly, Hybrid, and Server models
64+
- `FEATURES.md`: Feature #13 with detailed descriptions of all 5 interop services and their capabilities
65+
- `README.md`: Installation instructions with JavaScript module import requirement, updated Project Structure
66+
- `src/Blazouter/README.md` (NuGet): Concise TypeScript Integration section for package consumers
67+
- All documentation includes required module import: `<script type="module" src="_content/Blazouter/js/index.js"></script>`
68+
69+
### Changed
70+
- Project structure updated to include:
71+
- `src/Blazouter.TypeScript/` - Separate project for TypeScript source files
72+
- `src/Blazouter/Interops/` - All interop service implementations
73+
- `src/Blazouter/wwwroot/js/` - Compiled JavaScript modules (.js, .d.ts, .js.map files)
74+
- `RouterNavigationService` enhanced with navigation interop integration
75+
- Service registration extended via `AddBlazouterInterop()` method
76+
77+
### Technical Details
78+
- TypeScript source files: navigation.ts, document.ts, storage.ts, viewport.ts, clipboard.ts, index.ts
79+
- ES module format with explicit .js extensions for browser compatibility
80+
- TypeScript compilation generates declaration files for IntelliSense
81+
- JavaScript modules exposed via window object for C# JSRuntime interop
82+
- Optional module import - services gracefully handle missing JavaScript modules
83+
- Backward compatible - existing applications continue working without changes
84+
1085
## [1.0.12] - 2025-11-25
1186

1287
### Added
@@ -197,6 +272,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
197272
- FEATURES.md detailing all capabilities
198273
- Sample application demonstrating key features
199274

275+
[1.0.13]: https://github.com/Taiizor/Blazouter/compare/v1.0.12...v1.0.13
200276
[1.0.12]: https://github.com/Taiizor/Blazouter/compare/v1.0.11...v1.0.12
201277
[1.0.11]: https://github.com/Taiizor/Blazouter/compare/v1.0.10...v1.0.11
202278
[1.0.10]: https://github.com/Taiizor/Blazouter/compare/v1.0.9...v1.0.10

FEATURES.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,56 @@ Blazouter is a comprehensive routing library for Blazor applications, available
147147
- Sample application demonstrates all features in Navigation.razor and UserList.razor
148148
- 15 `Add()` method overloads for all common types (string, int, long, decimal, double, bool, DateTime, Guid, enum, and nullable variants)
149149

150+
### ✅ 13. TypeScript Integration for JavaScript Interop
151+
- Tested and verified working
152+
- Type-safe JavaScript modules with `.d.ts` definition files for IntelliSense
153+
- Comprehensive browser API integration with 5 specialized interop services
154+
- Automatic TypeScript compilation to JavaScript with source maps
155+
- Global namespace exposure pattern (`window.blazouterNavigation`, `window.blazouterDocument`, `window.blazouterStorage`, `window.blazouterViewport`, `window.blazouterClipboard`)
156+
- Optional service registration via `AddBlazouterInterop()`
157+
- **`NavigationInterop`** service for History API access
158+
- Navigate back/forward/go in browser history
159+
- URL and path information (getCurrentUrl, getPathname)
160+
- Hash navigation (getHash, setHash)
161+
- Query parameter helpers (getQueryString, getQueryParam, getAllQueryParams)
162+
- Page reload functionality
163+
- Check navigation availability
164+
- Manage history state (push and replace entries)
165+
- **`DocumentInterop`** service for document manipulation
166+
- Dynamic title updates
167+
- Meta tag management (description, keywords, Open Graph tags)
168+
- Canonical URL support for SEO
169+
- Scroll position tracking (getScrollPosition, setScrollPosition)
170+
- Scroll to top/element with smooth scrolling
171+
- Element visibility detection (isElementVisible)
172+
- CSS class manipulation (addClass, removeClass, toggleClass)
173+
- Document ready state checks
174+
- Element focus management
175+
- **`StorageInterop`** service for browser storage
176+
- Complete localStorage operations (set, get, remove, clear, keys, has)
177+
- Complete sessionStorage operations (set, get, remove, clear, keys, has)
178+
- Generic type support with JSON serialization
179+
- Type-safe storage access for complex objects
180+
- **`ViewportInterop`** service for viewport and device info
181+
- Viewport dimensions (getViewportSize, width, height)
182+
- Screen information (getScreenSize)
183+
- Device pixel ratio detection
184+
- Orientation detection (isPortrait, isLandscape, getOrientation)
185+
- Device type detection (isMobile, isTablet, isDesktop, getDeviceType)
186+
- Fullscreen API (isFullscreen, requestFullscreen, exitFullscreen)
187+
- **`ClipboardInterop`** service for clipboard operations
188+
- Copy text to clipboard with fallback for older browsers
189+
- Read text from clipboard
190+
- Feature detection (isClipboardSupported)
191+
- Permission checks (hasClipboardReadPermission, hasClipboardWritePermission)
192+
- Integration with `RouterNavigationService`
193+
- `GoBackAsync()` - True browser back navigation
194+
- `GoForwardAsync()` - Browser forward navigation
195+
- `CanGoBackAsync()` - Check if back navigation is available
196+
- Requires JavaScript module import: `<script type="module" src="_content/Blazouter/js/index.js"></script>`
197+
- Full documentation in [TYPESCRIPT_INTEGRATION.md](https://github.com/Taiizor/Blazouter/blob/develop/TYPESCRIPT_INTEGRATION.md)
198+
- Comprehensive demo page in WebAssembly sample at `/typescript-demo` showcasing all 5 interop services
199+
150200
## Components
151201

152202
### Router
@@ -197,6 +247,26 @@ Nested route renderer that:
197247
- Query parameter handling and URL building
198248
- Programmatic navigation API with NavigateTo(path)
199249
- Wrapper around NavigationManager for enhanced functionality
250+
- Async methods for browser navigation: GoBackAsync(), GoForwardAsync(), CanGoBackAsync()
251+
252+
### NavigationInterop (Optional)
253+
- Registered as scoped service via AddBlazouterInterop()
254+
- Type-safe access to browser History API
255+
- Navigate back and forward in history
256+
- Check history length and navigation availability
257+
- Manage history state (push, replace, get)
258+
- Requires JavaScript module import for functionality
259+
- Used by RouterNavigationService async navigation methods
260+
261+
### DocumentInterop (Optional)
262+
- Registered as scoped service via AddBlazouterInterop()
263+
- Type-safe document and DOM manipulation
264+
- Dynamic page title updates
265+
- Meta tag management (description, keywords, Open Graph)
266+
- Canonical URL support for SEO
267+
- Scroll management (scroll to top, scroll to element)
268+
- Element focus management
269+
- Requires JavaScript module import for functionality
200270

201271
### RouteAttributeDiscoveryService
202272
- Supports all 8 route attributes

README.md

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,82 @@ builder.Services.AddBlazouterErrorHandler<CustomRouterErrorHandler>();
760760
- `NavigationFailed` - Navigation operation failed
761761
- `ComponentLoadFailed` - Component failed to load
762762

763+
## 🔧 TypeScript Integration
764+
765+
Blazouter includes TypeScript-based JavaScript interop for enhanced browser integration with full type safety.
766+
767+
### Features
768+
769+
- **SEO Support**: Set meta tags, Open Graph tags, and canonical URLs
770+
- **Type Safety**: Full TypeScript definitions with `.d.ts` files for IntelliSense
771+
- **Browser Navigation**: True browser back/forward navigation using the History API
772+
- **Document Manipulation**: Dynamic title updates, meta tags, scrolling, and focus management
773+
774+
### Installation
775+
776+
Enable JavaScript interop by registering the services:
777+
778+
```csharp
779+
builder.Services.AddBlazouter();
780+
builder.Services.AddBlazouterInterop(); // Enable TypeScript interop
781+
```
782+
783+
Add the JavaScript module import to your `index.html`:
784+
785+
```html
786+
<!-- In wwwroot/index.html, add this in the <head> section -->
787+
<script type="module" src="_content/Blazouter/js/index.js"></script>
788+
```
789+
790+
### Browser Navigation Example
791+
792+
```csharp
793+
@inject RouterNavigationService NavService
794+
795+
<button @onclick="GoBack">Back</button>
796+
<button @onclick="GoForward">Forward</button>
797+
798+
@code {
799+
private async Task GoBack()
800+
{
801+
await NavService.GoBackAsync(); // Uses browser History API
802+
}
803+
804+
private async Task GoForward()
805+
{
806+
await NavService.GoForwardAsync();
807+
}
808+
}
809+
```
810+
811+
### Document Manipulation Example
812+
813+
```csharp
814+
@inject DocumentInterop DocumentInterop
815+
816+
@code {
817+
protected override async Task OnAfterRenderAsync(bool firstRender)
818+
{
819+
if (firstRender)
820+
{
821+
// Update page title
822+
await DocumentInterop.SetTitleAsync("Home - My App");
823+
824+
// Set meta tags for SEO
825+
await DocumentInterop.SetMetaTagAsync("description", "Welcome to my app");
826+
827+
// Set Open Graph tags for social sharing
828+
await DocumentInterop.SetOpenGraphTagAsync("og:title", "My App");
829+
830+
// Scroll to top on navigation
831+
await DocumentInterop.ScrollToTopAsync();
832+
}
833+
}
834+
}
835+
```
836+
837+
**[📚 Full TypeScript Integration Documentation →](TYPESCRIPT_INTEGRATION.md)**
838+
763839
## 🏗️ Project Structure
764840

765841
```
@@ -774,11 +850,17 @@ Blazouter/
774850
│ │ ├── Guards/ # Route guard implementations (AuthGuard)
775851
│ │ ├── Handlers/ # Error handler implementations (DefaultRouterErrorHandler)
776852
│ │ ├── Interfaces/ # Interface definitions (IRouteGuard, IRouteMiddleware, IRouteMatcherService, IRouterErrorHandler)
853+
│ │ ├── Interops/ # JavaScript interop services (NavigationInterop, DocumentInterop, StorageInterop, ViewportInterop, ClipboardInterop)
777854
│ │ ├── Models/ # Route models (RouteConfig, RouteMatch, RouterErrorContext, etc.)
778855
│ │ ├── Resources/ # Embedded resources
779-
│ │ ├── Services/ # Routing services (RouterStateService, RouteMatcherService, RouterNavigationService, etc.)
856+
│ │ ├── Services/ # Routing services (RouterStateService, RouteMatcherService, RouterNavigationService)
780857
│ │ ├── Utilities/ # Query string builder and helper utilities
781858
│ │ └── wwwroot/ # CSS and assets (blazouter.css, blazouter.min.css)
859+
│ │ └── js/ # Compiled JavaScript modules with TypeScript definitions (.js, .d.ts, .js.map)
860+
│ ├── Blazouter.TypeScript/ # TypeScript source files for JavaScript interop
861+
│ │ ├── TypeScript/ # TypeScript source files (navigation.ts, document.ts, storage.ts, viewport.ts, clipboard.ts, index.ts)
862+
│ │ ├── package.json # NPM dependencies for TypeScript compilation
863+
│ │ └── tsconfig.json # TypeScript compiler configuration
782864
│ ├── Blazouter.Server/ # Server-specific extensions
783865
│ │ ├── Extensions/ # Server integration (AddBlazouterSupport)
784866
│ │ ├── Pages/ # Server pages
@@ -861,6 +943,7 @@ This project is licensed under the MIT License.
861943
- [Documentation](https://github.com/Taiizor/Blazouter/blob/develop/FEATURES.md)
862944
- [Contributing Guide](https://github.com/Taiizor/Blazouter/blob/develop/CONTRIBUTING.md)
863945
- [Sample Applications](https://github.com/Taiizor/Blazouter/tree/develop/samples)
946+
- [TypeScript Integration](https://github.com/Taiizor/Blazouter/blob/develop/TYPESCRIPT_INTEGRATION.md)
864947
865948
## 🙏 Acknowledgments
866949

@@ -872,7 +955,7 @@ Inspired by React Router and built to bring similar capabilities to the Blazor e
872955
- [ ] Performance optimizations
873956
- [ ] Advanced caching strategies
874957
- [x] Query string helpers and utilities
875-
- [ ] Better TypeScript integration for JS interop
958+
- [x] Better TypeScript integration for JS interop
876959

877960
## ⭐ Show Your Support
878961

0 commit comments

Comments
 (0)