【问题标题】:Froala Angular Text Editor (v3) - Destroy and re-Initialize on input changesFroala Angular 文本编辑器 (v3) - 销毁并重新初始化输入更改
【发布时间】:2019-12-18 17:50:28
【问题描述】:

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

这是我正在尝试的,这同样适用于初始渲染,但在稍后更改输入时不起作用(我在初始渲染后 5 秒将 characterCountMax 更改为新值) .他们的原始 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;
      }
    }
  }
}

【问题讨论】:

    标签: angular wysiwyg froala


    【解决方案1】:

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

    标记

    <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();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 2014-06-28
      • 2022-12-07
      相关资源
      最近更新 更多