wip: refactor(client): migrate components to composition api

This commit is contained in:
syuilo 2022-01-16 08:38:55 +09:00
parent ed5c918d70
commit 133b5c6391
2 changed files with 39 additions and 96 deletions

View File

@ -1,82 +1,36 @@
<template> <template>
<component :is="self ? 'MkA' : 'a'" class="xlcxczvw _link" :[attr]="self ? url.substr(local.length) : url" :rel="rel" :target="target" <component :is="self ? 'MkA' : 'a'" ref="el" class="xlcxczvw _link" :[attr]="self ? url.substr(local.length) : url" :rel="rel" :target="target"
:title="url" :title="url"
@mouseover="onMouseover"
@mouseleave="onMouseleave"
> >
<slot></slot> <slot></slot>
<i v-if="target === '_blank'" class="fas fa-external-link-square-alt icon"></i> <i v-if="target === '_blank'" class="fas fa-external-link-square-alt icon"></i>
</component> </component>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from 'vue'; import { } from 'vue';
import { url as local } from '@/config'; import { url as local } from '@/config';
import { isTouchUsing } from '@/scripts/touch'; import { useTooltip } from '@/scripts/use-tooltip';
import * as os from '@/os'; import * as os from '@/os';
export default defineComponent({ const props = withDefaults(defineProps<{
props: { url: string;
url: { rel?: null | string;
type: String, }>(), {
required: true,
},
rel: {
type: String,
required: false,
}
},
data() {
const self = this.url.startsWith(local);
return {
local,
self: self,
attr: self ? 'to' : 'href',
target: self ? null : '_blank',
showTimer: null,
hideTimer: null,
checkTimer: null,
close: null,
};
},
methods: {
async showPreview() {
if (!document.body.contains(this.$el)) return;
if (this.close) return;
const { dispose } = await os.popup(import('@/components/url-preview-popup.vue'), {
url: this.url,
source: this.$el
}); });
this.close = () => { const self = props.url.startsWith(local);
dispose(); const attr = self ? 'to' : 'href';
}; const target = self ? null : '_blank';
this.checkTimer = setInterval(() => { const el = $ref();
if (!document.body.contains(this.$el)) this.closePreview();
}, 1000); useTooltip($$(el), (showing) => {
}, os.popup(import('@/components/url-preview-popup.vue'), {
closePreview() { showing,
if (this.close) { url: props.url,
clearInterval(this.checkTimer); source: el,
this.close(); }, {}, 'closed');
this.close = null;
}
},
onMouseover() {
if (isTouchUsing) return;
clearTimeout(this.showTimer);
clearTimeout(this.hideTimer);
this.showTimer = setTimeout(this.showPreview, 500);
},
onMouseleave() {
if (isTouchUsing) return;
clearTimeout(this.showTimer);
clearTimeout(this.hideTimer);
this.hideTimer = setTimeout(this.closePreview, 500);
}
}
}); });
</script> </script>

View File

@ -8,39 +8,28 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent } from 'vue'; import { onUnmounted } from 'vue';
import * as os from '@/os';
import { stream } from '@/stream'; import { stream } from '@/stream';
export default defineComponent({ let hasDisconnected = $ref(false);
data() {
return { function onDisconnected() {
hasDisconnected: false, hasDisconnected = true;
} }
},
computed: { function resetDisconnected() {
stream() { hasDisconnected = false;
return stream; }
},
}, function reload() {
created() {
stream.on('_disconnected_', this.onDisconnected);
},
beforeUnmount() {
stream.off('_disconnected_', this.onDisconnected);
},
methods: {
onDisconnected() {
this.hasDisconnected = true;
},
resetDisconnected() {
this.hasDisconnected = false;
},
reload() {
location.reload(); location.reload();
},
} }
stream.on('_disconnected_', onDisconnected);
onUnmounted(() => {
stream.off('_disconnected_', onDisconnected);
}); });
</script> </script>