|
1 | | -import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; |
| 1 | +import 'quill-emoji/dist/quill-emoji.js'; |
| 2 | +import { Component, EventEmitter, Input, Output, SecurityContext } from '@angular/core'; |
| 3 | +import { ContentChange, QuillModules } from 'ngx-quill'; |
| 4 | +import { DomSanitizer } from '@angular/platform-browser'; |
2 | 5 |
|
3 | 6 | @Component({ |
4 | 7 | selector: 'ds-markdown-editor', |
5 | 8 | templateUrl: './markdown-editor.component.html', |
6 | 9 | styleUrls: ['./markdown-editor.component.scss'] |
7 | 10 | }) |
8 | | -export class MarkdownEditorComponent implements OnInit { |
9 | | - // to allow multiple textarea on the same screen, need to set an uniqueId for the textarea |
10 | | - controlId: string; |
| 11 | +export class MarkdownEditorComponent { |
11 | 12 | /** |
12 | 13 | * Markdown Editor String value |
13 | 14 | */ |
14 | | - @Input() editValue: string; |
| 15 | + @Input() editValue = ''; |
15 | 16 | /** |
16 | 17 | * Markdown Editor String value Emitter |
17 | 18 | */ |
18 | 19 | @Output() editValueChange = new EventEmitter(); |
| 20 | + |
19 | 21 | /** |
20 | | - * Nu markdown library options (default is chinese) |
| 22 | + * Quill modules config |
21 | 23 | */ |
22 | | - options = { |
23 | | - minHeight: 200, |
24 | | - lang: 'en_US', |
25 | | - mode: 'ir', |
26 | | - preview: { |
27 | | - markdown: { |
28 | | - codeBlockPreview: false, |
29 | | - }, |
30 | | - actions: [ |
31 | | - 'desktop', 'tablet', 'mobile' |
32 | | - ] |
| 24 | + modules: QuillModules = { |
| 25 | + 'emoji-toolbar': true, |
| 26 | + toolbar: { |
| 27 | + container: [ |
| 28 | + ['bold', 'italic', 'underline', 'strike'], |
| 29 | + [{ 'header': 1 }, { 'header': 2 }], |
| 30 | + [{ 'list': 'ordered'}, { 'list': 'bullet' }], |
| 31 | + [{ 'indent': '-1'}, { 'indent': '+1' }], |
| 32 | + [{ 'direction': 'rtl' }], |
| 33 | + [{ 'size': ['small', false, 'large', 'huge'] }], |
| 34 | + [{ 'header': [1, 2, 3, 4, 5, 6, false] }], |
| 35 | + [{ 'font': [] }], |
| 36 | + [{ 'align': [] }], |
| 37 | + ['clean'], |
| 38 | + ['emoji'], |
| 39 | + ], |
33 | 40 | }, |
34 | | - toolbar: [ |
35 | | - 'emoji', 'headings', 'bold', 'italic', 'strike', 'link', '|', |
36 | | - 'list', 'ordered-list', 'check', 'outdent', 'indent', 'table', '|', |
37 | | - 'quote', 'line', 'code', 'inline-code', 'insert-before', 'insert-after', '|', |
38 | | - 'undo', 'redo', '|', |
39 | | - 'fullscreen', 'preview' |
40 | | - ], |
| 41 | + syntax: false |
41 | 42 | }; |
42 | 43 |
|
43 | | - ngOnInit(): void { |
44 | | - this.controlId = `MarkdownEditor-${Math.floor(100000 * Math.random())}`; |
45 | | - } |
| 44 | + constructor(private sanitizer: DomSanitizer) {} |
| 45 | + |
46 | 46 |
|
| 47 | + /** |
| 48 | + * Emit content update after editing |
| 49 | + * @param content |
| 50 | + */ |
| 51 | + updateContent(content: ContentChange) { |
| 52 | + const sanitizedContent = this.sanitizer.sanitize(SecurityContext.HTML, content.html); |
| 53 | + this.editValueChange.emit(sanitizedContent); |
| 54 | +} |
47 | 55 | } |
0 commit comments