native-document 1.0.194 → 1.0.196

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "native-document",
3
- "version": "1.0.194",
3
+ "version": "1.0.196",
4
4
  "description": "A reactive JavaScript framework that preserves native DOM simplicity without sacrificing modern features",
5
5
  "author": "AfroCodeur <https://github.com/afrocodeur>",
6
6
  "license": "MIT",
@@ -40,6 +40,7 @@ export default function FileField(name, props = {}) {
40
40
  mode: null,
41
41
  files: $.array([]),
42
42
  fileIcons: [],
43
+ maxFiles: null,
43
44
  props,
44
45
  });
45
46
  }
@@ -141,6 +142,7 @@ FileField.prototype.extensions = function(extensions, message) {
141
142
  * @returns {this}
142
143
  */
143
144
  FileField.prototype.maxFiles = function(max, message) {
145
+ this.$description.maxFiles = max;
144
146
  return this.addRule(Validation.maxFiles, [max], message);
145
147
  };
146
148
 
@@ -43,6 +43,7 @@ export default function FileImagePreviewMode(props = {}) {
43
43
  renderActions: null,
44
44
  mode: 'avatar',
45
45
  ratio: null,
46
+ previewSourceFrom: null,
46
47
  props,
47
48
  };
48
49
  }
@@ -272,4 +273,11 @@ FileImagePreviewMode.prototype.storyRatio = function() {
272
273
  FileImagePreviewMode.prototype.portraitRatio = function() {
273
274
  this.$description.ratio = '4/5';
274
275
  return this;
276
+ };
277
+
278
+ /**
279
+ * @param {Observable} observable
280
+ */
281
+ FileImagePreviewMode.prototype.previewSourceFrom = function(observable) {
282
+ this.$description.previewSourceFrom = observable;
275
283
  };
@@ -33,6 +33,7 @@ export default function FileWallMode(props = {}) {
33
33
  addIcon: null,
34
34
  renderCell: null,
35
35
  renderAdd: null,
36
+ previewItemsFrom: null,
36
37
  props,
37
38
  };
38
39
  }
