Skip to content

Commit

Permalink
Improvements and fixes for piral-blazor
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Dec 22, 2023
1 parent d566977 commit 11f3e6f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Piral Changelog

## 1.4.3 (tbd)

- Fixed issue with `piral-blazor` using routing in standalone mode
- Fixed issue with `piral-blazor` connecting the Piral events

## 1.4.2 (December 16, 2023)

- Fixed misplaced *index.d.ts* in a scaffolded pilet (#658)
Expand Down
65 changes: 34 additions & 31 deletions src/converters/piral-blazor/src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,32 @@ export function createConverter(
}
}

if (capabilities.includes('events')) {
const eventDispatcher = document.body.dispatchEvent;

// listen to all events for forwarding them
document.body.dispatchEvent = function (ev: CustomEvent) {
if (ev.type.startsWith('piral-')) {
const type = ev.type.replace('piral-', '');
const args = ev.detail.arg;

try {
JSON.stringify(args);
processEvent(type, args);
} catch {
console.warn(`The event "${type}" could not be serialized and will not be handled by Blazor.`);
}
}

return eventDispatcher.call(this, ev);
};
}

if (language && capabilities.includes('language')) {
if (typeof language.current === 'string') {
await setLanguage(language.current);
}

if (capabilities.includes('events')) {
const eventDispatcher = document.body.dispatchEvent;

// listen to all events for forwarding them
document.body.dispatchEvent = function (ev: CustomEvent) {
if (ev.type.startsWith('piral-')) {
const type = ev.type.replace('piral-', '');
const args = ev.detail.arg;

try {
JSON.stringify(args);
processEvent(type, args);
} catch {
console.warn(`The event "${type}" could not be serialized and will not be handled by Blazor.`);
}
}

return eventDispatcher.call(this, ev);
};
}

if (typeof language.onChange === 'function') {
language.onChange(setLanguage);
}
Expand Down Expand Up @@ -148,16 +148,6 @@ export function createConverter(

addGlobalEventListeners(el);

if (listener === undefined) {
listener = nav.listen(({ location, action }) => {
// POP is already handled by .NET
if (action !== 'POP') {
const url = makeUrl(location.href);
callNotifyLocationChanged(url, action === 'REPLACE', location.state);
}
});
}

locals.state = 'fresh';
locals.next = noop;
locals.dispose = attachEvents(
Expand Down Expand Up @@ -217,6 +207,19 @@ export function createConverter(
}

(loader || (convert.loader = loader = boot(opts)))
.then((config) => {
if (listener === undefined) {
listener = nav.listen(({ location, action }) => {
// POP is already handled by .NET
if (action !== 'POP') {
const url = makeUrl(location.href);
callNotifyLocationChanged(url, action === 'REPLACE', location.state);
}
});
}

return config;
})
.then((config) =>
dependency(config).then(() => {
if (locals.state === 'fresh') {
Expand Down
2 changes: 1 addition & 1 deletion src/converters/piral-blazor/src/interop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const wasmLib = 'Microsoft.AspNetCore.Components.WebAssembly';
const coreLib = 'Piral.Blazor.Core';

function isDotNet6OrBelow() {
return typeof window.Blazor._internal.NavigationLock === 'undefined';
return typeof window.Blazor?._internal?.NavigationLock === 'undefined';
}

function createBase() {
Expand Down

0 comments on commit 11f3e6f

Please sign in to comment.