MCPcopy
hub / github.com/sansan0/TrendRadar / handleFileDrop

Function handleFileDrop

docs/assets/script.js:365–428  ·  view source on GitHub ↗
(e, type)

Source from the content-addressed store, hash-verified

363}
364
365function handleFileDrop(e, type) {
366 const files = e.dataTransfer.files;
367 if (files.length === 0) return;
368
369 const file = files[0];
370
371 const validExtensions = type === 'config'
372 ? ['.yaml', '.yml', '.txt']
373 : type === 'timeline'
374 ? ['.yaml', '.yml']
375 : ['.txt', '.yaml', '.yml'];
376
377 const fileName = file.name.toLowerCase();
378 const isValid = validExtensions.some(ext => fileName.endsWith(ext));
379
380 if (!isValid) {
381 showToast(`请拖入 ${type === 'config' || type === 'timeline' ? 'YAML' : 'TXT'} 文件`, 'error');
382 return;
383 }
384
385 const reader = new FileReader();
386 reader.onload = (event) => {
387 const content = event.target.result;
388
389 if (type === 'config') {
390 try {
391 jsyaml.load(content);
392 document.getElementById('yaml-editor').value = content;
393 currentYaml = content;
394 syncYamlToUI();
395 showToast(`已加载: ${file.name}`, 'success');
396 } catch (err) {
397 showToast(`YAML 语法错误: ${err.message}`, 'error');
398 // 仍然加载,让用户修复
399 document.getElementById('yaml-editor').value = content;
400 currentYaml = content;
401 }
402 } else if (type === 'timeline') {
403 try {
404 jsyaml.load(content);
405 document.getElementById('timeline-editor').value = content;
406 currentTimeline = content;
407 updateBackdrop('timeline-editor', 'timeline-backdrop');
408 syncTimelineToUI();
409 showToast(`已加载: ${file.name}`, 'success');
410 } catch (err) {
411 showToast(`YAML 语法错误: ${err.message}`, 'error');
412 document.getElementById('timeline-editor').value = content;
413 currentTimeline = content;
414 }
415 } else {
416 document.getElementById('frequency-editor').value = content;
417 currentFrequency = content;
418 syncFrequencyToUI();
419 showToast(`已加载: ${file.name}`, 'success');
420 }
421 };
422

Callers 1

initDragAndDropFunction · 0.85

Calls 5

showToastFunction · 0.85
syncYamlToUIFunction · 0.85
updateBackdropFunction · 0.85
syncTimelineToUIFunction · 0.85
syncFrequencyToUIFunction · 0.85

Tested by

no test coverage detected