mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-28 22:42:11 +01:00
Merged PR 2059: feature(ui-modal): add QR code display for URLs in dialog modals
feature(ui-modal): add QR code display for URLs in dialog modals Add automatic URL detection and QR code rendering in dialog modals: - Parse dialog content to extract URLs (http/https) - Display extracted URLs as QR codes using angularx-qrcode library - Split content around URL to show text before and after the QR code - Auto-detect URLs by default, with optional showUrlAsQrCode override - Add comprehensive unit tests for URL parsing helpers Ref: #5511
This commit is contained in:
committed by
Lorenz Hilpert
parent
0aeef0592b
commit
7884e1af32
@@ -1,16 +1,43 @@
|
||||
@if (modalRef.data.subtitle; as subtitle) {
|
||||
<h2 class="subtitle">{{ subtitle }}</h2>
|
||||
}
|
||||
@if (modalRef.data.content; as content) {
|
||||
<p class="content">
|
||||
{{ content }}
|
||||
</p>
|
||||
|
||||
<!-- QR Code Display Mode -->
|
||||
@if (shouldShowQrCode(); as showQr) {
|
||||
@if (parsedContent(); as parsed) {
|
||||
@if (parsed.textBefore) {
|
||||
<p class="content">{{ parsed.textBefore }}</p>
|
||||
}
|
||||
|
||||
<div class="qr-code-container">
|
||||
<qrcode
|
||||
[qrdata]="parsed.url!"
|
||||
[width]="200"
|
||||
[errorCorrectionLevel]="'M'"
|
||||
[margin]="2"
|
||||
></qrcode>
|
||||
</div>
|
||||
|
||||
@if (parsed.textAfter) {
|
||||
<p class="content">{{ parsed.textAfter }}</p>
|
||||
}
|
||||
}
|
||||
} @else {
|
||||
<!-- Default Text Display Mode -->
|
||||
@if (modalRef.data.content; as content) {
|
||||
<p class="content">
|
||||
{{ content }}
|
||||
</p>
|
||||
}
|
||||
}
|
||||
|
||||
@if (modalRef.data.actions; as actions) {
|
||||
<div class="actions">
|
||||
@for (action of actions; track action) {
|
||||
<button [class.selected]="action.selected" (click)="handleCommand(action.command)">
|
||||
<button
|
||||
[class.selected]="action.selected"
|
||||
(click)="handleCommand(action.command)"
|
||||
>
|
||||
{{ action.label }}
|
||||
</button>
|
||||
}
|
||||
|
||||
@@ -15,11 +15,15 @@
|
||||
@apply text-lg text-center whitespace-pre-wrap mb-8 px-16;
|
||||
}
|
||||
|
||||
.qr-code-container {
|
||||
@apply flex flex-col items-center justify-center mb-8;
|
||||
}
|
||||
|
||||
.actions {
|
||||
@apply text-center mb-8;
|
||||
|
||||
button {
|
||||
@apply border-2 border-solid border-brand bg-white text-brand rounded-full py-3 px-6 font-bold text-lg outline-none self-end whitespace-nowrap ml-4;
|
||||
@apply border-2 border-solid border-brand bg-white text-brand rounded-full py-3 px-6 font-bold text-lg outline-none self-end whitespace-nowrap;
|
||||
|
||||
&.selected {
|
||||
@apply bg-brand text-white;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { Component, computed, OnInit } from '@angular/core';
|
||||
import { CommandService } from '@core/command';
|
||||
import { UiModalRef } from '../defs/modal-ref';
|
||||
import { DialogModel } from './dialog.model';
|
||||
import { parseDialogContentForUrl } from './dialog.helper';
|
||||
|
||||
@Component({
|
||||
selector: 'ui-dialog-modal',
|
||||
@@ -10,6 +11,26 @@ import { DialogModel } from './dialog.model';
|
||||
standalone: false,
|
||||
})
|
||||
export class UiDialogModalComponent implements OnInit {
|
||||
/**
|
||||
* Parsed content with URL extracted for QR code display.
|
||||
* Only relevant when showUrlAsQrCode is true.
|
||||
*/
|
||||
readonly parsedContent = computed(() => {
|
||||
const data = this.modalRef.data;
|
||||
if (!data.showUrlAsQrCode) {
|
||||
return null;
|
||||
}
|
||||
return parseDialogContentForUrl(data.content);
|
||||
});
|
||||
|
||||
/**
|
||||
* Whether to show the QR code instead of the URL text.
|
||||
*/
|
||||
readonly shouldShowQrCode = computed(() => {
|
||||
const parsed = this.parsedContent();
|
||||
return parsed !== null && parsed.url !== null;
|
||||
});
|
||||
|
||||
constructor(
|
||||
public modalRef: UiModalRef<any, DialogModel<any>>,
|
||||
private _command: CommandService,
|
||||
|
||||
48
apps/isa-app/src/ui/modal/dialog/dialog.helper.ts
Normal file
48
apps/isa-app/src/ui/modal/dialog/dialog.helper.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { ParsedDialogContent } from './dialog.model';
|
||||
|
||||
/**
|
||||
* Regular expression to match URLs in text.
|
||||
* Matches http:// and https:// URLs.
|
||||
*/
|
||||
const URL_REGEX = /https?:\/\/[^\s]+/i;
|
||||
|
||||
/**
|
||||
* Parses the dialog content and extracts the first URL.
|
||||
* Splits the content into text before the URL, the URL itself, and text after.
|
||||
*
|
||||
* @param content - The dialog content string to parse
|
||||
* @returns ParsedDialogContent with the split content
|
||||
*/
|
||||
export const parseDialogContentForUrl = (
|
||||
content: string | undefined,
|
||||
): ParsedDialogContent => {
|
||||
if (!content) {
|
||||
return { textBefore: '', url: null, textAfter: '' };
|
||||
}
|
||||
|
||||
const match = content.match(URL_REGEX);
|
||||
|
||||
if (!match || match.index === undefined) {
|
||||
return { textBefore: content, url: null, textAfter: '' };
|
||||
}
|
||||
|
||||
const url = match[0];
|
||||
const urlIndex = match.index;
|
||||
const textBefore = content.substring(0, urlIndex).trim();
|
||||
const textAfter = content.substring(urlIndex + url.length).trim();
|
||||
|
||||
return { textBefore, url, textAfter };
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if the given content contains a URL.
|
||||
*
|
||||
* @param content - The content string to check
|
||||
* @returns true if a URL is found, false otherwise
|
||||
*/
|
||||
export const contentHasUrl = (content: string | undefined): boolean => {
|
||||
if (!content) {
|
||||
return false;
|
||||
}
|
||||
return URL_REGEX.test(content);
|
||||
};
|
||||
152
apps/isa-app/src/ui/modal/dialog/dialog.model.spec.ts
Normal file
152
apps/isa-app/src/ui/modal/dialog/dialog.model.spec.ts
Normal file
@@ -0,0 +1,152 @@
|
||||
import { contentHasUrl, parseDialogContentForUrl } from './dialog.helper';
|
||||
import { ParsedDialogContent } from './dialog.model';
|
||||
|
||||
describe('parseDialogContentForUrl', () => {
|
||||
it('should return empty result for undefined content', () => {
|
||||
const result = parseDialogContentForUrl(undefined);
|
||||
|
||||
expect(result).toEqual<ParsedDialogContent>({
|
||||
textBefore: '',
|
||||
url: null,
|
||||
textAfter: '',
|
||||
});
|
||||
});
|
||||
|
||||
it('should return empty result for empty string', () => {
|
||||
const result = parseDialogContentForUrl('');
|
||||
|
||||
expect(result).toEqual<ParsedDialogContent>({
|
||||
textBefore: '',
|
||||
url: null,
|
||||
textAfter: '',
|
||||
});
|
||||
});
|
||||
|
||||
it('should return content as textBefore when no URL is found', () => {
|
||||
const content = 'This is some text without a URL';
|
||||
|
||||
const result = parseDialogContentForUrl(content);
|
||||
|
||||
expect(result).toEqual<ParsedDialogContent>({
|
||||
textBefore: content,
|
||||
url: null,
|
||||
textAfter: '',
|
||||
});
|
||||
});
|
||||
|
||||
it('should extract https URL from content', () => {
|
||||
const content = 'Text before https://example.com text after';
|
||||
|
||||
const result = parseDialogContentForUrl(content);
|
||||
|
||||
expect(result).toEqual<ParsedDialogContent>({
|
||||
textBefore: 'Text before',
|
||||
url: 'https://example.com',
|
||||
textAfter: 'text after',
|
||||
});
|
||||
});
|
||||
|
||||
it('should extract http URL from content', () => {
|
||||
const content = 'Text before http://example.com text after';
|
||||
|
||||
const result = parseDialogContentForUrl(content);
|
||||
|
||||
expect(result).toEqual<ParsedDialogContent>({
|
||||
textBefore: 'Text before',
|
||||
url: 'http://example.com',
|
||||
textAfter: 'text after',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle URL at the beginning of content', () => {
|
||||
const content = 'https://example.com/path text after';
|
||||
|
||||
const result = parseDialogContentForUrl(content);
|
||||
|
||||
expect(result).toEqual<ParsedDialogContent>({
|
||||
textBefore: '',
|
||||
url: 'https://example.com/path',
|
||||
textAfter: 'text after',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle URL at the end of content', () => {
|
||||
const content = 'Text before https://example.com/path';
|
||||
|
||||
const result = parseDialogContentForUrl(content);
|
||||
|
||||
expect(result).toEqual<ParsedDialogContent>({
|
||||
textBefore: 'Text before',
|
||||
url: 'https://example.com/path',
|
||||
textAfter: '',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle real-world content with newlines', () => {
|
||||
const content = `Punkte: 80500
|
||||
Um alle Vorteile der Kundenkarte nutzen zu können, ist eine Verknüpfung zu einem Online-Konto notwendig. Kund:innen können sich über den QR-Code selbstständig anmelden oder die Kundenkarte dem bestehendem Konto hinzufügen. Bereits gesammelte Punkte werden übernommen.
|
||||
https://h-k.me/QOHNTFVA`;
|
||||
|
||||
const result = parseDialogContentForUrl(content);
|
||||
|
||||
expect(result.url).toBe('https://h-k.me/QOHNTFVA');
|
||||
expect(result.textBefore).toContain('Punkte: 80500');
|
||||
expect(result.textBefore).toContain(
|
||||
'Bereits gesammelte Punkte werden übernommen.',
|
||||
);
|
||||
expect(result.textAfter).toBe('');
|
||||
});
|
||||
|
||||
it('should extract only the first URL when multiple URLs are present', () => {
|
||||
const content = 'First https://first.com then https://second.com';
|
||||
|
||||
const result = parseDialogContentForUrl(content);
|
||||
|
||||
expect(result).toEqual<ParsedDialogContent>({
|
||||
textBefore: 'First',
|
||||
url: 'https://first.com',
|
||||
textAfter: 'then https://second.com',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle URLs with paths and query parameters', () => {
|
||||
const content =
|
||||
'Visit https://example.com/path?query=value&foo=bar for more';
|
||||
|
||||
const result = parseDialogContentForUrl(content);
|
||||
|
||||
expect(result.url).toBe('https://example.com/path?query=value&foo=bar');
|
||||
expect(result.textBefore).toBe('Visit');
|
||||
expect(result.textAfter).toBe('for more');
|
||||
});
|
||||
});
|
||||
|
||||
describe('contentHasUrl', () => {
|
||||
it('should return false for undefined content', () => {
|
||||
expect(contentHasUrl(undefined)).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for empty string', () => {
|
||||
expect(contentHasUrl('')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for content without URL', () => {
|
||||
expect(contentHasUrl('This is text without a URL')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true for content with https URL', () => {
|
||||
expect(contentHasUrl('Check out https://example.com')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true for content with http URL', () => {
|
||||
expect(contentHasUrl('Check out http://example.com')).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true for real-world content', () => {
|
||||
const content = `Punkte: 80500
|
||||
Um alle Vorteile der Kundenkarte nutzen zu können...
|
||||
https://h-k.me/QOHNTFVA`;
|
||||
|
||||
expect(contentHasUrl(content)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,7 @@
|
||||
import { DialogSettings, KeyValueDTOOfStringAndString } from '@generated/swagger/crm-api';
|
||||
import {
|
||||
DialogSettings,
|
||||
KeyValueDTOOfStringAndString,
|
||||
} from '@generated/swagger/crm-api';
|
||||
|
||||
export interface DialogModel<T = any> {
|
||||
actions?: Array<KeyValueDTOOfStringAndString>;
|
||||
@@ -14,4 +17,21 @@ export interface DialogModel<T = any> {
|
||||
* default: true
|
||||
*/
|
||||
handleCommand?: boolean;
|
||||
/**
|
||||
* If true, URLs in the content will be displayed as QR codes.
|
||||
* default: false
|
||||
*/
|
||||
showUrlAsQrCode?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Result of parsing content for URLs
|
||||
*/
|
||||
export interface ParsedDialogContent {
|
||||
/** Text before the URL */
|
||||
textBefore: string;
|
||||
/** The extracted URL (if any) */
|
||||
url: string | null;
|
||||
/** Text after the URL */
|
||||
textAfter: string;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import { Observable, throwError } from 'rxjs';
|
||||
import { catchError, tap } from 'rxjs/operators';
|
||||
import { DialogModel } from './dialog.model';
|
||||
import { ToasterService } from '@shared/shell';
|
||||
import { contentHasUrl } from './dialog.helper';
|
||||
|
||||
@Injectable()
|
||||
export class OpenDialogInterceptor implements HttpInterceptor {
|
||||
@@ -21,7 +22,10 @@ export class OpenDialogInterceptor implements HttpInterceptor {
|
||||
private _toast: ToasterService,
|
||||
) {}
|
||||
|
||||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
intercept(
|
||||
req: HttpRequest<any>,
|
||||
next: HttpHandler,
|
||||
): Observable<HttpEvent<any>> {
|
||||
return next.handle(req).pipe(
|
||||
tap((response) => {
|
||||
if (response instanceof HttpResponse) {
|
||||
@@ -59,9 +63,17 @@ export class OpenDialogInterceptor implements HttpInterceptor {
|
||||
}
|
||||
|
||||
openDialog(model: DialogModel<any>) {
|
||||
// Auto-detect URLs and enable QR code display if URL is found
|
||||
// Can be overridden by explicitly setting showUrlAsQrCode in the model
|
||||
const showUrlAsQrCode =
|
||||
model.showUrlAsQrCode ?? contentHasUrl(model.content);
|
||||
|
||||
this._modal.open({
|
||||
content: UiDialogModalComponent,
|
||||
data: model,
|
||||
data: {
|
||||
...model,
|
||||
showUrlAsQrCode,
|
||||
},
|
||||
title: model.title,
|
||||
config: {
|
||||
canClose: (model.settings & 1) === 1,
|
||||
|
||||
@@ -2,7 +2,6 @@ import { OverlayModule } from '@angular/cdk/overlay';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { ModuleWithProviders, NgModule } from '@angular/core';
|
||||
import { UiModalComponent } from './modal.component';
|
||||
import { UiModalService } from './modal.service';
|
||||
import { UiDebugModalComponent } from './debug-modal/debug-modal.component';
|
||||
import { UiMessageModalComponent } from './message-modal.component';
|
||||
import { UiIconModule } from '@ui/icon';
|
||||
@@ -10,9 +9,10 @@ import { UiDialogModalComponent } from './dialog/dialog-modal.component';
|
||||
import { HTTP_INTERCEPTORS } from '@angular/common/http';
|
||||
import { OpenDialogInterceptor } from './dialog/open-dialog.interceptor';
|
||||
import { UiPromptModalComponent } from './prompt-modal';
|
||||
import { QRCodeComponent } from 'angularx-qrcode';
|
||||
|
||||
@NgModule({
|
||||
imports: [CommonModule, OverlayModule, UiIconModule],
|
||||
imports: [CommonModule, OverlayModule, UiIconModule, QRCodeComponent],
|
||||
declarations: [
|
||||
UiModalComponent,
|
||||
UiDebugModalComponent,
|
||||
|
||||
Reference in New Issue
Block a user