{"size": 2151, "ext": "js", "max_stars_repo_path": "main/copyShortcuts.js", "max_stars_repo_name": "Terry-Su/TSCopyPaste", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "main/copyShortcuts.js", "max_issues_repo_name": "Terry-Su/TSCopyPaste", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "main/copyShortcuts.js", "max_forks_repo_name": "Terry-Su/TSCopyPaste", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 18.7043478261, "max_line_length": 91, "alphanum_fraction": 0.6713156671}
/**
 * copyShortcuts module
 */

const electron = require('electron')
const dataManager = require('./dataManager')
const { globalShortcut, clipboard } = electron


const copyShortcuts = {
  bindCopyShortcuts() {
    const settings = dataManager.getCopySetting()
    settings.map(function (setting, id) {
      registerSaveClipboardToLocal({
        setting,
        id,
        dataManager,
        globalShortcut,
        clipboard
      })
      registerLoadLocalToClicpboard({
        setting,
        id,
        dataManager,
        globalShortcut,
        clipboard
      })
    })
  },
  saveContentToLocal
}

module.exports = copyShortcuts



function registerSaveClipboardToLocal({
  setting,
  id,
  dataManager,
  globalShortcut,
  clipboard,
  }) {
  const { key } = setting
  const { save } = key

  // shortcuts command, for example: "CommandOrControl+F1"
  const command = concatCommand(save)

  globalShortcut.register(command, () => {
    saveClipboardContentToLocal({
      id,
      dataManager,
      clipboard
    })
  })
}

function registerLoadLocalToClicpboard({
  setting,
  id,
  dataManager,
  globalShortcut,
  clipboard
  }) {
  const { key } = setting
  const { load } = key

  // shortcuts command, for example: "CommandOrControl+F1"
  const command = concatCommand(load)

  globalShortcut.register(command, () => {
    writeLocalContentToClipboard({
      id,
      dataManager,
      clipboard
    })
  })
}

function saveClipboardContentToLocal({
  id,
  dataManager,
  clipboard
}) {
  var clipboardContent = clipboard.readText()
  saveContentToLocal({
    content: clipboardContent,
    id,
    dataManager
  })
}

function saveContentToLocal({
  content,
  id,
  dataManager
}) {
  const tmpSettings = dataManager.getCopySetting()
  tmpSettings[id].content = content
  dataManager.setCopySetting(tmpSettings)
}

function writeLocalContentToClipboard({
  id,
  dataManager,
  clipboard
}) {
  var localContent = dataManager.getCopySetting()[id].content
  clipboard.writeText(localContent)
}

function concatCommand(arr) {
  return arr.reduce(function (accumulator, current) { return accumulator + '+' + current })
}