@youtyan/code-viewer 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/web-src/routes.ts +97 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@youtyan/code-viewer",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Local browser-based git diff viewer",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"files": [
|
|
23
23
|
"web",
|
|
24
24
|
"web-src/server",
|
|
25
|
+
"web-src/routes.ts",
|
|
25
26
|
"web-src/types.ts",
|
|
26
27
|
"README.md",
|
|
27
28
|
"LICENSE"
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
export type DiffRange = {
|
|
2
|
+
from: string;
|
|
3
|
+
to: string;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type SourceFileTarget = {
|
|
7
|
+
path: string;
|
|
8
|
+
ref: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type AppRoute =
|
|
12
|
+
| { screen: 'repo'; ref: string; path: string; range: DiffRange }
|
|
13
|
+
| { screen: 'diff'; range: DiffRange }
|
|
14
|
+
| { screen: 'file'; path: string; ref: string; range: DiffRange; view?: 'blob' | 'detail' }
|
|
15
|
+
| { screen: 'unknown'; reason: 'unknown-pathname' | 'missing-path'; rawPathname: string; rawSearch: string; range: DiffRange };
|
|
16
|
+
|
|
17
|
+
export const SPA_PATHS = ['/todif', '/todiff', '/file'] as const;
|
|
18
|
+
export const APP_ENTRY_PATHS = ['/', '/index.html'] as const;
|
|
19
|
+
|
|
20
|
+
export function assertNever(value: never): never {
|
|
21
|
+
throw new Error('unhandled route: ' + JSON.stringify(value));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function parseLegacyRange(value: string | null | undefined, fallback: DiffRange): DiffRange {
|
|
25
|
+
const raw = value || '';
|
|
26
|
+
const sep = raw.indexOf('..');
|
|
27
|
+
if (sep < 0) return fallback;
|
|
28
|
+
return {
|
|
29
|
+
from: raw.slice(0, sep) || fallback.from,
|
|
30
|
+
to: raw.slice(sep + 2) || fallback.to,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function parseRoute(pathname: string, search: string, fallbackRange: DiffRange): AppRoute {
|
|
35
|
+
const params = new URLSearchParams(search);
|
|
36
|
+
const legacyRange = parseLegacyRange(params.get('range'), fallbackRange);
|
|
37
|
+
const range = {
|
|
38
|
+
from: params.get('from') || legacyRange.from,
|
|
39
|
+
to: params.get('to') || legacyRange.to,
|
|
40
|
+
};
|
|
41
|
+
switch (pathname) {
|
|
42
|
+
case '/':
|
|
43
|
+
case '/index.html':
|
|
44
|
+
return {
|
|
45
|
+
screen: 'repo',
|
|
46
|
+
ref: params.get('ref') || params.get('target') || 'worktree',
|
|
47
|
+
path: params.get('path') || '',
|
|
48
|
+
range,
|
|
49
|
+
};
|
|
50
|
+
case '/todif':
|
|
51
|
+
case '/todiff':
|
|
52
|
+
return { screen: 'diff', range };
|
|
53
|
+
case '/file': {
|
|
54
|
+
const path = params.get('path') || '';
|
|
55
|
+
const target = params.get('target') || '';
|
|
56
|
+
const ref = target || params.get('ref') || 'worktree';
|
|
57
|
+
if (!path) return { screen: 'unknown', reason: 'missing-path', rawPathname: pathname, rawSearch: search, range };
|
|
58
|
+
return { screen: 'file', path, ref, range, view: target ? 'blob' : 'detail' };
|
|
59
|
+
}
|
|
60
|
+
default:
|
|
61
|
+
return { screen: 'unknown', reason: 'unknown-pathname', rawPathname: pathname, rawSearch: search, range };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function buildRoute(route: AppRoute): string {
|
|
66
|
+
switch (route.screen) {
|
|
67
|
+
case 'repo': {
|
|
68
|
+
const params = new URLSearchParams();
|
|
69
|
+
if (route.ref && route.ref !== 'worktree') params.set('ref', route.ref);
|
|
70
|
+
if (route.path) params.set('path', route.path);
|
|
71
|
+
const qs = params.toString();
|
|
72
|
+
return '/' + (qs ? '?' + qs : '');
|
|
73
|
+
}
|
|
74
|
+
case 'file':
|
|
75
|
+
if (route.view === 'blob') {
|
|
76
|
+
return '/file?path=' + encodeURIComponent(route.path) +
|
|
77
|
+
'&target=' + encodeURIComponent(route.ref || 'worktree');
|
|
78
|
+
}
|
|
79
|
+
return '/file?path=' + encodeURIComponent(route.path) +
|
|
80
|
+
'&ref=' + encodeURIComponent(route.ref || 'worktree') +
|
|
81
|
+
'&from=' + encodeURIComponent(route.range.from || '') +
|
|
82
|
+
'&to=' + encodeURIComponent(route.range.to || 'worktree');
|
|
83
|
+
case 'diff':
|
|
84
|
+
return '/todif?from=' + encodeURIComponent(route.range.from || '') +
|
|
85
|
+
'&to=' + encodeURIComponent(route.range.to || 'worktree');
|
|
86
|
+
case 'unknown':
|
|
87
|
+
return '/todif?from=' + encodeURIComponent(route.range.from || '') +
|
|
88
|
+
'&to=' + encodeURIComponent(route.range.to || 'worktree');
|
|
89
|
+
default:
|
|
90
|
+
return assertNever(route);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function buildRawFileUrl(target: SourceFileTarget): string {
|
|
95
|
+
return '/_file?path=' + encodeURIComponent(target.path) +
|
|
96
|
+
'&ref=' + encodeURIComponent(target.ref || 'worktree');
|
|
97
|
+
}
|