Skip to content

Commit

Permalink
refactor(editor): useTemplateRef参数不能与变量名一样,不然会有一个warn
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Jan 9, 2025
1 parent 552df3a commit 1baec3f
Show file tree
Hide file tree
Showing 25 changed files with 140 additions and 138 deletions.
6 changes: 3 additions & 3 deletions packages/editor/src/components/CodeBlockEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ const { height: codeBlockEditorHeight } = useEditorContentHeight();
const difVisible = ref(false);
const { rect: windowRect } = useWindowRect();
const magicVsEditor = useTemplateRef<InstanceType<typeof CodeEditor>>('magicVsEditor');
const magicVsEditorRef = useTemplateRef<InstanceType<typeof CodeEditor>>('magicVsEditor');
const diffChange = () => {
if (!magicVsEditor.value || !formBox.value?.form) {
if (!magicVsEditorRef.value || !formBox.value?.form) {
return;
}
formBox.value.form.values.content = magicVsEditor.value.getEditorValue();
formBox.value.form.values.content = magicVsEditorRef.value.getEditorValue();
difVisible.value = false;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/CodeParams.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const props = defineProps<{
const emit = defineEmits(['change']);
const form = useTemplateRef<InstanceType<typeof MForm>>('form');
const formRef = useTemplateRef<InstanceType<typeof MForm>>('form');
const getFormConfig = (items: FormConfig = []) => [
{
Expand Down Expand Up @@ -61,7 +61,7 @@ const codeParamsConfig = computed(() =>
*/
const onParamsChangeHandler = async (v: FormValue, eventData: ContainerChangeEventData) => {
try {
const value = await form.value?.submitForm(true);
const value = await formRef.value?.submitForm(true);
emit('change', value, eventData);
} catch (e) {
error(e);
Expand Down
26 changes: 13 additions & 13 deletions packages/editor/src/components/ContentMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ const emit = defineEmits<{
mouseenter: [];
}>();
const menu = useTemplateRef<HTMLDivElement>('menu');
const buttons = useTemplateRef<InstanceType<typeof ToolButton>[]>('buttons');
const subMenu = useTemplateRef<any>('subMenu');
const menuEl = useTemplateRef<HTMLDivElement>('menu');
const buttonRefs = useTemplateRef<InstanceType<typeof ToolButton>[]>('buttons');
const subMenuRef = useTemplateRef<any>('subMenu');
const visible = ref(false);
const subMenuData = ref<(MenuButton | MenuComponent)[]>([]);
const zIndex = useZIndex();
Expand All @@ -88,13 +88,13 @@ const menuStyle = computed(() => ({
zIndex: curZIndex.value,
}));
const contains = (el: HTMLElement) => menu.value?.contains(el) || subMenu.value?.contains(el);
const contains = (el: HTMLElement) => menuEl.value?.contains(el) || subMenuRef.value?.contains(el);
const hide = () => {
if (!visible.value) return;
visible.value = false;
subMenu.value?.hide();
subMenuRef.value?.hide();
emit('hide');
};
Expand All @@ -121,7 +121,7 @@ const outsideClickHideHandler = (e: MouseEvent) => {
};
const setPosition = (e: { clientY: number; clientX: number }) => {
const menuHeight = menu.value?.clientHeight || 0;
const menuHeight = menuEl.value?.clientHeight || 0;
let top = e.clientY;
if (menuHeight + e.clientY > document.body.clientHeight) {
Expand Down Expand Up @@ -158,15 +158,15 @@ const showSubMenu = (item: MenuButton | MenuComponent, index: number) => {
return;
}
if (menu.value) {
if (menuEl.value) {
// 将子菜单放置在按钮右侧,与按钮齐平
let y = menu.value.offsetTop;
if (buttons.value?.[index].$el) {
const rect = buttons.value?.[index].$el.getBoundingClientRect();
let y = menuEl.value.offsetTop;
if (buttonRefs.value?.[index].$el) {
const rect = buttonRefs.value?.[index].$el.getBoundingClientRect();
y = rect.top;
}
subMenu.value?.show({
clientX: menu.value.offsetLeft + menu.value.clientWidth - 2,
subMenuRef.value?.show({
clientX: menuEl.value.offsetLeft + menuEl.value.clientWidth - 2,
clientY: y,
});
}
Expand All @@ -190,7 +190,7 @@ onBeforeUnmount(() => {
});
defineExpose({
menu,
menu: menuEl,
menuPosition,
hide,
show,
Expand Down
18 changes: 9 additions & 9 deletions packages/editor/src/components/FloatingBox.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<Teleport to="body" v-if="visible">
<div ref="target" class="m-editor-float-box" :style="{ ...style, zIndex: curZIndex }" @mousedown="nextZIndex">
<div ref="titleEl" class="m-editor-float-box-title">
<div ref="title" class="m-editor-float-box-title">
<slot name="title">
<span>{{ title }}</span>
</slot>
Expand Down Expand Up @@ -47,8 +47,8 @@ const props = withDefaults(
},
);
const target = useTemplateRef<HTMLDivElement>('target');
const titleEl = useTemplateRef<HTMLDivElement>('titleEl');
const targetEl = useTemplateRef<HTMLDivElement>('target');
const titleEl = useTemplateRef<HTMLDivElement>('title');
const zIndex = useZIndex();
const curZIndex = ref<number>(0);
Expand All @@ -59,8 +59,8 @@ const bodyHeight = computed(() => {
return height.value - titleHeight.value;
}
if (target.value) {
return target.value.clientHeight - titleHeight.value;
if (targetEl.value) {
return targetEl.value.clientHeight - titleHeight.value;
}
return 'auto';
Expand All @@ -87,7 +87,7 @@ let moveable: VanillaMoveable | null = null;
const initMoveable = () => {
moveable = new VanillaMoveable(globalThis.document.body, {
className: 'm-editor-floating-box-moveable',
target: target.value,
target: targetEl.value,
draggable: true,
resizable: true,
edge: true,
Expand Down Expand Up @@ -126,7 +126,7 @@ watch(
await nextTick();
curZIndex.value = zIndex.nextZIndex();
const targetRect = target.value?.getBoundingClientRect();
const targetRect = targetEl.value?.getBoundingClientRect();
if (targetRect) {
width.value = targetRect.width;
height.value = targetRect.height;
Expand Down Expand Up @@ -168,11 +168,11 @@ const nextZIndex = () => {
curZIndex.value = zIndex.nextZIndex();
};
provide('parentFloating', target);
provide('parentFloating', targetEl);
defineExpose({
bodyHeight,
target,
target: targetEl,
titleEl,
});
</script>
4 changes: 2 additions & 2 deletions packages/editor/src/components/Resizer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ const emit = defineEmits<{
change: [e: OnDrag];
}>();
const target = useTemplateRef<HTMLSpanElement>('target');
const { isDragging } = useGetSo(target, emit);
const targetEl = useTemplateRef<HTMLSpanElement>('target');
const { isDragging } = useGetSo(targetEl, emit);
</script>
13 changes: 6 additions & 7 deletions packages/editor/src/components/ScrollBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const props = defineProps<{
const emit = defineEmits(['scroll']);
const bar = useTemplateRef<HTMLDivElement>('bar');
const thumb = useTemplateRef<HTMLDivElement>('thumb');
const barEl = useTemplateRef<HTMLDivElement>('bar');
const thumbEl = useTemplateRef<HTMLDivElement>('thumb');
const thumbSize = computed(() => props.size * (props.size / props.scrollSize));
const thumbPos = computed(() => (props.pos / props.scrollSize) * props.size);
Expand All @@ -35,9 +35,8 @@ const thumbStyle = computed(() => ({
let gesto: Gesto;
onMounted(() => {
if (!thumb.value) return;
const thumbEl = thumb.value;
gesto = new Gesto(thumbEl, {
if (!thumbEl.value) return;
gesto = new Gesto(thumbEl.value, {
container: window,
});
Expand All @@ -50,12 +49,12 @@ onMounted(() => {
scrollBy(getDelta(e));
});
bar.value?.addEventListener('wheel', wheelHandler, false);
barEl.value?.addEventListener('wheel', wheelHandler, false);
});
onBeforeUnmount(() => {
if (gesto) gesto.off();
bar.value?.removeEventListener('wheel', wheelHandler, false);
barEl.value?.removeEventListener('wheel', wheelHandler, false);
});
const wheelHandler = (e: WheelEvent) => {
Expand Down
12 changes: 6 additions & 6 deletions packages/editor/src/components/ScrollViewer.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="m-editor-scroll-viewer-container" ref="container">
<div ref="el" :style="style">
<div ref="target" :style="style">
<slot></slot>
</div>

Expand Down Expand Up @@ -63,8 +63,8 @@ const props = withDefaults(
},
);
const container = useTemplateRef<HTMLDivElement>('container');
const el = useTemplateRef<HTMLDivElement>('el');
const containerEl = useTemplateRef<HTMLDivElement>('container');
const el = useTemplateRef<HTMLDivElement>('target');
const style = computed(
() => `
width: ${isNumber(`${props.width}`) ? `${props.width}px` : props.width};
Expand All @@ -80,9 +80,9 @@ const scrollHeight = ref(0);
let scrollViewer: ScrollViewer;
onMounted(() => {
if (!container.value || !el.value) return;
if (!containerEl.value || !el.value) return;
scrollViewer = new ScrollViewer({
container: container.value,
container: containerEl.value,
target: el.value,
zoom: props.zoom,
correctionScrollSize: props.correctionScrollSize,
Expand Down Expand Up @@ -123,6 +123,6 @@ const hScrollHandler = (delta: number) => {
};
defineExpose({
container,
container: containerEl,
});
</script>
4 changes: 2 additions & 2 deletions packages/editor/src/components/SplitView.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div ref="el" class="m-editor-layout" :style="`min-width: ${props.minCenter + props.minLeft + props.minRight}px`">
<div ref="target" class="m-editor-layout" :style="`min-width: ${props.minCenter + props.minLeft + props.minRight}px`">
<template v-if="hasLeft && $slots.left">
<div class="m-editor-layout-left" :class="leftClass" :style="`width: ${left}px`">
<slot name="left"></slot>
Expand Down Expand Up @@ -51,7 +51,7 @@ const props = withDefaults(
},
);
const el = useTemplateRef<HTMLElement>('el');
const el = useTemplateRef<HTMLElement>('target');
const hasLeft = computed(() => typeof props.left !== 'undefined');
const hasRight = computed(() => typeof props.right !== 'undefined');
Expand Down
6 changes: 3 additions & 3 deletions packages/editor/src/fields/DataSourceInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ const emit = defineEmits<{
const { dataSourceService } = inject<Services>('services') || {};
const autocomplete = useTemplateRef<InstanceType<typeof TMagicAutocomplete>>('autocomplete');
const autocompleteRef = useTemplateRef<InstanceType<typeof TMagicAutocomplete>>('autocomplete');
const isFocused = ref(false);
const state = ref('');
const displayState = ref<{ value: string; type: 'var' | 'text' }[]>([]);
const input = computed<HTMLInputElement>(() => autocomplete.value?.inputRef?.input);
const input = computed<HTMLInputElement>(() => autocompleteRef.value?.inputRef?.input);
const dataSources = computed(() => dataSourceService?.get('dataSources') || []);
const setDisplayState = () => {
Expand All @@ -112,7 +112,7 @@ const mouseupHandler = async () => {
isFocused.value = true;
await nextTick();
autocomplete.value?.focus();
autocompleteRef.value?.focus();
if (focusOffset && input.value) {
input.value.setSelectionRange(anchorOffset, focusOffset);
Expand Down
8 changes: 4 additions & 4 deletions packages/editor/src/fields/DataSourceMethods.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const props = withDefaults(
const emit = defineEmits(['change']);
const codeConfig = ref<CodeBlockContent>();
const codeBlockEditor = useTemplateRef<InstanceType<typeof CodeBlockEditor>>('codeBlockEditor');
const codeBlockEditorRef = useTemplateRef<InstanceType<typeof CodeBlockEditor>>('codeBlockEditor');
let editIndex = -1;
Expand Down Expand Up @@ -94,7 +94,7 @@ const methodColumns: ColumnConfig[] = [
editIndex = index;
nextTick(() => {
codeBlockEditor.value?.show();
codeBlockEditorRef.value?.show();
});
},
},
Expand All @@ -121,7 +121,7 @@ const createCodeHandler = () => {
editIndex = -1;
nextTick(() => {
codeBlockEditor.value?.show();
codeBlockEditorRef.value?.show();
});
};
Expand Down Expand Up @@ -157,6 +157,6 @@ const submitCodeHandler = (value: CodeBlockContent, data: ContainerChangeEventDa
editIndex = -1;
codeConfig.value = void 0;
codeBlockEditor.value?.hide();
codeBlockEditorRef.value?.hide();
};
</script>
10 changes: 5 additions & 5 deletions packages/editor/src/hooks/use-code-block-edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { CodeBlockService } from '@editor/services/codeBlock';
export const useCodeBlockEdit = (codeBlockService?: CodeBlockService) => {
const codeConfig = ref<CodeBlockContent>();
const codeId = ref<string>();
const codeBlockEditor = useTemplateRef<InstanceType<typeof CodeBlockEditor>>('codeBlockEditor');
const codeBlockEditorRef = useTemplateRef<InstanceType<typeof CodeBlockEditor>>('codeBlockEditor');

// 新增代码块
const createCodeBlock = async () => {
Expand All @@ -29,7 +29,7 @@ export const useCodeBlockEdit = (codeBlockService?: CodeBlockService) => {

await nextTick();

codeBlockEditor.value?.show();
codeBlockEditorRef.value?.show();
};

// 编辑代码块
Expand All @@ -54,7 +54,7 @@ export const useCodeBlockEdit = (codeBlockService?: CodeBlockService) => {
codeId.value = id;

await nextTick();
codeBlockEditor.value?.show();
codeBlockEditorRef.value?.show();
};

// 删除代码块
Expand All @@ -67,13 +67,13 @@ export const useCodeBlockEdit = (codeBlockService?: CodeBlockService) => {

await codeBlockService?.setCodeDslById(codeId.value, values);

codeBlockEditor.value?.hide();
codeBlockEditorRef.value?.hide();
};

return {
codeId,
codeConfig,
codeBlockEditor,
codeBlockEditor: codeBlockEditorRef,

createCodeBlock,
editCode,
Expand Down
Loading

0 comments on commit 1baec3f

Please sign in to comment.