<template>
<div :class="prefixCls" :style="{ width: containerWidth }">
<ImgUpload :fullscreen="fullscreen" @uploading="handleImageUploading" @done="handleDone" @insert="handleInsertImg"
v-if="showImageUpload" v-show="editorRef" :disabled="disabled" />
<textarea :id="tinymceId" ref="elRef" :style="{ visibility: 'hidden' }" v-if="!initOptions.inline"></textarea>
<Sqltext :visible="SqltextOpen" :businessType="businessType" :textreavalue="value" v-if="SqltextOpen"
@close="CloseSqltext" />
<Sqltable :visible="SqltableOpen" :businessType="businessType" :textreavalue="value" v-if="SqltableOpen"
@close="CloseSqltable" />
<slot v-else></slot>
</div>
</template>
<script lang="ts">
import type { Editor, RawEditorSettings } from 'tinymce';
import tinymce from 'tinymce/tinymce';
import 'tinymce/themes/silver';
import 'tinymce/icons/default/icons';
import 'tinymce/plugins/advlist';
import 'tinymce/plugins/anchor';
import 'tinymce/plugins/autolink';
import 'tinymce/plugins/autosave';
import 'tinymce/plugins/code';
import 'tinymce/plugins/codesample';
import 'tinymce/plugins/directionality';
import 'tinymce/plugins/fullscreen';
import 'tinymce/plugins/hr';
import 'tinymce/plugins/insertdatetime';
import 'tinymce/plugins/link';
import 'tinymce/plugins/lists';
import 'tinymce/plugins/media';
import 'tinymce/plugins/nonbreaking';
import 'tinymce/plugins/noneditable';
import 'tinymce/plugins/pagebreak';
import 'tinymce/plugins/paste';
import 'tinymce/plugins/preview';
import 'tinymce/plugins/print';
import 'tinymce/plugins/save';
import 'tinymce/plugins/searchreplace';
import 'tinymce/plugins/spellchecker';
import 'tinymce/plugins/tabfocus';
import 'tinymce/plugins/table';
import 'tinymce/plugins/template';
import 'tinymce/plugins/textpattern';
import 'tinymce/plugins/visualblocks';
import 'tinymce/plugins/visualchars';
import 'tinymce/plugins/wordcount';
import 'tinymce/plugins/image';
import 'tinymce/plugins/charmap';
import 'tinymce/plugins/imagetools';
import 'tinymce/plugins/help';
import 'tinymce/plugins/quickbars';
import 'tinymce/plugins/importcss';
import 'tinymce/plugins/toc';
import 'tinymce/plugins/emoticons';
import { defineComponent, computed, nextTick, ref, unref, watch, onDeactivated, onBeforeUnmount, onMounted } from 'vue';
import ImgUpload from './ImgUpload.vue';
import { toolbar, plugins } from './tinymce';
import { buildShortUUID } from '@/utils/uuid';
import { bindHandlers } from './helper';
import { onMountedOrActivated } from '@/hooks/core/onMountedOrActivated';
import { useDesign } from '@/hooks/web/useDesign';
import { isNumber } from '@/utils/is';
import { useLocale } from '@/locales/useLocale';
import { useAppStore } from '@/store/modules/app';
import { useDebounceFn } from '@vueuse/core';
import Sqltext from './Sqltext.vue';
import Sqltable from './Sqltable.vue';
import { useI18n } from '@/hooks/web/useI18n';
const { t } = useI18n();
const tinymceProps = {
options: {
type: Object as PropType<Partial<RawEditorSettings>>,
default: () => ({}),
},
value: {
type: [String, Array, Object],
},
placeholder: {
type: String,
default: '请输入',
},
toolbar: {
type: [String, Array] as PropType<string | string[]>,
default: toolbar,
},
plugins: {
type: [String, Array] as PropType<string | string[]>,
default: plugins,
},
height: {
type: [Number, String] as PropType<string | number>,
required: false,
default: 400,
},
width: {
type: [Number, String] as PropType<string | number>,
required: false,
default: 'auto',
},
showImageUpload: {
type: Boolean,
default: true,
},
businessType: {
// 模板类型
type: [Number, String, undefined] as PropType<string | Number | undefined>,
default: undefined,
},
};
export default defineComponent({
name: 'JnpfEditor',
components: { ImgUpload, Sqltext, Sqltable },
inheritAttrs: false,
props: tinymceProps,
emits: ['change', 'update:value', 'inited', 'init-error', 'checkboxeditor', 'checkitemeditor'],
setup(props, { emit, attrs }) {
const editorRef = ref<Nullable<Editor>>(null);
const fullscreen = ref(false);
const SqltextOpen = ref(false);
const SqltableOpen = ref(false);
const tinymceId = ref<string>(buildShortUUID('tiny-vue'));
const elRef = ref<Nullable<HTMLElement>>(null);
const business = ref(null);
const { prefixCls } = useDesign('tinymce-container');
const appStore = useAppStore();
const debounceUpdateEditor = useDebounceFn(updateEditor, 300);
const tinymceContent = computed(() => props.value);
const containerWidth = computed(() => {
const width = props.width;
if (isNumber(width)) {
return `${width}px`;
}
return width;
});
const skinName = computed(() => {
return appStore.getDarkMode === 'light' ? 'oxide' : 'oxide-dark';
});
const langName = computed(() => {
const lang = useLocale().getLocale.value;
return ['zh_CN', 'en_US'].includes(lang) ? lang : 'zh_CN';
});
const initOptions = computed((): RawEditorSettings => {
const { height, options, toolbar, plugins, placeholder, businessType } = props;
const publicPath = import.meta.env.VITE_PUBLIC_PATH || '/';
return {
selector: `#${unref(tinymceId)}`,
height,
toolbar,
menubar: 'file edit insert view format table',
plugins,
language_url: publicPath + 'resource/tinymce/langs/' + langName.value + '.js',
language: langName.value,
branding: false,
default_link_target: '_blank',
link_title: false,
object_resizing: true,
paste_data_images: true, // 允许粘贴图像
auto_focus: undefined,
skin: skinName.value,
skin_url: publicPath + 'resource/tinymce/skins/ui/' + skinName.value,
content_css: publicPath + 'resource/tinymce/skins/ui/' + skinName.value + '/content.min.css',
...options,
setup: (editor: Editor) => {
console.log(editor, 'setup',);
let mybutton = t('contract.Webcontractterm.mybutton')
// 添加自定义按钮 数据库字段
editor.ui.registry.addButton('mybutton', {
tooltip: mybutton,
icon: 'table', // 使用内置的图标(例如:'bold'、'italic'等)
onAction: function () {
SqltextOpen.value = true;
},
});
// editor.ui.registry.addButton('Buttonfile', {
// // text: 'My Button', // 按钮的文本
// icon: '<svg width="16" height="16" fill="currentColor" class="bi bi-star" viewBox="0 0 16 16"><path d="M8 12.27l3.46 2.03-.93-3.56L14 6.89l-3.62-.03L8 2.5l-1.38 4.36L2 6.89l3.47 3.88-.93 3.56L8 12.27z"/></svg>', // 自定义 SVG 图标
// onAction: function () {
// // 按钮点击后的操作
// editor.insertContent('<p>这是自定义按钮的内容!</p>');
// },
// });
editor.ui.registry.addButton('Buttonfileother', {
// 数据库表名
// tooltip: t('contract.Webcontractterm.Buttonfileother'),
icon: 'table', // 设置图标类名
onAction: function () {
SqltableOpen.value = true;
// 按钮点击后的操作
// editor.insertContent('<p>这是自定义按钮的内容!</p>');
},
});
// console.log(editor, 'ig');
editorRef.value = editor;
editor.on('init', e => initSetup(e));
editor.on('NodeChange', () => {
bindInputEventListeners(editor); // 重新绑定事件,确保动态元素被捕获
});
},
deprecation_warnings: false,
placeholder,
};
});
// 绑定输入框事件
function bindInputEventListeners(ed: any) {
// 获取编辑器内所有 input/textarea 元素
const inputElements = ed.getDoc().querySelectorAll('input, textarea,td');
inputElements.forEach(input => {
// 避免重复绑定事件
if (input['__eventBound']) return;
// 监听输入事件(input/change)
input.addEventListener('input', handleInputEvent);
input.addEventListener('change', handleInputEvent);
// 标记已绑定事件
input['__eventBound'] = true;
});
}
// 输入事件处理函数
function handleInputEvent(e: Event) {
try {
// as HTMLInputElement | HTMLTextAreaElement;
const input: any = e.target;
const editor = tinymce.activeEditor; // 获取当前编辑器实例
if (!editor) return;
// 打印输入内容(可替换为业务逻辑)
console.log('输入框值变化:', {
id: input.id,
name: input.name,
value: input.value,
eventType: e.type,
});
const parser = new DOMParser();
const doc = parser.parseFromString(editor.getContent(), 'text/html');
// 查找所有 input 元素
const inputs:any = doc.querySelectorAll('input');
inputs.forEach(input1 => {
// console.log(input.getAttribute('id'));
if (input1.getAttribute('id') == input.id) {
input.setAttribute('value', input.value);
input1?.onfocus();
}
});
// 触发 Vue 数据更新(示例)
if (editor) {
editor.setContent(editor.getContent()); // 强制更新编辑器内容,触发 v-model 同步
}
} catch (error) { }
}
const disabled = computed(() => {
const { options } = props;
const getDisabled = (options && Reflect.get(options, 'readonly')) || attrs.disabled;
const editor = unref(editorRef);
if (editor) {
editor.setMode(getDisabled ? 'readonly' : 'design');
}
return !!getDisabled || false;
});
watch(
() => attrs.disabled,
() => {
const editor = unref(editorRef);
if (!editor) {
return;
}
editor.setMode(attrs.disabled ? 'readonly' : 'design');
},
);
watch(
() => editorRef.value,
() => {
const editor = unref(editorRef);
if (!editor) {
return;
}
editor.setMode(attrs.disabled ? 'readonly' : 'design');
},
);
watch(
() => appStore.getDarkMode,
() => {
updateEditor();
},
);
watch(
() => props.placeholder,
() => {
debounceUpdateEditor();
},
);
onMountedOrActivated(() => {
if (!initOptions.value.inline) {
tinymceId.value = buildShortUUID('tiny-vue');
}
nextTick(() => {
setTimeout(() => {
initEditor();
}, 30);
});
});
onBeforeUnmount(() => {
destroy();
});
onMounted(() => { });
onDeactivated(() => {
destroy();
});
function destroy() {
if (tinymce !== null) {
tinymce?.remove?.(unref(initOptions).selector!);
}
}
function initEditor() {
const el = unref(elRef);
if (el) {
el.style.visibility = '';
}
tinymce
.init(unref(initOptions))
.then(editor => {
emit('inited', editor);
})
.catch(err => {
emit('init-error', err);
});
}
function initSetup(e) {
console.log(e, 'eeeeeee');
const editor = unref(editorRef);
if (!editor) {
return;
}
const value = props.value || '';
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.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:value', content);
emit('change', content);
});
editor.on('FullscreenStateChanged', e => {
fullscreen.value = e.state;
});
}
function handleInsertImg(url: string) {
const editor = unref(editorRef);
if (!editor || !url) {
return;
}
editor.execCommand('mceInsertContent', false, `<img src="${url}"/>`);
const content = editor?.getContent() ?? '';
setValue(editor, content);
}
function handleImageUploading(name: string) {
const editor = unref(editorRef);
if (!editor) {
return;
}
editor.execCommand('mceInsertContent', false, getUploadingImgName(name));
const content = editor?.getContent() ?? '';
setValue(editor, content);
}
function handleDone(name: string, url: string) {
const editor = unref(editorRef);
if (!editor) {
return;
}
const content = editor?.getContent() ?? '';
const val = content?.replace(getUploadingImgName(name), `<img src="${url}"/>`) ?? '';
setValue(editor, val);
}
function getUploadingImgName(name: string) {
return `[uploading:${name}]`;
}
function updateEditor() {
destroy();
initEditor();
}
function CloseSqltext(e: Boolean, data: any, checkdata: any) {
// console.log(tinymceId.value, document.getElementById(tinymceId.value).focus())
// console.log(tinymce, editorRef.value, editorRef.value.selection,)
// setValueAll(editorRef.value.getContent(), data)
SqltextOpen.value = false;
try {
if (e) {
// const editor = elRef.value.editor
editorRef.value?.insertContent(data);
console.log(editorRef.value?.getContent());
// props.value = editorRef.value?.getContent()
emit('update:value', editorRef.value?.getContent());
emit('change', editorRef.value?.getContent());
emit('checkitemeditor', editorRef.value?.getContent());
}
} catch (error) {
// console.error('handleInsertImg error:', error);
}
}
function CloseSqltable(e: Boolean, data: any, checkdata: any) {
SqltableOpen.value = false;
if (e) {
editorRef.value?.insertContent(data);
// props.value = editorRef.value?.getContent()
emit('update:value', editorRef.value?.getContent());
emit('change', editorRef.value?.getContent());
emit('checkitemeditor', checkdata);
}
}
return {
prefixCls,
containerWidth,
initOptions,
tinymceContent,
elRef,
tinymceId,
handleInsertImg,
handleImageUploading,
handleDone,
editorRef,
fullscreen,
disabled,
SqltextOpen,
CloseSqltext,
SqltableOpen,
CloseSqltable,
business,
};
},
});
</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;
}
}
/* 定义图标样式 */
.tox-tbtn .tox-icon.tox-icon--my-icon {
background-image: url('https://pic4.zhimg.com/v2-4d9e9f936b9968f53be22b594aafa74f_r.jpg');
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
</style>
tooltip: mybutton mybutton代码没有执行