Commit 4458647b by 李嘉林

富文本组件

parent 66613e44
...@@ -20,24 +20,39 @@ ...@@ -20,24 +20,39 @@
*/ */
function formatRichText(html) { function formatRichText(html) {
let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) { let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
match = match.replace(/style="[^"]+"/gi, "").replace(/style='[^']+'/gi, ""); // match = match.replace(/style="[^"]+"/gi, "").replace(/style='[^']+'/gi, "");
match = match.replace(/width="[^"]+"/gi, "").replace(/width='[^']+'/gi, ""); // match = match.replace(/width="[^"]+"/gi, "").replace(/width='[^']+'/gi, "");
match = match // match = match
.replace(/height="[^"]+"/gi, "") // .replace(/height="[^"]+"/gi, "")
.replace(/height='[^']+'/gi, ""); // .replace(/height='[^']+'/gi, "");
let maxWidth = 375;
match.replace(/width="(.+?)"/g, function (val, val1) {
if (val1 > maxWidth) {
match = match
.replace(/width="[^"]+"/gi, "")
.replace(/width='[^']+'/gi, "");
match = match.replace(
/\<img/gi,
'<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'
);
}
});
return match; return match;
}); });
newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) { newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
match = match match = match
.replace(/width:[^;]+;/gi, "max-width:100%;") .replace(/ width:[^;]+;/gi, "max-width:100%;")
.replace(/width:[^;]+;/gi, "max-width:100%;"); .replace(/ width:[^;]+;/gi, "max-width:100%;");
match = match
.replace(/max-width:[^;]+;/gi, "max-width:100%;")
.replace(/max-width:[^;]+;/gi, "max-width:100%;");
return match; return match;
}); });
newContent = newContent.replace(/<br[^>]*\/>/gi, ""); // newContent = newContent.replace(/<br[^>]*\/>/gi, "");
newContent = newContent.replace( // newContent = newContent.replace(
/\<img/gi, // /\<img/gi,
'<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"' // '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'
); // );
return newContent; return newContent;
} }
export default { export default {
......
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