【问题标题】:Is it possible to somehow use pipes in kendo-editor in Angular?是否可以在 Angular 的 kendo-editor 中以某种方式使用管道?
【发布时间】:2020-08-28 18:12:02
【问题描述】:

是否可以在 Angular 的 kendo-editor 中以某种方式使用管道?

我的用例如下。我已经实现了将图像从本地机器上传到我自己的端点(如here 所述)。另外,我实现了一个返回图像的 get 端点。因此,我可以使用链接通过src 图像属性检索图像。但我需要对用户进行身份验证才能调用 get 端点。

我对这个问题的研究:How to intercept the src http request and set headers for it? 引导我找到了一个带有安全管道的解决方案,它可以为我运行请求。例如。 this 文章介绍了解决方案。

我能够实施该解决方案。所以,现在在我的 Angular 模板中我有:

<img [attr.src]="'http://localhost:44123/image/view/image280820208888.png' | authImage | async" />

图像是通过端点加载的,因为我能够使用身份验证(因为我显式运行 http 请求而不是将其委托给浏览器)。

所以,如果我能够以某种方式添加代码,那将是非常非常棒的

<img [attr.src]="'http://localhost:44123/image/view/image280820208888.png' | authImage | async" />

进入剑道编辑器值并实际看到图像。但我不知道如何在剑道编辑器的值中使用管道。

对于身份验证,我使用带有不记名令牌的标头。

那么,有人可以建议我如何在剑道编辑器中使用管道吗?

预加载图像并将它们存储在剑道编辑器值的 src 中的选项,因为 base64 不适合我,因为在我的剑道编辑器值中可能有很多图像,我存储值作为数据库中的字符串。因此,如果我使用 base64,我可能会很快耗尽空间(因为,我会将所有图像存储在文本中)。

更新

Here 是我尝试使用指令。可以看到指令的类被添加到图像中,但警报消息没有触发。

指令:

import { Directive, ElementRef } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Directive({
  // tslint:disable-next-line: directive-selector
  selector: '.customDownload',
})
export class ImgHandlingDirective {
  constructor(
    private el: ElementRef<HTMLImageElement>,
    private http: HttpClient,
  ) {
    
    alert("code reached");
  }
}

验证该类已添加到图像中:

Here是kendo-editor组件api。

指令本身运行良好。如果我们将&lt;img class="customDownload" /&gt; 添加到app.component 的模板中,那么指令中的代码将会到达并且会触发警报。

【问题讨论】:

    标签: angular kendo-ui src angular-pipe prose-mirror


    【解决方案1】:

    你可以使用指令,但是图片会上传两次,一次是本地上传,一次是通过httpClient:

    import { Directive, ElementRef } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { Observable } from 'rxjs';
    import { map } from 'rxjs/operators';
    
    @Directive({
      // tslint:disable-next-line: directive-selector
      selector: 'img[src]',
    })
    export class ImgHandlingDirective {
      constructor(
        private el: ElementRef<HTMLImageElement>,
        private http: HttpClient,
      ) {
        this.el.nativeElement.style.display = 'none';
    
        const url = this.el.nativeElement.src;
    
        this.loadImage(url).subscribe(src => {
          this.el.nativeElement.src = src;
          this.el.nativeElement.style.display = 'block';
        });
      }
      private loadImage(url: string): Observable<string> {
        return this.http
          // load the image as a blob
          .get(url, { responseType: 'blob' }).pipe(
            // create an object url of that blob that we can use in the src attribute
            map(e => URL.createObjectURL(e)),
          );
      }
    }
    

    更新后: 您可以更改uploadImage() 函数以通过httpClient 上传它

    public uploadImage(): void {
      const { src } = this.imageInfo;
    
      this.http
      // load the image as a blob
      .get(src, { responseType: 'blob' }).pipe(
        // create an object url of that blob that we can use in the src attribute
        map(e => URL.createObjectURL(e)),
      ).subscribe(() => {
        // Invoking the insertImage command of the Editor.
        this.editor.exec('insertImage', this.imageInfo);
    
        // Closing the Dialog.
        this.close();
      });
    }
    

    【讨论】:

    • 也许可以以某种方式向图像添加自定义指令?这样就不会以自定义方式上传不在剑道编辑器中的图像。
    • 我刚试过。该指令不会进入剑道编辑器。 :(
    • @SasukeUchiha 在 stackblitz 上创建一个示例。
    • 非常感谢您的宝贵时间。我刚刚更新了你的问题。
    • 您可以通过上传图片并在编辑器内容中查看来重现该问题。上传图片后,我需要调用该指令,但不会调用它。
    猜你喜欢
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2021-05-13
    • 2023-04-01
    • 2015-12-07
    • 1970-01-01
    相关资源
    最近更新 更多