chore: 使用on-before-unmount替换on-unmouted

This commit is contained in:
roymondchen 2023-12-15 16:20:50 +08:00
parent f212be136e
commit b72e487b58
16 changed files with 42 additions and 38 deletions

View File

@ -14,7 +14,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, ref, watch } from 'vue'; import { computed, nextTick, ref, watch } from 'vue';
import { getConfig } from './config'; import { getConfig } from './config';
import type { SelectProps } from './types'; import type { SelectProps } from './types';
@ -49,14 +49,16 @@ const visibleHandler = (...args: any[]) => {
const scrollbarWrap = ref<HTMLDivElement | undefined>(); const scrollbarWrap = ref<HTMLDivElement | undefined>();
const unWacth = watch( const unWatch = watch(
() => select.value?.scrollbar?.wrap$ || select.value?.scrollbar?.wrapRef, () => select.value?.scrollbar?.wrap$ || select.value?.scrollbar?.wrapRef,
(wrap) => { (wrap) => {
if (!wrap) { if (!wrap) {
return; return;
} }
nextTick(() => unWatch());
scrollbarWrap.value = wrap; scrollbarWrap.value = wrap;
unWacth();
}, },
{ {
immediate: true, immediate: true,

View File

@ -48,7 +48,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, inject, onUnmounted, ref } from 'vue'; import { computed, inject, onBeforeUnmount, ref } from 'vue';
import { TMagicButton, TMagicDialog, tMagicMessage, tMagicMessageBox, TMagicTag } from '@tmagic/design'; import { TMagicButton, TMagicDialog, tMagicMessage, tMagicMessageBox, TMagicTag } from '@tmagic/design';
import { ColumnConfig, FormConfig, FormState, MFormBox, MFormDrawer } from '@tmagic/form'; import { ColumnConfig, FormConfig, FormState, MFormBox, MFormDrawer } from '@tmagic/form';
@ -85,7 +85,7 @@ const windowReizehandler = () => {
globalThis.addEventListener('resize', windowReizehandler); globalThis.addEventListener('resize', windowReizehandler);
onUnmounted(() => { onBeforeUnmount(() => {
globalThis.removeEventListener('resize', windowReizehandler); globalThis.removeEventListener('resize', windowReizehandler);
}); });

View File

@ -36,7 +36,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, nextTick, onMounted, onUnmounted, ref } from 'vue'; import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue';
import { MenuButton, MenuComponent } from '@editor/type'; import { MenuButton, MenuComponent } from '@editor/type';
@ -99,7 +99,7 @@ const clickHandler = () => {
hide(); hide();
}; };
const outsideClickhideHandler = (e: MouseEvent) => { const outsideClickHideHandler = (e: MouseEvent) => {
if (!props.autoHide) return; if (!props.autoHide) return;
const target = e.target as HTMLElement | undefined; const target = e.target as HTMLElement | undefined;
@ -173,13 +173,13 @@ const mouseenterHandler = () => {
onMounted(() => { onMounted(() => {
if (props.isSubMenu) return; if (props.isSubMenu) return;
globalThis.addEventListener('mousedown', outsideClickhideHandler, true); globalThis.addEventListener('mousedown', outsideClickHideHandler, true);
}); });
onUnmounted(() => { onBeforeUnmount(() => {
if (props.isSubMenu) return; if (props.isSubMenu) return;
globalThis.removeEventListener('mousedown', outsideClickhideHandler, true); globalThis.removeEventListener('mousedown', outsideClickHideHandler, true);
}); });
defineExpose({ defineExpose({

View File

@ -5,7 +5,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, onMounted, onUnmounted, ref } from 'vue'; import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
import Gesto from 'gesto'; import Gesto from 'gesto';
defineOptions({ defineOptions({
@ -53,7 +53,7 @@ onMounted(() => {
bar.value?.addEventListener('wheel', wheelHandler, false); bar.value?.addEventListener('wheel', wheelHandler, false);
}); });
onUnmounted(() => { onBeforeUnmount(() => {
if (gesto) gesto.off(); if (gesto) gesto.off();
bar.value?.removeEventListener('wheel', wheelHandler, false); bar.value?.removeEventListener('wheel', wheelHandler, false);
}); });

View File

@ -23,7 +23,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, onMounted, onUnmounted, ref, watch } from 'vue'; import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { isNumber } from '@tmagic/utils'; import { isNumber } from '@tmagic/utils';
@ -94,7 +94,7 @@ onMounted(() => {
}); });
}); });
onUnmounted(() => { onBeforeUnmount(() => {
scrollViewer.destroy(); scrollViewer.destroy();
}); });

View File

@ -21,7 +21,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, onMounted, onUnmounted, ref } from 'vue'; import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
import { OnDrag } from 'gesto'; import { OnDrag } from 'gesto';
import Resizer from './Resizer.vue'; import Resizer from './Resizer.vue';
@ -110,7 +110,7 @@ onMounted(() => {
} }
}); });
onUnmounted(() => { onBeforeUnmount(() => {
resizerObserver.disconnect(); resizerObserver.disconnect();
}); });

