lesson-title-config.component.ts 6.45 KB
Newer Older
liujiangnan's avatar
liujiangnan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import {
  Component,
  ElementRef,
  EventEmitter,
  Input,
  OnChanges,
  OnDestroy,
  OnInit,
  Output,
  ViewChild
} from '@angular/core';

@Component({
  selector: 'app-lesson-title-config',
  templateUrl: './lesson-title-config.component.html',
  styleUrls: ['./lesson-title-config.component.scss']
})
export class LessonTitleConfigComponent implements OnInit, OnChanges, OnDestroy, OnChanges {

  fontFamilyList = [
    'Arial',
李维's avatar
李维 committed
22
    'ARBLI'
liujiangnan's avatar
liujiangnan committed
23 24 25
  ];

  colorList = [
李维's avatar
李维 committed
26
    '#111111',
liujiangnan's avatar
liujiangnan committed
27 28 29 30
    '#ffffff',
    '#595959',
    '#0075c2',
    '#c61c1e',
李维's avatar
李维 committed
31
    '#9cbc3a'
liujiangnan's avatar
liujiangnan committed
32 33 34 35 36
  ];
  MIN_FONT_SIZE = 1;
  MAX_FONT_SIZE = 7;
  isShowFontColorPane = false;
  isShowBGColorPane = false;
李维's avatar
李维 committed
37
  fontSizeRange: number[];
liujiangnan's avatar
liujiangnan committed
38 39 40 41 42 43 44 45 46 47

  editorContent = '';


  __fontFamily = 'Arial';
  __fontColor = '';
  __fontSize = 3;

  loopCnt = 0;
  maxLoops = 20;
李维's avatar
李维 committed
48 49 50 51 52 53 54 55
  groupIconsCount = {
    a: Array.from(Array(11).keys()),
    b: Array.from(Array(8).keys()),
    c: Array.from(Array(8).keys()),
  };
  prevIcons = [];
  prevType = '';
  @ViewChild('titleEl') titleEl: ElementRef;
liujiangnan's avatar
liujiangnan committed
56 57 58 59
  titleEW = null;

  @Input()
  titleObj = {
李维's avatar
李维 committed
60
    type: 'a',
liujiangnan's avatar
liujiangnan committed
61
    content: '',
李维's avatar
李维 committed
62
    icons: [],
liujiangnan's avatar
liujiangnan committed
63 64
    audio_url: ''
  };
李维's avatar
李维 committed
65 66
  @Input()
  withIcon = true;
liujiangnan's avatar
liujiangnan committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

  @Output()
  titleUpdated = new EventEmitter();

  constructor() {
    this.fontSizeRange = [];
    for (let i = this.MIN_FONT_SIZE; i <= this.MAX_FONT_SIZE; ++i) {
      this.fontSizeRange.push(i);
    }
    this.__fontSize = 3;
    this.__fontColor = this.colorList[0];

  }
  ngOnChanges(vars) {
    if (!vars.titleObj.previousValue) {// 初始化,内容是空
      return;
    }
    let defObj = this.titleObj;
    if (!vars.titleObj.currentValue) {
      defObj = {
李维's avatar
李维 committed
87
        type: 'a',
liujiangnan's avatar
liujiangnan committed
88
        content: '',
李维's avatar
李维 committed
89
        icons: [],
liujiangnan's avatar
liujiangnan committed
90 91 92 93 94
        audio_url: ''
      };
    } else {
      defObj = vars.titleObj.currentValue;
    }
李维's avatar
李维 committed
95 96
    this.titleObj.icons = defObj.icons || [];
    this.titleObj.type = defObj.type || 'a';
liujiangnan's avatar
liujiangnan committed
97 98 99 100 101 102 103 104
    this.titleObj.content = defObj.content || '';
    this.titleObj.audio_url = defObj.audio_url || '';
    this.titleEW.document.body.innerHTML = this.titleObj.content;
  }

