Skip to content

Commit 1da521e

Browse files
Remove previewAdv before save
1 parent 520aeef commit 1da521e

File tree

1 file changed

+83
-77
lines changed

1 file changed

+83
-77
lines changed

src/previewAdv.js

Lines changed: 83 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,36 @@ tinymce.PluginManager.add('previewAdv', (editor, url) => {
1313
}
1414
})
1515

16-
editor.on('init', function () {
17-
const params = editor.getParam('previewAdv') // Get the parameters from config file
18-
const thresholds = params.thresholds
19-
const blacklist = params.blacklist
16+
editor.on('blur', function () {
17+
removeAdvInEditor()
18+
})
2019

21-
let pCount = 0 // Counter for <p> tags
22-
let advCount = 1 // Counter for adv divs
20+
editor.on('focus', function () {
21+
if (isEnable) {
22+
insertAdv()
23+
}
24+
togglePreviewAdv(isEnable)
25+
})
26+
27+
function togglePreviewAdv (isEnable) {
28+
const advDivs = editor.getBody().querySelectorAll('.adv-preview')
29+
for (let i = 0; i < advDivs.length; i++) {
30+
advDivs[i].style.display = isEnable ? 'block' : 'none'
31+
}
2332

33+
// Update button state
34+
setTimeout(() => {
35+
const toolbar = editor.getContainer().querySelector('.tox-toolbar-overlord')
36+
const btn = toolbar.querySelector('button[aria-label="Enable Preview Adv"]')
37+
if (isEnable) {
38+
btn.classList.add('tox-tbtn--enabled')
39+
} else {
40+
btn.classList.remove('tox-tbtn--enabled')
41+
}
42+
}, 0)
43+
}
44+
45+
editor.on('init', function () {
2446
insertAdv()
2547
togglePreviewAdv(isEnable)
2648

@@ -37,87 +59,71 @@ tinymce.PluginManager.add('previewAdv', (editor, url) => {
3759
clearTimeout(typingTimer)
3860
typingTimer = setTimeout(addAdvInEditor, typingDebounce)
3961
})
62+
})
4063

41-
// === FUNCTIONS ===
42-
function insertAdv () {
43-
// REMOVE OLD ADV FOR LOAD NEW
44-
removeAdvInEditor()
45-
46-
const body = editor.getBody()
47-
const paragraphs = body.getElementsByTagName('p')
48-
49-
for (let i = 0; i < paragraphs.length; i++) {
50-
if (pCount === thresholds[advCount]) {
51-
if (paragraphs[i].nextElementSibling && paragraphs[i].nextElementSibling.classList.contains('adv-preview')) {
52-
continue
53-
}
54-
55-
// === BLACKLIST ===
56-
const bfBlacklist = blacklist.before.slice(1, -1).split('|')
57-
const afBlacklist = blacklist.after.slice(1, -1).split('|')
58-
59-
bfBlacklist.push('<br', '\\[[^\\]]')
60-
afBlacklist.push('<br', '\\[[^\\]]')
61-
62-
// AFTER BEFORE
63-
if (bfBlacklist.some(item => new RegExp(item).test(paragraphs[i].innerHTML))) {
64-
continue
65-
}
66-
67-
// AFTER BLACKLIST
68-
if (paragraphs[i + 1] && afBlacklist.some(item => new RegExp(item).test(paragraphs[i + 1].innerHTML))) {
69-
continue
70-
}
71-
// === END BLACKLIST ===
72-
73-
const div = editor.dom.create('div', { class: 'adv-preview', contenteditable: 'false' })
74-
div.style.backgroundColor = '#f3f3f3'
75-
div.style.color = '#666'
76-
div.style.padding = '10px'
77-
div.style.border = '1px solid #ccc'
78-
div.style.textAlign = 'center'
79-
div.style.margin = '10px 0'
80-
div.innerHTML = 'Spazio riservato per la pubblicità'
81-
editor.dom.insertAfter(div, paragraphs[i])
82-
advCount++
83-
}
64+
// === FUNCTIONS ===
65+
function insertAdv () {
66+
let pCount = 0
67+
let advCount = 1
8468

85-
pCount++
86-
}
87-
}
88-
function addAdvInEditor () {
89-
pCount = 0
90-
advCount = 1
69+
const params = editor.getParam('previewAdv') // Get the parameters from config file
70+
const thresholds = params.thresholds
71+
const blacklist = params.blacklist
9172

92-
// const bookmark = editor.selection.getBookmark(2, true)
93-
insertAdv()
73+
// REMOVE OLD ADV FOR LOAD NEW
74+
removeAdvInEditor()
75+
const body = editor.getBody()
76+
const paragraphs = body.getElementsByTagName('p')
9477

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

99-
if (nextNode) {
100-
editor.selection.setCursorLocation(nextNode, -1)
84+
// === BLACKLIST ===
85+
const bfBlacklist = blacklist.before.slice(1, -1).split('|')
86+
const afBlacklist = blacklist.after.slice(1, -1).split('|')
87+
88+
bfBlacklist.push('<br', '\\[[^\\]]')
89+
afBlacklist.push('<br', '\\[[^\\]]')
90+
91+
// AFTER BEFORE
92+
if (bfBlacklist.some(item => new RegExp(item).test(paragraphs[i].innerHTML))) {
93+
continue
10194
}
102-
}, 0)
103-
}
104-
})
10595

106-
function togglePreviewAdv (isEnable) {
107-
const advDivs = editor.getBody().querySelectorAll('.adv-preview')
96+
// AFTER BLACKLIST
97+
if (paragraphs[i + 1] && afBlacklist.some(item => new RegExp(item).test(paragraphs[i + 1].innerHTML))) {
98+
continue
99+
}
100+
// === END BLACKLIST ===
101+
102+
const div = editor.dom.create('div', { class: 'adv-preview', contenteditable: 'false' })
103+
div.style.backgroundColor = '#f3f3f3'
104+
div.style.color = '#666'
105+
div.style.padding = '10px'
106+
div.style.border = '1px solid #ccc'
107+
div.style.textAlign = 'center'
108+
div.style.margin = '10px 0'
109+
div.innerHTML = 'Spazio riservato per la pubblicità'
110+
editor.dom.insertAfter(div, paragraphs[i])
111+
advCount++
112+
}
108113

109-
for (let i = 0; i < advDivs.length; i++) {
110-
advDivs[i].style.display = isEnable ? 'block' : 'none'
114+
pCount++
111115
}
116+
}
117+
function addAdvInEditor () {
118+
// const bookmark = editor.selection.getBookmark(2, true)
119+
insertAdv()
112120

113-
// Update button state
114-
setTimeout(() => {
115-
const toolbar = editor.getContainer().querySelector('.tox-toolbar-overlord')
116-
const btn = toolbar.querySelector('button[aria-label="Enable Preview Adv"]')
117-
if (isEnable) {
118-
btn.classList.add('tox-tbtn--enabled')
119-
} else {
120-
btn.classList.remove('tox-tbtn--enabled')
121+
setTimeout(function () {
122+
editor.focus()
123+
const nextNode = editor.selection.getNode().nextSibling
124+
125+
if (nextNode) {
126+
editor.selection.setCursorLocation(nextNode, -1)
121127
}
122128
}, 0)
123129
}

0 commit comments

Comments
 (0)