Skip to content

Commit 2cb3946

Browse files
authored
Make issue suggestion work for all editors (#33340)
And do not handle special keys when the text-expander popup exists
1 parent 46d1e91 commit 2cb3946

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

web_src/js/features/comp/EditorMarkdown.ts

+5
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,13 @@ function handleNewline(textarea: HTMLTextAreaElement, e: Event) {
184184
triggerEditorContentChanged(textarea);
185185
}
186186

187+
function isTextExpanderShown(textarea: HTMLElement): boolean {
188+
return Boolean(textarea.closest('text-expander')?.querySelector('.suggestions'));
189+
}
190+
187191
export function initTextareaMarkdown(textarea) {
188192
textarea.addEventListener('keydown', (e) => {
193+
if (isTextExpanderShown(textarea)) return;
189194
if (e.key === 'Tab' && !e.ctrlKey && !e.metaKey && !e.altKey) {
190195
// use Tab/Shift-Tab to indent/unindent the selected lines
191196
handleIndentSelection(textarea, e);

web_src/js/features/comp/TextExpander.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import {matchEmoji, matchMention, matchIssue} from '../../utils/match.ts';
22
import {emojiString} from '../emoji.ts';
33
import {svg} from '../../svg.ts';
4-
import {parseIssueHref, parseIssueNewHref} from '../../utils.ts';
4+
import {parseIssueHref, parseRepoOwnerPathInfo} from '../../utils.ts';
55
import {createElementFromAttrs, createElementFromHTML} from '../../utils/dom.ts';
66
import {getIssueColor, getIssueIcon} from '../issue.ts';
77
import {debounce} from 'perfect-debounce';
88

99
const debouncedSuggestIssues = debounce((key: string, text: string) => new Promise<{matched:boolean; fragment?: HTMLElement}>(async (resolve) => {
10-
let issuePathInfo = parseIssueHref(window.location.href);
11-
if (!issuePathInfo.ownerName) issuePathInfo = parseIssueNewHref(window.location.href);
10+
const issuePathInfo = parseIssueHref(window.location.href);
11+
if (!issuePathInfo.ownerName) {
12+
const repoOwnerPathInfo = parseRepoOwnerPathInfo(window.location.pathname);
13+
issuePathInfo.ownerName = repoOwnerPathInfo.ownerName;
14+
issuePathInfo.repoName = repoOwnerPathInfo.repoName;
15+
// then no issuePathInfo.indexString here, it is only used to exclude the current issue when "matchIssue"
16+
}
1217
if (!issuePathInfo.ownerName) return resolve({matched: false});
1318

1419
const matches = await matchIssue(issuePathInfo.ownerName, issuePathInfo.repoName, issuePathInfo.indexString, text);

web_src/js/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export type RequestOpts = {
3030
data?: RequestData,
3131
} & RequestInit;
3232

33+
export type RepoOwnerPathInfo = {
34+
ownerName: string,
35+
repoName: string,
36+
}
37+
3338
export type IssuePathInfo = {
3439
ownerName: string,
3540
repoName: string,

web_src/js/utils.test.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
basename, extname, isObject, stripTags, parseIssueHref,
33
parseUrl, translateMonth, translateDay, blobToDataURI,
4-
toAbsoluteUrl, encodeURLEncodedBase64, decodeURLEncodedBase64, isImageFile, isVideoFile, parseIssueNewHref,
4+
toAbsoluteUrl, encodeURLEncodedBase64, decodeURLEncodedBase64, isImageFile, isVideoFile, parseRepoOwnerPathInfo,
55
} from './utils.ts';
66

77
test('basename', () => {
@@ -45,12 +45,14 @@ test('parseIssueHref', () => {
4545
expect(parseIssueHref('')).toEqual({ownerName: undefined, repoName: undefined, type: undefined, index: undefined});
4646
});
4747

48-
test('parseIssueNewHref', () => {
49-
expect(parseIssueNewHref('/owner/repo/issues/new')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues'});
50-
expect(parseIssueNewHref('/owner/repo/issues/new?query')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues'});
51-
expect(parseIssueNewHref('/sub/owner/repo/issues/new#hash')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues'});
52-
expect(parseIssueNewHref('/sub/owner/repo/compare/feature/branch-1...fix/branch-2')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'pulls'});
53-
expect(parseIssueNewHref('/other')).toEqual({});
48+
test('parseRepoOwnerPathInfo', () => {
49+
expect(parseRepoOwnerPathInfo('/owner/repo/issues/new')).toEqual({ownerName: 'owner', repoName: 'repo'});
50+
expect(parseRepoOwnerPathInfo('/owner/repo/releases')).toEqual({ownerName: 'owner', repoName: 'repo'});
51+
expect(parseRepoOwnerPathInfo('/other')).toEqual({});
52+
window.config.appSubUrl = '/sub';
53+
expect(parseRepoOwnerPathInfo('/sub/owner/repo/issues/new')).toEqual({ownerName: 'owner', repoName: 'repo'});
54+
expect(parseRepoOwnerPathInfo('/sub/owner/repo/compare/feature/branch-1...fix/branch-2')).toEqual({ownerName: 'owner', repoName: 'repo'});
55+
window.config.appSubUrl = '';
5456
});
5557

5658
test('parseUrl', () => {

web_src/js/utils.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {decode, encode} from 'uint8-to-base64';
2-
import type {IssuePageInfo, IssuePathInfo} from './types.ts';
2+
import type {IssuePageInfo, IssuePathInfo, RepoOwnerPathInfo} from './types.ts';
33

44
// transform /path/to/file.ext to file.ext
55
export function basename(path: string): string {
@@ -32,16 +32,17 @@ export function stripTags(text: string): string {
3232
}
3333

3434
export function parseIssueHref(href: string): IssuePathInfo {
35+
// FIXME: it should use pathname and trim the appSubUrl ahead
3536
const path = (href || '').replace(/[#?].*$/, '');
3637
const [_, ownerName, repoName, pathType, indexString] = /([^/]+)\/([^/]+)\/(issues|pulls)\/([0-9]+)/.exec(path) || [];
3738
return {ownerName, repoName, pathType, indexString};
3839
}
3940

40-
export function parseIssueNewHref(href: string): IssuePathInfo {
41-
const path = (href || '').replace(/[#?].*$/, '');
42-
const [_, ownerName, repoName, pathTypeField] = /([^/]+)\/([^/]+)\/(issues\/new|compare\/.+\.\.\.)/.exec(path) || [];
43-
const pathType = pathTypeField ? (pathTypeField.startsWith('issues/new') ? 'issues' : 'pulls') : undefined;
44-
return {ownerName, repoName, pathType};
41+
export function parseRepoOwnerPathInfo(pathname: string): RepoOwnerPathInfo {
42+
const appSubUrl = window.config.appSubUrl;
43+
if (appSubUrl && pathname.startsWith(appSubUrl)) pathname = pathname.substring(appSubUrl.length);
44+
const [_, ownerName, repoName] = /([^/]+)\/([^/]+)/.exec(pathname) || [];
45+
return {ownerName, repoName};
4546
}
4647

4748
export function parseIssuePageInfo(): IssuePageInfo {

0 commit comments

Comments
 (0)