Skip to content

Commit

Permalink
previewAdv.js, fix cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio-Emmolo committed Jul 1, 2024
1 parent 9a5d09b commit 46d60d7
Showing 1 changed file with 54 additions and 19 deletions.
73 changes: 54 additions & 19 deletions src/previewAdv.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,51 @@ tinymce.PluginManager.add('previewAdv', (editor, url) => {
let pCount = 0 // Counter for <p> tags
let advCount = 1 // Counter for adv divs

// Funzione per inserire le div di pubblicità
insertAdv()

// === TIMER ===
let typingTimer
const typingDebounce = 1000 // 1 second

// === EDITOR ACTIONS ===
// editor.on('change', function () {
// const bookmark = editor.selection.getBookmark(2, true)
//
// pCount = 0
// advCount = 1
// if (typing) {
// return
// }
// insertAdv()
//
// setTimeout(function () {
// editor.selection.moveToBookmark(bookmark)
// editor.focus()
// }, 0)
// })

editor.on('SaveContent', function (e) {
const tempDiv = document.createElement('div')
tempDiv.innerHTML = e.content

const advDivs = tempDiv.querySelectorAll('.adv-preview')
advDivs.forEach(function (div) {
div.remove()
})

e.content = tempDiv.innerHTML
})

editor.on('keydown', function () {
clearTimeout(typingTimer)
})

editor.on('keyup', function () {
clearTimeout(typingTimer)
typingTimer = setTimeout(addAdvInEditor, typingDebounce)
})

// === FUNCTIONS ===
function insertAdv () {
const advDivs = editor.getBody().querySelectorAll('.adv-preview')
for (let i = 0; i < advDivs.length; i++) {
Expand Down Expand Up @@ -39,32 +83,23 @@ tinymce.PluginManager.add('previewAdv', (editor, url) => {
}
}

insertAdv()

editor.on('change', function () {
const bookmark = editor.selection.getBookmark(2, true)
function addAdvInEditor () {

pCount = 0
advCount = 1

// const bookmark = editor.selection.getBookmark(2, true)
insertAdv()

setTimeout(function () {
editor.selection.moveToBookmark(bookmark)
editor.focus()
}, 0)
})
const nextNode = editor.selection.getNode().nextSibling

editor.on('SaveContent', function (e) {
const tempDiv = document.createElement('div')
tempDiv.innerHTML = e.content

const advDivs = tempDiv.querySelectorAll('.adv-preview')
advDivs.forEach(function (div) {
div.remove()
})

e.content = tempDiv.innerHTML
})
if (nextNode) {
editor.selection.setCursorLocation(nextNode, -1)
}
}, 0)
}
})

return {
Expand Down

0 comments on commit 46d60d7

Please sign in to comment.