feat(ui): implement home page and ai chat input with voice orb
Implement the core user interface components including the home page, a sophisticated AI chat input component, and a 3D voice visualization service using Three.js. - Add `HomePage` component with responsive layout and theme integration. - Add `AiChatInputComponent` supporting text, file attachments, and voice mode transitions. - Implement `VoiceOrbService` using Three.js to provide a reactive 3D animated orb for voice interaction. - Update routing to include the new home page. - Refactor `SettingsPage` to clean up unused RTL logic and improve UI styling. - Adjust Angular build budgets in `angular.json` to accommodate increased bundle size from Three.js.
This commit is contained in:
+4
-4
@@ -35,13 +35,13 @@
|
||||
"budgets": [
|
||||
{
|
||||
"type": "initial",
|
||||
"maximumWarning": "500kB",
|
||||
"maximumError": "1MB"
|
||||
"maximumWarning": "1.5MB",
|
||||
"maximumError": "2MB"
|
||||
},
|
||||
{
|
||||
"type": "anyComponentStyle",
|
||||
"maximumWarning": "4kB",
|
||||
"maximumError": "8kB"
|
||||
"maximumWarning": "8kB",
|
||||
"maximumError": "12kB"
|
||||
}
|
||||
],
|
||||
"outputHashing": "all"
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import { Routes } from '@angular/router';
|
||||
import { HomePage } from './pages/home/home.page';
|
||||
import { SettingsPage } from './pages/settings/settings.page';
|
||||
|
||||
export const routes: Routes = [
|
||||
{ path: '', pathMatch: 'full', redirectTo: '' },
|
||||
{
|
||||
path: '',
|
||||
title: 'دستیار هوشمند',
|
||||
component: HomePage,
|
||||
},
|
||||
{
|
||||
path: 'settings',
|
||||
title: 'تنظیمات',
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
.home-page {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: 100%;
|
||||
padding: 2rem 1rem;
|
||||
}
|
||||
|
||||
.home-content {
|
||||
width: 100%;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.home-header {
|
||||
text-align: center;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.home-icon-wrapper {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 140px;
|
||||
height: 140px;
|
||||
border-radius: 50%;
|
||||
background: transparent;
|
||||
margin-bottom: 1.5rem;
|
||||
padding: 0.75rem;
|
||||
}
|
||||
|
||||
.home-logo {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: contain;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.home-title {
|
||||
font-size: 1.75rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
margin: 0 0 0.5rem;
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
.home-subtitle {
|
||||
font-size: 0.9375rem;
|
||||
color: var(--text-muted);
|
||||
margin: 0;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.home-page {
|
||||
padding: 1.25rem 0.75rem;
|
||||
}
|
||||
|
||||
.home-header {
|
||||
margin-bottom: 1.25rem;
|
||||
}
|
||||
|
||||
.home-icon-wrapper {
|
||||
width: 100px;
|
||||
height: 100px;
|
||||
margin-bottom: 1.25rem;
|
||||
padding: 0.5rem;
|
||||
}
|
||||
|
||||
.home-logo {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.home-title {
|
||||
font-size: 1.375rem;
|
||||
}
|
||||
|
||||
.home-subtitle {
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.home-page,
|
||||
.home-header,
|
||||
.home-icon-wrapper {
|
||||
transition: none !important;
|
||||
animation: none !important;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<div class="home-page">
|
||||
<div class="home-content">
|
||||
<div class="home-header">
|
||||
<div class="home-icon-wrapper">
|
||||
<img
|
||||
class="home-logo"
|
||||
[src]="logoSrc()"
|
||||
alt="Raymand"
|
||||
/>
|
||||
</div>
|
||||
<h1 class="home-title">دستیار هوشمند</h1>
|
||||
<p class="home-subtitle">سوال خود را بپرسید، من اینجا هستم</p>
|
||||
</div>
|
||||
|
||||
<app-ai-chat-input
|
||||
placeholder="پیام خود را بنویسید..."
|
||||
(messageSent)="onMessageSent($event)"
|
||||
(fileSelected)="onFileSelected($event)"
|
||||
(voiceToggled)="onVoiceToggled($event)"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,32 @@
|
||||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { AiChatInputComponent } from '../../shared/ui/ai-chat-input/ai-chat-input.component';
|
||||
import { ThemeService } from '../../services/theme/theme.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-home',
|
||||
standalone: true,
|
||||
imports: [CommonModule, AiChatInputComponent],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
templateUrl: './home.page.html',
|
||||
styleUrl: './home.page.css',
|
||||
})
|
||||
export class HomePage {
|
||||
private readonly themeService = inject(ThemeService);
|
||||
|
||||
readonly isDarkMode = this.themeService.isDarkMode;
|
||||
|
||||
readonly logoSrc = () => this.isDarkMode() ? 'assets/images/RaymandLogo.png' : 'assets/images/login.png';
|
||||
|
||||
onMessageSent(message: string): void {
|
||||
console.log('Message sent:', message);
|
||||
}
|
||||
|
||||
onFileSelected(file: File): void {
|
||||
console.log('File selected:', file.name);
|
||||
}
|
||||
|
||||
onVoiceToggled(active: boolean): void {
|
||||
console.log('Voice mode:', active ? 'active' : 'inactive');
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,14 @@
|
||||
|
||||
.settings-tabs {
|
||||
position: relative;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.tab-divider {
|
||||
position: relative;
|
||||
z-index: 10;
|
||||
flex-shrink: 0;
|
||||
margin: 0.75rem 0;
|
||||
}
|
||||
|
||||
.tab-btn {
|
||||
|
||||
@@ -37,6 +37,8 @@
|
||||
}
|
||||
</button>
|
||||
|
||||
<div class="tab-divider w-px h-6 bg-black/25"></div>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="tab-btn flex-1 flex items-center justify-center gap-2 px-4 py-3 text-sm font-medium text-[var(--text-muted)] transition-all duration-200 hover:text-[var(--text-primary)] relative"
|
||||
@@ -96,7 +98,7 @@
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="dropzone relative flex flex-col items-center justify-center gap-3 p-8 border-2 border-dashed border-[var(--text-muted)]/20 rounded-xl bg-[var(--dropzone-bg)] cursor-pointer transition-all duration-300 hover:border-primary/50 hover:bg-primary/5 hover:shadow-[0_10px_30px_rgba(59,130,246,0.15)]"
|
||||
class="dropzone relative flex flex-col items-center justify-center gap-3 p-8 border-2 border-dashed border-[rgba(148,163,184,0.25)] rounded-xl bg-[var(--dropzone-bg)] cursor-pointer transition-all duration-300 hover:border-primary/50 hover:bg-primary/5 hover:shadow-[0_10px_30px_rgba(59,130,246,0.15)]"
|
||||
[class.dropzone-active]="isDragging()"
|
||||
(dragover)="onDragOver($event)"
|
||||
(dragleave)="onDragLeave()"
|
||||
@@ -302,22 +304,6 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="setting-item flex items-center justify-between gap-4 p-4 bg-[var(--card-bg)] rounded-xl shadow-[0_2px_8px_rgba(0,0,0,0.04)] transition-all duration-200 hover:shadow-[0_4px_16px_rgba(0,0,0,0.08)] hover:-translate-y-0.5"
|
||||
>
|
||||
<div class="setting-info flex flex-col gap-1 min-w-0">
|
||||
<span class="text-sm font-medium text-[var(--text-secondary)]">حالت RTL</span>
|
||||
<span class="text-xs text-[var(--text-muted)]"
|
||||
>جهت متن راستبهچپ (فارسی/عربی)</span
|
||||
>
|
||||
</div>
|
||||
<div class="setting-control shrink-0">
|
||||
<label class="toggle-switch" [class.toggle-active]="isRTL()">
|
||||
<input type="checkbox" [checked]="isRTL()" (change)="toggleRTL()" />
|
||||
<span class="toggle-slider"></span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ChangeDetectionStrategy, Component, signal, inject, computed, effect } from '@angular/core';
|
||||
import { ChangeDetectionStrategy, Component, signal, inject } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { ThemeService } from '../../services/theme/theme.service';
|
||||
@@ -29,7 +29,6 @@ export class SettingsPage {
|
||||
readonly language = signal('fa');
|
||||
readonly notificationsEnabled = signal(true);
|
||||
readonly isDarkMode = this.themeService.isDarkMode;
|
||||
readonly isRTL = signal(true);
|
||||
|
||||
selectTab(id: TabId): void {
|
||||
this.activeTab.set(id);
|
||||
@@ -77,12 +76,6 @@ export class SettingsPage {
|
||||
this.themeService.toggleDarkMode();
|
||||
}
|
||||
|
||||
toggleRTL(): void {
|
||||
this.isRTL.update(val => !val);
|
||||
document.documentElement.dir = this.isRTL() ? 'rtl' : 'ltr';
|
||||
document.body.style.direction = this.isRTL() ? 'rtl' : 'ltr';
|
||||
}
|
||||
|
||||
get fileCount(): number {
|
||||
return this.uploadedFiles().length;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,295 @@
|
||||
import { Injectable, NgZone, inject } from '@angular/core';
|
||||
import * as THREE from 'three';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class VoiceOrbService {
|
||||
private scene: THREE.Scene | null = null;
|
||||
private camera: THREE.PerspectiveCamera | null = null;
|
||||
private renderer: THREE.WebGLRenderer | null = null;
|
||||
private orb: THREE.Mesh | null = null;
|
||||
private orbGeometry: THREE.IcosahedronGeometry | null = null;
|
||||
private glowShell: THREE.Mesh | null = null;
|
||||
private originalPositions: Float32Array | null = null;
|
||||
private animationId: number | null = null;
|
||||
private audioContext: AudioContext | null = null;
|
||||
private analyser: AnalyserNode | null = null;
|
||||
private mediaStream: MediaStream | null = null;
|
||||
private audioDataArray: Uint8Array | null = null;
|
||||
private isListening = false;
|
||||
private readonly reducedMotion: boolean;
|
||||
|
||||
constructor(private readonly ngZone: NgZone) {
|
||||
this.reducedMotion = typeof window !== 'undefined'
|
||||
&& window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
||||
}
|
||||
|
||||
init(container: HTMLElement): void {
|
||||
if (this.scene) {
|
||||
if (this.renderer && this.renderer.domElement.parentNode !== container) {
|
||||
container.appendChild(this.renderer.domElement);
|
||||
}
|
||||
this.resize(container);
|
||||
return;
|
||||
}
|
||||
|
||||
const width = container.clientWidth || 280;
|
||||
const height = container.clientHeight || 280;
|
||||
|
||||
this.scene = new THREE.Scene();
|
||||
|
||||
this.camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100);
|
||||
this.camera.position.z = 4;
|
||||
|
||||
this.renderer = new THREE.WebGLRenderer({
|
||||
antialias: true,
|
||||
alpha: true,
|
||||
powerPreference: 'high-performance',
|
||||
});
|
||||
this.renderer.setSize(width, height);
|
||||
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
|
||||
this.renderer.setClearColor(0x000000, 0);
|
||||
container.appendChild(this.renderer.domElement);
|
||||
|
||||
const ambientLight = new THREE.AmbientLight(0x404060, 2);
|
||||
this.scene.add(ambientLight);
|
||||
|
||||
const pointLight1 = new THREE.PointLight(0x3b82f6, 12, 10);
|
||||
pointLight1.position.set(0, 0, 3);
|
||||
this.scene.add(pointLight1);
|
||||
|
||||
const pointLight2 = new THREE.PointLight(0x8b5cf6, 6, 10);
|
||||
pointLight2.position.set(-2, 1, 2);
|
||||
this.scene.add(pointLight2);
|
||||
|
||||
const pointLight3 = new THREE.PointLight(0x06b6d4, 4, 10);
|
||||
pointLight3.position.set(2, -1, 2);
|
||||
this.scene.add(pointLight3);
|
||||
|
||||
this.orbGeometry = new THREE.IcosahedronGeometry(1, 48);
|
||||
const orbMaterial = new THREE.MeshPhysicalMaterial({
|
||||
color: 0x60a5fa,
|
||||
emissive: 0x1e3a5f,
|
||||
emissiveIntensity: 0.35,
|
||||
roughness: 0.12,
|
||||
metalness: 0.05,
|
||||
clearcoat: 1,
|
||||
clearcoatRoughness: 0.08,
|
||||
});
|
||||
this.orb = new THREE.Mesh(this.orbGeometry, orbMaterial);
|
||||
this.scene.add(this.orb);
|
||||
|
||||
this.originalPositions = new Float32Array(this.orbGeometry.attributes['position'].array);
|
||||
|
||||
const glowGeometry = new THREE.IcosahedronGeometry(1.18, 32);
|
||||
const glowMaterial = new THREE.MeshBasicMaterial({
|
||||
color: 0x3b82f6,
|
||||
transparent: true,
|
||||
opacity: 0.07,
|
||||
blending: THREE.AdditiveBlending,
|
||||
side: THREE.BackSide,
|
||||
depthWrite: false,
|
||||
});
|
||||
this.glowShell = new THREE.Mesh(glowGeometry, glowMaterial);
|
||||
this.scene.add(this.glowShell);
|
||||
|
||||
this.setupResizeObserver(container);
|
||||
}
|
||||
|
||||
private setupResizeObserver(container: HTMLElement): void {
|
||||
if (typeof ResizeObserver === 'undefined') return;
|
||||
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
for (const entry of entries) {
|
||||
const { width, height } = entry.contentRect;
|
||||
if (width > 0 && height > 0 && this.renderer && this.camera) {
|
||||
this.renderer.setSize(width, height);
|
||||
this.camera.aspect = width / height;
|
||||
this.camera.updateProjectionMatrix();
|
||||
}
|
||||
}
|
||||
});
|
||||
observer.observe(container);
|
||||
}
|
||||
|
||||
startAnimation(): void {
|
||||
if (this.animationId !== null) return;
|
||||
|
||||
this.ngZone.runOutsideAngular(() => {
|
||||
const animate = (): void => {
|
||||
this.animationId = requestAnimationFrame(animate);
|
||||
this.renderFrame();
|
||||
};
|
||||
animate();
|
||||
});
|
||||
}
|
||||
|
||||
stopAnimation(): void {
|
||||
if (this.animationId !== null) {
|
||||
cancelAnimationFrame(this.animationId);
|
||||
this.animationId = null;
|
||||
}
|
||||
}
|
||||
|
||||
private renderFrame(): void {
|
||||
if (!this.scene || !this.camera || !this.renderer || !this.orb || !this.orbGeometry) {
|
||||
return;
|
||||
}
|
||||
|
||||
const time = performance.now() * 0.001;
|
||||
const audioLevel = this.getAudioLevel();
|
||||
const speed = this.reducedMotion ? 0.35 : 1;
|
||||
const t = time * speed;
|
||||
|
||||
this.updateOrbGeometry(t, audioLevel);
|
||||
|
||||
if (this.glowShell) {
|
||||
const glowOpacity = 0.05 + audioLevel * 0.18;
|
||||
const glowScale = 1 + Math.sin(t * 1.5) * 0.025 + audioLevel * 0.06;
|
||||
this.glowShell.scale.setScalar(glowScale);
|
||||
if (this.glowShell.material instanceof THREE.MeshBasicMaterial) {
|
||||
this.glowShell.material.opacity = glowOpacity;
|
||||
}
|
||||
}
|
||||
|
||||
const pulse = 1 + Math.sin(t * 2) * 0.018 + audioLevel * 0.07;
|
||||
this.orb.scale.setScalar(pulse);
|
||||
|
||||
this.orb.rotation.y = t * 0.08;
|
||||
this.orb.rotation.x = Math.sin(t * 0.15) * 0.1;
|
||||
|
||||
this.renderer.render(this.scene, this.camera);
|
||||
}
|
||||
|
||||
private updateOrbGeometry(time: number, audioLevel: number): void {
|
||||
if (!this.orbGeometry || !this.originalPositions) return;
|
||||
|
||||
const positions = this.orbGeometry.attributes['position'];
|
||||
const original = this.originalPositions;
|
||||
|
||||
for (let i = 0; i < positions.count; i++) {
|
||||
const ox = original[i * 3];
|
||||
const oy = original[i * 3 + 1];
|
||||
const oz = original[i * 3 + 2];
|
||||
|
||||
const n1 =
|
||||
Math.sin(ox * 3 + time * 1.2) *
|
||||
Math.cos(oy * 3 + time * 0.8) *
|
||||
Math.sin(oz * 3 + time * 1.5) *
|
||||
0.06;
|
||||
|
||||
const n2 =
|
||||
Math.sin(ox * 6 + time * 2.3) *
|
||||
Math.cos(oy * 6 + time * 1.7) *
|
||||
Math.sin(oz * 6 + time * 2.1) *
|
||||
0.02;
|
||||
|
||||
const n3 =
|
||||
Math.cos(ox * 2 + time * 0.6) *
|
||||
Math.sin(oy * 2 + time * 1.1) *
|
||||
0.015;
|
||||
|
||||
const displacement = 1 + n1 + n2 + n3 + audioLevel * 0.12;
|
||||
|
||||
positions.setXYZ(i, ox * displacement, oy * displacement, oz * displacement);
|
||||
}
|
||||
|
||||
positions.needsUpdate = true;
|
||||
this.orbGeometry.computeVertexNormals();
|
||||
}
|
||||
|
||||
resetOrbGeometry(): void {
|
||||
if (!this.orbGeometry || !this.originalPositions) return;
|
||||
const positions = this.orbGeometry.attributes['position'];
|
||||
const original = this.originalPositions;
|
||||
for (let i = 0; i < positions.count; i++) {
|
||||
positions.setXYZ(i, original[i * 3], original[i * 3 + 1], original[i * 3 + 2]);
|
||||
}
|
||||
positions.needsUpdate = true;
|
||||
this.orbGeometry.computeVertexNormals();
|
||||
}
|
||||
|
||||
resize(container: HTMLElement): void {
|
||||
if (!this.renderer || !this.camera) return;
|
||||
const width = container.clientWidth || 280;
|
||||
const height = container.clientHeight || 280;
|
||||
this.renderer.setSize(width, height);
|
||||
this.camera.aspect = width / height;
|
||||
this.camera.updateProjectionMatrix();
|
||||
}
|
||||
|
||||
async startListening(): Promise<void> {
|
||||
if (this.isListening) return;
|
||||
|
||||
try {
|
||||
this.mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
this.audioContext = new AudioContext();
|
||||
const source = this.audioContext.createMediaStreamSource(this.mediaStream);
|
||||
this.analyser = this.audioContext.createAnalyser();
|
||||
this.analyser.fftSize = 256;
|
||||
this.analyser.smoothingTimeConstant = 0.75;
|
||||
source.connect(this.analyser);
|
||||
this.audioDataArray = new Uint8Array(this.analyser.frequencyBinCount);
|
||||
this.isListening = true;
|
||||
} catch (err) {
|
||||
console.warn('VoiceOrbService: unable to access microphone.', err);
|
||||
this.isListening = false;
|
||||
}
|
||||
}
|
||||
|
||||
stopListening(): void {
|
||||
if (this.mediaStream) {
|
||||
this.mediaStream.getTracks().forEach((track) => track.stop());
|
||||
this.mediaStream = null;
|
||||
}
|
||||
if (this.audioContext && this.audioContext.state !== 'closed') {
|
||||
this.audioContext.close().catch(() => {});
|
||||
}
|
||||
this.audioContext = null;
|
||||
this.analyser = null;
|
||||
this.audioDataArray = null;
|
||||
this.isListening = false;
|
||||
}
|
||||
|
||||
private getAudioLevel(): number {
|
||||
if (!this.isListening || !this.analyser || !this.audioDataArray) return 0;
|
||||
this.analyser.getByteFrequencyData(this.audioDataArray as Uint8Array<ArrayBuffer>);
|
||||
let sum = 0;
|
||||
for (let i = 0; i < this.audioDataArray.length; i++) {
|
||||
sum += this.audioDataArray[i];
|
||||
}
|
||||
return Math.min(1, sum / this.audioDataArray.length / 200);
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.stopAnimation();
|
||||
this.stopListening();
|
||||
|
||||
if (this.orbGeometry) this.orbGeometry.dispose();
|
||||
if (this.orb && this.orb.material instanceof THREE.Material) {
|
||||
this.orb.material.dispose();
|
||||
}
|
||||
if (this.glowShell) {
|
||||
if (this.glowShell.material instanceof THREE.Material) {
|
||||
this.glowShell.material.dispose();
|
||||
}
|
||||
if (this.glowShell.geometry) {
|
||||
this.glowShell.geometry.dispose();
|
||||
}
|
||||
}
|
||||
if (this.renderer) {
|
||||
this.renderer.dispose();
|
||||
const canvas = this.renderer.domElement;
|
||||
if (canvas.parentNode) {
|
||||
canvas.parentNode.removeChild(canvas);
|
||||
}
|
||||
}
|
||||
|
||||
this.scene = null;
|
||||
this.camera = null;
|
||||
this.renderer = null;
|
||||
this.orb = null;
|
||||
this.orbGeometry = null;
|
||||
this.glowShell = null;
|
||||
this.originalPositions = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,347 @@
|
||||
.ai-chat-input-wrapper {
|
||||
width: 100%;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
position: relative;
|
||||
min-height: 72px;
|
||||
}
|
||||
|
||||
.ai-chat-input-wrapper.voice-active {
|
||||
min-height: 380px;
|
||||
}
|
||||
|
||||
/* Layers transition */
|
||||
.composer-layer,
|
||||
.voice-layer {
|
||||
transition: opacity 0.35s ease, transform 0.35s ease;
|
||||
}
|
||||
|
||||
.composer-layer {
|
||||
position: relative;
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
pointer-events: auto;
|
||||
}
|
||||
|
||||
.voice-layer {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
opacity: 0;
|
||||
transform: translateY(10px);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.ai-chat-input-wrapper.voice-active .composer-layer {
|
||||
opacity: 0;
|
||||
transform: translateY(10px);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.ai-chat-input-wrapper.voice-active .voice-layer {
|
||||
position: relative;
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
pointer-events: auto;
|
||||
}
|
||||
|
||||
/* Chat Composer */
|
||||
.chat-composer {
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
gap: 0.375rem;
|
||||
padding: 0.625rem 0.75rem;
|
||||
background: var(--surface-bg);
|
||||
border: 1px solid var(--surface-border);
|
||||
border-radius: 1.5rem;
|
||||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05), 0 1px 3px rgba(0, 0, 0, 0.03);
|
||||
transition: box-shadow 0.2s ease, border-color 0.2s ease;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.chat-composer:focus-within {
|
||||
border-color: var(--color-primary);
|
||||
box-shadow: 0 4px 20px rgba(59, 130, 246, 0.12), 0 1px 3px rgba(0, 0, 0, 0.03);
|
||||
}
|
||||
|
||||
/* Textarea */
|
||||
.composer-textarea-wrapper {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.composer-textarea {
|
||||
width: 100%;
|
||||
min-height: 24px;
|
||||
max-height: 192px;
|
||||
padding: 0.375rem 0;
|
||||
border: none;
|
||||
background: transparent;
|
||||
color: var(--app-text);
|
||||
font-family: inherit;
|
||||
font-size: 0.9375rem;
|
||||
line-height: 1.6;
|
||||
resize: none;
|
||||
outline: none;
|
||||
text-align: right;
|
||||
direction: rtl;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.composer-textarea::placeholder {
|
||||
color: var(--muted);
|
||||
text-align: right;
|
||||
opacity: 0.8;
|
||||
}
|
||||
|
||||
.composer-textarea:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* Remove scrollbar but keep functionality */
|
||||
.composer-textarea::-webkit-scrollbar {
|
||||
width: 4px;
|
||||
}
|
||||
.composer-textarea::-webkit-scrollbar-thumb {
|
||||
background: var(--muted);
|
||||
border-radius: 9999px;
|
||||
opacity: 0.3;
|
||||
}
|
||||
|
||||
/* Action Buttons */
|
||||
.composer-btn {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
border: none;
|
||||
border-radius: 50%;
|
||||
background: transparent;
|
||||
color: var(--muted);
|
||||
cursor: pointer;
|
||||
transition: background 0.2s ease, color 0.2s ease, transform 0.15s ease;
|
||||
flex-shrink: 0;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.composer-btn:hover:not(:disabled) {
|
||||
background: var(--sidebar-hover);
|
||||
color: var(--text-primary);
|
||||
transform: scale(1.05);
|
||||
}
|
||||
|
||||
.composer-btn:active:not(:disabled) {
|
||||
transform: scale(0.95);
|
||||
}
|
||||
|
||||
.composer-btn:disabled {
|
||||
opacity: 0.35;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.composer-btn:focus-visible {
|
||||
outline: 2px solid var(--color-primary);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
.send-btn {
|
||||
background: var(--color-primary);
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
.send-btn:hover:not(:disabled) {
|
||||
background: #2563eb;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
/* Attachment Menu */
|
||||
.attachment-menu {
|
||||
position: absolute;
|
||||
bottom: calc(100% + 0.5rem);
|
||||
right: 0.5rem;
|
||||
min-width: 180px;
|
||||
background: var(--surface-bg);
|
||||
border: 1px solid var(--surface-border);
|
||||
border-radius: 0.875rem;
|
||||
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12), 0 2px 8px rgba(0, 0, 0, 0.06);
|
||||
overflow: hidden;
|
||||
z-index: 50;
|
||||
animation: menuFadeIn 0.15s ease;
|
||||
}
|
||||
|
||||
.attachment-menu-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.625rem;
|
||||
width: 100%;
|
||||
padding: 0.625rem 1rem;
|
||||
border: none;
|
||||
background: transparent;
|
||||
color: var(--text-secondary);
|
||||
font-family: inherit;
|
||||
font-size: 0.875rem;
|
||||
text-align: right;
|
||||
cursor: pointer;
|
||||
transition: background 0.15s ease;
|
||||
}
|
||||
|
||||
.attachment-menu-item:hover {
|
||||
background: var(--sidebar-hover);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.attachment-menu-item:focus-visible {
|
||||
outline: 2px solid var(--color-primary);
|
||||
outline-offset: -2px;
|
||||
background: var(--sidebar-hover);
|
||||
}
|
||||
|
||||
@keyframes menuFadeIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(4px) scale(0.97);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0) scale(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Voice Mode */
|
||||
.voice-mode {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 1.25rem;
|
||||
padding: 1.5rem;
|
||||
background: var(--surface-bg);
|
||||
border: 1px solid var(--surface-border);
|
||||
border-radius: 1.5rem;
|
||||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05), 0 1px 3px rgba(0, 0, 0, 0.03);
|
||||
min-height: 320px;
|
||||
}
|
||||
|
||||
.orb-container {
|
||||
width: 260px;
|
||||
height: 260px;
|
||||
position: relative;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.orb-container canvas {
|
||||
display: block;
|
||||
width: 100% !important;
|
||||
height: 100% !important;
|
||||
}
|
||||
|
||||
.voice-mode-text {
|
||||
font-size: 0.9375rem;
|
||||
color: var(--text-secondary);
|
||||
margin: 0;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.voice-stop-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.625rem 1.25rem;
|
||||
border: 1px solid var(--surface-border);
|
||||
border-radius: 9999px;
|
||||
background: var(--surface-bg);
|
||||
color: var(--text-secondary);
|
||||
font-family: inherit;
|
||||
font-size: 0.875rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.voice-stop-btn:hover {
|
||||
background: #ef4444;
|
||||
color: #ffffff;
|
||||
border-color: #ef4444;
|
||||
transform: scale(1.03);
|
||||
}
|
||||
|
||||
.voice-stop-btn:active {
|
||||
transform: scale(0.97);
|
||||
}
|
||||
|
||||
.voice-stop-btn:focus-visible {
|
||||
outline: 2px solid var(--color-primary);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
/* Screen reader only */
|
||||
.sr-only {
|
||||
position: absolute;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
padding: 0;
|
||||
margin: -1px;
|
||||
overflow: hidden;
|
||||
clip: rect(0, 0, 0, 0);
|
||||
white-space: nowrap;
|
||||
border: 0;
|
||||
}
|
||||
|
||||
/* Reduced motion */
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.composer-layer,
|
||||
.voice-layer,
|
||||
.chat-composer,
|
||||
.composer-btn,
|
||||
.voice-stop-btn,
|
||||
.attachment-menu {
|
||||
transition: none !important;
|
||||
animation: none !important;
|
||||
}
|
||||
}
|
||||
|
||||
/* Mobile responsiveness */
|
||||
@media (max-width: 640px) {
|
||||
.ai-chat-input-wrapper {
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.chat-composer {
|
||||
gap: 0.25rem;
|
||||
padding: 0.5rem 0.625rem;
|
||||
border-radius: 1.25rem;
|
||||
}
|
||||
|
||||
.composer-btn {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
}
|
||||
|
||||
.composer-btn svg {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
}
|
||||
|
||||
.composer-textarea {
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.orb-container {
|
||||
width: 200px;
|
||||
height: 200px;
|
||||
}
|
||||
|
||||
.voice-mode {
|
||||
min-height: 280px;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.attachment-menu {
|
||||
right: 0.25rem;
|
||||
min-width: 160px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
<div class="ai-chat-input-wrapper" [class.voice-active]="isVoiceMode()">
|
||||
<div class="composer-layer" [class.layer-hidden]="isVoiceMode()">
|
||||
<div class="chat-composer" #composer>
|
||||
<button
|
||||
type="button"
|
||||
class="composer-btn plus-btn"
|
||||
(click)="toggleAttachmentMenu()"
|
||||
[attr.aria-expanded]="isAttachmentMenuOpen()"
|
||||
aria-label="افزودن پیوست"
|
||||
>
|
||||
<svg
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d="M12 5v14M5 12h14" />
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
<div class="composer-textarea-wrapper">
|
||||
<textarea
|
||||
#textarea
|
||||
class="composer-textarea"
|
||||
[placeholder]="placeholder()"
|
||||
[disabled]="disabled()"
|
||||
(input)="onTextareaInput()"
|
||||
(keydown)="onKeyDown($event)"
|
||||
rows="1"
|
||||
dir="rtl"
|
||||
aria-label="متن پیام"
|
||||
></textarea>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="composer-btn mic-btn"
|
||||
(click)="toggleVoiceMode()"
|
||||
aria-label="حالت صوتی"
|
||||
>
|
||||
<svg
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d="M12 2a3 3 0 0 0-3 3v7a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3Z" />
|
||||
<path d="M19 10v2a7 7 0 0 1-14 0v-2" />
|
||||
<line x1="12" x2="12" y1="19" y2="22" />
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="composer-btn send-btn"
|
||||
[disabled]="!message().trim() || disabled()"
|
||||
(click)="sendMessage()"
|
||||
aria-label="ارسال پیام"
|
||||
>
|
||||
<svg
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d="M12 19V5M5 12l7-7 7 7" />
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
@if (isAttachmentMenuOpen()) {
|
||||
<div class="attachment-menu" role="menu">
|
||||
<button
|
||||
type="button"
|
||||
class="attachment-menu-item"
|
||||
role="menuitem"
|
||||
(click)="selectFile(); closeAttachmentMenu();"
|
||||
>
|
||||
<svg
|
||||
width="18"
|
||||
height="18"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d="M14.5 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7.5L14.5 2z" />
|
||||
<polyline points="14 2 14 8 20 8" />
|
||||
</svg>
|
||||
<span>آپلود فایل</span>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="attachment-menu-item"
|
||||
role="menuitem"
|
||||
(click)="selectFile(); closeAttachmentMenu();"
|
||||
>
|
||||
<svg
|
||||
width="18"
|
||||
height="18"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<rect x="3" y="3" width="18" height="18" rx="2" ry="2" />
|
||||
<circle cx="8.5" cy="8.5" r="1.5" />
|
||||
<polyline points="21 15 16 10 5 21" />
|
||||
</svg>
|
||||
<span>آپلود تصویر</span>
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
|
||||
<input
|
||||
#fileInput
|
||||
type="file"
|
||||
class="sr-only"
|
||||
(change)="onFileChange($event)"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="voice-layer" [class.layer-hidden]="!isVoiceMode()">
|
||||
<div class="voice-mode" #voiceContainer>
|
||||
<div class="orb-container" #orbContainer></div>
|
||||
<p class="voice-mode-text">در حال گوش دادن...</p>
|
||||
<button
|
||||
type="button"
|
||||
class="voice-stop-btn"
|
||||
(click)="exitVoiceMode()"
|
||||
aria-label="خروج از حالت صوتی"
|
||||
>
|
||||
<svg
|
||||
width="18"
|
||||
height="18"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<line x1="18" x2="6" y1="6" y2="18" />
|
||||
<line x1="6" x2="18" y1="6" y2="18" />
|
||||
</svg>
|
||||
<span>پایان گفتگو</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,177 @@
|
||||
import {
|
||||
ChangeDetectionStrategy,
|
||||
Component,
|
||||
input,
|
||||
output,
|
||||
signal,
|
||||
ViewChild,
|
||||
ElementRef,
|
||||
AfterViewChecked,
|
||||
OnInit,
|
||||
OnDestroy,
|
||||
inject,
|
||||
} from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { VoiceOrbService } from '../../../services/voice-orb/voice-orb.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-ai-chat-input',
|
||||
standalone: true,
|
||||
imports: [CommonModule],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
templateUrl: './ai-chat-input.component.html',
|
||||
styleUrl: './ai-chat-input.component.css',
|
||||
})
|
||||
export class AiChatInputComponent implements AfterViewChecked, OnInit, OnDestroy {
|
||||
private readonly voiceOrbService = inject(VoiceOrbService);
|
||||
|
||||
readonly placeholder = input<string>('پیام خود را بنویسید...');
|
||||
readonly disabled = input<boolean>(false);
|
||||
|
||||
readonly messageSent = output<string>();
|
||||
readonly fileSelected = output<File>();
|
||||
readonly voiceToggled = output<boolean>();
|
||||
|
||||
readonly message = signal('');
|
||||
readonly isVoiceMode = signal(false);
|
||||
readonly isAttachmentMenuOpen = signal(false);
|
||||
|
||||
@ViewChild('textarea') textareaRef!: ElementRef<HTMLTextAreaElement>;
|
||||
@ViewChild('composer') composerRef!: ElementRef<HTMLDivElement>;
|
||||
@ViewChild('voiceContainer') voiceContainerRef!: ElementRef<HTMLDivElement>;
|
||||
@ViewChild('orbContainer') orbContainerRef!: ElementRef<HTMLDivElement>;
|
||||
@ViewChild('fileInput') fileInputRef!: ElementRef<HTMLInputElement>;
|
||||
|
||||
private orbInited = false;
|
||||
private menuCloseHandler: ((event: MouseEvent) => void) | null = null;
|
||||
private escapeHandler: ((event: KeyboardEvent) => void) | null = null;
|
||||
|
||||
selectFile(): void {
|
||||
this.fileInputRef?.nativeElement.click();
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.attachDocumentListeners();
|
||||
}
|
||||
|
||||
ngAfterViewChecked(): void {
|
||||
if (this.isVoiceMode() && !this.orbInited && this.orbContainerRef) {
|
||||
this.orbInited = true;
|
||||
this.voiceOrbService.init(this.orbContainerRef.nativeElement);
|
||||
this.voiceOrbService.resetOrbGeometry();
|
||||
this.voiceOrbService.startAnimation();
|
||||
this.voiceOrbService.startListening().catch(() => {
|
||||
/* orb continues without audio reactivity */
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.detachDocumentListeners();
|
||||
this.voiceOrbService.dispose();
|
||||
}
|
||||
|
||||
onTextareaInput(): void {
|
||||
const textarea = this.textareaRef?.nativeElement;
|
||||
if (!textarea) return;
|
||||
|
||||
this.message.set(textarea.value);
|
||||
|
||||
textarea.style.height = 'auto';
|
||||
const scrollHeight = textarea.scrollHeight;
|
||||
const maxHeight = 192;
|
||||
const newHeight = Math.min(scrollHeight, maxHeight);
|
||||
textarea.style.height = newHeight + 'px';
|
||||
}
|
||||
|
||||
onKeyDown(event: KeyboardEvent): void {
|
||||
if (event.key === 'Enter' && !event.shiftKey) {
|
||||
event.preventDefault();
|
||||
this.sendMessage();
|
||||
}
|
||||
}
|
||||
|
||||
sendMessage(): void {
|
||||
const text = this.message().trim();
|
||||
if (!text) return;
|
||||
|
||||
this.messageSent.emit(text);
|
||||
this.message.set('');
|
||||
|
||||
const textarea = this.textareaRef?.nativeElement;
|
||||
if (textarea) {
|
||||
textarea.value = '';
|
||||
textarea.style.height = 'auto';
|
||||
}
|
||||
}
|
||||
|
||||
toggleAttachmentMenu(): void {
|
||||
this.isAttachmentMenuOpen.update((open) => !open);
|
||||
}
|
||||
|
||||
closeAttachmentMenu(): void {
|
||||
this.isAttachmentMenuOpen.set(false);
|
||||
}
|
||||
|
||||
onFileChange(event: Event): void {
|
||||
const input = event.target as HTMLInputElement;
|
||||
const file = input.files?.[0];
|
||||
if (file) {
|
||||
this.fileSelected.emit(file);
|
||||
}
|
||||
if (input.value) {
|
||||
input.value = '';
|
||||
}
|
||||
this.closeAttachmentMenu();
|
||||
}
|
||||
|
||||
toggleVoiceMode(): void {
|
||||
if (this.isVoiceMode()) {
|
||||
this.exitVoiceMode();
|
||||
} else {
|
||||
this.enterVoiceMode();
|
||||
}
|
||||
}
|
||||
|
||||
private enterVoiceMode(): void {
|
||||
this.isVoiceMode.set(true);
|
||||
this.orbInited = false;
|
||||
this.voiceToggled.emit(true);
|
||||
}
|
||||
|
||||
exitVoiceMode(): void {
|
||||
this.voiceOrbService.stopAnimation();
|
||||
this.voiceOrbService.stopListening();
|
||||
this.isVoiceMode.set(false);
|
||||
this.voiceToggled.emit(false);
|
||||
}
|
||||
|
||||
private attachDocumentListeners(): void {
|
||||
this.menuCloseHandler = (event: MouseEvent) => {
|
||||
if (!this.isAttachmentMenuOpen()) return;
|
||||
const target = event.target as Node;
|
||||
if (this.composerRef && this.composerRef.nativeElement.contains(target)) {
|
||||
return;
|
||||
}
|
||||
this.closeAttachmentMenu();
|
||||
};
|
||||
|
||||
this.escapeHandler = (event: KeyboardEvent) => {
|
||||
if (event.key === 'Escape' && this.isAttachmentMenuOpen()) {
|
||||
this.closeAttachmentMenu();
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('mousedown', this.menuCloseHandler);
|
||||
document.addEventListener('keydown', this.escapeHandler);
|
||||
}
|
||||
|
||||
private detachDocumentListeners(): void {
|
||||
if (this.menuCloseHandler) {
|
||||
document.removeEventListener('mousedown', this.menuCloseHandler);
|
||||
}
|
||||
if (this.escapeHandler) {
|
||||
document.removeEventListener('keydown', this.escapeHandler);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user