Electronjs 拖放

Electronjs drag and drop

我是电子新手,想处理拖放功能。 想要删除文件并获取其扩展名。基于扩展更改屏幕上的内容。

即如果将 .mp3 文件放入其中,则需要将 backgroundColor 更改为绿色并将 .jpg 文件更改为红色。

我已经成功地使用了拖放,但不知道如何正确处理它。到目前为止我的代码是:
main.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello World!</title>
  </head>
  <body style="color: grey;">
    <div class="character">
      <h1>{nome}</h1>
      <img id="char_anim" src="assets/goku.gif" />
    </div>
  </body>
  <a href="#" id="drag">item</a>
  <script type="text/javascript" charset="utf-8">
    document.getElementById("drag").ondragstart = (event) => {
      event.preventDefault();
      ipcRenderer.send("ondragstart", "/path/to/item");

    };
  </script>
</html>

main.js

const { app, BrowserWindow, ipcMain } = require("electron");
const path = require("path");

const MAIN_HTML = path.join("file://", __dirname, "main.html");
const CHILD_PADDING = 50;

const onAppReady = function () {
  let parent = new BrowserWindow({
    width: 800,
    height: 1200,
    transparent: false,
    frame: true,
  });

  parent.once("close", () => {
    parent = null;
  });

  parent.loadURL(MAIN_HTML);
};

//~ app.on('ready', onAppReady);
app.on("ready", () => setTimeout(onAppReady, 500));

// dragndrop
ipcMain.on("ondragstart", (event, filePath) => {
  event.sender.startDrag({
    file: filePath,
    icon: "/path/to/icon.png",
  });
});

我可以解释更多,但我只需要一种方法来处理该应用程序,以了解哪个扩展程序具有此已删除的文件并为其中一个或另一个显示不同的消息。

在您的主进程中,您可以使用 path 模块获取文件扩展名:

let ext = require('path').extname(filePath)

然后,您可以将返回值发送给渲染器,如下所示:

event.sender.send('get-file-extension', ext);

之后,您可以使用 ipcRenderer 获取发送的值以更改背景颜色:

ipcRenderer.on('get-file-extension', (event, extension) => {
     if(extension == '.mp3')
          document.body.style.backgroundColor = "green";
     else if(extension == '.jpg')
          document.body.style.backgroundColor = "red";
});

我在挣扎。不知道把代码放在哪里。 index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Hello World!</title>
  </head>
  <body style="color: grey;">
    <div class="character">
      <h1>{Goku Desbloqueado}</h1>
      <img id="char_anim" src="assets/goku.gif" />
    </div>
  </body>

  <a href="#" id="drag">Arraste para cá.</a>
  <script type="text/javascript" charset="utf-8">
    document.getElementById("drag").ondragstart = (event) => {
      event.preventDefault();
      ipcRenderer.send("ondragstart", "/path/to/item");
    };
  </script>
  <script src="./renderer.js"></script>
</html>

main.js

const { app, BrowserWindow, ipcMain } = require("electron");
const path = require("path");
let ext = require("path").extname(filePath);

const MAIN_HTML = path.join("file://", __dirname, "main.html");
const CHILD_PADDING = 50;

const onAppReady = function () {
  let parent = new BrowserWindow({
    width: 800,
    height: 1200,
    transparent: false,
    frame: true,
  });

  parent.once("close", () => {
    parent = null;
  });

  parent.loadURL(MAIN_HTML);
};

//~ app.on('ready', onAppReady);
app.on("ready", () => setTimeout(onAppReady, 500));

// dragndrop
ipcMain.on("ondragstart", (event, filePath) => {
  event.sender.startDrag({
    file: filePath,
    icon: "/path/to/icon.png",
  });
});

ipcRenderer.on("get-file-extension", (event, extension) => {
  if (extension == ".mp3") document.body.style.backgroundColor = "green";
  else if (extension == ".jpg") document.body.style.backgroundColor = "red";
});
renderer.js
const { ipcRenderer } = require("electron");

event.sender.send("get-file-extension", ext);

对不起