1 | import Parse from '../parse';
|
2 | import { helperGetLastLIne } from '../helpers';
|
3 | import { isObject } from '../utils';
|
4 |
|
5 | const 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 |
|
19 | let 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 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | const getId = (url) => {
|
40 | let str = url.split('?')[0].split('/').pop();
|
41 | return str.slice(0, -5);
|
42 | };
|
43 |
|
44 |
|
45 | class WebRich extends Parse {
|
46 | constructor(option) {
|
47 | super(option);
|
48 | }
|
49 |
|
50 | renderImage(props) {
|
51 |
|
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 |
|
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 |
|
69 |
|
70 |
|
71 |
|
72 |
|
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 |
|
165 | export default WebRich;
|