【发布时间】:2020-05-04 08:33:01
【问题描述】:
我创建了一个自定义工具栏服务来手动将样式应用于目标 iframe。
HTML
<button (click)="exec('bold')">B</button>
<div contenteditable (input)="onInput($event.target.innerHTML)" class="editor" #editor></div>
打字稿
import {
Component,
Input,
OnInit,
Output,
EventEmitter,
OnChanges,
ViewChild,
ElementRef
} from '@angular/core';
@Component({
selector: 'editor',
templateUrl: './editor.html',
styleUrls: ['./editor.scss']
})
export class Editor implements OnInit, OnChanges {
@Input() value: any;
@ViewChild('editor') editor: ElementRef;
@Output() valueChange = new EventEmitter();
ngOnInit() {
document.execCommand("styleWithCSS", false, null);
this.editor.nativeElement.innerHTML = this.value;
}
ngOnChanges(changes: any) {
try {
if (this.editor.nativeElement.innerHTML != this.value) {
this.editor.nativeElement.innerHTML = this.value;
}
} catch (err) {
}
}
exec(a, b = null) {
document.execCommand(a, false, b);
};
onInput(newValue) {
this.valueChange.emit(newValue);
}
}
它按预期工作,但我想利用外部 API (CKEditor) 进行任何调用以进行更新。因此,我纯粹使用 CKEditor 来定位我的组件,而不是 execCommand。据我了解,这些文本编辑器中的大多数都要求您使用它们的内部工具栏,但我不想使用它。
我已经模拟了我在这里尝试做的一个示例:https://stackblitz.com/edit/angular-rich-editor-test-b3yvjv。
【问题讨论】:
标签: javascript angular ckeditor