View File

@ -1,4 +1,4 @@
import { onMounted, onUnmounted, type Ref, ref } from 'vue'; import { onBeforeUnmount, onMounted, type Ref, ref } from 'vue';
import Gesto, { type OnDrag } from 'gesto'; import Gesto, { type OnDrag } from 'gesto';
export const useGetSo = (target: Ref<HTMLElement | undefined>, emit: (evt: 'change', e: OnDrag<Gesto>) => void) => { export const useGetSo = (target: Ref<HTMLElement | undefined>, emit: (evt: 'change', e: OnDrag<Gesto>) => void) => {
@ -24,7 +24,7 @@ export const useGetSo = (target: Ref<HTMLElement | undefined>, emit: (evt: 'chan
}); });
}); });
onUnmounted(() => { onBeforeUnmount(() => {
getso?.unset(); getso?.unset();
isDraging.value = false; isDraging.value = false;
}); });

View File

@ -1,4 +1,4 @@
import { onUnmounted, reactive, toRaw, watch } from 'vue'; import { onBeforeUnmount, reactive, toRaw, watch } from 'vue';
import { cloneDeep } from 'lodash-es'; import { cloneDeep } from 'lodash-es';
import type { EventOption } from '@tmagic/core'; import type { EventOption } from '@tmagic/core';
@ -165,7 +165,7 @@ export const initServiceState = (
}, },
); );
onUnmounted(() => { onBeforeUnmount(() => {
editorService.resetState(); editorService.resetState();
historyService.resetState(); historyService.resetState();
propsService.resetState(); propsService.resetState();
@ -405,7 +405,7 @@ export const initServiceEvents = (
depService.addTarget(createRelatedCompTarget(props.collectorOptions)); depService.addTarget(createRelatedCompTarget(props.collectorOptions));
} }
onUnmounted(() => { onBeforeUnmount(() => {
depService.off('add-target', targetAddHandler); depService.off('add-target', targetAddHandler);
depService.off('remove-target', targetRemoveHandler); depService.off('remove-target', targetRemoveHandler);
depService.off('dep-update', depUpdateHandler); depService.off('dep-update', depUpdateHandler);

View File

@ -19,7 +19,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { onMounted, onUnmounted, ref, watch } from 'vue'; import { onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { FullScreen } from '@element-plus/icons-vue'; import { FullScreen } from '@element-plus/icons-vue';
import { throttle } from 'lodash-es'; import { throttle } from 'lodash-es';
import * as monaco from 'monaco-editor'; import * as monaco from 'monaco-editor';
@ -188,7 +188,7 @@ onMounted(async () => {
init(); init();
}); });
onUnmounted(() => { onBeforeUnmount(() => {
resizeObserver.disconnect(); resizeObserver.disconnect();
}); });

View File

@ -15,7 +15,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, getCurrentInstance, inject, onMounted, onUnmounted, ref, watchEffect } from 'vue'; import { computed, getCurrentInstance, inject, onBeforeUnmount, onMounted, ref, watchEffect } from 'vue';
import { tMagicMessage } from '@tmagic/design'; import { tMagicMessage } from '@tmagic/design';
import type { FormState, FormValue } from '@tmagic/form'; import type { FormState, FormValue } from '@tmagic/form';
@ -64,7 +64,7 @@ onMounted(() => {
emit('mounted', internalInstance); emit('mounted', internalInstance);
}); });
onUnmounted(() => { onBeforeUnmount(() => {
services?.propsService.off('props-configs-change', init); services?.propsService.off('props-configs-change', init);
}); });

View File

@ -27,7 +27,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, inject, nextTick, onMounted, onUnmounted, ref, toRaw, watch } from 'vue'; import { computed, inject, nextTick, onBeforeUnmount, onMounted, ref, toRaw, watch } from 'vue';
import { ArrowLeftBold, ArrowRightBold, Plus } from '@element-plus/icons-vue'; import { ArrowLeftBold, ArrowRightBold, Plus } from '@element-plus/icons-vue';
import { NodeType } from '@tmagic/schema'; import { NodeType } from '@tmagic/schema';
@ -72,7 +72,7 @@ onMounted(() => {
pageBar.value && resizeObserver.observe(pageBar.value); pageBar.value && resizeObserver.observe(pageBar.value);
}); });
onUnmounted(() => { onBeforeUnmount(() => {
resizeObserver.disconnect(); resizeObserver.disconnect();
}); });

