如何使用angular cdk执行图像拖放?

3b6akqbq  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(222)

现在,我正在我的web应用程序项目中执行从图像目录到画布的图像拖放。到目前为止,该代码作为一个在pc上运行的web应用程序运行。但是,我发现,当我通过移动设备(包括i-phone 6s和三星android)运行该程序时,我的拖放代码不起作用。
在pc上工作的源代码符合以下代码:
拖动部分:image-directory.component.html和image-directory.component.ts image-directory.component.html部分:

<ng-container *ngFor="let imageDirectory of imageDirectories">
  <ng-container *ngIf="imageDirectory.type === selectType">
    <div class="image-flexbox">
      <img
        *ngFor="let image of imageDirectory.images; let i = index"
        [id]="imageDirectory.type + i"
        [src]="image.src"
        draggable="true"
        (dragstart)="onDragStart($event, image)"
        (dragend)="onDragEnd($event, image)"
      />
    </div>
  </ng-container>
</ng-container>

我已将cdkdrag设置为或<img…/>,但无法拖动选定的单个图像。但它将整个内容移动到div中。试图将cdkdrag设置为正常工作需要我使用单个图像的固定列表设置导出类,以便在image-directory.component.ts节中进行拖动,这是不容易做到的。
image-directory.component.ts节:

import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { take } from 'rxjs/operators';
import { TemplatesService } from 'src/app/core/services/templates.service';

import { ImageDirectory, ImageDirectoryType, ImageItem } from '../../../core/interfaces/sketch.interface';
import { CATEGORY } from './../../../core/constants/category';

@Component({
  selector: 'app-image-directory',
  templateUrl: './image-directory.component.html',
  styleUrls: ['./image-directory.component.scss']
})
export class ImageDirectoryComponent implements OnInit {

  // WARN: loading multiple images by reading the directory could cause security issues! (client shouldn't be able to read the directory)

  @Input() selectType: ImageDirectoryType = 'default';

  @Output() imagesDragStart: EventEmitter<{e: DragEvent, imageItem: ImageItem}> = new EventEmitter();
  @Output() imagesDragEnd: EventEmitter<{e: DragEvent, imageItem: ImageItem}> = new EventEmitter();
  imageDirectories: ImageDirectory[];
  // imageDirectories: ImageDirectory

  constructor(
    private templatesService: TemplatesService,
  ) { }

  ngOnInit(): void {
    this.templatesService.getList().pipe(take(1)).subscribe(templates => {
      console.log(templates);

      const imageDir: ImageDirectory[] = CATEGORY.map(category => {
        return {
          type: category,
          images: templates.filter(t => t.category === category).map((t, i) => {
            return {
              title: `${category}_${i}`,
              src: this.templatesService.get(t.fileId),
              remarks: t.remarks,
              subCategory: t.sub_category,
              category: t.category
            };
          })
        } as ImageDirectory;
      });
      this.imageDirectories = imageDir;
    });
  }

  onDragStart(e: DragEvent, imageItem: ImageItem): void {
    // e.preventDefault();
    return this.imagesDragStart.emit({e, imageItem});
  }

  onDragEnd(e: DragEvent, imageItem: ImageItem): void {
    // e.preventDefault();
    return this.imagesDragEnd.emit({e, imageItem});
  }

}

放置部分:sketch.component.html和sketch.component.ts
sketch.component.html部分:

.....
<app-image-directory
  class="gallery"
  id="ImageDirectory"
  style="overflow-y: auto; height:275px"
  (imagesDragEnd)="onImageDragEnd($event)"
  (imagesDragStart)="onImageDragStart($event)"
  [selectType]="selectType"
>
</app-image-directory>

<div>
  <div>
    <div #canvasContainer (click)="onCanvasClick($event)" (dragover)="onCanvasDragOver($event)" (drop)="onCanvasDrop($event)"  id="MyCanvas" width="300" height="400" style="border:1px solid red" >
      <canvas id="FaceCompositionCanvas" width="300" height="400" style="border:1px solid"></canvas>
    </div>
  </div>
</div>

草图.component.ts部分:

import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { UntilDestroy } from '@ngneat/until-destroy';
import Konva from 'konva';
import { BehaviorSubject, generate } from 'rxjs';
import { ToolNavItem } from 'src/app/core/interfaces/sketch.interface';
import { HumanFace } from 'src/app/core/models/human-face';
import { FaceTools, ToolMode } from 'src/app/core/models/tools';
import { v4 as uuidv4 } from 'uuid';

import {
  BrushSizeItem,
  ColorItem,
  ImageDirectoryType,
  ImageItem,
  MenuItem,
  SideNavItem,
  ToolItem,
} from '../../core/interfaces/sketch.interface';

@UntilDestroy()
@Component({
  selector: 'app-sketch',
  templateUrl: './sketch.component.html',
  styleUrls: ['./sketch.component.scss']
})
export class SketchComponent implements OnInit, OnDestroy {

  // current select item
  selectedItem: BehaviorSubject<ImageItem[]> = new BehaviorSubject([]);

  // last dragged item
  lastImageDrag: {e: DragEvent, imageItem: ImageItem} = null;

  // current image list in canvas (will show on right side as images pannel)
  imagesItems: BehaviorSubject<ImageItem[]> = new BehaviorSubject([]);

  // current type selected (will show on ImageDirectory element)
  selectType: ImageDirectoryType = 'default';

....
  onCanvasDrop(e) {
    e.preventDefault();
    // need proper way to deal with issue
    // Set left and top of Last Image Drag

    this.addImage(this.lastImageDrag); // this is function to add image into the layer of canvas
  }

  onCanvasDragOver(e) {
    e.preventDefault(); // needed to make drop event work properly
  }

 .....

问题是图像目录部分不能有一个固定的数字来根据Angular cdk文档创建图像对象数组。
如何处理这些问题,以便在web应用程序中使用Angular cdk的拖放函数和变量。任何可行的替代方案都将不胜感激。
顺便说一句,我使用Angular 版本10虽然。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题