简体   繁体   English

PrimeNG 编辑器组件自动聚焦页面加载

[英]PrimeNG editor component auto focus on page loading

I am using PrimeNG UI Library's Editor component .我正在使用PrimeNG UI 库的编辑器组件 Currently, version is 6.1.2.目前,版本为 6.1.2。 The problem appears when the page is loaded, and the Editor has some content, the page automatically scrolls to the editor and focus on it.页面加载时出现问题,编辑器有一些内容,页面自动滚动到编辑器并聚焦。 How to disable this autofocus?如何禁用此自动对焦? I've tried to use simple window.scroll(0,0) , but it looks strange when on page loading it scrolls down and then up.我尝试使用简单的window.scroll(0,0) ,但是在页面加载时它向下滚动然后向上滚动时看起来很奇怪。

Probably the issue with quill text editor, which is used in PrimeNG.可能是 PrimeNG 中使用的 quill 文本编辑器的问题。 Anyway, any workaround here?无论如何,这里有什么解决方法吗? Thank you.谢谢你。

We ended up with the following solution: Had to add Directive which use method NgOnChanges (because issue happens not only on page load, also when content is changed programmatically).我们结束了以下解决方案:必须添加指令其使用方法NgOnChanges (因为问题不仅发生在页面加载,也当内容被编程改变)。 So, on change action, display style changes to 'none', then after timeout show element.因此,在更改操作时,显示样式更改为“无”,然后在超时后显示元素。

Directive:指示:

import { Directive, Input, OnChanges, SimpleChange, ElementRef } from "@angular/core";

@Directive({
    selector: '[p-editor-model]'
})
export class PeAutoscrollFixDirective implements OnChanges {
    @Input("p-editor-model") content: string;

    ngOnChanges(changes: { [property: string]: SimpleChange }) {
        let change = changes["content"];
        let elemPosition = this.element.nativeElement.getBoundingClientRect().top + document.body.scrollTop;
        let clientHeight = document.documentElement.clientHeight;

        if (change.isFirstChange() || elemPosition > clientHeight)
        {
            this.element.nativeElement.style.display = 'none';
            setTimeout(() => {
                this.element.nativeElement.style.display = '';
            });
        }
    }

    constructor(private element: ElementRef) {
    }
}

Need to add this directive to Module as declarations and exports.需要将此指令添加到 Module 作为声明和导出。

And using this directive looks like:使用这个指令看起来像:

<p-editor [p-editor-model]="model" [(ngModel)]="model"></p-editor>

To anyone who finds it useful: it can also be done by using a public variable to control the readonly attribute.对于发现它有用的任何人:它也可以通过使用公共变量来控制readonly属性来完成。 Initialize the variable as true, and then change it to false in the onAfterViewInit .将变量初始化为 true,然后在onAfterViewInit其更改为 false。

component.ts:组件.ts:

import { OnInit, AfterViewInit } from '@angular/core';

export class myComponent implements OnInit, AfterViewInit {
    public tempReadOnly: boolean = true;

    ngAfterViewInit() {
        this.tempReadOnly = false;
    }
}

component.html:组件.html:

<p-editor [readonly]="tempReadOnly" [(ngModel)]="model"></p-editor>

You can even use an additional readonly variable if necessary and use it like this:如有必要,您甚至可以使用额外的readonly变量,并像这样使用它:

<p-editor [readonly]="isReadOnly || tempReadOnly" [(ngModel)]="model"></p-editor>

You can set editor style display:none untill your page load.您可以设置编辑器样式 display:none 直到您的页面加载。 Once it is loaded enable it set display:inline;加载后启用它设置 display:inline; using setTimeout() if you are using javascript.如果您使用的是 javascript,请使用 setTimeout()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM