Angular项目中Quill编辑器默认选中字号无法同步到编辑区域的问题求助
Angular项目中Quill编辑器默认选中字号无法同步到编辑区域的问题求助
我在Angular 17项目里用ngx-quill(版本25.3.2,依赖的quill核心包是2.0.0)开发富文本编辑器,现在碰到个棘手的问题:我想给页面上不同的Quill实例设置对应的默认字号,但尝试的各种方法都感觉像是临时的hack,完全不是预想中那种直观的实现方式。
最开始我试过用Angular的各种条件语法给<option>标签加selected属性,想让对应的字号默认选中,但试了好几种写法都走不通。
后来总算折腾出了能让字号下拉框默认选中正确值的方法,但编辑区域的字体大小就是不跟着同步,这部分始终搞不定。先把目前能运行的代码贴出来:
export class QuillEditor implements OnInit { @ViewChild("contextModel", { static: true }) contextModel: ElementRef; @ViewChild("toolbar", { static: true }) toolbarElement: ElementRef; @Input() isHeaderOrFooter = false; public ngOnInit(): void { this.initializeQuill() } private initializeQuill(): void { const editorElement = this.contextModel.nativeElement; const toolbarElement = this.toolbarElement.nativeElement; let fontSizeStyle = Quill.import("attributors/style/size"); fontSizeStyle.whitelist = ["10px", "12px", "14px", "16px"]; // 自定义的字号白名单数组 Quill.register(fontSizeStyle, true); this.Quill = new Quill(editorElement, { theme: "snow", modules: { table: true, toolbar: toolbarElement, }, placeholder: "Enter Text", }); // 其他Quill相关的初始化配置... const defaultFontSize = this.isHeaderOrFooter ? "10px" : "14px"; // 这段代码感觉很hack,但确实让下拉框选中了正确的字号 const quillLength = this.Quill.getLength(); this.Quill.formatText(0, quillLength, 'size', defaultFontSize); const sizeDropdown = toolbarElement.querySelector("select.ql-size"); if (sizeDropdown) { sizeDropdown.value = defaultFontSize; // 给下拉框设置默认值 sizeDropdown.dispatchEvent(new Event("change")); } } }
现在的情况是:字号下拉框能正确默认选中我要的大小,但编辑区域里的文字(包括占位符和输入后的内容)还是用的默认字号,完全没同步过去。有没有大佬能给点指导,怎么让编辑区域也应用这个默认字号?
备注:内容来源于stack exchange,提问作者Kevin G
相关产品推荐
相关产品推荐

