mirror of
https://github.com/muerwre/orchidmap-front.git
synced 2025-04-28 12:36:41 +07:00
typed some utils
This commit is contained in:
parent
cf6866240b
commit
5c3f09ec2c
5 changed files with 34 additions and 22 deletions
55
src/utils/history.ts
Normal file
55
src/utils/history.ts
Normal file
|
@ -0,0 +1,55 @@
|
|||
import { history } from '$redux/store';
|
||||
|
||||
interface IUrlData {
|
||||
path: string,
|
||||
mode: 'edit' | '',
|
||||
host: string,
|
||||
hash: string,
|
||||
protocol: 'http' | 'https',
|
||||
}
|
||||
|
||||
export const getPath = (): string => (window.location && window.location.pathname);
|
||||
export const pushPath = (url: string): string => history.push(url);
|
||||
export const replacePath = (url: string): string => history.replace(url);
|
||||
|
||||
export const getUrlData = (url = getPath()) => {
|
||||
const [, path, mode] = url.split('/');
|
||||
const { host, hash, protocol } = window.location;
|
||||
|
||||
return {
|
||||
path, mode, host, hash, protocol,
|
||||
};
|
||||
};
|
||||
|
||||
// Parses query string
|
||||
export const parseQuery = (queryString: string) => {
|
||||
let params = {};
|
||||
const queries = decodeURIComponent(queryString)
|
||||
.substring(queryString.substr(0, 1) === '?' ? 1 : 0)
|
||||
.split('&');
|
||||
for (let i = 0, l = queries.length; i < l; i += 1) {
|
||||
const temp = queries[i].split('=');
|
||||
params = { ...params, [temp[0]]: temp[1] };
|
||||
}
|
||||
return params;
|
||||
};
|
||||
|
||||
export const pushLoaderState = state => {
|
||||
document.getElementById('loader-bar').style.width = `${state}%`;
|
||||
};
|
||||
|
||||
export const pushNetworkInitError = () => {
|
||||
document.getElementById('loader-error').style.opacity = String(1);
|
||||
};
|
||||
|
||||
export const copyToClipboard = str => {
|
||||
const el = document.createElement('textarea');
|
||||
el.value = str;
|
||||
el.setAttribute('readonly', '');
|
||||
el.style.position = 'absolute';
|
||||
el.style.left = '-9999px';
|
||||
document.body.appendChild(el);
|
||||
el.select();
|
||||
document.execCommand('copy');
|
||||
document.body.removeChild(el);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue