From 728b6cb823fcda5fb1d08ff50979f2909420169d Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Wed, 15 Dec 2021 11:16:29 +0500 Subject: [PATCH] fix content loading in editor causes not to save --- apps/mobile/html/Web.bundle/site/init.js | 52 ++++++++++++------- apps/mobile/html/Web.bundle/site/listeners.js | 9 +++- apps/mobile/src/views/Editor/Functions.js | 31 +++++------ apps/mobile/src/views/Editor/tiny/tiny.js | 4 +- 4 files changed, 55 insertions(+), 41 deletions(-) diff --git a/apps/mobile/html/Web.bundle/site/init.js b/apps/mobile/html/Web.bundle/site/init.js index 9b5f492f9..852a62f53 100755 --- a/apps/mobile/html/Web.bundle/site/init.js +++ b/apps/mobile/html/Web.bundle/site/init.js @@ -149,18 +149,18 @@ function init_callback(_editor) { reactNativeEventHandler('focus', 'editor'); }); - editor.on('SetContent', function (event) { - if (globalThis.isClearingNoteData) { - globalThis.isClearingNoteData = false; - return; - } - setTimeout(function () { - editor.undoManager.transact(function () {}); - }, 1000); - if (!event.paste) { - reactNativeEventHandler('noteLoaded', true); - } - }); + // editor.on('SetContent', function (event) { + // if (globalThis.isClearingNoteData) { + // globalThis.isClearingNoteData = false; + // return; + // } + // setTimeout(function () { + // editor.undoManager.transact(function () {}); + // }, 1000); + // if (!event.paste) { + // reactNativeEventHandler('noteLoaded', true); + // } + // }); editor.on('NewBlock', function (e) { console.log('New Block'); @@ -215,10 +215,13 @@ function init_callback(_editor) { block: 'nearest' }); }); - editor.on('input', onChange); - editor.on('keyup', onChange); - editor.on('NodeChange', onChange); - editor.on('compositionend', onChange); + + editor.on('input ExecCommand ObjectResized Redo Undo', onChange); + editor.on('keyup', e => { + if (e.key !== 'Backspace') return; + if (!editor.getHTML) return; + onChange(); + }); } const plugins = [ @@ -493,12 +496,20 @@ function delay(base = 0) { let noteedited = false; const onChange = function (event) { - console.log(event.type, event.selectionChange); - if (event.type === 'nodechange' && !event.selectionChange) return; + // console.log(event.type, event.selectionChange); + // if (event.type === 'nodechange' && !event.selectionChange) return; + + if (event.type && event.type.toLowerCase() === 'execcommand') { + if ( + event.command.toLowerCase() === 'mcefocus' || + event.command.toLowerCase() === 'mcerepaint' + ) { + return; + } + } if (isLoading) { isLoading = false; - return; } if (prevCount === 0) { @@ -506,7 +517,8 @@ const onChange = function (event) { } if (prevCount === 0 && event.type !== 'paste') return; - if (event.type !== 'nodechange' && event.type !== 'compositionend') { + console.log(event); + if (event.type !== 'compositionend') { if (!noteedited) { noteedited = true; reactNativeEventHandler('noteedited'); diff --git a/apps/mobile/html/Web.bundle/site/listeners.js b/apps/mobile/html/Web.bundle/site/listeners.js index 467ad71a9..e90910270 100755 --- a/apps/mobile/html/Web.bundle/site/listeners.js +++ b/apps/mobile/html/Web.bundle/site/listeners.js @@ -150,14 +150,19 @@ function attachMessageListener() { switch (type) { case 'inject': - tinymce.activeEditor.setContent(value); + tinymce.activeEditor.setHTML(value); break; case 'html': isLoading = true; globalThis.isClearingNoteData = false; tinymce.activeEditor.mode.set('readonly'); if (!isInvalidValue(value)) { - tinymce.activeEditor.setContent(value); + tinymce.activeEditor.setHTML(value); + reactNativeEventHandler('noteLoaded', true); + globalThis.isClearingNoteData = false; + setTimeout(function () { + tinymce.activeEditor.undoManager.transact(function () {}); + }, 1000); } else { globalThis.isClearingNoteData = false; reactNativeEventHandler('noteLoaded', true); diff --git a/apps/mobile/src/views/Editor/Functions.js b/apps/mobile/src/views/Editor/Functions.js index b8b3218a3..4509e89ca 100644 --- a/apps/mobile/src/views/Editor/Functions.js +++ b/apps/mobile/src/views/Editor/Functions.js @@ -118,7 +118,6 @@ export async function clearTimer(clear) { }); } waitForContent = false; - console.log("cleared timer & saving note",content.data); if (clear) { if (!noteEdited) return; if ( @@ -449,22 +448,21 @@ export const _onMessage = async evt => { break; case 'tiny': if (message.sessionId !== sessionId) return; - if (message.value === '
') return; - if (message.value !== content.data) { - if (prevNoteContent && message.value === prevNoteContent) { - prevNoteContent = null; - noteEdited = false; - return; - } - - noteEdited = true; - lastEditTime = Date.now(); - content = { - type: message.type, - data: message.value - }; - onNoteChange(); + if (prevNoteContent && message.value === prevNoteContent) { + prevNoteContent = null; + noteEdited = false; + return; } + + console.log("tiny content recieved"); + + noteEdited = true; + lastEditTime = Date.now(); + content = { + type: message.type, + data: message.value + }; + onNoteChange(); if (waitForContent) { eSendEvent('content_event'); } @@ -607,7 +605,6 @@ export async function clearEditor( waitForContent = true; await clearTimer(true); } - console.log(noteEdited, content.data); disableSaving = true; db.fs.cancel(getNote()?.id); diff --git a/apps/mobile/src/views/Editor/tiny/tiny.js b/apps/mobile/src/views/Editor/tiny/tiny.js index 96b194870..a44d402da 100644 --- a/apps/mobile/src/views/Editor/tiny/tiny.js +++ b/apps/mobile/src/views/Editor/tiny/tiny.js @@ -8,7 +8,7 @@ document.getElementById("titleInput").value = ''; autosize(); window.prevContent = ""; globalThis.isClearingNoteData = true; -tinymce.activeEditor.setContent(''); +tinymce.activeEditor.setHTML(''); tinymce.activeEditor.undoManager.clear(); info = document.querySelector(infoBar); info.querySelector('#infosaved').innerText = ""; @@ -90,7 +90,7 @@ const updateSavingState = value => ` export const clearEditor = ` -tinymce.activeEditor.setContent(""); +tinymce.activeEditor.setHTML(""); `; const clearTitle = ` document.getElementById(titleInput).value = '';