• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* @file
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16var RICH_EDITOR = {};
17
18RICH_EDITOR.editor = document.getElementById('editorjs_box');
19
20RICH_EDITOR.setHtml = function (contents) {
21  var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
22  if (base64regex.test(contents)) {
23    RICH_EDITOR.editor.innerHTML = decodeURIComponent(escape(atob(contents)));
24  } else {
25    RICH_EDITOR.editor.innerHTML = contents;
26  }
27};
28
29RICH_EDITOR.getHtml = function () {
30  return document.getElementById('editorjs_box').innerHTML;
31};
32
33RICH_EDITOR.undo = function () {
34  document.execCommand('undo', false, null);
35};
36
37RICH_EDITOR.redo = function () {
38  document.execCommand('redo', false, null);
39};
40
41RICH_EDITOR.setBold = function () {
42  document.execCommand('bold');
43};
44
45RICH_EDITOR.setItalic = function () {
46  document.execCommand('italic', false, null);
47};
48
49RICH_EDITOR.setSubscript = function () {
50  document.execCommand('subscript', false, null);
51};
52
53RICH_EDITOR.setSuperscript = function () {
54  document.execCommand('superscript', false, null);
55};
56
57RICH_EDITOR.setStrikeThrough = function () {
58  document.execCommand('strikeThrough', false, null);
59};
60
61RICH_EDITOR.setUnderline = function () {
62  document.execCommand('underline', false, null);
63};
64
65RICH_EDITOR.getListStyle = function () {
66  var selection;
67  var type;
68  if (window.getSelection) {
69    selection = getSelection();
70  }
71  if (!selection) {
72    return
73  }
74  var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange();
75  try {
76    var child = range.commonAncestorContainer;
77    for (var i = 0; i < 10; i++) {
78      if (child.nodeName === 'OL') {
79        console.info('insertOrderedList');
80        document.execCommand('insertOrderedList', false, null);
81        return child.style['list-style'];
82      }
83      if (child.nodeName === 'UL') {
84        console.info('insertUnorderedList');
85        document.execCommand('insertUnorderedList', false, null);
86        return child.style['list-style'];
87      }
88      if (child.parentNode) {
89        child = child.parentNode;
90      }
91    }
92  } catch (err) {
93    console.error(err);
94  }
95
96};
97
98RICH_EDITOR.setNumbers = function () {
99  let listStyle = RICH_EDITOR.getListStyle();
100  if (listStyle === 'decimal') {
101    return;
102  }
103  document.execCommand('insertOrderedList', false, null);
104  var selection;
105  var type;
106  if (window.getSelection) {
107    selection = getSelection();
108  }
109  if (!selection) {
110    return
111  }
112  var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange();
113  try {
114    var child = range.commonAncestorContainer;
115    for (var i = 0; i < 10; i++) {
116      if (child.nodeName === 'OL') {
117        child.style['list-style'] = 'decimal';
118        break;
119      }
120      if (child.parentNode) {
121        child = child.parentNode;
122      }
123    }
124  } catch (err) {
125    console.error(err);
126  }
127};
128
129RICH_EDITOR.setABC = function () {
130  let listStyle = RICH_EDITOR.getListStyle();
131  if (listStyle === 'lower-alpha') {
132    return;
133  }
134  document.execCommand('insertOrderedList', false, null);
135  var selection;
136  var type;
137  if (window.getSelection) {
138    selection = getSelection();
139  }
140  if (!selection) {
141    return
142  }
143  var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange();
144  try {
145    var child = range.commonAncestorContainer;
146    for (var i = 0; i < 10; i++) {
147      if (child.nodeName === 'OL') {
148        child.style['list-style'] = 'lower-alpha';
149        break;
150      }
151      if (child.parentNode) {
152        child = child.parentNode;
153      }
154    }
155  } catch (err) {
156    console.error(err);
157  }
158};
159
160RICH_EDITOR.setBullets = function () {
161  let listStyle = RICH_EDITOR.getListStyle();
162  if (listStyle === 'disc') {
163    return;
164  }
165  document.execCommand('insertUnorderedList', false, null);
166  var selection;
167  var type;
168  if (window.getSelection) {
169    selection = getSelection();
170  }
171  if (!selection) {
172    return
173  }
174  var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange();
175  try {
176    var child = range.commonAncestorContainer;
177    for (var i = 0; i < 10; i++) {
178      if (child.nodeName === 'UL') {
179        child.style['list-style'] = 'disc';
180        break;
181      }
182      if (child.parentNode) {
183        child = child.parentNode;
184      }
185    }
186  } catch (err) {
187    console.error(err);
188  }
189};
190
191RICH_EDITOR.setSquare = function () {
192  let listStyle = RICH_EDITOR.getListStyle();
193  if (listStyle === 'square') {
194    return;
195  }
196  document.execCommand('insertUnorderedList', false, null);
197  var selection;
198  var type;
199  if (window.getSelection) {
200    selection = getSelection();
201  }
202  if (!selection) {
203    return
204  }
205  var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange();
206  try {
207    var child = range.commonAncestorContainer;
208    for (var i = 0; i < 10; i++) {
209      if (child.nodeName === 'UL') {
210        child.style['list-style'] = 'square';
211        break;
212      }
213      if (child.parentNode) {
214        child = child.parentNode;
215      }
216    }
217  } catch (err) {
218    console.error(err);
219  }
220};
221
222RICH_EDITOR.setTextColor = function (color) {
223  document.execCommand('foreColor', false, color);
224};
225
226RICH_EDITOR.setFontSize = function (fontSize) {
227  document.execCommand('fontSize', false, fontSize);
228};
229
230RICH_EDITOR.execFontSize = function (size, unit) {
231  if (size === '12') {
232    document.execCommand('fontSize', false, 3);
233  } else if (size === '16') {
234    document.execCommand('fontSize', false, 4);
235  } else if (size === '20') {
236    document.execCommand('fontSize', false, 5);
237  } else if (size === '24') {
238    document.execCommand('fontSize', false, 6);
239  } else if (size === '28') {
240    document.execCommand('fontSize', false, 7);
241  }
242};
243
244var pad = 24;
245RICH_EDITOR.setIndent = function () {
246  var parents = document.getElementById('editorjs_box');
247  parents.removeAttribute('padding-left');
248  if (pad >= 408) {
249    return
250  }
251  pad = pad + 24;
252  parents.style.paddingLeft = pad + 'px';
253};
254
255RICH_EDITOR.setOutdent = function () {
256  var parents = document.getElementById('editorjs_box');
257  parents.removeAttribute('padding-left');
258  if (pad === 24) {
259    parents.style.paddingLeft = 24 + 'px';
260  } else {
261    pad = pad - 24;
262    parents.style.paddingLeft = pad + 'px';
263  }
264};
265
266RICH_EDITOR.setJustifyLeft = function () {
267  document.execCommand('justifyLeft', false, null);
268};
269
270RICH_EDITOR.setJustifyCenter = function () {
271  document.execCommand('justifyCenter', false, null);
272};
273
274RICH_EDITOR.setJustifyRight = function () {
275  document.execCommand('justifyRight', false, null);
276};
277
278RICH_EDITOR.insertImage = function (url) {
279  var html = '<br></br><img src="' + url
280  + '" alt="picvision" style="margin:0px auto;width:90%;display:table-cell;'
281  + 'vertical-align:middle;border-radius:10px;max-width:90%" /><br></br>';
282  document.getElementById('editorjs_box').innerHTML += html
283  document.getElementById('editorjs_box').scrollIntoView(false);
284};
285
286RICH_EDITOR.insertHTML = function (html) {
287  document.execCommand('insertHTML', false, html);
288};
289
290RICH_EDITOR.setDone = function () {
291  var html = '<input type="checkbox" checked="checked"/> &nbsp;';
292  document.execCommand('insertHTML', false, html);
293};
294
295RICH_EDITOR.addTodo = function (e) {
296  var KEY_ENTER;
297  KEY_ENTER = 13;
298  if (e.which === KEY_ENTER) {
299    var node = RICH_EDITOR.getSelectedAnchorNode();
300    if (node && node.nodeName === '#text') {
301      node = node.parentElement;
302    }
303    if (node && node.nodeName === 'SPAN' && node.previousElementSibling
304    && node.previousElementSibling.className === 'note-checkbox') {
305      RICH_EDITOR.setTodo();
306      e.preventDefault();
307    }
308  }
309};
310
311RICH_EDITOR.setTodo = function () {
312  var parent = document.getElementById('editorjs_box');
313  var isContentEmpty = parent.innerHTML.trim().length === 0 || parent.innerHTML === '<br>';
314  var html = (isContentEmpty ? '' : '<br/>')
315  + '<input name="checkbox" type="checkbox" onclick="onCheckChange(this)" class="note-checkbox">'
316  + '<span class="note-checkbox-txt">&nbsp;</span>';
317  document.execCommand('insertHTML', false, html);
318};
319
320function onCheckChange(checkbox) {
321  if (checkbox.checked === true) {
322    checkbox.setAttribute('checked', 'checked');
323  } else {
324    checkbox.removeAttribute('checked');
325  }
326}
327
328RICH_EDITOR.restorerange = function () {
329  var selection = window.getSelection();
330  selection.removeAllRanges();
331  var range = document.createRange();
332  range.setStart(RICH_EDITOR.currentSelection.startContainer, RICH_EDITOR.currentSelection.startOffset);
333  range.setEnd(RICH_EDITOR.currentSelection.endContainer, RICH_EDITOR.currentSelection.endOffset);
334  selection.addRange(range);
335};
336
337// 获取光标开始位置归属节点
338
339RICH_EDITOR.getSelectedAnchorNode = function () {
340  var node;
341  var selection;
342  if (window.getSelection) {
343    selection = getSelection();
344    node = selection.anchorNode;
345  }
346  if (!node && document.selection) {
347    selection = document.selection;
348    var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange();
349    node = range.commonAncestorContainer ? range.commonAncestorContainer : range.parentElement
350                                                                             ? range.parentElement() : range.item(0);
351  }
352  return node;
353};
354
355RICH_EDITOR.cancelSelection = function () {
356  var selection = window.getSelection();
357  selection.removeAllRanges();
358}
359
360var callBackToApp;
361
362function getHtmlContent() {
363  console.log('getHtmlContent');
364  var htmlString = RICH_EDITOR.getHtml();
365  let imgName = getImagePathFromContent(htmlString);
366  htmlString = window.btoa(unescape(encodeURIComponent(htmlString)));
367  callBackToApp.callbackImagePath(imgName);
368  var str = callBackToApp.callbackhtml(htmlString);
369  console.log('getHtmlContent end');
370}
371
372function saveHtmlContent() {
373  console.log('saveHtmlContent');
374  var htmlString = RICH_EDITOR.getHtml();
375  let imgName = getImagePathFromContent(htmlString);
376  htmlString = window.btoa(unescape(encodeURIComponent(htmlString)));
377
378  callBackToApp.callbackImagePath(imgName);
379  var str = callBackToApp.callbackhtmlSave(htmlString);
380  console.log('saveHtmlContent end');
381}
382
383function getImagePathFromContent(contentInfo) {
384  let imgReg = /<img[^>]+>/g;
385  let imgName = "";
386  let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
387  let imgArray = contentInfo.match(imgReg);
388  // 取第一张图片做为标题栏后的缩略图
389  if (imgArray && imgArray.length > 0) {
390    let src = imgArray[0].match(srcReg);
391    if (src != null && src.length > 1) {
392      imgName = src[1];
393      if (imgName.indexOf('shuxue.png') >= 0 || imgName.indexOf('cake.png') >= 0) {
394        imgName = "/res/" + imgName;
395      }
396    }
397  }
398  return imgName;
399}
400
401function scheduledSaveContent() {
402  console.info('scheduledSaveContent');
403  var htmlString = RICH_EDITOR.getHtml();
404  let imgName = getImagePathFromContent(htmlString);
405  htmlString = window.btoa(unescape(encodeURIComponent(htmlString)));
406  callBackToApp.callbackImagePath(imgName);
407  var str = callBackToApp.callbackScheduledSave(htmlString);
408  console.info('scheduledSaveContent end');
409}
410
411document.body.addEventListener('paste', (event) => {
412  let length = event.clipboardData.items.length;
413  if (length > 0) {
414    let file = event.clipboardData.items[0].getAsFile();
415    const reader = new FileReader();
416    reader.onloadend = () => {
417      callBackToApp.callbackPasteImage(reader.result);
418    }
419    reader.readAsDataURL(file);
420    event.preventDefault();
421  }
422});
423
424RICH_EDITOR.getFontSizes = function () {
425  document.execCommand('fontSize', false, null);
426  var fontElements = window.getSelection().anchorNode.parentNode;
427  var getSize = parseInt(window.getComputedStyle(fontElements, null).fontSize)
428  var str = callBackToApp.callbackGetSize(getSize);
429};
430
431RICH_EDITOR.insertImageHtml = function (contents) {
432  let selection = window.getSelection();
433  if (!selection.rangeCount)
434  return false;
435  selection.deleteFromDocument();
436  let img = document.createElement('img');
437  img.src = contents;
438  selection.getRangeAt(0).insertNode(img);
439};
440
441document.addEventListener('click', (e) => {
442  console.info(`lsq: e is ${JSON.stringify(e)}`)
443  var parent = document.getElementById('editorjs_box');
444  if (parent.id !== 'editorjs_box') {
445    e.preventDefault()
446  }
447})
448
449document.getElementById('addToDo').addEventListener('click', () => {
450  callBackToApp.addToDo()
451})
452
453document.getElementById('chooseStyle').addEventListener('click', () => {
454  callBackToApp.chooseStyle()
455})
456
457document.getElementById('openAlbum').addEventListener('click', () => {
458  callBackToApp.openAlbum()
459})
460
461function changeSizeToRk() {
462  document.getElementById('img1').style.width = '40px';
463  document.getElementById('img1').style.height = '40px';
464  document.getElementById('img2').style.width = '40px';
465  document.getElementById('img2').style.height = '40px';
466  document.getElementById('img3').style.width = '40px';
467  document.getElementById('img3').style.height = '40px';
468  document.getElementById('lable1').style.fontSize = '20px';
469  document.getElementById('lable2').style.fontSize = '20px';
470  document.getElementById('lable3').style.fontSize = '20px';
471}
472
473function changeSizeToPhone() {
474  document.getElementById('img1').style.width = '24px';
475  document.getElementById('img1').style.height = '24px';
476  document.getElementById('img2').style.width = '24px';
477  document.getElementById('img2').style.height = '24px';
478  document.getElementById('img3').style.width = '24px';
479  document.getElementById('img3').style.height = '24px';
480  document.getElementById('lable1').style.fontSize = '12px';
481  document.getElementById('lable2').style.fontSize = '12px';
482  document.getElementById('lable3').style.fontSize = '12px';
483}
484
485function changeSizeToTablet() {
486  document.getElementById('img1').style.width = '28px';
487  document.getElementById('img1').style.height = '28px';
488  document.getElementById('img2').style.width = '28px';
489  document.getElementById('img2').style.height = '28px';
490  document.getElementById('img3').style.width = '28px';
491  document.getElementById('img3').style.height = '28px';
492  document.getElementById('lable1').style.fontSize = '12px';
493  document.getElementById('lable2').style.fontSize = '12px';
494  document.getElementById('lable3').style.fontSize = '12px';
495}
496
497function hiddenButton() {
498  document.getElementById('buttonBox').style.display = 'none';
499}
500
501RICH_EDITOR.getFocus = function () {
502  return document.getElementById('editorjs_box').focus();
503}
504
505document.getElementById('editorjs_box').addEventListener('click', () => {
506  if (callBackToApp.getBreakPoint() === 'sm') {
507    document.getElementById('buttonBox').style.display = 'flex';
508  }
509})