From 32e0fbb595d0adb64fdef9d09db4be0d30388a61 Mon Sep 17 00:00:00 2001 From: zwx1126739 Date: Thu, 18 Jul 2024 13:51:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=A4=87=E5=BF=98=E5=BD=95?= =?UTF-8?q?=E5=91=8A=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zwx1126739 --- common/resources/hvigorfile.js | 2 +- .../src/main/resources/rawfile/rich_editor.js | 103 ++++++++++-------- common/utils/hvigorfile.js | 2 +- features/hvigorfile.js | 2 +- hvigorfile.js | 2 +- product/default/hvigorfile.js | 2 +- 6 files changed, 65 insertions(+), 48 deletions(-) diff --git a/common/resources/hvigorfile.js b/common/resources/hvigorfile.js index 42ed4b4..29ad39f 100644 --- a/common/resources/hvigorfile.js +++ b/common/resources/hvigorfile.js @@ -1,3 +1,3 @@ // Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently. -module.exports = require('@ohos/hvigor-ohos-plugin').harTasks +module.exports = require('@ohos/hvigor-ohos-plugin').harTasks; diff --git a/common/resources/src/main/resources/rawfile/rich_editor.js b/common/resources/src/main/resources/rawfile/rich_editor.js index 119d9f4..936ee5e 100644 --- a/common/resources/src/main/resources/rawfile/rich_editor.js +++ b/common/resources/src/main/resources/rawfile/rich_editor.js @@ -14,10 +14,14 @@ */ var RICH_EDITOR = {}; - +let storage = window.localStorage; RICH_EDITOR.editor = document.getElementById('editorjs_box'); RICH_EDITOR.setHtml = function (contents) { + let paddingLeft = storage.getItem('paddingLeft'); + if (contents) { + RICH_EDITOR.editor.style.paddingLeft = paddingLeft + 'px'; + } var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/; if (base64regex.test(contents)) { RICH_EDITOR.editor.innerHTML = decodeURIComponent(escape(atob(contents))); @@ -69,7 +73,7 @@ RICH_EDITOR.getListStyle = function () { selection = getSelection(); } if (!selection) { - return + return; } var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange(); try { @@ -78,12 +82,14 @@ RICH_EDITOR.getListStyle = function () { if (child.nodeName === 'OL') { console.info('insertOrderedList'); document.execCommand('insertOrderedList', false, null); - return child.style['list-style']; + child.style['list-style']; + break; } if (child.nodeName === 'UL') { console.info('insertUnorderedList'); document.execCommand('insertUnorderedList', false, null); - return child.style['list-style']; + child.style['list-style']; + break; } if (child.parentNode) { child = child.parentNode; @@ -107,7 +113,7 @@ RICH_EDITOR.setNumbers = function () { selection = getSelection(); } if (!selection) { - return + return; } var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange(); try { @@ -138,7 +144,7 @@ RICH_EDITOR.setABC = function () { selection = getSelection(); } if (!selection) { - return + return; } var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange(); try { @@ -169,7 +175,7 @@ RICH_EDITOR.setBullets = function () { selection = getSelection(); } if (!selection) { - return + return; } var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange(); try { @@ -200,7 +206,7 @@ RICH_EDITOR.setSquare = function () { selection = getSelection(); } if (!selection) { - return + return; } var range = selection.getRangeAt ? selection.getRangeAt(0) : selection.createRange(); try { @@ -242,27 +248,36 @@ RICH_EDITOR.execFontSize = function (size, unit) { }; var pad = 24; -RICH_EDITOR.setIndent = function (pad) { +RICH_EDITOR.setIndent = function () { var parents = document.getElementById('editorjs_box'); parents.removeAttribute('padding-left'); if (pad >= 408) { - return + return; } pad = pad + 24; parents.style.paddingLeft = pad + 'px'; - document.execCommand('indent', false, pad); + if (!storage) { + return; + } + storage.setItem('paddingLeft', pad); }; -RICH_EDITOR.setOutdent = function (pad) { +RICH_EDITOR.setOutdent = function () { var parents = document.getElementById('editorjs_box'); parents.removeAttribute('padding-left'); if (pad === 24) { parents.style.paddingLeft = 24 + 'px'; - document.execCommand('outdent', false, pad); + if (!storage) { + return; + } + storage.setItem('paddingLeft', pad); } else { pad = pad - 24; parents.style.paddingLeft = pad + 'px'; - document.execCommand('outdent', false, pad); + if (!storage) { + return; + } + storage.setItem('paddingLeft', pad); } }; @@ -279,10 +294,10 @@ RICH_EDITOR.setJustifyRight = function () { }; RICH_EDITOR.insertImage = function (url) { - var html = '

picvision

'; - document.getElementById('editorjs_box').innerHTML += html + var html = '

picvision

