• 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  + '<span>&nbsp;</span>'
316  + '<input name="checkbox" type="checkbox" onclick="onCheckChange(this)" class="note-checkbox">'
317  + '<span class="note-checkbox-txt">&nbsp;</span>';
318  document.execCommand('insertHTML', false, html);
319};
320
321function onCheckChange(checkbox) {
322  if (checkbox.checked === true) {
323    checkbox.setAttribute('checked', 'checked');
324  } else {
325    checkbox.removeAttribute('checked');
326  }
327}
328
329RICH_EDITOR.restorerange = function () {
330  var selection = window.getSelection();
331  selection.removeAllRanges();
332  var range = document.createRange();
333  range.setStart(RICH_EDITOR.currentSelection.startContainer, RICH_EDITOR.currentSelection.startOffset);
334  range.setEnd(RICH_EDITOR.currentSelection.endContainer, RICH_EDITOR.currentSelection.endOffset);
335  selection.addRange(range);
336};
337
338// 获取光标开始位置归属节点
339
340RICH_EDITOR.getSelectedAnchorNode = function () {
341  var node;
342  var selection;
343  if (window.getSelection) {
344    selection = getSelection();
345    node = selection.anchorNode;
346  }
347  if (!node && document.selection) {
348    selection = document.selection;
349    var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange();
350    node = range.commonAncestorContainer ? range.commonAncestorContainer : range.parentElement
351                                                                             ? range.parentElement() : range.item(0);
352  }
353  return node;
354};
355
356RICH_EDITOR.cancelSelection = function () {
357  var selection = window.getSelection();
358  selection.removeAllRanges();
359}
360
361var callBackToApp;
362
363function getHtmlContent() {
364  console.log('getHtmlContent');
365  var htmlString = RICH_EDITOR.getHtml();
366  let imgName = getImagePathFromContent(htmlString);
367  htmlString = window.btoa(unescape(encodeURIComponent(htmlString)));
368  callBackToApp.callbackImagePath(imgName);
369  var str = callBackToApp.callbackhtml(htmlString);
370  console.log('getHtmlContent end');
371}
372
373function saveHtmlContent() {
374  console.log('saveHtmlContent');
375  var htmlString = RICH_EDITOR.getHtml();
376  let imgName = getImagePathFromContent(htmlString);
377  htmlString = window.btoa(unescape(encodeURIComponent(htmlString)));
378
379  callBackToApp.callbackImagePath(imgName);
380  var str = callBackToApp.callbackhtmlSave(htmlString);
381  console.log('saveHtmlContent end');
382}
383
384function getImagePathFromContent(contentInfo) {
385  let imgReg = /<img[^>]+>/g;
386  let imgName = "";
387  let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
388  let imgArray = contentInfo.match(imgReg);
389  // 取第一张图片做为标题栏后的缩略图
390  if (imgArray && imgArray.length > 0) {
391    let src = imgArray[0].match(srcReg);
392    if (src != null && src.length > 1) {
393      imgName = src[1];
394      if (imgName.indexOf('shuxue.png') >= 0 || imgName.indexOf('cake.png') >= 0) {
395        imgName = "/res/" + imgName;
396      }
397    }
398  }
399  return imgName;
400}
401
402function scheduledSaveContent() {
403  console.info('scheduledSaveContent');
404  var htmlString = RICH_EDITOR.getHtml();
405  let imgName = getImagePathFromContent(htmlString);
406  htmlString = window.btoa(unescape(encodeURIComponent(htmlString)));
407  callBackToApp.callbackImagePath(imgName);
408  var str = callBackToApp.callbackScheduledSave(htmlString);
409  console.info('scheduledSaveContent end');
410}
411
412document.body.addEventListener('paste', (event) => {
413  let length = event.clipboardData.items.length;
414  if (length > 0) {
415    let file = event.clipboardData.items[0].getAsFile();
416    const reader = new FileReader();
417    reader.onloadend = () => {
418      callBackToApp.callbackPasteImage(reader.result);
419    }
420    reader.readAsDataURL(file);
421    event.preventDefault();
422  }
423});
424
425RICH_EDITOR.getFontSizes = function () {
426  document.execCommand('fontSize', false, null);
427  var fontElements = window.getSelection().anchorNode.parentNode;
428  var getSize = parseInt(window.getComputedStyle(fontElements, null).fontSize)
429  var str = callBackToApp.callbackGetSize(getSize);
430};
431
432RICH_EDITOR.insertImageHtml = function (contents) {
433  let selection = window.getSelection();
434  if (!selection.rangeCount)
435  return false;
436  selection.deleteFromDocument();
437  let img = document.createElement('img');
438  img.src = contents;
439  selection.getRangeAt(0).insertNode(img);
440};
441
442document.addEventListener('click', (e) => {
443  console.info(`lsq: e is ${JSON.stringify(e)}`)
444  var parent = document.getElementById('editorjs_box');
445  if (parent.id !== 'editorjs_box') {
446    e.preventDefault()
447  }
448})
449
450document.getElementById('addToDo').addEventListener('click', () => {
451  callBackToApp.addToDo()
452})
453
454document.getElementById('chooseStyle').addEventListener('click', () => {
455  callBackToApp.chooseStyle()
456})
457
458document.getElementById('openAlbum').addEventListener('click', () => {
459  callBackToApp.openAlbum()
460})
461
462function changeSizeToRk() {
463  document.getElementById('img1').style.width = '40px';
464  document.getElementById('img1').style.height = '40px';
465  document.getElementById('img2').style.width = '40px';
466  document.getElementById('img2').style.height = '40px';
467  document.getElementById('img3').style.width = '40px';
468  document.getElementById('img3').style.height = '40px';
469  document.getElementById('lable1').style.fontSize = '20px';
470  document.getElementById('lable2').style.fontSize = '20px';
471  document.getElementById('lable3').style.fontSize = '20px';
472}
473
474function changeSizeToPhone() {
475  document.getElementById('img1').style.width = '24px';
476  document.getElementById('img1').style.height = '24px';
477  document.getElementById('img2').style.width = '24px';
478  document.getElementById('img2').style.height = '24px';
479  document.getElementById('img3').style.width = '24px';
480  document.getElementById('img3').style.height = '24px';
481  document.getElementById('lable1').style.fontSize = '12px';
482  document.getElementById('lable2').style.fontSize = '12px';
483  document.getElementById('lable3').style.fontSize = '12px';
484}
485
486function changeSizeToTablet() {
487  document.getElementById('img1').style.width = '28px';
488  document.getElementById('img1').style.height = '28px';
489  document.getElementById('img2').style.width = '28px';
490  document.getElementById('img2').style.height = '28px';
491  document.getElementById('img3').style.width = '28px';
492  document.getElementById('img3').style.height = '28px';
493  document.getElementById('lable1').style.fontSize = '12px';
494  document.getElementById('lable2').style.fontSize = '12px';
495  document.getElementById('lable3').style.fontSize = '12px';
496}
497
498function hiddenButton() {
499  document.getElementById('buttonBox').style.display = 'none';
500}
501
502RICH_EDITOR.getFocus = function () {
503  return document.getElementById('editorjs_box').focus();
504}
505
506document.getElementById('editorjs_box').addEventListener('click', () => {
507  if (callBackToApp.getBreakPoint() === 'sm') {
508    document.getElementById('buttonBox').style.display = 'flex';
509  }
510})