Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tweakpane-plugin-file-import

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tweakpane-plugin-file-import - npm Package Compare versions

Comparing version 0.1.6 to 0.1.7

482

dist/tweakpane-plugin-file-import.js

@@ -11,36 +11,2 @@ (function (global, factory) {

class Emitter {
constructor() {
this.observers_ = {};
}
on(eventName, handler) {
let observers = this.observers_[eventName];
if (!observers) {
observers = this.observers_[eventName] = [];
}
observers.push({
handler: handler,
});
return this;
}
off(eventName, handler) {
const observers = this.observers_[eventName];
if (observers) {
this.observers_[eventName] = observers.filter((observer) => {
return observer.handler !== handler;
});
}
return this;
}
emit(eventName, event) {
const observers = this.observers_[eventName];
if (!observers) {
return;
}
observers.forEach((observer) => {
observer.handler(event);
});
}
}
const PREFIX = 'tp';

@@ -173,130 +139,2 @@ function ClassName(viewName) {

function computeOffset(ev, elem) {
var _a, _b;
const win = elem.ownerDocument.defaultView;
const rect = elem.getBoundingClientRect();
return {
x: ev.pageX - (((_a = (win && win.scrollX)) !== null && _a !== void 0 ? _a : 0) + rect.left),
y: ev.pageY - (((_b = (win && win.scrollY)) !== null && _b !== void 0 ? _b : 0) + rect.top),
};
}
class PointerHandler {
constructor(element) {
this.lastTouch_ = null;
this.onDocumentMouseMove_ = this.onDocumentMouseMove_.bind(this);
this.onDocumentMouseUp_ = this.onDocumentMouseUp_.bind(this);
this.onMouseDown_ = this.onMouseDown_.bind(this);
this.onTouchEnd_ = this.onTouchEnd_.bind(this);
this.onTouchMove_ = this.onTouchMove_.bind(this);
this.onTouchStart_ = this.onTouchStart_.bind(this);
this.elem_ = element;
this.emitter = new Emitter();
element.addEventListener('touchstart', this.onTouchStart_, {
passive: false,
});
element.addEventListener('touchmove', this.onTouchMove_, {
passive: true,
});
element.addEventListener('touchend', this.onTouchEnd_);
element.addEventListener('mousedown', this.onMouseDown_);
}
computePosition_(offset) {
const rect = this.elem_.getBoundingClientRect();
return {
bounds: {
width: rect.width,
height: rect.height,
},
point: offset
? {
x: offset.x,
y: offset.y,
}
: null,
};
}
onMouseDown_(ev) {
var _a;
ev.preventDefault();
(_a = ev.currentTarget) === null || _a === void 0 ? void 0 : _a.focus();
const doc = this.elem_.ownerDocument;
doc.addEventListener('mousemove', this.onDocumentMouseMove_);
doc.addEventListener('mouseup', this.onDocumentMouseUp_);
this.emitter.emit('down', {
altKey: ev.altKey,
data: this.computePosition_(computeOffset(ev, this.elem_)),
sender: this,
shiftKey: ev.shiftKey,
});
}
onDocumentMouseMove_(ev) {
this.emitter.emit('move', {
altKey: ev.altKey,
data: this.computePosition_(computeOffset(ev, this.elem_)),
sender: this,
shiftKey: ev.shiftKey,
});
}
onDocumentMouseUp_(ev) {
const doc = this.elem_.ownerDocument;
doc.removeEventListener('mousemove', this.onDocumentMouseMove_);
doc.removeEventListener('mouseup', this.onDocumentMouseUp_);
this.emitter.emit('up', {
altKey: ev.altKey,
data: this.computePosition_(computeOffset(ev, this.elem_)),
sender: this,
shiftKey: ev.shiftKey,
});
}
onTouchStart_(ev) {
ev.preventDefault();
const touch = ev.targetTouches.item(0);
const rect = this.elem_.getBoundingClientRect();
this.emitter.emit('down', {
altKey: ev.altKey,
data: this.computePosition_(touch
? {
x: touch.clientX - rect.left,
y: touch.clientY - rect.top,
}
: undefined),
sender: this,
shiftKey: ev.shiftKey,
});
this.lastTouch_ = touch;
}
onTouchMove_(ev) {
const touch = ev.targetTouches.item(0);
const rect = this.elem_.getBoundingClientRect();
this.emitter.emit('move', {
altKey: ev.altKey,
data: this.computePosition_(touch
? {
x: touch.clientX - rect.left,
y: touch.clientY - rect.top,
}
: undefined),
sender: this,
shiftKey: ev.shiftKey,
});
this.lastTouch_ = touch;
}
onTouchEnd_(ev) {
var _a;
const touch = (_a = ev.targetTouches.item(0)) !== null && _a !== void 0 ? _a : this.lastTouch_;
const rect = this.elem_.getBoundingClientRect();
this.emitter.emit('up', {
altKey: ev.altKey,
data: this.computePosition_(touch
? {
x: touch.clientX - rect.left,
y: touch.clientY - rect.top,
}
: undefined),
sender: this,
shiftKey: ev.shiftKey,
});
}
}
function constrainRange(value, min, max) {

@@ -477,55 +315,168 @@ return Math.min(Math.max(value, min), max);

const containerClassName = ClassName('ctn');
const buttonClassName = ClassName('btn');
const inputClassName = ClassName('input');
const deleteButtonClassName = ClassName('btn');
class FilePluginView {
constructor(doc, config) {
// Binding event handlers
this.onDrop_ = this.onDrop_.bind(this);
// DOM --------------------------
// Root
this.element = doc.createElement('div');
// Create container and children
// Container
this.container = doc.createElement('div');
this.container.style.height = `calc(var(--bld-us) * ${config.lineCount})`;
this.container.classList.add(containerClassName());
config.viewProps.bindClassModifiers(this.container);
// File input field
this.input = doc.createElement('input');
this.input.classList.add(inputClassName());
this.input.setAttribute('type', 'file');
this.input.setAttribute('accept', config.filetypes ? config.filetypes.join(',') : '*');
this.input.style.height = `calc(20px * ${config.lineCount})`;
// Icon
this.fileIcon = doc.createElement('div');
this.fileIcon.classList.add(containerClassName('icon'));
// Text
this.text = doc.createElement('span');
this.text.classList.add(containerClassName('text'));
// Warning text
this.warning = doc.createElement('span');
this.warning.classList.add(containerClassName('warning'));
this.warning.style.display = 'none';
// Delete button
this.deleteButton = doc.createElement('button');
this.deleteButton.classList.add(deleteButtonClassName('b'));
this.deleteButton.innerHTML = 'Delete';
this.deleteButton.style.display = 'none';
this.container.appendChild(this.input);
this.container.appendChild(this.fileIcon);
this.element.appendChild(this.container);
this.fileIconEl_ = doc.createElement('div');
this.fileIconEl_.classList.add(containerClassName('icon'));
this.container.appendChild(this.fileIconEl_);
this.textEl_ = doc.createElement('span');
this.textEl_.classList.add(containerClassName('text'));
// Create button
this.button = doc.createElement('button');
this.button.classList.add(buttonClassName('b'));
this.button.innerHTML = 'Delete';
this.button.style.display = 'none';
this.element.appendChild(this.button);
// Add drag and drop event handlers
this.element.addEventListener('drop', this.onDrop_);
this.element.addEventListener('dragover', this.onDragOver_);
// Events ------------------------
// Receive the bound value from the controller
this.value_ = config.value;
// Handle 'change' event of the value
this.value_.emitter.on('change', this.onValueChange_.bind(this));
// Bind view props to the element
config.viewProps.bindClassModifiers(this.element);
// View dispose handler
config.viewProps.handleDispose(() => {
// Called when the view is disposing
console.log('TODO: dispose view');
this.element.appendChild(this.warning);
this.element.appendChild(this.deleteButton);
}
/**
* Changes the style of the container based on whether the user is dragging or not.
* @param state if the user is dragging or not.
*/
changeDraggingState(state) {
var _a, _b;
if (state) {
(_a = this.container) === null || _a === void 0 ? void 0 : _a.classList.add(containerClassName('input_area_dragging'));
}
else {
(_b = this.container) === null || _b === void 0 ? void 0 : _b.classList.remove(containerClassName('input_area_dragging'));
}
}
}
class FilePluginController {
constructor(doc, config) {
this.value = config.value;
this.viewProps = config.viewProps;
this.view = new FilePluginView(doc, {
viewProps: this.viewProps,
value: config.value,
lineCount: config.lineCount,
filetypes: config.filetypes,
});
this.config = config;
// Bind event handlers
this.onFile = this.onFile.bind(this);
this.onDrop = this.onDrop.bind(this);
this.onDragOver = this.onDragOver.bind(this);
this.onDragLeave = this.onDragLeave.bind(this);
this.onDeleteClick = this.onDeleteClick.bind(this);
this.view.input.addEventListener('change', this.onFile);
this.view.element.addEventListener('drop', this.onDrop);
this.view.element.addEventListener('dragover', this.onDragOver);
this.view.element.addEventListener('dragleave', this.onDragLeave);
this.view.deleteButton.addEventListener('click', this.onDeleteClick);
this.value.emitter.on('change', () => this.handleValueChange());
// Dispose event handlers
this.viewProps.handleDispose(() => {
this.view.input.removeEventListener('change', this.onFile);
this.view.element.removeEventListener('drop', this.onDrop);
this.view.element.removeEventListener('dragover', this.onDragOver);
this.view.element.removeEventListener('dragleave', this.onDragLeave);
this.view.deleteButton.removeEventListener('click', this.onDeleteClick);
});
}
/**
* Called when a `dragover` event is created.
* It simply prevents files being opened when these events are occuring.
* @param ev Drag event object.
* Called when the value of the input changes.
* @param event change event.
*/
onDragOver_(ev) {
ev.preventDefault();
onFile(_event) {
const input = this.view.input;
// Check if user has chosen a file.
// If it's valid, we update the value. Otherwise, show warning.
if (input.files && input.files.length > 0) {
const file = input.files[0];
if (!this.isFileValid(file)) {
this.showWarning();
}
else {
this.value.setRawValue(file);
}
}
}
/**
* Called whenever a `drop` event is created.
* It changes the `rawValue` of the controller with the file that was dropped.
* @param ev Event object.
* Shows warning text for 5 seconds.
*/
onDrop_(ev) {
showWarning() {
this.view.warning.style.display = 'block';
this.view.warning.innerHTML = 'Unaccepted file type.';
setTimeout(() => {
// Resetting warning text
this.view.warning.innerHTML = '';
this.view.warning.style.display = 'none';
}, 5000);
}
/**
* Checks if the file is valid with the given filetypes.
* @param file File object
* @returns true if the file is valid.
*/
isFileValid(file) {
var _a;
const filetypes = this.config.filetypes;
const fileExtension = '.' + ((_a = file.name.split('.').pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase());
return !(filetypes &&
filetypes.length > 0 &&
!filetypes.includes(fileExtension) &&
fileExtension);
}
/**
* Event handler when the delete HTML button is clicked.
* It resets the `rawValue` of the controller.
*/
onDeleteClick() {
const file = this.value.rawValue;
if (file) {
// Resetting the value
this.value.setRawValue(null);
// Resetting the input
this.view.input.value = '';
// Resetting the warning text
this.view.warning.innerHTML = '';
this.view.warning.style.display = 'none';
}
}
/**
* Called when the user drags over a file.
* Updates the style of the container.
* @param event drag event.
*/
onDragOver(event) {
event.preventDefault();
this.view.changeDraggingState(true);
}
/**
* Called when the user leaves the container while dragging.
* Updates the style of the container.
*/
onDragLeave() {
this.view.changeDraggingState(false);
}
/**
* Called when the user drops a file in the container.
* Either shows a warning if it's invalid or updates the value if it's valid.
* @param ev drag event.
*/
onDrop(ev) {
if (ev instanceof DragEvent) {

@@ -540,3 +491,10 @@ // Prevent default behavior (Prevent file from being opened)

const file = filesArray.item(0);
this.value_.setRawValue(file);
if (file) {
if (!this.isFileValid(file)) {
this.showWarning();
}
else {
this.value.setRawValue(file);
}
}
}

@@ -546,2 +504,3 @@ }

}
this.view.changeDraggingState(false);
}

@@ -551,24 +510,35 @@ /**

*/
onValueChange_() {
const fileObj = this.value_.rawValue;
handleValueChange() {
const fileObj = this.value.rawValue;
const containerEl = this.view.container;
const textEl = this.view.text;
const fileIconEl = this.view.fileIcon;
const deleteButton = this.view.deleteButton;
if (fileObj) {
// Setting the text of the file to the element
this.textEl_.textContent = fileObj.name;
textEl.textContent = fileObj.name;
// Removing icon and adding text
this.container.appendChild(this.textEl_);
if (this.container.contains(this.fileIconEl_)) {
this.container.removeChild(this.fileIconEl_);
containerEl.appendChild(textEl);
if (containerEl.contains(fileIconEl)) {
containerEl.removeChild(fileIconEl);
}
// Resetting warning text
this.view.warning.innerHTML = '';
this.view.warning.style.display = 'none';
// Adding button to delete
this.button.style.display = 'block';
this.container.style.border = 'unset';
deleteButton.style.display = 'block';
containerEl.style.border = 'unset';
}
else {
// Setting the text of the file to the element
this.textEl_.textContent = '';
textEl.textContent = '';
// Removing text and adding icon
this.container.appendChild(this.fileIconEl_);
this.container.removeChild(this.textEl_);
this.button.style.display = 'none';
this.container.style.border = '1px dashed #717070';
containerEl.appendChild(fileIconEl);
containerEl.removeChild(textEl);
// Resetting warning text
this.view.warning.innerHTML = '';
this.view.warning.style.display = 'none';
// Hiding button and resetting border
deleteButton.style.display = 'none';
containerEl.style.border = '1px dashed #717070';
}

@@ -578,80 +548,2 @@ }

class FilePluginController {
constructor(doc, config) {
// Binding click event handlers
this.onContainerClick_ = this.onContainerClick_.bind(this);
this.onButtonClick_ = this.onButtonClick_.bind(this);
// Receive the bound value from the plugin
this.value = config.value;
// Get filetypes
this.filetypes = config.filetypes;
// and also view props
this.viewProps = config.viewProps;
this.viewProps.handleDispose(() => {
// Called when the controller is disposing
});
// Create a custom view
this.view = new FilePluginView(doc, {
value: this.value,
viewProps: this.viewProps,
lineCount: config.lineCount,
});
// You can use `PointerHandler` to handle pointer events in the same way as Tweakpane do
const containerPtHandler = new PointerHandler(this.view.container);
containerPtHandler.emitter.on('down', this.onContainerClick_);
const buttonPtHandler = new PointerHandler(this.view.button);
buttonPtHandler.emitter.on('down', this.onButtonClick_);
}
/**
* Event handler when the container HTML element is clicked.
* It checks if the filetype is valid and if the user has chosen a file.
* If the file is valid, the `rawValue` of the controller is set.
* @param ev Pointer event.
*/
onContainerClick_(_ev) {
// Accepted filetypes
const filetypes = this.filetypes;
// Creates hidden `input` and mimicks click to open file explorer
const input = document.createElement('input');
input.setAttribute('type', 'file');
input.style.opacity = '0';
input.style.position = 'fixed';
input.style.pointerEvents = 'none';
document.body.appendChild(input);
// Adds event listener when user chooses file
input.addEventListener('input', (_ev) => {
var _a;
// Check if user has chosen a file
if (input.files && input.files.length > 0) {
const file = input.files[0];
const fileExtension = '.' + ((_a = file.name.split('.').pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase());
// Check if filetype is allowed
if (filetypes &&
filetypes.length > 0 &&
!filetypes.includes(fileExtension) &&
fileExtension) {
return;
}
else {
this.value.setRawValue(file);
}
}
}, { once: true });
// Click hidden input to open file explorer and remove it
input.click();
document.body.removeChild(input);
}
/**
* Event handler when the delete HTML button is clicked.
* It resets the `rawValue` of the controller.
* @param ev Pointer event.
*/
onButtonClick_(_ev) {
const file = this.value.rawValue;
if (file) {
this.value.setRawValue(null);
}
}
}
const TweakpaneFileInputPlugin = {

@@ -663,3 +555,3 @@ id: 'file-input',

// See rollup.config.js for details
css: '.tp-ctnv{-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:rgba(0,0,0,0);border-width:0;font-family:inherit;font-size:inherit;font-weight:inherit;margin:0;outline:none;padding:0}.tp-ctnv{background-color:var(--in-bg);border-radius:var(--elm-br);box-sizing:border-box;color:var(--in-fg);font-family:inherit;height:var(--bld-us);line-height:var(--bld-us);min-width:0;width:100%}.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;display:flex;justify-content:center;align-items:center;overflow:hidden;position:relative;border:1px dashed #717070;border-radius:5px}.tp-ctnv.tp-v-disabled{opacity:.5}.tp-ctnv_text{color:var(--in-fg);bottom:2px;display:inline-block;font-size:.9em;height:-moz-max-content;height:max-content;line-height:.9;margin:.2rem;max-height:100%;max-width:100%;opacity:.5;position:absolute;right:2px;text-align:right;white-space:normal;width:-moz-max-content;width:max-content;word-wrap:break-word}.tp-ctnv_frac{background-color:var(--in-fg);border-radius:1px;height:2px;left:50%;margin-top:-1px;position:absolute;top:50%}.tp-ctnv_icon{box-sizing:border-box;position:relative;display:block;transform:scale(var(--ggs, 1));width:16px;height:6px;border:2px solid;border-top:0;border-bottom-left-radius:2px;border-bottom-right-radius:2px;margin-top:8px;opacity:.5}.tp-ctnv_icon::after{content:"";display:block;box-sizing:border-box;position:absolute;width:8px;height:8px;border-left:2px solid;border-top:2px solid;transform:rotate(45deg);left:2px;bottom:4px}.tp-ctnv_icon::before{content:"";display:block;box-sizing:border-box;position:absolute;border-radius:3px;width:2px;height:10px;background:currentColor;left:5px;bottom:3px}.tp-btnv_b{margin-top:10px}',
css: '.tp-ctnv{-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:rgba(0,0,0,0);border-width:0;font-family:inherit;font-size:inherit;font-weight:inherit;margin:0;outline:none;padding:0}.tp-ctnv{background-color:var(--in-bg);border-radius:var(--elm-br);box-sizing:border-box;color:var(--in-fg);font-family:inherit;height:var(--bld-us);line-height:var(--bld-us);min-width:0;width:100%}.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;display:flex;justify-content:center;align-items:center;overflow:hidden;position:relative;height:100%;width:100%;border:1px dashed #717070;border-radius:5px}.tp-ctnv.tp-v-disabled{opacity:.5}.tp-ctnv_input_area_dragging{border:1px dashed #6774ff;background-color:rgba(88,88,185,.231372549)}.tp-ctnv_warning{color:var(--in-fg);bottom:2px;display:inline-block;font-size:.9em;height:-moz-max-content;height:max-content;line-height:.9;margin:.2rem;opacity:.5;white-space:normal;width:-moz-max-content;width:max-content;word-wrap:break-word;text-align:right;width:100%;margin-top:var(--cnt-h-p)}.tp-ctnv_text{color:var(--in-fg);bottom:2px;display:inline-block;font-size:.9em;height:-moz-max-content;height:max-content;line-height:.9;margin:.2rem;max-height:100%;max-width:100%;opacity:.5;position:absolute;right:2px;text-align:right;white-space:normal;width:-moz-max-content;width:max-content;word-wrap:break-word}.tp-ctnv_frac{background-color:var(--in-fg);border-radius:1px;height:2px;left:50%;margin-top:-1px;position:absolute;top:50%}.tp-ctnv_icon{box-sizing:border-box;position:absolute;display:block;transform:scale(var(--ggs, 1));width:16px;height:6px;border:2px solid;border-top:0;border-bottom-left-radius:2px;border-bottom-right-radius:2px;margin-top:8px;opacity:.5}.tp-ctnv_icon::after{content:"";display:block;box-sizing:border-box;position:absolute;width:8px;height:8px;border-left:2px solid;border-top:2px solid;transform:rotate(45deg);left:2px;bottom:4px}.tp-ctnv_icon::before{content:"";display:block;box-sizing:border-box;position:absolute;border-radius:3px;width:2px;height:10px;background:currentColor;left:5px;bottom:3px}.tp-btnv_b{margin-top:10px}.tp-inputv{opacity:0}',
accept(exValue, params) {

@@ -666,0 +558,0 @@ if (typeof exValue !== 'string') {

@@ -1,1 +0,1 @@

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).TweakpaneFileImportPlugin={})}(this,(function(t){"use strict";class e{constructor(){this.observers_={}}on(t,e){let n=this.observers_[t];return n||(n=this.observers_[t]=[]),n.push({handler:e}),this}off(t,e){const n=this.observers_[t];return n&&(this.observers_[t]=n.filter((t=>t.handler!==e))),this}emit(t,e){const n=this.observers_[t];n&&n.forEach((t=>{t.handler(e)}))}}const n="tp";function i(t){return(e,i)=>[n,"-",t,"v",e?`_${e}`:"",i?`-${i}`:""].join("")}function o(t){return e=>n=>{if(!e&&void 0===n)return{succeeded:!1,value:void 0};if(e&&void 0===n)return{succeeded:!0,value:void 0};const i=t(n);return void 0!==i?{succeeded:!0,value:i}:{succeeded:!1,value:void 0}}}function s(t){return{custom:e=>o(e)(t),boolean:o((t=>"boolean"==typeof t?t:void 0))(t),number:o((t=>"number"==typeof t?t:void 0))(t),string:o((t=>"string"==typeof t?t:void 0))(t),function:o((t=>"function"==typeof t?t:void 0))(t),constant:e=>o((t=>t===e?e:void 0))(t),raw:o((t=>t))(t),object:e=>o((t=>{var n;if(null!==(n=t)&&"object"==typeof n)return function(t,e){return Object.keys(e).reduce(((n,i)=>{if(void 0===n)return;const o=(0,e[i])(t[i]);return o.succeeded?Object.assign(Object.assign({},n),{[i]:o.value}):void 0}),{})}(t,e)}))(t),array:e=>o((t=>{var n;if(Array.isArray(t))return n=e,t.reduce(((t,e)=>{if(void 0===t)return;const i=n(e);return i.succeeded&&void 0!==i.value?[...t,i.value]:void 0}),[])}))(t)}}const r={optional:s(!0),required:s(!1)};class a{constructor(t){this.constraints=t}constrain(t){return this.constraints.reduce(((t,e)=>e.constrain(t)),t)}}function l(t){return e=>e.toFixed(Math.max(Math.min(t,20),0))}l(0);function c(t,e){var n,i;const o=e.ownerDocument.defaultView,s=e.getBoundingClientRect();return{x:t.pageX-((null!==(n=o&&o.scrollX)&&void 0!==n?n:0)+s.left),y:t.pageY-((null!==(i=o&&o.scrollY)&&void 0!==i?i:0)+s.top)}}class u{constructor(t){this.lastTouch_=null,this.onDocumentMouseMove_=this.onDocumentMouseMove_.bind(this),this.onDocumentMouseUp_=this.onDocumentMouseUp_.bind(this),this.onMouseDown_=this.onMouseDown_.bind(this),this.onTouchEnd_=this.onTouchEnd_.bind(this),this.onTouchMove_=this.onTouchMove_.bind(this),this.onTouchStart_=this.onTouchStart_.bind(this),this.elem_=t,this.emitter=new e,t.addEventListener("touchstart",this.onTouchStart_,{passive:!1}),t.addEventListener("touchmove",this.onTouchMove_,{passive:!0}),t.addEventListener("touchend",this.onTouchEnd_),t.addEventListener("mousedown",this.onMouseDown_)}computePosition_(t){const e=this.elem_.getBoundingClientRect();return{bounds:{width:e.width,height:e.height},point:t?{x:t.x,y:t.y}:null}}onMouseDown_(t){var e;t.preventDefault(),null===(e=t.currentTarget)||void 0===e||e.focus();const n=this.elem_.ownerDocument;n.addEventListener("mousemove",this.onDocumentMouseMove_),n.addEventListener("mouseup",this.onDocumentMouseUp_),this.emitter.emit("down",{altKey:t.altKey,data:this.computePosition_(c(t,this.elem_)),sender:this,shiftKey:t.shiftKey})}onDocumentMouseMove_(t){this.emitter.emit("move",{altKey:t.altKey,data:this.computePosition_(c(t,this.elem_)),sender:this,shiftKey:t.shiftKey})}onDocumentMouseUp_(t){const e=this.elem_.ownerDocument;e.removeEventListener("mousemove",this.onDocumentMouseMove_),e.removeEventListener("mouseup",this.onDocumentMouseUp_),this.emitter.emit("up",{altKey:t.altKey,data:this.computePosition_(c(t,this.elem_)),sender:this,shiftKey:t.shiftKey})}onTouchStart_(t){t.preventDefault();const e=t.targetTouches.item(0),n=this.elem_.getBoundingClientRect();this.emitter.emit("down",{altKey:t.altKey,data:this.computePosition_(e?{x:e.clientX-n.left,y:e.clientY-n.top}:void 0),sender:this,shiftKey:t.shiftKey}),this.lastTouch_=e}onTouchMove_(t){const e=t.targetTouches.item(0),n=this.elem_.getBoundingClientRect();this.emitter.emit("move",{altKey:t.altKey,data:this.computePosition_(e?{x:e.clientX-n.left,y:e.clientY-n.top}:void 0),sender:this,shiftKey:t.shiftKey}),this.lastTouch_=e}onTouchEnd_(t){var e;const n=null!==(e=t.targetTouches.item(0))&&void 0!==e?e:this.lastTouch_,i=this.elem_.getBoundingClientRect();this.emitter.emit("up",{altKey:t.altKey,data:this.computePosition_(n?{x:n.clientX-i.left,y:n.clientY-i.top}:void 0),sender:this,shiftKey:t.shiftKey})}}function h(t){return[t[0],t[1],t[2]]}function d(t){return e=>function(t,e){const n=l("float"===e?2:0);return`rgb(${h(t.getComponents("rgb",e)).map((t=>n(t))).join(", ")})`}(e,t)}function p(t){return e=>function(t,e){const n=l(2),i=l("float"===e?2:0);return`rgba(${t.getComponents("rgb",e).map(((t,e)=>(3===e?n:i)(t))).join(", ")})`}(e,t)}function v(t){return e=>function(t,e){const n=l("float"===e?2:0),i=["r","g","b"];return`{${h(t.getComponents("rgb",e)).map(((t,e)=>`${i[e]}: ${n(t)}`)).join(", ")}}`}(e,t)}function f(t){return e=>function(t,e){const n=l(2),i=l("float"===e?2:0),o=["r","g","b","a"];return`{${t.getComponents("rgb",e).map(((t,e)=>`${o[e]}: ${(3===e?n:i)(t)}`)).join(", ")}}`}(e,t)}["int","float"].reduce(((t,e)=>[...t,{format:{alpha:!1,mode:"rgb",notation:"func",type:e},stringifier:d(e)},{format:{alpha:!0,mode:"rgb",notation:"func",type:e},stringifier:p(e)},{format:{alpha:!1,mode:"rgb",notation:"object",type:e},stringifier:v(e)},{format:{alpha:!0,mode:"rgb",notation:"object",type:e},stringifier:f(e)}]),[]);const m=i("ctn"),b=i("btn");class g{constructor(t,e){this.onDrop_=this.onDrop_.bind(this),this.element=t.createElement("div"),this.container=t.createElement("div"),this.container.style.height=`calc(var(--bld-us) * ${e.lineCount})`,this.container.classList.add(m()),this.element.appendChild(this.container),this.fileIconEl_=t.createElement("div"),this.fileIconEl_.classList.add(m("icon")),this.container.appendChild(this.fileIconEl_),this.textEl_=t.createElement("span"),this.textEl_.classList.add(m("text")),this.button=t.createElement("button"),this.button.classList.add(b("b")),this.button.innerHTML="Delete",this.button.style.display="none",this.element.appendChild(this.button),this.element.addEventListener("drop",this.onDrop_),this.element.addEventListener("dragover",this.onDragOver_),this.value_=e.value,this.value_.emitter.on("change",this.onValueChange_.bind(this)),e.viewProps.bindClassModifiers(this.element),e.viewProps.handleDispose((()=>{console.log("TODO: dispose view")}))}onDragOver_(t){t.preventDefault()}onDrop_(t){if(t instanceof DragEvent&&(t.preventDefault(),t.dataTransfer&&t.dataTransfer.files)){const e=t.dataTransfer.files;if(1==e.length){const t=e.item(0);this.value_.setRawValue(t)}}}onValueChange_(){const t=this.value_.rawValue;t?(this.textEl_.textContent=t.name,this.container.appendChild(this.textEl_),this.container.contains(this.fileIconEl_)&&this.container.removeChild(this.fileIconEl_),this.button.style.display="block",this.container.style.border="unset"):(this.textEl_.textContent="",this.container.appendChild(this.fileIconEl_),this.container.removeChild(this.textEl_),this.button.style.display="none",this.container.style.border="1px dashed #717070")}}class _{constructor(t,e){this.onContainerClick_=this.onContainerClick_.bind(this),this.onButtonClick_=this.onButtonClick_.bind(this),this.value=e.value,this.filetypes=e.filetypes,this.viewProps=e.viewProps,this.viewProps.handleDispose((()=>{})),this.view=new g(t,{value:this.value,viewProps:this.viewProps,lineCount:e.lineCount});new u(this.view.container).emitter.on("down",this.onContainerClick_);new u(this.view.button).emitter.on("down",this.onButtonClick_)}onContainerClick_(t){const e=this.filetypes,n=document.createElement("input");n.setAttribute("type","file"),n.style.opacity="0",n.style.position="fixed",n.style.pointerEvents="none",document.body.appendChild(n),n.addEventListener("input",(t=>{var i;if(n.files&&n.files.length>0){const t=n.files[0],o="."+(null===(i=t.name.split(".").pop())||void 0===i?void 0:i.toLowerCase());if(e&&e.length>0&&!e.includes(o)&&o)return;this.value.setRawValue(t)}}),{once:!0}),n.click(),document.body.removeChild(n)}onButtonClick_(t){this.value.rawValue&&this.value.setRawValue(null)}}const y=[{id:"file-input",type:"input",css:'.tp-ctnv{-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:rgba(0,0,0,0);border-width:0;font-family:inherit;font-size:inherit;font-weight:inherit;margin:0;outline:none;padding:0}.tp-ctnv{background-color:var(--in-bg);border-radius:var(--elm-br);box-sizing:border-box;color:var(--in-fg);font-family:inherit;height:var(--bld-us);line-height:var(--bld-us);min-width:0;width:100%}.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;display:flex;justify-content:center;align-items:center;overflow:hidden;position:relative;border:1px dashed #717070;border-radius:5px}.tp-ctnv.tp-v-disabled{opacity:.5}.tp-ctnv_text{color:var(--in-fg);bottom:2px;display:inline-block;font-size:.9em;height:-moz-max-content;height:max-content;line-height:.9;margin:.2rem;max-height:100%;max-width:100%;opacity:.5;position:absolute;right:2px;text-align:right;white-space:normal;width:-moz-max-content;width:max-content;word-wrap:break-word}.tp-ctnv_frac{background-color:var(--in-fg);border-radius:1px;height:2px;left:50%;margin-top:-1px;position:absolute;top:50%}.tp-ctnv_icon{box-sizing:border-box;position:relative;display:block;transform:scale(var(--ggs, 1));width:16px;height:6px;border:2px solid;border-top:0;border-bottom-left-radius:2px;border-bottom-right-radius:2px;margin-top:8px;opacity:.5}.tp-ctnv_icon::after{content:"";display:block;box-sizing:border-box;position:absolute;width:8px;height:8px;border-left:2px solid;border-top:2px solid;transform:rotate(45deg);left:2px;bottom:4px}.tp-ctnv_icon::before{content:"";display:block;box-sizing:border-box;position:absolute;border-radius:3px;width:2px;height:10px;background:currentColor;left:5px;bottom:3px}.tp-btnv_b{margin-top:10px}',accept(t,e){if("string"!=typeof t)return null;const n=r,i=function(t,e){const n=r.required.object(e)(t);return n.succeeded?n.value:void 0}(e,{view:n.required.constant("file-input"),lineCount:n.optional.number,filetypes:n.optional.array(n.required.string)});return i?{initialValue:t,params:i}:null},binding:{reader:t=>t=>t instanceof File?t:null,constraint:t=>new a([]),writer:t=>(t,e)=>{t.write(e)}},controller(t){var e;return new _(t.document,{value:t.value,viewProps:t.viewProps,lineCount:null!==(e=t.params.lineCount)&&void 0!==e?e:3,filetypes:t.params.filetypes})}}];t.plugins=y,Object.defineProperty(t,"__esModule",{value:!0})}));
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TweakpaneFileImportPlugin={})}(this,(function(e){"use strict";const t="tp";function i(e){return(i,n)=>[t,"-",e,"v",i?`_${i}`:"",n?`-${n}`:""].join("")}function n(e){return t=>i=>{if(!t&&void 0===i)return{succeeded:!1,value:void 0};if(t&&void 0===i)return{succeeded:!0,value:void 0};const n=e(i);return void 0!==n?{succeeded:!0,value:n}:{succeeded:!1,value:void 0}}}function o(e){return{custom:t=>n(t)(e),boolean:n((e=>"boolean"==typeof e?e:void 0))(e),number:n((e=>"number"==typeof e?e:void 0))(e),string:n((e=>"string"==typeof e?e:void 0))(e),function:n((e=>"function"==typeof e?e:void 0))(e),constant:t=>n((e=>e===t?t:void 0))(e),raw:n((e=>e))(e),object:t=>n((e=>{var i;if(null!==(i=e)&&"object"==typeof i)return function(e,t){return Object.keys(t).reduce(((i,n)=>{if(void 0===i)return;const o=(0,t[n])(e[n]);return o.succeeded?Object.assign(Object.assign({},i),{[n]:o.value}):void 0}),{})}(e,t)}))(e),array:t=>n((e=>{var i;if(Array.isArray(e))return i=t,e.reduce(((e,t)=>{if(void 0===e)return;const n=i(t);return n.succeeded&&void 0!==n.value?[...e,n.value]:void 0}),[])}))(e)}}const r={optional:o(!0),required:o(!1)};class a{constructor(e){this.constraints=e}constrain(e){return this.constraints.reduce(((e,t)=>t.constrain(e)),e)}}function s(e){return t=>t.toFixed(Math.max(Math.min(e,20),0))}s(0);function l(e){return[e[0],e[1],e[2]]}function d(e){return t=>function(e,t){const i=s("float"===t?2:0);return`rgb(${l(e.getComponents("rgb",t)).map((e=>i(e))).join(", ")})`}(t,e)}function c(e){return t=>function(e,t){const i=s(2),n=s("float"===t?2:0);return`rgba(${e.getComponents("rgb",t).map(((e,t)=>(3===t?i:n)(e))).join(", ")})`}(t,e)}function p(e){return t=>function(e,t){const i=s("float"===t?2:0),n=["r","g","b"];return`{${l(e.getComponents("rgb",t)).map(((e,t)=>`${n[t]}: ${i(e)}`)).join(", ")}}`}(t,e)}function h(e){return t=>function(e,t){const i=s(2),n=s("float"===t?2:0),o=["r","g","b","a"];return`{${e.getComponents("rgb",t).map(((e,t)=>`${o[t]}: ${(3===t?i:n)(e)}`)).join(", ")}}`}(t,e)}["int","float"].reduce(((e,t)=>[...e,{format:{alpha:!1,mode:"rgb",notation:"func",type:t},stringifier:d(t)},{format:{alpha:!0,mode:"rgb",notation:"func",type:t},stringifier:c(t)},{format:{alpha:!1,mode:"rgb",notation:"object",type:t},stringifier:p(t)},{format:{alpha:!0,mode:"rgb",notation:"object",type:t},stringifier:h(t)}]),[]);const u=i("ctn"),v=i("input"),g=i("btn");class f{constructor(e,t){this.element=e.createElement("div"),this.container=e.createElement("div"),this.container.classList.add(u()),t.viewProps.bindClassModifiers(this.container),this.input=e.createElement("input"),this.input.classList.add(v()),this.input.setAttribute("type","file"),this.input.setAttribute("accept",t.filetypes?t.filetypes.join(","):"*"),this.input.style.height=`calc(20px * ${t.lineCount})`,this.fileIcon=e.createElement("div"),this.fileIcon.classList.add(u("icon")),this.text=e.createElement("span"),this.text.classList.add(u("text")),this.warning=e.createElement("span"),this.warning.classList.add(u("warning")),this.warning.style.display="none",this.deleteButton=e.createElement("button"),this.deleteButton.classList.add(g("b")),this.deleteButton.innerHTML="Delete",this.deleteButton.style.display="none",this.container.appendChild(this.input),this.container.appendChild(this.fileIcon),this.element.appendChild(this.container),this.element.appendChild(this.warning),this.element.appendChild(this.deleteButton)}changeDraggingState(e){var t,i;e?null===(t=this.container)||void 0===t||t.classList.add(u("input_area_dragging")):null===(i=this.container)||void 0===i||i.classList.remove(u("input_area_dragging"))}}class b{constructor(e,t){this.value=t.value,this.viewProps=t.viewProps,this.view=new f(e,{viewProps:this.viewProps,value:t.value,lineCount:t.lineCount,filetypes:t.filetypes}),this.config=t,this.onFile=this.onFile.bind(this),this.onDrop=this.onDrop.bind(this),this.onDragOver=this.onDragOver.bind(this),this.onDragLeave=this.onDragLeave.bind(this),this.onDeleteClick=this.onDeleteClick.bind(this),this.view.input.addEventListener("change",this.onFile),this.view.element.addEventListener("drop",this.onDrop),this.view.element.addEventListener("dragover",this.onDragOver),this.view.element.addEventListener("dragleave",this.onDragLeave),this.view.deleteButton.addEventListener("click",this.onDeleteClick),this.value.emitter.on("change",(()=>this.handleValueChange())),this.viewProps.handleDispose((()=>{this.view.input.removeEventListener("change",this.onFile),this.view.element.removeEventListener("drop",this.onDrop),this.view.element.removeEventListener("dragover",this.onDragOver),this.view.element.removeEventListener("dragleave",this.onDragLeave),this.view.deleteButton.removeEventListener("click",this.onDeleteClick)}))}onFile(e){const t=this.view.input;if(t.files&&t.files.length>0){const e=t.files[0];this.isFileValid(e)?this.value.setRawValue(e):this.showWarning()}}showWarning(){this.view.warning.style.display="block",this.view.warning.innerHTML="Unaccepted file type.",setTimeout((()=>{this.view.warning.innerHTML="",this.view.warning.style.display="none"}),5e3)}isFileValid(e){var t;const i=this.config.filetypes,n="."+(null===(t=e.name.split(".").pop())||void 0===t?void 0:t.toLowerCase());return!(i&&i.length>0&&!i.includes(n)&&n)}onDeleteClick(){this.value.rawValue&&(this.value.setRawValue(null),this.view.input.value="",this.view.warning.innerHTML="",this.view.warning.style.display="none")}onDragOver(e){e.preventDefault(),this.view.changeDraggingState(!0)}onDragLeave(){this.view.changeDraggingState(!1)}onDrop(e){if(e instanceof DragEvent&&(e.preventDefault(),e.dataTransfer&&e.dataTransfer.files)){const t=e.dataTransfer.files;if(1==t.length){const e=t.item(0);e&&(this.isFileValid(e)?this.value.setRawValue(e):this.showWarning())}}this.view.changeDraggingState(!1)}handleValueChange(){const e=this.value.rawValue,t=this.view.container,i=this.view.text,n=this.view.fileIcon,o=this.view.deleteButton;e?(i.textContent=e.name,t.appendChild(i),t.contains(n)&&t.removeChild(n),this.view.warning.innerHTML="",this.view.warning.style.display="none",o.style.display="block",t.style.border="unset"):(i.textContent="",t.appendChild(n),t.removeChild(i),this.view.warning.innerHTML="",this.view.warning.style.display="none",o.style.display="none",t.style.border="1px dashed #717070")}}const m=[{id:"file-input",type:"input",css:'.tp-ctnv{-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:rgba(0,0,0,0);border-width:0;font-family:inherit;font-size:inherit;font-weight:inherit;margin:0;outline:none;padding:0}.tp-ctnv{background-color:var(--in-bg);border-radius:var(--elm-br);box-sizing:border-box;color:var(--in-fg);font-family:inherit;height:var(--bld-us);line-height:var(--bld-us);min-width:0;width:100%}.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;display:flex;justify-content:center;align-items:center;overflow:hidden;position:relative;height:100%;width:100%;border:1px dashed #717070;border-radius:5px}.tp-ctnv.tp-v-disabled{opacity:.5}.tp-ctnv_input_area_dragging{border:1px dashed #6774ff;background-color:rgba(88,88,185,.231372549)}.tp-ctnv_warning{color:var(--in-fg);bottom:2px;display:inline-block;font-size:.9em;height:-moz-max-content;height:max-content;line-height:.9;margin:.2rem;opacity:.5;white-space:normal;width:-moz-max-content;width:max-content;word-wrap:break-word;text-align:right;width:100%;margin-top:var(--cnt-h-p)}.tp-ctnv_text{color:var(--in-fg);bottom:2px;display:inline-block;font-size:.9em;height:-moz-max-content;height:max-content;line-height:.9;margin:.2rem;max-height:100%;max-width:100%;opacity:.5;position:absolute;right:2px;text-align:right;white-space:normal;width:-moz-max-content;width:max-content;word-wrap:break-word}.tp-ctnv_frac{background-color:var(--in-fg);border-radius:1px;height:2px;left:50%;margin-top:-1px;position:absolute;top:50%}.tp-ctnv_icon{box-sizing:border-box;position:absolute;display:block;transform:scale(var(--ggs, 1));width:16px;height:6px;border:2px solid;border-top:0;border-bottom-left-radius:2px;border-bottom-right-radius:2px;margin-top:8px;opacity:.5}.tp-ctnv_icon::after{content:"";display:block;box-sizing:border-box;position:absolute;width:8px;height:8px;border-left:2px solid;border-top:2px solid;transform:rotate(45deg);left:2px;bottom:4px}.tp-ctnv_icon::before{content:"";display:block;box-sizing:border-box;position:absolute;border-radius:3px;width:2px;height:10px;background:currentColor;left:5px;bottom:3px}.tp-btnv_b{margin-top:10px}.tp-inputv{opacity:0}',accept(e,t){if("string"!=typeof e)return null;const i=r,n=function(e,t){const i=r.required.object(t)(e);return i.succeeded?i.value:void 0}(t,{view:i.required.constant("file-input"),lineCount:i.optional.number,filetypes:i.optional.array(i.required.string)});return n?{initialValue:e,params:n}:null},binding:{reader:e=>e=>e instanceof File?e:null,constraint:e=>new a([]),writer:e=>(e,t)=>{e.write(t)}},controller(e){var t;return new b(e.document,{value:e.value,viewProps:e.viewProps,lineCount:null!==(t=e.params.lineCount)&&void 0!==t?t:3,filetypes:e.params.filetypes})}}];e.plugins=m,Object.defineProperty(e,"__esModule",{value:!0})}));

@@ -0,0 +0,0 @@ Copyright (c) 2021 cocopon <cocopon@me.com>

{
"name": "tweakpane-plugin-file-import",
"version": "0.1.6",
"version": "0.1.7",
"description": "A general-purpose and simple file input Tweakpane plugin",

@@ -18,5 +18,6 @@ "homepage": "https://github.com/LuchoTurtle/tweakpane-plugin-file-import",

"scripts": {
"release": "release-it",
"prepare": "run-s clean build",
"prepublishOnly": "npm test",
"start": "npm run watch",
"start": "run-p watch server",
"test": "eslint --ext .ts \"src/**/*.ts\"",

@@ -31,6 +32,6 @@ "assets": "run-s clean build assets:version assets:zip",

"build:prod": "rollup --config rollup.config.js --environment BUILD:production",
"release": "release-it",
"format": "run-p format:*",
"format:scss": "prettier --parser scss --write \"src/sass/**/*.scss\"",
"format:ts": "eslint --ext .ts --fix \"src/**/*.ts\"",
"server": "http-server -c-1 -o /test/browser.html",
"watch": "run-p watch:*",

@@ -56,2 +57,3 @@ "watch:sass": "onchange --initial --kill \"src/sass/**/*.scss\" -- npm run build:dev",

"eslint-plugin-simple-import-sort": "^10.0.0",
"http-server": "^14.1.1",
"install": "^0.13.0",

@@ -58,0 +60,0 @@ "jsdom": "^23.0.1",

@@ -0,0 +0,0 @@ # `tweakpane-plugin-file-import`

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc