@dile/crud 2.11.1 → 2.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -210,6 +210,14 @@ export class DileAjaxForm extends DileI18nMixin(LitElement) {
210
210
 
211
211
  doAction() {
212
212
  this.feedback.clear();
213
+ return this.sendData();
214
+ }
215
+
216
+ async sendData() {
217
+ // Forms can implement an optional async prepareData() to compute data before getData() is called
218
+ if (typeof this.form.prepareData === 'function') {
219
+ await this.form.prepareData();
220
+ }
213
221
  this.ajaxsave.data = this.form.getData();
214
222
  this.ajaxsave.generateRequest();
215
223
  }
@@ -55,10 +55,7 @@ export class DileConfirmedAjaxForm extends DileAjaxForm {
55
55
  }
56
56
 
57
57
  onConfirmAccepted() {
58
- // Call the parent's doAction method directly, bypassing our override
59
- const formData = this.form.getData();
60
- this.ajaxsave.data = formData;
61
- this.ajaxsave.generateRequest();
58
+ this.sendData();
62
59
  }
63
60
 
64
61
  onConfirmCancelled() {
@@ -25,6 +25,12 @@ export class DileImageUploader extends DileI18nMixin(LitElement) {
25
25
  maxWidth: { type: Number },
26
26
  minHeight: { type: Number },
27
27
  maxHeight: { type: Number },
28
+ crop: { type: Boolean },
29
+ cropAspectRatio: { type: Number },
30
+ cropOutputWidth: { type: Number },
31
+ cropOutputHeight: { type: Number },
32
+ cropOutputType: { type: String },
33
+ cropRound: { type: Boolean },
28
34
  };
29
35
  }
30
36
 
@@ -32,6 +38,8 @@ export class DileImageUploader extends DileI18nMixin(LitElement) {
32
38
  super();
33
39
  this.responseAdapter = new ResponseApiAdapter();
34
40
  this.allowedExtensions = ['jpeg', 'jpg', 'png'];
41
+ this.crop = false;
42
+ this.cropRound = false;
35
43
  }
36
44
 
37
45
  render() {
@@ -56,6 +64,12 @@ export class DileImageUploader extends DileI18nMixin(LitElement) {
56
64
  .maxWidth=${this.maxWidth}
57
65
  .minHeight=${this.minHeight}
58
66
  .maxHeight=${this.maxHeight}
67
+ ?crop=${this.crop}
68
+ .cropAspectRatio=${this.cropAspectRatio}
69
+ .cropOutputWidth=${this.cropOutputWidth}
70
+ .cropOutputHeight=${this.cropOutputHeight}
71
+ .cropOutputType=${this.cropOutputType}
72
+ ?cropRound=${this.cropRound}
59
73
  ></dile-image-uploader-form>
60
74
  </dile-ajax-form>
61
75
  `;
@@ -23,6 +23,9 @@ export class DileImageUploaderForm extends DileForm(LitElement) {
23
23
  margin: 0.5rem 0;
24
24
  font-size: var(--dile-file-preview-font-size, 0.8rem);
25
25
  }
26
+ dile-image-cropper {
27
+ margin: 0.5rem 0;
28
+ }
26
29
  .image-summary img {
27
30
  max-width: 60px;
28
31
  max-height: 60px;
@@ -42,13 +45,24 @@ export class DileImageUploaderForm extends DileForm(LitElement) {
42
45
  maxWidth: { type: Number },
43
46
  minHeight: { type: Number },
44
47
  maxHeight: { type: Number },
48
+ crop: { type: Boolean },
49
+ cropAspectRatio: { type: Number },
50
+ cropOutputWidth: { type: Number },
51
+ cropOutputHeight: { type: Number },
52
+ cropOutputType: { type: String },
53
+ cropRound: { type: Boolean },
45
54
  imageInfo: { state: true },
55
+ cropFile: { state: true },
46
56
  };
47
57
  }
48
58
 
49
59
  constructor() {
50
60
  super();
51
61
  this.imageInfo = null;
62
+ this.cropFile = null;
63
+ this.crop = false;
64
+ this.cropRound = false;
65
+ this._croppedFile = null;
52
66
  this._inspectToken = 0;
53
67
  }
54
68
 
@@ -66,6 +80,24 @@ export class DileImageUploaderForm extends DileForm(LitElement) {
66
80
  @element-changed=${this.handleFileChange}
67
81
  ></dile-drop-file>
68
82
  ${this.imageSummaryTemplate}
83
+ ${this.cropperTemplate}
84
+ `;
85
+ }
86
+
87
+ get cropperTemplate() {
88
+ if (!this.cropFile) {
89
+ return '';
90
+ }
91
+ return html`
92
+ <dile-image-cropper
93
+ id="cropper"
94
+ .file=${this.cropFile}
95
+ .aspectRatio=${this.cropAspectRatio}
96
+ .outputWidth=${this.cropOutputWidth}
97
+ .outputHeight=${this.cropOutputHeight}
98
+ .outputType=${this.cropOutputType || ''}
99
+ ?round=${this.cropRound}
100
+ ></dile-image-cropper>
69
101
  `;
70
102
  }
71
103
 
@@ -75,7 +107,7 @@ export class DileImageUploaderForm extends DileForm(LitElement) {
75
107
  }
76
108
  return html`
77
109
  <div class="image-summary">
78
- <img src="${this.imageInfo.thumbnail}" alt="">
110
+ ${this.cropFile ? '' : html`<img src="${this.imageInfo.thumbnail}" alt="">`}
79
111
  <span>${this.translations.image_dimensions_summary(this.imageInfo.width, this.imageInfo.height, formatFileSize(this.imageInfo.size))}</span>
80
112
  </div>
81
113
  `;
@@ -85,10 +117,15 @@ export class DileImageUploaderForm extends DileForm(LitElement) {
85
117
  return this.renderRoot.querySelector('#dropfile');
86
118
  }
87
119
 
120
+ get cropperEl() {
121
+ return this.renderRoot.querySelector('#cropper');
122
+ }
123
+
88
124
  async handleFileChange(e) {
89
125
  const file = e.detail.value;
90
126
  const token = ++this._inspectToken;
91
127
  this.imageInfo = null;
128
+ this.resetCrop();
92
129
 
93
130
  if (!file) {
94
131
  return;
@@ -121,6 +158,13 @@ export class DileImageUploaderForm extends DileForm(LitElement) {
121
158
  return;
122
159
  }
123
160
 
161
+ if (this.crop) {
162
+ // Cropper.js is only downloaded when the crop feature is enabled
163
+ await import('@dile/image-cropper/image-cropper.js');
164
+ if (token !== this._inspectToken) return;
165
+ this.cropFile = file;
166
+ }
167
+
124
168
  this.imageInfo = {
125
169
  width: info.width,
126
170
  height: info.height,
@@ -129,6 +173,30 @@ export class DileImageUploaderForm extends DileForm(LitElement) {
129
173
  };
130
174
  }
131
175
 
176
+ async prepareData() {
177
+ this._croppedFile = this.cropFile && this.cropperEl
178
+ ? await this.cropperEl.getFile(this.cropFile.name)
179
+ : null;
180
+ }
181
+
182
+ adaptDataOut(data) {
183
+ if (this._croppedFile && this.cropFile) {
184
+ return { ...data, image: this._croppedFile };
185
+ }
186
+ return data;
187
+ }
188
+
189
+ resetCrop() {
190
+ this.cropFile = null;
191
+ this._croppedFile = null;
192
+ }
193
+
194
+ clearData() {
195
+ super.clearData();
196
+ this.imageInfo = null;
197
+ this.resetCrop();
198
+ }
199
+
132
200
  rejectFile(message) {
133
201
  this.dropFileEl.errored = true;
134
202
  this.dropFileEl.message = message;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/crud",
3
- "version": "2.11.1",
3
+ "version": "2.12.0",
4
4
  "description": "Components to create a generic crud system based on Web Components and Lit",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -25,6 +25,7 @@
25
25
  },
26
26
  "homepage": "https://dile-components.com/",
27
27
  "dependencies": {
28
+ "@dile/image-cropper": "^0.1.1",
28
29
  "@dile/ui": "^3.19.2",
29
30
  "axios": "^1.19.0",
30
31
  "lit": "^2.7.0 || ^3.0.0"
@@ -32,5 +33,5 @@
32
33
  "publishConfig": {
33
34
  "access": "public"
34
35
  },
35
- "gitHead": "545b8f87465a4567904c577699562f3c5f676e68"
36
+ "gitHead": "18e5ffa93ffefd0cfa333a0e30b0df1daa48747f"
36
37
  }