@cocreate/file
Advanced tools
Comparing version 1.14.2 to 1.15.0
@@ -0,1 +1,15 @@ | ||
# [1.15.0](https://github.com/CoCreate-app/CoCreate-file/compare/v1.14.2...v1.15.0) (2024-02-05) | ||
### Bug Fixes | ||
* Removed https://cdn.cocreate.app/latest/CoCreate.min.css ([e0ae28d](https://github.com/CoCreate-app/CoCreate-file/commit/e0ae28de52a02ee58947093f6dad3dd9dfee93e8)) | ||
* renderValue ([4e3308a](https://github.com/CoCreate-app/CoCreate-file/commit/4e3308a157ee2257bc1eb603ddc1484b5579cac8)) | ||
* typo renderValue condition ([65ba50f](https://github.com/CoCreate-app/CoCreate-file/commit/65ba50fe6a84e02bad033f423d39a0ab22fdb766)) | ||
### Features | ||
* setValue() ([44d4012](https://github.com/CoCreate-app/CoCreate-file/commit/44d40127823d7ccdf8286943b435eeff939e6e2f)) | ||
## [1.14.2](https://github.com/CoCreate-app/CoCreate-file/compare/v1.14.1...v1.14.2) (2024-01-17) | ||
@@ -2,0 +16,0 @@ |
{ | ||
"name": "@cocreate/file", | ||
"version": "1.14.2", | ||
"version": "1.15.0", | ||
"description": "A headless file uploader that uses HTML5 attributes for customization. Allows easy upload of files to server.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -29,3 +29,3 @@ /******************************************************************************** | ||
import Actions from '@cocreate/actions'; | ||
import render from '@cocreate/render'; | ||
import { render } from '@cocreate/render'; | ||
import { queryElements } from '@cocreate/utils'; | ||
@@ -46,3 +46,3 @@ import '@cocreate/element-prototype'; | ||
*/ | ||
function init(elements) { | ||
async function init(elements) { | ||
if (!elements) | ||
@@ -53,9 +53,5 @@ elements = document.querySelectorAll('[type="file"]') | ||
for (let i = 0; i < elements.length; i++) { | ||
if (elements[i].tagName !== 'INPUT') { | ||
// TODO: create input and append to div if input dos not exist | ||
// check if input exist | ||
let input = document.createElement("input"); | ||
input.type = "file"; | ||
input.setAttribute('hidden', '') | ||
elements[i].appendChild(input); | ||
let nestedInput, isInput = elements[i].tagName === 'INPUT' | ||
if (!isInput) { | ||
nestedInput = elements[i].querySelector('input[type="file"]') | ||
} | ||
@@ -65,17 +61,50 @@ | ||
elements[i].getFiles = async () => await getFiles([elements[i]]) | ||
elements[i].setValue = (files) => setFiles(elements[i], files); | ||
elements[i].renderValue = (files) => setFiles(elements[i], files); | ||
// elements[i].setValue = (value) => pickr.setColor(value); | ||
// if (elements[i].renderValue) { | ||
// let data = await elements[i].getValue() | ||
// if (data) | ||
// elements[i].setValue(data) | ||
// } | ||
if (elements[i].hasAttribute('directory')) { | ||
if (window.showDirectoryPicker) | ||
if (!isInput && window.showDirectoryPicker) | ||
elements[i].addEventListener("click", fileEvent); | ||
else if ('webkitdirectory' in elements[i]) { | ||
elements[i].webkitdirectory = true | ||
elements[i].addEventListener("change", fileEvent) | ||
if (!isInput && !nestedInput) { | ||
nestedInput = document.createElement("input"); | ||
nestedInput.type = "file"; | ||
nestedInput.setAttribute('hidden', '') | ||
elements[i].appendChild(nestedInput); | ||
} | ||
if (nestedInput) { | ||
elements[i].addEventListener("click", function () { | ||
nestedInput.click() | ||
}); | ||
nestedInput.addEventListener("change", fileEvent); | ||
} else | ||
elements[i].addEventListener("change", fileEvent); | ||
} else | ||
console.error("Directory selection not supported in this browser."); | ||
} else if (window.showOpenFilePicker) | ||
} else if (!isInput && window.showOpenFilePicker) | ||
elements[i].addEventListener("click", fileEvent); | ||
else | ||
elements[i].addEventListener("change", fileEvent); | ||
else { | ||
if (!isInput && !nestedInput) { | ||
nestedInput = document.createElement("input"); | ||
nestedInput.type = "file"; | ||
nestedInput.setAttribute('hidden', '') | ||
elements[i].appendChild(nestedInput); | ||
} | ||
if (nestedInput) { | ||
elements[i].addEventListener("click", function () { | ||
nestedInput.click() | ||
}); | ||
nestedInput.addEventListener("change", fileEvent); | ||
} else | ||
elements[i].addEventListener("change", fileEvent); | ||
} | ||
} | ||
@@ -89,3 +118,3 @@ } | ||
let files = input.files; | ||
if (!files.length) { | ||
if (!files || !files.length) { | ||
event.preventDefault() | ||
@@ -128,3 +157,3 @@ const multiple = input.multiple | ||
if (!files[i].src) | ||
files[i] = await readFile(files[i]) | ||
await readFile(files[i]) | ||
@@ -149,4 +178,4 @@ files[i].directory = handle.directory || '/' | ||
if (!input.renderValue) | ||
input.renderValue(selected.values()) | ||
if (input.renderValue) | ||
input.renderValue(Array.from(selected.values())) | ||
@@ -158,3 +187,2 @@ const isImport = input.getAttribute('import') | ||
} | ||
} | ||
@@ -212,5 +240,5 @@ } catch (error) { | ||
if (selected) { | ||
for (let file of selected.values()) { | ||
for (let file of Array.from(selected.values())) { | ||
if (!file.src) | ||
file = await readFile(file) | ||
await readFile(file) | ||
@@ -226,3 +254,2 @@ file = await getCustomData({ ...file }) | ||
// gets file custom data | ||
async function getCustomData(file) { | ||
@@ -239,6 +266,8 @@ let form = document.querySelector(`[file_id="${file.id}"]`); | ||
} | ||
delete file.input; | ||
return file; | ||
} | ||
// This function reads the file and returns its src | ||
@@ -258,3 +287,3 @@ function readFile(file) { | ||
readAs = 'readAsDataURL'; | ||
} if (['mp3', 'wav', 'wma', 'aac', 'ogg'].includes(fileType[1]) | ||
} else if (['mp3', 'wav', 'wma', 'aac', 'ogg'].includes(fileType[1]) | ||
|| fileType[0] === 'audio') { // updated condition | ||
@@ -284,2 +313,20 @@ readAs = 'readAsDataURL'; | ||
function setFiles(element, files) { | ||
if (!files) return | ||
if (!Array.isArray(files)) | ||
files = [files] | ||
else if (!files.length) | ||
return | ||
let selected = inputs.get(element) || new Map() | ||
for (let i = 0; i < files.length; i++) { | ||
files[i].input = element | ||
selected.set(files[i].id, files[i]) | ||
Files.set(files[i].id, files[i]) | ||
} | ||
inputs.set(element, selected); | ||
if (element.renderValue) | ||
render({ source: element, data: Array.from(selected.values()) }) | ||
} | ||
async function save(element, action, data) { | ||
@@ -286,0 +333,0 @@ try { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
138774
1213