This commit is contained in:
Kerwin Bryant 2025-03-04 07:11:45 +00:00
parent 9fc2ba3705
commit a212ff6bb0

View File

@ -26,7 +26,7 @@ async function loadChildren(path: string, recursive?: boolean) {
const fileTree = document.querySelector('#view-file-tree'); const fileTree = document.querySelector('#view-file-tree');
const apiBaseUrl = fileTree.getAttribute('data-api-base-url'); const apiBaseUrl = fileTree.getAttribute('data-api-base-url');
const refTypeNameSubURL = fileTree.getAttribute('data-current-ref-type-name-sub-url'); const refTypeNameSubURL = fileTree.getAttribute('data-current-ref-type-name-sub-url');
const response = await GET(`${apiBaseUrl}/tree/${refTypeNameSubURL}/${path ?? ''}?recursive=${recursive ?? false}`); const response = await GET(`${apiBaseUrl}/tree/${refTypeNameSubURL}/${encodeURIComponent(path ?? '')}?recursive=${recursive ?? false}`);
const json = await response.json(); const json = await response.json();
if (json instanceof Array) { if (json instanceof Array) {
return json.map((i) => ({ return json.map((i) => ({
@ -49,7 +49,7 @@ async function loadContent(sidebarEl: HTMLElement) {
} }
function reloadContentScript(sidebarEl: HTMLElement, contentEl: Element) { function reloadContentScript(sidebarEl: HTMLElement, contentEl: Element) {
contentEl.querySelector('.show-tree-sidebar-button').addEventListener('click', () => { contentEl.querySelector('.show-tree-sidebar-button')?.addEventListener('click', () => {
toggleSidebar(sidebarEl, true); toggleSidebar(sidebarEl, true);
}); });
} }
@ -70,41 +70,26 @@ export function initViewFileTreeSidebar() {
const refName = fileTree.getAttribute('data-current-ref-short-name'); const refName = fileTree.getAttribute('data-current-ref-short-name');
const refString = (refType ? (`/${refType}`) : '') + (refName ? (`/${refName}`) : ''); const refString = (refType ? (`/${refType}`) : '') + (refName ? (`/${refName}`) : '');
const selectedItem = ref(treePath); const selectedItem = ref(getSelectedPath(refString));
const files = await loadChildren(treePath, true); const files = await loadChildren(treePath, true);
fileTree.classList.remove('is-loading'); fileTree.classList.remove('is-loading');
const fileTreeView = createApp(ViewFileTree, {files, selectedItem, loadChildren, loadContent: (path: string) => { const fileTreeView = createApp(ViewFileTree, {files, selectedItem, loadChildren, loadContent: (path: string) => {
window.history.pushState(null, null, `${baseUrl}/src${refString}/${path}`); selectedItem.value = getSelectedPath(refString, `${baseUrl}/src${refString}/${path}`);
selectedItem.value = path; window.history.pushState(null, null, `${baseUrl}/src${refString}/${encodeURIComponent(path)}`);
loadContent(el); loadContent(el);
}}); }});
fileTreeView.mount(fileTree); fileTreeView.mount(fileTree);
window.addEventListener('popstate', () => { window.addEventListener('popstate', () => {
selectedItem.value = extractPath(window.location.href); selectedItem.value = getSelectedPath(refString);
loadContent(el); loadContent(el);
}); });
}); });
} }
function extractPath(url: string) { function getSelectedPath(ref: string, url?: string) {
// Create a URL object const path = url ?? (new URL(window.location.href).pathname);
const urlObj = new URL(url); return path.substring(path.indexOf(ref) + ref.length + 1);
// Get the pathname part
const path = urlObj.pathname;
// Define a regular expression to match "/{param1}/{param2}/src/{branch}/{main}/"
const regex = /^\/[^/]+\/[^/]+\/src\/[^/]+\/[^/]+\//;
// Use RegExp#exec() method to match the path
const match = regex.exec(path);
if (match) {
return path.substring(match[0].length);
}
// If the path does not match, return the original path
return path;
} }