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