Skip to content

Commit 64007a1

Browse files
committed
feat: add source link target selector
1 parent 0af8c06 commit 64007a1

5 files changed

Lines changed: 93 additions & 8 deletions

File tree

web/src/utils/url.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ const mirrorContentRegs = [
4040
/\/aosp\-mirror\/platform_frameworks_base\/[^\/]+(.*)$/g,
4141
];
4242

43+
export const sourceLinkTargetOptions = [
44+
'cs.android.com',
45+
'googlesource',
46+
'githubusercontent',
47+
] as const;
48+
export type SourceLinkTarget = (typeof sourceLinkTargetOptions)[number];
49+
export const DEFAULT_SOURCE_LINK_TARGET: SourceLinkTarget = 'cs.android.com';
50+
51+
export const getGoogleSourceUrl = (filePath: string): string => {
52+
const tagEnd = filePath.indexOf('/');
53+
if (tagEnd < 0) return sourceBaseurl;
54+
const tag = filePath.substring(0, tagEnd);
55+
const sourcePath = filePath.substring(tagEnd);
56+
return `${sourceBaseurl}platform/frameworks/base/+/refs/tags/${tag}${sourcePath}`;
57+
};
58+
4359
export const getMirrorContentUrl = (filePath: string): string => {
4460
for (const [tag, baseUrl] of manualTagMirrors) {
4561
const tagPrefix = `${tag}/`;

web/src/views/home/HomePage.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import DiffConcurrentSelect from './DiffConcurrentSelect.vue';
1010
import DiffResultList from './DiffResultList.vue';
1111
import { skipNextAutoDiffOnReload, useSharedHomeState } from './homeState';
1212
import MinSdkSelect from './MinSdkSelect.vue';
13+
import SourceLinkTargetSelect from './SourceLinkTargetSelect.vue';
1314
1415
const title = document.title;
1516
const {
@@ -139,6 +140,7 @@ const handleClearLocalCache = async () => {
139140
</template>
140141
</MPopconfirm>
141142
<div flex-1></div>
143+
<SourceLinkTargetSelect />
142144
<MinSdkSelect />
143145
<DiffConcurrentSelect />
144146
<a
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script setup lang="ts">
2+
import MOption from '@/components/MOption.vue';
3+
import MSelect from '@/components/MSelect.vue';
4+
import { useSharedHomeState } from './homeState';
5+
6+
const { sourceLinkTarget, sourceLinkTargetOptions } = useSharedHomeState();
7+
</script>
8+
9+
<template>
10+
<MSelect
11+
v-model="sourceLinkTarget"
12+
label="Link"
13+
trigger-min-width="136px"
14+
popover-min-width="136px"
15+
>
16+
<MOption
17+
v-for="target in sourceLinkTargetOptions"
18+
:key="target"
19+
:value="target"
20+
/>
21+
</MSelect>
22+
</template>

web/src/views/home/TagCard.vue

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<script setup lang="ts">
2-
import { getSourceUrlWithLine } from '@/utils/url';
2+
import {
3+
getGoogleSourceUrl,
4+
getMirrorContentUrl,
5+
getSourceUrlWithLine,
6+
} from '@/utils/url';
37
import { computed } from 'vue';
48
import { ANDROID_PREFIX_LEN, useSharedHomeState } from './homeState';
59
@@ -8,7 +12,8 @@ const props = defineProps<{
812
future?: boolean;
913
}>();
1014
11-
const { urlBuilder, getDiffResult, searchFromData } = useSharedHomeState();
15+
const { urlBuilder, getDiffResult, searchFromData, sourceLinkTarget } =
16+
useSharedHomeState();
1217
const diffResult = computed(() => getDiffResult(props.tag));
1318
1419
const title = computed<string | undefined>(() => {
@@ -28,9 +33,9 @@ const title = computed<string | undefined>(() => {
2833
});
2934
const notFound = computed(() => diffResult.value?.notFound);
3035
const sourceUrl = computed<string | undefined>(() => {
31-
const t = urlBuilder.value?.templateUrl;
32-
if (!t) return '';
33-
const u = t[0] + props.tag + t[1];
36+
const builder = urlBuilder.value;
37+
if (!builder) return '';
38+
const t = builder.templateUrl;
3439
const loc = (() => {
3540
if (searchFromData.value.targetKind === 'member') {
3641
return diffResult.value?.members?.[0]?.loc;
@@ -39,6 +44,14 @@ const sourceUrl = computed<string | undefined>(() => {
3944
return diffResult.value?.target?.loc;
4045
}
4146
})();
47+
if (sourceLinkTarget.value === 'googlesource') {
48+
const u = getGoogleSourceUrl(props.tag + builder.filePath);
49+
return loc ? getSourceUrlWithLine(u, loc) : u;
50+
}
51+
if (sourceLinkTarget.value === 'githubusercontent') {
52+
return getMirrorContentUrl(props.tag + builder.filePath);
53+
}
54+
const u = t[0] + props.tag + t[1];
4255
if (loc) {
4356
return getSourceUrlWithLine(u, loc);
4457
}

web/src/views/home/homeState.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import {
88
import { colors, findStructByPath, useEqualComputed, useTask } from '@/utils';
99
import androidVersionList from '@/utils/android.data';
1010
import { emptyArray } from '@/utils/constants';
11-
import { androidApiVersionList, DEFAULT_MIN_SDK, } from '@/utils/constants';
12-
import { getVersionUrlBuilder } from '@/utils/url';
11+
import { androidApiVersionList, DEFAULT_MIN_SDK } from '@/utils/constants';
12+
import {
13+
DEFAULT_SOURCE_LINK_TARGET,
14+
getVersionUrlBuilder,
15+
sourceLinkTargetOptions,
16+
type SourceLinkTarget,
17+
} from '@/utils/url';
1318
import {
1419
createSharedComposable,
1520
useStorage,
@@ -26,6 +31,7 @@ const SKIP_NEXT_AUTO_DIFF_STATE_KEY = '__androidApiDiffSkipNextAutoDiff';
2631
const DIFF_CONCURRENT_COUNT_STORAGE_KEY =
2732
'android-api-diff:diff-concurrent-count:v1';
2833
const MIN_SDK_STORAGE_KEY = 'android-api-diff:min-sdk:v1';
34+
const SOURCE_LINK_TARGET_STORAGE_KEY = 'android-api-diff:source-link-target:v1';
2935
const SEARCH_HISTORY_STORAGE_KEY = 'android-api-diff:search-history:v1';
3036
const DEFAULT_SEARCH_HISTORY = [
3137
'IActivityTaskManager.getTasks',
@@ -42,6 +48,8 @@ export const diffConcurrentCountOptions = Array.from(
4248

4349
export const minSdkOptions = androidApiVersionList;
4450

51+
export { sourceLinkTargetOptions };
52+
4553
const normalizeDiffConcurrentCount = (value: unknown) => {
4654
const count = Number(value);
4755
if (!Number.isInteger(count)) return DEFAULT_DIFF_CONCURRENT_COUNT;
@@ -51,11 +59,21 @@ const normalizeDiffConcurrentCount = (value: unknown) => {
5159
const normalizeMinSdk = (value: unknown) => {
5260
const sdk = Number(value);
5361
if (!Number.isInteger(sdk)) return DEFAULT_MIN_SDK;
54-
const minSdk = minSdkOptions[0] ;
62+
const minSdk = minSdkOptions[0];
5563
const maxSdk = minSdkOptions.at(-1)!;
5664
return Math.min(Math.max(sdk, minSdk), maxSdk);
5765
};
5866

67+
const normalizeSourceLinkTarget = (value: unknown): SourceLinkTarget => {
68+
if (
69+
typeof value === 'string' &&
70+
(sourceLinkTargetOptions as readonly string[]).includes(value)
71+
) {
72+
return value as SourceLinkTarget;
73+
}
74+
return DEFAULT_SOURCE_LINK_TARGET;
75+
};
76+
5977
const normalizeSearchHistory = (value: unknown): string[] => {
6078
if (!Array.isArray(value)) return [];
6179
const seen = new Set<string>();
@@ -118,6 +136,18 @@ export const useSharedHomeState = createSharedComposable(() => {
118136
},
119137
},
120138
);
139+
const sourceLinkTarget = useStorage<SourceLinkTarget>(
140+
SOURCE_LINK_TARGET_STORAGE_KEY,
141+
DEFAULT_SOURCE_LINK_TARGET,
142+
undefined,
143+
{
144+
flush: 'sync',
145+
serializer: {
146+
read: (raw) => normalizeSourceLinkTarget(raw),
147+
write: (value) => normalizeSourceLinkTarget(value),
148+
},
149+
},
150+
);
121151
const searchHistory = useStorage<string[]>(
122152
SEARCH_HISTORY_STORAGE_KEY,
123153
() => [...DEFAULT_SEARCH_HISTORY],
@@ -413,5 +443,7 @@ export const useSharedHomeState = createSharedComposable(() => {
413443
diffConcurrentCountOptions,
414444
minSdk,
415445
minSdkOptions,
446+
sourceLinkTarget,
447+
sourceLinkTargetOptions,
416448
};
417449
});

0 commit comments

Comments
 (0)