Skip to content

Commit 7f9d58f

Browse files
HesterGdelvh
andauthored
Support paste treepath when creating a new file or updating the file name (#23209)
Close #23204 Quick Demo: https://user-images.githubusercontent.com/17645053/222058727-ad30a37c-f0ac-4184-9946-a71fcee473b5.mov --------- Co-authored-by: delvh <[email protected]>
1 parent 79acf7a commit 7f9d58f

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

web_src/js/features/repo-editor.js

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,27 @@ export function initRepoEditor() {
9191
$('#commit-button').text($(this).attr('button_text'));
9292
});
9393

94+
const joinTreePath = ($fileNameEl) => {
95+
const parts = [];
96+
$('.breadcrumb span.section').each(function () {
97+
const element = $(this);
98+
if (element.find('a').length) {
99+
parts.push(element.find('a').text());
100+
} else {
101+
parts.push(element.text());
102+
}
103+
});
104+
if ($fileNameEl.val()) parts.push($fileNameEl.val());
105+
$('#tree_path').val(parts.join('/'));
106+
};
107+
94108
const $editFilename = $('#file-name');
95-
$editFilename.on('keyup', function (e) {
96-
const $section = $('.breadcrumb span.section');
97-
const $divider = $('.breadcrumb div.divider');
98-
let value;
99-
let parts;
109+
$editFilename.on('input', function () {
110+
const parts = $(this).val().split('/');
100111

101-
if (e.keyCode === 8 && getCursorPosition($(this)) === 0 && $section.length > 0) {
102-
value = $section.last().find('a').text();
103-
$(this).val(value + $(this).val());
104-
$(this)[0].setSelectionRange(value.length, value.length);
105-
$section.last().remove();
106-
$divider.last().remove();
107-
}
108-
if (e.keyCode === 191) {
109-
parts = $(this).val().split('/');
112+
if (parts.length > 1) {
110113
for (let i = 0; i < parts.length; ++i) {
111-
value = parts[i];
114+
const value = parts[i];
112115
if (i < parts.length - 1) {
113116
if (value.length) {
114117
$(`<span class="section"><a href="#">${htmlEscape(value)}</a></span>`).insertBefore($(this));
@@ -117,21 +120,28 @@ export function initRepoEditor() {
117120
} else {
118121
$(this).val(value);
119122
}
120-
$(this)[0].setSelectionRange(0, 0);
123+
this.setSelectionRange(0, 0);
121124
}
122125
}
123-
parts = [];
124-
$('.breadcrumb span.section').each(function () {
125-
const element = $(this);
126-
if (element.find('a').length) {
127-
parts.push(element.find('a').text());
128-
} else {
129-
parts.push(element.text());
130-
}
131-
});
132-
if ($(this).val()) parts.push($(this).val());
133-
$('#tree_path').val(parts.join('/'));
134-
}).trigger('keyup');
126+
127+
joinTreePath($(this));
128+
});
129+
130+
$editFilename.on('keydown', function (e) {
131+
const $section = $('.breadcrumb span.section');
132+
133+
// Jump back to last directory once the filename is empty
134+
if (e.code === 'Backspace' && getCursorPosition($(this)) === 0 && $section.length > 0) {
135+
e.preventDefault();
136+
const $divider = $('.breadcrumb div.divider');
137+
const value = $section.last().find('a').text();
138+
$(this).val(value + $(this).val());
139+
this.setSelectionRange(value.length, value.length);
140+
$section.last().remove();
141+
$divider.last().remove();
142+
joinTreePath($(this));
143+
}
144+
});
135145

136146
const $editArea = $('.repository.editor textarea#edit_area');
137147
if (!$editArea.length) return;

0 commit comments

Comments
 (0)