24
24
v-icon edit
25
25
span {{ $t('Rich text') }}
26
26
v-tab-item
27
- pre( ref ="render" , contenteditable ="true" , v-html = "htmlContent" , @input ="rebuildMarkdownFromHTML" )
27
+ pre( ref ="render" , contenteditable ="true" , @input ="rebuildMarkdownFromHTML" )
28
28
v-tab
29
29
v-icon code
30
30
span {{ $t('Markdown code') }}
34
34
35
35
<script >
36
36
function parser (input ) {
37
- let result = ' ' ;
37
+ let result = document . createDocumentFragment () ;
38
38
39
39
if (input .trim () !== ' ' ) {
40
40
let firstRule = {};
@@ -152,7 +152,18 @@ function parser(input) {
152
152
},
153
153
{
154
154
formatter : function (inner , outter ) {
155
- return ' <label md-outter="' + outter + ' " md-start-tag="[ ] "><input type="checkbox" disabled"/>' + parser (inner) + ' </label>' ;
155
+ const label = document .createElement (' label' );
156
+ label .setAttribute (' md-outter' , outter);
157
+ label .setAttribute (' md-start-tag' , ' [ ] ' );
158
+
159
+ const input = document .createElement (' input' );
160
+ input .setAttribute (' type' , ' checkbox' );
161
+ input .setAttribute (' disabled' , true );
162
+
163
+ label .appendChild ( input );
164
+ label .appendChild ( parser (inner) );
165
+
166
+ return label;
156
167
},
157
168
regex: / \[ \] ([^ \n ] + )(\n | $ )/ gm ,
158
169
outter_index: 0 ,
@@ -161,7 +172,22 @@ function parser(input) {
161
172
},
162
173
{
163
174
formatter : function (inner , outter ) {
164
- return ' <label md-outter="' + outter + ' " md-start-tag="[x] "><input type="checkbox" disabled checked/><del>' + parser (inner) + ' </del></label>' ;
175
+ const label = document .createElement (' label' );
176
+ label .setAttribute (' md-outter' , outter);
177
+ label .setAttribute (' md-start-tag' , ' [x] ' );
178
+
179
+ const input = document .createElement (' input' );
180
+ input .setAttribute (' type' , ' checkbox' );
181
+ input .setAttribute (' disabled' , true );
182
+ input .setAttribute (' checked' , true );
183
+
184
+ const del = document .createElement (' del' );
185
+ del .appendChild ( parser (inner) );
186
+
187
+ label .appendChild ( input );
188
+ label .appendChild ( del );
189
+
190
+ return label;
165
191
},
166
192
regex: / \[ x\] ([^ \n ] + )(\n | $ )/ gm ,
167
193
outter_index: 0 ,
@@ -190,32 +216,31 @@ function parser(input) {
190
216
191
217
if (typeof (firstRule .position ) !== ' undefined' && typeof (firstRule .rule ) !== ' undefined' && typeof (firstRule .test ) !== ' undefined' ) {
192
218
const before = input .substring (0 , firstRule .position );
193
- // result += '<span md-outter="' + before + '">' + before + '</span>';
194
- result += before;
219
+ if (before !== ' ' ) {
220
+ result .appendChild ( document .createTextNode (before) );
221
+ }
195
222
196
223
if (typeof (firstRule .rule .formatter ) === ' function' ) {
197
224
if (typeof (firstRule .rule .inner_index ) !== ' undefined' ) {
198
- result += firstRule .rule .formatter (firstRule .test [firstRule .rule .inner_index ], firstRule .test [firstRule .rule .outter_index ]);
225
+ result . appendChild ( firstRule .rule .formatter (firstRule .test [firstRule .rule .inner_index ], firstRule .test [firstRule .rule .outter_index ]) );
199
226
}
200
227
} else {
201
- result += ' <' + firstRule .rule .tag ;
202
- if (typeof (firstRule .rule .tag_arguments ) !== ' undefined' ) {
203
- result += ' ' + firstRule .rule .tag_arguments ;
204
- }
205
- result += ' md-outter="' + firstRule .test [firstRule .rule .outter_index ] + ' "' ;
228
+ var el = document .createElement (firstRule .rule .tag );
229
+
230
+ el .setAttribute (' md-outter' , firstRule .test [firstRule .rule .outter_index ]);
206
231
if (typeof (firstRule .rule .start_tag_index ) !== ' undefined' ) {
207
- result += ' md-start-tag=" ' + firstRule .test [firstRule .rule .start_tag_index ] + ' " ' ;
232
+ el . setAttribute ( ' md-start-tag' , firstRule .test [firstRule .rule .start_tag_index ]) ;
208
233
}
209
234
if (typeof (firstRule .rule .inner_index ) !== ' undefined' ) {
210
- result += ' >' + parser (firstRule .test [firstRule .rule .inner_index ]) + ' </' + firstRule .rule .tag + ' >' ;
211
- } else {
212
- result += ' />' ;
235
+ el .appendChild ( parser (firstRule .test [firstRule .rule .inner_index ]) );
213
236
}
237
+
238
+ result .appendChild ( el );
214
239
}
215
- result += parser (input .substring (firstRule .position + firstRule .test [firstRule .rule .outter_index ].length ));
240
+
241
+ result .appendChild ( parser (input .substring (firstRule .position + firstRule .test [firstRule .rule .outter_index ].length )) );
216
242
} else {
217
- // result += '<span md-outter="' + input + '">' + input + '</span>';
218
- result += input;
243
+ result .appendChild ( document .createTextNode (input) );
219
244
}
220
245
}
221
246
@@ -232,64 +257,17 @@ export default {
232
257
markdownContent: ' ' ,
233
258
};
234
259
},
235
- computed: {
236
- htmlContent () {
237
- return parser (this .markdownContent );
238
- },
239
- },
240
260
watch: {
241
261
value (val ) {
242
262
this .markdownContent = val;
243
263
},
244
- /* TODO
245
- markdownContent() {
246
- const selection = window.getSelection();
247
-
248
- let startSelection = selection.anchorOffset;
249
- let startNode = selection.anchorNode;
250
-
251
- let endSelection = selection.focusOffset;
252
- let endNode = selection.focusNode;
253
-
254
- if(startSelection > endSelection && startNode == endNode) {
255
- const temp = startSelection;
256
- const tempNode = startNode;
257
-
258
- startSelection = endSelection;
259
- startNode = endNode;
260
-
261
- endSelection = temp;
262
- endNode = tempNode;
263
- }
264
-
265
- let selectionStartIndex = 0;
266
- let selectionEndIndex = 0;
267
- for (let i_childNodes = 0; i_childNodes < this.$refs.render.childNodes.length; i_childNodes++) {
268
- const child = this.$refs.render.childNodes[i_childNodes];
269
-
270
- if(startNode.parentNode == child || startNode == child) {
271
- selectionStartIndex = i_childNodes+1;
272
- }
273
- if(endNode.parentNode == child || endNode == child) {
274
- selectionEndIndex = i_childNodes+1;
275
- }
264
+ markdownContent (val ) {
265
+ while (this .$refs .render .firstChild ) {
266
+ this .$refs .render .removeChild (this .$refs .render .firstChild );
276
267
}
277
268
278
- const that = this;
279
- setTimeout(function() {
280
- let range = document.createRange();
281
-
282
- startNode = that.$refs.render.childNodes[selectionStartIndex];
283
- endNode = that.$refs.render.childNodes[selectionEndIndex];
284
-
285
- range.setStart(startNode, 0);
286
- range.setEnd(endNode, endNode.textContent.length - 1);
287
-
288
- window.getSelection().removeAllRanges();
289
- window.getSelection().addRange(range);
290
- }, 200);
291
- }
292
- */
269
+ this .$refs .render .appendChild ( parser (val) );
270
+ },
293
271
},
294
272
methods: {
295
273
change (val ) {
0 commit comments