Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting a default font size #4486

Open
kgrove-ch opened this issue Nov 8, 2024 · 0 comments
Open

Setting a default font size #4486

kgrove-ch opened this issue Nov 8, 2024 · 0 comments

Comments

@kgrove-ch
Copy link

kgrove-ch commented Nov 8, 2024

I'm trying to programmatically set the default font size for different Quill rte's on a page in my application, but each approach seems more and more of a hacky workaround than something that would seem pretty straightforward.

I'm using Quill (ngx-quill ^25.3.2/quill ^2.0.0) in my Angular v17 application and I've tried using Angular's various if statement syntax for the selected attribute on the <option> tag that I need to set as the default font size, but I've run into many dead ends with that approach.

The one thing I've gotten to work, though seemingly through hoops and ladders, is getting the correct font size to be selected in the dropdown. Here's the approach I've gotten to work:

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 = // array of px font sizes;
    Quill.register(fontSizeStyle, true);
    this.Quill = new Quill(editorElement, {
            theme: "snow",
            modules: {
                table: true,
                toolbar: toolbarElement,
            },
            placeholder: "Enter Text",
    });
    // other quill setup
    const defaultFontSize = this.isHeaderOrFooter ? "10px" : "14px";
    // this seems pretty hacky, but it works
    const quillLength = this.Quill.getLength();
    this.Quill.formatText(0, quillLength, 'size', defaultFontSize);
    const sizeDropdown = toolbarElement.querySelector("select.ql-size");
    if (sizeDropdown) {
        sizeDropdown.value = defaultFontSize; // Set default font size in dropdown
        sizeDropdown.dispatchEvent(new Event("change"));
     }
  }
}

The only thing I can't seem to get working is having the editor font size reflect the default selected font size. Any guidance would be much appreciated

@kgrove-ch kgrove-ch changed the title Programmatically setting a default font size Setting a default font size Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant