</ng-template>
</ng-template>
-<button class="help-tooltip-button" containerClass="help-tooltip" [tooltipHtml]="tooltipTemplate" triggers="focus"></button>
+<button
+ class="help-tooltip-button" containerClass="help-tooltip" title="Click to get help"
+ #tooltipDirective="bs-tooltip" [tooltip]="tooltipTemplate" triggers="click"
+></button>
.tooltip-inner {
text-align: left;
padding: 10px;
+ max-width: 300px;
font-size: 13px;
font-family: $main-fonts;
-import { Component, Input, OnInit } from '@angular/core'
+import { Component, ElementRef, HostListener, Input, OnInit, ViewChild } from '@angular/core'
+import { MarkdownService } from '@app/videos/shared'
+import { TooltipDirective } from 'ngx-bootstrap/tooltip'
@Component({
selector: 'my-help',
})
export class HelpComponent implements OnInit {
+ @ViewChild('tooltipDirective') tooltipDirective: TooltipDirective
@Input() preHtml = ''
@Input() postHtml = ''
@Input() customHtml = ''
mainHtml = ''
+ constructor (private elementRef: ElementRef) { }
+
ngOnInit () {
if (this.helpType === 'custom') {
this.mainHtml = this.customHtml
}
if (this.helpType === 'markdownText') {
- this.mainHtml = 'Markdown compatible.<br /><br />' +
- 'Supports:' +
- '<ul>' +
- '<li>Links</li>' +
- '<li>Lists</li>' +
- '<li>Emphasis</li>' +
- '</ul>'
+ this.mainHtml = this.formatMarkdownSupport(MarkdownService.TEXT_RULES)
return
}
if (this.helpType === 'markdownEnhanced') {
- this.mainHtml = 'Markdown compatible.<br /><br />' +
- 'Supports:' +
- '<ul>' +
- '<li>Links</li>' +
- '<li>Lists</li>' +
- '<li>Emphasis</li>' +
- '<li>Images</li>' +
- '</ul>'
+ this.mainHtml = this.formatMarkdownSupport(MarkdownService.ENHANCED_RULES)
return
}
}
+
+ @HostListener('document:click', ['$event.target'])
+ public onClick (targetElement) {
+ const clickedInside = this.elementRef.nativeElement.contains(targetElement)
+
+ if (this.tooltipDirective.isOpen && !clickedInside) {
+ this.tooltipDirective.hide()
+ }
+ }
+
+ private formatMarkdownSupport (rules: string[]) {
+ return '<a href="https://en.wikipedia.org/wiki/Markdown#Example" target="_blank">Markdown</a> compatible that supports:' +
+ this.createMarkdownList(rules)
+ }
+
+ private createMarkdownList (rules: string[]) {
+ const rulesToText = {
+ 'emphasis': 'Emphasis',
+ 'link': 'Links',
+ 'newline': 'New lines',
+ 'list': 'Lists',
+ 'image': 'Images'
+ }
+
+ const bullets = rules.map(r => rulesToText[r])
+ .filter(text => text)
+ .map(text => '<li>' + text + '</li>')
+ .join('')
+
+ return '<ul>' + bullets + '</ul>'
+ }
}
</div>
<div class="form-group">
- <label for="description">Description</label> <my-help helpType="markdownText"></my-help>
+ <label for="description">Description</label>
+ <my-help helpType="markdownText" preHtml="Video descriptions are truncated by default and require manual action to expand them."></my-help>
<my-markdown-textarea truncate="250" formControlName="description"></my-markdown-textarea>
<div *ngIf="formErrors.description" class="form-error">
</div>
<div class="form-group">
- <label for="support">Support</label><my-help helpType="markdownEnhanced"></my-help>
+ <label for="support">Support</label>
+ <my-help helpType="markdownEnhanced" preHtml="Short text to tell to people how they can support you (membership platform...)."></my-help>
<my-markdown-textarea
id="support" formControlName="support" textareaWidth="500px" [previewColumn]="true" markdownType="enhanced"
[classes]="{ 'input-error': formErrors['support'] }"
@Injectable()
export class MarkdownService {
+ static TEXT_RULES = [
+ 'linkify',
+ 'autolink',
+ 'emphasis',
+ 'link',
+ 'newline',
+ 'list'
+ ]
+ static ENHANCED_RULES = MarkdownService.TEXT_RULES.concat([ 'image' ])
+
private textMarkdownIt: MarkdownIt.MarkdownIt
private enhancedMarkdownIt: MarkdownIt.MarkdownIt
constructor () {
- this.textMarkdownIt = new MarkdownIt('zero', { linkify: true, breaks: true })
- .enable('linkify')
- .enable('autolink')
- .enable('emphasis')
- .enable('link')
- .enable('newline')
- .enable('list')
- this.setTargetToLinks(this.textMarkdownIt)
-
- this.enhancedMarkdownIt = new MarkdownIt('zero', { linkify: true, breaks: true })
- .enable('linkify')
- .enable('autolink')
- .enable('emphasis')
- .enable('link')
- .enable('newline')
- .enable('list')
- .enable('image')
- this.setTargetToLinks(this.enhancedMarkdownIt)
+ this.textMarkdownIt = this.createMarkdownIt(MarkdownService.TEXT_RULES)
+ this.enhancedMarkdownIt = this.createMarkdownIt(MarkdownService.ENHANCED_RULES)
}
textMarkdownToHTML (markdown: string) {
return this.avoidTruncatedLinks(html)
}
+ private createMarkdownIt (rules: string[]) {
+ const markdownIt = new MarkdownIt('zero', { linkify: true, breaks: true })
+
+ for (let rule of rules) {
+ markdownIt.enable(rule)
+ }
+
+ this.setTargetToLinks(markdownIt)
+
+ return markdownIt
+ }
+
private setTargetToLinks (markdownIt: MarkdownIt.MarkdownIt) {
// Snippet from markdown-it documentation: https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer
const defaultRender = markdownIt.renderer.rules.link_open || function (tokens, idx, options, env, self) {