Skip to content

Commit

Permalink
Use clipboard contentTypes; add max clipboard item count
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Jacobs committed Feb 21, 2017
1 parent 6eee4b0 commit 471c71d
Show file tree
Hide file tree
Showing 2 changed files with 3,317 additions and 31 deletions.
72 changes: 41 additions & 31 deletions src/index.js
Expand Up @@ -5,6 +5,8 @@ const copyIcon = require('./CopyIcon.png');
const noItemsIcon = require('./NoItemsIcon.png');


const MAX_CLIPBOARD_ITEM_COUNT = 20;

const clipboardStorage = [];

startWatchingClipboard();
Expand All @@ -13,8 +15,6 @@ const plugin = ({term, display, actions}) => {
const match = /clipboard\s(.*)/.exec(term);
if (match && clipboardStorage.length) {
const [, filter] = match;
console.log(clipboardStorage);
console.log(filter);
const displayObjs = clipboardStorage.filter(({ type, value }) => {
if (!filter) return true;
if (type === 'image') {
Expand All @@ -24,8 +24,8 @@ const plugin = ({term, display, actions}) => {
}).map(({ type, value }, i) => {
const isImage = (type === 'image');
return isImage ?
generateImageDisplay({ type, value, i+1 }) :
generateTextDisplay({ type, value, i+1 })
generateImageDisplay({ type, value, index: i + 1 }) :
generateTextDisplay({ type, value, index: i + 1 })
});
display(displayObjs);
} else if (match) { // length == 0
Expand All @@ -43,10 +43,10 @@ module.exports = {
}


function generateTextDisplay({ type, value, i }) {
function generateTextDisplay({ type, value, index }) {
return {
icon: copyIcon,
title: `${i}. ${value}`,
title: `${index}. ${value}`,
onSelect: () => {
actions.copyToClipboard(value);
new Notification('Text copied to clipboard', {
Expand All @@ -61,10 +61,10 @@ function generateTextDisplay({ type, value, i }) {
};
}

function generateImageDisplay({ type, value, i }) {
function generateImageDisplay({ type, value, index }) {
return {
icon: copyIcon,
title: `${i}. Image`,
title: `${index}. Image`,
onSelect: () => {
clipboard.writeImage(value);
new Notification('Image copied to clipboard');
Expand All @@ -82,33 +82,43 @@ function generateImageDisplay({ type, value, i }) {


const imageDataUrlPreambleRegex = /^data:image\/.+;base64,.+/;
const imageContentTypes = ['image/gif', 'image/png', 'image/jpeg', 'image/bmp', 'image/webp'];
function startWatchingClipboard() {
setInterval(() => {
let clipboardImageValue = clipboard.readImage();
const clipboardTextValue = clipboard.readText();

const textIsImage = imageDataUrlPreambleRegex.test(clipboardTextValue);
let isImage = textIsImage;
if (textIsImage) {
clipboardImageValue = nativeImage.createFromDataURL(clipboardTextValue);
} else if (imageDataUrlPreambleRegex.test(clipboardImageValue.toDataURL())) {
isImage = true;
} else {
if (!clipboardTextValue || /^\s*$/.test(clipboardTextValue)) {
return;
}
}
setInterval(readClipboardAndSaveNewValues, 1000);
}

function readClipboardAndSaveNewValues() {
let clipboardImageValue;
const clipboardTextValue = clipboard.readText();

const clipboardValue = {
type: isImage ? 'image' : 'text',
value: isImage ? clipboardImageValue : clipboardTextValue
};
const clipboardAvailableFormats = clipboard.availableFormats();
const textIsImage = imageDataUrlPreambleRegex.test(clipboardTextValue);
let isImage = imageContentTypes.reduce((prevResult, imageContentType) => {
return prevResult || clipboardAvailableFormats.includes(imageContentType);
}, false);

const lastValue = clipboardStorage[0];
if (!lastValue || !valuesAreEqual(lastValue, clipboardValue)) {
clipboardStorage.unshift(clipboardValue);
if (isImage) {
clipboardImageValue = clipboard.readImage();
} else if (textIsImage) {
isImage = true;
clipboardImageValue = nativeImage.createFromDataURL(clipboardTextValue);
} else {
// make sure there is a non-falsy value
if (!clipboardTextValue || /^\s*$/.test(clipboardTextValue)) {
return;
}
}, 1000);
}

const clipboardValue = {
type: isImage ? 'image' : 'text',
value: isImage ? clipboardImageValue : clipboardTextValue
};

const lastValue = clipboardStorage[0];
if (!lastValue || !valuesAreEqual(lastValue, clipboardValue)) {
clipboardStorage.unshift(clipboardValue);
clipboardStorage.length = MAX_CLIPBOARD_ITEM_COUNT;
}
}

function valuesAreEqual(prevValue, newValue) {
Expand Down

0 comments on commit 471c71d

Please sign in to comment.