onWordShared()

in client/src/pages/translate/translate.ts [219:252]


  onWordShared(word: WordTranslation) {
    const selectedWord = this.selectedWord;
    const shareTitle = this.i18n.getTranslation('shareTitle') || undefined;
    const shareText = selectedWord ? this.i18n.getTranslation('shareText', {
      original: selectedWord.original || selectedWord.english,
      translation: selectedWord.translation,
      language: this.endangeredLanguageService.currentLanguage.name
    }) || undefined : undefined;
    const img = this._sharedImage;
    if (!img) {
      // image not rendered - default to sharing text
      logger.warn('Shared image data not found');
      share({ text: shareText, title: shareTitle }).then(
        () => { },
        ex => logger.warn('Error sharing image', ex)
      );
      return;
    }
    const files: File[] = [new File([img], `woolaroo-translation-${word.original}.jpg`, { type: img.type })];
    share({ text: shareText, title: shareTitle, files }).then(
      () => { },
      ex => {
        logger.warn('Error sharing image', ex);
        if (ex instanceof NotSupportedError) {
          // sharing not supported - default to downloading image
          try {
            downloadFile(img, `woolaroo-translation-${word.original}.jpg`);
          } catch (err) {
            logger.warn('Error downloading image', err);
          }
        }
      }
    );
  }