UNPKG

4.1 kBJavaScriptView Raw
1import Parse from '../parse';
2import { helperGetLastLIne } from '../helpers';
3import { isObject } from '../utils';
4
5const getUrlType = (url) => {
6 if (url.includes('guihuazhi.com')) {
7 if (url.includes('article')) {
8 return 'article';
9 } else if (url.includes('topic')) {
10 return 'topic';
11 } else {
12 return 'other';
13 }
14 } else {
15 return 'other';
16 }
17};
18
19let routes = {
20 article: {
21 route: '/app/article/detail',
22 id: 'aid'
23 },
24 topic: {
25 route: '/app/topic/detail',
26 id: 'topicId'
27 },
28 other: {
29 route: '',
30 id: 'id'
31 }
32};
33// const getClientRoute = (type) => {
34//
35//
36// return o[type]
37// };
38
39const getId = (url) => {
40 let str = url.split('?')[0].split('/').pop();
41 return str.slice(0, -5);
42};
43
44
45class WebRich extends Parse {
46 constructor(option) {
47 super(option);
48 }
49
50 renderImage(props) {
51 // 适配quill自定义图片样式
52 let {format, height, width, size, url, desc = ''} = props;
53 let o = {format, height, width, size, url};
54 let value = desc.replace(/ /g," ");
55 return `<div class="huazhi-image" image=${JSON.stringify(o)} value=${value}></div>`;
56 }
57
58 renderLink(props) {
59 // return `<p><a href="${props.route.webURL}" target="_blank">${props.content}</a></p>`;
60 return `<a class="huazhi-link" target="_blank" href="${props.route.webUrl || props.route.webURL}">${props.content}</a>`;
61 }
62
63 renderVideo(props) {
64 return `<video class="huazhi-video" src="${props.url}" controls="controls" result=${JSON.stringify(props)}></video>`;
65 }
66
67 /**
68 * ops => json
69 * 富文本编辑内容格式化
70 * @param {object} source html字符串/detail结构
71 * @param {boolean} isJSonString 是否转为json-string
72 * @return {array<object>|string} json结构
73 */
74 opsToHTML(source, isJSonString = false) {
75 let results = [];
76
77 let tmp = null;
78 for (let i = 0, l = source.length; i < l; i++) {
79 let { insert, attributes = {} } = source[i];
80 // 文本
81 if (typeof insert === 'string') {
82 const { bold, header, blockquote, link } = attributes;
83 if (!tmp) {
84 tmp = {
85 type: 'text',
86 value: []
87 };
88 }
89
90 // 标题或者引用
91 if (header === 2 || blockquote === true) {
92 let lastLine = helperGetLastLIne(tmp.value);
93
94 if (tmp.value.length) {
95 results.push(tmp);
96 }
97
98 tmp = null;
99 let block = {
100 type: 'text',
101 value: lastLine
102 };
103 if (header === 2) block.head = 1;
104
105 if (blockquote === true) block.block = 1;
106
107 results.push(block);
108 } else if (bold) {
109 tmp.value.push({ type: 'string', bold: 1, content: insert });
110 } else if (link) {
111 // 链接处理
112 let type = getUrlType(link);
113 let info = routes[type];
114 let value = {
115 type: 'link',
116 content: insert,
117 route: {
118 route: info.route,
119 webUrl: link,
120 params: {
121 [info.id]: getId(link)
122 }
123 }
124 };
125 tmp.value.push(value);
126 } else {
127 tmp.value.push({ type: 'string', content: insert });
128 }
129
130 // 非文本
131 } else if (isObject(insert)) {
132 // 清理文本
133 if (tmp && tmp.value.length) {
134 results.push(tmp);
135 tmp = null;
136 }
137
138 // 图片提取
139 if (insert.image && this.isImage(insert.image)) {
140 results.push({ ...insert.image, type: 'image' });
141 }
142
143 // 分割线
144 if (insert.hr === true) {
145 results.push({
146 type: 'line'
147 });
148 }
149
150 // 视频提取
151 if (insert.simpleVideo && insert.simpleVideo.url) {
152 results.push({ ...insert.simpleVideo, type: 'video' });
153 }
154 }
155
156 // 末尾不考虑其他因素
157 if (i === l - 1 && tmp) {
158 results.push(tmp);
159 }
160 }
161 return isJSonString ? JSON.stringify(results) : results;
162 }
163}
164
165export default WebRich;