-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (116 loc) · 5.54 KB
/
script.js
File metadata and controls
147 lines (116 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Initialize CodeMirror instances for each editor
var htmlEditor = CodeMirror.fromTextArea(document.getElementById('htmlCode'), {
mode: 'htmlmixed',
theme: 'darcula',
lineWrapping: true,
autoCloseBrackets: true,
lineNumbers: true,
autoCloseTags: true
});
var cssEditor = CodeMirror.fromTextArea(document.getElementById('cssCode'), {
mode: 'css',
theme: 'darcula',
lineWrapping: true,
autoCloseBrackets: true,
lineNumbers: true
});
var jsEditor = CodeMirror.fromTextArea(document.getElementById('jsCode'), {
mode: 'javascript',
theme: 'darcula',
lineWrapping: true,
autoCloseBrackets: true,
lineNumbers: true
});
htmlEditor.setSize(440, 300);
cssEditor.setSize(440, 300);
jsEditor.setSize(440, 300);
htmlEditor.on("change", (editor, data) => {
updateOutput();
});
cssEditor.on("change", (editor, data) => {
updateOutput();
});
jsEditor.on("change", (editor, data) => {
updateOutput();
});
function updateOutput() {
let htmlCode = htmlEditor.getValue();
let cssCode = "<style>" + cssEditor.getValue(); + "</style>";
let jsCode = jsEditor.getValue();
let outputFrame = document.getElementById('outputFrame');
outputFrame.contentDocument.body.innerHTML = htmlCode + cssCode;
outputFrame.contentWindow.eval(jsCode);
}
const codeSections = document.querySelectorAll('.code-section');
const resizeHandle = document.getElementById('resize-handle');
const outputWrapper = document.getElementById('output-wrapper');
const outputFrame = document.getElementById('outputFrame');
const scroll = document.querySelector('.CodeMirror-vscrollbar');
let isResizing = false;
resizeHandle.addEventListener('mousedown', handleMouseDown);
resizeHandle.addEventListener('touchstart', handleTouchStart, { passive: true });
document.addEventListener('mouseup', handleMouseUp);
document.addEventListener('touchend', handleTouchEnd);
function handleMouseDown(event) {
isResizing = true;
document.addEventListener('mousemove', handleMouseMove);
}
function handleTouchStart(event) {
isResizing = true;
document.addEventListener('touchmove', handleTouchMove, { passive: false });
}
function handleMouseMove(event) {
if (isResizing) {
const height = window.innerHeight - event.clientY;
adjustScrollBarVisibility(height);
outputWrapper.style.height = `${height}px`;
outputFrame.style.height = `${height}px`;
}
}
function handleTouchMove(event) {
event.preventDefault();
if (isResizing && event.touches.length > 0) {
const touchY = event.touches[0].clientY;
const height = window.innerHeight - touchY;
adjustScrollBarVisibility(height);
outputWrapper.style.height = `${height}px`;
outputFrame.style.height = `${height}px`;
}
}
function handleMouseUp() {
isResizing = false;
document.removeEventListener('mousemove', handleMouseMove);
}
function handleTouchEnd() {
isResizing = false;
document.removeEventListener('touchmove', handleTouchMove);
}
function adjustScrollBarVisibility(height) {
// Loop through all CodeMirror instances and adjust scroll bar visibility
codeSections.forEach(function (codeSection) {
const scroll = codeSection.querySelector('.CodeMirror-vscrollbar');
if (height > 395) {
scroll.style.display = 'none';
} else {
scroll.style.display = 'block';
}
});
}
function toggleCodeSection(sectionId) {
const sections = document.querySelectorAll('.code-section');
const links = document.querySelectorAll('.nav-link');
sections.forEach(section => {
if (section.id === sectionId) {
section.style.display = 'block';
} else {
section.style.display = 'none';
}
});
links.forEach(link => {
link.classList.remove('active');
});
event.currentTarget.classList.add('active');
}
$(function () {
$("#output-section").draggable();
});