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'), { const self = props.url.startsWith(local);
url: this.url, const attr = self ? 'to' : 'href';
source: this.$el const target = self ? null : '_blank';
});
this.close = () => { const el = $ref();
dispose();
};
this.checkTimer = setInterval(() => { useTooltip($$(el), (showing) => {
if (!document.body.contains(this.$el)) this.closePreview(); os.popup(import('@/components/url-preview-popup.vue'), {
}, 1000); showing,
}, url: props.url,
closePreview() { source: el,
if (this.close) { }, {}, 'closed');
clearInterval(this.checkTimer);
this.close();
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() { location.reload();
stream.on('_disconnected_', this.onDisconnected); }
},
beforeUnmount() { stream.on('_disconnected_', onDisconnected);
stream.off('_disconnected_', this.onDisconnected);
}, onUnmounted(() => {
methods: { stream.off('_disconnected_', onDisconnected);
onDisconnected() {
this.hasDisconnected = true;
},
resetDisconnected() {
this.hasDisconnected = false;
},
reload() {
location.reload();
},
}
}); });
</script> </script>