]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/misc/help.component.ts
Migrate to $localize
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / misc / help.component.ts
1 import { AfterContentInit, Component, ContentChildren, Input, OnChanges, OnInit, QueryList, TemplateRef } from '@angular/core'
2 import { MarkdownService } from '@app/core'
3 import { PeerTubeTemplateDirective } from '../angular'
4
5 @Component({
6 selector: 'my-help',
7 styleUrls: [ './help.component.scss' ],
8 templateUrl: './help.component.html'
9 })
10
11 export class HelpComponent implements OnInit, OnChanges, AfterContentInit {
12 @Input() helpType: 'custom' | 'markdownText' | 'markdownEnhanced' = 'custom'
13 @Input() tooltipPlacement = 'right auto'
14
15 @ContentChildren(PeerTubeTemplateDirective) templates: QueryList<PeerTubeTemplateDirective<'preHtml' | 'customHtml' | 'postHtml'>>
16
17 isPopoverOpened = false
18 mainHtml = ''
19
20 preHtmlTemplate: TemplateRef<any>
21 customHtmlTemplate: TemplateRef<any>
22 postHtmlTemplate: TemplateRef<any>
23
24 ngOnInit () {
25 this.init()
26 }
27
28 ngAfterContentInit () {
29 {
30 const t = this.templates.find(t => t.name === 'preHtml')
31 if (t) this.preHtmlTemplate = t.template
32 }
33
34 {
35 const t = this.templates.find(t => t.name === 'customHtml')
36 if (t) this.customHtmlTemplate = t.template
37 }
38
39 {
40 const t = this.templates.find(t => t.name === 'postHtml')
41 if (t) this.postHtmlTemplate = t.template
42 }
43 }
44
45 ngOnChanges () {
46 this.init()
47 }
48
49 onPopoverHidden () {
50 this.isPopoverOpened = false
51 }
52
53 onPopoverShown () {
54 this.isPopoverOpened = true
55 }
56
57 private init () {
58 if (this.helpType === 'markdownText') {
59 this.mainHtml = this.formatMarkdownSupport(MarkdownService.TEXT_RULES)
60 return
61 }
62
63 if (this.helpType === 'markdownEnhanced') {
64 this.mainHtml = this.formatMarkdownSupport(MarkdownService.ENHANCED_RULES)
65 return
66 }
67 }
68
69 private formatMarkdownSupport (rules: string[]) {
70 // tslint:disable:max-line-length
71 return $localize`<a href="https://en.wikipedia.org/wiki/Markdown#Example" target="_blank" rel="noopener noreferrer">Markdown</a> compatible that supports:` +
72 this.createMarkdownList(rules)
73 }
74
75 private createMarkdownList (rules: string[]) {
76 const rulesToText = {
77 'emphasis': $localize`Emphasis`,
78 'link': $localize`Links`,
79 'newline': $localize`New lines`,
80 'list': $localize`Lists`,
81 'image': $localize`Images`
82 }
83
84 const bullets = rules.map(r => rulesToText[r])
85 .filter(text => text)
86 .map(text => '<li>' + text + '</li>')
87 .join('')
88
89 return '<ul>' + bullets + '</ul>'
90 }
91 }