| 1 |
import { createBrowserHistory } from 'history'; |
| 2 |
import { parse } from 'qs'; |
| 3 |
import { addQueryArgs, getQueryArgs } from '@wordpress/url'; |
| 4 |
|
| 5 |
export const objectWithoutPropertiesLoose = (source, excluded) => { |
| 6 |
if (source == null) return {}; |
| 7 |
var target = {}; |
| 8 |
var sourceKeys = Object.keys(source); |
| 9 |
var key, i; |
| 10 |
for (i = 0;i < sourceKeys.length;i++) { |
| 11 |
key = sourceKeys[i]; |
| 12 |
if (excluded.indexOf(key) >= 0) continue; |
| 13 |
target[key] = source[key]; |
| 14 |
} |
| 15 |
return target; |
| 16 |
}; |
| 17 |
|
| 18 |
let _history; |
| 19 |
|
| 20 |
function getHistory() { |
| 21 |
if (!_history) { |
| 22 |
const path = document.location.pathname; |
| 23 |
const basename = path.substring(0, path.lastIndexOf('/')); |
| 24 |
const browserHistory = createBrowserHistory({ |
| 25 |
basename: basename, |
| 26 |
}); |
| 27 |
|
| 28 |
_history = { |
| 29 |
get length() { |
| 30 |
return browserHistory.length; |
| 31 |
}, |
| 32 |
get action() { |
| 33 |
return browserHistory.action; |
| 34 |
}, |
| 35 |
get location() { |
| 36 |
const { location } = browserHistory; |
| 37 |
const query = parse(location.search.substring(1)); |
| 38 |
let pathname = query.page || '/'; |
| 39 |
|
| 40 |
if (!pathname.startsWith('/')) { |
| 41 |
pathname = '/' + pathname; |
| 42 |
} |
| 43 |
|
| 44 |
return { |
| 45 |
...location, |
| 46 |
actualPathname: path, |
| 47 |
pathname, |
| 48 |
}; |
| 49 |
}, |
| 50 |
get pathname() { |
| 51 |
return this.location.pathname.substring(1); |
| 52 |
}, |
| 53 |
createHref: (...args) => |
| 54 |
browserHistory.createHref.apply(browserHistory, args), |
| 55 |
push: (...args) => browserHistory.push.apply(browserHistory, args), |
| 56 |
replace: (...args) => |
| 57 |
browserHistory.replace.apply(browserHistory, args), |
| 58 |
go: (...args) => browserHistory.go.apply(browserHistory, args), |
| 59 |
goBack: (...args) => |
| 60 |
browserHistory.goBack.apply(browserHistory, args), |
| 61 |
goForward: (...args) => |
| 62 |
browserHistory.goForward.apply(browserHistory, args), |
| 63 |
block: (...args) => |
| 64 |
browserHistory.block.apply(browserHistory, args), |
| 65 |
listen(listener) { |
| 66 |
return browserHistory.listen(() => { |
| 67 |
listener(this.location, this.action); |
| 68 |
}); |
| 69 |
}, |
| 70 |
}; |
| 71 |
} |
| 72 |
return _history; |
| 73 |
} |
| 74 |
|
| 75 |
const history = getHistory(); |
| 76 |
|
| 77 |
export function getQuery() { |
| 78 |
const search = getHistory().location.search; |
| 79 |
if (search.length) { |
| 80 |
return parse(search.substring(1)) || {}; |
| 81 |
} |
| 82 |
return {}; |
| 83 |
} |
| 84 |
|
| 85 |
export const getPath = () => getHistory().location.pathname; |
| 86 |
|
| 87 |
export const makeRouteURL = (page, query = null, ignoreKeys = [], merge_existing_query_params=true) => { |
| 88 |
let queryArgs = getQueryArgs(page); |
| 89 |
const currentQuery = getQuery(); |
| 90 |
|
| 91 |
let args = merge_existing_query_params ? { ...currentQuery, ...queryArgs } : {...queryArgs}; |
| 92 |
|
| 93 |
if (query != null) { |
| 94 |
args = { ...args, ...query }; |
| 95 |
} |
| 96 |
|
| 97 |
if (page !== '/' && !Array.isArray(page)) { |
| 98 |
args.page = page; |
| 99 |
} else if (Array.isArray(page)) { |
| 100 |
args.page = page.join('/'); |
| 101 |
} |
| 102 |
|
| 103 |
if (args.page.startsWith('../')) { |
| 104 |
args.page = args.page.substring('3'); |
| 105 |
} |
| 106 |
|
| 107 |
if ( |
| 108 |
currentQuery?.action === 'elementor' || |
| 109 |
currentQuery?.action === 'edit' |
| 110 |
) { |
| 111 |
ignoreKeys?.push('page'); |
| 112 |
} |
| 113 |
|
| 114 |
if (Boolean(query?.isGutenbergEditor) === true) { |
| 115 |
ignoreKeys?.push('page'); |
| 116 |
ignoreKeys?.push('isGutenbergEditor'); |
| 117 |
} |
| 118 |
|
| 119 |
if (args?.platform && args.page?.startsWith(args.platform)) { |
| 120 |
ignoreKeys?.push('platform'); |
| 121 |
} |
| 122 |
|
| 123 |
if (Boolean(query?.closing) === true) { |
| 124 |
ignoreKeys?.push('closing'); |
| 125 |
} |
| 126 |
|
| 127 |
if (page === '') { |
| 128 |
ignoreKeys?.push('path'); |
| 129 |
} |
| 130 |
|
| 131 |
if (ignoreKeys?.length > 0) { |
| 132 |
args = objectWithoutPropertiesLoose(args, ignoreKeys); |
| 133 |
} |
| 134 |
|
| 135 |
// Keep `page` as the first query param (e.g. ?page=betterdocs-faq&faq_action=create). |
| 136 |
if (args.page !== undefined) { |
| 137 |
args = { page: args.page, ...args }; |
| 138 |
} |
| 139 |
|
| 140 |
return addQueryArgs(getHistory().location.actualPathname, args); |
| 141 |
}; |
| 142 |
|
| 143 |
export { getHistory, history }; |
| 144 |
|