PluginProbe
HTML Editor for Contact Form 7 / 0.2
HTML Editor for Contact Form 7 v0.2
trunk 0.1 0.2 1.0 1.0.1
cf7-coder / assets / script.js

script.js in HTML Editor for Contact Form 7 0.2, at assets/script.js

105 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 'use strict';
2 (function ($) {
3 $(function () {
4 const checkboxSelector = '#wpcf7_codemiror_dark';
5 const textareaId = 'wpcf7-form';
6 const $textarea = $('#' + textareaId);
7 const editorSettings = wp.codeEditor.defaultSettings ? _.clone(wp.codeEditor.defaultSettings) : {};
8
9 const codemirrorGen = {
10 indentUnit: 4,
11 indentWithTabs: true,
12 inputStyle: 'contenteditable',
13 lineNumbers: true,
14 lineWrapping: true,
15 matchBrackets: true,
16 styleActiveLine: true,
17 continueComments: true,
18 extraKeys: {
19 'Ctrl-Space': 'autocomplete',
20 'Ctrl-/': 'toggleComment',
21 'Cmd-/': 'toggleComment',
22 'Alt-F': 'findPersistent',
23 'Ctrl-F': 'findPersistent',
24 'Cmd-F': 'findPersistent',
25 'Ctrl-D': function (cm) {
26 cm.execCommand('duplicateLine');
27 },
28 'Cmd-D': function (cm) {
29 cm.execCommand('duplicateLine');
30 },
31 },
32 direction: 'ltr',
33 gutters: ['CodeMirror-lint-markers', 'CodeMirror-linenumbers'],
34 mode: 'htmlmixed',
35 lint: true,
36 autoCloseBrackets: true,
37 autoCloseTags: true,
38 matchTags: { bothTags: true },
39 tabSize: 2,
40 };
41
42 let editorHTML = null;
43
44 // Функція ініціалізації CodeMirror з підтримкою теми
45 function initEditor(isDark) {
46 const finalSettings = Object.assign({}, editorSettings, {
47 codemirror: Object.assign({}, editorSettings.codemirror || {}, codemirrorGen, {
48 theme: isDark ? 'material' : 'default',
49 }),
50 });
51
52 return wp.codeEditor.initialize(textareaId, finalSettings);
53 }
54
55 // Ініціалізуємо редактор, якщо textarea існує
56 if ($textarea.length) {
57 const isDark = $(checkboxSelector).is(':checked');
58 editorHTML = initEditor(isDark);
59
60 editorHTML.codemirror.on('change', function () {
61 document.getElementById(textareaId).value = editorHTML.codemirror.getValue();
62 });
63
64 $('#informationdiv_coder').insertAfter('#informationdiv').show();
65
66 // Зміна теми при кліку на чекбокс
67 $(checkboxSelector).on('change', function () {
68 const newIsDark = $(this).is(':checked');
69 const currentValue = editorHTML.codemirror.getValue();
70
71 editorHTML.codemirror.toTextArea();
72 editorHTML = initEditor(newIsDark);
73 editorHTML.codemirror.setValue(currentValue);
74
75 editorHTML.codemirror.on('change', function () {
76 document.getElementById(textareaId).value = editorHTML.codemirror.getValue();
77 });
78 });
79 }
80
81 // Очікуємо wpcf7.taggen і перевизначаємо insert
82 function waitForWpcf7Taggen(callback, attempt = 0) {
83 if (typeof wpcf7 !== 'undefined' && wpcf7.taggen && typeof wpcf7.taggen.insert === 'function') {
84 callback();
85 } else if (attempt < 20) {
86 setTimeout(() => waitForWpcf7Taggen(callback, attempt + 1), 100);
87 } else {
88 console.warn('wpcf7.taggen.insert не знайдено.');
89 }
90 }
91
92 waitForWpcf7Taggen(() => {
93 const originalInsert = wpcf7.taggen.insert;
94
95 wpcf7.taggen.insert = function (content) {
96 if (editorHTML && editorHTML.codemirror) {
97 const cursor = editorHTML.codemirror.getCursor();
98 editorHTML.codemirror.replaceRange(content, cursor);
99 document.getElementById(textareaId).value = editorHTML.codemirror.getValue();
100 }
101 originalInsert.apply(this, arguments);
102 };
103 });
104 });
105 })(jQuery);