wangeditor v4版本,基于vue3项目,实现动态拉伸编辑区大小

搜了一圈,没有找到合适的答案,自己研究写了一个,思路对了,写起来就比较简单

直接上代码,index.vue

<template>
  <div style="position: relative">
    <Editor
      ref="targetRef"
      v-bind="$attrs"
      :style="editorStyle"
      @onCreated="handleCreated"
      @onChange="handleChange"
    />
    <span ref="resizeRef" class="is-resize"> icon占位 </span>
  </div>
</template>
<script setup lang="ts">
import "@wangeditor/editor/dist/css/style.css"; // 引入 css
import { onBeforeUnmount, shallowRef, computed, ref, CSSProperties } from "vue";
import { IDomEditor } from "@wangeditor/editor";
import { Editor } from "@wangeditor/editor-for-vue";
import { useResize } from "./use-resize";

const props = defineProps(["resize", "editorHeight"]);
const editorHeight = computed(() => `${props.editorHeight}px`);
const editorRef = shallowRef<IDomEditor>();
const targetRef = ref<HTMLElement>();
const resizeRef = ref<HTMLElement>();
const resize = computed(() => props.resize);
useResize(targetRef, resizeRef, resize);
const editorStyle = ref<CSSProperties>({ height: editorHeight.value });

// 编辑器创建完毕时的回调函数。
const handleCreated = (editor: IDomEditor) => {
  editorRef.value = editor; // 记录 editor 实例,重要!
};
// 编辑器内容、选区变化时的回调函数。
const handleChange = (editor: IDomEditor) => {
  if (targetRef.value && resize.value) {
    editorStyle.value = { height: targetRef.value.box.style.height };
  }
};

// 组件销毁时,也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (editor == null) return;
  editor.destroy();
});
</script>

<style lang="scss">
.is-resize {
  position: absolute;
  right: 0;
  bottom: 0;
  cursor: ns-resize;
  user-select: none;
}
</style>

use-resize.ts

import { onBeforeUnmount, onMounted, watchEffect } from "vue";
import type { ComputedRef, Ref } from "vue";

export const useResize = (
  targetRef: Ref<HTMLElement | undefined>,
  resizeRef: Ref<HTMLElement | undefined>,
  resize: ComputedRef<boolean>
) => {
  const minHeight = 260;

  const onMousedown = (e: MouseEvent) => {
    const downY = e.clientY;
    const targetRect = targetRef.value!.box.getBoundingClientRect();
    const targetTop = targetRect.top;
    const targetHeight = targetRect.height;
    const clientHeight = document.documentElement.clientHeight;

    const maxHeight = -targetTop + clientHeight;

    const onMousemove = (e: MouseEvent) => {
      // console.log('onMousemove')

      e.preventDefault();

      const height = Math.min(
        Math.max(e.clientY - downY + targetHeight, minHeight),
        maxHeight
      );

      targetRef.value!.box.style.height = height + "px";
    };

    const onMouseup = () => {
      document.removeEventListener("mousemove", onMousemove);
      document.removeEventListener("mouseup", onMouseup);
    };

    document.addEventListener("mousemove", onMousemove);
    document.addEventListener("mouseup", onMouseup);
  };

  const onResize = () => {
    if (resizeRef.value && targetRef.value) {
      resizeRef.value.addEventListener("mousedown", onMousedown);
    }
  };

  const offResize = () => {
    if (resizeRef.value && targetRef.value) {
      resizeRef.value.removeEventListener("mousedown", onMousedown);
    }
  };

  onMounted(() => {
    watchEffect(() => {
      if (resize.value) {
        onResize();
      } else {
        offResize();
      }
    });
  });

  onBeforeUnmount(() => {
    offResize();
  });
};

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值