angular - Froala Angular 文本编辑器 (v3) - 在输入更改时销毁并重新初始化

标签 angular wysiwyg froala

我正在创建一个可重复使用的 Angular 组件,它负责在 Angular 应用程序中呈现 Froala 文本编辑器。我在组件首次渲染初始化方面一切正常,但我试图解释一些组件输入在组件初始化后发生变化,然后销毁然后根据这些输入重新初始化文本编辑器(这会导致更改文本编辑器选项)。

这是我正在尝试的,它再次适用于初始渲染,但在稍后更改输入时不起作用(我正在将 characterCountMax 更改为新值 5 秒在初始渲染之后)。他们原始 javascript 库的这个 Angular 集成 ( https://github.com/froala/angular-froala-wysiwyg#manual-initialization ) 的文档非常简单,没有涵盖这一点,所以我希望其他人已经做到了这一点。

组件模板:

<div [hidden]="!isInitialized" [froalaEditor]="editorOptions" (froalaInit)="initialize($event)" (froalaModelChange)="onContentChanged($event)"></div>

组件 TS:

import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core';
import { TextEditorOptions } from './text-editor-options';
import { TextEditorToolbarButtons, defaultTextEditorToolbarButtons } from './text-editor-toolbar-buttons.enum';

@Component({
  selector: 'app-text-editor',
  templateUrl: './text-editor.component.html',
  styleUrls: ['./text-editor.component.css']
})
export class TextEditorComponent implements OnInit, OnChanges {
  @Input() placeholderText = 'Insert text here...';
  @Input() showCharacterCount = false;
  @Input() characterCountMax = 1000;
  @Input() heightMin = 300;
  @Input() toolbarButtons: Array<TextEditorToolbarButtons> = defaultTextEditorToolbarButtons;
  public editorOptions: TextEditorOptions;
  public isInitialized = true;
  private froalaControl: any;

  constructor() { }

  ngOnInit() {
    this.updateEditorOptions();
  }

  ngOnChanges(changes: SimpleChanges) {
    this.updateEditorOptions();
  }

  public initialize(initControls: any) {
    console.log('initializing');
    this.froalaControl = initControls;
    if (this.froalaControl) {
      this.froalaControl.initialize();
    }
  }

  public onContentChanged(text: string) {
    console.log(text);
  }

  private updateEditorOptions() {
    if (!this.toolbarButtons) {
      this.toolbarButtons = defaultTextEditorToolbarButtons;
    }

    this.editorOptions = {
      placeholderText: this.placeholderText,
      attribution: false,
      charCounterCount: this.showCharacterCount,
      charCounterMax: this.characterCountMax,
      heightMin: this.heightMin,
      toolbarButtons: this.toolbarButtons,
      toolbarButtonsXS: this.toolbarButtons,
      toolbarButtonsSM: this.toolbarButtons,
      toolbarButtonsMD: this.toolbarButtons
    };

    if (this.froalaControl) {
      const editor = this.froalaControl.getEditor();
      if (editor) {
        this.isInitialized = false;
        if (editor.opts) {
          editor.opts = Object.assign(editor.opts, this.editorOptions);
        }
        this.froalaControl.destroy();
        this.initialize(this.froalaControl);
        this.isInitialized = true;
      }
    }
  }
}

最佳答案

这是一个示例,我从编辑器外部更改语言。我在编辑器的 destoy 和初始化之间使用了一个超时。

标记

<div class="na-froala-editor" *ngIf="options && isInitialized"
  [froalaEditor]="options"
  (froalaInit)="initialize($event)"
  [froalaModel]="editorContent$ | async"
  (froalaModelChange)="froalaModelChange($event)">
</div>

代码

@Input()
public set language(language: string) {
  this.languageVal = language;

  // When the language change after the initialisation ( option is already set)
  // We need to destroy the froalaEditor and initialize it again
  if (this.options && this.froalaControl) {
    this.options.language = language;
    const editor = this.froalaControl.getEditor();
    if (editor) {
      this.isInitialized = false;
      this.froalaControl.destroy();
      // Delay is necessary to let the froala editor finish to destroy
      setTimeout(() => {
        if (editor.opts) {
          editor.opts = this.options;
        }
        this.initialize(this.froalaControl);
        this.isInitialized = true;
      }, 100);
    }
  }
}

public initialize(initControls) {
  this.froalaControl = initControls;
  if (this.froalaControl) {
    this.froalaControl.initialize();
  }
}

关于angular - Froala Angular 文本编辑器 (v3) - 在输入更改时销毁并重新初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59397600/

相关文章:

javascript - Froala 编辑器 AWS S3 CORS 问题

javascript - Angular2 和 jQuery 'change' 事件

angular - 如何在 Angular 7 中增加超过 2 分钟的 HTTP 请求超时?

javascript - 最佳基于 Web 的 SVG 所见即所得

javascript - CLEditor - 如果达到最大长度则失去焦点

css - 将 froala 所见即所得的文本区域更改为笔记本

angular - rxjs 和 WebStorm

angular - ERROR in Error during template compile of 'AppModule'

javascript - 以 Angular 清除 trumbowyg 编辑器

javascript - Froala 编辑器自定义输入字段