Skip to content

Commit

Permalink
Add button for toggle preview ADV
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio-Emmolo committed Jul 4, 2024
1 parent 46d60d7 commit 4c000e0
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/previewAdv.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
/* global tinymce */

tinymce.PluginManager.add('previewAdv', (editor, url) => {
let isEnable = true

editor.ui.registry.addToggleButton('previewAdv', {
icon: 'preview',
tooltip: 'Enable Preview Adv',
onAction: async (_) => {
isEnable = !isEnable
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 () {
const params = editor.getParam('previewAdv')
const thresholds = params.thresholds
Expand All @@ -9,28 +39,13 @@ tinymce.PluginManager.add('previewAdv', (editor, url) => {
let advCount = 1 // Counter for adv divs

insertAdv()
togglePreviewAdv(isEnable)

// === 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
Expand Down Expand Up @@ -84,7 +99,6 @@ tinymce.PluginManager.add('previewAdv', (editor, url) => {
}

function addAdvInEditor () {

pCount = 0
advCount = 1

Expand Down

0 comments on commit 4c000e0

Please sign in to comment.