-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
337 lines (273 loc) · 10.3 KB
/
Copy pathcontent.js
File metadata and controls
337 lines (273 loc) · 10.3 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// State management for resizing
let isResizing = false;
let currentSeparator = null;
let currentContainer = null;
let isVerticalMode = false;
function setVerticalLayout() {
// 找到包含编辑器和PDF的容器 - 使用更精确的选择器
let container = document.querySelector('#ide-redesign-editor-and-pdf-panel [data-panel-group][data-panel-group-direction="horizontal"]');
if (!container) {
// 备用选择器
container = document.querySelector('div[data-panel-group][data-panel-group-id]');
}
if (!container) {
console.log('Container not found');
return;
}
console.log('Container found:', container);
isVerticalMode = true;
// 修改容器为垂直布局
container.style.display = "flex";
container.style.flexDirection = "column";
container.style.height = "100%";
container.style.width = "100%";
container.style.overflow = "hidden";
container.setAttribute('data-panel-group-direction', 'vertical');
// 查找编辑器和PDF面板 - 直接使用ID选择器
const editorPanel = document.getElementById('ide-redesign-editor-panel');
const pdfPanel = document.getElementById('ide-redesign-pdf-panel');
console.log('Editor panel:', editorPanel);
console.log('PDF panel:', pdfPanel);
if (!editorPanel || !pdfPanel) {
console.error('无法找到编辑器或PDF面板');
console.log('Editor panel found:', !!editorPanel);
console.log('PDF panel found:', !!pdfPanel);
return;
}
// 设置编辑器面板样式
editorPanel.style.height = '50%';
editorPanel.style.width = '100%';
editorPanel.style.flex = '0 0 50%';
editorPanel.style.minHeight = '100px';
editorPanel.style.maxHeight = 'none';
editorPanel.style.overflow = 'auto';
editorPanel.style.position = 'relative';
// 设置PDF面板样式
pdfPanel.style.height = '50%';
pdfPanel.style.width = '100%';
pdfPanel.style.flex = '0 0 50%';
pdfPanel.style.minHeight = '100px';
pdfPanel.style.maxHeight = 'none';
pdfPanel.style.overflow = 'auto';
pdfPanel.style.position = 'relative';
const separator = container.querySelector('[role="separator"]');
if (separator) {
const newSeparator = separator.cloneNode(true);
separator.parentNode.replaceChild(newSeparator, separator);
newSeparator.setAttribute('data-resize-handle-state', 'disabled');
newSeparator.setAttribute('data-panel-resize-handle-enabled', 'false');
newSeparator.style.cssText = `
width: 100% !important;
height: 8px !important;
cursor: row-resize !important;
background: #e0e0e0 !important;
position: relative !important;
z-index: 10 !important;
flex-shrink: 0 !important;
transition: background 0.2s !important;
pointer-events: auto !important;
touch-action: auto !important;
user-select: none !important;
`;
newSeparator._mouseenterHandler = () => {
newSeparator.style.background = "#bdbdbd";
};
newSeparator._mouseleaveHandler = () => {
if (!isResizing) newSeparator.style.background = "#e0e0e0";
};
newSeparator.addEventListener('mouseenter', newSeparator._mouseenterHandler);
newSeparator.addEventListener('mouseleave', newSeparator._mouseleaveHandler);
Array.from(newSeparator.children).forEach(child => {
child.style.pointerEvents = 'none';
});
setupResizer(newSeparator, container);
}
// 强制重新渲染PDF面板
if (pdfPanel) {
window.dispatchEvent(new Event('resize'));
setTimeout(() => {
pdfPanel.style.display = 'none';
void pdfPanel.offsetHeight;
pdfPanel.style.display = '';
}, 150);
}
}
function restoreHorizontalLayout() {
let container = document.querySelector('#ide-redesign-editor-and-pdf-panel [data-panel-group][data-panel-group-direction="vertical"]');
if (!container) {
container = document.querySelector('div[data-panel-group][data-panel-group-id]');
}
if (!container) return;
isVerticalMode = false;
container.style.flexDirection = "row";
container.setAttribute('data-panel-group-direction', 'horizontal');
const editorPanel = document.getElementById('ide-redesign-editor-panel');
const pdfPanel = document.getElementById('ide-redesign-pdf-panel');
if (editorPanel) {
editorPanel.style.cssText = "";
}
if (pdfPanel) {
pdfPanel.style.cssText = "";
}
const separator = container.querySelector('[role="separator"]');
if (separator) {
separator.style.cssText = "";
if (separator._mouseenterHandler) {
separator.removeEventListener('mouseenter', separator._mouseenterHandler);
delete separator._mouseenterHandler;
}
if (separator._mouseleaveHandler) {
separator.removeEventListener('mouseleave', separator._mouseleaveHandler);
delete separator._mouseleaveHandler;
}
removeResizer(separator);
}
const pdfPanelForRefresh = document.getElementById('ide-redesign-pdf-panel');
if (pdfPanelForRefresh) {
window.dispatchEvent(new Event('resize'));
setTimeout(() => {
pdfPanelForRefresh.style.display = 'none';
void pdfPanelForRefresh.offsetHeight;
pdfPanelForRefresh.style.display = '';
}, 150);
}
}
function setupResizer(separator, container) {
removeResizer(separator);
separator._resizeHandler = (e) => startResize(e, separator, container);
separator.addEventListener('mousedown', separator._resizeHandler, true);
}
function removeResizer(separator) {
if (separator._resizeHandler) {
separator.removeEventListener('mousedown', separator._resizeHandler, true);
delete separator._resizeHandler;
}
}
function startResize(e, separator, container) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
isResizing = true;
currentSeparator = separator;
currentContainer = container;
separator.style.background = "#9e9e9e";
document.body.style.cssText = `
cursor: row-resize !important;
user-select: none !important;
-webkit-user-select: none !important;
`;
document.documentElement.style.cursor = "row-resize";
const style = document.createElement('style');
style.id = 'overleaf-vertical-cursor-override';
style.textContent = `* { cursor: row-resize !important; }`;
document.head.appendChild(style);
document.addEventListener('mousemove', handleResize, true);
document.addEventListener('mouseup', stopResize, true);
}
function handleResize(e) {
if (!isResizing || !currentContainer) return;
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
const containerRect = currentContainer.getBoundingClientRect();
const idePanel = document.getElementById('ide-redesign-editor-panel');
const pdfPanel = document.getElementById('ide-redesign-pdf-panel');
if (!idePanel || !pdfPanel) return;
const mouseY = e.clientY;
const containerTop = containerRect.top;
const containerHeight = containerRect.height;
let percentage = ((mouseY - containerTop) / containerHeight) * 100;
percentage = Math.max(10, Math.min(90, percentage));
idePanel.style.height = `${percentage}%`;
idePanel.style.flex = "none";
pdfPanel.style.height = `${100 - percentage}%`;
pdfPanel.style.flex = "none";
window.dispatchEvent(new Event('resize'));
}
function stopResize(e) {
if (!isResizing) return;
if (e) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
}
isResizing = false;
document.body.style.cssText = "";
document.documentElement.style.cssText = "";
const cursorOverride = document.getElementById('overleaf-vertical-cursor-override');
if (cursorOverride) cursorOverride.remove();
if (currentSeparator) currentSeparator.style.background = "#e0e0e0";
currentSeparator = null;
currentContainer = null;
document.removeEventListener('mousemove', handleResize, true);
document.removeEventListener('mouseup', stopResize, true);
const pdfPanel = document.getElementById('ide-redesign-pdf-panel');
if (pdfPanel) {
setTimeout(() => {
pdfPanel.style.display = 'none';
void pdfPanel.offsetHeight;
pdfPanel.style.display = '';
}, 50);
}
}
function injectLayoutOption() {
const layoutDropdown = document.querySelector('[aria-labelledby="layout-dropdown-btn"]');
if (!layoutDropdown) return;
if (document.getElementById("vertical-layout-option")) return;
const menuItems = layoutDropdown.querySelectorAll('[role="menuitem"]');
if (menuItems.length === 0) return;
const lastItem = menuItems[menuItems.length - 1];
const verticalOption = document.createElement('a');
verticalOption.setAttribute('role', 'menuitem');
verticalOption.setAttribute('aria-current', 'false');
verticalOption.setAttribute('aria-selected', 'false');
verticalOption.setAttribute('data-rr-ui-dropdown-item', '');
verticalOption.setAttribute('tabindex', '0');
verticalOption.setAttribute('href', '#');
verticalOption.setAttribute('id', 'vertical-layout-option');
verticalOption.className = 'dropdown-item';
verticalOption.innerHTML = `
<span class="material-symbols dropdown-item-leading-icon" aria-hidden="true" translate="no">view_agenda</span>
<div class="d-flex flex-column">
Editor & PDF (Vertical)
</div>
`;
verticalOption.addEventListener('click', (e) => {
e.preventDefault();
setVerticalLayout();
layoutDropdown.querySelectorAll('[role="menuitem"]').forEach(item => {
item.setAttribute('aria-current', 'false');
item.setAttribute('aria-selected', 'false');
});
verticalOption.setAttribute('aria-current', 'true');
verticalOption.setAttribute('aria-selected', 'true');
});
lastItem.parentNode.insertBefore(verticalOption, lastItem.nextSibling);
// Add click listeners to other layout options to restore horizontal layout
menuItems.forEach(item => {
item.addEventListener('click', () => {
if (isVerticalMode) {
restoreHorizontalLayout();
}
}, true);
});
}
let injectionAttempts = 0;
const maxAttempts = 50;
function tryInjectLayoutOption() {
if (injectionAttempts >= maxAttempts) return;
injectionAttempts++;
injectLayoutOption();
if (!document.getElementById("vertical-layout-option")) {
setTimeout(tryInjectLayoutOption, 200);
}
}
window.addEventListener("load", () => {
tryInjectLayoutOption();
const observer = new MutationObserver(() => {
if (!document.getElementById("vertical-layout-option")) {
injectLayoutOption();
}
});
observer.observe(document.body, { childList: true, subtree: true });
});