The primeng editor uses on the inside the Qill rich text editor. To do what you want you actually need to extend the Quill editor. They offer extensibility and you can override the actions that happen when you press the toolbar buttons. In this case you need to add your own custom toolbar handler for the image
action. To get the Quill editor from the primeng editor there is a provided method getQuill check the primeng Editor documentation for more details. You can get the editor in the ngAfterViewinit
lifecycle event hook and then attach the custom handler to add your image processing. To implement the handler you need to duplicate all the code that creates the input and downloads the file. Unfortunately it is not so simple but you can then do anything you want inside it. I have copied it from the original code in Quill editor and just added the size handling.
Here is my sample implementation.
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
public title = 'CodeSandbox';
public text: string;
@ViewChild(Editor) editor: Editor;
public ngAfterViewInit() {
const quill = this.editor.getQuill();
const toolbar = quill.getModule('toolbar');
toolbar.addHandler('image', function () {
const fileInput = toolbar.container.querySelector(
'input.ql-image[type=file]'
);
if (fileInput == null) {
fileInput = document.createElement('input');
fileInput.setAttribute('type', 'file');
fileInput.setAttribute(
'accept',
'image/png, image/gif, image/jpeg, image/bmp, image/x-icon'
);
fileInput.classList.add('ql-image');
fileInput.addEventListener('change', function () {
if (fileInput.files != null && fileInput.files[0] != null) {
if (fileInput.files[0].size > 102400) {
alert('image to big it neeeds to be les than 100kb');
} else {
const reader = new FileReader();
reader.onload = function (e) {
const range = quill.getSelection(true);
quill.updateContents(
new Delta().retain(range.index).delete(range.length).insert({
image: e.target.result
}),
'user'
);
quill.setSelection(range.index + 1, 'silent');
fileInput.value = '';
};
reader.readAsDataURL(fileInput.files[0]);
}
}
});
toolbar.container.appendChild(fileInput);
}
fileInput.click();
});
}
}
And also the html template.
<p-editor [(ngModel)]="text" [style]="{'height':'320px'}"></p-editor>
To see it working you can check the CodeSandbox with the fully implemented sample.