Skip to content

Add repo file tree middle click open #34730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
6 changes: 5 additions & 1 deletion web_src/js/components/ViewFileTree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ async function loadViewContent(url: string) {
document.querySelector('.repo-view-content').innerHTML = await response.text();
}

async function navigateTreeView(treePath: string) {
async function navigateTreeView(treePath: string, newTab?: boolean) {
const url = `${props.repoLink}/src/${props.currentRefNameSubURL}/${pathEscapeSegments(treePath)}`;
if (newTab) {
window.open(url, '_blank');
return;
}
window.history.pushState({treePath, url}, null, url);
selectedItem.value = treePath;
await loadViewContent(url);
Expand Down
28 changes: 21 additions & 7 deletions web_src/js/components/ViewFileTreeItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Item = {

const props = defineProps<{
item: Item,
navigateViewContent:(treePath: string) => void,
navigateViewContent:(treePath: string, newTab?: boolean) => void,
loadChildren:(treePath: string, subPath?: string) => Promise<Item[]>,
selectedItem?: string,
}>();
Expand All @@ -35,16 +35,26 @@ const doLoadChildren = async () => {
}
};

const doLoadDirContent = () => {
doLoadChildren();
props.navigateViewContent(props.item.fullPath);
const doLoadDirContent = (event: MouseEvent) => {
// open the directory in a new tab if either
// - the auxiliary button (usually the mouse wheel button) is the origin of the click
// - the ctrl key or meta key (for mac support) was pressed while clicking
const openNewTab = event.button === 1 || event.ctrlKey || event.metaKey;
if (!openNewTab) doLoadChildren();
props.navigateViewContent(props.item.fullPath, openNewTab);
};

const doLoadFileContent = () => {
props.navigateViewContent(props.item.fullPath);
const doLoadFileContent = (event: MouseEvent) => {
const openNewTab = event.button === 1 || event.ctrlKey || event.metaKey;
props.navigateViewContent(props.item.fullPath, openNewTab);
};

const doGotoSubModule = () => {
const doGotoSubModule = (event: MouseEvent) => {
const openNewTab = event.button === 1 || event.ctrlKey || event.metaKey;
if (openNewTab) {
window.open(props.item.submoduleUrl, '_blank');
return;
}
location.href = props.item.submoduleUrl;
};
</script>
Expand All @@ -55,6 +65,7 @@ const doGotoSubModule = () => {
v-if="item.entryMode === 'commit'" class="tree-item type-submodule"
:title="item.entryName"
@click.stop="doGotoSubModule"
@click.middle.stop="doGotoSubModule"
>
<!-- submodule -->
<div class="item-content">
Expand All @@ -68,6 +79,7 @@ const doGotoSubModule = () => {
:class="{'selected': selectedItem === item.fullPath}"
:title="item.entryName"
@click.stop="doLoadFileContent"
@click.middle.stop="doLoadFileContent"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything is handled in @click.stop, so the @click.middle.stop should be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@click.stop does not fire for aux clicks. @click.middle.stop is transformed into a mousup event by vue internally, which is able to recognize aux clicks. @auxclick.stop also exists, but Safari only added support for it lately, so it may not work for some users with older browsers.
@click.stop only fires if the click is released on the same button where it was pressed. The mouseup event always fires when the click is released, regardless of the starting point. This difference in behavior is why I didn't unify both handlers into a single @mouseup.stop.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@click.stop only fires if the click is released on the same button where it was pressed.

But that's the expected behavior.

For example, if you "mousedown" in a normal <a>, then move mouse out, the link won't open.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I just wanted to say is that I didn't unify both calls into a mouseup because that would mess with the current behavior

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if "Ctrl+LeftClick"? I guess it won't trigger click.middle?

>
<!-- symlink -->
<div class="item-content">
Expand All @@ -81,6 +93,7 @@ const doGotoSubModule = () => {
:class="{'selected': selectedItem === item.fullPath}"
:title="item.entryName"
@click.stop="doLoadFileContent"
@click.middle.stop="doLoadFileContent"
>
<!-- file -->
<div class="item-content">
Expand All @@ -94,6 +107,7 @@ const doGotoSubModule = () => {
:class="{'selected': selectedItem === item.fullPath}"
:title="item.entryName"
@click.stop="doLoadDirContent"
@click.middle.stop="doLoadDirContent"
>
<!-- directory -->
<div class="item-toggle">
Expand Down