| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
const codeEditor = function () { |
| 5 |
const editorSettings = wp.codeEditor.defaultSettings ? _.clone(wp.codeEditor.defaultSettings) : {}; |
| 6 |
const codemirror_gen = |
| 7 |
{ |
| 8 |
"indentUnit": 2, |
| 9 |
"indentWithTabs": true, |
| 10 |
"inputStyle": "contenteditable", |
| 11 |
"lineNumbers": true, |
| 12 |
"lineWrapping": true, |
| 13 |
"matchBrackets": true, |
| 14 |
"styleActiveLine": true, |
| 15 |
"continueComments": true, |
| 16 |
"extraKeys": { |
| 17 |
"Ctrl-Space": "autocomplete", |
| 18 |
"Ctrl-\/": "toggleComment", |
| 19 |
"Cmd-\/": "toggleComment", |
| 20 |
"Alt-F": "findPersistent", |
| 21 |
"Ctrl-F": "findPersistent", |
| 22 |
"Cmd-F": "findPersistent" |
| 23 |
}, |
| 24 |
"direction": "ltr", |
| 25 |
"gutters": ["CodeMirror-lint-markers"], |
| 26 |
"lint": true, |
| 27 |
"autoCloseBrackets": true, |
| 28 |
"autoCloseTags": true, |
| 29 |
"matchTags": { |
| 30 |
"bothTags": true |
| 31 |
}, |
| 32 |
"tabSize": 2, |
| 33 |
|
| 34 |
}; |
| 35 |
|
| 36 |
const html_code = document.getElementById('html_code'); |
| 37 |
let htmleditor; |
| 38 |
if (html_code) { |
| 39 |
let codemirror_el = |
| 40 |
{ |
| 41 |
"tagname-lowercase": true, |
| 42 |
"attr-lowercase": true, |
| 43 |
"attr-value-double-quotes": false, |
| 44 |
"doctype-first": false, |
| 45 |
"tag-pair": true, |
| 46 |
"spec-char-escape": true, |
| 47 |
"id-unique": true, |
| 48 |
"src-not-empty": true, |
| 49 |
"attr-no-duplication": true, |
| 50 |
"alt-require": true, |
| 51 |
"space-tab-mixed-disabled": "tab", |
| 52 |
"attr-unsafe-chars": true, |
| 53 |
"mode": 'htmlmixed', |
| 54 |
|
| 55 |
}; |
| 56 |
|
| 57 |
editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,); |
| 58 |
|
| 59 |
htmleditor = wp.codeEditor.initialize(html_code, editorSettings); |
| 60 |
|
| 61 |
|
| 62 |
const htmlNavigationMenu = document.getElementById("htmlNavigationMenu"); |
| 63 |
|
| 64 |
htmleditor.codemirror.eachLine(function (line) { |
| 65 |
const text = line.text; |
| 66 |
if (text.startsWith("<!-- NAV:")) { |
| 67 |
let navItem = document.createElement("li"); |
| 68 |
let navLink = document.createElement("a"); |
| 69 |
navLink.href = "#"; |
| 70 |
// navLink.innerText = text.replace("<!-- NAV:", "").trim(); |
| 71 |
navLink.innerText = text.replace(/<!-- NAV:|-->|<!-- NAV: -->/g, '').trim(); |
| 72 |
navLink.addEventListener("click", function (e) { |
| 73 |
e.preventDefault(); |
| 74 |
|
| 75 |
// Remove existing highlights from the gutter |
| 76 |
htmleditor.codemirror.eachLine(function (l) { |
| 77 |
htmleditor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter'); |
| 78 |
}); |
| 79 |
|
| 80 |
// Set cursor and scroll to line |
| 81 |
htmleditor.codemirror.setCursor(line.lineNo()); |
| 82 |
let heightOfLine = htmleditor.codemirror.defaultTextHeight(); |
| 83 |
let verticalPos = line.lineNo() * heightOfLine; |
| 84 |
htmleditor.codemirror.scrollTo(null, verticalPos); |
| 85 |
|
| 86 |
// Highlight the line number in the gutter |
| 87 |
htmleditor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter'); |
| 88 |
|
| 89 |
htmleditor.codemirror.focus(); |
| 90 |
}); |
| 91 |
navItem.appendChild(navLink); |
| 92 |
htmlNavigationMenu.appendChild(navItem); |
| 93 |
} |
| 94 |
}); |
| 95 |
} |
| 96 |
|
| 97 |
const css_code = document.getElementById('css_code'); |
| 98 |
let csseditor; |
| 99 |
if (css_code) { |
| 100 |
let codemirror_el = {"mode": 'css',}; |
| 101 |
editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,); |
| 102 |
// const tab = document.querySelector('[data-content="css-code"]'); |
| 103 |
// tab.classList.add('tab-content-active'); |
| 104 |
csseditor = wp.codeEditor.initialize(css_code, editorSettings); |
| 105 |
// tab.classList.remove('tab-content-active'); |
| 106 |
|
| 107 |
const cssNavigationMenu = document.getElementById("cssNavigationMenu"); |
| 108 |
|
| 109 |
csseditor.codemirror.eachLine(function (line) { |
| 110 |
const text = line.text; |
| 111 |
if (text.startsWith("/* NAV:")) { |
| 112 |
let navItem = document.createElement("li"); |
| 113 |
let navLink = document.createElement("a"); |
| 114 |
navLink.href = "#"; |
| 115 |
// navLink.innerText = text.replace("// NAV:", "").trim(); |
| 116 |
navLink.innerText = text.replace(/\/\* NAV:|\*\/|\/\* NAV: \*\//g, '').trim(); |
| 117 |
navLink.addEventListener("click", function (e) { |
| 118 |
e.preventDefault(); |
| 119 |
|
| 120 |
// Remove existing highlights from the gutter |
| 121 |
csseditor.codemirror.eachLine(function (l) { |
| 122 |
csseditor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter'); |
| 123 |
}); |
| 124 |
|
| 125 |
// Set cursor and scroll to line |
| 126 |
csseditor.codemirror.setCursor(line.lineNo()); |
| 127 |
let heightOfLine = csseditor.codemirror.defaultTextHeight(); |
| 128 |
let verticalPos = line.lineNo() * heightOfLine; |
| 129 |
csseditor.codemirror.scrollTo(null, verticalPos); |
| 130 |
|
| 131 |
// Highlight the line number in the gutter |
| 132 |
csseditor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter'); |
| 133 |
|
| 134 |
csseditor.codemirror.focus(); |
| 135 |
}); |
| 136 |
navItem.appendChild(navLink); |
| 137 |
cssNavigationMenu.appendChild(navItem); |
| 138 |
} |
| 139 |
}); |
| 140 |
} |
| 141 |
|
| 142 |
const js_code = document.getElementById('js_code'); |
| 143 |
let jseditor; |
| 144 |
if (js_code) { |
| 145 |
let codemirror_el = { |
| 146 |
"boss": true, |
| 147 |
"curly": true, |
| 148 |
"eqeqeq": true, |
| 149 |
"eqnull": true, |
| 150 |
"es3": true, |
| 151 |
"expr": true, |
| 152 |
"immed": true, |
| 153 |
"noarg": true, |
| 154 |
"nonbsp": true, |
| 155 |
"onevar": true, |
| 156 |
"quotmark": "single", |
| 157 |
"trailing": true, |
| 158 |
"undef": true, |
| 159 |
"unused": true, |
| 160 |
"browser": true, |
| 161 |
"globals": { |
| 162 |
"_": false, |
| 163 |
"Backbone": false, |
| 164 |
"jQuery": true, |
| 165 |
"JSON": false, |
| 166 |
"wp": false |
| 167 |
}, |
| 168 |
"mode": 'javascript', |
| 169 |
}; |
| 170 |
editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,); |
| 171 |
// const tab = document.querySelector('[data-content="js-code"]'); |
| 172 |
// tab.classList.add('tab-content-active'); |
| 173 |
jseditor = wp.codeEditor.initialize(js_code, editorSettings); |
| 174 |
// tab.classList.remove('tab-content-active'); |
| 175 |
|
| 176 |
const jsNavigationMenu = document.getElementById("jsNavigationMenu"); |
| 177 |
|
| 178 |
jseditor.codemirror.eachLine(function (line) { |
| 179 |
const text = line.text; |
| 180 |
if (text.startsWith("// NAV:")) { |
| 181 |
let navItem = document.createElement("li"); |
| 182 |
let navLink = document.createElement("a"); |
| 183 |
navLink.href = "#"; |
| 184 |
navLink.innerText = text.replace("// NAV:", "").trim(); |
| 185 |
navLink.addEventListener("click", function (e) { |
| 186 |
e.preventDefault(); |
| 187 |
|
| 188 |
// Remove existing highlights from the gutter |
| 189 |
jseditor.codemirror.eachLine(function (l) { |
| 190 |
jseditor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter'); |
| 191 |
}); |
| 192 |
|
| 193 |
// Set cursor and scroll to line |
| 194 |
jseditor.codemirror.setCursor(line.lineNo()); |
| 195 |
let heightOfLine = jseditor.codemirror.defaultTextHeight(); |
| 196 |
let verticalPos = line.lineNo() * heightOfLine; |
| 197 |
jseditor.codemirror.scrollTo(null, verticalPos); |
| 198 |
|
| 199 |
// Highlight the line number in the gutter |
| 200 |
jseditor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter'); |
| 201 |
|
| 202 |
jseditor.codemirror.focus(); |
| 203 |
}); |
| 204 |
navItem.appendChild(navLink); |
| 205 |
jsNavigationMenu.appendChild(navItem); |
| 206 |
} |
| 207 |
}); |
| 208 |
} |
| 209 |
|
| 210 |
const php_code = document.getElementById('php_code'); |
| 211 |
if (php_code) { |
| 212 |
let codemirror_el = { |
| 213 |
firstLineNumber: 4, |
| 214 |
mode: { |
| 215 |
name: 'php', |
| 216 |
startOpen: true |
| 217 |
} |
| 218 |
}; |
| 219 |
editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,); |
| 220 |
// const tab = document.querySelector('[data-content="php-code"]'); |
| 221 |
// tab.classList.add('tab-content-active'); |
| 222 |
const phpeditor = wp.codeEditor.initialize(php_code, editorSettings); |
| 223 |
// tab.classList.remove('tab-content-active'); |
| 224 |
|
| 225 |
const phpNavigationMenu = document.getElementById("phpNavigationMenu"); |
| 226 |
|
| 227 |
phpeditor.codemirror.eachLine(function (line) { |
| 228 |
const text = line.text; |
| 229 |
if (text.startsWith("// NAV:")) { |
| 230 |
let navItem = document.createElement("li"); |
| 231 |
let navLink = document.createElement("a"); |
| 232 |
navLink.href = "#"; |
| 233 |
navLink.innerText = text.replace("// NAV:", "").trim(); |
| 234 |
navLink.addEventListener("click", function (e) { |
| 235 |
e.preventDefault(); |
| 236 |
|
| 237 |
// Remove existing highlights from the gutter |
| 238 |
phpeditor.codemirror.eachLine(function (l) { |
| 239 |
phpeditor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter'); |
| 240 |
}); |
| 241 |
|
| 242 |
// Set cursor and scroll to line |
| 243 |
phpeditor.codemirror.setCursor(line.lineNo()); |
| 244 |
let heightOfLine = phpeditor.codemirror.defaultTextHeight(); |
| 245 |
let verticalPos = line.lineNo() * heightOfLine; |
| 246 |
phpeditor.codemirror.scrollTo(null, verticalPos); |
| 247 |
|
| 248 |
// Highlight the line number in the gutter |
| 249 |
phpeditor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter'); |
| 250 |
|
| 251 |
phpeditor.codemirror.focus(); |
| 252 |
}); |
| 253 |
navItem.appendChild(navLink); |
| 254 |
phpNavigationMenu.appendChild(navItem); |
| 255 |
} |
| 256 |
}); |
| 257 |
} |
| 258 |
|
| 259 |
const global_php = document.getElementById('wpcoder-global-php'); |
| 260 |
if (global_php) { |
| 261 |
let codemirror_el = { |
| 262 |
mode: { |
| 263 |
name: 'php', |
| 264 |
startOpen: true, |
| 265 |
|
| 266 |
}, |
| 267 |
firstLineNumber: 4, |
| 268 |
}; |
| 269 |
editorSettings.codemirror = _.extend({}, editorSettings.codemirror, codemirror_gen, codemirror_el,); |
| 270 |
const editor = wp.codeEditor.initialize(global_php, editorSettings); |
| 271 |
|
| 272 |
const navigationMenu = document.getElementById("phpNavigationMenu"); |
| 273 |
|
| 274 |
editor.codemirror.eachLine(function (line) { |
| 275 |
const text = line.text; |
| 276 |
if (text.startsWith("// NAV:")) { |
| 277 |
let navItem = document.createElement("li"); |
| 278 |
let navLink = document.createElement("a"); |
| 279 |
navLink.href = "#"; |
| 280 |
navLink.innerText = text.replace("// NAV:", "").trim(); |
| 281 |
navLink.addEventListener("click", function (e) { |
| 282 |
e.preventDefault(); |
| 283 |
|
| 284 |
// Remove existing highlights from the gutter |
| 285 |
editor.codemirror.eachLine(function (l) { |
| 286 |
editor.codemirror.removeLineClass(l, 'gutter', 'highlighted-gutter'); |
| 287 |
}); |
| 288 |
|
| 289 |
// Set cursor and scroll to line |
| 290 |
editor.codemirror.setCursor(line.lineNo()); |
| 291 |
let heightOfLine = editor.codemirror.defaultTextHeight(); |
| 292 |
let verticalPos = line.lineNo() * heightOfLine; |
| 293 |
editor.codemirror.scrollTo(null, verticalPos); |
| 294 |
|
| 295 |
// Highlight the line number in the gutter |
| 296 |
editor.codemirror.addLineClass(line.lineNo(), 'gutter', 'highlighted-gutter'); |
| 297 |
|
| 298 |
editor.codemirror.focus(); |
| 299 |
}); |
| 300 |
navItem.appendChild(navLink); |
| 301 |
navigationMenu.appendChild(navItem); |
| 302 |
} |
| 303 |
}); |
| 304 |
|
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
// Live Preview |
| 310 |
let timeout; |
| 311 |
const previewCheckbox = document.getElementById('checkbox_preview'); |
| 312 |
|
| 313 |
function initPreviewListener() { |
| 314 |
document.querySelectorAll('.wowp-settings__page-content').forEach(editor => { |
| 315 |
editor.addEventListener('keydown', () => { |
| 316 |
clearTimeout(timeout); |
| 317 |
if (previewCheckbox.checked) { |
| 318 |
timeout = setTimeout(updatePreview, 500); |
| 319 |
} |
| 320 |
}); |
| 321 |
}); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
if (previewCheckbox) { |
| 326 |
updatePreview(); |
| 327 |
|
| 328 |
previewCheckbox.addEventListener('change', () => { |
| 329 |
if (previewCheckbox.checked) { |
| 330 |
updatePreview(); |
| 331 |
} |
| 332 |
}); |
| 333 |
} |
| 334 |
|
| 335 |
initPreviewListener(); |
| 336 |
|
| 337 |
function updatePreview() { |
| 338 |
const html = htmleditor.codemirror.getValue(); |
| 339 |
const css = csseditor.codemirror.getValue(); |
| 340 |
|
| 341 |
let styleLinks = []; |
| 342 |
|
| 343 |
document.querySelectorAll('input[id*="include_file_"]').forEach(input => { |
| 344 |
const url = input.value.trim(); |
| 345 |
|
| 346 |
if (url.endsWith('.css')) { |
| 347 |
styleLinks.push(`<link rel="stylesheet" href="${url}">`); |
| 348 |
} |
| 349 |
}); |
| 350 |
|
| 351 |
const iframe = document.getElementById('wowp-preview'); |
| 352 |
iframe.setAttribute("srcdoc", ` |
| 353 |
<!DOCTYPE html> |
| 354 |
<html> |
| 355 |
<head> |
| 356 |
<style>body{margin:0;padding:0;box-sizing:border-box;}</style> |
| 357 |
${styleLinks.join('\n')} |
| 358 |
<style>${css}</style> |
| 359 |
</head> |
| 360 |
<body>${html}</body> |
| 361 |
</html> |
| 362 |
`); |
| 363 |
} |
| 364 |
|
| 365 |
jQuery('.wowp-preview__dot.is-toggle').on('click', function () { |
| 366 |
jQuery(this).toggleClass('is-active'); |
| 367 |
jQuery(this).find('.dashicons').toggleClass('is-hidden'); |
| 368 |
jQuery('.wowp-preview__iframe #wowp-preview').toggleClass('is-hidden'); |
| 369 |
jQuery('.wowp-preview__iframe').toggleClass('is-toggled'); |
| 370 |
}); |
| 371 |
|
| 372 |
jQuery('.wowp-preview__dot.is-reset').on('click', function () { |
| 373 |
jQuery('.wowp-preview__iframe').removeAttr('style'); |
| 374 |
updatePreview(); |
| 375 |
}); |
| 376 |
|
| 377 |
const iframe = jQuery('.wowp-preview__iframe'); |
| 378 |
const $size = iframe.find('.wowp-preview__size'); |
| 379 |
// let timeout; |
| 380 |
|
| 381 |
if (iframe.length && $size.length) { |
| 382 |
const observer = new ResizeObserver(entries => { |
| 383 |
for (const entry of entries) { |
| 384 |
const width = Math.round(entry.contentRect.width); |
| 385 |
const height = Math.round(entry.contentRect.height) - 50; |
| 386 |
|
| 387 |
$size.text(`${width}px × ${height}px`).fadeIn(150); |
| 388 |
|
| 389 |
clearTimeout(timeout); |
| 390 |
// timeout = setTimeout(() => $size.fadeOut(300), 3000); |
| 391 |
} |
| 392 |
}); |
| 393 |
|
| 394 |
observer.observe(iframe[0]); |
| 395 |
} |
| 396 |
|
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
const changeTemplate = function () { |
| 401 |
|
| 402 |
const template = document.getElementById('template'); |
| 403 |
if (!template) { |
| 404 |
return false; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
template.addEventListener('change', function (event) { |
| 409 |
|
| 410 |
const parent = document.getElementById('wpcoder-template'); |
| 411 |
let type = template.value; |
| 412 |
type = default_custom_post(type); |
| 413 |
const elements = parent.querySelectorAll('.wowp-field'); |
| 414 |
|
| 415 |
switch (type) { |
| 416 |
case 'post_all': |
| 417 |
case 'page_all': |
| 418 |
elements[1].classList.add('is-hidden'); |
| 419 |
elements[2].classList.add('is-hidden'); |
| 420 |
elements[3].classList.add('is-hidden'); |
| 421 |
break; |
| 422 |
case 'post_selected': |
| 423 |
case 'post_in_category': |
| 424 |
case 'page_selected': |
| 425 |
case 'archive_category': |
| 426 |
case 'archive_tag': |
| 427 |
case 'archive_author': |
| 428 |
elements[1].classList.remove('is-hidden'); |
| 429 |
elements[2].classList.remove('is-hidden'); |
| 430 |
elements[3].classList.add('is-hidden'); |
| 431 |
break; |
| 432 |
case 'page_type': |
| 433 |
elements[1].classList.remove('is-hidden'); |
| 434 |
elements[2].classList.add('is-hidden'); |
| 435 |
elements[3].classList.remove('is-hidden'); |
| 436 |
break; |
| 437 |
default: |
| 438 |
elements[1].classList.add('is-hidden'); |
| 439 |
elements[2].classList.add('is-hidden'); |
| 440 |
elements[3].classList.add('is-hidden'); |
| 441 |
break; |
| 442 |
|
| 443 |
} |
| 444 |
|
| 445 |
}); |
| 446 |
|
| 447 |
function default_custom_post(el) { |
| 448 |
if (el.includes('custom_post_selected')) { |
| 449 |
return 'post_selected'; |
| 450 |
} |
| 451 |
if (el.includes('custom_post_tax')) { |
| 452 |
return 'post_category'; |
| 453 |
} |
| 454 |
if (el.includes('custom_post_all')) { |
| 455 |
return 'post_all'; |
| 456 |
} |
| 457 |
return el; |
| 458 |
} |
| 459 |
|
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
const feature = function () { |
| 464 |
const texts = document.querySelectorAll('.w_card-description'); |
| 465 |
|
| 466 |
if (!texts) { |
| 467 |
return false; |
| 468 |
} |
| 469 |
texts.forEach((el) => { |
| 470 |
el.addEventListener('click', () => { |
| 471 |
el.classList.toggle('is-open'); |
| 472 |
}) |
| 473 |
}) |
| 474 |
|
| 475 |
} |
| 476 |
|
| 477 |
document.addEventListener('DOMContentLoaded', function () { |
| 478 |
|
| 479 |
new codeEditor; |
| 480 |
new changeTemplate; |
| 481 |
new feature; |
| 482 |
|
| 483 |
}) |