Skip to content

Commit

Permalink
Merge pull request #149 from kimlimjustin/feat/hover
Browse files Browse the repository at this point in the history
feat: hover effect
  • Loading branch information
kimlimjustin authored Oct 7, 2021
2 parents 08fdfcf + b11e4b6 commit 9313f2e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 4 deletions.
36 changes: 36 additions & 0 deletions src/Components/Constants/fileTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const IMAGE_TYPES = [
'.jpg',
'.png',
'.gif',
'.bmp',
'.jpeg',
'.jpe',
'.jif',
'.jfif',
'.jfi',
'.webp',
'.tiff',
'.tif',
'.ico',
'.svg',
'.webp',
];
const VIDEO_TYPES = [
'.mp4',
'.webm',
'.mpg',
'.mp2',
'.mpeg',
'.mpe',
'.mpv',
'.ocg',
'.m4p',
'.m4v',
'.avi',
'.wmv',
'.mov',
'.qt',
'.flv',
'.swf',
];
export { IMAGE_TYPES, VIDEO_TYPES };
5 changes: 1 addition & 4 deletions src/Components/Files/File Preview/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import { URLify, eURLify } from '../../Functions/urlify';
import hljs from 'highlight.js';
import marked from 'marked';
import storage from 'electron-json-storage-sync';
import { IMAGE_TYPES, VIDEO_TYPES } from '../../Constants/fileTypes';

//prettier-ignore
const FILE_TYPES_AVAILABLE_FOR_PREVIEW = ['.pdf', '.html', '.docx', '.htm', '.xlsx', '.xls', '.xlsb', 'xls', '.ods', '.fods', '.csv', '.txt', '.py', '.js', '.bat', '.css', '.c++', '.cpp', '.cc', '.c', '.diff', '.patch', '.go', '.java', '.json', '.php', '.ts', '.tsx', '.jsx', '.jpg', '.png', '.gif', '.bmp', '.jpeg', '.jpe', '.jif', '.jfif', '.jfi', '.webp', '.tiff', '.tif', '.ico', '.svg', '.webp', '.mp4', '.webm', '.mpg', '.mp2', '.mpeg', '.mpe', '.mpv', '.ocg', '.m4p', '.m4v', '.avi', '.wmv', '.mov', '.qt', '.flv', '.swf', '.md']
//prettier-ignore
const IMAGE_TYPES = ['.jpg', '.png', '.gif', '.bmp', '.jpeg', '.jpe', '.jif', '.jfif', '.jfi', '.webp', '.tiff', '.tif', '.ico', '.svg', '.webp']
//prettier-ignore
const VIDEO_TYPES = ['.mp4', '.webm', '.mpg', '.mp2', '.mpeg', '.mpe', '.mpv', '.ocg', '.m4p', '.m4v', '.avi', '.wmv', '.mov', '.qt', '.flv', '.swf']

/**
* Close the preview file
Expand Down
12 changes: 12 additions & 0 deletions src/Components/Files/files.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
}
}

.hovering .file-grid-filename {
-webkit-line-clamp: initial !important;
}

.detail-view {
display: inline-flex;
width: -webkit-fill-available;
Expand Down Expand Up @@ -132,3 +136,11 @@
background: var(--selected-grid-background) !important;
color: var(--selected-grid-color) !important;
}

.hover-preview {
position: fixed;
width: 300px;
img {
width: 100%;
}
}
63 changes: 63 additions & 0 deletions src/Components/Layout/hover.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import path from 'path';
import { IMAGE_TYPES } from '../Constants/fileTypes';

/**
* Listen to mouse hovering
* @returns {void}
*/
const Hover = (): void => {
let timeOut: number;
let displayName: string;
let hoveringElement: HTMLElement;
const hoverPreviewElement = document.createElement('div');

document.querySelector('#workspace').addEventListener('mousemove', (e) => {
const x = (e as MouseEvent).clientX;
const y = (e as MouseEvent).clientY;
window.clearTimeout(timeOut);
hoverPreviewElement?.parentNode?.removeChild(hoverPreviewElement);

// Ignore workspace hovering
if ((e.target as HTMLElement).id === 'workspace') {
if (hoveringElement?.dataset?.path && displayName)
hoveringElement.querySelector('.file-grid-filename').innerHTML =
displayName;
return;
}
hoveringElement?.classList?.remove('hovering');

const target = (e.target as HTMLElement)?.dataset?.path
? (e.target as HTMLElement)
: ((e.target as HTMLElement)?.parentNode as HTMLElement);

const filenameGrid = target.querySelector('.file-grid-filename');

if (target !== hoveringElement) {
displayName = undefined;
}
hoveringElement = target;

timeOut = window.setTimeout(() => {
displayName = filenameGrid.innerHTML;
filenameGrid.innerHTML = path.basename(
unescape(target.dataset.path)
);
target?.classList?.add('hovering');

if (
IMAGE_TYPES.indexOf(path.extname(filenameGrid.innerHTML)) !== -1
) {
hoverPreviewElement.innerHTML = `<img src="${unescape(
target.dataset.path
)}">`;
hoverPreviewElement.classList.add('hover-preview');
hoverPreviewElement.style.top = y + 'px';
hoverPreviewElement.style.left = x + 'px';
hoverPreviewElement.dataset.path = target.dataset.path;
document.body.appendChild(hoverPreviewElement);
}
}, 500);
});
};

export default Hover;
3 changes: 3 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { SelectListener } from './Components/Files/File Operation/select';
import { Shortcut } from './Components/Shortcut/shortcut';
import path from 'path';
import fs from 'fs';
import Hover from './Components/Layout/hover';

const args = ipcRenderer.sendSync('args');
if (args.listen && args.theme) {
Expand Down Expand Up @@ -68,4 +69,6 @@ document.addEventListener('DOMContentLoaded', async () => {
ContextMenu(document.getElementById('workspace'));
// Initialize shortcut
Shortcut();
// Initialize hover handler
Hover();
});

1 comment on commit 9313f2e

@vercel
Copy link

@vercel vercel bot commented on 9313f2e Oct 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.