This repository was archived by the owner on Jul 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimage-properties-pane.ts
More file actions
162 lines (159 loc) · 8.49 KB
/
image-properties-pane.ts
File metadata and controls
162 lines (159 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { DocumentEditor } from '@syncfusion/ej2-documenteditor';
import { createElement, KeyboardEventArgs } from '@syncfusion/ej2-base';
import { NumericTextBox } from '@syncfusion/ej2-inputs';
import { CheckBox } from '@syncfusion/ej2-buttons';
/**
* Image Property pane
*/
export class ImageProperties {
private documentEditor: DocumentEditor;
private elementId: string;
public element: HTMLElement;
private widthElement: HTMLElement;
private heightElement: HTMLElement;
private widthNumericBox: NumericTextBox;
private heightNumericBox: NumericTextBox;
private aspectRatioBtn: CheckBox;
private isMaintainAspectRatio: boolean;
private isWidthApply: boolean = false;
private isHeightApply: boolean = false;
constructor(docEditor: DocumentEditor) {
this.documentEditor = docEditor;
this.elementId = this.documentEditor.element.id;
this.isMaintainAspectRatio = false;
this.initializeImageProperties();
}
private initializeImageProperties = (): void => {
// tslint:disable-next-line:max-line-length
this.element = createElement('div', { id: this.elementId + '_imageProperties', styles: 'width:269px;' });
this.element.style.display = 'none';
this.initImageProp();
this.wireEvents();
}
private initImageProp = (): void => {
// tslint:disable-next-line:max-line-length
let imageDiv: HTMLElement = createElement('div', { id: this.elementId + '_imageDiv', className: 'e-de-property-div-padding', styles: 'border:0px' });
this.element.appendChild(imageDiv);
let label: HTMLElement = createElement('label', { className: 'e-de-prop-label', styles: 'padding:3px' });
label.textContent = 'Image';
imageDiv.appendChild(label);
let outerDiv: HTMLElement = createElement('div', { styles: 'margin-left:2px;margin-top:10px' });
imageDiv.appendChild(outerDiv);
this.widthElement = this.createImagePropertiesDiv('_widthDiv', outerDiv, '_widthInput', 'W', 'Width');
// tslint:disable-next-line:max-line-length
this.widthNumericBox = new NumericTextBox({ min: 0, max: 23500, cssClass: 'e-de-image-property', showSpinButton: false, format: 'n0', decimals: 2 });
this.widthNumericBox.appendTo(this.widthElement);
this.heightElement = this.createImagePropertiesDiv('_heightDiv', outerDiv, '_heightInput', 'H', 'Height');
// tslint:disable-next-line:max-line-length
this.heightNumericBox = new NumericTextBox({ min: 0, max: 23500, cssClass: 'e-de-image-property', showSpinButton: false, format: 'n0', decimals: 2 });
this.heightNumericBox.appendTo(this.heightElement);
// tslint:disable-next-line:max-line-length
let aspectRatioDiv: HTMLElement = createElement('div', { id: this.elementId + '_aspectRatioDiv', styles: 'height:14px;margin-left:5px;float:left' });
outerDiv.appendChild(aspectRatioDiv);
// tslint:disable-next-line:max-line-length
let aspectRatio: HTMLInputElement = createElement('input', { id: this.elementId + '_aspectRatio', className: 'e-de-prop-label' }) as HTMLInputElement;
aspectRatioDiv.setAttribute('title', 'Aspect ratio');
aspectRatioDiv.appendChild(aspectRatio);
this.aspectRatioBtn = new CheckBox({ label: 'Aspect ratio' }, aspectRatio);
}
private createImagePropertiesDiv = (id: string, outerDiv: HTMLElement, inputId: string, spanContent: string, tooltip: string): HTMLElement => {
// tslint:disable-next-line:max-line-length
let divElement: HTMLElement = createElement('div', { id: this.elementId + id, styles: 'position: relative;width: 100%;margin-right:6px; float:left;margin-bottom: 7px;' });
divElement.setAttribute('title', tooltip);
outerDiv.appendChild(divElement);
// tslint:disable-next-line:max-line-length
let inputElement: HTMLElement = createElement('input', { id: this.elementId + inputId, className: 'e-textbox', styles: 'width:100%;' });
divElement.appendChild(inputElement);
let spanElement: HTMLElement = createElement('span', { styles: 'position: absolute;left:8px;top:8px;color:#8C8C8C;' });
spanElement.textContent = spanContent;
divElement.appendChild(spanElement);
return inputElement;
}
public wireEvents = (): void => {
this.aspectRatioBtn.element.addEventListener('change', this.onAspectRatioBtnClick);
this.widthNumericBox.element.addEventListener('click', (): void => { this.isWidthApply = true; });
this.heightNumericBox.element.addEventListener('click', (): void => { this.isHeightApply = true; });
this.widthNumericBox.element.addEventListener('keydown', this.onImageWidth);
this.heightNumericBox.element.addEventListener('keydown', this.onImageHeight);
this.widthNumericBox.element.addEventListener('blur', (): void => { this.applyImageWidth(); this.isWidthApply = false; });
this.heightNumericBox.element.addEventListener('blur', (): void => { this.applyImageHeight(); this.isHeightApply = false; });
}
private onImageWidth = (e: KeyboardEventArgs): void => {
if (e.keyCode === 13) {
setTimeout((): void => { this.applyImageWidth(); this.isWidthApply = false; }, 30);
}
}
private onImageHeight = (e: KeyboardEventArgs): void => {
if (e.keyCode === 13) {
setTimeout((): void => { this.applyImageHeight(); this.isHeightApply = false; }, 30);
}
}
private applyImageWidth = (): void => {
if (!this.isMaintainAspectRatio) {
// tslint:disable-next-line:max-line-length
let width: number = this.widthNumericBox.value;
let height: number = this.heightNumericBox.value;
if (width > this.widthNumericBox.max) {
width = this.widthNumericBox.max;
}
if (height > this.heightNumericBox.max) {
height = this.heightNumericBox.max;
}
if (!(width === null || height === null)) {
this.documentEditor.selection.imageFormat.resize(width, height);
}
} else if (this.isMaintainAspectRatio) {
// tslint:disable-next-line:max-line-length
let width: number = this.widthNumericBox.value;
if (width > this.widthNumericBox.max) {
width = this.widthNumericBox.max;
}
let ratio: number = width / this.documentEditor.selection.imageFormat.width;
let height: number = this.heightNumericBox.value * ratio;
this.heightNumericBox.value = height;
if (!(width === null || height === null)) {
this.documentEditor.selection.imageFormat.resize(width, height);
}
}
}
private applyImageHeight = (): void => {
if (!this.isMaintainAspectRatio) {
// tslint:disable-next-line:max-line-length
let width: number = this.widthNumericBox.value;
let height: number = this.heightNumericBox.value;
if (!(width === null || height === null)) {
this.documentEditor.selection.imageFormat.resize(width, height);
}
} else if (this.isMaintainAspectRatio) {
// tslint:disable-next-line:max-line-length
let height: number = this.heightNumericBox.value;
let ratio: number = height / this.documentEditor.selection.imageFormat.height;
let width: number = this.widthNumericBox.value * ratio;
this.widthNumericBox.value = width;
if (!(width === null || height === null)) {
this.documentEditor.selection.imageFormat.resize(width, height);
}
}
}
private onAspectRatioBtnClick = (): void => {
if (this.isMaintainAspectRatio) {
this.isMaintainAspectRatio = false;
} else {
this.isMaintainAspectRatio = true;
}
}
public showImageProperties(isShow: boolean): void {
if (this.element.style.display === 'block') {
this.updateImageProperties();
}
if (!isShow && this.element.style.display === 'none' || (isShow && this.element.style.display === 'block')) {
return;
}
this.element.style.display = isShow ? 'block' : 'none';
this.documentEditor.resize();
}
public updateImageProperties(): void {
this.widthNumericBox.value = this.documentEditor.selection.imageFormat.width;
this.heightNumericBox.value = this.documentEditor.selection.imageFormat.height;
}
}