Better help on markdown fields

This commit is contained in:
Chocobozzz 2018-02-23 10:05:17 +01:00
parent 8a8e02a43e
commit 621d99f53f
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
5 changed files with 72 additions and 37 deletions

View File

@ -12,4 +12,7 @@
</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>

View File

@ -18,6 +18,7 @@
.tooltip-inner {
text-align: left;
padding: 10px;
max-width: 300px;
font-size: 13px;
font-family: $main-fonts;

View File

@ -1,4 +1,6 @@
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',
@ -7,6 +9,7 @@ import { Component, Input, OnInit } from '@angular/core'
})
export class HelpComponent implements OnInit {
@ViewChild('tooltipDirective') tooltipDirective: TooltipDirective
@Input() preHtml = ''
@Input() postHtml = ''
@Input() customHtml = ''
@ -14,6 +17,8 @@ export class HelpComponent implements OnInit {
mainHtml = ''
constructor (private elementRef: ElementRef) { }
ngOnInit () {
if (this.helpType === 'custom') {
this.mainHtml = this.customHtml
@ -21,26 +26,44 @@ export class HelpComponent implements OnInit {
}
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>'
}
}

View File

@ -20,7 +20,8 @@
</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">
@ -127,7 +128,8 @@
</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'] }"

View File

@ -4,28 +4,22 @@ import * as MarkdownIt from 'markdown-it'
@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) {
@ -40,6 +34,18 @@ export class MarkdownService {
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) {