Better help on markdown fields
authorChocobozzz <me@florianbigard.com>
Fri, 23 Feb 2018 09:05:17 +0000 (10:05 +0100)
committerChocobozzz <me@florianbigard.com>
Fri, 23 Feb 2018 09:05:17 +0000 (10:05 +0100)
client/src/app/shared/misc/help.component.html
client/src/app/shared/misc/help.component.scss
client/src/app/shared/misc/help.component.ts
client/src/app/videos/+video-edit/shared/video-edit.component.html
client/src/app/videos/shared/markdown.service.ts

index 956095996c13a918f547a73ae329fcf14fc7a395..bacbe9cdbb5950941740e164737f01ce4f7304f6 100644 (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>
index 5fe6b736695ebc0c400d7ee08dfe59004efa4481..b8bf3a7a5c95ad2dca2661fba52730d301f0bb67 100644 (file)
@@ -18,6 +18,7 @@
     .tooltip-inner {
       text-align: left;
       padding: 10px;
+      max-width: 300px;
 
       font-size: 13px;
       font-family: $main-fonts;
index b8530e1d4739c2db3ffb95022996808033cc638c..a4a223cd6108d87b047ddfe9c039b8806f8f9989 100644 (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>'
+  }
 }
index f88abcbf82927a313687a9977d83b087c19e2bea..f4ec4f95c5d7792d4c4785af76d3aa6a8dd1cbc4 100644 (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">
         </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'] }"
index fdd0ec8d2786364ab357afdb7155b7746d5e1acb..9d73efa4640695eed3b9c7d2650f13a87472044e 100644 (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) {