'; + document.getElementById('editorjs_box').innerHTML += html; document.getElementById('editorjs_box').scrollIntoView(false); }; @@ -303,8 +318,8 @@ RICH_EDITOR.addTodo = function (e) { if (node && node.nodeName === '#text') { node = node.parentElement; } - if (node && node.nodeName === 'SPAN' && node.previousElementSibling - && node.previousElementSibling.className === 'note-checkbox') { + if (node && node.nodeName === 'SPAN' && node.previousElementSibling && + node.previousElementSibling.className === 'note-checkbox') { RICH_EDITOR.setTodo(); e.preventDefault(); } @@ -314,10 +329,10 @@ RICH_EDITOR.addTodo = function (e) { RICH_EDITOR.setTodo = function () { var parent = document.getElementById('editorjs_box'); var isContentEmpty = parent.innerHTML.trim().length === 0 || parent.innerHTML === '
'; - var html = (isContentEmpty ? '' : '
') - + ' ' - + '' - + ' '; + var html = (isContentEmpty ? '' : '
') + + ' ' + + '' + + ' '; document.execCommand('insertHTML', false, html); }; @@ -359,7 +374,7 @@ RICH_EDITOR.getSelectedAnchorNode = function () { RICH_EDITOR.cancelSelection = function () { var selection = window.getSelection(); selection.removeAllRanges(); -} +}; var callBackToApp; @@ -386,7 +401,7 @@ function saveHtmlContent() { function getImagePathFromContent(contentInfo) { let imgReg = /]+>/g; - let imgName = ""; + let imgName = ''; let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; let imgArray = contentInfo.match(imgReg); // 取第一张图片做为标题栏后的缩略图 @@ -395,7 +410,7 @@ function getImagePathFromContent(contentInfo) { if (src != null && src.length > 1) { imgName = src[1]; if (imgName.indexOf('shuxue.png') >= 0 || imgName.indexOf('cake.png') >= 0) { - imgName = "/res/" + imgName; + imgName = '/res/' + imgName; } } } @@ -421,7 +436,7 @@ document.body.addEventListener('paste', (event) => { const reader = new FileReader(); reader.onloadend = () => { callBackToApp.callbackPasteImage(reader.result); - } + }; reader.readAsDataURL(file); event.preventDefault(); } @@ -430,39 +445,41 @@ document.body.addEventListener('paste', (event) => { RICH_EDITOR.getFontSizes = function () { document.execCommand('fontSize', false, null); var fontElements = window.getSelection().anchorNode.parentNode; - var getSize = parseInt(window.getComputedStyle(fontElements, null).fontSize) + var getSize = parseInt(window.getComputedStyle(fontElements, null).fontSize); var str = callBackToApp.callbackGetSize(getSize); }; RICH_EDITOR.insertImageHtml = function (contents) { let selection = window.getSelection(); - if (!selection.rangeCount) - return false; + if (!selection.rangeCount) { + return false; + } selection.deleteFromDocument(); let img = document.createElement('img'); img.src = contents; selection.getRangeAt(0).insertNode(img); + return; }; document.addEventListener('click', (e) => { - console.info(`lsq: e is ${JSON.stringify(e)}`) + console.info(`lsq: e is ${JSON.stringify(e)}`); var parent = document.getElementById('editorjs_box'); if (parent.id !== 'editorjs_box') { - e.preventDefault() + e.preventDefault(); } -}) +}); document.getElementById('addToDo').addEventListener('click', () => { - callBackToApp.addToDo() -}) + callBackToApp.addToDo(); +}); document.getElementById('chooseStyle').addEventListener('click', () => { - callBackToApp.chooseStyle() -}) + callBackToApp.chooseStyle(); +}); document.getElementById('openAlbum').addEventListener('click', () => { - callBackToApp.openAlbum() -}) + callBackToApp.openAlbum(); +}); function changeSizeToRk() { document.getElementById('img1').style.width = '40px'; @@ -506,12 +523,12 @@ function hiddenButton() { RICH_EDITOR.getFocus = function () { return document.getElementById('editorjs_box').focus(); -} +}; RICH_EDITOR.getBlur = function () { return document.getElementById('editorjs_box').blur(); -} +}; document.getElementById('editorjs_box').addEventListener('click', () => { document.getElementById('buttonBox').style.display = 'flex'; -}) \ No newline at end of file +}); \ No newline at end of file diff --git a/common/utils/hvigorfile.js b/common/utils/hvigorfile.js index 42ed4b4..29ad39f 100644 --- a/common/utils/hvigorfile.js +++ b/common/utils/hvigorfile.js @@ -1,3 +1,3 @@ // Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently. -module.exports = require('@ohos/hvigor-ohos-plugin').harTasks +module.exports = require('@ohos/hvigor-ohos-plugin').harTasks; diff --git a/features/hvigorfile.js b/features/hvigorfile.js index 42ed4b4..29ad39f 100644 --- a/features/hvigorfile.js +++ b/features/hvigorfile.js @@ -1,3 +1,3 @@ // Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently. -module.exports = require('@ohos/hvigor-ohos-plugin').harTasks +module.exports = require('@ohos/hvigor-ohos-plugin').harTasks; diff --git a/hvigorfile.js b/hvigorfile.js index 9602dc9..c7d411d 100644 --- a/hvigorfile.js +++ b/hvigorfile.js @@ -14,4 +14,4 @@ */ // Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently. -module.exports = require('@ohos/hvigor-ohos-plugin').appTasks \ No newline at end of file +module.exports = require('@ohos/hvigor-ohos-plugin').appTasks; \ No newline at end of file diff --git a/product/default/hvigorfile.js b/product/default/hvigorfile.js index d7720ee..c12d7a8 100644 --- a/product/default/hvigorfile.js +++ b/product/default/hvigorfile.js @@ -1,2 +1,2 @@ // Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently. -module.exports = require('@ohos/hvigor-ohos-plugin').hapTasks +module.exports = require('@ohos/hvigor-ohos-plugin').hapTasks;