Commit 74e497ed authored by lvtz's avatar lvtz

fix rich-text

parent 60221eab
......@@ -34,27 +34,11 @@ Page({
} catch (error) {
content = [];
}
// data.content = this.replaceDetail(content);
data.content =content;
data.content = content;
this.setData({
detail: data,
})
}
})
},
replaceDetail (details){
var texts='';//待拼接的内容
while(details.indexOf('<img')!=-1){//寻找img 循环
texts+=details.substring('0',details.indexOf('<img')+4);//截取到<img前面的内容
details = details.substring(details.indexOf('<img')+4);//<img 后面的内容
if(details.indexOf('style=')!=-1 && details.indexOf('style=')<details.indexOf('>')){
texts+=details.substring(0,details.indexOf('style="')+7)+"max-width:100%;width: 100%;margin:0 auto;";//从 <img 后面的内容 截取到style= 加上自己要加的内容
details=details.substring(details.indexOf('style="')+7); //style后面的内容拼接
}else{
texts+=' style="max-width:100%;width: 100%" ';
}
}
texts+=details;//最后拼接的内容
return texts
}
})
\ No newline at end of file
{
"navigationBarTitleText": "帮助详情",
"usingComponents": {
"expiredTip": "../../components/expiredTip"
"expiredTip": "../../components/expiredTip",
"parser":"../../../components/Parser/parser"
}
}
\ No newline at end of file
......@@ -4,11 +4,7 @@
{{detail.title}}
</view>
<view class="detailbox" wx:if="{{detail.content.length > 0}}">
<rich-text
class="rich-text-box"
nodes="{{detail.content}}"
space="nbsp"
/>
<parser html="{{detail.content}}"></parser>
</view>
</view>
<view class="footer-box">
......
......@@ -9,7 +9,7 @@ Page({
localImageRoot: "../../../images/",
sid: 0,
page: 1,
perPage: 10,
perPage: 20,
list: [],
tatol: "",
hasmore: true,
......
/* Parser富文本插件 https://github.com/jin-yufeng/Parser */
"use strict";function _classCallCheck(t,s){if(!(t instanceof s))throw new TypeError("Cannot call a class as a function")}function isBlankChar(t){return" "==t||"\t"==t||"\r"==t||"\n"==t||"\v"==t||"\f"==t}var _createClass=function(){function t(t,s){for(var i=0;i<s.length;i++){var e=s[i];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(t,e.key,e)}}return function(s,i,e){return i&&t(s.prototype,i),e&&t(s,e),s}}(),config=require("./config.js"),CssHandler=function(){function t(s){_classCallCheck(this,t),this.parseCss=function(t){return new CssParser(t,{},!0).parse()},this.styles=s}return _createClass(t,[{key:"getStyle",value:function(t){var s="";return t=t.replace(/<style[\s\S]*?>([\s\S]*?)<\/style[\s\S]*?>/g,function(){return s+=arguments[1],""}),this.styles=new CssParser(s,this.styles).parse(),t}},{key:"match",value:function(t,s){var i=(this.styles[t]||"")+";";if(s.class)for(var e=s.class.split(" "),a=0;a<e.length;a++)i+=(this.styles["."+e[a]]||"")+";";return s.id&&(i+=(this.styles["#"+s.id]||"")+";"),";"==i?"":i}}]),t}();module.exports=CssHandler;var CssParser=function(){function t(s){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},e=arguments[2];_classCallCheck(this,t),this.data=s,this.res=e?i:this.merge(i),this._floor=0,this._i=0,this._list=[],this._comma=!1,this._sectionStart=0,this._stateHandler=this.SpaceHandler}return _createClass(t,[{key:"merge",value:function(t){var s=JSON.parse(JSON.stringify(config.userAgentStyles));for(var i in t)s[i]=(s[i]||"")+t[i];return s}},{key:"parse",value:function(){for(;this._i<this.data.length;this._i++)this._stateHandler(this.data[this._i]);return this.res}},{key:"SpaceHandler",value:function(t){"."==t||"#"==t||t>="a"&&t<="z"||t>="A"&&t<="Z"?(this._sectionStart=this._i,this._stateHandler=this.StyleNameHandler):"/"==t&&"*"==this.data[this._i+1]?this.CommentHandler():isBlankChar(t)||";"==t||(this._stateHandler=this.IgnoreHandler)}},{key:"CommentHandler",value:function(){this._i=this.data.indexOf("*/",this._i),-1==this._i&&(this._i=this.data.length),this._i++,this._stateHandler=this.SpaceHandler}},{key:"IgnoreHandler",value:function(t){"{"==t?this._floor++:"}"==t&&--this._floor<=0&&(this._list=[],this._stateHandler=this.SpaceHandler)}},{key:"StyleNameHandler",value:function(t){isBlankChar(t)?(this._list.push(this.data.substring(this._sectionStart,this._i)),this._stateHandler=this.NameSpaceHandler):"{"==t?(this._list.push(this.data.substring(this._sectionStart,this._i)),this._floor=1,this._sectionStart=this._i+1,this.ContentHandler()):","==t?(this._list.push(this.data.substring(this._sectionStart,this._i)),this._sectionStart=this._i+1,this._comma=!0):t>="a"&&t<="z"||t>="A"&&t<="Z"||t>="0"&&t<="9"||"."==t||"#"==t||"-"==t||"_"==t||(this._stateHandler=this.IgnoreHandler)}},{key:"NameSpaceHandler",value:function(t){"{"==t?(this._floor=1,this._sectionStart=this._i+1,this.ContentHandler()):","==t?(this._comma=!0,this._sectionStart=this._i+1,this._stateHandler=this.StyleNameHandler):isBlankChar(t)||(this._comma?(this._stateHandler=this.StyleNameHandler,this._sectionStart=this._i,this._i--,this._comma=!1):this._stateHandler=this.IgnoreHandler)}},{key:"ContentHandler",value:function(){this._i=this.data.indexOf("}",this._i),-1==this._i&&(this._i=this.data.length);for(var t,s=!1,i=this.data.substring(this._sectionStart,this._i),e=0;e<i.length;e++)isBlankChar(i[e])?s||(t=e,s=!0):s&&(0==t?i=i.substring(e):e-t>1&&(i=i.substring(0,t)+" "+i.substring(e)),e=t,s=!1);s&&(i=i.substring(0,t));for(var e=0;e<this._list.length;e++)this.res[this._list[e]]=(this.res[this._list[e]]||"")+i;this._list=[],this._stateHandler=this.SpaceHandler}}]),t}();
\ No newline at end of file
/*
解析和匹配 Css 的选择器
github:https://github.com/jin-yufeng/Parser
docs:https://jin-yufeng.github.io/Parser
author:JinYufeng
update:2020/03/15
*/
var cfg = require('./config.js');
class CssHandler {
constructor(tagStyle) {
var styles = Object.assign({}, cfg.userAgentStyles);
for (var item in tagStyle)
styles[item] = (styles[item] ? styles[item] + ';' : '') + tagStyle[item];
this.styles = styles;
}
getStyle = data => this.styles = new CssParser(data, this.styles).parse();
match(name, attrs) {
var tmp, matched = (tmp = this.styles[name]) ? tmp + ';' : '';
if (attrs.class) {
var items = attrs.class.split(' ');
for (var i = 0, item; item = items[i]; i++)
if (tmp = this.styles['.' + item])
matched += tmp + ';';
}
if (tmp = this.styles['#' + attrs.id])
matched += tmp + ';';
return matched;
}
}
module.exports = CssHandler;
class CssParser {
constructor(data, init) {
this.data = data;
this.floor = 0;
this.i = 0;
this.list = [];
this.res = init;
this.state = this.Space;
}
parse() {
for (var c; c = this.data[this.i]; this.i++)
this.state(c);
return this.res;
}
section = () => this.data.substring(this.start, this.i);
isLetter = c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
// 状态机
Space(c) {
if (c == '.' || c == '#' || this.isLetter(c)) {
this.start = this.i;
this.state = this.Name;
} else if (c == '/' && this.data[this.i + 1] == '*')
this.Comment();
else if (!cfg.blankChar[c] && c != ';')
this.state = this.Ignore;
}
Comment() {
this.i = this.data.indexOf('*/', this.i) + 1;
if (!this.i) this.i = this.data.length;
this.state = this.Space;
}
Ignore(c) {
if (c == '{') this.floor++;
else if (c == '}' && !--this.floor) this.state = this.Space;
}
Name(c) {
if (cfg.blankChar[c]) {
this.list.push(this.section());
this.state = this.NameSpace;
} else if (c == '{') {
this.list.push(this.section());
this.Content();
} else if (c == ',') {
this.list.push(this.section());
this.Comma();
} else if (!this.isLetter(c) && (c < '0' || c > '9') && c != '-' && c != '_')
this.state = this.Ignore;
}
NameSpace(c) {
if (c == '{') this.Content();
else if (c == ',') this.Comma();
else if (!cfg.blankChar[c]) this.state = this.Ignore;
}
Comma() {
while (cfg.blankChar[this.data[++this.i]]);
if (this.data[this.i] == '{') this.Content();
else {
this.start = this.i--;
this.state = this.Name;
}
}
Content() {
this.start = ++this.i;
if ((this.i = this.data.indexOf('}', this.i)) == -1) this.i = this.data.length;
var content = this.section();
for (var i = 0, item; item = this.list[i++];)
if (this.res[item]) this.res[item] += ';' + content;
else this.res[item] = content;
this.list = [];
this.state = this.Space;
}
}
\ No newline at end of file
This diff is collapsed.
"use strict";var trustTags={a:0,abbr:1,ad:0,audio:0,b:1,blockquote:1,br:0,code:1,col:0,colgroup:0,dd:1,del:1,dl:1,dt:1,div:1,em:1,fieldset:0,font:1,h1:0,h2:0,h3:0,h4:0,h5:0,h6:0,hr:0,i:1,img:1,ins:1,label:1,legend:0,li:0,ol:0,p:1,q:1,source:0,span:1,strong:1,sub:0,sup:0,table:0,tbody:0,td:0,tfoot:0,th:0,thead:0,tr:0,u:1,ul:0,video:1},blockTags={address:!0,article:!0,aside:!0,body:!0,center:!0,cite:!0,footer:!0,header:!0,html:!0,nav:!0,pre:!0,section:!0},ignoreTags={area:!0,base:!0,basefont:!0,canvas:!0,circle:!0,command:!0,ellipse:!0,embed:!0,frame:!0,head:!0,iframe:!0,input:!0,isindex:!0,keygen:!0,line:!0,link:!0,map:!0,meta:!0,param:!0,path:!0,polygon:!0,polyline:!0,rect:!0,script:!0,source:!0,textarea:!0,title:!0,track:!0,use:!0,wbr:!0},userAgentStyles={a:"display:inline;color:#366092;word-break:break-all;",address:"font-style:italic;",blockquote:"background-color:#f6f6f6;border-left:3px solid #dbdbdb;color:#6c6c6c;padding:5px 0 5px 10px;",center:"text-align:center;",cite:"font-style:italic;",code:"padding:0 1px 0 1px;margin-left:2px;margin-right:2px;background-color:#f8f8f8;border-radius:3px;",dd:"margin-left:40px;",img:"max-width:100%;",mark:"display:inline;background-color:yellow;",pre:"font-family:monospace;white-space:pre;overflow:scroll;",s:"display:inline;text-decoration:line-through;",u:"display:inline;text-decoration:underline;"},SDKVersion=wx.getSystemInfoSync().SDKVersion,versionHigherThan=function(){for(var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",e=SDKVersion.split("."),s=t.split("."),r=Math.max(e.length,s.length);e.length<r;)e.push("0");for(;s.length<r;)s.push("0");for(var a=0;a<r;a++){var i=parseInt(e[a]),l=parseInt(s[a]);if(i>l)return!0;if(i<l)return!1}return!0};versionHigherThan("2.7.1")?(trustTags.bdi=0,trustTags.bdo=0,trustTags.caption=0,trustTags.rt=0,trustTags.ruby=0,ignoreTags.rp=!0,trustTags.big=1,trustTags.small=1,trustTags.pre=0,delete blockTags.pre):(blockTags.caption=!0,userAgentStyles.big="display:inline;font-size:1.2em;",userAgentStyles.small="display:inline;font-size:0.8em;"),module.exports={highlight:null,LabelAttrsHandler:function(t,e){switch(t.name){case"div":case"p":t.attrs.align&&(t.attrs.style="text-align:"+t.attrs.align+";"+(t.attrs.style||""),delete t.attrs.align);break;case"img":t.attrs["data-src"]&&(t.attrs.src=t.attrs.src||t.attrs["data-src"],delete t.attrs["data-src"]),!t.attrs.ignore&&t.attrs.src&&(e.imgList.includes(t.attrs.src)&&t.attrs.src.includes("http")&&(t.attrs.src=t.attrs.src+(t.attrs.src.includes("?")?"&":"?")+"parserid="+e.imgList.length),e.imgList.push(t.attrs.src),"a"==e.bubbling()&&(t.attrs.ignore="true")),e._domain&&"/"==t.attrs.src[0]&&("/"==t.attrs.src[1]?t.attrs.src=e._protocol+":"+t.attrs.src:t.attrs.src=e._domain+t.attrs.src);break;case"a":case"ad":e.bubbling();break;case"font":if(name="span",t.attrs.style=t.attrs.style||"",t.attrs.color&&(t.attrs.style="color:"+t.attrs.color+";"+t.attrs.style,delete t.attrs.color),t.attrs.face&&(t.attrs.style="font-family:"+t.attrs.face+";"+t.attrs.style,delete t.attrs.face),t.attrs.size){var s=parseInt(attrs.size);s<1?s=1:s>7&&(s=7);var r=[10,13,16,18,24,32,48];t.attrs.style="font-size:"+r[s-1]+"px;"+t.attrs.style,delete t.attrs.size}break;case"video":case"audio":t.attrs.id?e["_"+t.name+"Num"]++:t.attrs.id=t.name+ ++e["_"+t.name+"Num"],"video"==t.name&&(t.attrs.style=t.attrs.style||"",t.attrs.width&&(t.attrs.style="width:"+parseFloat(t.attrs.width)+(t.attrs.width.includes("%")?"%":"px")+";"+t.attrs.style,delete t.attrs.width),t.attrs.height&&(t.attrs.style="height:"+parseFloat(t.attrs.height)+(t.attrs.height.includes("%")?"%":"px")+";"+t.attrs.style,delete t.attrs.height),e._videoNum>3&&(t.lazyLoad=!0)),t.attrs.source=[],t.attrs.src&&t.attrs.source.push(t.attrs.src),t.attrs.controls||t.attrs.autoplay||console.warn("存在没有controls属性的"+t.name+"标签,可能导致无法播放",t),e.bubbling();break;case"source":var a=e._STACK[e._STACK.length-1];!a||"video"!=a.name&&"audio"!=a.name||(a.attrs.source.push(t.attrs.src),a.attrs.src||(a.attrs.src=t.attrs.src))}},trustAttrs:{align:!0,alt:!0,author:!0,autoplay:!0,border:!0,cellpadding:!0,cellspacing:!0,class:!0,color:!0,colspan:!0,controls:!0,"data-src":!0,dir:!0,face:!0,height:!0,href:!0,id:!0,ignore:!0,loop:!0,muted:!0,name:!0,poster:!0,rowspan:!0,size:!0,span:!0,src:!0,start:!0,style:!0,type:!0,"unit-id":!0,width:!0},selfClosingTags:{area:!0,base:!0,basefont:!0,br:!0,col:!0,circle:!0,ellipse:!0,embed:!0,frame:!0,hr:!0,img:!0,input:!0,isindex:!0,keygen:!0,line:!0,link:!0,meta:!0,param:!0,path:!0,polygon:!0,polyline:!0,rect:!0,source:!0,track:!0,use:!0,wbr:!0},trustTags:trustTags,blockTags:blockTags,ignoreTags:ignoreTags,userAgentStyles:userAgentStyles,versionHigherThan:versionHigherThan};
\ No newline at end of file
/* 配置文件 */
const canIUse = wx.canIUse('editor'); // 高基础库标识,用于兼容
module.exports = {
// 过滤器函数
filter: null,
// 代码高亮函数
highlight: null,
// 文本处理函数
onText: null,
blankChar: makeMap(' ,\xA0,\t,\r,\n,\f'),
// 块级标签,将被转为 div
blockTags: makeMap('address,article,aside,body,caption,center,cite,footer,header,html,nav,section' + (canIUse ? '' : ',pre')),
// 将被移除的标签
ignoreTags: makeMap('area,base,basefont,canvas,command,embed,frame,iframe,input,isindex,keygen,link,map,meta,param,script,source,style,svg,textarea,title,track,use,wbr' + (canIUse ? ',rp' : '')),
// 只能被 rich-text 显示的标签
richOnlyTags: makeMap('a,colgroup,fieldset,legend,picture,table' + (canIUse ? ',bdi,bdo,rt,ruby' : '')),
// 自闭合的标签
selfClosingTags: makeMap('area,base,basefont,br,col,circle,ellipse,embed,frame,hr,img,input,isindex,keygen,line,link,meta,param,path,polygon,rect,source,track,use,wbr'),
// 信任的属性
trustAttrs: makeMap('align,alt,app-id,author,autoplay,border,cellpadding,cellspacing,class,color,colspan,controls,data-src,dir,face,height,href,id,ignore,loop,media,muted,name,path,poster,rowspan,size,span,src,start,style,type,unit-id,width,xmlns'),
// bool 型的属性
boolAttrs: makeMap('autoplay,controls,ignore,loop,muted'),
// 信任的标签
trustTags: makeMap('a,abbr,ad,audio,b,blockquote,br,code,col,colgroup,dd,del,dl,dt,div,em,fieldset,h1,h2,h3,h4,h5,h6,hr,i,img,ins,label,legend,li,ol,p,q,source,span,strong,sub,sup,table,tbody,td,tfoot,th,thead,tr,title,ul,video' + (canIUse ? ',bdi,bdo,caption,pre,rt,ruby' : '')),
// 默认的标签样式
userAgentStyles: {
address: 'font-style:italic',
big: 'display:inline;font-size:1.2em',
blockquote: 'background-color:#f6f6f6;border-left:3px solid #dbdbdb;color:#6c6c6c;padding:5px 0 5px 10px',
caption: 'display:table-caption;text-align:center',
center: 'text-align:center',
cite: 'font-style:italic',
dd: 'margin-left:40px',
mark: 'background-color:yellow',
pre: 'font-family:monospace;white-space:pre;overflow:scroll',
s: 'text-decoration:line-through',
small: 'display:inline;font-size:0.8em',
u: 'text-decoration:underline'
}
}
function makeMap(str) {
var map = {},
list = str.split(',');
for (var i = list.length; i--;)
map[list[i]] = true;
return map;
}
\ No newline at end of file
This diff is collapsed.
{
"component": true,
"usingComponents": {
"trees": "./trees/trees"
}
}
\ No newline at end of file
<!--parser 主组件-->
<slot wx:if="{{!html[0].name&&!html[0].type}}" />
<trees class="top" style="{{selectable?'user-select:text;-webkit-user-select:text;':''}}{{showAm}}" animation="{{scaleAm}}" lazy-load="{{lazyLoad}}" nodes="{{html[0].name||html[0].type?html:[]}}" bindtap="_tap" bindtouchstart="_touchstart" bindtouchmove="_touchmove" />
<image wx:for="{{imgs}}" wx:key="index" id="{{index}}" src="{{item}}" hidden bindload="_load" />
\ No newline at end of file
:host {
display: block;
overflow: scroll;
-webkit-overflow-scrolling: touch;
}
.top {
display: inherit;
}
@keyframes show {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
\ No newline at end of file
"use strict";var inlineTags={abbr:!0,b:!0,big:!0,code:!0,del:!0,em:!0,font:!0,i:!0,ins:!0,label:!0,mark:!0,q:!0,s:!0,small:!0,span:!0,strong:!0,u:!0};module.exports={getStyle:function(t,e){res="";var s=getRegExp("float[^;]+(?![sS]*?float)","i");return s.test(t)&&(res+=s.exec(t)[0]),s=getRegExp("margin[^;]+","gi"),s.test(t)&&(res+=";"+t.match(s).join(";")),s=getRegExp("displays*:s*([^;]*)(?![sS]*?display)","i"),s.test(t)&&"flex"!=s.exec(t)[1]?res+=";"+s.exec(t)[0]:res+=";display:"+e,s=getRegExp("flex[^;]*:[^;]+","ig"),s.test(t)&&(res+=";"+t.match(s).join(";")),s=getRegExp("[^;s]*width[^;]+","ig"),s.test(t)&&(res+=";"+t.match(s).join(";")),res},setImgStyle:function(t,e,s){return"widthFix"==e&&(t.attrs.style+=";height:auto !important"),t.attrs.style&&(t.attrs.style=t.attrs.style.replace(getRegExp("width[^;]*?%","gi"),"width:100%").replace(getRegExp("margin[^;]+","gi"),"")),s||(delete t.attrs.src,t.attrs.style+=";width:5px !important;height:5px !important"),[t]},setStyle:function(t){return t.attrs.style&&(t.attrs.style=t.attrs.style.replace(getRegExp("width[^;]*?%","gi"),"width:100%").replace(getRegExp("margin[^;]+","gi"),"")),[t]},notContinue:function(t){return"a"==t.name||"li"==t.name||"ol"==t.name||"ul"==t.name||!(t["continue"]||inlineTags[t.name])}};
\ No newline at end of file
var inlineTags = {
abbr: 1,
b: 1,
big: 1,
code: 1,
del: 1,
em: 1,
i: 1,
ins: 1,
label: 1,
q: 1,
small: 1,
span: 1,
strong: 1
}
module.exports = {
// 获取图片大小
load: function (e) {
if (e.target.dataset.auto)
e.instance.setStyle({
width: e.detail.width + 'px'
})
},
// 链接点击态
visited: function (e, owner) {
if (!e.instance.hasClass('_visited'))
e.instance.addClass('_visited')
owner.callMethod('linkpress', e);
},
// 是否通过 rich-text 显示
useRichText: function (item) {
return !item.c && !inlineTags[item.name] && (item.attrs.style || '').indexOf('display:inline') == -1
}
}
\ No newline at end of file
/* Parser富文本插件 https://github.com/jin-yufeng/Parser */
"use strict";Component({data:{imgLoad:!1},created:function(){var t=this;this.triggerError=function(r,e,o,a,i){t._top&&t._top.triggerEvent("error",{source:r,target:e,errMsg:o,errCode:a,context:i})},this.loadSource=function(r){return!t.data.controls[r.id]&&r.source.length>1?(t.data.controls[r.id]={play:!1,index:1},t.setData({controls:t.data.controls}),!0):!!(t.data.controls[r.id]&&r.source.length>t.data.controls[r.id].index+1)&&(t.data.controls[r.id].index++,t.setData({controls:t.data.controls}),!0)}},detached:function(){this._observer&&this._observer.disconnect()},properties:{nodes:{type:Array,value:[]},controls:{type:Object,value:{}}},methods:{playEvent:function(t){if(this._top&&this._top.videoContexts.length>1&&this._top.data.autopause){var r=!0,e=!1,o=void 0;try{for(var a,i=this._top.videoContexts[Symbol.iterator]();!(r=(a=i.next()).done);r=!0){var n=a.value;n.id!=t.currentTarget.dataset.id&&n.pause()}}catch(t){e=!0,o=t}finally{try{!r&&i.return&&i.return()}finally{if(e)throw o}}}},previewEvent:function(t){if(this._top&&!t.target.dataset.hasOwnProperty("ignore")){var r=!0,e=t.currentTarget.dataset.src;this._top.triggerEvent("imgtap",{id:t.currentTarget.id,src:e,ignore:function(){return r=!1}}),r&&this._top.data.autopreview&&wx.previewImage({current:e,urls:this._top.imgList.length?this._top.imgList:[e]})}},tapEvent:function(t){if(this._top){var r=!0,e=t.currentTarget.dataset.href;this._top.triggerEvent("linkpress",{href:e,ignore:function(){return r=!1}}),r&&e&&("#"==e[0]?this._top.data.useAnchor&&this._top.navigateTo({id:e.substring(1)}):/^http/.test(e)?this._top.data.autocopy&&wx.setClipboardData({data:e,success:function(){wx.showToast({title:"链接已复制"})}}):wx.navigateTo({url:e}))}},adError:function(t){this.triggerError("ad",t.currentTarget,t.detail.errMsg,t.detail.errCode)},videoError:function(t){!this.loadSource(t.currentTarget.dataset)&&this._top&&this.triggerError("video",t.currentTarget,t.detail.errMsg,void 0,this._top.getVideoContext(t.currentTarget.id))},audioError:function(t){this.loadSource(t.currentTarget.dataset)||this.triggerError("audio",t.currentTarget,t.detail.errMsg)},loadVideo:function(t){this.data.controls[t.currentTarget.dataset.id]={play:!0,index:0},this.setData({controls:this.data.controls})}}});
\ No newline at end of file
/*
trees 递归子组件
github:https://github.com/jin-yufeng/Parser
docs:https://jin-yufeng.github.io/Parser
author:JinYufeng
update:2020/03/23
*/
Component({
data: {
canIUse: !!wx.chooseMessageFile
},
properties: {
nodes: Array,
lazyLoad: Boolean
},
methods: {
// 视频播放事件
play(e) {
this.top.group && this.top.group.pause(this.top.i);
if (this.top.videoContexts.length > 1 && this.top.data.autopause)
for (var i = this.top.videoContexts.length; i--;)
if (this.top.videoContexts[i].id != e.currentTarget.id)
this.top.videoContexts[i].pause();
},
// 图片点击事件
imgtap(e) {
var attrs = e.target.dataset.attrs;
if (!attrs.ignore) {
var preview = true;
this.top.triggerEvent('imgtap', {
id: e.target.id,
src: attrs.src,
ignore: () => preview = false
})
if (preview) {
if (this.top.group) return this.top.group.preview(this.top.i, attrs.i);
var urls = this.top.imgList,
current = urls[attrs.i] ? urls[attrs.i] : (urls = [attrs.src], attrs.src);
wx.previewImage({
current,
urls
})
}
}
},
// 链接点击事件
linkpress(e) {
var jump = true,
attrs = e.currentTarget.dataset.attrs;
attrs.ignore = () => jump = false;
this.top.triggerEvent('linkpress', attrs);
if (jump) {
if (attrs['app-id'])
wx.navigateToMiniProgram({
appId: attrs['app-id'],
path: attrs.path
})
else if (attrs.href) {
if (attrs.href[0] == '#')
this.top.navigateTo({
id: attrs.href.substring(1)
})
else if (attrs.href.indexOf('http') == 0 || attrs.href.indexOf('//') == 0)
wx.setClipboardData({
data: attrs.href,
success: () =>
wx.showToast({
title: '链接已复制'
})
})
else
wx.navigateTo({
url: attrs.href,
})
}
}
},
// 错误事件
error(e) {
var context, src = '',
source = e.target.dataset.source,
i = e.target.dataset.i,
node = this.data.nodes[i];
if (source == 'video' || source == 'audio') {
// 加载其他 source
var index = (node.i || 0) + 1;
if (index < node.attrs.source.length)
return this.setData({
[`nodes[${i}].i`]: index
})
if (this.top) context = this.top.getVideoContext(e.target.id);
} else if (source == 'img')
context = {
setSrc: (newSrc) => src = newSrc
}
this.top && this.top.triggerEvent('error', {
source,
target: e.target,
context,
...e.detail
})
if (source == 'img') {
var data = {
[`nodes[${i}].attrs.src`]: src
}
if (!src) data[`nodes[${i}].err`] = 1;
this.setData(data);
}
},
// 加载视频
loadVideo(e) {
var i = e.target.dataset.i;
this.setData({
[`nodes[${i}].lazyLoad`]: false,
[`nodes[${i}].attrs.autoplay`]: true
})
},
// 加载图片
loadImg(e) {
var data = e.target.dataset;
if (data.auto)
this.setData({
[`nodes[${data.i}].attrs.style`]: `${this.data.nodes[data.i].attrs.style};width:${e.detail.width}px`
})
}
}
})
\ No newline at end of file
{"component":true,"usingComponents":{"trees":"./trees"}}
\ No newline at end of file
{
"component": true,
"usingComponents": {
"trees": "./trees"
}
}
\ No newline at end of file
<wxs module="Handler" src="./handler.wxs" /><block wx:for='{{nodes}}' wx:key="index"><block wx:if="{{Handler.notContinue(item)}}"><rich-text wx:if="{{item.name=='img'}}" id="{{item.attrs.id}}" class="img" style="text-indent:0;{{Handler.getStyle(item.attrs.style,'inline-block')}}" nodes='{{Handler.setImgStyle(item,controls.imgMode,imgLoad)}}' data-ignore='{{item.attrs.ignore}}' data-src='{{item.attrs.src}}' bindtap='previewEvent' /><block wx:elif="{{item.type=='text'}}"><text wx:if="{{!item.decode}}" decode>{{item.text}}</text><rich-text wx:else style="display:inline-block" nodes="{{[item]}}"></rich-text></block><text wx:elif="{{item.name=='br'}}">\n</text><block wx:elif="{{item.name=='video'}}"><view wx:if="{{item.lazyLoad&&!controls[item.attrs.id].play}}" class="pvideo {{item.attrs.class||''}}" style="{{item.attrs.style}}" data-id="{{item.attrs.id}}" bindtap="loadVideo"><view class="video-triangle" /></view><video wx:else src='{{controls[item.attrs.id]?item.attrs.source[controls[item.attrs.id].index]:item.attrs.src}}' id="{{item.attrs.id}}" loop='{{item.attrs.loop}}' controls='{{item.attrs.controls}}' autoplay="{{item.attrs.autoplay||controls[item.attrs.id].play}}" unit-id="{{item.attrs['unit-id']}}" class="v {{item.attrs.class||''}}" muted="{{item.attrs.muted}}" style="{{item.attrs.style}}" data-id="{{item.attrs.id}}" data-source="{{item.attrs.source}}" bindplay='playEvent' binderror="videoError" /></block><audio wx:elif="{{item.name=='audio'}}" id="{{item.attrs.id}}" class="{{item.attrs.class}}" src='{{controls[item.attrs.id]?item.attrs.source[controls[item.attrs.id].index]:item.attrs.src}}' loop='{{item.attrs.loop}}' controls='{{item.attrs.controls}}' poster='{{item.attrs.poster}}' name='{{item.attrs.name}}' author='{{item.attrs.author}}' style="{{item.attrs.style}}" data-id="{{item.attrs.id}}" data-source="{{item.attrs.source}}" binderror="audioError" /><view wx:elif="{{item.name=='a'}}" url="{{item.attrs.href}}" class="a {{item.attrs.class||''}}" style="{{item.attrs.style}}" data-href='{{item.attrs.href}}' hover-class="navigator-hover" hover-start-time="25" hover-stay-time="300" bindtap="tapEvent"><trees class="node" nodes="{{item.children}}" controls="{{controls}}" /></view><ad wx:elif="{{item.name=='ad'}}" unit-id="{{item.attrs['unit-id']}}" class="{{item.attrs.class}}" style="{{item.attrs.style}}" binderror="adError"></ad><rich-text wx:else id="{{item.attrs.id}}" class="{{item.name}}" style="{{Handler.getStyle(item.attrs.style,'block')}}" nodes="{{Handler.setStyle(item)}}" /></block><trees wx:else id="{{item.attrs.id}}" class="node {{item.name+' '+(item.attrs.class||'')}}" style="{{item.attrs.style}}" nodes="{{item.children}}" controls="{{controls}}" /></block>
\ No newline at end of file
<!--trees 递归子组件-->
<wxs module="handler" src="./handler.wxs" />
<block wx:for="{{nodes}}" wx:key="index" wx:for-item="n">
<rich-text wx:if="{{n.en||n.svg||n.err}}" class="_svg" nodes="{{[n]}}" />
<!--图片-->
<image wx:elif="{{n.name=='img'}}" class="_img" style="{{n.attrs.style}}" src="{{n.attrs.src}}" lazy-load="{{lazyLoad}}" mode="{{n.mode||'widthFix'}}" show-menu-by-longpress="{{!n.attrs.ignore}}" webp="{{n.webp}}" data-attrs="{{n.attrs}}" data-i="{{index}}" data-auto="{{n.auto}}" data-source="img" bindtap="imgtap" bindload="{{canIUse?handler.load:'loadImg'}}" binderror="error" />
<!--文本-->
<text wx:elif="{{n.type=='text'}}" decode>{{n.text}}</text>
<text wx:elif="{{n.name=='br'}}">\n</text>
<!--链接-->
<view wx:elif="{{n.name=='a'}}" class="_a {{n.attrs.class}}" hover-class="_hover" style="{{n.attrs.style}}" data-attrs="{{n.attrs}}" bindtap="{{canIUse?handler.visited:'linkpress'}}">
<trees nodes="{{n.children}}" />
</view>
<!--视频-->
<block wx:elif="{{n.name=='video'}}">
<view wx:if="{{n.lazyLoad}}" id="{{n.attrs.id}}" class="_video {{n.attrs.class}}" style="{{n.attrs.style}}" data-i="{{index}}" bindtap="loadVideo" />
<video wx:else id="{{n.attrs.id}}" class="{{n.attrs.class}}" style="{{n.attrs.style}}" autoplay="{{n.attrs.autoplay}}" controls="{{n.attrs.controls}}" loop="{{n.attrs.loop}}" muted="{{n.attrs.muted}}" poster="{{n.attrs.poster}}" src="{{n.attrs.source[n.i||0]}}" unit-id="{{n.attrs['unit-id']}}" data-i="{{index}}" data-source="video" binderror="error" bindplay="play" />
</block>
<!--音频-->
<audio wx:elif="{{n.name=='audio'}}" id="{{n.attrs.id}}" class="{{n.attrs.class}}" style="{{n.attrs.style}}" author="{{n.attrs.author}}" autoplay="{{n.attrs.autoplay}}" controls="{{n.attrs.controls}}" loop="{{n.attrs.loop}}" name="{{n.attrs.name}}" poster="{{n.attrs.poster}}" src="{{n.attrs.source[n.i||0]}}" data-i="{{index}}" data-source="audio" binderror="error" bindplay="play" />
<!--广告-->
<ad wx:elif="{{n.name=='ad'}}" class="{{n.attrs.class}}" style="{{n.attrs.style}}" unit-id="{{n.attrs['unit-id']}}" data-source="ad" binderror="error" />
<!--列表-->
<view wx:elif="{{n.name=='li'}}" class="{{n.attrs.class}}" style="{{n.attrs.style}};display:flex">
<view wx:if="{{n.type=='ol'}}" class="_ol-bef">{{n.num}}</view>
<view wx:else class="_ul-bef">
<view wx:if="{{n.floor%3==0}}" class="_ul-p1">█</view>
<view wx:elif="{{n.floor%3==2}}" class="_ul-p2" />
<view wx:else class="_ul-p1" style="border-radius:50%">█</view>
</view>
<trees class="_node _li" lazy-load="{{lazyLoad}}" nodes="{{n.children}}" />
</view>
<!--富文本-->
<rich-text wx:elif="{{handler.useRichText(n)}}" id="{{n.attrs.id}}" class="_p __{{n.name}}" nodes="{{[n]}}" />
<!--继续递归-->
<trees wx:else id="{{n.attrs.id}}" class="_node _{{n.name}} {{n.attrs.class}}" style="{{n.attrs.style}}" lazy-load="{{lazyLoad}}" nodes="{{n.children}}" />
</block>
\ No newline at end of file
.navigator-hover{opacity:.7;text-decoration:underline}:host{display:inherit;float:inherit}.a{display:inline;color:#366092}.sub,.sup,.bdo,.bdi,.ruby,.rt{display:inline-block!important}.div,.blockquote,.p{display:block}.b,.strong{display:inline;font-weight:bold}.em,.i{display:inline;font-style:italic}.del{display:inline;text-decoration:line-through}.ins{display:inline;text-decoration:underline}.code{display:inline;font-family:monospace}.big{font-size:1.2em;display:inline}.small{font-size:.8em;display:inline}.q,.span,.label,.abbr{display:inline}.q::before{content:'"'}.q::after{content:'"'}.pvideo{background-color:black;width:300px;height:225px;display:inline-block;position:relative}.video-triangle{border-width:15px 0 15px 30px;border-style:solid;border-color:transparent transparent transparent white;position:absolute;left:50%;top:50%;margin:-15px 0 0 -15px}
\ No newline at end of file
/* 在这里引入自定义样式 */
/* 链接和图片效果 */
._a {
color: #366092;
display: inline;
padding: 1.5px 0 1.5px 0;
word-break: break-all;
}
._hover {
opacity: 0.7;
text-decoration: underline;
}
._visited {
color: #551a8b;
}
._img {
height: 50px;
max-width: 100%;
}
/* 内部样式 */
:host {
display: inline;
}
._blockquote, ._div, ._p, ._ul, ._ol, ._li {
display: block;
}
._b, ._strong {
font-weight: bold;
}
._code {
font-family: monospace;
}
._del {
text-decoration: line-through;
}
._em, ._i {
font-style: italic;
}
._h1 {
font-size: 2em;
}
._h2 {
font-size: 1.5em;
}
._h3 {
font-size: 1.17em;
}
._h5 {
font-size: 0.83em;
}
._h6 {
font-size: 0.67em;
}
._h1, ._h2, ._h3, ._h4, ._h5, ._h6 {
display: block;
font-weight: bold;
}
._ins {
text-decoration: underline;
}
._li {
flex: 1;
width: 0;
}
._ol-bef {
margin-right: 5px;
text-align: right;
width: 36px;
}
._ul-bef {
line-height: normal;
margin: 0 12px 0 23px;
}
._ol-bef, ._ul_bef {
flex: none;
user-select: none;
}
._ul-p1 {
display: inline-block;
height: 0.3em;
line-height: 0.3em;
overflow: hidden;
width: 0.3em;
}
._ul-p2 {
border: 0.05em solid black;
border-radius: 50%;
display: inline-block;
height: 0.23em;
width: 0.23em;
}
._q::before {
content: '"';
}
._q::after {
content: '"';
}
._sub {
font-size: smaller;
vertical-align: sub;
}
._sup {
font-size: smaller;
vertical-align: super;
}
.__bdi, .__bdo, .__ruby, .__rt, ._svg {
display: inline-block;
}
._video {
background-color: black;
display: inline-block;
height: 225px;
position: relative;
width: 300px;
}
._video::after {
border-left-color: white;
border-style: solid;
border-width: 15px 0 15px 30px;
content: '';
left: 50%;
margin: -15px 0 0 -15px;
position: absolute;
top: 50%;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment