Skip to content

Commit

Permalink
Remove previewAdv before save
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio-Emmolo committed Jul 23, 2024
1 parent 520aeef commit 1da521e
Showing 1 changed file with 83 additions and 77 deletions.
160 changes: 83 additions & 77 deletions src/previewAdv.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,36 @@ tinymce.PluginManager.add('previewAdv', (editor, url) => {
}
})

editor.on('init', function () {
const params = editor.getParam('previewAdv') // Get the parameters from config file
const thresholds = params.thresholds
const blacklist = params.blacklist
editor.on('blur', function () {
removeAdvInEditor()
})

let pCount = 0 // Counter for <p> tags
let advCount = 1 // Counter for adv divs
editor.on('focus', function () {
if (isEnable) {
insertAdv()
}
togglePreviewAdv(isEnable)
})

function togglePreviewAdv (isEnable) {
const advDivs = editor.getBody().querySelectorAll('.adv-preview')
for (let i = 0; i < advDivs.length; i++) {
advDivs[i].style.display = isEnable ? 'block' : 'none'
}

// Update button state
setTimeout(() => {
const toolbar = editor.getContainer().querySelector('.tox-toolbar-overlord')
const btn = toolbar.querySelector('button[aria-label="Enable Preview Adv"]')
if (isEnable) {
btn.classList.add('tox-tbtn--enabled')
} else {
btn.classList.remove('tox-tbtn--enabled')
}
}, 0)
}

editor.on('init', function () {
insertAdv()
togglePreviewAdv(isEnable)

Expand All @@ -37,87 +59,71 @@ tinymce.PluginManager.add('previewAdv', (editor, url) => {
clearTimeout(typingTimer)
typingTimer = setTimeout(addAdvInEditor, typingDebounce)
})
})

// === FUNCTIONS ===
function insertAdv () {
// REMOVE OLD ADV FOR LOAD NEW
removeAdvInEditor()

const body = editor.getBody()
const paragraphs = body.getElementsByTagName('p')

for (let i = 0; i < paragraphs.length; i++) {
if (pCount === thresholds[advCount]) {
if (paragraphs[i].nextElementSibling && paragraphs[i].nextElementSibling.classList.contains('adv-preview')) {
continue
}

// === BLACKLIST ===
const bfBlacklist = blacklist.before.slice(1, -1).split('|')
const afBlacklist = blacklist.after.slice(1, -1).split('|')

bfBlacklist.push('<br', '\\[[^\\]]')
afBlacklist.push('<br', '\\[[^\\]]')

// AFTER BEFORE
if (bfBlacklist.some(item => new RegExp(item).test(paragraphs[i].innerHTML))) {
continue
}

// AFTER BLACKLIST
if (paragraphs[i + 1] && afBlacklist.some(item => new RegExp(item).test(paragraphs[i + 1].innerHTML))) {
continue
}
// === END BLACKLIST ===

const div = editor.dom.create('div', { class: 'adv-preview', contenteditable: 'false' })
div.style.backgroundColor = '#f3f3f3'
div.style.color = '#666'
div.style.padding = '10px'
div.style.border = '1px solid #ccc'
div.style.textAlign = 'center'
div.style.margin = '10px 0'
div.innerHTML = 'Spazio riservato per la pubblicità'
editor.dom.insertAfter(div, paragraphs[i])
advCount++
}
// === FUNCTIONS ===
function insertAdv () {
let pCount = 0
let advCount = 1

pCount++
}
}
function addAdvInEditor () {
pCount = 0
advCount = 1
const params = editor.getParam('previewAdv') // Get the parameters from config file
const thresholds = params.thresholds
const blacklist = params.blacklist

// const bookmark = editor.selection.getBookmark(2, true)
insertAdv()
// REMOVE OLD ADV FOR LOAD NEW
removeAdvInEditor()
const body = editor.getBody()
const paragraphs = body.getElementsByTagName('p')

setTimeout(function () {
editor.focus()
const nextNode = editor.selection.getNode().nextSibling
for (let i = 0; i < paragraphs.length; i++) {
if (pCount === thresholds[advCount]) {
if (paragraphs[i].nextElementSibling && paragraphs[i].nextElementSibling.classList.contains('adv-preview')) {
continue
}

if (nextNode) {
editor.selection.setCursorLocation(nextNode, -1)
// === BLACKLIST ===
const bfBlacklist = blacklist.before.slice(1, -1).split('|')
const afBlacklist = blacklist.after.slice(1, -1).split('|')

bfBlacklist.push('<br', '\\[[^\\]]')
afBlacklist.push('<br', '\\[[^\\]]')

// AFTER BEFORE
if (bfBlacklist.some(item => new RegExp(item).test(paragraphs[i].innerHTML))) {
continue
}
}, 0)
}
})

function togglePreviewAdv (isEnable) {
const advDivs = editor.getBody().querySelectorAll('.adv-preview')
// AFTER BLACKLIST
if (paragraphs[i + 1] && afBlacklist.some(item => new RegExp(item).test(paragraphs[i + 1].innerHTML))) {
continue
}
// === END BLACKLIST ===

const div = editor.dom.create('div', { class: 'adv-preview', contenteditable: 'false' })
div.style.backgroundColor = '#f3f3f3'
div.style.color = '#666'
div.style.padding = '10px'
div.style.border = '1px solid #ccc'
div.style.textAlign = 'center'
div.style.margin = '10px 0'
div.innerHTML = 'Spazio riservato per la pubblicità'
editor.dom.insertAfter(div, paragraphs[i])
advCount++
}

for (let i = 0; i < advDivs.length; i++) {
advDivs[i].style.display = isEnable ? 'block' : 'none'
pCount++
}
}
function addAdvInEditor () {
// const bookmark = editor.selection.getBookmark(2, true)
insertAdv()

// Update button state
setTimeout(() => {
const toolbar = editor.getContainer().querySelector('.tox-toolbar-overlord')
const btn = toolbar.querySelector('button[aria-label="Enable Preview Adv"]')
if (isEnable) {
btn.classList.add('tox-tbtn--enabled')
} else {
btn.classList.remove('tox-tbtn--enabled')
setTimeout(function () {
editor.focus()
const nextNode = editor.selection.getNode().nextSibling

if (nextNode) {
editor.selection.setCursorLocation(nextNode, -1)
}
}, 0)
}
Expand Down

0 comments on commit 1da521e

Please sign in to comment.