const tl_rtc_app_module_cut_paste_content = { props : { isMobile: { type: Boolean, default: false }, otherSetting: { type: Object, default: function(){ return {} } }, user: { type: Object, default: function(){ return {} } }, socket: { type: Object, default: function(){ return {} } }, leftModule: { type: String, default: '' }, rightModule: { type: String, default: '' } }, computed: { propsUser(){ return this.user; }, propsIsMobile(){ return this.isMobile; }, propsOtherSetting(){ return this.otherSetting; }, propsSocket(){ return this.socket; }, propsLeftModule(){ return this.leftModule; }, propsRightModule(){ return this.rightModule; }, }, watch: { leftModule: function (val) { }, rightModule: function (val) { } }, data : function(){ return { title: '', cutPasteId: '', code: '', status: '', cutPasteDetailList: [], maxInputContentLength: 65000, // 约等于 64kb showMode: 'textarea', // 显示界面内容,textarea, histroy cutPasteShareHashKey: 'cut-paste-share', lang: 'txt', currentCutPaste: { id: -1, content: '', curInputContent: '', curFormatContent: '', preInputContent: '', preFormatContent: '', inputContentSaveTime: '', createTimeFormat: '', type: '', } } }, methods: { /** * 事件触发,向上传递 * @param {*} event */ leftModuleChange: function(event){ this.$emit('left-module-change', event) }, /** * 事件触发,向上传递 * @param {*} event */ rightModuleChange: function(event){ this.$emit('right-module-change', event) }, mouseEnter: function(event, title){ tl_rtc_app_comm.mouseEnterTips({ id: event.target.id, text: title, position: 'bottom' }); }, mouseLeave: function(){ tl_rtc_app_comm.mouseLeaveTips(); }, /** * 回退 */ goBack: function(){ this.emitSubModuleEvent({ event: 'sub-module-core-change-module-show', data: { showRightModule: false, } }) }, /** * 分享剪贴板 */ shareCutPaste: function(){ let that = this layer.open({ type: 1, title: '分享剪贴板', closeBtn: 0, skin: 'tl-rtc-app-layer-cut-paste-share', content: `
' + transferContent + ''
},
/**
* 编辑内容
* @param {*} event
*/
cutPastePreChange(event) {
// 直接获取当前光标位置
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const offset = range.startOffset;
const node = range.startContainer;
// 更新内容
this.currentCutPaste.curInputContent = event.target.innerText;
this.currentCutPaste.curFormatContent = this.currentCutPaste.curInputContent;
// 在下一个 tick 恢复光标位置
this.$nextTick(() => {
try {
const newRange = document.createRange();
newRange.setStart(node, offset);
newRange.setEnd(node, offset);
selection.removeAllRanges();
selection.addRange(newRange);
} catch(e) {
console.warn('Failed to restore cursor position:', e);
}
});
},
/**
* 设置剪贴板密码
*/
setCutPastePassword: async function(){
let that = this
layer.prompt({
formType: 0,
title: "设置剪贴板密码",
btn : ['确定', '取消'],
value: "",
shadeClose : true,
skin: 'layui-layer-prompt tl-rtc-app-layer-cut-paste-set-password',
maxlength : 15,
}, async function (value, index, elem) {
value = value.trim()
if(value.length > 20){
that.popWarningMsg("密码长度不能超过20位")
return false;
}
const params = {
cutPasteId: that.cutPasteId,
password: value
}
if(!window.tl_rtc_app_comm.checkRequestParams(params)){
that.popErrorMsg("请求参数非法")
return
}
const { data: updateRes } = await that.tlRequest({
url: '/api/web/cut-paste/update-cut-paste-password',
method: 'post',
useCache: false,
data: params,
})
if(!updateRes.success){
that.popErrorMsg(updateRes.msg)
layer.close(index)
return
}
that.popSuccessMsg(updateRes.msg)
layer.close(index)
return false
});
},
// 添加光标位置保存和恢复方法
saveCaretPosition(element) {
const selection = window.getSelection();
if (selection.rangeCount > 0) {
return selection.getRangeAt(0);
}
return null;
},
//
restoreCaretPosition(element, range) {
if (range) {
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
},
},
mounted: async function() {
let that = this
// 定时,每隔5秒保存一次输入内容
setInterval(async () => {
if(that.propsLeftModule !== 'cut_paste' || that.propsRightModule !== 'content'){
return
}
await this.autoSaveCutPasteContent()
}, 1000 * 5);
},
created(){
// 监听打开剪贴板内容
window.subModule.$on('sub-module-cut-paste-content-open', this.openCutPasteContent);
},
template: `