|
| 1 | +/** |
| 2 | + * Use of this source code is governed by an MIT-style license that can be |
| 3 | + * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE |
| 4 | + */ |
| 5 | + |
| 6 | +import { DOCUMENT } from '@angular/common'; |
| 7 | +import { |
| 8 | + AfterViewInit, |
| 9 | + ChangeDetectorRef, |
| 10 | + Component, |
| 11 | + ElementRef, |
| 12 | + EventEmitter, |
| 13 | + Inject, |
| 14 | + Input, |
| 15 | + OnChanges, |
| 16 | + OnInit, |
| 17 | + Output, |
| 18 | + SimpleChanges, |
| 19 | + ViewChild |
| 20 | +} from '@angular/core'; |
| 21 | + |
| 22 | +import { Color } from '../interfaces/color'; |
| 23 | +import { HsbaColorType, TransformOffset } from '../interfaces/type'; |
| 24 | +import { calculateColor, calculateOffset } from '../util/util'; |
| 25 | +import { HandlerComponent } from './handler.component'; |
| 26 | +import { PaletteComponent } from './palette.component'; |
| 27 | + |
| 28 | +type EventType = MouseEvent | TouchEvent; |
| 29 | + |
| 30 | +type EventHandle = (e: EventType) => void; |
| 31 | + |
| 32 | +function getPosition(e: EventType): { pageX: number; pageY: number } { |
| 33 | + const obj = 'touches' in e ? e.touches[0] : e; |
| 34 | + const scrollXOffset = document.documentElement.scrollLeft || document.body.scrollLeft || window.pageXOffset; |
| 35 | + const scrollYOffset = document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset; |
| 36 | + return { pageX: obj.pageX - scrollXOffset, pageY: obj.pageY - scrollYOffset }; |
| 37 | +} |
| 38 | + |
| 39 | +@Component({ |
| 40 | + // eslint-disable-next-line @angular-eslint/component-selector |
| 41 | + selector: 'color-picker', |
| 42 | + standalone: true, |
| 43 | + imports: [HandlerComponent, PaletteComponent], |
| 44 | + template: ` |
| 45 | + <div |
| 46 | + #slider |
| 47 | + class="ant-color-picker-select" |
| 48 | + (mousedown)="dragStartHandle($event)" |
| 49 | + (touchstart)="dragStartHandle($event)" |
| 50 | + > |
| 51 | + <color-palette> |
| 52 | + <div |
| 53 | + #transform |
| 54 | + style="position: absolute; z-index: 1;" |
| 55 | + [style.left]="offsetValue.x + 'px'" |
| 56 | + [style.top]="offsetValue.y + 'px'" |
| 57 | + > |
| 58 | + <color-handler [color]="toRgbString()" /> |
| 59 | + </div> |
| 60 | + <div |
| 61 | + class="ant-color-picker-saturation" |
| 62 | + style=" |
| 63 | + background-image: linear-gradient(0deg, #000, transparent), |
| 64 | + linear-gradient(90deg, #fff, hsla(0, 0%, 100%, 0)); |
| 65 | + " |
| 66 | + [style.background-color]="toHsb()" |
| 67 | + ></div> |
| 68 | + </color-palette> |
| 69 | + </div> |
| 70 | + ` |
| 71 | +}) |
| 72 | +export class PickerComponent implements OnInit, AfterViewInit, OnChanges { |
| 73 | + @ViewChild('slider', { static: false }) containerRef!: ElementRef<HTMLDivElement>; |
| 74 | + @ViewChild('transform', { static: false }) transformRef!: ElementRef<HTMLDivElement>; |
| 75 | + |
| 76 | + @Input() color: Color | null = null; |
| 77 | + @Output() readonly nzOnChange = new EventEmitter<Color>(); |
| 78 | + @Output() readonly nzOnChangeComplete = new EventEmitter<HsbaColorType>(); |
| 79 | + @Input() disabled: boolean = false; |
| 80 | + |
| 81 | + offsetValue: TransformOffset = { x: 0, y: 0 }; |
| 82 | + dragRef: boolean = false; |
| 83 | + mouseMoveRef: (e: MouseEvent | TouchEvent) => void = () => null; |
| 84 | + mouseUpRef: (e: MouseEvent | TouchEvent) => void = () => null; |
| 85 | + |
| 86 | + toRgbString(): string { |
| 87 | + return this.color?.toRgbString() as string; |
| 88 | + } |
| 89 | + |
| 90 | + toHsb(): string { |
| 91 | + return `hsl(${this.color?.toHsb().h},100%, 50%)`; |
| 92 | + } |
| 93 | + |
| 94 | + constructor( |
| 95 | + private cdr: ChangeDetectorRef, |
| 96 | + @Inject(DOCUMENT) private document: Document |
| 97 | + ) {} |
| 98 | + |
| 99 | + ngOnInit(): void { |
| 100 | + this.document.removeEventListener('mousemove', this.mouseMoveRef); |
| 101 | + this.document.removeEventListener('mouseup', this.mouseUpRef); |
| 102 | + this.document.removeEventListener('touchmove', this.mouseMoveRef); |
| 103 | + this.document.removeEventListener('touchend', this.mouseUpRef); |
| 104 | + this.mouseMoveRef = () => null; |
| 105 | + this.mouseUpRef = () => null; |
| 106 | + } |
| 107 | + |
| 108 | + ngOnChanges(changes: SimpleChanges): void { |
| 109 | + const { color } = changes; |
| 110 | + |
| 111 | + if (color) { |
| 112 | + if (!this.dragRef && this.containerRef && this.transformRef) { |
| 113 | + const calcOffset = calculateOffset( |
| 114 | + this.containerRef.nativeElement, |
| 115 | + this.transformRef.nativeElement, |
| 116 | + this.color |
| 117 | + ); |
| 118 | + if (calcOffset) { |
| 119 | + this.offsetValue = calcOffset; |
| 120 | + this.cdr.detectChanges(); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + ngAfterViewInit(): void { |
| 127 | + if (!this.dragRef && this.containerRef && this.transformRef) { |
| 128 | + const calcOffset = calculateOffset(this.containerRef.nativeElement, this.transformRef.nativeElement, this.color); |
| 129 | + if (calcOffset) { |
| 130 | + this.offsetValue = calcOffset; |
| 131 | + this.cdr.detectChanges(); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + dragStartHandle(e: MouseEvent | TouchEvent): void { |
| 137 | + this.onDragStart(e); |
| 138 | + } |
| 139 | + |
| 140 | + updateOffset: EventHandle = (e: EventType, direction: 'x' | 'y' = 'y') => { |
| 141 | + const { pageX, pageY } = getPosition(e); |
| 142 | + const { |
| 143 | + x: rectX, |
| 144 | + y: rectY, |
| 145 | + width, |
| 146 | + height |
| 147 | + } = this.containerRef?.nativeElement?.getBoundingClientRect() || { x: 0, y: 0, width: 0, height: 0 }; |
| 148 | + const { width: targetWidth, height: targetHeight } = this.transformRef?.nativeElement?.getBoundingClientRect() || { |
| 149 | + width: 0, |
| 150 | + height: 0 |
| 151 | + }; |
| 152 | + |
| 153 | + const centerOffsetX = targetWidth / 2; |
| 154 | + const centerOffsetY = targetHeight / 2; |
| 155 | + |
| 156 | + const offsetX = Math.max(0, Math.min(pageX - rectX, width)) - centerOffsetX; |
| 157 | + const offsetY = Math.max(0, Math.min(pageY - rectY, height)) - centerOffsetY; |
| 158 | + |
| 159 | + const calcOffset = { |
| 160 | + x: offsetX, |
| 161 | + y: direction === 'x' ? this.offsetValue.y : offsetY |
| 162 | + }; |
| 163 | + // Exclusion of boundary cases |
| 164 | + if ((targetWidth === 0 && targetHeight === 0) || targetWidth !== targetHeight) { |
| 165 | + return; |
| 166 | + } |
| 167 | + this.offsetValue = calcOffset; |
| 168 | + this.nzOnChange.emit( |
| 169 | + calculateColor(calcOffset, this.containerRef.nativeElement, this.transformRef.nativeElement, this.color) |
| 170 | + ); |
| 171 | + this.cdr.detectChanges(); |
| 172 | + }; |
| 173 | + |
| 174 | + onDragMove: EventHandle = (e: EventType) => { |
| 175 | + e.preventDefault(); |
| 176 | + this.updateOffset(e); |
| 177 | + }; |
| 178 | + |
| 179 | + onDragStop: EventHandle = (e: EventType) => { |
| 180 | + e.preventDefault(); |
| 181 | + this.dragRef = false; |
| 182 | + this.document.removeEventListener('mousemove', this.onDragMove); |
| 183 | + this.document.removeEventListener('mouseup', this.mouseUpRef); |
| 184 | + this.document.removeEventListener('touchmove', this.mouseMoveRef); |
| 185 | + this.document.removeEventListener('touchend', this.mouseUpRef); |
| 186 | + this.mouseMoveRef = () => null; |
| 187 | + this.mouseUpRef = () => null; |
| 188 | + this.nzOnChangeComplete?.emit(); |
| 189 | + }; |
| 190 | + |
| 191 | + onDragStart: EventHandle = (e: EventType) => { |
| 192 | + if (this.disabled) { |
| 193 | + return; |
| 194 | + } |
| 195 | + this.updateOffset(e); |
| 196 | + this.dragRef = true; |
| 197 | + this.document.addEventListener('mousemove', this.onDragMove); |
| 198 | + this.document.addEventListener('mouseup', this.onDragStop); |
| 199 | + this.document.addEventListener('touchmove', this.onDragMove); |
| 200 | + this.document.addEventListener('touchend', this.onDragStop); |
| 201 | + this.mouseMoveRef = this.onDragMove; |
| 202 | + this.mouseUpRef = this.onDragStop; |
| 203 | + this.cdr.markForCheck(); |
| 204 | + }; |
| 205 | +} |
0 commit comments