Skip to content

Commit

Permalink
🚧 refactor file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
palmcivet committed Jul 21, 2023
1 parent de4802f commit 8d05aa8
Show file tree
Hide file tree
Showing 26 changed files with 335 additions and 491 deletions.
165 changes: 2 additions & 163 deletions src/Scrollbar/index.ts
Original file line number Diff line number Diff line change
@@ -1,167 +1,6 @@
// @ts-nocheck

/**
* - 检测挂载节点宽度和滚动条宽度,将容器宽度放大,隐藏原生滚动条
* - 监听滚动事件,替代滚动行为
*/

type Direction = "vertical" | "horizontal";

export interface IScrollbarOptions {
/**
* @member 是否悬浮
*/
isLevitative: boolean;
/**
* @member 是否可隐藏
*/
suppressible: boolean;
/**
* @member 滚动行为
*/
scrollBehavior: "beisaier" | "smooth";
}

export class Scrollbar {
/**
* @description 滚动方向
*/
public direction!: Direction;

/**
* @description 是否为垂直方向
*/
private get isVertical() {
return this.direction === "vertical";
}

/**
* @description 挂载的容器
*/
private readonly root!: HTMLElement;

/**
* @description 滚动条轨道
*/
private readonly scrollbarThumb!: HTMLDivElement;

/**
* @description 滚动条
*/
private readonly scrollbarTrack!: HTMLDivElement;

/**
* @description 保存挂载节点的样式
*/
private readonly rootStyleMap: { [k: string]: string } = {};

/**
* @description 水平方向是否溢出
*/
private isOverflowX!: boolean;

/**
* @description 垂直方向是否溢出
*/
private get isOverflowY() {
return false;
}

/**
* @description 配置项
*/
private options!: IScrollbarOptions;

constructor(
root: HTMLElement,
direction?: Direction,
options?: Partial<IScrollbarOptions>
) {
this.options = {
suppressible: true,
isLevitative: true,
scrollBehavior: "smooth",
...options,
};

this.root = root;
this.rootStyleMap = {
position: this.root.style.position,
};
this.root.style.position = "relative";

this.scrollbarTrack = document.createElement("div");
this.scrollbarTrack.className = `unitext-scrollbar scrollbar-track ${direction}`;

this.scrollbarThumb = document.createElement("div");
this.scrollbarThumb.className = "scrollbar-thumb";
this.scrollbarTrack.appendChild(this.scrollbarThumb);
}

public invoke(): void {
this.root.appendChild(this.scrollbarTrack);
this.root.addEventListener("mouseenter", this.onShowScrollbar.bind(this));
this.root.addEventListener("mouseleave", this.onHideScrollbar.bind(this));
}

public dispose(): void {
this.root.removeEventListener("mouseenter", this.onShowScrollbar);
this.root.removeEventListener("mouseleave", this.onHideScrollbar);

Object.keys(this.rootStyleMap).forEach((styleName) => {
this.root.style[styleName as any] = this.rootStyleMap[styleName];
});

this.scrollbarTrack.removeChild(this.scrollbarThumb);
this.root.removeChild(this.scrollbarTrack);
}

public updateOptions(options: Partial<IScrollbarOptions>): void {}

public setVisibility(status: boolean): void {
if (this.options.suppressible) {
this.scrollbarTrack.style.display = "none";
}
}

public setThumbSize(size: number): void {
const attribute: keyof HTMLDivElement = `client${
this.isVertical ? "Height" : "Width"
}`;
if (size >= this.scrollbarTrack[attribute] || size <= 0) {
return;
}

this.scrollbarThumb.style[this.isVertical ? "height" : "width"] = `${size}px`;
}

public scrollTo(position: number): void {
const attribute: keyof HTMLDivElement = `client${
this.isVertical ? "Height" : "Width"
}`;

if (position <= 0) {
this.scrollbarThumb.style[this.isVertical ? "top" : "left"] = "0px";
} else if (position >= this.scrollbarTrack[attribute]) {
this.scrollbarThumb.style[this.isVertical ? "bottom" : "right"] = "0px";
}

this.scrollbarThumb.style[this.isVertical ? "top" : "left"] = `${position}px`;
}

private onScroll() {}

private onClick(event: MouseEvent) {
this.scrollTo(event.clientX);
}

/**
* @description 显示滚动条
*/
private onShowScrollbar(): void {}

/**
* @description 隐藏滚动条
*/
private onHideScrollbar(): void {}
}
export * from "./scrollbar";
export * from "./type";
191 changes: 0 additions & 191 deletions src/TreeView/treemodel.ts

This file was deleted.

File renamed without changes.
18 changes: 5 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
export { EventBus } from "./EventBus";

export { Scrollbar } from "./Scrollbar";
export type { IScrollbarOptions } from "./Scrollbar";

export { ListView } from "./ListView";
export type { IListViewOptions } from "./ListView";

export { TreeView } from "./TreeView";
export type { ITreeViewOptions } from "./TreeView";
export type { ITreeNodeFile, ITreeNodeFolder } from "./TreeView/treemodel";

export { SplitView } from "./SplitView";
export * from "./event-bus";
export * from "./scrollbar";
export * from "./list-view";
export * from "./tree-view";
export * from "./split-view";
File renamed without changes.
2 changes: 2 additions & 0 deletions src/list-view/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./list-view";
export * from "./type";
Loading

0 comments on commit 8d05aa8

Please sign in to comment.