misskey/packages/client/src/components/global/MkA.vue

103 lines
2.3 KiB
Vue
Raw Normal View History

<template>
<a :href="to" :class="active ? activeClass : null" @click.prevent="nav" @contextmenu.prevent.stop="onContextmenu">
<slot></slot>
</a>
</template>
<script lang="ts" setup>
import { inject } from 'vue';
2021-11-11 18:02:25 +01:00
import * as os from '@/os';
import copyToClipboard from '@/scripts/copy-to-clipboard';
import { url } from '@/config';
import { popout as popout_ } from '@/scripts/popout';
import { i18n } from '@/i18n';
import { useRouter } from '@/router';
const props = withDefaults(defineProps<{
to: string;
activeClass?: null | string;
behavior?: null | 'window' | 'browser' | 'modalWindow';
}>(), {
activeClass: null,
behavior: null,
});
const router = useRouter();
const active = $computed(() => {
if (props.activeClass == null) return false;
const resolved = router.resolve(props.to);
if (resolved == null) return false;
if (resolved.route.path === router.currentRoute.value.path) return true;
if (resolved.route.name == null) return false;
if (router.currentRoute.value.name == null) return false;
return resolved.route.name === router.currentRoute.value.name;
});
function onContextmenu(ev) {
const selection = window.getSelection();
if (selection && selection.toString() !== '') return;
os.contextMenu([{
type: 'label',
text: props.to,
}, {
2022-12-20 02:52:39 +01:00
icon: 'ti ti-app-window',
text: i18n.ts.openInWindow,
action: () => {
os.pageWindow(props.to);
},
}, {
2022-12-21 00:39:28 +01:00
icon: 'ti ti-player-eject',
text: i18n.ts.showInPage,
action: () => {
2022-07-17 22:03:39 +02:00
router.push(props.to, 'forcePage');
},
}, null, {
icon: 'ti ti-external-link',
text: i18n.ts.openInNewTab,
action: () => {
window.open(props.to, '_blank');
},
}, {
icon: 'ti ti-link',
text: i18n.ts.copyLink,
action: () => {
copyToClipboard(`${url}${props.to}`);
},
}], ev);
}
function openWindow() {
os.pageWindow(props.to);
}
function modalWindow() {
os.modalPageWindow(props.to);
}
2021-04-11 14:09:35 +02:00
function popout() {
popout_(props.to);
}
2022-07-17 22:03:39 +02:00
function nav(ev: MouseEvent) {
if (props.behavior === 'browser') {
location.href = props.to;
return;
}
2021-03-02 17:03:29 +01:00
if (props.behavior) {
if (props.behavior === 'window') {
return openWindow();
} else if (props.behavior === 'modalWindow') {
return modalWindow();
}
}
2022-07-17 22:03:39 +02:00
if (ev.shiftKey) {
return openWindow();
}
router.push(props.to, ev.ctrlKey ? 'forcePage' : null);
}
</script>