Skip to content

Commit

Permalink
Merge pull request #551 from biati-digital/code-cleanup
Browse files Browse the repository at this point in the history
refactor: code cleanup and minor improvements
  • Loading branch information
biati-digital authored Feb 9, 2025
2 parents da26dba + 4a6b496 commit 30fdcd2
Show file tree
Hide file tree
Showing 32 changed files with 2,347 additions and 2,187 deletions.
32 changes: 31 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"files": {
"ignore": [
"packages/**/dist/*",
"packages/**/*.mjs",
"packages/**/*.d.ts"
]
},
"linter": {
"rules": {
"correctness": {
Expand All @@ -9,5 +16,28 @@
"noParameterAssign": "off"
}
}
},
"css": {
"formatter": {
"enabled": true,
"quoteStyle": "double",
"indentStyle": "space",
"indentWidth": 2
},
"linter": {
"enabled": true
},
"parser": {
"cssModules": true
}
},
"json": {
"formatter": {
"enabled": false,
"trailingCommas": "none"
},
"linter": {
"enabled": false
}
}
}
}
15 changes: 4 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
"author": "Biati Digital",
"scripts": {
"prepare": "npm run build --workspaces --if-present",
"lint": "npm run lint:scripts && npm run lint:styles",
"lint:scripts": "eslint ./packages --ext .ts",
"lint:styles": "stylelint './**/*.css' --allow-empty-input",
"lint": "npx @biomejs/biome ci .",
"format": "npx @biomejs/biome format --write .",
"build": "npm run build --workspaces --if-present",
"watch": "npm run watch --workspaces --if-present",
"change:beta": "changeset pre enter beta",
Expand All @@ -29,20 +28,14 @@
"private": true,
"license": "GPLV3",
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@types/node": "^22.5.4",
"@typescript-eslint/eslint-plugin": "^8.4.0",
"@typescript-eslint/parser": "^8.4.0",
"eslint": "^9.10.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"stylelint": "^16.2.1",
"stylelint-config-recommended": "^14.0.0",
"ts-node": "^10.9.2",
"typescript": "^5.0.2",
"vite": "^5.1.6",
"vite-plugin-dts": "^4.2.1",
"vite-plugin-static-copy": "^1.0.1"
}
}
}
2 changes: 1 addition & 1 deletion packages/drag-navigation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"bugs": "https://github.com/biati-digital/glightbox/issues",
"type": "module",
"main": "./dist/index.cjs.js",
"main": "./dist/index.es.js",
"module": "./dist/index.es.js",
"types": "./dist/index.d.ts",
"files": [
Expand Down
207 changes: 111 additions & 96 deletions packages/drag-navigation/src/drag.ts
Original file line number Diff line number Diff line change
@@ -1,114 +1,129 @@
import type { PluginOptions, PluginType } from '@glightbox/plugin-core';
import { GLightboxPlugin } from '@glightbox/plugin-core';
import type { PluginOptions, PluginType } from "@glightbox/plugin-core";
import { GLightboxPlugin } from "@glightbox/plugin-core";

export interface DragOptions extends PluginOptions {
dragToleranceX?: number;
dragToleranceX?: number;
}

export default class DragNavigation extends GLightboxPlugin {
name = 'drag';
type: PluginType = 'other';
options: DragOptions = {};
isDown = false;
slider: HTMLElement | null = null;
actveSlide: HTMLElement | null = null;
startX = 0;
scrollLeft = 0;
activeSlideIndex = 0;
movedAmount = 0;
movedDirection = '';
defaults: DragOptions = {
dragToleranceX: 10
};
mouseDownEvent: ((e: MouseEvent) => void) | null = null;
mouseLeaveEvent: ((e: MouseEvent) => void) | null = null;
mouseUpEvent: ((e: MouseEvent) => void) | null = null;
mouseMoveEvent: ((e: MouseEvent) => void) | null = null;
name = "drag";
type: PluginType = "other";
options: DragOptions = {};
isDown = false;
slider: HTMLElement | null = null;
actveSlide: HTMLElement | null = null;
startX = 0;
scrollLeft = 0;
activeSlideIndex = 0;
movedAmount = 0;
movedDirection = "";
defaults: DragOptions = {
dragToleranceX: 10,
};
mouseDownEvent: ((e: MouseEvent) => void) | null = null;
mouseLeaveEvent: ((e: MouseEvent) => void) | null = null;
mouseUpEvent: ((e: MouseEvent) => void) | null = null;
mouseMoveEvent: ((e: MouseEvent) => void) | null = null;

constructor(options: Partial<DragOptions> = {}) {
super();
this.options = { ...this.defaults, ...options };
}
constructor(options: Partial<DragOptions> = {}) {
super();
this.options = { ...this.defaults, ...options };
}

init(): void {
const slider = document.querySelector<HTMLElement>('.gl-slider');
if (!slider) {
return;
}
init(): void {
const slider = document.querySelector<HTMLElement>(".gl-slider");
if (!slider) {
return;
}

this.mouseDownEvent = this.onMouseDown.bind(this);
this.mouseLeaveEvent = this.onMouseLeave.bind(this);
this.mouseUpEvent = this.onMouseUp.bind(this);
this.mouseMoveEvent = this.onMouseMove.bind(this);
this.mouseDownEvent = this.onMouseDown.bind(this);
this.mouseLeaveEvent = this.onMouseLeave.bind(this);
this.mouseUpEvent = this.onMouseUp.bind(this);
this.mouseMoveEvent = this.onMouseMove.bind(this);

slider.addEventListener('mousedown', this.mouseDownEvent);
slider.addEventListener('mouseleave', this.mouseLeaveEvent);
slider.addEventListener('mouseup', this.mouseUpEvent);
slider.addEventListener('mousemove', this.mouseMoveEvent);
this.slider = slider;
}
slider.addEventListener("mousedown", this.mouseDownEvent);
slider.addEventListener("mouseleave", this.mouseLeaveEvent);
slider.addEventListener("mouseup", this.mouseUpEvent);
slider.addEventListener("mousemove", this.mouseMoveEvent);
this.slider = slider;
}

destroy(): void {
this.mouseDownEvent && this.slider?.removeEventListener('mousedown', this.mouseDownEvent);
this.mouseLeaveEvent && this.slider?.removeEventListener('mouseleave', this.mouseLeaveEvent);
this.mouseUpEvent && this.slider?.removeEventListener('mouseup', this.mouseUpEvent);
this.mouseMoveEvent && this.slider?.removeEventListener('mousemove', this.mouseMoveEvent);
}
destroy(): void {
this.mouseDownEvent &&
this.slider?.removeEventListener("mousedown", this.mouseDownEvent);
this.mouseLeaveEvent &&
this.slider?.removeEventListener("mouseleave", this.mouseLeaveEvent);
this.mouseUpEvent &&
this.slider?.removeEventListener("mouseup", this.mouseUpEvent);
this.mouseMoveEvent &&
this.slider?.removeEventListener("mousemove", this.mouseMoveEvent);
}

onMouseDown(e: MouseEvent) {
if (!this.slider) {
return;
}
this.isDown = true;
this.slider?.classList.add('doing-drag');
this.startX = e.pageX - this.slider.offsetLeft;
this.scrollLeft = this.slider.scrollLeft;
this.actveSlide = this.slider.querySelector('.visible');
this.activeSlideIndex = Number.parseInt(this.actveSlide?.getAttribute('data-index') || '0');
}
onMouseDown(e: MouseEvent) {
if (!this.slider) {
return;
}
this.isDown = true;
this.slider?.classList.add("doing-drag");
this.startX = e.pageX - this.slider.offsetLeft;
this.scrollLeft = this.slider.scrollLeft;
this.actveSlide = this.slider.querySelector(".visible");
this.activeSlideIndex = Number.parseInt(
this.actveSlide?.getAttribute("data-index") || "0",
);
}

onMouseUp() {
this.isDown = false;
let scrollTo = this.actveSlide;
if (this.movedAmount > 10) {
const nextIndex = this.movedDirection === 'right' ? this.activeSlideIndex + 1 : this.activeSlideIndex - 1;
const next = this.slider?.querySelector<HTMLElement>(`div[data-index="${nextIndex}"]`);
if (next) {
scrollTo = next;
}
}
this.slider?.addEventListener('scrollend', this.removeDragClass);
scrollTo?.scrollIntoView({ behavior: 'smooth', block: 'start', inline: 'start' });
}
onMouseUp() {
this.isDown = false;
let scrollTo = this.actveSlide;
if (this.movedAmount > 10) {
const nextIndex =
this.movedDirection === "right"
? this.activeSlideIndex + 1
: this.activeSlideIndex - 1;
const next = this.slider?.querySelector<HTMLElement>(
`div[data-index="${nextIndex}"]`,
);
if (next) {
scrollTo = next;
}
}
this.slider?.addEventListener("scrollend", this.removeDragClass);
scrollTo?.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "start",
});
}

