@kitschpatrol/tweakpane-plugin-file-import 1.1.2-beta.2 → 1.2.0-fork.2
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/dist/tweakpane-plugin-file-import.d.ts +11 -0
- package/dist/tweakpane-plugin-file-import.js +390 -290
- package/dist/tweakpane-plugin-file-import.lite.js +247 -0
- package/dist/tweakpane-plugin-file-import.lite.min.js +2 -0
- package/dist/tweakpane-plugin-file-import.min.js +2 -1
- package/{LICENSE.txt → license.txt} +4 -0
- package/package.json +68 -60
- package/readme.md +119 -0
- package/README.md +0 -31
- package/dist/types/controller/controller.d.ts +0 -58
- package/dist/types/index.d.ts +0 -3
- package/dist/types/plugin.d.ts +0 -8
- package/dist/types/view/view.d.ts +0 -24
|
@@ -1,296 +1,396 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// ClassName('tmp') will generate a CSS class name like `tp-tmpv`
|
|
5
|
-
const containerClassName = ClassName('ctn');
|
|
6
|
-
const inputClassName = ClassName('input');
|
|
7
|
-
const deleteButtonClassName = ClassName('btn');
|
|
8
|
-
class FilePluginView {
|
|
9
|
-
constructor(doc, config) {
|
|
10
|
-
// Root
|
|
11
|
-
this.element = doc.createElement('div');
|
|
12
|
-
// Container
|
|
13
|
-
this.container = doc.createElement('div');
|
|
14
|
-
this.container.classList.add(containerClassName());
|
|
15
|
-
config.viewProps.bindClassModifiers(this.container);
|
|
16
|
-
// File input field
|
|
17
|
-
this.input = doc.createElement('input');
|
|
18
|
-
this.input.classList.add(inputClassName());
|
|
19
|
-
this.input.setAttribute('type', 'file');
|
|
20
|
-
this.input.setAttribute('accept', config.filetypes ? config.filetypes.join(',') : '*');
|
|
21
|
-
this.input.style.height = `calc(20px * ${config.lineCount})`;
|
|
22
|
-
// Icon
|
|
23
|
-
this.fileIcon = doc.createElement('div');
|
|
24
|
-
this.fileIcon.classList.add(containerClassName('icon'));
|
|
25
|
-
// Text
|
|
26
|
-
this.text = doc.createElement('span');
|
|
27
|
-
this.text.classList.add(containerClassName('text'));
|
|
28
|
-
// Warning text
|
|
29
|
-
this.warning = doc.createElement('span');
|
|
30
|
-
this.warning.classList.add(containerClassName('warning'));
|
|
31
|
-
this.warning.innerHTML = config.invalidFiletypeMessage;
|
|
32
|
-
this.warning.style.display = 'none';
|
|
33
|
-
// Delete button
|
|
34
|
-
this.deleteButton = doc.createElement('button');
|
|
35
|
-
this.deleteButton.classList.add(deleteButtonClassName('b'));
|
|
36
|
-
this.deleteButton.innerHTML = 'Delete';
|
|
37
|
-
this.deleteButton.style.display = 'none';
|
|
38
|
-
this.container.appendChild(this.input);
|
|
39
|
-
this.container.appendChild(this.fileIcon);
|
|
40
|
-
this.element.appendChild(this.container);
|
|
41
|
-
this.element.appendChild(this.warning);
|
|
42
|
-
this.element.appendChild(this.deleteButton);
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Changes the style of the container based on whether the user is dragging or not.
|
|
46
|
-
* @param state if the user is dragging or not.
|
|
47
|
-
*/
|
|
48
|
-
changeDraggingState(state) {
|
|
49
|
-
var _a, _b;
|
|
50
|
-
if (state) {
|
|
51
|
-
(_a = this.container) === null || _a === void 0 ? void 0 : _a.classList.add(containerClassName('input_area_dragging'));
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
(_b = this.container) === null || _b === void 0 ? void 0 : _b.classList.remove(containerClassName('input_area_dragging'));
|
|
55
|
-
}
|
|
56
|
-
}
|
|
1
|
+
// node_modules/.pnpm/@tweakpane+core@2.0.5/node_modules/@tweakpane/core/dist/misc/type-util.js
|
|
2
|
+
function forceCast(v) {
|
|
3
|
+
return v;
|
|
57
4
|
}
|
|
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
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
5
|
+
// node_modules/.pnpm/@tweakpane+core@2.0.5/node_modules/@tweakpane/core/dist/common/micro-parsers.js
|
|
6
|
+
function parseObject(value, keyToParserMap) {
|
|
7
|
+
return forceCast(Object.keys(keyToParserMap).reduce((tmp, key) => {
|
|
8
|
+
if (tmp === void 0) return;
|
|
9
|
+
const parser = keyToParserMap[key];
|
|
10
|
+
const result = parser(value[key]);
|
|
11
|
+
return result.succeeded ? Object.assign(Object.assign({}, tmp), { [key]: result.value }) : void 0;
|
|
12
|
+
}, {}));
|
|
13
|
+
}
|
|
14
|
+
function parseArray(value, parseItem) {
|
|
15
|
+
return value.reduce((tmp, item) => {
|
|
16
|
+
if (tmp === void 0) return;
|
|
17
|
+
const result = parseItem(item);
|
|
18
|
+
if (!result.succeeded || result.value === void 0) return;
|
|
19
|
+
return [...tmp, result.value];
|
|
20
|
+
}, []);
|
|
21
|
+
}
|
|
22
|
+
function isObject(value) {
|
|
23
|
+
if (value === null) return false;
|
|
24
|
+
return typeof value === "object";
|
|
25
|
+
}
|
|
26
|
+
function createMicroParserBuilder(parse) {
|
|
27
|
+
return (optional) => (v) => {
|
|
28
|
+
if (!optional && v === void 0) return {
|
|
29
|
+
succeeded: false,
|
|
30
|
+
value: void 0
|
|
31
|
+
};
|
|
32
|
+
if (optional && v === void 0) return {
|
|
33
|
+
succeeded: true,
|
|
34
|
+
value: void 0
|
|
35
|
+
};
|
|
36
|
+
const result = parse(v);
|
|
37
|
+
return result !== void 0 ? {
|
|
38
|
+
succeeded: true,
|
|
39
|
+
value: result
|
|
40
|
+
} : {
|
|
41
|
+
succeeded: false,
|
|
42
|
+
value: void 0
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function createMicroParserBuilders(optional) {
|
|
47
|
+
return {
|
|
48
|
+
custom: (parse) => createMicroParserBuilder(parse)(optional),
|
|
49
|
+
boolean: createMicroParserBuilder((v) => typeof v === "boolean" ? v : void 0)(optional),
|
|
50
|
+
number: createMicroParserBuilder((v) => typeof v === "number" ? v : void 0)(optional),
|
|
51
|
+
string: createMicroParserBuilder((v) => typeof v === "string" ? v : void 0)(optional),
|
|
52
|
+
function: createMicroParserBuilder((v) => typeof v === "function" ? v : void 0)(optional),
|
|
53
|
+
constant: (value) => createMicroParserBuilder((v) => v === value ? value : void 0)(optional),
|
|
54
|
+
raw: createMicroParserBuilder((v) => v)(optional),
|
|
55
|
+
object: (keyToParserMap) => createMicroParserBuilder((v) => {
|
|
56
|
+
if (!isObject(v)) return;
|
|
57
|
+
return parseObject(v, keyToParserMap);
|
|
58
|
+
})(optional),
|
|
59
|
+
array: (itemParser) => createMicroParserBuilder((v) => {
|
|
60
|
+
if (!Array.isArray(v)) return;
|
|
61
|
+
return parseArray(v, itemParser);
|
|
62
|
+
})(optional)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const MicroParsers = {
|
|
66
|
+
optional: createMicroParserBuilders(true),
|
|
67
|
+
required: createMicroParserBuilders(false)
|
|
68
|
+
};
|
|
69
|
+
function parseRecord(value, keyToParserMap) {
|
|
70
|
+
const map = keyToParserMap(MicroParsers);
|
|
71
|
+
const result = MicroParsers.required.object(map)(value);
|
|
72
|
+
return result.succeeded ? result.value : void 0;
|
|
73
|
+
}
|
|
74
|
+
// node_modules/.pnpm/@tweakpane+core@2.0.5/node_modules/@tweakpane/core/dist/common/view/class-name.js
|
|
75
|
+
const PREFIX = "tp";
|
|
76
|
+
/**
|
|
77
|
+
* A utility function for generating BEM-like class name.
|
|
78
|
+
* @param viewName The name of the view. Used as part of the block name.
|
|
79
|
+
* @return A class name generator function.
|
|
80
|
+
*/
|
|
81
|
+
function ClassName(viewName) {
|
|
82
|
+
/**
|
|
83
|
+
* Generates a class name.
|
|
84
|
+
* @param [opt_elementName] The name of the element.
|
|
85
|
+
* @param [opt_modifier] The name of the modifier.
|
|
86
|
+
* @return A class name.
|
|
87
|
+
*/
|
|
88
|
+
const fn = (opt_elementName, opt_modifier) => {
|
|
89
|
+
return [
|
|
90
|
+
PREFIX,
|
|
91
|
+
"-",
|
|
92
|
+
viewName,
|
|
93
|
+
"v",
|
|
94
|
+
opt_elementName ? `_${opt_elementName}` : "",
|
|
95
|
+
opt_modifier ? `-${opt_modifier}` : ""
|
|
96
|
+
].join("");
|
|
97
|
+
};
|
|
98
|
+
return fn;
|
|
99
|
+
}
|
|
100
|
+
// node_modules/.pnpm/@tweakpane+core@2.0.5/node_modules/@tweakpane/core/dist/misc/semver.js
|
|
101
|
+
/***
|
|
102
|
+
* A simple semantic versioning perser.
|
|
103
|
+
*/
|
|
104
|
+
var Semver = class {
|
|
105
|
+
/**
|
|
106
|
+
* @hidden
|
|
107
|
+
*/
|
|
108
|
+
constructor(text) {
|
|
109
|
+
const [core, prerelease] = text.split("-");
|
|
110
|
+
const coreComps = core.split(".");
|
|
111
|
+
this.major = parseInt(coreComps[0], 10);
|
|
112
|
+
this.minor = parseInt(coreComps[1], 10);
|
|
113
|
+
this.patch = parseInt(coreComps[2], 10);
|
|
114
|
+
this.prerelease = prerelease !== null && prerelease !== void 0 ? prerelease : null;
|
|
115
|
+
}
|
|
116
|
+
toString() {
|
|
117
|
+
const core = [
|
|
118
|
+
this.major,
|
|
119
|
+
this.minor,
|
|
120
|
+
this.patch
|
|
121
|
+
].join(".");
|
|
122
|
+
return this.prerelease !== null ? [core, this.prerelease].join("-") : core;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
// node_modules/.pnpm/@tweakpane+core@2.0.5/node_modules/@tweakpane/core/dist/version.js
|
|
126
|
+
const VERSION = new Semver("2.0.5");
|
|
127
|
+
// node_modules/.pnpm/@tweakpane+core@2.0.5/node_modules/@tweakpane/core/dist/plugin/plugin.js
|
|
128
|
+
/**
|
|
129
|
+
* Creates a plugin with the current core.
|
|
130
|
+
* @param plugin The plugin without the core version.
|
|
131
|
+
* @return A plugin with the core version.
|
|
132
|
+
*/
|
|
133
|
+
function createPlugin(plugin) {
|
|
134
|
+
return Object.assign({ core: VERSION }, plugin);
|
|
135
|
+
}
|
|
136
|
+
// node_modules/.pnpm/@tweakpane+core@2.0.5/node_modules/@tweakpane/core/dist/common/constraint/composite.js
|
|
137
|
+
/**
|
|
138
|
+
* A constraint to combine multiple constraints.
|
|
139
|
+
* @template T The type of the value.
|
|
140
|
+
*/
|
|
141
|
+
var CompositeConstraint = class {
|
|
142
|
+
constructor(constraints) {
|
|
143
|
+
this.constraints = constraints;
|
|
144
|
+
}
|
|
145
|
+
constrain(value) {
|
|
146
|
+
return this.constraints.reduce((result, c) => {
|
|
147
|
+
return c.constrain(result);
|
|
148
|
+
}, value);
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
// src/view/view.ts
|
|
152
|
+
const containerClassName = ClassName("ctn");
|
|
153
|
+
const inputClassName = ClassName("input");
|
|
154
|
+
const buttonClassName = ClassName("btn");
|
|
155
|
+
var FilePluginView = class {
|
|
156
|
+
clearButton;
|
|
157
|
+
container;
|
|
158
|
+
element;
|
|
159
|
+
fileIcon;
|
|
160
|
+
input;
|
|
161
|
+
text;
|
|
162
|
+
warning;
|
|
163
|
+
constructor(doc, config) {
|
|
164
|
+
this.element = doc.createElement("div");
|
|
165
|
+
this.container = doc.createElement("div");
|
|
166
|
+
this.container.classList.add(containerClassName());
|
|
167
|
+
config.viewProps.bindClassModifiers(this.container);
|
|
168
|
+
this.input = doc.createElement("input");
|
|
169
|
+
this.input.classList.add(inputClassName());
|
|
170
|
+
this.input.setAttribute("type", "file");
|
|
171
|
+
this.input.setAttribute("accept", config.filetypes ? config.filetypes.join(",") : "*");
|
|
172
|
+
this.input.style.height = `calc(20px * ${config.lineCount})`;
|
|
173
|
+
this.fileIcon = doc.createElement("div");
|
|
174
|
+
this.fileIcon.classList.add(containerClassName("icon"));
|
|
175
|
+
this.text = doc.createElement("span");
|
|
176
|
+
this.text.classList.add(containerClassName("text"));
|
|
177
|
+
this.warning = doc.createElement("span");
|
|
178
|
+
this.warning.classList.add(containerClassName("warning"));
|
|
179
|
+
this.warning.textContent = config.invalidFiletypeMessage;
|
|
180
|
+
this.warning.style.display = "none";
|
|
181
|
+
this.clearButton = doc.createElement("button");
|
|
182
|
+
this.clearButton.classList.add(buttonClassName("b"));
|
|
183
|
+
this.clearButton.textContent = "Delete";
|
|
184
|
+
this.clearButton.style.display = "none";
|
|
185
|
+
this.container.append(this.input);
|
|
186
|
+
this.container.append(this.fileIcon);
|
|
187
|
+
this.element.append(this.container);
|
|
188
|
+
this.element.append(this.warning);
|
|
189
|
+
this.element.append(this.clearButton);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Changes the style of the container based on whether the user is dragging or
|
|
193
|
+
* not.
|
|
194
|
+
*
|
|
195
|
+
* @param state If the user is dragging or not.
|
|
196
|
+
*/
|
|
197
|
+
changeDraggingState(state) {
|
|
198
|
+
if (state) this.container.classList.add(containerClassName("input_area_dragging"));
|
|
199
|
+
else this.container.classList.remove(containerClassName("input_area_dragging"));
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
// src/controller/controller.ts
|
|
203
|
+
var FilePluginController = class {
|
|
204
|
+
value;
|
|
205
|
+
view;
|
|
206
|
+
viewProps;
|
|
207
|
+
config;
|
|
208
|
+
constructor(doc, config) {
|
|
209
|
+
this.value = config.value;
|
|
210
|
+
this.viewProps = config.viewProps;
|
|
211
|
+
this.view = new FilePluginView(doc, {
|
|
212
|
+
filetypes: config.filetypes,
|
|
213
|
+
invalidFiletypeMessage: config.invalidFiletypeMessage,
|
|
214
|
+
lineCount: config.lineCount,
|
|
215
|
+
value: config.value,
|
|
216
|
+
viewProps: this.viewProps
|
|
217
|
+
});
|
|
218
|
+
this.config = config;
|
|
219
|
+
this.onFile = this.onFile.bind(this);
|
|
220
|
+
this.onDrop = this.onDrop.bind(this);
|
|
221
|
+
this.onDragOver = this.onDragOver.bind(this);
|
|
222
|
+
this.onDragLeave = this.onDragLeave.bind(this);
|
|
223
|
+
this.onDeleteClick = this.onDeleteClick.bind(this);
|
|
224
|
+
this.view.input.addEventListener("change", this.onFile);
|
|
225
|
+
this.view.element.addEventListener("drop", this.onDrop);
|
|
226
|
+
this.view.element.addEventListener("dragover", this.onDragOver);
|
|
227
|
+
this.view.element.addEventListener("dragleave", this.onDragLeave);
|
|
228
|
+
this.view.clearButton.addEventListener("click", this.onDeleteClick);
|
|
229
|
+
this.value.emitter.on("change", () => {
|
|
230
|
+
this.handleValueChange();
|
|
231
|
+
});
|
|
232
|
+
this.viewProps.handleDispose(() => {
|
|
233
|
+
this.view.input.removeEventListener("change", this.onFile);
|
|
234
|
+
this.view.element.removeEventListener("drop", this.onDrop);
|
|
235
|
+
this.view.element.removeEventListener("dragover", this.onDragOver);
|
|
236
|
+
this.view.element.removeEventListener("dragleave", this.onDragLeave);
|
|
237
|
+
this.view.clearButton.removeEventListener("click", this.onDeleteClick);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Called when the value (bound to the controller) changes (e.g. when the file
|
|
242
|
+
* is selected).
|
|
243
|
+
*/
|
|
244
|
+
handleValueChange() {
|
|
245
|
+
const fileObject = this.value.rawValue;
|
|
246
|
+
const containerElement = this.view.container;
|
|
247
|
+
const textElement = this.view.text;
|
|
248
|
+
const fileIconElement = this.view.fileIcon;
|
|
249
|
+
const { clearButton } = this.view;
|
|
250
|
+
if (fileObject) {
|
|
251
|
+
textElement.textContent = fileObject.name;
|
|
252
|
+
containerElement.append(textElement);
|
|
253
|
+
if (containerElement.contains(fileIconElement)) fileIconElement.remove();
|
|
254
|
+
this.view.warning.style.display = "none";
|
|
255
|
+
clearButton.style.display = "block";
|
|
256
|
+
containerElement.style.border = "unset";
|
|
257
|
+
} else {
|
|
258
|
+
textElement.textContent = "";
|
|
259
|
+
containerElement.append(fileIconElement);
|
|
260
|
+
textElement.remove();
|
|
261
|
+
this.view.warning.style.display = "none";
|
|
262
|
+
clearButton.style.display = "none";
|
|
263
|
+
containerElement.style.border = "1px dashed #717070";
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Checks if the file is valid with the given filetypes.
|
|
268
|
+
*
|
|
269
|
+
* @param file File object
|
|
270
|
+
*
|
|
271
|
+
* @returns True if the file is valid.
|
|
272
|
+
*/
|
|
273
|
+
isFileValid(file) {
|
|
274
|
+
const { filetypes } = this.config;
|
|
275
|
+
const fileExtension = "." + (file.name.split(".").pop()?.toLowerCase() ?? "");
|
|
276
|
+
return !filetypes || filetypes.length === 0 || filetypes.includes(fileExtension);
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Event handler when the delete HTML button is clicked. It resets the
|
|
280
|
+
* `rawValue` of the controller.
|
|
281
|
+
*/
|
|
282
|
+
onDeleteClick() {
|
|
283
|
+
if (!this.value.rawValue) return;
|
|
284
|
+
this.value.setRawValue(null);
|
|
285
|
+
this.view.input.value = "";
|
|
286
|
+
this.view.warning.style.display = "none";
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Called when the user leaves the container while dragging. Updates the style
|
|
290
|
+
* of the container.
|
|
291
|
+
*/
|
|
292
|
+
onDragLeave() {
|
|
293
|
+
this.view.changeDraggingState(false);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Called when the user drags over a file. Updates the style of the container.
|
|
297
|
+
*
|
|
298
|
+
* @param event Drag event.
|
|
299
|
+
*/
|
|
300
|
+
onDragOver(event) {
|
|
301
|
+
event.preventDefault();
|
|
302
|
+
this.view.changeDraggingState(true);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Called when the user drops a file in the container. Either shows a warning
|
|
306
|
+
* if it's invalid or updates the value if it's valid.
|
|
307
|
+
*
|
|
308
|
+
* @param event Drag event.
|
|
309
|
+
*/
|
|
310
|
+
onDrop(event) {
|
|
311
|
+
try {
|
|
312
|
+
if (!(event instanceof DragEvent)) return;
|
|
313
|
+
event.preventDefault();
|
|
314
|
+
const files = event.dataTransfer?.files;
|
|
315
|
+
if (files?.length !== 1) return;
|
|
316
|
+
const file = files.item(0);
|
|
317
|
+
if (!file) return;
|
|
318
|
+
if (this.isFileValid(file)) this.value.setRawValue(file);
|
|
319
|
+
else this.showWarning();
|
|
320
|
+
} finally {
|
|
321
|
+
this.view.changeDraggingState(false);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Called when the value of the input changes.
|
|
326
|
+
*
|
|
327
|
+
* @param _event Change event.
|
|
328
|
+
*/
|
|
329
|
+
onFile(_event) {
|
|
330
|
+
const file = this.view.input.files?.item(0);
|
|
331
|
+
if (!file) return;
|
|
332
|
+
if (this.isFileValid(file)) this.value.setRawValue(file);
|
|
333
|
+
else this.showWarning();
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Shows warning text for 5 seconds.
|
|
337
|
+
*/
|
|
338
|
+
showWarning() {
|
|
339
|
+
this.view.warning.style.display = "block";
|
|
340
|
+
setTimeout(() => {
|
|
341
|
+
this.view.warning.style.display = "none";
|
|
342
|
+
}, 5e3);
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
// src/plugin.ts
|
|
346
|
+
function readFile(exValue) {
|
|
347
|
+
return exValue instanceof File ? exValue : null;
|
|
348
|
+
}
|
|
349
|
+
function writeFile(target, inValue) {
|
|
350
|
+
target.write(inValue);
|
|
230
351
|
}
|
|
231
|
-
|
|
232
352
|
const TweakpaneFileInputPlugin = createPlugin({
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
return (target, inValue) => {
|
|
270
|
-
// Use `target.write()` to write the primitive value to the target,
|
|
271
|
-
// or `target.writeProperty()` to write a property of the target
|
|
272
|
-
target.write(inValue);
|
|
273
|
-
};
|
|
274
|
-
},
|
|
275
|
-
},
|
|
276
|
-
controller(args) {
|
|
277
|
-
var _a, _b;
|
|
278
|
-
const defaultNumberOfLines = 3;
|
|
279
|
-
const defaultFiletypeWarningText = 'Unaccepted file type.';
|
|
280
|
-
// Create a controller for the plugin
|
|
281
|
-
return new FilePluginController(args.document, {
|
|
282
|
-
value: args.value,
|
|
283
|
-
viewProps: args.viewProps,
|
|
284
|
-
invalidFiletypeMessage: (_a = args.params.invalidFiletypeMessage) !== null && _a !== void 0 ? _a : defaultFiletypeWarningText,
|
|
285
|
-
lineCount: (_b = args.params.lineCount) !== null && _b !== void 0 ? _b : defaultNumberOfLines,
|
|
286
|
-
filetypes: args.params.filetypes,
|
|
287
|
-
});
|
|
288
|
-
},
|
|
353
|
+
accept(exValue, params) {
|
|
354
|
+
if (typeof exValue !== "string") return null;
|
|
355
|
+
const result = parseRecord(params, (p) => ({
|
|
356
|
+
filetypes: p.optional.array(p.required.string),
|
|
357
|
+
invalidFiletypeMessage: p.optional.string,
|
|
358
|
+
lineCount: p.optional.number,
|
|
359
|
+
view: p.required.constant("file-input")
|
|
360
|
+
}));
|
|
361
|
+
if (!result) return null;
|
|
362
|
+
return {
|
|
363
|
+
initialValue: exValue,
|
|
364
|
+
params: result
|
|
365
|
+
};
|
|
366
|
+
},
|
|
367
|
+
binding: {
|
|
368
|
+
constraint(_args) {
|
|
369
|
+
return new CompositeConstraint([]);
|
|
370
|
+
},
|
|
371
|
+
reader(_args) {
|
|
372
|
+
return readFile;
|
|
373
|
+
},
|
|
374
|
+
writer(_args) {
|
|
375
|
+
return writeFile;
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
controller(args) {
|
|
379
|
+
return new FilePluginController(args.document, {
|
|
380
|
+
filetypes: args.params.filetypes,
|
|
381
|
+
invalidFiletypeMessage: args.params.invalidFiletypeMessage ?? "Unaccepted file type.",
|
|
382
|
+
lineCount: args.params.lineCount ?? 3,
|
|
383
|
+
value: args.value,
|
|
384
|
+
viewProps: args.viewProps
|
|
385
|
+
});
|
|
386
|
+
},
|
|
387
|
+
id: "file-input",
|
|
388
|
+
type: "input"
|
|
289
389
|
});
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
const
|
|
390
|
+
// src/styles/plugin.scss?inline
|
|
391
|
+
var plugin_default = ".tp-ctnv{appearance:none;font-family:inherit;font-size:inherit;font-weight:inherit;background-color:#0000;background-color:var(--in-bg);border-radius:var(--bld-br);box-sizing:border-box;color:var(--in-fg);height:var(--cnt-usz);font-family:inherit;line-height:var(--cnt-usz);border-width:0;outline:none;width:100%;min-width:0;margin:0;padding:0}.tp-ctnv:hover{background-color:var(--in-bg-h)}.tp-ctnv:focus{background-color:var(--in-bg-f)}.tp-ctnv:active{background-color:var(--in-bg-a)}.tp-ctnv:disabled{opacity:.5}.tp-ctnv{cursor:pointer;border:1px dashed #717070;border-radius:5px;flex-wrap:nowrap;justify-content:center;align-items:center;width:100%;height:100%;display:flex;position:relative;overflow:hidden}.tp-ctnv.tp-v-disabled{opacity:.5}.tp-ctnv_input_area_dragging{background-color:#5858b93b;border:1px dashed #6774ff}.tp-ctnv_warning{width:100%;height:max-content;margin-top:var(--cnt-vp);color:var(--in-fg);text-align:right;overflow-wrap:break-word;white-space:normal;opacity:.5;font-size:.9em;line-height:1.5;display:inline-block;bottom:2px}.tp-ctnv_text{width:max-content;max-width:100%;height:max-content;max-height:100%;color:var(--in-fg);text-align:right;overflow-wrap:break-word;white-space:normal;opacity:.5;margin:.2rem;font-size:.9em;line-height:.9;display:inline-block;position:absolute;bottom:2px;right:2px}.tp-ctnv_frac{background-color:var(--in-fg);border-radius:1px;height:2px;margin-top:-1px;position:absolute;top:50%;left:50%}.tp-ctnv_icon{transform:scale(var(--ggs,1));box-sizing:border-box;opacity:.5;border:2px solid;border-top:0;border-bottom-right-radius:2px;border-bottom-left-radius:2px;width:16px;height:6px;margin-top:8px;display:block;position:absolute}.tp-ctnv_icon:after{content:\"\";box-sizing:border-box;border-top:2px solid;border-left:2px solid;width:8px;height:8px;display:block;position:absolute;bottom:4px;left:2px;transform:rotate(45deg)}.tp-ctnv_icon:before{content:\"\";box-sizing:border-box;background:currentColor;border-radius:3px;width:2px;height:10px;display:block;position:absolute;bottom:3px;left:5px}.tp-btnv_b{margin-top:10px}.tp-inputv{opacity:0}\n";
|
|
392
|
+
// src/index.ts
|
|
393
|
+
const id = "file-input";
|
|
394
|
+
const css = plugin_default;
|
|
294
395
|
const plugins = [TweakpaneFileInputPlugin];
|
|
295
|
-
|
|
296
396
|
export { css, id, plugins };
|