@@ -103,4 +104,12 @@ FileWallMode.prototype.renderCell = function(fn) {
103
104
  FileWallMode.prototype.renderAdd = function(fn) {
104
105
  this.$description.renderAdd = fn;
105
106
  return this;
107
+ };
108
+
109
+ /**
110
+ *
111
+ * @param {Observable} observable
112
+ */
113
+ FileWallMode.prototype.previewItemsFrom = function(observable) {
114
+ this.$description.previewItemsFrom = observable;
106
115
  };
@@ -1,4 +1,4 @@
1
- import type { ValidChild } from '../../../../../types/elements';
1
+ import type {Observable, ValidChild} from '../../../../../types/elements';
2
2
  import type { GlobalAttributes } from '../../../../../types/globals';
3
3
  import type { BaseComponent } from '../../../BaseComponent';
4
4
 
@@ -36,6 +36,7 @@ export interface FileImagePreviewModeInterface extends BaseComponent {
36
36
  landscapeRatio(): this;
37
37
  storyRatio(): this;
38
38
  portraitRatio(): this;
39
+ previewSourceFrom(observable: Observable<string>): this;
39
40
 
40
41
  size(size: number | string): this;
41
42
  placeholderIcon(icon: ValidChild): this;
@@ -1,7 +1,9 @@
1
- import type { ValidChild } from '../../../../../types/elements';
1
+ import type { ValidChild} from '../../../../../types/elements';
2
2
  import type { GlobalAttributes } from '../../../../../types/globals';
3
3
  import type { BaseComponent } from '../../../BaseComponent';
4
4
  import type { FileItemPreviewInterface } from './FileItemPreview';
5
+ import {ObservableArray} from "../../../../../types/observable";
6
+ import * as url from "node:url";
5
7
 
6
8
  export type FileWallModeDescription = {
7
9
  cellSize: string | number;
@@ -19,6 +21,7 @@ export interface FileWallModeInterface extends BaseComponent {
19
21
  addIcon(icon: ValidChild): this;
20
22
  renderCell(fn: (file: File, preview: FileItemPreviewInterface) => ValidChild): this;
21
23
  renderAdd(fn: () => ValidChild): this;
24
+ previewItemsFrom(observable: ObservableArray<Record<string, any> & { file: string } >): this;
22
25
  }
23
26
 
24
27
 
@@ -5,7 +5,11 @@ import './file-image-preview-mode.css';
5
5
 
6
6
  export default function FileImagePreviewModeRender($desc, nodeInstance) {
7
7
  const {$files, fieldDesc, fieldInstance} = $desc.$context;
8
- const $preview = $(null);
8
+ const $preview = $desc.previewSourceFrom ?? $(null);
9
+
10
+ if(fieldDesc.defaultValue) {
11
+ $preview.set(fieldDesc.defaultValue);
12
+ }
9
13
 
10
14
  const input = buildInput(fieldDesc, fieldInstance, $files, $preview);
11
15
 
@@ -115,7 +119,7 @@ const buildHoverOverlay = ($desc, imagePreview, input, $files, nodeInstance) =>
115
119
  const wrapper = Div({
116
120
  class: buildShapeClass(nodeInstance, 'file-image-preview-wrapper is-hover-overlay', $desc),
117
121
  style: props.style.value()
118
- }, [imagePreview, overlay]);
122
+ }, [imagePreview, overlay]);
119
123
 
120
124
  wrapper.nd.onClick(() => input.click());
121
125
 
@@ -1,11 +1,15 @@
1
- import {Div, Input, Span, ForEachArray} from '../../../../../elements';
1
+ import {Div, Input, Span, ForEachArray, ShowIf} from '../../../../../elements';
2
2
  import {getFileThumbnail} from '../helpers';
3
+ import FileItemPreview from '../../../../components/form/field/types/file-field-mode/FileItemPreview';
4
+ import { $ } from '../../../../core/data/Observable';
3
5
 
4
6
  import './file-wall-mode.css';
5
7
 
6
8
  export default function FileWallModeRender($desc, modeInstance) {
7
9
  const {$files, fieldDesc, fieldInstance} = $desc.$context;
8
10
 
11
+ const data = $desc.previewItemsFrom ?? $files;
12
+
9
13
  const input = buildInput(fieldDesc, fieldInstance, $files);
10
14
 
11
15
  const addCell = $desc.renderCell
@@ -15,8 +19,9 @@ export default function FileWallModeRender($desc, modeInstance) {
15
19
  return Div({class: 'file-wall-wrapper'}, [
16
20
  input,
17
21
  Div({class: 'file-wall'}, [
18
- ForEachArray($files, addCell),
19
- buildAddCell($desc, input, modeInstance),
22
+ fieldDesc.defaultValue ? ForEachArray(fieldDesc.defaultValue, (file) => addCell(new FileItemPreview(file))) : null,
23
+ ForEachArray(data, addCell),
24
+ buildAddCell($desc, input, modeInstance, data),
20
25
  ]),
21
26
  ]);
22
27
  }
@@ -64,7 +69,7 @@ const buildCell = (item, $desc, modeInstance, fieldInstance) => {
64
69
  return cell;
65
70
  };
66
71
 
67
- const buildAddCell = ($desc, input, modeInstance) => {
72
+ const buildAddCell = ($desc, input, modeInstance, $previewList) => {
68
73
  let cell = null;
69
74
  const size = $desc.cellSize || 100;
70
75
 
@@ -80,6 +85,18 @@ const buildAddCell = ($desc, input, modeInstance) => {
80
85
 
81
86
  cell.nd.onClick(() => input.click());
82
87
 
88
+ if($desc.maxFiles > 0) {
89
+ let nbTotalPreview = null;
90
+ if($desc.defaultValue) {
91
+ nbTotalPreview = $.computed((defaults, list) => {
92
+ return defaults.length + list.length;
93
+ }, [$desc.defaultValue, $previewList]);
94
+ } else {
95
+ nbTotalPreview = $previewList.toLength();
96
+ }
97
+ return ShowIf(nbTotalPreview.isLessThanOrEqualTo($desc.maxFiles), cell);
98
+ }
99
+
83
100
  return cell;
84
101
  };
85
102
 
@@ -21,8 +21,8 @@ export const formatSize = (bytes) => {
21
21
 
22
22
 
23
23
  export const getFileThumbnail = (file, fieldInstance) => {
24
- const isImage = file.type?.startsWith('image/');
25
- const url = URL.createObjectURL(file);
24
+ const isImage = file.type?.startsWith('image/') || !!file.url;
25
+ const url = file.url ?? URL.createObjectURL(file);
26
26
  if(isImage) {
27
27
  return Img(url, {
28
28
  class: 'file-item-img',