{"size": 3295, "ext": "js", "max_stars_repo_path": "renderer.js", "max_stars_repo_name": "ahmetkorkmaz3/clipaste", "max_stars_repo_stars_event_min_datetime": "2020-04-03T17:35:55.000Z", "max_stars_repo_stars_event_max_datetime": "2021-10-05T18:29:57.000Z", "max_issues_repo_path": "renderer.js", "max_issues_repo_name": "ahmetkorkmaz3/clipaste", "max_issues_repo_issues_event_min_datetime": "2020-10-06T15:12:05.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-11T09:13:46.000Z", "max_forks_repo_path": "renderer.js", "max_forks_repo_name": "ahmetkorkmaz3/clipaste", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 4, "max_issues_count": 4, "max_forks_count": null, "avg_line_length": 23.3687943262, "max_line_length": 75, "alphanum_fraction": 0.6069802731}
const electron = require("electron");
const { app, ipcRenderer, clipboard } = require("electron");
const path = require("path");
const low = require("lowdb");
const FileSync = require("lowdb/adapters/FileSync");
const Swal = require("sweetalert2");

const $ = require("jquery");

function createItemTemplate(itemID, text) {
  return (
    `
  <div class="item" itemID="` +
    itemID +
    `">
    <div class="text">
    ` +
    text +
    `
    </div>
    <button class="clipboard" itemID="` +
    itemID +
    `">
      <span class="icon icon-clipboard"></span>
    </button>
    <button class="delete" itemID="` +
    itemID +
    `">
      <span class="icon icon-delete"></span>
    </button>
  </div>`
  );
}

ipcRenderer.on("update-clipboard", async event => {
  let text = await getLastItemDb();
  await writeLastItem(text[0]);
});

function getLastItemDb() {
  const adapter = new FileSync(
    path.join(electron.remote.app.getPath("home"), "/.clipaste.json")
  );
  const db = low(adapter);
  return (text = db
    .get("clipboard")
    .takeRight(1)
    .value());
}

$(function() {
  const adapter = new FileSync(
    path.join(electron.remote.app.getPath("home"), "/.clipaste.json")
  );
  const db = low(adapter);
  const items = db.get("clipboard").value();

  items.reverse();
  writeItems(items);
});

$(".minimize").click(function() {
  ipcRenderer.send("hide-window");
});

$(".exit").click(function() {
  Swal.fire({
    title: "Are you sure?",
    text: "Application is closing. Are you sure you want to continue?",
    showCancelButton: true,
    confirmButtonColor: "#d33",
    cancelButtonColor: "#aaa",
    confirmButtonText: "Yes, Exit!",
    cancelButtonText: "Cancel"
  }).then(result => {
    if (result.value) {
      ipcRenderer.send("app-quit");
    }
  });
});

$(".delete-all").click(function() {
  Swal.fire({
    title: "Are you sure?",
    text: "You won't be able to revert this!",
    showCancelButton: true,
    confirmButtonColor: "#d33",
    cancelButtonColor: "#aaa",
    confirmButtonText: "Delete Clipboard",
    cancelButtonText: "Cancel"
  }).then(result => {
    if (result.value) {
      const adapter = new FileSync(
        path.join(electron.remote.app.getPath("home"), "/.clipaste.json")
      );
      const db = low(adapter);
      db.get("clipboard")
        .remove()
        .write();
      clearDashboard();
    }
  });
});

$("#items").on("click", ".item button.delete", function() {
  itemID = $(this).attr("itemid");
  deleteItemClipboard(itemID);
});

$("#items").on("click", ".item button.clipboard", function() {
  itemID = $(this).attr("itemid");
  let text = $("#items div.item[itemid='" + itemID + "'] div.text").text();
  clipboard.writeText(text);
});

function deleteItemClipboard(itemID) {
  const adapter = new FileSync(
    path.join(electron.remote.app.getPath("home"), "/.clipaste.json")
  );
  const db = low(adapter);
  db.get("clipboard")
    .remove({ id: itemID })
    .write();

  $("div[itemid=" + itemID + "]").remove();
}

function writeItems(items) {
  items.forEach(element => {
    $("#items").append(createItemTemplate(element.id, element.text));
  });
}

function writeLastItem(item) {
  $("#items").prepend(createItemTemplate(item.id, item.text));
}

function clearDashboard() {
  $("#items").empty();
}
