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

fix(primeng/p-editor): change logic setValue when quill is not attached for DOM #12244

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions src/app/components/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export class Editor implements AfterViewInit, AfterViewChecked, AfterContentInit

value: string;

delayedCommand: Function | null = null;

_readonly: boolean;

onModelChange: Function = () => {};
Expand All @@ -127,22 +129,36 @@ export class Editor implements AfterViewInit, AfterViewChecked, AfterContentInit

headerTemplate: TemplateRef<any>;

private get isAttachedQuillEditorToDOM(): boolean {
return this.quillElements?.editorElement?.isConnected;
}

private quillElements: { editorElement: HTMLElement; toolbarElement: HTMLElement } | null = null;

constructor(public el: ElementRef) {}

ngAfterViewInit(): void {
this.initQuillElements();

if (this.quillElements?.editorElement?.isConnected) {
if (this.isAttachedQuillEditorToDOM) {
this.initQuillEditor();
}
}

ngAfterViewChecked(): void {
if (!this.quill && this.quillElements?.editorElement?.isConnected) {
// The problem is inside the `quill` library, we need to wait for a new release.
// Function `isLine` - used `getComputedStyle`, it was rewritten in the next release.
// (We need to wait for a release higher than 1.3.7).
// These checks and code can be removed.
if (!this.quill && this.isAttachedQuillEditorToDOM) {
this.initQuillEditor();
}

// Can also be deleted after updating `quill`.
if (this.delayedCommand && this.isAttachedQuillEditorToDOM) {
this.delayedCommand();
this.delayedCommand = null;
}
}

ngAfterContentInit() {
Expand All @@ -159,8 +175,27 @@ export class Editor implements AfterViewInit, AfterViewChecked, AfterContentInit
this.value = value;

if (this.quill) {
if (value) this.quill.setContents(this.quill.clipboard.convert(value));
else this.quill.setText('');
if (value) {
const command = (): void => {
this.quill.setContents(this.quill.clipboard.convert(this.value));
};

if (this.isAttachedQuillEditorToDOM) {
command();
} else {
this.delayedCommand = command;
}
} else {
const command = (): void => {
this.quill.setText('');
};

if (this.isAttachedQuillEditorToDOM) {
command();
} else {
this.delayedCommand = command;
}
}
}
}

Expand Down