-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
143 lines (127 loc) · 5.86 KB
/
main.js
File metadata and controls
143 lines (127 loc) · 5.86 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
const codeEditorElement = document.getElementById('code-editor'); // Get the textarea element
const livePreview = document.getElementById('live-preview');
const lineNumberCheckbox = document.getElementById('lineNumber');
const wordWrapCheckbox = document.getElementById('wordWrap');
const themeSelector = document.getElementById('theme-selector');
const fontStyleSelector = document.getElementById('fontStyle');
const fontSizeInput = document.getElementById('fontSize');
const whitespaceCheckbox = document.getElementById('whitespace');
const tabSizeSelector = document.getElementById('tabSize');
const lintingCheckbox = document.getElementById('linting');
const autoTagCloseCheckbox = document.getElementById('autoTagClose');
// Tab Management Variables
let tabs = [{ name: 'File1', content: '' }]; // Initial tab named File1
let activeTab = tabs[0];
// CodeMirror Initialization
const codeMirror = CodeMirror.fromTextArea(codeEditorElement, {
mode: 'htmlmixed',
theme: themeSelector.value,
lineNumbers: lineNumberCheckbox.checked,
lineWrapping: wordWrapCheckbox.checked,
autoCloseTags: autoTagCloseCheckbox.checked,
autoCloseBrackets: true,
indentUnit: parseInt(tabSizeSelector.value), // Set initial tab size
matchBrackets: true, // Enable bracket matching
foldGutter: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
lint: lintingCheckbox.checked, // Enable linting based on checkbox
});
// Preview Update Function
function updatePreview() {
const content = codeMirror.getValue();
livePreview.contentDocument.body.innerHTML = content;
activeTab.content = content;
setTimeout(updateDimensions, 0);
}
// CodeMirror Change Event Listener
codeMirror.on('change', () => {
updatePreview();
});
lineNumberCheckbox.addEventListener('change', function() {
codeMirror.setOption("lineNumbers", lineNumberCheckbox.checked);
});
wordWrapCheckbox.addEventListener('change', function() {
codeMirror.setOption("lineWrapping", wordWrapCheckbox.checked);
});
themeSelector.addEventListener('change', function() {
const selectedTheme = themeSelector.value;
codeMirror.setOption("theme", selectedTheme);
});
// Font Style and Size
fontStyleSelector.addEventListener('change', function() {
const selectedFontStyle = fontStyleSelector.value;
codeMirror.getWrapperElement().style.fontFamily = selectedFontStyle; // Change font style
});
fontSizeInput.addEventListener('input', function() {
const fontSize = fontSizeInput.value;
codeMirror.getWrapperElement().style.fontSize = `${fontSize}px`; // Change font size
});
// Whitespace Characters
whitespaceCheckbox.addEventListener('change', function() {
codeMirror.setOption("showWhitespace", whitespaceCheckbox.checked); // Show whitespace characters
});
// Tab Size
tabSizeSelector.addEventListener('change', function() {
const tabSize = parseInt(tabSizeSelector.value);
codeMirror.setOption("indentUnit", tabSize); // Update tab size
});
// Linting
lintingCheckbox.addEventListener('change', function() {
codeMirror.setOption("lint", lintingCheckbox.checked); // Enable or disable linting
});
// Auto Tag Close
autoTagCloseCheckbox.addEventListener('change', function() {
codeMirror.setOption("autoCloseTags", autoTagCloseCheckbox.checked); // Enable or disable auto tag close
});
// Commenting functionality
codeMirror.addKeyMap({
'Ctrl-/': function(cm) {
cm.toggleComment(); // Toggle comment on selected lines
},
'Alt-Up': function(cm) { // Move line up
const cursor = cm.getCursor();
const line = cm.getLine(cursor.line);
if (cursor.line > 0) {
cm.replaceRange(line + "\n", { line: cursor.line - 1 }); // Move line up
cm.replaceRange("", { line: cursor.line, ch: 0 }, { line: cursor.line + 1, ch: 0 }); // Remove original
cm.setCursor(cursor.line - 1, cursor.ch); // Move cursor
}
},
'Alt-Down': function(cm) { // Move line down
const cursor = cm.getCursor();
const line = cm.getLine(cursor.line);
if (cursor.line < cm.lineCount() - 1) {
cm.replaceRange("", { line: cursor.line, ch: 0 }, { line: cursor.line + 1, ch: 0 }); // Remove original
cm.replaceRange(line + "\n", { line: cursor.line + 1 }); // Move line down
cm.setCursor(cursor.line + 1, cursor.ch); // Move cursor
}
},
'Shift-Alt-Up': function(cm) { // Copy line up
const cursor = cm.getCursor();
const line = cm.getLine(cursor.line);
if (cursor.line > 0) {
cm.replaceRange(line + "\n", { line: cursor.line - 1 }); // Copy line up
cm.setCursor(cursor.line, cursor.ch); // Keep cursor in the same position
}
},
'Shift-Alt-Down': function(cm) { // Copy line down
const cursor = cm.getCursor();
const line = cm.getLine(cursor.line);
if (cursor.line < cm.lineCount() - 1) {
cm.replaceRange(line + "\n", { line: cursor.line + 1 }); // Copy line down
cm.setCursor(cursor.line + 1, cursor.ch); // Keep cursor in the same position
}
}
});
// Multi-Cursor Functionality (using Ctrl + click)
codeMirror.on('gutterClick', function(cm, line, gutter, event) {
if (event.ctrlKey || event.metaKey) {
const cursor = cm.getCursor();
if (cm.getLine(line) !== undefined) {
cm.addSelection({line: line, ch: 0}, {line: line, ch: cm.getLine(line).length}); // Add new cursor
}
}
});
// Initial Setup
renderTabs();
setActiveTab(activeTab);