干燥机配套车间生产管理系统/云平台前端
baoshiwei
2023-03-10 1fb197352b6a263646e4ccd3ed1c7854ede031dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<template>
  <MarkDown v-bind="bindProps" @change="onChange" @get="onGetVditor" />
</template>
 
<script lang="ts">
  import { computed, defineComponent, watch } from 'vue';
  import { MarkDown } from '/@/components/Markdown';
  import { propTypes } from '/@/utils/propTypes';
 
  export default defineComponent({
    name: 'JMarkdownEditor',
    // 不将 attrs 的属性绑定到 html 标签上
    inheritAttrs: false,
    components: { MarkDown },
    props: {
      value: propTypes.string.def(''),
      disabled: propTypes.bool.def(false),
    },
    emits: ['change', 'update:value'],
    setup(props, { emit, attrs }) {
      // markdown 组件实例
      let mdRef: any = null;
      // vditor 组件实例
      let vditorRef: any = null;
      // 合并 props 和 attrs
      const bindProps = computed(() => Object.assign({}, props, attrs));
 
      // 相当于 onMounted
      function onGetVditor(instance) {
        mdRef = instance;
        vditorRef = mdRef.getVditor();
 
        // 监听禁用,切换编辑器禁用状态
        watch(
          () => props.disabled,
          (disabled) => (disabled ? vditorRef.disabled() : vditorRef.enable()),
          { immediate: true }
        );
      }
 
      // value change 事件
      function onChange(value) {
        emit('change', value);
        emit('update:value', value);
      }
 
      return {
        bindProps,
 
        onChange,
        onGetVditor,
      };
    },
  });
</script>
 
<style lang="less" scoped></style>