Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
299 lines (265 loc) · 10.4 KB
/
Copy pathcontent.js
File metadata and controls
299 lines (265 loc) · 10.4 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
(function () {
'use strict';
const ATTR = 'data-better-draft-enhanced';
// Snapshot of each element's inline style before we touch it, so revert()
// can restore GitHub's own styling instead of wiping it.
const originalStyles = new WeakMap();
let enhanceTimeout = null;
// Original margin-right of each enhanced merge row, so repeat alignments can
// reset before measuring instead of compounding.
const originalMarginRight = new WeakMap();
let alignResizeTimeout = null;
// Text nodes we emptied to remove the label, with their original data, so
// revert() can put the text back: [textNode, originalData][]
const emptiedTextNodes = [];
const BTN_STYLES_BASE = {
backgroundColor: '#0969da',
color: '#ffffff',
border: 'none',
borderRadius: '6px',
padding: '4px 12px',
fontSize: '13px',
fontWeight: '500',
lineHeight: '20px',
cursor: 'pointer',
textDecoration: 'none',
verticalAlign: 'middle',
fontFamily: '-apple-system,BlinkMacSystemFont,"Segoe UI","Noto Sans",Helvetica,Arial,sans-serif',
boxShadow: '0 1px 0 rgba(0,0,0,0.1)',
};
// <summary> has a <details-dialog> child — inline-flex would pull it into
// the layout and blow up the button size. Use inline-block instead.
const BTN_STYLES_SUMMARY = { ...BTN_STYLES_BASE, display: 'inline-block' };
const BTN_STYLES_BUTTON = { ...BTN_STYLES_BASE, display: 'inline-flex', alignItems: 'center' };
// ── Helpers ──────────────────────────────────────────────────────────────
function isPullRequestPage() {
return /^\/[^/]+\/[^/]+\/pull\/\d+(\/|$)/.test(window.location.pathname);
}
// Sidebar: <summary> inside #partial-discussion-sidebar
function findSidebarDraftLink() {
const sidebar = document.getElementById('partial-discussion-sidebar');
if (!sidebar) return null;
for (const el of sidebar.querySelectorAll('summary')) {
if (el.textContent.trim().toLowerCase() !== 'convert to draft') continue;
let container = el.parentElement;
for (let i = 0; i < 5 && container && container !== document.body; i++) {
if (container.textContent.toLowerCase().includes('still in progress')) {
return { btn: el, container };
}
container = container.parentElement;
}
// Anchor text not found — don't guess at a container.
return { btn: el, container: null };
}
return null;
}
// Merge-box: <button> with "Convert to draft" text, NOT in sidebar
function findMergeBoxDraftBtn() {
const sidebar = document.getElementById('partial-discussion-sidebar');
for (const el of document.querySelectorAll('button')) {
if (el.textContent.trim().toLowerCase() !== 'convert to draft') continue;
if (sidebar && sidebar.contains(el)) continue;
// Walk up to find the row that holds "Still in progress?"
let container = el.parentElement;
for (let i = 0; i < 5 && container && container !== document.body; i++) {
if (container.textContent.toLowerCase().includes('still in progress')) {
return { btn: el, container };
}
container = container.parentElement;
}
// Anchor text not found — don't guess at a container.
return { btn: el, container: null };
}
return null;
}
function rememberStyle(el) {
if (!originalStyles.has(el)) {
originalStyles.set(el, el.getAttribute('style') || '');
}
}
function injectStyles() {
if (document.getElementById('better-draft-style')) return;
const style = document.createElement('style');
style.id = 'better-draft-style';
style.textContent = `
[data-better-draft-enhanced="btn"],
[data-better-draft-enhanced="btn"] *,
[data-better-draft-enhanced="btn"]:hover,
[data-better-draft-enhanced="btn"]:hover *,
[data-better-draft-enhanced="btn"]:focus,
[data-better-draft-enhanced="btn"]:focus * {
color: #ffffff !important;
text-decoration: none !important;
}
[data-better-draft-enhanced="btn"]:hover {
background-color: #0757ba !important;
}
[data-better-draft-enhanced="merge-btn"],
[data-better-draft-enhanced="merge-btn"] *,
[data-better-draft-enhanced="merge-btn"]:hover,
[data-better-draft-enhanced="merge-btn"]:hover * {
color: #ffffff !important;
text-decoration: none !important;
}
[data-better-draft-enhanced="merge-btn"]:hover {
background-color: #0757ba !important;
}
`;
document.head.appendChild(style);
}
// Remove the "Still in progress?" label inside a container, keeping the
// button. The label's element is hidden with display:none (tracked via ATTR
// so revert() restores it); a bare text node is emptied and restored in
// revert(). Hiding rather than removing keeps the label's text present in
// textContent, which the finders use as their anchor on later passes.
function removeStillInProgressLabel(container, btn) {
const walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT);
const matches = [];
let node;
while ((node = walker.nextNode())) {
if (node.data.toLowerCase().includes('still in progress')) matches.push(node);
}
for (const textNode of matches) {
const el = textNode.parentElement;
if (el && el !== container && !el.contains(btn)) {
if (!el.getAttribute(ATTR)) {
rememberStyle(el);
el.setAttribute(ATTR, 'label');
}
el.style.display = 'none';
} else if (!emptiedTextNodes.some(([t]) => t === textNode)) {
emptiedTextNodes.push([textNode, textNode.data]);
textNode.data = '';
}
}
}
function revert() {
for (const [textNode, data] of emptiedTextNodes) {
textNode.data = data;
}
emptiedTextNodes.length = 0;
for (const el of document.querySelectorAll('[' + ATTR + ']')) {
const original = originalStyles.get(el);
if (original === undefined) {
el.removeAttribute('style');
} else if (original === '') {
el.removeAttribute('style');
} else {
el.setAttribute('style', original);
}
el.removeAttribute(ATTR);
}
}
// Shift the row so its trailing edge matches the merge box's trailing edge.
// margin-right moves a block row's border-box edge in BOTH directions
// (padding-right would not), so this works whether the row sits short of
// the box or sticks out past it.
function alignMergeRow(row) {
// A navigation/revert may have happened before this ran.
if (row.getAttribute(ATTR) !== 'merge-row') return;
// Walk up from the row to find a wide preceding sibling — the merge box.
let node = row;
let mergeBox = null;
while (node && node !== document.body && !mergeBox) {
const prev = node.previousElementSibling;
if (prev && prev.getBoundingClientRect().width > 100) mergeBox = prev;
node = node.parentElement;
}
if (!mergeBox) return;
if (!originalMarginRight.has(row)) {
originalMarginRight.set(row, getComputedStyle(row).marginRight);
}
const base = originalMarginRight.get(row);
row.style.marginRight = base; // reset before measuring
const diff = row.getBoundingClientRect().right - mergeBox.getBoundingClientRect().right;
if (diff) {
row.style.marginRight = (parseFloat(base) || 0) + diff + 'px';
}
}
function alignAllMergeRows() {
for (const row of document.querySelectorAll('[' + ATTR + '="merge-row"]')) {
alignMergeRow(row);
}
}
// ── Enhancement ───────────────────────────────────────────────────────────
function enhance() {
if (!isPullRequestPage()) {
revert();
return;
}
injectStyles();
// 1. Sidebar button + its container
const found = findSidebarDraftLink();
if (found) {
const { btn, container } = found;
if (!btn.getAttribute(ATTR)) {
rememberStyle(btn);
btn.setAttribute(ATTR, 'btn');
Object.assign(btn.style, BTN_STYLES_SUMMARY);
if (container) {
rememberStyle(container);
container.setAttribute(ATTR, 'container');
Object.assign(container.style, {
border: 'none',
padding: '0',
marginTop: '8px',
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-end',
gap: '8px',
});
removeStillInProgressLabel(container, btn);
}
}
}
// 2. Merge-box button — style in place and align right edge with merge box
const mergeFound = findMergeBoxDraftBtn();
if (mergeFound) {
const { btn: mergeBtn, container: mergeRow } = mergeFound;
if (!mergeBtn.getAttribute(ATTR)) {
rememberStyle(mergeBtn);
mergeBtn.setAttribute(ATTR, 'merge-btn');
Object.assign(mergeBtn.style, BTN_STYLES_BUTTON);
if (!mergeRow) return;
rememberStyle(mergeRow);
mergeRow.setAttribute(ATTR, 'merge-row');
removeStillInProgressLabel(mergeRow, mergeBtn);
// Two frames so fonts/images have settled before measuring.
requestAnimationFrame(() => requestAnimationFrame(() => alignMergeRow(mergeRow)));
}
}
}
function scheduleEnhance() {
clearTimeout(enhanceTimeout);
enhanceTimeout = setTimeout(enhance, 300);
}
// ── Observers & event listeners ───────────────────────────────────────────
scheduleEnhance();
window.addEventListener('resize', () => {
clearTimeout(alignResizeTimeout);
alignResizeTimeout = setTimeout(alignAllMergeRows, 150);
});
const domObserver = new MutationObserver((mutations) => {
for (const m of mutations) {
if (m.addedNodes.length || m.removedNodes.length) {
scheduleEnhance();
return;
}
}
});
domObserver.observe(document.body, { childList: true, subtree: true });
for (const evt of ['turbo:load', 'turbo:render', 'pjax:end']) {
document.addEventListener(evt, () => {
revert();
scheduleEnhance();
});
}
let lastPath = window.location.pathname;
setInterval(() => {
if (window.location.pathname !== lastPath) {
lastPath = window.location.pathname;
revert();
scheduleEnhance();
}
}, 1000);
}());