All files / src/fieldcontrols rich-text.ts

17.24% Statements 5/29
0% Branches 0/18
0% Functions 0/7
18.52% Lines 5/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 901x         1x 1x       1x   1x                                                                                                                                                          
import { FieldSettings, Content, Resources, Retrier } from 'sn-client-js';
import { bindable, computedFrom } from 'aurelia-framework';
import { IFieldControl } from './IFieldControl';
 
import { AureliaClientFieldConfig } from '../AureliaClientFieldConfig';
import { FieldBaseControl } from './FieldBaseControl';
import { ValidationRules, FluentRules } from 'aurelia-validation';
 
import { Quill as QuillType } from 'quill';
 
const Quill = require('quill') as QuillType;
 
export class RichText extends FieldBaseControl<string, FieldSettings.LongTextFieldSetting> {
 
    private quillElementRef: HTMLDivElement;
    private containerRef: HTMLDivElement;
    private quill: QuillType;
 
    isSelected: boolean = false;
    textValue: string;
    get rules(): any {
        const parentRules = super.rules || [];
        let thisRules = ValidationRules
            .ensure('value')
            .minLength(this.settings.MinLength || 0)
            .maxLength(this.settings.MaxLength || Infinity);
 
        return [...(parentRules ? parentRules : []), ...(thisRules.rules ? thisRules.rules : [])];
    }
 
    private async initializeQuill() {
        await Retrier.Create(async () => { return this.quillElementRef !== undefined; })
            .Run();
        if (this.quillElementRef && !this.quill) {
            this.quill = new Quill(this.quillElementRef, {
                modules: {
                    toolbar:
                    [
                        ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
                        ['blockquote', 'code-block'],
 
                        [{ 'list': 'ordered' }, { 'list': 'bullet' }],
                        [{ 'script': 'sub' }, { 'script': 'super' }],      // superscript/subscript
                        [{ 'indent': '-1' }, { 'indent': '+1' }],          // outdent/indent
                        [{ 'direction': 'rtl' }],                         // text direction
 
                        [
                            { 'size': ['small', false, 'large', 'huge'] },   // custom dropdown
                            { 'header': [1, 2, 3, 4, 5, 6, false] }
                        ],
 
                        [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
                        [{ 'align': [] }],
 
                        ['clean']                                         // remove formatting button
                    ]
                },
                theme: 'snow'
            });
 
            this.quill.on('selection-change', (range, oldRange, source) => {
                this.isSelected = range !== null;
                if (!this.isSelected){
                    let selectedNode: Node | null = window.getSelection().baseNode;
                    while (selectedNode != null) {
                        if (selectedNode === this.containerRef) {
                            this.isSelected = true;
                            return;
                        }
                        selectedNode = selectedNode.parentNode;
                    }
                    this.isSelected = false;
                }
            })
            .on('text-change', (delta, oldDelta, source) => {
                this.value = (this.quill as any).root.innerHTML;
                this.textValue = this.quill.getText().trim();
            });
            (this.quill as any).root.innerHTML = this.value;
        }
    }
 
    activate(model) {
        super.activate(model);
        this.initializeQuill();
 
    }
}