onMouseMove(e: MouseEvent) {
if (!this.isDown || !this.slider) {
return;
}
e.preventDefault();
onMouseMove(e: MouseEvent) {
if (!this.isDown || !this.slider) {
return;
}
e.preventDefault();

const x = e.pageX - this.slider.offsetLeft;
const SCROLL_SPEED = 1;
const walk = (x - this.startX) * SCROLL_SPEED;
const x = e.pageX - this.slider.offsetLeft;
const SCROLL_SPEED = 1;
const walk = (x - this.startX) * SCROLL_SPEED;

const sliderWidth = this.slider.clientWidth;
let moved = this.scrollLeft - walk;
if (this.activeSlideIndex > 0) {
moved = moved - (sliderWidth * this.activeSlideIndex + 1);
}
const percentage = (moved / sliderWidth) * 100;
this.movedDirection = percentage > 0 ? 'right' : 'left';
this.slider.scrollLeft = this.scrollLeft - walk;
this.movedAmount = Math.abs(percentage);
}
const sliderWidth = this.slider.clientWidth;
let moved = this.scrollLeft - walk;
if (this.activeSlideIndex > 0) {
moved = moved - (sliderWidth * this.activeSlideIndex + 1);
}
const percentage = (moved / sliderWidth) * 100;
this.movedDirection = percentage > 0 ? "right" : "left";
this.slider.scrollLeft = this.scrollLeft - walk;
this.movedAmount = Math.abs(percentage);
}

