vben
2021-01-28 9edc281322772f6b5ec8093c40fdf90408d85da7
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<template>
  <div :class="prefixCls" :style="{ width: containerWidth }">
    <ImgUpload
      @uploading="handleImageUploading"
      @done="handleDone"
      v-if="showImageUpload"
      v-show="editorRef"
    />
    <textarea :id="tinymceId" ref="elRef" :style="{ visibility: 'hidden' }"></textarea>
  </div>
</template>
 
<script lang="ts">
  import {
    defineComponent,
    computed,
    nextTick,
    ref,
    unref,
    watch,
    onUnmounted,
    onDeactivated,
  } from 'vue';
  import { basicProps } from './props';
  import toolbar from './toolbar';
  import plugins from './plugins';
  import { getTinymce } from './getTinymce';
  import { useScript } from '/@/hooks/web/useScript';
  import { snowUuid } from '/@/utils/uuid';
  import { bindHandlers } from './helper';
  import lineHeight from './lineHeight';
  import { onMountedOrActivated } from '/@/hooks/core/onMountedOrActivated';
  import ImgUpload from './ImgUpload.vue';
  import { useDesign } from '/@/hooks/web/useDesign';
 
  const CDN_URL = 'https://cdn.bootcdn.net/ajax/libs/tinymce/5.5.1';
 
  const tinymceScriptSrc = `${CDN_URL}/tinymce.min.js`;
 
  export default defineComponent({
    name: 'Tinymce',
    components: { ImgUpload },
    inheritAttrs: false,
    props: basicProps,
    emits: ['change', 'update:modelValue'],
    setup(props, { emit, attrs }) {
      const editorRef = ref<any>(null);
      const tinymceId = ref<string>(snowUuid('tiny-vue'));
      const elRef = ref<Nullable<HTMLElement>>(null);
 
      const { prefixCls } = useDesign('tinymce-container');
 
      const tinymceContent = computed(() => {
        return props.modelValue;
      });
 
      const containerWidth = computed(() => {
        const width = props.width;
        if (/^[\d]+(\.[\d]+)?$/.test(width.toString())) {
          return `${width}px`;
        }
        return width;
      });
 
      const initOptions = computed(() => {
        const { height, options } = props;
        return {
          selector: `#${unref(tinymceId)}`,
          base_url: CDN_URL,
          suffix: '.min',
          height: height,
          toolbar: toolbar,
          menubar: 'file edit insert view format table',
          plugins: plugins,
          // 语言包
          language_url: 'resource/tinymce/langs/zh_CN.js',
          // 中文
          language: 'zh_CN',
          default_link_target: '_blank',
          link_title: false,
          advlist_bullet_styles: 'square',
          advlist_number_styles: 'default',
          object_resizing: false,
          fontsize_formats: '10px 11px 12px 14px 16px 18px 20px 24px 36px 48px',
          lineheight_formats: '1 1.5 1.75 2.0 3.0 4.0 5.0',
          ...options,
          setup: (editor: any) => {
            editorRef.value = editor;
            editor.on('init', (e: Event) => initSetup(e));
          },
        };
      });
 
      const { toPromise } = useScript({
        src: tinymceScriptSrc,
      });
 
      watch(
        () => attrs.disabled,
        () => {
          const editor = unref(editorRef);
          if (!editor) return;
          editor.setMode(attrs.disabled ? 'readonly' : 'design');
        }
      );
      onMountedOrActivated(() => {
        tinymceId.value = snowUuid('tiny-vue');
        nextTick(() => {
          init();
        });
      });
 
      onUnmounted(() => {
        destory();
      });
 
      onDeactivated(() => {
        destory();
      });
 
      function destory() {
        if (getTinymce() !== null) {
          getTinymce()?.remove?.(unref(editorRef));
        }
      }
 
      function init() {
        toPromise().then(() => {
          setTimeout(() => {
            initEditor();
          }, 0);
        });
      }
 
      function initEditor() {
        getTinymce().PluginManager.add('lineHeight', lineHeight(getTinymce()));
        const el = unref(elRef);
        if (el) {
          el.style.visibility = '';
        }
        getTinymce().init(unref(initOptions));
      }
 
      function initSetup(e: Event) {
        const editor = unref(editorRef);
        if (!editor) return;
        const value = props.modelValue || '';
 
        editor.setContent(value);
        bindModelHandlers(editor);
        bindHandlers(e, attrs, unref(editorRef));
      }
 
      function setValue(editor: Recordable, val: string, prevVal?: string) {
        if (
          editor &&
          typeof val === 'string' &&
          val !== prevVal &&
          val !== editor.getContent({ format: attrs.outputFormat })
        ) {
          editor.setContent(val);
        }
      }
 
      function bindModelHandlers(editor: any) {
        const modelEvents = attrs.modelEvents ? attrs.modelEvents : null;
        const normalizedEvents = Array.isArray(modelEvents) ? modelEvents.join(' ') : modelEvents;
 
        watch(
          () => props.modelValue,
          (val: string, prevVal: string) => {
            setValue(editor, val, prevVal);
          }
        );
 
        watch(
          () => props.value,
          (val: string, prevVal: string) => {
            setValue(editor, val, prevVal);
          },
          {
            immediate: true,
          }
        );
 
        editor.on(normalizedEvents ? normalizedEvents : 'change keyup undo redo', () => {
          const content = editor.getContent({ format: attrs.outputFormat });
          emit('update:modelValue', content);
          emit('change', content);
        });
      }
 
      function handleImageUploading(name: string) {
        const editor = unref(editorRef);
        if (!editor) return;
        const content = editor?.getContent() ?? '';
        setValue(editor, `${content}\n${getImgName(name)}`);
      }
 
      function handleDone(name: string, url: string) {
        const editor = unref(editorRef);
        if (!editor) return;
 
        const content = editor?.getContent() ?? '';
        const val = content?.replace(getImgName(name), `<img src="${url}"/>`) ?? '';
        setValue(editor, val);
      }
 
      function getImgName(name: string) {
        return `[uploading:${name}]`;
      }
 
      return {
        prefixCls,
        containerWidth,
        initOptions,
        tinymceContent,
        tinymceScriptSrc,
        elRef,
        tinymceId,
        handleImageUploading,
        handleDone,
        editorRef,
      };
    },
  });
</script>
 
<style lang="less" scoped></style>
 
<style lang="less">
  @prefix-cls: ~'@{namespace}-tinymce-container';
 
  .@{prefix-cls} {
    position: relative;
    line-height: normal;
 
    textarea {
      z-index: -1;
      visibility: hidden;
    }
  }
</style>