-
Notifications
You must be signed in to change notification settings - Fork 1
/
codepentabs.user.js
171 lines (145 loc) · 4.21 KB
/
codepentabs.user.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// ==UserScript==
// @name Codepen Tabs
// @version 1.20190518.10
// @description Replaces Codepen.io resizable code editors with editor tabs
// @author rovyko
// @namespace https://github.com/rovyko/codepen-tabs
// @downloadURL https://github.com/rovyko/codepen-tabs/raw/master/codepentabs.user.js
// @updateURL https://github.com/rovyko/codepen-tabs/raw/master/codepentabs.user.js
// @grant none
// @include /https?:\/\/codepen\.io\/(?:[^\/]*\/)?pen/
// ==/UserScript==
/*
Original obtained from https://github.com/rovyko/codepen-tabs/raw/master/codepentabs.user.js
Licensed under the GNU General Public License v2.0
https://raw.githubusercontent.com/rovyko/codepen-tabs/master/LICENSE
*/
// configuration
const prefix = "cdpntb";
// query selectors
const qs = {
editorWrapper: ".top-boxes.editor-parent",
editors: ".box",
resizerNode: ".editor-resizer",
dragHandleNode: ".powers-drag-handle",
inputNode: "input",
labelNode: "label",
nameNode: ".box-title-name",
contentNode: `.${prefix}-content`,
}
// add global style
const styler = document.createElement("style");
styler.innerHTML =
`
.${prefix}-noselect {
/* https://stackoverflow.com/a/4407335 */
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none
user-select: none;
}
.${prefix}-wrap {
height: 100%;
width: 100%;
}
.${prefix}-tabs {
position: relative;
min-height: 100%;
clear: both;
}
.${prefix}-tab {
float: left;
}
.${prefix}-tab label {
background: #444857;
padding: 8px 20px;
margin-left: -1px;
position: relative;
top: 6px;
left: 1px;
}
.${prefix}-tab > input[type=radio] {
display: none;
}
.${prefix}-content {
position: absolute;
top: 31px;
left: 0;
background: #1a1b1f;
right: 0;
bottom: 0;
visibility: hidden;
}
.${prefix}-tab > input[type=radio]:checked ~ label {
background: #17181c;
border-bottom: 1px solid #1a1b1f;
z-index: 2;
}
.${prefix}-tab > input[type=radio]:checked ~ .${prefix}-content {
visibility: visible;
}
`;
document.head.appendChild(styler);
// setup tabs template
const tempTab = document.createElement("template");
tempTab.innerHTML =
`
<div class="${prefix}-tab">
<input type="radio" name="${prefix}-tabgroup">
<label class="${prefix}-noselect"></label>
<div class="${prefix}-content"></div>
</div>
`;
// find current nodes
const editorWrapper = document.querySelector(qs.editorWrapper);
const editors = editorWrapper.querySelectorAll(qs.editors);
// remove editor resizers
const resizers = editorWrapper.querySelectorAll(qs.resizerNode);
console.log(editorWrapper, resizers);
for (const resizer of resizers) {
resizer.remove();
}
// remove drag handles
const dragHandles = editorWrapper.querySelectorAll(qs.dragHandleNode);
for (const dragHandle of dragHandles) {
dragHandle.remove();
}
// create and deploy tabs wrapper into editor wrapper
const tabsWrapper = document.createElement("div");
tabsWrapper.className = `${prefix}-wrap`;
editorWrapper.insertBefore(tabsWrapper, editorWrapper.firstChild);
console.log("deployed");
const tabsBox = document.createElement("div");
tabsBox.className = `${prefix}-tabs`;
tabsWrapper.appendChild(tabsBox);
// create a tab for each editor and add to tabs wrapper
for (const [i, editor] of [...editors].entries()) {
// get editor name
const nameNode = editor.querySelector(qs.nameNode);
const name = nameNode.textContent;
// clone the tab template
const cloneTab = document.importNode(tempTab.content, true);
// format the hidden radio input
const inputNode = cloneTab.querySelector(qs.inputNode);
const linkId = `${prefix}-tab-${i}`;
inputNode.id = linkId;
if (i === 0) {
inputNode.setAttribute("checked", "");
}
// format the label tab
const labelNode = cloneTab.querySelector(qs.labelNode);
labelNode.innerHTML = name;
labelNode.setAttribute("for", linkId);
// format the content div
const contentNode = cloneTab.querySelector(qs.contentNode);
editor.style.height = "100%";
// move all children of editor to tab content
// this prevents the content from being vertically resized
while (editor.childNodes.length > 0) {
contentNode.appendChild(editor.childNodes[0]);
}
editor.remove();
tabsBox.appendChild(cloneTab);
}