  ngOnInit() {
    if (!this.titleObj) {
      this.titleObj = {
李维's avatar
李维 committed
105
        type: 'a',
liujiangnan's avatar
liujiangnan committed
106
        content: '',
李维's avatar
李维 committed
107
        icons: [],
liujiangnan's avatar
liujiangnan committed
108 109 110
        audio_url: ''
      };
    }
李维's avatar
李维 committed
111 112
    this.titleObj.icons = this.titleObj.icons || [];
    this.titleObj.type = this.titleObj.type || 'a';
liujiangnan's avatar
liujiangnan committed
113 114 115
    this.titleObj.content = this.titleObj.content || '';
    this.titleObj.audio_url = this.titleObj.audio_url || '';

李维's avatar
李维 committed
116 117 118 119 120 121 122 123
    this.editorContent = `<html lang="en"><head><meta charset="utf-8">
  <meta name="viewport"
          content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"/>
  </head>
  <body style="height:48px;overflow: hidden;margin: 0;padding: 0 .5rem;background: #FFF;line-height: 48px;">
  ${this.titleObj.content}
  </body>
  </html>`;
liujiangnan's avatar
liujiangnan committed
124 125
    this.titleEW = this.titleEl.nativeElement.contentWindow;
    const tdoc = this.titleEW.document;
李维's avatar
李维 committed
126
    tdoc.designMode = "on";
liujiangnan's avatar
liujiangnan committed
127 128 129
    tdoc.open('text/html', 'replace');
    tdoc.write(this.editorContent);
    tdoc.close();
李维's avatar
李维 committed
130 131
    tdoc.addEventListener("keypress", this.keyPress, true);
    tdoc.addEventListener("blur", () => {
liujiangnan's avatar
liujiangnan committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
      if (this.titleObj.content === this.titleEW.document.body.innerHTML.trim()) {
        return;
      }
      this.shouldSave();
    }, true);
  }
  htmlEncode(text) {
    if (!text) {
      return '';
    }
    return text.replace(/\&/ig, '&amp;')
      .replace(/\</ig, '&lt;')
      .replace(/\>/ig, '&gt;')
      .replace(/\"/ig, '&quot;');
  }

  htmlDecode(text) {
    if (!text) {
      return '';
    }
    return text.replace(/\&amp\;/ig, '&')
      .replace(/\&lt\;/ig, '<')
      .replace(/\&gt\;/ig, '>')
      .replace(/\&quot\;/ig, '"');
  }


  ngOnDestroy(): void {

  }
李维's avatar
李维 committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  iconsChanges(val) {
    let a = this.titleObj.icons;
    let b = val;

    if (a.length > b.length) {
      const diff = a.filter(x => !b.includes(x));
      const ti = [...this.titleObj.icons];
      for (let i = 0; i < diff.length; i++) {
        const d = diff[i];
        const idx = ti.indexOf(d);
        ti.splice(idx, 1);
      }
      this.titleObj.icons = ti;
    } else {
      const diff = b.filter(x => !a.includes(x));
      this.titleObj.icons = [...this.titleObj.icons, ...diff];
    }
liujiangnan's avatar
liujiangnan committed
179

李维's avatar
李维 committed
180 181 182 183 184 185
    this.shouldSave();
  }
  typeChange(val) {
    this.titleObj.icons = [];
    this.shouldSave();
  }
liujiangnan's avatar
liujiangnan committed
186 187 188 189 190 191 192 193 194 195 196 197
  keyPress(evt) {
    try {

      if (evt.charCode === 13) {
        evt.preventDefault();
        evt.stopPropagation();
        return;
      }
      if (evt.ctrlKey) {
        const key = String.fromCharCode(evt.charCode).toLowerCase();
        let cmd = '';
        switch (key) {
李维's avatar
李维 committed
198 199 200
          case 'b': cmd = "bold"; break;
          case 'i': cmd = "italic"; break;
          case 'u': cmd = "underline"; break;
liujiangnan's avatar
liujiangnan committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        }


        if (cmd) {
          this.execEditorCommand(cmd);

          // stop the event bubble
          evt.preventDefault();
          evt.stopPropagation();
        }
      }
    } catch (e) {
      console.log(1, e);
      alert(e);
    }
  }
  execEditorCommand(command, option?: any) {
    try {
      this.titleEW.focus();
李维's avatar
李维 committed
220
      this.titleEW.document.execCommand(command, false, option);
liujiangnan's avatar
liujiangnan committed
221 222 223 224 225 226 227 228 229 230 231
      this.loopCnt = 0;

      return false;
    } catch (e) {
      alert(e);
      if (this.loopCnt < this.maxLoops) {
        setTimeout(() => {
          this.execEditorCommand(command, option);
        }, 100);
        this.loopCnt += 1;
      } else {
李维's avatar
李维 committed
232
        alert("Error executing command.");
liujiangnan's avatar
liujiangnan committed
233 234 235 236 237 238 239 240 241 242 243 244
      }
    }
  }

  onSelectColor(color) {
    this.execEditorCommand('forecolor', color);
    this.__fontColor = color;
  }
  onChangeFontColor(val) {
    this.execEditorCommand('forecolor', this.__fontColor);
  }
  onChangeFontFamily(font) {
李维's avatar
李维 committed
245
    this.execEditorCommand('fontname', font);
liujiangnan's avatar
liujiangnan committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
  }
  onChangeFontSize(size?: any) {

    if (size) {
      size += this.__fontSize;
    } else {
      size = this.__fontSize;
    }
    size = Math.max(this.MIN_FONT_SIZE, size);
    size = Math.min(this.MAX_FONT_SIZE, size);
    this.execEditorCommand('fontsize', size);
  }
  onChangeBold() {
    this.execEditorCommand('bold');
  }
  onChangeItalic() {
    this.execEditorCommand('italic');
  }
  onChangeUnderline() {
    this.execEditorCommand('underline');
  }
  onChangeStrikethrough() {
    this.execEditorCommand('strikethrough');
  }
  titleAudioUploaded(res) {
    this.titleObj.audio_url = res.url;
    this.titleUpdated.emit(this.titleObj);
  }
  shouldSave = () => {
李维's avatar
李维 committed
275
    console.log('title shouldSave');
liujiangnan's avatar
liujiangnan committed
276 277 278 279 280
    this.titleObj.content = this.titleEW.document.body.innerHTML.trim();
    this.titleUpdated.emit(this.titleObj);
  }
}