vgapp 1.1.1 → 1.1.3
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.
- package/CHANGELOG.md +8 -0
- package/README.md +19 -16
- package/app/langs/en/buttons.json +17 -2
- package/app/langs/en/messages.json +36 -1
- package/app/langs/ru/buttons.json +17 -2
- package/app/langs/ru/messages.json +69 -34
- package/app/modules/module-fn.js +15 -9
- package/app/modules/vgfilepreview/index.js +3 -0
- package/app/modules/vgfilepreview/js/i18n.js +56 -0
- package/app/modules/vgfilepreview/js/renderers/image-modal.js +145 -0
- package/app/modules/vgfilepreview/js/renderers/image.js +92 -0
- package/app/modules/vgfilepreview/js/renderers/index.js +19 -0
- package/app/modules/vgfilepreview/js/renderers/office-modal.js +168 -0
- package/app/modules/vgfilepreview/js/renderers/office.js +79 -0
- package/app/modules/vgfilepreview/js/renderers/pdf-modal.js +260 -0
- package/app/modules/vgfilepreview/js/renderers/pdf.js +76 -0
- package/app/modules/vgfilepreview/js/renderers/playlist.js +71 -0
- package/app/modules/vgfilepreview/js/renderers/text-modal.js +343 -0
- package/app/modules/vgfilepreview/js/renderers/text.js +83 -0
- package/app/modules/vgfilepreview/js/renderers/video-modal.js +272 -0
- package/app/modules/vgfilepreview/js/renderers/video.js +80 -0
- package/app/modules/vgfilepreview/js/renderers/zip-modal.js +522 -0
- package/app/modules/vgfilepreview/js/renderers/zip.js +89 -0
- package/app/modules/vgfilepreview/js/vgfilepreview.js +532 -0
- package/app/modules/vgfilepreview/readme.md +68 -0
- package/app/modules/vgfilepreview/scss/_variables.scss +113 -0
- package/app/modules/vgfilepreview/scss/vgfilepreview.scss +460 -0
- package/app/modules/vgfiles/js/base.js +463 -175
- package/app/modules/vgfiles/js/droppable.js +260 -260
- package/app/modules/vgfiles/js/render.js +153 -153
- package/app/modules/vgfiles/js/vgfiles.js +41 -29
- package/app/modules/vgfiles/readme.md +116 -217
- package/app/modules/vgfiles/scss/_variables.scss +18 -10
- package/app/modules/vgfiles/scss/vgfiles.scss +153 -59
- package/app/modules/vgformsender/js/vgformsender.js +7 -2
- package/app/modules/vgmodal/js/vgmodal.js +12 -0
- package/app/modules/vgnav/js/vgnav.js +135 -135
- package/app/modules/vgnav/readme.md +67 -67
- package/app/modules/vgnestable/README.md +307 -307
- package/app/modules/vgnestable/scss/_variables.scss +60 -60
- package/app/modules/vgnestable/scss/vgnestable.scss +163 -163
- package/app/modules/vgselect/js/vgselect.js +62 -59
- package/app/modules/vgselect/scss/vgselect.scss +22 -22
- package/app/modules/vgspy/readme.md +28 -28
- package/app/utils/js/components/audio-metadata.js +240 -0
- package/app/utils/js/components/file-icon.js +109 -0
- package/app/utils/js/components/file-preview.js +304 -0
- package/app/utils/js/components/sanitize.js +150 -150
- package/build/vgapp.css +1 -1
- package/build/vgapp.css.map +1 -1
- package/index.js +1 -0
- package/index.scss +9 -6
- package/package.json +1 -1
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import BaseModule from "../../base-module";
|
|
2
|
+
import {mergeDeepObject} from "../../../utils/js/functions";
|
|
3
|
+
import FilePreviewHelper from "../../../utils/js/components/file-preview";
|
|
4
|
+
import {getSVG} from "../../module-fn";
|
|
5
|
+
import createFilePreviewRenderers from "./renderers";
|
|
6
|
+
import {createFilePreviewI18n, resolveFilePreviewLang} from "./i18n";
|
|
7
|
+
|
|
8
|
+
const NAME = 'filepreview';
|
|
9
|
+
const NAME_KEY = 'vg.filepreview';
|
|
10
|
+
|
|
11
|
+
class VGFilePreview extends BaseModule {
|
|
12
|
+
constructor(el, params = {}) {
|
|
13
|
+
super(el, params);
|
|
14
|
+
|
|
15
|
+
this._params = this._getParams(el, mergeDeepObject({
|
|
16
|
+
validate: true,
|
|
17
|
+
lang: 'ru',
|
|
18
|
+
ui: {
|
|
19
|
+
nameOnly: false
|
|
20
|
+
}
|
|
21
|
+
}, params));
|
|
22
|
+
|
|
23
|
+
this._filePath = '';
|
|
24
|
+
this._fileUrl = null;
|
|
25
|
+
this._isValid = false;
|
|
26
|
+
this._fileMeta = {};
|
|
27
|
+
this._fields = [];
|
|
28
|
+
this._editableFields = {};
|
|
29
|
+
this._helper = new FilePreviewHelper(this._element);
|
|
30
|
+
this._renderers = createFilePreviewRenderers();
|
|
31
|
+
this._lang = resolveFilePreviewLang(this._params.lang, this._element);
|
|
32
|
+
this._i18n = createFilePreviewI18n(this._lang);
|
|
33
|
+
this._inlineAudio = null;
|
|
34
|
+
this._inlineAudioSrc = '';
|
|
35
|
+
this._inlineAudioButton = null;
|
|
36
|
+
this._inlineAudioIcon = null;
|
|
37
|
+
this._inlineAudioContainer = null;
|
|
38
|
+
|
|
39
|
+
this.init();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static get NAME() {
|
|
43
|
+
return NAME;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static get NAME_KEY() {
|
|
47
|
+
return NAME_KEY;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get filePath() {
|
|
51
|
+
return this._filePath;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get fileUrl() {
|
|
55
|
+
return this._fileUrl;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
get isValid() {
|
|
59
|
+
return this._isValid;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get fileMeta() {
|
|
63
|
+
return this._fileMeta;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
get fields() {
|
|
67
|
+
return this._fields;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
get editableFields() {
|
|
71
|
+
return this._editableFields;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
init() {
|
|
75
|
+
const filePath = this._helper.getFilePath();
|
|
76
|
+
this._filePath = filePath;
|
|
77
|
+
|
|
78
|
+
if (this._params.validate) {
|
|
79
|
+
const validation = this._helper.validateFilePath(filePath);
|
|
80
|
+
this._isValid = validation.isValid;
|
|
81
|
+
this._fileUrl = validation.fileUrl;
|
|
82
|
+
} else {
|
|
83
|
+
this._isValid = true;
|
|
84
|
+
this._fileUrl = null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this._helper.syncState(this._isValid);
|
|
88
|
+
|
|
89
|
+
if (!this._isValid) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
this._fileMeta = this._helper.getFileMeta(this._filePath);
|
|
94
|
+
return this.preview();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
preview() {
|
|
98
|
+
this._setState('loading');
|
|
99
|
+
this._fields = this._helper.getFields();
|
|
100
|
+
this._editableFields = this._helper.resolveEditableFields(this._fields);
|
|
101
|
+
this._helper.syncEditableFields(this._editableFields);
|
|
102
|
+
this._renderIcon();
|
|
103
|
+
this._renderTextFields();
|
|
104
|
+
this._renderDownloadField();
|
|
105
|
+
this._renderPreview();
|
|
106
|
+
|
|
107
|
+
return this._editableFields;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
_renderIcon() {
|
|
111
|
+
const iconField = this._editableFields.icon;
|
|
112
|
+
if (!iconField) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const icon = getSVG(this._filePath);
|
|
117
|
+
if (!icon) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
iconField.innerHTML = icon;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
_renderTextFields() {
|
|
125
|
+
const nameField = this._editableFields.name;
|
|
126
|
+
if (nameField) {
|
|
127
|
+
if (this._isAudioFile()) {
|
|
128
|
+
this._renderAudioNameField(nameField);
|
|
129
|
+
this._element.setAttribute('data-vg-filepreview-renderer', 'audio');
|
|
130
|
+
} else if (this._fileMeta.name) {
|
|
131
|
+
nameField.classList.remove('vg-filepreview-audio-inline');
|
|
132
|
+
nameField.textContent = this._fileMeta.name;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const extField = this._editableFields.ext;
|
|
137
|
+
if (extField && this._fileMeta.ext) {
|
|
138
|
+
extField.textContent = this._fileMeta.ext;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const sizeField = this._editableFields.size;
|
|
142
|
+
if (sizeField && this._fileMeta.sizeText) {
|
|
143
|
+
sizeField.textContent = this._fileMeta.sizeText;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const originalNameField = this._editableFields.original_name;
|
|
147
|
+
if (!originalNameField) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (this._fileMeta.originalName) {
|
|
152
|
+
originalNameField.textContent = this._fileMeta.originalName;
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (this._fileMeta.isMedia) {
|
|
157
|
+
originalNameField.textContent = '';
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
_renderAudioNameField(nameField) {
|
|
162
|
+
const displayName = String(
|
|
163
|
+
this._element?.getAttribute('data-vg-filepreview-display-name')
|
|
164
|
+
|| this._fileMeta?.originalName
|
|
165
|
+
|| this._fileMeta?.name
|
|
166
|
+
|| ''
|
|
167
|
+
).trim();
|
|
168
|
+
|
|
169
|
+
const fileName = displayName;
|
|
170
|
+
if (!fileName) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
nameField.innerHTML = '';
|
|
175
|
+
nameField.classList.add('vg-filepreview-audio-inline');
|
|
176
|
+
|
|
177
|
+
const button = document.createElement('button');
|
|
178
|
+
button.type = 'button';
|
|
179
|
+
button.className = 'vg-filepreview-audio-inline__toggle';
|
|
180
|
+
button.setAttribute('aria-label', 'Play/Pause');
|
|
181
|
+
|
|
182
|
+
const icon = document.createElement('span');
|
|
183
|
+
icon.className = 'vg-filepreview-audio-inline__icon';
|
|
184
|
+
button.appendChild(icon);
|
|
185
|
+
|
|
186
|
+
const text = document.createElement('span');
|
|
187
|
+
text.className = 'vg-filepreview-audio-inline__name';
|
|
188
|
+
text.textContent = fileName;
|
|
189
|
+
|
|
190
|
+
button.addEventListener('click', (event) => {
|
|
191
|
+
event.preventDefault();
|
|
192
|
+
event.stopPropagation();
|
|
193
|
+
this._toggleInlineAudio();
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
nameField.appendChild(button);
|
|
197
|
+
nameField.appendChild(text);
|
|
198
|
+
|
|
199
|
+
this._inlineAudioButton = button;
|
|
200
|
+
this._inlineAudioIcon = icon;
|
|
201
|
+
const rootFile = this._element?.classList?.contains('file')
|
|
202
|
+
? this._element
|
|
203
|
+
: this._element?.closest?.('.file');
|
|
204
|
+
this._inlineAudioContainer = rootFile || this._element || nameField;
|
|
205
|
+
this._setInlineAudioProgress(0);
|
|
206
|
+
const isCurrentAudioPlaying = VGFilePreview._activeAudioOwner === this && this._inlineAudio && !this._inlineAudio.paused;
|
|
207
|
+
this._syncInlineAudioIcon(isCurrentAudioPlaying);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
_toggleInlineAudio() {
|
|
211
|
+
const src = this._fileUrl?.href || this._filePath || '';
|
|
212
|
+
if (!src || !this._inlineAudioButton) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (VGFilePreview._activeAudioOwner && VGFilePreview._activeAudioOwner !== this) {
|
|
217
|
+
VGFilePreview._activeAudioOwner._stopInlineAudio();
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (!this._inlineAudio || this._inlineAudioSrc !== src) {
|
|
221
|
+
this._stopInlineAudio();
|
|
222
|
+
this._inlineAudio = new Audio(src);
|
|
223
|
+
this._inlineAudioSrc = src;
|
|
224
|
+
this._inlineAudio.addEventListener('ended', () => {
|
|
225
|
+
this._syncInlineAudioIcon(false);
|
|
226
|
+
this._setInlineAudioProgress(0);
|
|
227
|
+
});
|
|
228
|
+
this._inlineAudio.addEventListener('timeupdate', () => this._syncInlineAudioProgress());
|
|
229
|
+
this._inlineAudio.addEventListener('loadedmetadata', () => this._syncInlineAudioProgress());
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (this._inlineAudio.paused) {
|
|
233
|
+
this._inlineAudio.play().then(() => {
|
|
234
|
+
VGFilePreview._activeAudioOwner = this;
|
|
235
|
+
this._syncInlineAudioIcon(true);
|
|
236
|
+
}).catch(() => {
|
|
237
|
+
this._syncInlineAudioIcon(false);
|
|
238
|
+
});
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
this._inlineAudio.pause();
|
|
243
|
+
this._syncInlineAudioIcon(false);
|
|
244
|
+
if (VGFilePreview._activeAudioOwner === this) {
|
|
245
|
+
VGFilePreview._activeAudioOwner = null;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
_stopInlineAudio() {
|
|
250
|
+
if (!this._inlineAudio) {
|
|
251
|
+
this._syncInlineAudioIcon(false);
|
|
252
|
+
this._setInlineAudioProgress(0);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
this._inlineAudio.pause();
|
|
257
|
+
this._inlineAudio.currentTime = 0;
|
|
258
|
+
this._syncInlineAudioIcon(false);
|
|
259
|
+
this._setInlineAudioProgress(0);
|
|
260
|
+
if (VGFilePreview._activeAudioOwner === this) {
|
|
261
|
+
VGFilePreview._activeAudioOwner = null;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
_syncInlineAudioIcon(isPlaying) {
|
|
266
|
+
if (!this._inlineAudioIcon) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
this._inlineAudioIcon.innerHTML = isPlaying ? (getSVG('pause') || '') : (getSVG('play') || '');
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
_syncInlineAudioProgress() {
|
|
274
|
+
if (!this._inlineAudio) {
|
|
275
|
+
this._setInlineAudioProgress(0);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const duration = Number(this._inlineAudio.duration || 0);
|
|
280
|
+
const currentTime = Number(this._inlineAudio.currentTime || 0);
|
|
281
|
+
if (!duration || !Number.isFinite(duration) || duration <= 0) {
|
|
282
|
+
this._setInlineAudioProgress(0);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const progress = Math.max(0, Math.min(100, (currentTime / duration) * 100));
|
|
287
|
+
this._setInlineAudioProgress(progress);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
_setInlineAudioProgress(percent) {
|
|
291
|
+
if (!this._inlineAudioContainer) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const normalized = Math.max(0, Math.min(100, Number(percent) || 0));
|
|
296
|
+
this._inlineAudioContainer.style.setProperty('--vg-filepreview-audio-inline-progress', `${normalized}%`);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
_renderDownloadField() {
|
|
300
|
+
const downloadField = this._editableFields.download;
|
|
301
|
+
if (!downloadField) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const downloadLabel = this._i18n?.button('download') || '';
|
|
306
|
+
const downloadIcon = getSVG('download') || '';
|
|
307
|
+
const fieldTag = String(downloadField.tagName || '').toUpperCase();
|
|
308
|
+
let control = null;
|
|
309
|
+
|
|
310
|
+
if (fieldTag === 'A' || fieldTag === 'BUTTON') {
|
|
311
|
+
control = downloadField;
|
|
312
|
+
} else {
|
|
313
|
+
control = downloadField.querySelector('[data-vg-filepreview-download-control]');
|
|
314
|
+
if (!control) {
|
|
315
|
+
control = document.createElement('button');
|
|
316
|
+
control.type = 'button';
|
|
317
|
+
downloadField.innerHTML = '';
|
|
318
|
+
downloadField.appendChild(control);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
control.classList.add('vg-filepreview-download-trigger');
|
|
323
|
+
control.setAttribute('data-vg-filepreview-download-control', 'true');
|
|
324
|
+
|
|
325
|
+
if (fieldTag === 'A' && control === downloadField) {
|
|
326
|
+
control.setAttribute('href', this._fileUrl?.href || this._filePath || '#');
|
|
327
|
+
if (this._fileMeta?.name) {
|
|
328
|
+
control.setAttribute('download', this._fileMeta.name);
|
|
329
|
+
}
|
|
330
|
+
} else if (String(control.tagName || '').toUpperCase() === 'BUTTON') {
|
|
331
|
+
control.setAttribute('type', 'button');
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (!control.hasAttribute('data-vg-filepreview-download-content-init')) {
|
|
335
|
+
control.setAttribute('data-vg-filepreview-download-content-init', 'true');
|
|
336
|
+
control.innerHTML = '';
|
|
337
|
+
|
|
338
|
+
if (downloadIcon) {
|
|
339
|
+
const icon = document.createElement('span');
|
|
340
|
+
icon.className = 'vg-filepreview-download-trigger__icon';
|
|
341
|
+
icon.innerHTML = downloadIcon;
|
|
342
|
+
control.appendChild(icon);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (downloadLabel) {
|
|
346
|
+
const text = document.createElement('span');
|
|
347
|
+
text.className = 'vg-filepreview-download-trigger__text';
|
|
348
|
+
text.textContent = downloadLabel;
|
|
349
|
+
control.appendChild(text);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (!control.hasAttribute('data-vg-filepreview-download-bind')) {
|
|
354
|
+
control.setAttribute('data-vg-filepreview-download-bind', 'true');
|
|
355
|
+
control.addEventListener('click', (event) => {
|
|
356
|
+
event.preventDefault();
|
|
357
|
+
this._downloadFile();
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
_downloadFile() {
|
|
363
|
+
const src = this._fileUrl?.href || this._filePath || '';
|
|
364
|
+
if (!src) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const fileName = this._fileMeta?.originalName || this._fileMeta?.name || 'file';
|
|
369
|
+
|
|
370
|
+
fetch(src, {
|
|
371
|
+
method: 'GET',
|
|
372
|
+
credentials: 'same-origin'
|
|
373
|
+
})
|
|
374
|
+
.then((response) => {
|
|
375
|
+
if (!response.ok) {
|
|
376
|
+
throw new Error(`HTTP ${response.status}`);
|
|
377
|
+
}
|
|
378
|
+
return response.blob();
|
|
379
|
+
})
|
|
380
|
+
.then((blob) => {
|
|
381
|
+
const objectUrl = URL.createObjectURL(blob);
|
|
382
|
+
this._downloadByLink(objectUrl, fileName);
|
|
383
|
+
setTimeout(() => URL.revokeObjectURL(objectUrl), 1000);
|
|
384
|
+
})
|
|
385
|
+
.catch(() => {
|
|
386
|
+
this._downloadByLink(src, fileName);
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
_downloadByLink(href, fileName) {
|
|
391
|
+
const link = document.createElement('a');
|
|
392
|
+
link.href = href;
|
|
393
|
+
link.style.display = 'none';
|
|
394
|
+
link.rel = 'noopener noreferrer';
|
|
395
|
+
if (fileName) {
|
|
396
|
+
link.setAttribute('download', fileName);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
document.body.appendChild(link);
|
|
400
|
+
link.click();
|
|
401
|
+
document.body.removeChild(link);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
_renderPreview() {
|
|
405
|
+
const isNameOnly = Boolean(this._params?.ui?.nameOnly);
|
|
406
|
+
const previewContainer = this._resolvePreviewContainer({
|
|
407
|
+
autoCreate: !isNameOnly
|
|
408
|
+
});
|
|
409
|
+
if (!previewContainer && !isNameOnly) {
|
|
410
|
+
this._setState('error');
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (previewContainer) {
|
|
415
|
+
previewContainer.innerHTML = '';
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const context = {
|
|
419
|
+
element: this._element,
|
|
420
|
+
filePath: this._filePath,
|
|
421
|
+
fileUrl: this._fileUrl,
|
|
422
|
+
fileMeta: this._fileMeta,
|
|
423
|
+
previewContainer,
|
|
424
|
+
lang: this._lang,
|
|
425
|
+
i18n: this._i18n,
|
|
426
|
+
ui: this._params?.ui || {}
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
let rendered = false;
|
|
430
|
+
this._renderers.forEach((renderer) => {
|
|
431
|
+
if (rendered || typeof renderer?.canRender !== 'function' || typeof renderer?.render !== 'function') {
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
if (!renderer.canRender(context)) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
try {
|
|
440
|
+
rendered = renderer.render(context) === true;
|
|
441
|
+
} catch (error) {
|
|
442
|
+
rendered = false;
|
|
443
|
+
}
|
|
444
|
+
if (rendered) {
|
|
445
|
+
this._element.setAttribute('data-vg-filepreview-renderer', renderer.name || 'custom');
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
if (!rendered && !isNameOnly) {
|
|
450
|
+
this._element.removeAttribute('data-vg-filepreview-renderer');
|
|
451
|
+
this._setState('empty');
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
this._setState('ready');
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
_resolvePreviewContainer(params = {}) {
|
|
458
|
+
const autoCreate = !Object.prototype.hasOwnProperty.call(params, 'autoCreate') || Boolean(params.autoCreate);
|
|
459
|
+
const editablePreview = this._editableFields.preview;
|
|
460
|
+
if (editablePreview) {
|
|
461
|
+
editablePreview.setAttribute('data-vg-filepreview-slot', 'preview');
|
|
462
|
+
return editablePreview;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const existedPreview = this._element.querySelector('[data-vg-filepreview-slot="preview"]');
|
|
466
|
+
if (existedPreview) {
|
|
467
|
+
this._editableFields.preview = existedPreview;
|
|
468
|
+
return existedPreview;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
if (!autoCreate) {
|
|
472
|
+
return null;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const container = document.createElement('div');
|
|
476
|
+
container.className = 'preview';
|
|
477
|
+
container.setAttribute('data-vg-filepreview-slot', 'preview');
|
|
478
|
+
this._element.appendChild(container);
|
|
479
|
+
this._editableFields.preview = container;
|
|
480
|
+
|
|
481
|
+
return container;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
static init(element, params = {}) {
|
|
485
|
+
return VGFilePreview.getOrCreateInstance(element, params);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
static stopActiveInlineAudio() {
|
|
489
|
+
const owner = VGFilePreview._activeAudioOwner;
|
|
490
|
+
if (owner && typeof owner._stopInlineAudio === 'function') {
|
|
491
|
+
owner._stopInlineAudio();
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
VGFilePreview._activeAudioOwner = null;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
static stopActiveInlineAudioIfDetached(nodes = []) {
|
|
498
|
+
const owner = VGFilePreview._activeAudioOwner;
|
|
499
|
+
if (!owner || !owner._element || !Array.isArray(nodes) || !nodes.length) {
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const shouldStop = nodes.some((node) => {
|
|
504
|
+
if (!node || typeof node.contains !== 'function') {
|
|
505
|
+
return false;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
return node === owner._element || node.contains(owner._element);
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
if (shouldStop) {
|
|
512
|
+
owner._stopInlineAudio();
|
|
513
|
+
VGFilePreview._activeAudioOwner = null;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
_setState(state = '') {
|
|
518
|
+
const value = String(state || '').trim();
|
|
519
|
+
if (!value) {
|
|
520
|
+
this._element.removeAttribute('data-vg-filepreview-state');
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
this._element.setAttribute('data-vg-filepreview-state', value);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
_isAudioFile() {
|
|
527
|
+
const ext = String(this._fileMeta?.ext || '').toLowerCase();
|
|
528
|
+
return ['.mp3', '.wav', '.ogg', '.flac', '.aac', '.m4a', '.opus', '.wma'].includes(ext);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
export default VGFilePreview;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
## VGFilePreview
|
|
2
|
+
|
|
3
|
+
Модуль предпросмотра файлов для элементов с атрибутом `data-vg-filepreview`.
|
|
4
|
+
|
|
5
|
+
### Новые фичи
|
|
6
|
+
|
|
7
|
+
- Inline-аудио в поле `.name`: play/pause, прогресс через CSS-переменную `--vg-filepreview-audio-inline-progress`, и контроль единственного активного аудио.
|
|
8
|
+
- Унифицированная кнопка скачивания: модуль сам создает/инициализирует control в поле `download`, скачивает через `fetch + blob` и имеет fallback на прямую ссылку.
|
|
9
|
+
- Режим `ui.nameOnly`: рендер только действий по имени файла (без кнопок/контейнера предпросмотра).
|
|
10
|
+
- Автоопределение языка (`ru`/`en`) с приоритетом: `params.lang` -> `element[lang]` -> ближайший `[lang]` -> `<html lang>` -> `navigator.language`.
|
|
11
|
+
- Видео-плейлист между соседними превью: `prev/next`, циклическая навигация и hotkeys `ArrowLeft/ArrowRight`.
|
|
12
|
+
- ZIP-предпросмотр с кэшем: таблица содержимого архива + предпросмотр поддерживаемых файлов внутри архива (текст/изображения), включая deflate-raw через `DecompressionStream`.
|
|
13
|
+
- Текстовый/Markdown modal с кэшем, безопасной обработкой ссылок и прерыванием прошлых запросов (`AbortController`).
|
|
14
|
+
|
|
15
|
+
### Что умеет модуль
|
|
16
|
+
|
|
17
|
+
- Определяет тип файла и подставляет SVG-иконку.
|
|
18
|
+
- Заполняет поля карточки: `name`, `ext`, `size`, `original_name`, `icon`, `download`, `preview`.
|
|
19
|
+
- Запускает предпросмотр по типам файлов:
|
|
20
|
+
- изображения (`png/jpg/webp/svg/...`);
|
|
21
|
+
- видео (`mp4/webm/mov/mkv/avi/m4v`);
|
|
22
|
+
- текст (`txt/md/csv/json/xml/yml/yaml/log/ini/conf/env`);
|
|
23
|
+
- `pdf`;
|
|
24
|
+
- office (`doc/docx/xls/xlsx/ppt/pptx/odt/ods/odp`);
|
|
25
|
+
- архивы (`zip`).
|
|
26
|
+
- Для интерактивных рендеров автоматически вешает обработчики клика на `.name` (`is-preview-action`).
|
|
27
|
+
|
|
28
|
+
### Состояния и валидация
|
|
29
|
+
|
|
30
|
+
- Валидация пути через helper (`data-vg-filepreview-valid`, `data-vg-filepreview-error`).
|
|
31
|
+
- Текущее состояние рендера в `data-vg-filepreview-state`:
|
|
32
|
+
- `loading`
|
|
33
|
+
- `ready`
|
|
34
|
+
- `empty`
|
|
35
|
+
- `error`
|
|
36
|
+
- Выбранный рендерер в `data-vg-filepreview-renderer` (`image`, `video`, `pdf`, `office`, `zip`, `text`, `audio`).
|
|
37
|
+
|
|
38
|
+
### Атрибуты и слоты
|
|
39
|
+
|
|
40
|
+
- `data-vg-filepreview` — путь к файлу (обязательный).
|
|
41
|
+
- `data-fields` — список полей для синхронизации (например, `name,size,download`).
|
|
42
|
+
- Поддерживаемые поля: `icon`, `name`, `ext`, `size`, `original_name`, `preview`, `download`.
|
|
43
|
+
- Если поле `preview` не найдено, модуль создает `<div class="preview" data-vg-filepreview-slot="preview">` автоматически (кроме `ui.nameOnly`).
|
|
44
|
+
|
|
45
|
+
### Параметры
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
new VGFilePreview(element, {
|
|
49
|
+
validate: true,
|
|
50
|
+
lang: 'ru',
|
|
51
|
+
ui: {
|
|
52
|
+
nameOnly: false
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
- `validate` (`boolean`, default `true`) — проверка пути до файла.
|
|
58
|
+
- `lang` (`'ru' | 'en'`) — язык кнопок/сообщений.
|
|
59
|
+
- `ui.nameOnly` (`boolean`, default `false`) — не создавать UI-контейнер предпросмотра.
|
|
60
|
+
|
|
61
|
+
### Расширение
|
|
62
|
+
|
|
63
|
+
Рендереры подключаются через `js/renderers/index.js`.
|
|
64
|
+
|
|
65
|
+
1. Создайте рендерер в `js/renderers/`.
|
|
66
|
+
2. Реализуйте `canRender(context)` и `render(context)`.
|
|
67
|
+
3. Добавьте рендерер в `js/renderers/index.js` в нужном порядке приоритета.
|
|
68
|
+
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
$filepreview-map: (
|
|
2
|
+
panel: (
|
|
3
|
+
bg: #111,
|
|
4
|
+
color: #fff,
|
|
5
|
+
border: 1px solid rgba(255, 255, 255, .12),
|
|
6
|
+
),
|
|
7
|
+
modal: (
|
|
8
|
+
content-bg: #111,
|
|
9
|
+
content-color: #fff,
|
|
10
|
+
close-top: 10px,
|
|
11
|
+
close-right: 10px,
|
|
12
|
+
body-padding: 1rem 0 0,
|
|
13
|
+
title-size: 13px,
|
|
14
|
+
title-weight: 600,
|
|
15
|
+
title-line-height: 1.3,
|
|
16
|
+
title-margin-bottom: 10px,
|
|
17
|
+
),
|
|
18
|
+
audio-inline: (
|
|
19
|
+
gap: 12px,
|
|
20
|
+
toggle-size: 24px,
|
|
21
|
+
icon-size: 18px,
|
|
22
|
+
toggle-radius: 999px,
|
|
23
|
+
toggle-bg: rgba(255, 255, 255, .12),
|
|
24
|
+
toggle-color: #fff,
|
|
25
|
+
name-line-height: 1.2,
|
|
26
|
+
progress-track: rgba(255, 255, 255, .06),
|
|
27
|
+
progress-fill: rgba(255, 255, 255, .16),
|
|
28
|
+
progress-radius: 6px,
|
|
29
|
+
progress-padding-x: 4px,
|
|
30
|
+
),
|
|
31
|
+
trigger: (
|
|
32
|
+
radius: 8px,
|
|
33
|
+
padding: 8px 12px,
|
|
34
|
+
font-size: 13px,
|
|
35
|
+
font-weight: 600,
|
|
36
|
+
color: #fff,
|
|
37
|
+
image-bg: #0f766e,
|
|
38
|
+
text-bg: #334155,
|
|
39
|
+
video-bg: #7c3aed,
|
|
40
|
+
zip-bg: #b45309,
|
|
41
|
+
),
|
|
42
|
+
image: (
|
|
43
|
+
body-gap: 12px,
|
|
44
|
+
img-max-width: min(92vw, 1240px),
|
|
45
|
+
img-max-height: calc(92vh - 80px),
|
|
46
|
+
img-radius: 8px,
|
|
47
|
+
thumb-margin-top: 10px,
|
|
48
|
+
thumb-max-width: 180px,
|
|
49
|
+
thumb-max-height: 100px,
|
|
50
|
+
thumb-radius: 8px,
|
|
51
|
+
),
|
|
52
|
+
text: (
|
|
53
|
+
content-padding: 12px,
|
|
54
|
+
content-max-height: calc(84vh - 90px),
|
|
55
|
+
content-bg: rgba(255, 255, 255, .06),
|
|
56
|
+
content-radius: 8px,
|
|
57
|
+
content-font-size: 13px,
|
|
58
|
+
content-line-height: 1.45,
|
|
59
|
+
markdown-heading-line-height: 1.25,
|
|
60
|
+
markdown-block-margin: 0 0 10px,
|
|
61
|
+
markdown-list-margin: 0 0 10px 20px,
|
|
62
|
+
markdown-code-padding: 10px,
|
|
63
|
+
markdown-code-radius: 6px,
|
|
64
|
+
markdown-code-bg: rgba(255, 255, 255, .08),
|
|
65
|
+
markdown-link-color: #93c5fd,
|
|
66
|
+
),
|
|
67
|
+
video: (
|
|
68
|
+
player-width: min(92vw, 1240px),
|
|
69
|
+
player-max-height: calc(86vh - 90px),
|
|
70
|
+
player-radius: 8px,
|
|
71
|
+
player-bg: #000,
|
|
72
|
+
),
|
|
73
|
+
pdf: (
|
|
74
|
+
toolbar-gap: 8px,
|
|
75
|
+
toolbar-margin-bottom: 10px,
|
|
76
|
+
btn-radius: 8px,
|
|
77
|
+
btn-padding: 6px 10px,
|
|
78
|
+
btn-font-size: 12px,
|
|
79
|
+
btn-font-weight: 600,
|
|
80
|
+
btn-bg: rgba(255, 255, 255, .12),
|
|
81
|
+
meta-font-size: 12px,
|
|
82
|
+
meta-opacity: .75,
|
|
83
|
+
frame-width: min(96vw, 1280px),
|
|
84
|
+
frame-height: min(78vh, 860px),
|
|
85
|
+
frame-radius: 8px,
|
|
86
|
+
frame-bg: #fff,
|
|
87
|
+
),
|
|
88
|
+
office: (
|
|
89
|
+
frame-width: min(96vw, 1280px),
|
|
90
|
+
frame-height: min(74vh, 820px),
|
|
91
|
+
frame-radius: 8px,
|
|
92
|
+
frame-bg: #fff,
|
|
93
|
+
footer-gap: 12px,
|
|
94
|
+
footer-margin-top: 10px,
|
|
95
|
+
hint-font-size: 12px,
|
|
96
|
+
hint-opacity: .75,
|
|
97
|
+
btn-radius: 8px,
|
|
98
|
+
btn-padding: 6px 10px,
|
|
99
|
+
btn-font-size: 12px,
|
|
100
|
+
btn-font-weight: 600,
|
|
101
|
+
btn-bg: rgba(255, 255, 255, .12),
|
|
102
|
+
),
|
|
103
|
+
zip: (
|
|
104
|
+
content-max-height: calc(84vh - 90px),
|
|
105
|
+
content-bg: rgba(255, 255, 255, .06),
|
|
106
|
+
content-radius: 8px,
|
|
107
|
+
content-padding: 10px,
|
|
108
|
+
table-font-size: 12px,
|
|
109
|
+
table-cell-padding: 6px 8px,
|
|
110
|
+
table-border: 1px solid rgba(255, 255, 255, .12),
|
|
111
|
+
status-font-size: 13px,
|
|
112
|
+
),
|
|
113
|
+
);
|