All files / lib/internal/conversation-item conversation-item.component.ts

82.57% Statements 218/264
100% Branches 0/0
0% Functions 0/10
82.57% Lines 218/264

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 2651x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x                 1x 1x 1x     1x 1x       1x 1x 1x     1x 1x 1x     1x 1x 1x           1x 1x 1x                                   1x 1x 1x           1x  
import {
  Component,
  ElementRef,
  EventEmitter,
  HostBinding,
  HostListener,
  Input,
  OnChanges,
  OnDestroy,
  Output,
  SimpleChanges,
  ViewChild,
} from '@angular/core';
 
import { FocusableElementOwner, Key } from '../../utils/util.types';
 
import {
  AriaLiveReadDisposer,
  AriaLiveReaderComponent,
} from '../aria-live-reader/aria-live-reader.component';
 
export type ConversationItemType = 'Hint' | 'Question' | 'Answer';
 
/**
 * Markdown style structured text nodes
 */
export type StructuredTextNode =
  | ListNode
  | ParagraphNode
  | StrongNode
  | EmNode
  | TextNode;
 
/**
 * Inline nodes
 */
export type InlineNode = StrongNode | EmNode | TextNode;
 
/**
 * A List
 */
export interface ListNode {
  /** key */
  key: string;
  /** type discriminator */
  type: 'list';
  /** is it an ordered list */
  ordered: boolean;
  /** list items */
  nodes: ListItemNode[];
}
 
/**
 * An item of a list
 */
export interface ListItemNode {
  /** key */
  key: string;
  /** type discriminator */
  type: 'list_item';
  /** content of the item */
  nodes: StructuredTextNode[];
}
 
/**
 * A paragraph
 */
export interface ParagraphNode {
  /** key */
  key: string;
  /** type discriminator */
  type: 'paragraph';
  /** content of the item */
  nodes: InlineNode[];
}
 
/**
 * A text rendered in bold
 */
export interface StrongNode {
  /** key */
  key: string;
  /** type discriminator */
  type: 'strong';
  nodes: InlineNode[];
}
 
export interface EmNode {
  /** key */
  key: string;
  /** type discriminator */
  type: 'em';
  nodes: InlineNode[];
}
 
export interface TextNode {
  /** key */
  key: string;
  /** type discriminator */
  type: 'text';
  text: string;
}
 
@Component({
  selector: 'gc-conversation-item',
  templateUrl: './conversation-item.component.html',
  styleUrl: './conversation-item.component.css',
  standalone: false,
})
export class ConversationItemComponent
  implements FocusableElementOwner, OnChanges, OnDestroy
{
  @Output()
  public readonly onRateItem: EventEmitter<Key> = new EventEmitter<Key>();
 
  @Output()
  public readonly onFocus: EventEmitter<ConversationItemComponent> =
    new EventEmitter<ConversationItemComponent>();
 
  @Input()
  public key?: Key;
 
  @Input()
  public type?: ConversationItemType;
 
  @Input()
  public text?: string | StructuredTextNode[];
 
  @Input()
  public time?: string;
 
  @Input()
  public rating?: string;
 
  @Input()
  public partial?: boolean;
 
  @Input()
  public keyboardFocusable?: boolean;
 
  /** @ignore */
  @ViewChild('ariaLiveReader')
  private readonly ariaLiveReader?: AriaLiveReaderComponent;
 
  /** @ignore */
  @HostBinding('attr.tabindex')
  protected get _tabIndex() {
    return this.keyboardFocusable ? 0 : -1;
  }
 
  /** @ignore */
  @HostBinding('class.gc-answer')
  protected get _gcAnswer() {
    return this.type === 'Answer';
  }
 
  /** @ignore */
  @HostBinding('class.gc-hint')
  protected get _gcHint() {
    return this.type === 'Hint';
  }
 
  /** @ignore */
  @HostBinding('class.gc-question')
  protected get _gcQuestion() {
    return this.type === 'Question';
  }
 
  protected get hasContent() {
    if (this.text) {
      if (typeof this.text === 'string') {
        return this.text !== '';
      } else {
        return this.text.length > 0;
      }
    }
    return false;
  }
 
  protected get structuredContent(): StructuredTextNode[] {
    if (this.text && typeof this.text !== 'string') {
      return this.text;
    }
    return [];
  }
 
  /** @ignore */
  private _reading?: AriaLiveReadDisposer;
 
  constructor(private readonly host: ElementRef<HTMLElement>) {}
 
  /** @ignore */
  @HostListener('focus')
  protected _handle_onfocus() {
    this.onFocus.emit(this);
  }
 
  /** @ignore */
  ngOnChanges(changes: SimpleChanges): void {
    if ('partial' in changes) {
      if (this.partial) {
        this._impl_startReadState();
      } else {
        this._impl_stopReadState();
      }
    }
  }
 
  /** @ignore */
  ngOnDestroy(): void {
    this._reading?.();
  }
 
  public focusChild(): boolean {
    this.host.nativeElement.focus();
    return true;
  }
 
  /** @ignore */
  protected _impl_isFocused() {
    return document.activeElement === this.host.nativeElement;
  }
 
  /** @ignore */
  protected _impl_triggerRating() {
    this.onRateItem.emit(this.key);
  }
 
  /** @ignore */
  private _impl_startReadState() {
    const doRead = (r: AriaLiveReaderComponent) => {
      this._reading = r.readText('Antwort wird generiert', 2000, 10000);
    };
    this.useTimeoutIfUninitialized(() => this.ariaLiveReader, doRead);
  }
 
  /** @ignore */
  private useTimeoutIfUninitialized<V>(
    val: () => V | undefined,
    action: (val: V) => void,
  ) {
    const v = val();
    if (v) {
      action(v);
    } else {
      setTimeout(() => {
        const v2 = val();
        if (v2) {
          action(v2);
        } else {
          console.warn('useTimeoutIfUninitialized failed');
        }
      }, 0);
    }
  }
 
  /** @ignore */
  private _impl_stopReadState() {
    if (this._reading) {
      this._reading();
      this._reading = undefined;
    }
  }
}