UNPKG

tdesign-miniprogram

Version:
1 lines 4.28 kB
import{__decorate}from"tslib";import{SuperComponent,wxComponent}from"../common/src/index";import config from"../common/config";import props from"./props";import usingConfig from"../mixins/using-config";const{prefix:prefix}=config,componentName="chat-actionbar";let ChatActionbar=class extends SuperComponent{constructor(){super(...arguments),this.behaviors=[usingConfig({componentName:componentName})],this.options={multipleSlots:!0},this.properties=props,this.data={actions:[],classPrefix:`${prefix}-${componentName}`,pComment:"",iconMap:{good:"thumb-up",bad:"thumb-down",replay:"refresh",copy:"copy",share:"share-1",quote:"enter"},iconActiveMap:{good:"thumb-up-filled",bad:"thumb-down-filled"},widthStyle:"",popoverStyle:"transition: none;position: fixed;",popoverPosition:"",longpressVisible:!1},this.observers={comment(t){this.setPComment(t)},"actionBar, pComment, placement"(){this.setActions()},longPressPosition(t){"longpress"===this.properties.placement&&(t?this.showPopover(t):this.hidePopover())}},this.methods={filterSpecialChars(t){let e=t;const i=[];e=e.replace(/^(\s*\|.*\|.*\n\s*\|[-: ]+\|.*\n(\s*\|.*\|.*\n)*)/gm,t=>{const e=t.replace(/\[\d+(?:,\d+)*\]\(@ref\)/g,"").replace(/(\*\*|__)(.*?)\1|(\*|_)(.*?)\3/g,"$2$4").replace(/<br\s*\/?>/gi,"\n");return i.push(e),`%%TABLE${i.length-1}%%`}),e=e.replace(/^(\s*)#{1,6}\s+/gm,"$1"),e=e.replace(/(\*\*|__)(.*?)\1|(\*|_)(.*?)\3/g,"$2$4"),e=e.replace(/!\[.*?\]\(.*?\)/g,""),e=e.replace(/\[\d+(?:,\d+)*\]\(@ref\)/g,"");return e=e.replace(/(\\|`|\{|\}|\[|\]|\(|\)|\||!|@ref|\([@#]\w+\))/g,""),e=e.replace(/\[\d+\]/g,""),e=e.replace(/<br\s*\/?>/gi,"\n"),e=e.replace(/%%TABLE(\d+)%%/g,(t,e)=>i[parseInt(e,10)]||""),e.replace(/\n{3,}/g,"\n\n").trim()},handleActionClick(t){const{name:e}=t.currentTarget.dataset;if("copy"===e&&this.data.content)this.data.handleCopy();else if("good"===e){const t="good"===this.data.pComment;this.setData({pComment:t?void 0:"good"}),this.triggerEvent("actions",{name:e,active:!t,chatId:this.properties.chatId})}else if("bad"===e){const t="bad"===this.data.pComment;this.setData({pComment:t?void 0:"bad"}),this.triggerEvent("actions",{name:e,active:!t,chatId:this.properties.chatId})}else this.triggerEvent("actions",{name:e,chatId:this.properties.chatId});this.onVisibleChange({detail:{visible:!1}})},handleCopy(){if(!this.data.content)return;const t="markdown"===this.data.copyMode?this.data.content:this.data.filterSpecialChars(this.data.content);this.triggerEvent("actions",{name:"copy",data:t})},setActions(){const{globalConfig:t}=this.data,e=[];let i=[];i=this.properties.actionBar,i.forEach(i=>{var o,s;"good"===i||"bad"===i?e.push({name:i,isActive:this.data.pComment===i,text:(null===(o=t.actionBar)||void 0===o?void 0:o[i])||i}):e.push({name:i,isActive:!1,text:(null===(s=t.actionBar)||void 0===s?void 0:s[i])||i})}),this.setData({actions:e})},setPComment(t){this.setData({pComment:t||""})},showPopover(t){this.setData({widthStyle:`width: ${128*this.data.actions.length+8*(this.data.actions.length-1)}rpx`,popoverPosition:`top:${t.y}px;left:${t.x}px`,longpressVisible:!0}),setTimeout(()=>{const t=this.selectComponent(".popover"),e=this.createSelectorQuery().in(t);e.select(".t-popover").boundingClientRect(),e.exec(t=>{const[e]=t,{screenWidth:i}=wx.getWindowInfo();e.left+e.width>i?this.setData({popoverStyle:"transition: none;position:fixed; left: unset !important; right: 16rpx !important;"}):e.left<=0&&this.setData({popoverStyle:"transition: none;position:fixed; left: 16rpx !important;"})})},200)},hidePopover(){this.onVisibleChange({detail:{visible:!1}})},onVisibleChange(t){const{visible:e}=t.detail;this.setData({longpressVisible:e}),e||setTimeout(()=>{this.setData({popoverPosition:"",popoverStyle:"transition: none;position: fixed;"})},200)}},this.lifetimes={created(){this.data.filterSpecialChars=this.filterSpecialChars.bind(this),this.data.handleActionClick=this.handleActionClick.bind(this),this.data.handleCopy=this.handleCopy.bind(this),this.data.showPopover=this.showPopover.bind(this),this.data.hidePopover=this.hidePopover.bind(this),this.data.setPComment=this.setPComment.bind(this)},attached(){this.setData({pComment:this.properties.comment||""}),this.setActions()},detached(){}}}};ChatActionbar=__decorate([wxComponent()],ChatActionbar);export default ChatActionbar;