mirror of
https://github.com/billsonnn/nitro-react.git
synced 2025-02-17 01:12:37 +01:00
Card size/position is now stored in LocalStorage
This commit is contained in:
parent
147b299b5e
commit
16e3457ec8
1
src/api/utils/GetLocalStorage.ts
Normal file
1
src/api/utils/GetLocalStorage.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export const GetLocalStorage = <T>(key: string) => JSON.parse(window.localStorage.getItem(key)) as T ?? null;
|
1
src/api/utils/SetLocalStorage.ts
Normal file
1
src/api/utils/SetLocalStorage.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export const SetLocalStorage = <T>(key: string, value: T) => window.localStorage.setItem(key, JSON.stringify(value));
|
5
src/api/utils/WindowSaveOptions.ts
Normal file
5
src/api/utils/WindowSaveOptions.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export interface WindowSaveOptions
|
||||||
|
{
|
||||||
|
offset: { x: number, y: number };
|
||||||
|
size: { width: number, height: number };
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
export * from './CloneObject';
|
export * from './CloneObject';
|
||||||
export * from './ColorUtils';
|
export * from './ColorUtils';
|
||||||
export * from './ConvertSeconds';
|
export * from './ConvertSeconds';
|
||||||
|
export * from './GetLocalStorage';
|
||||||
export * from './LocalizeBadgeDescription';
|
export * from './LocalizeBadgeDescription';
|
||||||
export * from './LocalizeBageName';
|
export * from './LocalizeBageName';
|
||||||
export * from './LocalizeFormattedNumber';
|
export * from './LocalizeFormattedNumber';
|
||||||
@ -11,4 +12,6 @@ export * from './PlaySound';
|
|||||||
export * from './ProductImageUtility';
|
export * from './ProductImageUtility';
|
||||||
export * from './Randomizer';
|
export * from './Randomizer';
|
||||||
export * from './RoomChatFormatter';
|
export * from './RoomChatFormatter';
|
||||||
|
export * from './SetLocalStorage';
|
||||||
export * from './SoundNames';
|
export * from './SoundNames';
|
||||||
|
export * from './WindowSaveOptions';
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { FC, useMemo } from 'react';
|
import { FC, useEffect, useMemo, useRef } from 'react';
|
||||||
import { Column, ColumnProps } from '..';
|
import { Column, ColumnProps } from '..';
|
||||||
|
import { GetLocalStorage, SetLocalStorage, WindowSaveOptions } from '../../api';
|
||||||
import { DraggableWindow, DraggableWindowPosition, DraggableWindowProps } from '../draggable-window';
|
import { DraggableWindow, DraggableWindowPosition, DraggableWindowProps } from '../draggable-window';
|
||||||
import { NitroCardContextProvider } from './NitroCardContext';
|
import { NitroCardContextProvider } from './NitroCardContext';
|
||||||
|
|
||||||
@ -11,6 +12,7 @@ export interface NitroCardViewProps extends DraggableWindowProps, ColumnProps
|
|||||||
export const NitroCardView: FC<NitroCardViewProps> = props =>
|
export const NitroCardView: FC<NitroCardViewProps> = props =>
|
||||||
{
|
{
|
||||||
const { theme = 'primary', uniqueKey = null, handleSelector = '.drag-handler', windowPosition = DraggableWindowPosition.CENTER, disableDrag = false, overflow = 'hidden', position = 'relative', gap = 0, classNames = [], ...rest } = props;
|
const { theme = 'primary', uniqueKey = null, handleSelector = '.drag-handler', windowPosition = DraggableWindowPosition.CENTER, disableDrag = false, overflow = 'hidden', position = 'relative', gap = 0, classNames = [], ...rest } = props;
|
||||||
|
const elementRef = useRef<HTMLDivElement>();
|
||||||
|
|
||||||
const getClassNames = useMemo(() =>
|
const getClassNames = useMemo(() =>
|
||||||
{
|
{
|
||||||
@ -23,10 +25,40 @@ export const NitroCardView: FC<NitroCardViewProps> = props =>
|
|||||||
return newClassNames;
|
return newClassNames;
|
||||||
}, [ theme, classNames ]);
|
}, [ theme, classNames ]);
|
||||||
|
|
||||||
|
useEffect(() =>
|
||||||
|
{
|
||||||
|
if(!uniqueKey || !elementRef || !elementRef.current) return;
|
||||||
|
|
||||||
|
const localStorage = GetLocalStorage<WindowSaveOptions>(`nitro.windows.${ uniqueKey }`);
|
||||||
|
const element = elementRef.current;
|
||||||
|
|
||||||
|
if(localStorage && localStorage.size)
|
||||||
|
{
|
||||||
|
element.style.width = `${ localStorage.size.width }px`;
|
||||||
|
element.style.height = `${ localStorage.size.height }px`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const observer = new ResizeObserver(event =>
|
||||||
|
{
|
||||||
|
const newStorage = { ...GetLocalStorage<Partial<WindowSaveOptions>>(`nitro.windows.${ uniqueKey }`) } as WindowSaveOptions;
|
||||||
|
|
||||||
|
newStorage.size = { width: element.offsetWidth, height: element.offsetHeight };
|
||||||
|
|
||||||
|
SetLocalStorage<WindowSaveOptions>(`nitro.windows.${ uniqueKey }`, newStorage);
|
||||||
|
});
|
||||||
|
|
||||||
|
observer.observe(element);
|
||||||
|
|
||||||
|
return () =>
|
||||||
|
{
|
||||||
|
observer.disconnect();
|
||||||
|
}
|
||||||
|
}, [ uniqueKey ]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NitroCardContextProvider value={ { theme } }>
|
<NitroCardContextProvider value={ { theme } }>
|
||||||
<DraggableWindow uniqueKey={ uniqueKey } handleSelector={ handleSelector } windowPosition={ windowPosition } disableDrag={ disableDrag }>
|
<DraggableWindow uniqueKey={ uniqueKey } handleSelector={ handleSelector } windowPosition={ windowPosition } disableDrag={ disableDrag }>
|
||||||
<Column overflow={ overflow } position={ position } gap={ gap } classNames={ getClassNames } { ...rest } />
|
<Column innerRef={ elementRef } overflow={ overflow } position={ position } gap={ gap } classNames={ getClassNames } { ...rest } />
|
||||||
</DraggableWindow>
|
</DraggableWindow>
|
||||||
</NitroCardContextProvider>
|
</NitroCardContextProvider>
|
||||||
);
|
);
|
||||||
|
@ -2,10 +2,10 @@ import { MouseEventType, TouchEventType } from '@nitrots/nitro-renderer';
|
|||||||
import { CSSProperties, FC, Key, MouseEvent as ReactMouseEvent, ReactNode, TouchEvent as ReactTouchEvent, useCallback, useEffect, useRef, useState } from 'react';
|
import { CSSProperties, FC, Key, MouseEvent as ReactMouseEvent, ReactNode, TouchEvent as ReactTouchEvent, useCallback, useEffect, useRef, useState } from 'react';
|
||||||
import { createPortal } from 'react-dom';
|
import { createPortal } from 'react-dom';
|
||||||
import { Base } from '..';
|
import { Base } from '..';
|
||||||
|
import { GetLocalStorage, SetLocalStorage, WindowSaveOptions } from '../../api';
|
||||||
import { DraggableWindowPosition } from './DraggableWindowPosition';
|
import { DraggableWindowPosition } from './DraggableWindowPosition';
|
||||||
|
|
||||||
const CURRENT_WINDOWS: HTMLElement[] = [];
|
const CURRENT_WINDOWS: HTMLElement[] = [];
|
||||||
const POS_MEMORY: Map<Key, { x: number, y: number }> = new Map();
|
|
||||||
const BOUNDS_THRESHOLD_TOP: number = 0;
|
const BOUNDS_THRESHOLD_TOP: number = 0;
|
||||||
const BOUNDS_THRESHOLD_LEFT: number = 0;
|
const BOUNDS_THRESHOLD_LEFT: number = 0;
|
||||||
|
|
||||||
@ -138,7 +138,14 @@ export const DraggableWindow: FC<DraggableWindowProps> = props =>
|
|||||||
setOffset({ x: offsetX, y: offsetY });
|
setOffset({ x: offsetX, y: offsetY });
|
||||||
setIsDragging(false);
|
setIsDragging(false);
|
||||||
|
|
||||||
if(uniqueKey !== null) POS_MEMORY.set(uniqueKey, { x: offsetX, y: offsetY });
|
if(uniqueKey !== null)
|
||||||
|
{
|
||||||
|
const newStorage = { ...GetLocalStorage<WindowSaveOptions>(`nitro.windows.${ uniqueKey }`) } as WindowSaveOptions;
|
||||||
|
|
||||||
|
newStorage.offset = { x: offsetX, y: offsetY };
|
||||||
|
|
||||||
|
SetLocalStorage<WindowSaveOptions>(`nitro.windows.${ uniqueKey }`, newStorage);
|
||||||
|
}
|
||||||
}, [ dragHandler, delta, offset, uniqueKey ]);
|
}, [ dragHandler, delta, offset, uniqueKey ]);
|
||||||
|
|
||||||
const onDragMouseUp = useCallback((event: MouseEvent) =>
|
const onDragMouseUp = useCallback((event: MouseEvent) =>
|
||||||
@ -187,17 +194,6 @@ export const DraggableWindow: FC<DraggableWindowProps> = props =>
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(uniqueKey !== null)
|
|
||||||
{
|
|
||||||
const memory = POS_MEMORY.get(uniqueKey);
|
|
||||||
|
|
||||||
if(memory)
|
|
||||||
{
|
|
||||||
offsetX = memory.x;
|
|
||||||
offsetY = memory.y;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
setDelta({ x: 0, y: 0 });
|
setDelta({ x: 0, y: 0 });
|
||||||
setOffset({ x: offsetX, y: offsetY });
|
setOffset({ x: offsetX, y: offsetY });
|
||||||
|
|
||||||
@ -253,6 +249,18 @@ export const DraggableWindow: FC<DraggableWindowProps> = props =>
|
|||||||
}
|
}
|
||||||
}, [ isDragging, onDragMouseUp, onDragMouseMove, onDragTouchUp, onDragTouchMove ]);
|
}, [ isDragging, onDragMouseUp, onDragMouseMove, onDragTouchUp, onDragTouchMove ]);
|
||||||
|
|
||||||
|
useEffect(() =>
|
||||||
|
{
|
||||||
|
if(!uniqueKey) return;
|
||||||
|
|
||||||
|
const localStorage = GetLocalStorage<WindowSaveOptions>(`nitro.windows.${ uniqueKey }`);
|
||||||
|
|
||||||
|
if(!localStorage || !localStorage.offset) return;
|
||||||
|
|
||||||
|
setDelta({ x: 0, y: 0 });
|
||||||
|
if(localStorage.offset) setOffset(localStorage.offset);
|
||||||
|
}, [ uniqueKey ]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
createPortal(
|
createPortal(
|
||||||
<Base position="absolute" innerRef={ elementRef } className="draggable-window" onMouseDownCapture={ onMouseDown } onTouchStartCapture={ onTouchStart } style={ dragStyle }>
|
<Base position="absolute" innerRef={ elementRef } className="draggable-window" onMouseDownCapture={ onMouseDown } onTouchStartCapture={ onTouchStart } style={ dragStyle }>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { NitroLogger } from '@nitrots/nitro-renderer';
|
import { NitroLogger } from '@nitrots/nitro-renderer';
|
||||||
import { Dispatch, SetStateAction, useState } from 'react';
|
import { Dispatch, SetStateAction, useState } from 'react';
|
||||||
|
import { GetLocalStorage, SetLocalStorage } from '../api';
|
||||||
|
|
||||||
const useLocalStorageState = <T>(key: string, initialValue: T): [ T, Dispatch<SetStateAction<T>>] =>
|
const useLocalStorageState = <T>(key: string, initialValue: T): [ T, Dispatch<SetStateAction<T>>] =>
|
||||||
{
|
{
|
||||||
@ -9,9 +10,9 @@ const useLocalStorageState = <T>(key: string, initialValue: T): [ T, Dispatch<Se
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
const item = window.localStorage.getItem(key);
|
const item = GetLocalStorage<T>(key);
|
||||||
|
|
||||||
return item ? JSON.parse(item) : initialValue;
|
return item ?? initialValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
catch(error)
|
catch(error)
|
||||||
@ -28,7 +29,7 @@ const useLocalStorageState = <T>(key: string, initialValue: T): [ T, Dispatch<Se
|
|||||||
|
|
||||||
setStoredValue(valueToStore);
|
setStoredValue(valueToStore);
|
||||||
|
|
||||||
if(typeof window !== 'undefined') window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
if(typeof window !== 'undefined') SetLocalStorage(key, valueToStore);
|
||||||
}
|
}
|
||||||
|
|
||||||
catch(error)
|
catch(error)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user