sync
This commit is contained in:
1 parent
5cb14ffd44
commit
05ce7035dd
15 files changed
+946
-460
No files matched your search
@@ -1,19 +0,0 @@
|
||||
/**
|
||||
* @template {keyof HTMLElementTagNameMap} K
|
||||
* @param {K} tagName
|
||||
* @param {string | ((e: HTMLElementTagNameMap[K]) => void) | null} [init]
|
||||
* @param {...(string | Node)} children
|
||||
* @returns {HTMLElementTagNameMap[K]}
|
||||
*/
|
||||
export function createElement(tagName, init, ...children) {
|
||||
const element = document.createElement(tagName);
|
||||
if (typeof init === 'function') {
|
||||
init(element);
|
||||
} else if (init != null) {
|
||||
element.className = init;
|
||||
}
|
||||
if (children.length > 0) {
|
||||
element.append(...children);
|
||||
}
|
||||
return element;
|
||||
}
|
||||
@@ -0,0 +1,553 @@
|
||||
import type { MovableOptions, BoxRect, MarkRequired, SnapLine } from "./types";
|
||||
|
||||
export function createElement<K extends keyof HTMLElementTagNameMap>(
|
||||
tagName: K,
|
||||
init?: string | ((e: HTMLElementTagNameMap[K]) => void) | null,
|
||||
...children: (string | Node)[]
|
||||
): HTMLElementTagNameMap[K];
|
||||
|
||||
export function createElement(
|
||||
tagName: string,
|
||||
init?: string | ((e: HTMLElement) => void) | null,
|
||||
...children: (string | Node)[]
|
||||
): HTMLElement;
|
||||
|
||||
export function createElement(
|
||||
tagName: string,
|
||||
init?: string | ((e: any) => void) | null,
|
||||
...children: (string | Node)[]
|
||||
): HTMLElement {
|
||||
const el = document.createElement(tagName);
|
||||
|
||||
if (typeof init === 'function') {
|
||||
init(el);
|
||||
} else if (init != null) {
|
||||
el.className = init;
|
||||
}
|
||||
|
||||
if (children.length > 0) {
|
||||
el.append(...children);
|
||||
}
|
||||
|
||||
return el;
|
||||
}
|
||||
|
||||
export function mergeOptions<T extends object, D extends Partial<T>>(
|
||||
defaults: D,
|
||||
opts?: T
|
||||
): MarkRequired<T, Extract<keyof D, keyof T>> {
|
||||
const result: Record<string, unknown> = { ...defaults };
|
||||
if (opts) {
|
||||
for (const [key, value] of Object.entries(opts)) {
|
||||
if (value !== undefined) {
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result as MarkRequired<T, Extract<keyof D, keyof T>>;
|
||||
}
|
||||
|
||||
type ResizeDir = 'n' | 's' | 'w' | 'e' | 'nw' | 'se' | 'ne' | 'sw';
|
||||
type DragAction = ResizeDir | 'move';
|
||||
|
||||
const CURSOR_MAP: Record<DragAction, string> = {
|
||||
n: 'ns-resize', s: 'ns-resize',
|
||||
w: 'ew-resize', e: 'ew-resize',
|
||||
nw: 'nwse-resize', se: 'nwse-resize',
|
||||
ne: 'nesw-resize', sw: 'nesw-resize',
|
||||
move: 'move'
|
||||
};
|
||||
|
||||
function isResizeDir(dir: string | undefined): dir is ResizeDir {
|
||||
return typeof dir === 'string' && dir !== 'move' &&
|
||||
Object.prototype.hasOwnProperty.call(CURSOR_MAP, dir);
|
||||
}
|
||||
|
||||
function collectSnapTargets(
|
||||
box: HTMLElement,
|
||||
element: HTMLElement,
|
||||
dragSelector: string,
|
||||
toContainer: boolean,
|
||||
toSiblings: boolean
|
||||
): { targetsX: number[], targetsY: number[] } {
|
||||
const setX = new Set<number>();
|
||||
const setY = new Set<number>();
|
||||
const parent = box.offsetParent as HTMLElement | null;
|
||||
|
||||
if (toContainer) {
|
||||
const isRoot = !parent || parent === document.body || parent === document.documentElement;
|
||||
const cw = isRoot ? document.documentElement.clientWidth : parent.clientWidth;
|
||||
const ch = isRoot ? document.documentElement.clientHeight : parent.clientHeight;
|
||||
setX.add(0).add(cw / 2).add(cw);
|
||||
setY.add(0).add(ch / 2).add(ch);
|
||||
}
|
||||
|
||||
if (toSiblings) {
|
||||
const scope = element === box && parent ? parent : element;
|
||||
const siblings = scope.querySelectorAll<HTMLElement>(dragSelector);
|
||||
for (const sb of siblings) {
|
||||
if (sb === box || sb.offsetParent !== parent) {
|
||||
continue;
|
||||
}
|
||||
const sl = sb.offsetLeft;
|
||||
const st = sb.offsetTop;
|
||||
const sw = sb.offsetWidth;
|
||||
const sh = sb.offsetHeight;
|
||||
setX.add(sl).add(sl + sw / 2).add(sl + sw);
|
||||
setY.add(st).add(st + sh / 2).add(st + sh);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
targetsX: Array.from(setX),
|
||||
targetsY: Array.from(setY)
|
||||
};
|
||||
}
|
||||
|
||||
function snapAxisMove(
|
||||
rawPos: number,
|
||||
size: number,
|
||||
targets: number[],
|
||||
threshold: number
|
||||
): { pos: number; guides: number[] } {
|
||||
if (threshold <= 0 || targets.length === 0) {
|
||||
return { pos: rawPos, guides: [] };
|
||||
}
|
||||
|
||||
const offsets = [0, size / 2, size];
|
||||
let bestDist = threshold + 1;
|
||||
let bestPos = rawPos;
|
||||
|
||||
for (const off of offsets) {
|
||||
const anchor = rawPos + off;
|
||||
for (const t of targets) {
|
||||
const dist = Math.abs(t - anchor);
|
||||
if (dist <= threshold && dist < bestDist) {
|
||||
bestDist = dist;
|
||||
bestPos = t - off;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const guides = new Set<number>();
|
||||
for (const off of offsets) {
|
||||
const finalAnchor = bestPos + off;
|
||||
for (const t of targets) {
|
||||
if (Math.abs(t - finalAnchor) < 0.01) {
|
||||
guides.add(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { pos: bestPos, guides: Array.from(guides) };
|
||||
}
|
||||
|
||||
function snapEdge(
|
||||
rawEdge: number,
|
||||
targets: number[],
|
||||
threshold: number,
|
||||
minBound: number,
|
||||
maxBound: number
|
||||
): { edge: number; guide: number | null } {
|
||||
if (threshold <= 0 || targets.length === 0) {
|
||||
return { edge: rawEdge, guide: null };
|
||||
}
|
||||
|
||||
let bestDist = threshold + 1;
|
||||
let bestEdge = rawEdge;
|
||||
let bestGuide: number | null = null;
|
||||
|
||||
for (const target of targets) {
|
||||
if (target < minBound || target > maxBound) {
|
||||
continue;
|
||||
}
|
||||
const dist = Math.abs(target - rawEdge);
|
||||
if (dist <= threshold && dist < bestDist) {
|
||||
bestDist = dist;
|
||||
bestEdge = target;
|
||||
bestGuide = target;
|
||||
}
|
||||
}
|
||||
|
||||
return { edge: bestEdge, guide: bestGuide };
|
||||
}
|
||||
|
||||
export function appendResizer(parent: ParentNode) {
|
||||
parent.append(
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'resizer';
|
||||
layer.dataset.dir = 'e';
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'resizer';
|
||||
layer.dataset.dir = 's';
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'resizer';
|
||||
layer.dataset.dir = 'w';
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'resizer';
|
||||
layer.dataset.dir = 'n';
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'resizer';
|
||||
layer.dataset.dir = 'se';
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'resizer';
|
||||
layer.dataset.dir = 'sw';
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'resizer';
|
||||
layer.dataset.dir = 'nw';
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'resizer';
|
||||
layer.dataset.dir = 'ne';
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export function makeMovable<T = void>(element: HTMLElement, opts: MovableOptions<T>) {
|
||||
|
||||
const DEFAULT_OPTIONS = {
|
||||
minSize: 60,
|
||||
snapThreshold: 0,
|
||||
snapToContainer: true,
|
||||
snapToSiblings: true,
|
||||
hoverSelector: '.drag-hover',
|
||||
hoverIgnore: 'button,input,textarea,select,a,[contenteditable],svg,use',
|
||||
dragSelector: '.drag-box',
|
||||
resizerSelector: '.resizer',
|
||||
rootDragClass: 'is-interacting'
|
||||
} satisfies Partial<MovableOptions<T>>;
|
||||
|
||||
const opt = mergeOptions(DEFAULT_OPTIONS, opts);
|
||||
const controller = new AbortController();
|
||||
|
||||
let dragController: AbortController | null = null;
|
||||
let rafId = 0;
|
||||
|
||||
let state: {
|
||||
moved: boolean;
|
||||
resized: boolean;
|
||||
hasGuides: boolean;
|
||||
box: HTMLElement;
|
||||
pointerId: number;
|
||||
action: DragAction;
|
||||
x: number;
|
||||
y: number;
|
||||
clientX: number;
|
||||
clientY: number;
|
||||
lastClientX: number;
|
||||
lastClientY: number;
|
||||
left: number;
|
||||
top: number;
|
||||
width: number;
|
||||
height: number;
|
||||
curLeft: number;
|
||||
curTop: number;
|
||||
curWidth: number;
|
||||
curHeight: number;
|
||||
targetsX: number[];
|
||||
targetsY: number[];
|
||||
prevCursor: string;
|
||||
} | null = null;
|
||||
|
||||
function getAction(el: Element, target: Element): DragAction | null {
|
||||
if (opt.hoverIgnore && target.closest(opt.hoverIgnore)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (opt.resizable) {
|
||||
const resizer = target.closest<HTMLElement>(opt.resizerSelector);
|
||||
if (resizer && resizer.closest(opt.dragSelector) === el) {
|
||||
const dir = resizer.dataset.dir;
|
||||
if (isResizeDir(dir)) {
|
||||
return dir;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (opt.movable) {
|
||||
const hover = target.closest(opt.hoverSelector);
|
||||
if (hover && hover.closest(opt.dragSelector) === el) {
|
||||
return 'move';
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function emitSnap(guides: SnapLine[]) {
|
||||
if (!state) {
|
||||
return;
|
||||
}
|
||||
if (guides.length > 0 || state.hasGuides) {
|
||||
state.hasGuides = guides.length > 0;
|
||||
if (typeof opt.onSnap === 'function') {
|
||||
opt.onSnap.call(opt.context, guides);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyFrame() {
|
||||
rafId = 0;
|
||||
|
||||
if (!state) {
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
box, action, x, y, clientX, clientY,
|
||||
lastClientX, lastClientY,
|
||||
left, top, width, height,
|
||||
targetsX, targetsY
|
||||
} = state;
|
||||
|
||||
if (clientX === lastClientX && clientY === lastClientY) {
|
||||
return;
|
||||
}
|
||||
state.lastClientX = clientX;
|
||||
state.lastClientY = clientY;
|
||||
|
||||
const dx = clientX - x;
|
||||
const dy = clientY - y;
|
||||
const guides: SnapLine[] = [];
|
||||
|
||||
if (action === 'move') {
|
||||
const snapX = snapAxisMove(left + dx, width, targetsX, opt.snapThreshold);
|
||||
const snapY = snapAxisMove(top + dy, height, targetsY, opt.snapThreshold);
|
||||
|
||||
if (snapX.pos !== state.curLeft) {
|
||||
box.style.left = `${snapX.pos}px`;
|
||||
state.curLeft = snapX.pos;
|
||||
state.moved = true;
|
||||
}
|
||||
if (snapY.pos !== state.curTop) {
|
||||
box.style.top = `${snapY.pos}px`;
|
||||
state.curTop = snapY.pos;
|
||||
state.moved = true;
|
||||
}
|
||||
|
||||
for (const pos of snapX.guides) {
|
||||
guides.push({ axis: 'x', pos });
|
||||
}
|
||||
for (const pos of snapY.guides) {
|
||||
guides.push({ axis: 'y', pos });
|
||||
}
|
||||
emitSnap(guides);
|
||||
return;
|
||||
}
|
||||
|
||||
let newWidth = width, newHeight = height,
|
||||
newLeft = left, newTop = top;
|
||||
|
||||
if (action.includes('e')) {
|
||||
const rawRight = left + Math.max(opt.minSize, width + dx);
|
||||
const snap = snapEdge(rawRight, targetsX, opt.snapThreshold, left + opt.minSize, Infinity);
|
||||
newWidth = snap.edge - left;
|
||||
if (snap.guide != null) {
|
||||
guides.push({ axis: 'x', pos: snap.guide });
|
||||
}
|
||||
}
|
||||
|
||||
if (action.includes('w')) {
|
||||
const right = left + width;
|
||||
const rawLeft = right - Math.max(opt.minSize, width - dx);
|
||||
const snap = snapEdge(rawLeft, targetsX, opt.snapThreshold, -Infinity, right - opt.minSize);
|
||||
newLeft = snap.edge;
|
||||
newWidth = right - newLeft;
|
||||
if (snap.guide != null) {
|
||||
guides.push({ axis: 'x', pos: snap.guide });
|
||||
}
|
||||
}
|
||||
|
||||
if (action.includes('s')) {
|
||||
const rawBottom = top + Math.max(opt.minSize, height + dy);
|
||||
const snap = snapEdge(rawBottom, targetsY, opt.snapThreshold, top + opt.minSize, Infinity);
|
||||
newHeight = snap.edge - top;
|
||||
if (snap.guide != null) {
|
||||
guides.push({ axis: 'y', pos: snap.guide });
|
||||
}
|
||||
}
|
||||
|
||||
if (action.includes('n')) {
|
||||
const bottom = top + height;
|
||||
const rawTop = bottom - Math.max(opt.minSize, height - dy);
|
||||
const snap = snapEdge(rawTop, targetsY, opt.snapThreshold, -Infinity, bottom - opt.minSize);
|
||||
newTop = snap.edge;
|
||||
newHeight = bottom - newTop;
|
||||
if (snap.guide != null) {
|
||||
guides.push({ axis: 'y', pos: snap.guide });
|
||||
}
|
||||
}
|
||||
|
||||
if (newWidth !== state.curWidth) {
|
||||
box.style.width = `${newWidth}px`;
|
||||
state.curWidth = newWidth;
|
||||
state.resized = true;
|
||||
}
|
||||
if (newHeight !== state.curHeight) {
|
||||
box.style.height = `${newHeight}px`;
|
||||
state.curHeight = newHeight;
|
||||
state.resized = true;
|
||||
}
|
||||
if (newLeft !== state.curLeft) {
|
||||
box.style.left = `${newLeft}px`;
|
||||
state.curLeft = newLeft;
|
||||
state.resized = true;
|
||||
}
|
||||
if (newTop !== state.curTop) {
|
||||
box.style.top = `${newTop}px`;
|
||||
state.curTop = newTop;
|
||||
state.resized = true;
|
||||
}
|
||||
|
||||
emitSnap(guides);
|
||||
}
|
||||
|
||||
function onPointerMove(e: PointerEvent) {
|
||||
if (!state || e.pointerId !== state.pointerId) {
|
||||
return;
|
||||
}
|
||||
state.clientX = e.clientX;
|
||||
state.clientY = e.clientY;
|
||||
if (!rafId) {
|
||||
rafId = requestAnimationFrame(applyFrame);
|
||||
}
|
||||
}
|
||||
|
||||
function finishDrag(e?: PointerEvent) {
|
||||
if (!state || (e && e.pointerId !== state.pointerId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (rafId) {
|
||||
cancelAnimationFrame(rafId);
|
||||
rafId = 0;
|
||||
if (e) {
|
||||
applyFrame();
|
||||
}
|
||||
}
|
||||
|
||||
dragController?.abort();
|
||||
dragController = null;
|
||||
|
||||
emitSnap([]);
|
||||
|
||||
const {
|
||||
box, pointerId, moved, resized,
|
||||
curLeft, curTop, curWidth, curHeight,
|
||||
prevCursor
|
||||
} = state;
|
||||
state = null;
|
||||
|
||||
if (box.hasPointerCapture(pointerId)) {
|
||||
box.releasePointerCapture(pointerId);
|
||||
}
|
||||
|
||||
const root = document.documentElement;
|
||||
root.classList.remove(opt.rootDragClass);
|
||||
root.style.cursor = prevCursor;
|
||||
|
||||
if (e && (moved || resized)) {
|
||||
const rect: BoxRect = {
|
||||
top: curTop,
|
||||
left: curLeft,
|
||||
width: curWidth,
|
||||
height: curHeight
|
||||
};
|
||||
|
||||
if (moved && typeof opt.onMoved === 'function') {
|
||||
opt.onMoved.call(opt.context, rect);
|
||||
}
|
||||
if (resized && typeof opt.onResized === 'function') {
|
||||
opt.onResized.call(opt.context, rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
element.addEventListener('pointerdown', e => {
|
||||
if (state) {
|
||||
return;
|
||||
}
|
||||
if (e.pointerType === 'mouse' && e.button !== 0) {
|
||||
return;
|
||||
}
|
||||
if (!(e.target instanceof Element)) {
|
||||
return;
|
||||
}
|
||||
const box = e.target.closest<HTMLElement>(opt.dragSelector);
|
||||
if (!box || !element.contains(box)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const action = getAction(box, e.target);
|
||||
if (!action) {
|
||||
return;
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
try {
|
||||
box.setPointerCapture(e.pointerId);
|
||||
} catch {
|
||||
}
|
||||
|
||||
const left = box.offsetLeft;
|
||||
const top = box.offsetTop;
|
||||
const width = box.offsetWidth;
|
||||
const height = box.offsetHeight;
|
||||
|
||||
const { targetsX, targetsY } = opt.snapThreshold > 0 ?
|
||||
collectSnapTargets(box, element, opt.dragSelector, opt.snapToContainer, opt.snapToSiblings) :
|
||||
{ targetsX: [], targetsY: [] };
|
||||
|
||||
const root = document.documentElement;
|
||||
state = {
|
||||
moved: false,
|
||||
resized: false,
|
||||
hasGuides: false,
|
||||
box,
|
||||
pointerId: e.pointerId,
|
||||
action,
|
||||
x: e.clientX,
|
||||
y: e.clientY,
|
||||
clientX: e.clientX,
|
||||
clientY: e.clientY,
|
||||
lastClientX: e.clientX,
|
||||
lastClientY: e.clientY,
|
||||
left,
|
||||
top,
|
||||
width,
|
||||
height,
|
||||
curLeft: left,
|
||||
curTop: top,
|
||||
curWidth: width,
|
||||
curHeight: height,
|
||||
targetsX,
|
||||
targetsY,
|
||||
prevCursor: root.style.cursor
|
||||
};
|
||||
root.classList.add(opt.rootDragClass);
|
||||
root.style.cursor = CURSOR_MAP[action];
|
||||
|
||||
dragController = new AbortController();
|
||||
const { signal } = dragController;
|
||||
document.addEventListener('pointermove', onPointerMove, { signal });
|
||||
document.addEventListener('pointerup', finishDrag, { signal });
|
||||
document.addEventListener('pointercancel', finishDrag, { signal });
|
||||
box.addEventListener('lostpointercapture', finishDrag, { signal });
|
||||
|
||||
if (typeof opt.onResizeStart === 'function') {
|
||||
opt.onResizeStart.call(opt.context);
|
||||
}
|
||||
}, { signal: controller.signal });
|
||||
|
||||
return () => {
|
||||
finishDrag();
|
||||
controller.abort();
|
||||
};
|
||||
}
|
||||
Vendored
+132
-10
@@ -9,6 +9,40 @@ export interface OptionParamBase {
|
||||
getText?: GetTextCallback;
|
||||
}
|
||||
|
||||
export interface RectLike {
|
||||
top?: number;
|
||||
left?: number;
|
||||
width?: number;
|
||||
height?: number;
|
||||
zIndex?: number;
|
||||
}
|
||||
|
||||
export type BoxRect = Required<Omit<RectLike, 'zIndex'>>;
|
||||
|
||||
export interface SnapLine {
|
||||
axis: 'x' | 'y';
|
||||
pos: number;
|
||||
}
|
||||
|
||||
export interface MovableOptions<T = void> {
|
||||
movable: boolean;
|
||||
resizable: boolean;
|
||||
minSize?: number;
|
||||
snapThreshold?: number;
|
||||
snapToContainer?: boolean;
|
||||
snapToSiblings?: boolean;
|
||||
hoverSelector?: string;
|
||||
hoverIgnore?: string;
|
||||
dragSelector?: string;
|
||||
resizerSelector?: string;
|
||||
rootDragClass?: string;
|
||||
context?: T;
|
||||
onMoved?(this: T | undefined, rect: BoxRect): void;
|
||||
onResizeStart?(this: T | undefined): void;
|
||||
onResized?(this: T | undefined, rect: BoxRect): void;
|
||||
onSnap?(this: T | undefined, guides: SnapLine[]): void;
|
||||
}
|
||||
|
||||
export type MarkRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
|
||||
|
||||
export interface NotifyOption {
|
||||
@@ -31,17 +65,13 @@ export interface PopupRectParam {
|
||||
}
|
||||
|
||||
export interface Popup {
|
||||
constructor(opts?: PopupOptions): void;
|
||||
readonly container: HTMLElement | undefined;
|
||||
|
||||
get container(): HTMLElement;
|
||||
title: string | HTMLElement;
|
||||
|
||||
get title(): string;
|
||||
set title(title: string);
|
||||
loading: boolean;
|
||||
|
||||
get loading(): boolean;
|
||||
set loading(flag: boolean);
|
||||
|
||||
get rect(): PopupRectParam | null;
|
||||
get rect(): PopupRectParam | undefined;
|
||||
set rect(r: PopupRectParam);
|
||||
|
||||
close(result?: any, animation?: boolean): void;
|
||||
@@ -52,10 +82,10 @@ export interface Popup {
|
||||
export interface PopupButton {
|
||||
className?: string;
|
||||
tabIndex?: number;
|
||||
key: string;
|
||||
key?: string;
|
||||
text: string;
|
||||
isPrimary?: boolean;
|
||||
trigger: (this: Popup) => boolean | Promise<boolean>;
|
||||
trigger?: (popup: Popup, button?: PopupButton) => boolean | Promise<boolean> | string | number;
|
||||
}
|
||||
|
||||
export interface PopupOptions {
|
||||
@@ -123,6 +153,13 @@ export interface PopupOptions {
|
||||
resolve?: (this: Popup, result: { result: any, popup: Popup }) => void;
|
||||
}
|
||||
|
||||
export interface InputOptions {
|
||||
tabIndex?: number;
|
||||
default?: string;
|
||||
placeholder?: string;
|
||||
[key: string]: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 复选框选项
|
||||
*/
|
||||
@@ -162,3 +199,88 @@ export interface CheckboxOptions {
|
||||
*/
|
||||
onchange?: (this: HTMLInputElement, ev: Event) => any;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 数据项基础类型
|
||||
*/
|
||||
export interface DropdownItemBase {
|
||||
__checked?: 0 | 1 | 2 | undefined;
|
||||
__p?: string;
|
||||
__level?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据项类型
|
||||
*/
|
||||
export type DropdownItem = DropdownItemBase & Record<string, any>;
|
||||
|
||||
|
||||
export interface Dropdown {
|
||||
sourceFilter?: (this: Dropdown) => DropdownItem[];
|
||||
onSelectedList?: (this: Dropdown, list: DropdownItem[]) => void;
|
||||
onSelected?: (this: Dropdown, it?: DropdownItem) => void;
|
||||
onExpanded?: (this: Dropdown) => void;
|
||||
onCollapsed?: (this: Dropdown) => void;
|
||||
|
||||
create(): HTMLDivElement;
|
||||
|
||||
readonly multiSelect?: boolean;
|
||||
readonly ignoreAll?: boolean;
|
||||
disabled: boolean;
|
||||
source: DropdownItem[];
|
||||
readonly selected?: DropdownItem;
|
||||
readonly selectedList: DropdownItem[];
|
||||
|
||||
select(selected?: string, silence?: boolean, ignoreCase?: boolean): boolean | undefined;
|
||||
selectlist(selectedlist: string[], silence: boolean): boolean | undefined;
|
||||
}
|
||||
|
||||
/** 下拉列表参数对象 */
|
||||
export interface DropdownOptions {
|
||||
/** 文本关键字 */
|
||||
textKey?: string;
|
||||
/** 值关键字 */
|
||||
valueKey?: string;
|
||||
/** 源码显示的关键字 */
|
||||
htmlKey?: string;
|
||||
/** 子集合的关键字 */
|
||||
childKey?: string;
|
||||
/** 最大输入长度 */
|
||||
maxLength?: number;
|
||||
/**
|
||||
* 模板创建函数
|
||||
* @param this 当前Dropdown
|
||||
* @param it 下拉项目
|
||||
* @returns 返回渲染的字符串
|
||||
*/
|
||||
htmlTemplate?: (this: Dropdown, it: DropdownItem) => string;
|
||||
/** 是否允许多选 */
|
||||
multiSelect?: boolean;
|
||||
/** 选中值 */
|
||||
selected?: string;
|
||||
/** 选中的数组 */
|
||||
selectedList?: string[];
|
||||
/** 是否禁用 */
|
||||
disabled?: boolean;
|
||||
/** 是否支持输入 */
|
||||
input?: boolean;
|
||||
/** 是否支持搜索 */
|
||||
search?: boolean;
|
||||
/** 搜索的关键字数组 */
|
||||
searchKeys?: string[];
|
||||
/** 搜索提示文本,默认值取语言资源 `searchHolder` "Search..." */
|
||||
searchPlaceholder?: string;
|
||||
/** 焦点索引 */
|
||||
tabIndex?: number;
|
||||
/** 输入框的提示文本 */
|
||||
placeholder?: string;
|
||||
/** 是否固定为向下展开 */
|
||||
slideFixed?: boolean;
|
||||
/** 父元素,默认添加到头元素之后 */
|
||||
wrapper?: HTMLElement;
|
||||
/** 是否忽略 All 选项 */
|
||||
ignoreAll?: boolean;
|
||||
/** 获取文本的代理函数 */
|
||||
getText?: GetTextCallback;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import './ui/css/variables/definition.scss';
|
||||
import './ui/css/common.scss';
|
||||
import { createElement } from "./functions";
|
||||
import { appendResizer, createElement, makeMovable } from "./functions";
|
||||
import { createIcon, changeIcon, resolveIcon } from "./ui/icon";
|
||||
import { createCheckbox, createRadiobox, resolveCheckbox } from "./ui/checkbox";
|
||||
import { setTooltip, resolveTooltip } from "./ui/tooltip";
|
||||
@@ -170,6 +170,8 @@ function notify(opts) {
|
||||
|
||||
export {
|
||||
createElement,
|
||||
makeMovable,
|
||||
appendResizer,
|
||||
// icon
|
||||
createIcon,
|
||||
changeIcon,
|
||||
|
||||
@@ -124,4 +124,104 @@
|
||||
.ui-notify-message {
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
}
|
||||
|
||||
.drag-box {
|
||||
position: relative;
|
||||
|
||||
.drag-hover {
|
||||
cursor: move;
|
||||
touch-action: none;
|
||||
}
|
||||
|
||||
.resizer {
|
||||
position: absolute;
|
||||
touch-action: none;
|
||||
|
||||
&[data-dir="n"],
|
||||
&[data-dir="s"] {
|
||||
width: 100%;
|
||||
height: 7px;
|
||||
left: 0;
|
||||
cursor: ns-resize;
|
||||
}
|
||||
|
||||
&[data-dir="w"],
|
||||
&[data-dir="e"] {
|
||||
width: 7px;
|
||||
height: 100%;
|
||||
top: 0;
|
||||
cursor: ew-resize;
|
||||
}
|
||||
|
||||
&[data-dir="nw"],
|
||||
&[data-dir="se"],
|
||||
&[data-dir="ne"],
|
||||
&[data-dir="sw"] {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
}
|
||||
|
||||
&[data-dir="nw"],
|
||||
&[data-dir="se"] {
|
||||
cursor: nwse-resize;
|
||||
}
|
||||
|
||||
&[data-dir="ne"],
|
||||
&[data-dir="sw"] {
|
||||
cursor: nesw-resize;
|
||||
}
|
||||
|
||||
&[data-dir="w"],
|
||||
&[data-dir="nw"],
|
||||
&[data-dir="sw"] {
|
||||
left: -4px;
|
||||
}
|
||||
|
||||
&[data-dir="e"],
|
||||
&[data-dir="ne"],
|
||||
&[data-dir="se"] {
|
||||
right: -4px;
|
||||
}
|
||||
|
||||
&[data-dir="n"],
|
||||
&[data-dir="nw"],
|
||||
&[data-dir="ne"] {
|
||||
top: -4px;
|
||||
}
|
||||
|
||||
&[data-dir="s"],
|
||||
&[data-dir="sw"],
|
||||
&[data-dir="se"] {
|
||||
bottom: -4px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.snap-line {
|
||||
position: absolute;
|
||||
pointer-events: none;
|
||||
z-index: 9999;
|
||||
display: none;
|
||||
|
||||
&.line-x {
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
width: 1px;
|
||||
border-left: 1px dashed var(--red-color);
|
||||
}
|
||||
|
||||
&.line-y {
|
||||
left: 0;
|
||||
right: 0;
|
||||
top: 0;
|
||||
height: 1px;
|
||||
border-top: 1px dashed var(--red-color);
|
||||
}
|
||||
}
|
||||
|
||||
html.is-interacting,
|
||||
html.is-interacting * {
|
||||
user-select: none !important;
|
||||
}
|
||||
+1
-72
@@ -66,15 +66,6 @@ $buttonHeight: 28px;
|
||||
padding: 5px 0 3px 12px;
|
||||
}
|
||||
|
||||
.ui-popup-header-title,
|
||||
.ui-popup-move {
|
||||
cursor: move;
|
||||
}
|
||||
|
||||
.ui-popup-header-title.no-move {
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
>.ui-popup-header-icons {
|
||||
flex: 0 0 auto;
|
||||
padding: 5px 12px 3px 0;
|
||||
@@ -251,75 +242,13 @@ $buttonHeight: 28px;
|
||||
}
|
||||
}
|
||||
|
||||
.ui-popup-border {
|
||||
position: absolute;
|
||||
|
||||
&.ui-popup-border-left,
|
||||
&.ui-popup-border-right {
|
||||
width: 6px;
|
||||
height: 100%;
|
||||
top: 0;
|
||||
cursor: ew-resize;
|
||||
}
|
||||
|
||||
&.ui-popup-border-top,
|
||||
&.ui-popup-border-bottom {
|
||||
width: 100%;
|
||||
height: 6px;
|
||||
left: 0;
|
||||
cursor: ns-resize;
|
||||
}
|
||||
|
||||
&.ui-popup-border-top-left,
|
||||
&.ui-popup-border-top-right,
|
||||
&.ui-popup-border-bottom-right,
|
||||
&.ui-popup-border-bottom-left {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
}
|
||||
|
||||
&.ui-popup-border-top-left,
|
||||
&.ui-popup-border-bottom-right {
|
||||
cursor: nwse-resize;
|
||||
}
|
||||
|
||||
&.ui-popup-border-top-right,
|
||||
&.ui-popup-border-bottom-left {
|
||||
cursor: nesw-resize;
|
||||
}
|
||||
|
||||
&.ui-popup-border-left,
|
||||
&.ui-popup-border-top-left,
|
||||
&.ui-popup-border-bottom-left {
|
||||
left: -4px;
|
||||
}
|
||||
|
||||
&.ui-popup-border-right,
|
||||
&.ui-popup-border-top-right,
|
||||
&.ui-popup-border-bottom-right {
|
||||
right: -4px;
|
||||
}
|
||||
|
||||
&.ui-popup-border-top,
|
||||
&.ui-popup-border-top-left,
|
||||
&.ui-popup-border-top-right {
|
||||
top: -4px;
|
||||
}
|
||||
|
||||
&.ui-popup-border-bottom,
|
||||
&.ui-popup-border-bottom-right,
|
||||
&.ui-popup-border-bottom-left {
|
||||
bottom: -4px;
|
||||
}
|
||||
}
|
||||
|
||||
&.ui-popup-collapse {
|
||||
min-height: 40px;
|
||||
min-width: 160px;
|
||||
|
||||
.ui-popup-body,
|
||||
.ui-popup-footer,
|
||||
.ui-popup-border {
|
||||
.resizer {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -419,7 +419,6 @@ export class DateSelector {
|
||||
|
||||
/**
|
||||
* 获取日期选择框元素
|
||||
* @readonly
|
||||
* @type {HTMLInputElement | undefined}
|
||||
*/
|
||||
get element() { return this._var.el }
|
||||
|
||||
+11
-24
@@ -1,12 +1,12 @@
|
||||
import './css/dropdown.scss';
|
||||
import { r as lang } from "../utility/lgres";
|
||||
import { contains, nullOrEmpty } from "../utility/strings";
|
||||
import { mergeOptions, global, isPositive, throttle } from "../utility";
|
||||
import { createElement } from "../functions";
|
||||
import { global, isPositive, throttle } from "../utility";
|
||||
import { createElement, mergeOptions } from "../functions";
|
||||
import { createCheckbox } from "./checkbox";
|
||||
import { createIcon } from "./icon"
|
||||
|
||||
/** @import { DropdownOptions } from './dropdown.types' */
|
||||
/** @import { DropdownOptions, DropdownItem, Dropdown as IDropdown, MarkRequired } from '../types' */
|
||||
|
||||
/** @satisfies {Partial<DropdownOptions>} */
|
||||
const DEFAULT_OPTIONS = {
|
||||
@@ -17,7 +17,7 @@ const DEFAULT_OPTIONS = {
|
||||
maxLength: 500
|
||||
};
|
||||
|
||||
/** @typedef {import('../types').MarkRequired<DropdownOptions, keyof typeof DEFAULT_OPTIONS>} ResolvedDropdownOptions */
|
||||
/** @typedef {MarkRequired<DropdownOptions, keyof typeof DEFAULT_OPTIONS>} ResolvedDropdownOptions */
|
||||
|
||||
const SymbolDropdown = Symbol.for('ui-dropdown');
|
||||
const DropdownItemHeight = 30;
|
||||
@@ -142,19 +142,8 @@ function getValue(it, valuekey, textkey) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据项基础类型
|
||||
* @typedef DropdownItemBase
|
||||
* @property {0 | 1 | 2 | undefined} [__checked] 是否选中
|
||||
* @property {string} [__p] 父节点 Id
|
||||
* @property {number} [__level] 层级
|
||||
* @interface
|
||||
* @implements {IDropdown}
|
||||
*/
|
||||
|
||||
/**
|
||||
* 数据项类型
|
||||
* @typedef {DropdownItemBase & Object.<string, any>} DropdownItem
|
||||
*/
|
||||
|
||||
export class Dropdown {
|
||||
_var = {
|
||||
/** @type {ResolvedDropdownOptions} */
|
||||
@@ -180,24 +169,22 @@ export class Dropdown {
|
||||
dropHeight: 0
|
||||
};
|
||||
|
||||
/** @type {(() => DropdownItem[]) | undefined} */
|
||||
/** @type {((this: IDropdown) => DropdownItem[]) | undefined} */
|
||||
sourceFilter;
|
||||
/** @type {((list: DropdownItem[]) => void) | undefined} */
|
||||
/** @type {((this: IDropdown, list: DropdownItem[]) => void) | undefined} */
|
||||
onSelectedList;
|
||||
/** @type {((it: DropdownItem | undefined) => void) | undefined} */
|
||||
/** @type {((this: IDropdown, it: DropdownItem | undefined) => void) | undefined} */
|
||||
onSelected;
|
||||
/** @type {((this: Dropdown) => void) | undefined} */
|
||||
/** @type {((this: IDropdown) => void) | undefined} */
|
||||
onExpanded;
|
||||
/** @type {((this: Dropdown) => void) | undefined} */
|
||||
/** @type {((this: IDropdown) => void) | undefined} */
|
||||
onCollapsed;
|
||||
|
||||
/**
|
||||
* @param {DropdownOptions} [options]
|
||||
*/
|
||||
constructor(options) {
|
||||
if (options != null) {
|
||||
this._var.options = mergeOptions(DEFAULT_OPTIONS, options);
|
||||
}
|
||||
this._var.options = mergeOptions(DEFAULT_OPTIONS, options);
|
||||
const opts = this._var.options;
|
||||
const getText = opts.getText;
|
||||
if (typeof getText === 'function') {
|
||||
|
||||
Vendored
-50
@@ -1,50 +0,0 @@
|
||||
import type { GetTextCallback } from '../types';
|
||||
|
||||
/** 下拉列表参数对象 */
|
||||
interface DropdownOptions {
|
||||
/** 文本关键字 */
|
||||
textKey?: string;
|
||||
/** 值关键字 */
|
||||
valueKey?: string;
|
||||
/** 源码显示的关键字 */
|
||||
htmlKey?: string;
|
||||
/** 子集合的关键字 */
|
||||
childKey?: string;
|
||||
/** 最大输入长度 */
|
||||
maxLength?: number;
|
||||
/**
|
||||
* 模板创建函数
|
||||
* @param this 当前Dropdown
|
||||
* @param it 下拉项目
|
||||
* @returns 返回渲染的字符串
|
||||
*/
|
||||
htmlTemplate?: (this: Dropdown, it: DropdownItem) => string;
|
||||
/** 是否允许多选 */
|
||||
multiSelect?: boolean;
|
||||
/** 选中值 */
|
||||
selected?: string;
|
||||
/** 选中的数组 */
|
||||
selectedList?: string[];
|
||||
/** 是否禁用 */
|
||||
disabled?: boolean;
|
||||
/** 是否支持输入 */
|
||||
input?: boolean;
|
||||
/** 是否支持搜索 */
|
||||
search?: boolean;
|
||||
/** 搜索的关键字数组 */
|
||||
searchKeys?: string[];
|
||||
/** 搜索提示文本,默认值取语言资源 `searchHolder` "Search..." */
|
||||
searchPlaceholder?: string;
|
||||
/** 焦点索引 */
|
||||
tabIndex?: number;
|
||||
/** 输入框的提示文本 */
|
||||
placeholder?: string;
|
||||
/** 是否固定为向下展开 */
|
||||
slideFixed?: boolean;
|
||||
/** 父元素,默认添加到头元素之后 */
|
||||
wrapper?: HTMLElement;
|
||||
/** 是否忽略 All 选项 */
|
||||
ignoreAll?: boolean;
|
||||
/** 获取文本的代理函数 */
|
||||
getText?: GetTextCallback;
|
||||
}
|
||||
@@ -1125,7 +1125,6 @@ export class Grid {
|
||||
|
||||
/**
|
||||
* 列类型枚举
|
||||
* @readonly
|
||||
* @type {GridColumnTypeEnum}
|
||||
* @property {number} Common=0 - 通用列(只读)
|
||||
* @property {number} Input=1 - 单行文本列
|
||||
@@ -1179,7 +1178,6 @@ export class Grid {
|
||||
|
||||
/**
|
||||
* 获取全局是否可支持点击扩展
|
||||
* @readonly
|
||||
* @type {boolean}
|
||||
*/
|
||||
get isExpandable() {
|
||||
@@ -1192,14 +1190,12 @@ export class Grid {
|
||||
|
||||
/**
|
||||
* 获取 Grid 的页面元素
|
||||
* @readonly
|
||||
* @type {HTMLDivElement}
|
||||
*/
|
||||
get element() { return this._var.el }
|
||||
|
||||
/**
|
||||
* 获取当前 Grid 是否已发生改变
|
||||
* @readonly
|
||||
* @type {boolean}
|
||||
*/
|
||||
get changed() {
|
||||
@@ -1212,14 +1208,12 @@ export class Grid {
|
||||
|
||||
/**
|
||||
* 返回所有数据的数据(未过滤)
|
||||
* @readonly
|
||||
* @type {GridRowItem[]}
|
||||
*/
|
||||
get allSource() { return this._var.source?.map(s => s.values) }
|
||||
|
||||
/**
|
||||
* 获取已过滤的数据中的扩展对象数组
|
||||
* @readonly
|
||||
* @type {GridExpandableObject[]}
|
||||
* @property {HTMLElement} element - 扩展行元素
|
||||
*/
|
||||
@@ -1227,14 +1221,12 @@ export class Grid {
|
||||
|
||||
/**
|
||||
* 获取当前是否为虚模式状态
|
||||
* @readonly
|
||||
* @type {boolean}
|
||||
*/
|
||||
get virtual() { return this._var.currentSource?.length > this.virtualCount }
|
||||
|
||||
/**
|
||||
* 获取当前排序的列关键字,为 null 则当前无排序列
|
||||
* @readonly
|
||||
* @type {string | null}
|
||||
*/
|
||||
get sortKey() {
|
||||
@@ -1246,7 +1238,6 @@ export class Grid {
|
||||
|
||||
/**
|
||||
* 获取当前选中的行对象
|
||||
* @readonly
|
||||
* @type {GridRowItem | null}
|
||||
* @since 1.0.7
|
||||
*/
|
||||
@@ -1280,14 +1271,12 @@ export class Grid {
|
||||
|
||||
/**
|
||||
* 获取虚模式起始索引
|
||||
* @readonly
|
||||
* @type {number}
|
||||
*/
|
||||
get startIndex() { return this._var.startIndex }
|
||||
|
||||
/**
|
||||
* 获取当前选中行的索引,为 -1 则当前没有选中行
|
||||
* @readonly
|
||||
* @type {number}
|
||||
*/
|
||||
get selectedIndex() { return (this._var.selectedIndexes && this._var.selectedIndexes[0]) ?? -1 }
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ function createUse(type, id) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {typeof SVGElement} K
|
||||
* @template {Element} K
|
||||
* @param {K} svg
|
||||
* @param {string} type
|
||||
* @param {string} id
|
||||
|
||||
+142
-246
@@ -2,36 +2,14 @@ import "./css/popup.scss";
|
||||
import { r as lang } from "../utility/lgres";
|
||||
import { nullOrEmpty } from "../utility/strings";
|
||||
import { global } from "../utility";
|
||||
import { createElement } from "../functions";
|
||||
import { appendResizer, createElement, makeMovable, mergeOptions } from "../functions";
|
||||
import { createIcon, changeIcon } from "./icon";
|
||||
import { requestAnimationFrame } from "../ui";
|
||||
|
||||
/** @import { PopupOptions, PopupRectParam } from '../types' */
|
||||
|
||||
const ResizeMods = {
|
||||
right: 1,
|
||||
bottom: 2,
|
||||
left: 4,
|
||||
top: 8,
|
||||
bottomRight: 2 | 1,
|
||||
bottomLeft: 2 | 4,
|
||||
topRight: 8 | 1,
|
||||
topLeft: 8 | 4
|
||||
}
|
||||
/** @import { PopupOptions, PopupRectParam, PopupButton, Popup as IPopup, MarkRequired, InputOptions } from '../types' */
|
||||
|
||||
const IconStyle = 'fa-light';
|
||||
|
||||
// const Cursors = {
|
||||
// [ResizeMods.right]: 'ew-resize',
|
||||
// [ResizeMods.bottom]: 'ns-resize',
|
||||
// [ResizeMods.bottomRight]: 'nwse-resize',
|
||||
// [ResizeMods.left]: 'ew-resize',
|
||||
// [ResizeMods.bottomLeft]: 'nesw-resize',
|
||||
// [ResizeMods.top]: 'ns-resize',
|
||||
// [ResizeMods.topRight]: 'nesw-resize',
|
||||
// [ResizeMods.topLeft]: 'nwse-resize'
|
||||
// }
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {string} px
|
||||
@@ -49,28 +27,47 @@ function trimPx(px) {
|
||||
}
|
||||
}
|
||||
|
||||
/** @type {import('../types').Popup} */
|
||||
/** @satisfies {Partial<PopupOptions>} */
|
||||
const DEFAULT_OPTIONS = {
|
||||
content: '',
|
||||
title: '',
|
||||
persistent: false,
|
||||
mask: true,
|
||||
changeZIndex: false,
|
||||
closable: true,
|
||||
movable: true,
|
||||
resizable: false,
|
||||
minWidth: 200,
|
||||
minHeight: 200,
|
||||
collapsable: false
|
||||
};
|
||||
|
||||
/** @typedef {MarkRequired<PopupOptions, keyof typeof DEFAULT_OPTIONS>} ResolvedPopupOptions */
|
||||
|
||||
/**
|
||||
* @implements {IPopup}
|
||||
*/
|
||||
export class Popup {
|
||||
_var = {
|
||||
/** @type {HTMLElement | undefined} */
|
||||
mask: undefined,
|
||||
/** @type {PopupOptions | undefined} */
|
||||
option: undefined,
|
||||
/** @type {ResolvedPopupOptions} */
|
||||
option: DEFAULT_OPTIONS,
|
||||
/** @type {PopupRectParam | undefined} */
|
||||
bounds: undefined,
|
||||
cursor: undefined
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {PopupOptions} [opts]
|
||||
* @param {PopupOptions} opts
|
||||
*/
|
||||
constructor(opts) {
|
||||
this._var.option = opts;
|
||||
this._var.option = mergeOptions(DEFAULT_OPTIONS, opts);
|
||||
}
|
||||
|
||||
get container() { return /** @type {HTMLElement | undefined} */ (this._var.mask?.querySelector('.ui-popup-container')) }
|
||||
|
||||
get title() { return this._var.option?.title ?? ''; }
|
||||
get title() { return this._var.option.title; }
|
||||
set title(title) {
|
||||
const element = this._var.mask?.querySelector('.ui-popup-container .ui-popup-header .ui-popup-header-title');
|
||||
if (element instanceof HTMLElement) {
|
||||
@@ -80,9 +77,7 @@ export class Popup {
|
||||
element.replaceChildren(title);
|
||||
}
|
||||
}
|
||||
if (this._var.option != null) {
|
||||
this._var.option.title = title;
|
||||
}
|
||||
this._var.option.title = title;
|
||||
}
|
||||
|
||||
_getLoadingPanel() {
|
||||
@@ -166,11 +161,15 @@ export class Popup {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} [result]
|
||||
* @param {boolean} [animation]
|
||||
*/
|
||||
close(result = null, animation = true) {
|
||||
const option = this._var.option;
|
||||
const mask = this._var.mask;
|
||||
const doClose = () => {
|
||||
if (option?.persistent) {
|
||||
if (option.persistent) {
|
||||
if (mask != null) {
|
||||
mask.style.display = 'none';
|
||||
}
|
||||
@@ -188,10 +187,10 @@ export class Popup {
|
||||
} else {
|
||||
doClose();
|
||||
}
|
||||
if (typeof option?.onMasking === 'function') {
|
||||
if (typeof option.onMasking === 'function') {
|
||||
option.onMasking.call(this, false);
|
||||
}
|
||||
if (typeof option?.resolve === 'function') {
|
||||
if (typeof option.resolve === 'function') {
|
||||
option.resolve.call(this, {
|
||||
result,
|
||||
popup: this
|
||||
@@ -206,20 +205,24 @@ export class Popup {
|
||||
create() {
|
||||
const mask = createElement('div', 'ui-popup-mask ui-popup-active');
|
||||
const option = this._var.option;
|
||||
if (option?.mask === false) {
|
||||
if (!option.mask) {
|
||||
mask.classList.add('ui-popup-transparent');
|
||||
} else if (typeof option?.onMasking === 'function') {
|
||||
} else if (typeof option.onMasking === 'function') {
|
||||
option.onMasking.call(this, true);
|
||||
}
|
||||
if (option?.zIndex != null && !isNaN(option.zIndex)) {
|
||||
if (option.zIndex != null && !isNaN(option.zIndex)) {
|
||||
mask.style.zIndex = String(option.zIndex);
|
||||
}
|
||||
const container = createElement('div', 'ui-popup-container');
|
||||
if (option?.changeZIndex === true) {
|
||||
const container = createElement('div', 'ui-popup-container drag-box');
|
||||
if (option.changeZIndex) {
|
||||
container.addEventListener('mousedown', () => {
|
||||
const masks = [...this._var.mask?.parentElement?.children].filter(e => e.classList.contains('ui-popup-mask'));
|
||||
const children = this._var.mask?.parentElement?.children;
|
||||
if (children == null) {
|
||||
return;
|
||||
}
|
||||
const masks = [...children].filter(e => e.classList.contains('ui-popup-mask'));
|
||||
let max = 200;
|
||||
masks.forEach(m => {
|
||||
(/** @type {HTMLElement[]} */ (masks)).forEach(m => {
|
||||
let index;
|
||||
if (m.dataset.zindex != null) {
|
||||
index = parseInt(m.dataset.zindex);
|
||||
@@ -243,63 +246,27 @@ export class Popup {
|
||||
tabIndex = 0;
|
||||
}
|
||||
container.tabIndex = tabIndex + 1;
|
||||
let content = option?.content;
|
||||
let content = option.content;
|
||||
if (!(content instanceof HTMLElement)) {
|
||||
content = createElement('div', d => d.innerText = content);
|
||||
content = createElement('div', d => d.innerText = (/** @type {string} */ (content)));
|
||||
}
|
||||
container.append(
|
||||
createElement('div', header => {
|
||||
header.className = 'ui-popup-header';
|
||||
let title = option?.title;
|
||||
let title = option.title;
|
||||
if (!(title instanceof HTMLElement)) {
|
||||
title = createElement('div', t => {
|
||||
if (option?.movable === false) {
|
||||
t.className = 'ui-popup-header-title no-move';
|
||||
} else {
|
||||
t.className = 'ui-popup-header-title';
|
||||
}
|
||||
t.innerText = title;
|
||||
t.className = 'ui-popup-header-title';
|
||||
t.innerText = (/** @type {string} */ (title));
|
||||
});
|
||||
}
|
||||
header.appendChild(title);
|
||||
if (option?.movable !== false) {
|
||||
const move = header; // title.querySelector('.ui-popup-move') ?? title;
|
||||
move.addEventListener('mousedown', e => {
|
||||
if (['svg', 'use'].includes(e.target?.tagName)) {
|
||||
return;
|
||||
}
|
||||
if (e.buttons !== 1) {
|
||||
return;
|
||||
}
|
||||
const parent = option?.mask === false ? mask.parentElement : mask;
|
||||
const x = e.clientX - container.offsetLeft;
|
||||
const y = e.clientY - container.offsetTop;
|
||||
/** @type {boolean} */
|
||||
let moved;
|
||||
const move = e => {
|
||||
if (e.buttons === 1) {
|
||||
container.style.left = `${e.clientX - x}px`;
|
||||
container.style.top = `${e.clientY - y}px`;
|
||||
moved = true;
|
||||
} else {
|
||||
parent.dispatchEvent(new MouseEvent('mouseup'));
|
||||
}
|
||||
};
|
||||
parent.addEventListener('mousemove', move, { passive: false });
|
||||
const up = () => {
|
||||
parent.removeEventListener('mousemove', move, { passive: false });
|
||||
parent.removeEventListener('mouseup', up);
|
||||
if (moved === true && typeof option?.onMoveEnded === 'function') {
|
||||
option.onMoveEnded.call(this);
|
||||
}
|
||||
moved = false;
|
||||
};
|
||||
parent.addEventListener('mouseup', up);
|
||||
});
|
||||
if (option.movable) {
|
||||
header.classList.add('drag-hover');
|
||||
}
|
||||
const icons = createElement('div', icons => {
|
||||
icons.className = 'ui-popup-header-icons';
|
||||
if (option?.collapsable === true) {
|
||||
if (option.collapsable) {
|
||||
const collapse = createIcon(IconStyle, 'compress-alt');
|
||||
collapse.tabIndex = tabIndex + 2;
|
||||
collapse.classList.add('icon-expand');
|
||||
@@ -324,13 +291,13 @@ export class Popup {
|
||||
container.classList.add('ui-popup-collapse');
|
||||
changeIcon(collapse, IconStyle, 'expand-alt');
|
||||
}
|
||||
if (typeof option?.onResizeEnded === 'function') {
|
||||
if (typeof option.onResizeEnded === 'function') {
|
||||
option.onResizeEnded.call(this);
|
||||
}
|
||||
});
|
||||
icons.appendChild(collapse);
|
||||
}
|
||||
if (option?.closable !== false) {
|
||||
if (option.closable) {
|
||||
const cancel = createIcon(IconStyle, 'times');
|
||||
cancel.tabIndex = tabIndex + 3;
|
||||
cancel.addEventListener('keypress', e => {
|
||||
@@ -348,7 +315,7 @@ export class Popup {
|
||||
createElement('div', null, createIcon(IconStyle, 'spinner-third'))
|
||||
))
|
||||
);
|
||||
if (Array.isArray(option?.buttons) && option.buttons.length > 0) {
|
||||
if (Array.isArray(option.buttons) && option.buttons.length > 0) {
|
||||
tabIndex = Math.max.apply(null, [...container.querySelectorAll('[tabindex]')].map(e => e.tabIndex ?? 0));
|
||||
container.appendChild(
|
||||
createElement('div', 'ui-popup-footer', ...option.buttons.map((b, i) => {
|
||||
@@ -368,14 +335,14 @@ export class Popup {
|
||||
button.addEventListener('click', () => {
|
||||
if (typeof b.trigger === 'function') {
|
||||
const result = b.trigger(this);
|
||||
if (typeof result?.then === 'function') {
|
||||
if (typeof result === 'boolean' || typeof result === 'string' || typeof result === 'number') {
|
||||
this.close(result);
|
||||
} else if (typeof result?.then === 'function') {
|
||||
result.then(r => {
|
||||
if (r !== false) {
|
||||
this.close(r);
|
||||
}
|
||||
}).catch(reason => console.warn(reason));
|
||||
} else if (result !== false) {
|
||||
this.close(result);
|
||||
}
|
||||
} else {
|
||||
this.close(b.key ?? i);
|
||||
@@ -387,10 +354,12 @@ export class Popup {
|
||||
const tabs = [...container.querySelectorAll('[tabindex]')].map(e => e.tabIndex ?? 0);
|
||||
const tabMin = Math.min.apply(null, tabs);
|
||||
const tabMax = Math.max.apply(null, tabs);
|
||||
/** @type {HTMLElement | null} */
|
||||
const last = container.querySelector(`[tabindex="${tabMax}"]`);
|
||||
if (last != null) {
|
||||
last.addEventListener('keydown', e => {
|
||||
if (e.key === 'Tab') {
|
||||
/** @type {HTMLElement | null} */
|
||||
const first = container.querySelector(`[tabindex="${tabMin}"]`);
|
||||
first?.focus();
|
||||
e.preventDefault();
|
||||
@@ -398,50 +367,34 @@ export class Popup {
|
||||
});
|
||||
}
|
||||
} else {
|
||||
container.querySelector('.ui-popup-body>.ui-popup-loading').classList.add('ui-popup-loading-content');
|
||||
container.querySelector('.ui-popup-body>.ui-popup-loading')?.classList.add('ui-popup-loading-content');
|
||||
}
|
||||
// resizable
|
||||
if (option?.resizable === true) {
|
||||
container.append(
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'ui-popup-border ui-popup-border-right';
|
||||
layer.addEventListener('mousedown', e => this._resize(ResizeMods.right, e));
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'ui-popup-border ui-popup-border-bottom';
|
||||
layer.addEventListener('mousedown', e => this._resize(ResizeMods.bottom, e));
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'ui-popup-border ui-popup-border-left';
|
||||
layer.addEventListener('mousedown', e => this._resize(ResizeMods.left, e));
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'ui-popup-border ui-popup-border-top';
|
||||
layer.addEventListener('mousedown', e => this._resize(ResizeMods.top, e));
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'ui-popup-border ui-popup-border-bottom-right';
|
||||
layer.addEventListener('mousedown', e => this._resize(ResizeMods.bottomRight, e));
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'ui-popup-border ui-popup-border-bottom-left';
|
||||
layer.addEventListener('mousedown', e => this._resize(ResizeMods.bottomLeft, e));
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'ui-popup-border ui-popup-border-top-left';
|
||||
layer.addEventListener('mousedown', e => this._resize(ResizeMods.topLeft, e));
|
||||
}),
|
||||
createElement('layer', layer => {
|
||||
layer.className = 'ui-popup-border ui-popup-border-top-right';
|
||||
layer.addEventListener('mousedown', e => this._resize(ResizeMods.topRight, e));
|
||||
})
|
||||
)
|
||||
if (option.resizable) {
|
||||
appendResizer(container);
|
||||
}
|
||||
if (option.movable || option.resizable) {
|
||||
const parent = !option.mask ? document.body : mask;
|
||||
makeMovable(parent, {
|
||||
movable: option.movable,
|
||||
resizable: option.resizable,
|
||||
context: this,
|
||||
onMoved: option.onMoveEnded,
|
||||
onResizeStart: option.onResizeStarted,
|
||||
onResized: option.onResizeEnded
|
||||
});
|
||||
}
|
||||
|
||||
mask.appendChild(container);
|
||||
this._var.mask = mask;
|
||||
return mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} parent
|
||||
* @param {boolean} hidden
|
||||
* @returns {Promise<HTMLElement> | undefined}
|
||||
*/
|
||||
show(parent = document.body, hidden = false) {
|
||||
if (parent == null) {
|
||||
return;
|
||||
@@ -469,7 +422,7 @@ export class Popup {
|
||||
return Promise.resolve(mask);
|
||||
}
|
||||
}
|
||||
if (this._var.option?.mask === false) {
|
||||
if (!this._var.option.mask) {
|
||||
// calculator position
|
||||
const container = this.container;
|
||||
if (container != null) {
|
||||
@@ -487,97 +440,14 @@ export class Popup {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
_resize(mod, e) {
|
||||
if (e.buttons !== 1) {
|
||||
return;
|
||||
}
|
||||
const container = this.container;
|
||||
if (container == null) {
|
||||
return;
|
||||
}
|
||||
const option = this._var.option;
|
||||
if (typeof option?.onResizeStarted === 'function') {
|
||||
option.onResizeStarted.call(this);
|
||||
}
|
||||
const mask = this._var.mask;
|
||||
// this._var.cursor = mask.style.cursor;
|
||||
// mask.style.cursor = Cursors[mod];
|
||||
const originalX = e.clientX;
|
||||
const originalY = e.clientY;
|
||||
const original = {
|
||||
width: container.offsetWidth,
|
||||
height: container.offsetHeight,
|
||||
left: container.offsetLeft,
|
||||
top: container.offsetTop
|
||||
};
|
||||
const minWidth = option?.minWidth ?? 200;
|
||||
const minHeight = option?.minHeight ?? 200;
|
||||
/** @type {boolean} */
|
||||
let resized;
|
||||
const parent = option?.mask === false ? mask.parentElement : mask;
|
||||
const move = e => {
|
||||
if (e.buttons !== 1) {
|
||||
parent?.dispatchEvent(new MouseEvent('mouseup'));
|
||||
return;
|
||||
}
|
||||
const offsetX = e.clientX - originalX;
|
||||
const offsetY = e.clientY - originalY;
|
||||
let width = original.width;
|
||||
let height = original.height;
|
||||
let x = original.left;
|
||||
let y = original.top;
|
||||
if ((mod & ResizeMods.right) === ResizeMods.right) {
|
||||
width += offsetX;
|
||||
if (width < minWidth) {
|
||||
width = minWidth;
|
||||
}
|
||||
}
|
||||
if ((mod & ResizeMods.bottom) === ResizeMods.bottom) {
|
||||
height += offsetY;
|
||||
if (height < minHeight) {
|
||||
height = minHeight;
|
||||
}
|
||||
}
|
||||
if ((mod & ResizeMods.left) === ResizeMods.left) {
|
||||
width -= offsetX;
|
||||
if (width < minWidth) {
|
||||
width = minWidth;
|
||||
x = originalX + original.width - minWidth;
|
||||
} else {
|
||||
x += offsetX;
|
||||
}
|
||||
}
|
||||
if ((mod & ResizeMods.top) === ResizeMods.top) {
|
||||
height -= offsetY;
|
||||
if (height < minHeight) {
|
||||
height = minHeight;
|
||||
y = originalY + original.height - minHeight;
|
||||
} else {
|
||||
y += offsetY;
|
||||
}
|
||||
}
|
||||
if (typeof option?.onResizing === 'function') {
|
||||
option.onResizing.call(this, x, y, width, height);
|
||||
} else {
|
||||
container.style.cssText += `left: ${x}px; top: ${y}px; width: ${width}px; height: ${height}px`;
|
||||
}
|
||||
resized = true;
|
||||
}
|
||||
parent?.addEventListener('mousemove', move, { passive: false });
|
||||
const up = () => {
|
||||
parent.removeEventListener('mousemove', move, { passive: false });
|
||||
parent.removeEventListener('mouseup', up);
|
||||
// mask.style.cursor = this._var.cursor;
|
||||
if (resized === true && typeof option?.onResizeEnded === 'function') {
|
||||
option.onResizeEnded.call(this);
|
||||
}
|
||||
resized = false;
|
||||
};
|
||||
parent?.addEventListener('mouseup', up);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string | HTMLElement} title
|
||||
* @param {string | HTMLElement} content
|
||||
* @param {...PopupButton} buttons
|
||||
* @returns {Popup}
|
||||
*/
|
||||
export function createPopup(title, content, ...buttons) {
|
||||
const popup = new Popup({
|
||||
title,
|
||||
@@ -589,11 +459,11 @@ export function createPopup(title, content, ...buttons) {
|
||||
|
||||
/**
|
||||
* 解析对话框元素
|
||||
* @param {HTMLElement | string} wrapper - 解析该 `.dialog` 元素
|
||||
* @param {HTMLElement | string | null} wrapper - 解析该 `.dialog` 元素
|
||||
* @param {Function} [callback] - 关闭对话框时的回调
|
||||
* @param {boolean} [removable] - 是否可移除
|
||||
* @param {number} [zIndex] - 对话框默认 `z-index`
|
||||
* @returns {Popup} 返回弹出框字典
|
||||
* @returns {Popup | null} 返回弹出框字典
|
||||
*/
|
||||
export function resolvePopup(wrapper, callback, removable, zIndex) {
|
||||
if (typeof wrapper === 'string') {
|
||||
@@ -606,18 +476,20 @@ export function resolvePopup(wrapper, callback, removable, zIndex) {
|
||||
return null;
|
||||
}
|
||||
const title = wrapper.querySelector('.dialog-title>.title')?.innerText;
|
||||
/** @type {HTMLElement | null} */
|
||||
const content = wrapper.querySelector('.dialog-title+div');
|
||||
const buttons = [...wrapper.querySelectorAll('.dialog-func>input[type="button"]')].reverse().map(b => ({
|
||||
const buttons = /** @type {HTMLInputElement[]} */ ([...wrapper.querySelectorAll('.dialog-func>input[type="button"]')].reverse()).map(b => ({
|
||||
tabIndex: b.tabIndex,
|
||||
text: b.value,
|
||||
isPrimary: b.dataset.primary != null,
|
||||
trigger: b.onclick == null ? null : (popup => (b.onclick.call(popup), false))
|
||||
// @ts-ignore
|
||||
trigger: b.onclick == null ? undefined : ((/** @type {IPopup} */ popup) => (b.onclick?.call(popup, null), false))
|
||||
}));
|
||||
const popup = new Popup({
|
||||
title,
|
||||
content,
|
||||
content: content ?? '',
|
||||
persistent: !removable,
|
||||
resolve: typeof callback === 'function' ? (result => callback(result)) : null,
|
||||
resolve: typeof callback === 'function' ? (result => callback(result)) : undefined,
|
||||
zIndex: wrapper.zIndex ?? zIndex,
|
||||
buttons
|
||||
});
|
||||
@@ -634,8 +506,15 @@ const iconTypes = {
|
||||
'error': 'times-circle'
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string | HTMLElement} title
|
||||
* @param {string} message
|
||||
* @param {keyof iconTypes} iconType
|
||||
* @param {HTMLElement} parent
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
export function showAlert(title, message, iconType = 'info', parent = document.body) {
|
||||
const r = typeof GetTextByKey === 'function' ? GetTextByKey : lang;
|
||||
const r = typeof global.GetTextByKey === 'function' ? global.GetTextByKey : lang;
|
||||
return new Promise(resolve => {
|
||||
const popup = new Popup({
|
||||
title,
|
||||
@@ -648,16 +527,26 @@ export function showAlert(title, message, iconType = 'info', parent = document.b
|
||||
{ text: r('ok', 'OK'), isPrimary: true }
|
||||
]
|
||||
});
|
||||
popup.show(parent).then(mask => {
|
||||
popup.show(parent)?.then(mask => {
|
||||
/** @type {HTMLElement | null} */
|
||||
const button = mask.querySelector('.ui-popup-container .ui-popup-footer .ui-popup-button');
|
||||
button?.focus();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string | HTMLElement} title
|
||||
* @param {boolean} multiline
|
||||
* @param {string} message
|
||||
* @param {InputOptions} options
|
||||
* @param {PopupButton[]} buttons
|
||||
* @param {HTMLElement} parent
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
export function showInput(title, multiline, message, options, buttons, parent = document.body) {
|
||||
const r = typeof GetTextByKey === 'function' ? GetTextByKey : lang;
|
||||
options ??= {};
|
||||
const r = typeof global.GetTextByKey === 'function' ? global.GetTextByKey : lang;
|
||||
const opts = mergeOptions({}, options);
|
||||
return new Promise(resolve => {
|
||||
const input = multiline ?
|
||||
createElement('textarea', text => {
|
||||
@@ -667,29 +556,29 @@ export function showInput(title, multiline, message, options, buttons, parent =
|
||||
input.type = 'text';
|
||||
input.className = 'ui-input';
|
||||
});
|
||||
for (let key of Object.keys(options)) {
|
||||
for (let key of Object.keys(opts)) {
|
||||
switch (key) {
|
||||
case 'tabIndex':
|
||||
if (!isNaN(options.tabIndex)) {
|
||||
input.tabIndex = options.tabIndex;
|
||||
if (!isNaN(opts.tabIndex)) {
|
||||
input.tabIndex = opts.tabIndex;
|
||||
}
|
||||
break;
|
||||
case 'default':
|
||||
if (!nullOrEmpty(options.default)) {
|
||||
input.value = options.default;
|
||||
if (!nullOrEmpty(opts.default)) {
|
||||
input.value = opts.default;
|
||||
}
|
||||
break;
|
||||
case 'placeholder':
|
||||
if (!nullOrEmpty(options.placeholder)) {
|
||||
input.placeholder = options.placeholder;
|
||||
if (!nullOrEmpty(opts.placeholder)) {
|
||||
input.placeholder = opts.placeholder;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
input[key] = options[key];
|
||||
input[key] = opts[key];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isNaN(options.tabIndex)) {
|
||||
if (isNaN(opts.tabIndex)) {
|
||||
let tabIndex = Math.max.apply(null, [...document.querySelectorAll('[tabindex]')].map(e => e.tabIndex ?? 0));
|
||||
if (tabIndex < 0) {
|
||||
tabIndex = 0;
|
||||
@@ -708,12 +597,21 @@ export function showInput(title, multiline, message, options, buttons, parent =
|
||||
{ key: 'no', text: r('no', 'No') }
|
||||
]
|
||||
});
|
||||
popup.show(parent).then(() => input.focus());
|
||||
popup.show(parent)?.then(() => input.focus());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string | HTMLElement} title
|
||||
* @param {string | HTMLElement} content
|
||||
* @param {PopupButton[]} buttons
|
||||
* @param {keyof iconTypes} iconType
|
||||
* @param {HTMLElement} parent
|
||||
* @returns
|
||||
*/
|
||||
export function showConfirm(title, content, buttons, iconType = 'question', parent = document.body) {
|
||||
const r = typeof GetTextByKey === 'function' ? GetTextByKey : lang;
|
||||
const r = typeof global.GetTextByKey === 'function' ? global.GetTextByKey : lang;
|
||||
return new Promise(resolve => {
|
||||
const wrapper = createElement('div', 'message-wrapper');
|
||||
if (!nullOrEmpty(iconType)) {
|
||||
@@ -731,13 +629,10 @@ export function showConfirm(title, content, buttons, iconType = 'question', pare
|
||||
text: b.text,
|
||||
isPrimary: b.isPrimary,
|
||||
trigger: p => {
|
||||
let result;
|
||||
if (typeof b.trigger === 'function') {
|
||||
result = b.trigger(p, b);
|
||||
} else {
|
||||
result = b.key ?? i;
|
||||
return b.trigger(p, b);
|
||||
}
|
||||
return result;
|
||||
return b.key ?? i;
|
||||
}
|
||||
};
|
||||
}) ??
|
||||
@@ -746,7 +641,8 @@ export function showConfirm(title, content, buttons, iconType = 'question', pare
|
||||
{ key: 'no', text: r('no', 'No') }
|
||||
]
|
||||
});
|
||||
popup.show(parent).then(mask => {
|
||||
popup.show(parent)?.then(mask => {
|
||||
/** @type {HTMLElement | null} */
|
||||
const button = mask.querySelector('.ui-popup-container .ui-popup-footer .ui-popup-button:first-child');
|
||||
button?.focus();
|
||||
});
|
||||
|
||||
+1
-23
@@ -2,32 +2,10 @@ import { getCookie, setCookie, deleteCookie } from "./utility/cookie";
|
||||
import { domLoad, init, isTag, r, lang } from "./utility/lgres";
|
||||
import { get, post, upload } from "./utility/request";
|
||||
import { nullOrEmpty, contains, endsWith, padStart, formatUrl, escapeHtml, escapeEmoji } from "./utility/strings";
|
||||
import { mergeOptions } from "./functions";
|
||||
|
||||
const g = typeof window !== 'undefined' ? window : self;
|
||||
|
||||
/** @import { MarkRequired } from './types' */
|
||||
|
||||
/**
|
||||
* 合并参数
|
||||
* @template {object} T 可选参数类型
|
||||
* @template {Partial<T>} D 部分缺省值的选项类型
|
||||
* @param {D} defaults 默认参数
|
||||
* @param {T} opts 传入的待合并参数
|
||||
* @returns {MarkRequired<T, Extract<keyof D, keyof T>>} 返回已合并的拥有默认值的参数
|
||||
*/
|
||||
function mergeOptions(defaults, opts) {
|
||||
/** @type {Record<string, unknown>} */
|
||||
const result = { ...defaults };
|
||||
if (opts) {
|
||||
for (const [key, value] of Object.entries(opts)) {
|
||||
if (value !== undefined) {
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return /** @type {MarkRequired<T, Extract<keyof D, keyof T>>} */ (result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {any} n
|
||||
* @returns {n is number}
|
||||
|
||||
Reference in new issue
Block a user