diff --git a/src/previewAdv.js b/src/previewAdv.js index f2153b6..6f843cc 100644 --- a/src/previewAdv.js +++ b/src/previewAdv.js @@ -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
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)
@@ -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('
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('
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)
}