onMouseLeave() {
this.isDown = false;
this.slider?.classList.remove('doing-drag');
}
onMouseLeave() {
this.isDown = false;
this.slider?.classList.remove("doing-drag");
}

removeDragClass() {
this.slider?.classList.remove('doing-drag');
this.slider?.removeEventListener('scrollend', this.removeDragClass);
}
removeDragClass() {
this.slider?.classList.remove("doing-drag");
this.slider?.removeEventListener("scrollend", this.removeDragClass);
}
}
3 changes: 1 addition & 2 deletions packages/drag-navigation/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { default as ImageSlide } from './drag';

export { default as ImageSlide } from "./drag";
43 changes: 20 additions & 23 deletions packages/drag-navigation/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
// vite.config.ts
import { resolve } from 'path';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import { resolve } from "node:path";
import { defineConfig } from "vite";
import dts from "vite-plugin-dts";
// https://vitejs.dev/guide/build.html#library-mode

export default defineConfig({
build: {
minify: true,
cssCodeSplit: true,
cssMinify: true,
lib: {
entry: resolve(__dirname, 'src/index.ts'),
name: 'DragPlugin',
fileName: (format, entryName) => {
if (format === 'umd') {
return `${entryName}.umd.js`;
}
if (format === 'cjs') {
return `${entryName}.cjs.js`;
}
return `${entryName}.es.js`;
},
formats: ['es', 'cjs', 'umd']
}
},
plugins: [dts()]
build: {
minify: true,
cssCodeSplit: true,
cssMinify: true,
lib: {
entry: resolve(__dirname, "src/index.ts"),
name: "DragPlugin",
fileName: (format, entryName) => {
if (format === "umd") {
return `${entryName}.umd.js`;
}
return `${entryName}.es.js`;
},
formats: ["es", "umd"],
},
},
plugins: [dts()],
});
Loading

0 comments on commit 30fdcd2

Please sign in to comment.