View File

@ -56,6 +56,8 @@ const unWatch = watch(
(stage) => { (stage) => {
if (!stage) return; if (!stage) return;
nextTick(() => unWatch());
stage.on('select', (el: HTMLElement, event: MouseEvent) => { stage.on('select', (el: HTMLElement, event: MouseEvent) => {
const els = stage.renderer.getElementsFromPoint(event) || []; const els = stage.renderer.getElementsFromPoint(event) || [];
const ids = els.map((el) => el.id).filter((id) => Boolean(id)); const ids = els.map((el) => el.id).filter((id) => Boolean(id));
@ -64,8 +66,6 @@ const unWatch = watch(
filterTextChangeHandler(ids); filterTextChangeHandler(ids);
}); });
unWatch();
}, },
{ {
immediate: true, immediate: true,

View File

@ -31,7 +31,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, inject, markRaw, nextTick, onMounted, onUnmounted, ref, toRaw, watch, watchEffect } from 'vue'; import { computed, inject, markRaw, nextTick, onBeforeUnmount, onMounted, ref, toRaw, watch, watchEffect } from 'vue';
import { cloneDeep } from 'lodash-es'; import { cloneDeep } from 'lodash-es';
import type { MApp, MContainer } from '@tmagic/schema'; import type { MApp, MContainer } from '@tmagic/schema';
@ -140,7 +140,7 @@ onMounted(() => {
} }
}); });
onUnmounted(() => { onBeforeUnmount(() => {
stage?.destroy(); stage?.destroy();
resizeObserver.disconnect(); resizeObserver.disconnect();
services?.editorService.set('stage', null); services?.editorService.set('stage', null);

View File

@ -26,7 +26,7 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { inject, onBeforeMount, Ref, ref, watch, watchEffect } from 'vue'; import { inject, nextTick, onBeforeMount, Ref, ref, watch, watchEffect } from 'vue';
import { TMagicSelect } from '@tmagic/design'; import { TMagicSelect } from '@tmagic/design';
import { getValueByKeyPath } from '@tmagic/utils'; import { getValueByKeyPath } from '@tmagic/utils';
@ -331,12 +331,15 @@ if (typeof props.config.options === 'function') {
} }
if (props.config.remote) { if (props.config.remote) {
const unWacth = watch( const unWatch = watch(
() => tMagicSelect.value?.scrollbarWrap, () => tMagicSelect.value?.scrollbarWrap,
(scrollbarWrap) => { (scrollbarWrap) => {
if (!scrollbarWrap) { if (!scrollbarWrap) {
return; return;
} }
nextTick(() => unWatch());
scrollbarWrap.addEventListener('scroll', async (e: Event) => { scrollbarWrap.addEventListener('scroll', async (e: Event) => {
const el = e.currentTarget as HTMLDivElement; const el = e.currentTarget as HTMLDivElement;
if (moreLoadingVisible.value) { if (moreLoadingVisible.value) {
@ -353,7 +356,6 @@ if (props.config.remote) {
options.value = await getOptions(); options.value = await getOptions();
moreLoadingVisible.value = false; moreLoadingVisible.value = false;
}); });
unWacth();
}, },
{ {
immediate: true, immediate: true,

View File

@ -16,7 +16,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { getCurrentInstance, inject, onMounted, onUnmounted } from 'vue'; import { getCurrentInstance, inject, onBeforeUnmount, onMounted } from 'vue';
import Core from '@tmagic/core'; import Core from '@tmagic/core';
@ -32,7 +32,7 @@ export default (props: any) => {
node?.emit('mounted', vm); node?.emit('mounted', vm);
}); });
onUnmounted(() => { onBeforeUnmount(() => {
node?.emit('destroy', vm); node?.emit('destroy', vm);
}); });

View File

@ -16,7 +16,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { inject, onMounted, onUnmounted } from 'vue'; import { inject, onBeforeUnmount, onMounted } from 'vue';
import Core from '@tmagic/core'; import Core from '@tmagic/core';
import type { MComponent } from '@tmagic/schema'; import type { MComponent } from '@tmagic/schema';
@ -43,7 +43,7 @@ export default ({ config, methods }: UseAppOptions) => {
node?.emit('mounted', emitData); node?.emit('mounted', emitData);
}); });
onUnmounted(() => { onBeforeUnmount(() => {
node?.emit('destroy', emitData); node?.emit('destroy', emitData);
}); });