@gakr-gakr/diffs 0.1.0
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/README.md +217 -0
- package/api.ts +10 -0
- package/assets/viewer-runtime.js +1888 -0
- package/autobot.plugin.json +218 -0
- package/index.ts +11 -0
- package/package.json +50 -0
- package/runtime-api.ts +1 -0
- package/skills/diffs/SKILL.md +23 -0
- package/src/browser.ts +564 -0
- package/src/config.ts +443 -0
- package/src/http.ts +324 -0
- package/src/language-hints.ts +117 -0
- package/src/pierre-themes.ts +59 -0
- package/src/plugin.ts +73 -0
- package/src/prompt-guidance.ts +7 -0
- package/src/render.ts +557 -0
- package/src/store.ts +387 -0
- package/src/tool.ts +547 -0
- package/src/types.ts +127 -0
- package/src/url.ts +60 -0
- package/src/viewer-assets.ts +103 -0
- package/src/viewer-client.ts +353 -0
- package/src/viewer-payload.ts +94 -0
- package/tsconfig.json +16 -0
package/src/url.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { AutoBotConfig } from "../api.js";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_GATEWAY_PORT = 18789;
|
|
4
|
+
type ViewerBaseUrlFieldName = "baseUrl" | "viewerBaseUrl";
|
|
5
|
+
|
|
6
|
+
export function buildViewerUrl(params: {
|
|
7
|
+
config: AutoBotConfig;
|
|
8
|
+
viewerPath: string;
|
|
9
|
+
baseUrl?: string;
|
|
10
|
+
}): string {
|
|
11
|
+
const baseUrl = params.baseUrl?.trim() || resolveGatewayBaseUrl(params.config);
|
|
12
|
+
const normalizedBase = normalizeViewerBaseUrl(baseUrl);
|
|
13
|
+
const viewerPath = params.viewerPath.startsWith("/")
|
|
14
|
+
? params.viewerPath
|
|
15
|
+
: `/${params.viewerPath}`;
|
|
16
|
+
const parsedBase = new URL(normalizedBase);
|
|
17
|
+
const basePath = parsedBase.pathname === "/" ? "" : parsedBase.pathname.replace(/\/+$/, "");
|
|
18
|
+
parsedBase.pathname = `${basePath}${viewerPath}`;
|
|
19
|
+
parsedBase.search = "";
|
|
20
|
+
parsedBase.hash = "";
|
|
21
|
+
return parsedBase.toString();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function normalizeViewerBaseUrl(
|
|
25
|
+
raw: string,
|
|
26
|
+
fieldName: ViewerBaseUrlFieldName = "baseUrl",
|
|
27
|
+
): string {
|
|
28
|
+
let parsed: URL;
|
|
29
|
+
try {
|
|
30
|
+
parsed = new URL(raw);
|
|
31
|
+
} catch {
|
|
32
|
+
throw new Error(`Invalid ${fieldName}: ${raw}`);
|
|
33
|
+
}
|
|
34
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
35
|
+
throw new Error(`${fieldName} must use http or https: ${raw}`);
|
|
36
|
+
}
|
|
37
|
+
if (parsed.search || parsed.hash) {
|
|
38
|
+
throw new Error(`${fieldName} must not include query/hash: ${raw}`);
|
|
39
|
+
}
|
|
40
|
+
parsed.search = "";
|
|
41
|
+
parsed.hash = "";
|
|
42
|
+
parsed.pathname = parsed.pathname.replace(/\/+$/, "");
|
|
43
|
+
const withoutTrailingSlash = parsed.toString().replace(/\/+$/, "");
|
|
44
|
+
return withoutTrailingSlash;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function resolveGatewayBaseUrl(config: AutoBotConfig): string {
|
|
48
|
+
const scheme = config.gateway?.tls?.enabled ? "https" : "http";
|
|
49
|
+
const port =
|
|
50
|
+
typeof config.gateway?.port === "number" ? config.gateway.port : DEFAULT_GATEWAY_PORT;
|
|
51
|
+
const customHost = config.gateway?.customBindHost?.trim();
|
|
52
|
+
|
|
53
|
+
if (config.gateway?.bind === "custom" && customHost) {
|
|
54
|
+
return `${scheme}://${customHost}:${port}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Viewer links are used by local canvas/clients; default to loopback to avoid
|
|
58
|
+
// container/bridge interfaces that are often unreachable from the caller.
|
|
59
|
+
return `${scheme}://127.0.0.1:${port}`;
|
|
60
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
export const VIEWER_ASSET_PREFIX = "/plugins/diffs/assets/";
|
|
6
|
+
export const VIEWER_LOADER_PATH = `${VIEWER_ASSET_PREFIX}viewer.js`;
|
|
7
|
+
export const VIEWER_RUNTIME_PATH = `${VIEWER_ASSET_PREFIX}viewer-runtime.js`;
|
|
8
|
+
const VIEWER_RUNTIME_RELATIVE_IMPORT_PATH = "./viewer-runtime.js";
|
|
9
|
+
const VIEWER_RUNTIME_CANDIDATE_RELATIVE_PATHS = [
|
|
10
|
+
"./assets/viewer-runtime.js",
|
|
11
|
+
"../assets/viewer-runtime.js",
|
|
12
|
+
] as const;
|
|
13
|
+
|
|
14
|
+
type ServedViewerAsset = {
|
|
15
|
+
body: string | Buffer;
|
|
16
|
+
contentType: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type RuntimeAssetCache = {
|
|
20
|
+
mtimeMs: number;
|
|
21
|
+
runtimeBody: Buffer;
|
|
22
|
+
loaderBody: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
let runtimeAssetCache: RuntimeAssetCache | null = null;
|
|
26
|
+
|
|
27
|
+
type ViewerRuntimeFileUrlParams = {
|
|
28
|
+
baseUrl?: string | URL;
|
|
29
|
+
stat?: (path: string) => Promise<unknown>;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function isMissingFileError(error: unknown): error is NodeJS.ErrnoException {
|
|
33
|
+
return error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function resolveViewerRuntimeFileUrl(
|
|
37
|
+
params: ViewerRuntimeFileUrlParams = {},
|
|
38
|
+
): Promise<URL> {
|
|
39
|
+
const baseUrl = params.baseUrl ?? import.meta.url;
|
|
40
|
+
const stat = params.stat ?? ((path: string) => fs.stat(path));
|
|
41
|
+
let missingFileError: NodeJS.ErrnoException | null = null;
|
|
42
|
+
|
|
43
|
+
for (const relativePath of VIEWER_RUNTIME_CANDIDATE_RELATIVE_PATHS) {
|
|
44
|
+
const candidateUrl = new URL(relativePath, baseUrl);
|
|
45
|
+
try {
|
|
46
|
+
await stat(fileURLToPath(candidateUrl));
|
|
47
|
+
return candidateUrl;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (isMissingFileError(error)) {
|
|
50
|
+
missingFileError = error;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (missingFileError) {
|
|
58
|
+
throw missingFileError;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
throw new Error("viewer runtime asset candidates were not checked");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function getServedViewerAsset(pathname: string): Promise<ServedViewerAsset | null> {
|
|
65
|
+
if (pathname !== VIEWER_LOADER_PATH && pathname !== VIEWER_RUNTIME_PATH) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const assets = await loadViewerAssets();
|
|
70
|
+
if (pathname === VIEWER_LOADER_PATH) {
|
|
71
|
+
return {
|
|
72
|
+
body: assets.loaderBody,
|
|
73
|
+
contentType: "text/javascript; charset=utf-8",
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (pathname === VIEWER_RUNTIME_PATH) {
|
|
78
|
+
return {
|
|
79
|
+
body: assets.runtimeBody,
|
|
80
|
+
contentType: "text/javascript; charset=utf-8",
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function loadViewerAssets(): Promise<RuntimeAssetCache> {
|
|
88
|
+
const runtimeUrl = await resolveViewerRuntimeFileUrl();
|
|
89
|
+
const runtimePath = fileURLToPath(runtimeUrl);
|
|
90
|
+
const runtimeStat = await fs.stat(runtimePath);
|
|
91
|
+
if (runtimeAssetCache && runtimeAssetCache.mtimeMs === runtimeStat.mtimeMs) {
|
|
92
|
+
return runtimeAssetCache;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const runtimeBody = await fs.readFile(runtimePath);
|
|
96
|
+
const hash = crypto.createHash("sha1").update(runtimeBody).digest("hex").slice(0, 12);
|
|
97
|
+
runtimeAssetCache = {
|
|
98
|
+
mtimeMs: runtimeStat.mtimeMs,
|
|
99
|
+
runtimeBody,
|
|
100
|
+
loaderBody: `import "${VIEWER_RUNTIME_RELATIVE_IMPORT_PATH}?v=${hash}";\n`,
|
|
101
|
+
};
|
|
102
|
+
return runtimeAssetCache;
|
|
103
|
+
}
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { FileDiff, preloadHighlighter } from "@pierre/diffs";
|
|
2
|
+
import type {
|
|
3
|
+
FileContents,
|
|
4
|
+
FileDiffMetadata,
|
|
5
|
+
FileDiffOptions,
|
|
6
|
+
SupportedLanguages,
|
|
7
|
+
} from "@pierre/diffs";
|
|
8
|
+
import { normalizeDiffViewerPayloadLanguages } from "./language-hints.js";
|
|
9
|
+
import type { DiffViewerPayload, DiffLayout, DiffTheme } from "./types.js";
|
|
10
|
+
import { parseViewerPayloadJson } from "./viewer-payload.js";
|
|
11
|
+
|
|
12
|
+
type ViewerState = {
|
|
13
|
+
theme: DiffTheme;
|
|
14
|
+
layout: DiffLayout;
|
|
15
|
+
backgroundEnabled: boolean;
|
|
16
|
+
wrapEnabled: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type DiffController = {
|
|
20
|
+
payload: DiffViewerPayload;
|
|
21
|
+
diff: FileDiff;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const controllers: DiffController[] = [];
|
|
25
|
+
|
|
26
|
+
const viewerState: ViewerState = {
|
|
27
|
+
theme: "dark",
|
|
28
|
+
layout: "unified",
|
|
29
|
+
backgroundEnabled: true,
|
|
30
|
+
wrapEnabled: true,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function parsePayload(element: HTMLScriptElement): DiffViewerPayload {
|
|
34
|
+
const raw = element.textContent?.trim();
|
|
35
|
+
if (!raw) {
|
|
36
|
+
throw new Error("Diff payload was empty.");
|
|
37
|
+
}
|
|
38
|
+
return parseViewerPayloadJson(raw);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function getCards(): Array<{ host: HTMLElement; payload: DiffViewerPayload }> {
|
|
42
|
+
const cards: Array<{ host: HTMLElement; payload: DiffViewerPayload }> = [];
|
|
43
|
+
for (const card of document.querySelectorAll<HTMLElement>(".oc-diff-card")) {
|
|
44
|
+
const host = card.querySelector<HTMLElement>("[data-autobot-diff-host]");
|
|
45
|
+
const payloadNode = card.querySelector<HTMLScriptElement>("[data-autobot-diff-payload]");
|
|
46
|
+
if (!host || !payloadNode) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
cards.push({ host, payload: parsePayload(payloadNode) });
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.warn("Skipping invalid diff payload", error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return cards;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function ensureShadowRoot(host: HTMLElement): void {
|
|
60
|
+
if (host.shadowRoot) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const template = host.querySelector<HTMLTemplateElement>(
|
|
64
|
+
":scope > template[shadowrootmode='open']",
|
|
65
|
+
);
|
|
66
|
+
if (!template) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const shadowRoot = host.attachShadow({ mode: "open" });
|
|
70
|
+
shadowRoot.append(template.content.cloneNode(true));
|
|
71
|
+
template.remove();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function getHydrateProps(payload: DiffViewerPayload): {
|
|
75
|
+
fileDiff?: FileDiffMetadata;
|
|
76
|
+
oldFile?: FileContents;
|
|
77
|
+
newFile?: FileContents;
|
|
78
|
+
} {
|
|
79
|
+
if (payload.fileDiff) {
|
|
80
|
+
return { fileDiff: payload.fileDiff };
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
oldFile: payload.oldFile,
|
|
84
|
+
newFile: payload.newFile,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function createToolbarButton(params: {
|
|
89
|
+
title: string;
|
|
90
|
+
active: boolean;
|
|
91
|
+
iconMarkup: string;
|
|
92
|
+
onClick: () => void;
|
|
93
|
+
}): HTMLButtonElement {
|
|
94
|
+
const button = document.createElement("button");
|
|
95
|
+
button.type = "button";
|
|
96
|
+
button.className = "oc-diff-toolbar-button";
|
|
97
|
+
button.dataset.active = String(params.active);
|
|
98
|
+
button.title = params.title;
|
|
99
|
+
button.setAttribute("aria-label", params.title);
|
|
100
|
+
button.innerHTML = params.iconMarkup;
|
|
101
|
+
applyToolbarButtonStyles(button, params.active);
|
|
102
|
+
button.addEventListener("click", (event) => {
|
|
103
|
+
event.preventDefault();
|
|
104
|
+
params.onClick();
|
|
105
|
+
});
|
|
106
|
+
return button;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function applyToolbarStyles(toolbar: HTMLElement): void {
|
|
110
|
+
toolbar.style.display = "inline-flex";
|
|
111
|
+
toolbar.style.alignItems = "center";
|
|
112
|
+
toolbar.style.gap = "6px";
|
|
113
|
+
toolbar.style.marginInlineStart = "6px";
|
|
114
|
+
toolbar.style.flex = "0 0 auto";
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function applyToolbarButtonStyles(button: HTMLButtonElement, active: boolean): void {
|
|
118
|
+
button.style.display = "inline-flex";
|
|
119
|
+
button.style.alignItems = "center";
|
|
120
|
+
button.style.justifyContent = "center";
|
|
121
|
+
button.style.width = "24px";
|
|
122
|
+
button.style.height = "24px";
|
|
123
|
+
button.style.padding = "0";
|
|
124
|
+
button.style.margin = "0";
|
|
125
|
+
button.style.border = "0";
|
|
126
|
+
button.style.borderRadius = "0";
|
|
127
|
+
button.style.background = "transparent";
|
|
128
|
+
button.style.boxShadow = "none";
|
|
129
|
+
button.style.lineHeight = "0";
|
|
130
|
+
button.style.cursor = "pointer";
|
|
131
|
+
button.style.overflow = "visible";
|
|
132
|
+
button.style.flex = "0 0 auto";
|
|
133
|
+
button.style.opacity = active ? "0.92" : "0.6";
|
|
134
|
+
button.style.color =
|
|
135
|
+
viewerState.theme === "dark" ? "rgba(226, 232, 240, 0.74)" : "rgba(15, 23, 42, 0.52)";
|
|
136
|
+
button.dataset.active = String(active);
|
|
137
|
+
const icon = button.querySelector("svg");
|
|
138
|
+
if (!icon) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
icon.style.display = "block";
|
|
142
|
+
icon.style.width = "16px";
|
|
143
|
+
icon.style.height = "16px";
|
|
144
|
+
icon.style.minWidth = "16px";
|
|
145
|
+
icon.style.minHeight = "16px";
|
|
146
|
+
icon.style.overflow = "visible";
|
|
147
|
+
icon.style.flex = "0 0 auto";
|
|
148
|
+
icon.style.color = "inherit";
|
|
149
|
+
icon.style.fill = "currentColor";
|
|
150
|
+
icon.style.pointerEvents = "none";
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function splitIcon(): string {
|
|
154
|
+
return `<svg viewBox="0 0 16 16" aria-hidden="true">
|
|
155
|
+
<path fill="currentColor" d="M14 0H8.5v16H14a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2m-1.5 6.5v1h1a.5.5 0 0 1 0 1h-1v1a.5.5 0 0 1-1 0v-1h-1a.5.5 0 0 1 0-1h1v-1a.5.5 0 0 1 1 0"></path>
|
|
156
|
+
<path fill="currentColor" opacity="0.5" d="M2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h5.5V0zm.5 7.5h3a.5.5 0 0 1 0 1h-3a.5.5 0 0 1 0-1"></path>
|
|
157
|
+
</svg>`;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function unifiedIcon(): string {
|
|
161
|
+
return `<svg viewBox="0 0 16 16" aria-hidden="true">
|
|
162
|
+
<path fill="currentColor" fill-rule="evenodd" d="M16 14a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V8.5h16zm-8-4a.5.5 0 0 0-.5.5v1h-1a.5.5 0 0 0 0 1h1v1a.5.5 0 0 0 1 0v-1h1a.5.5 0 0 0 0-1h-1v-1A.5.5 0 0 0 8 10" clip-rule="evenodd"></path>
|
|
163
|
+
<path fill="currentColor" fill-rule="evenodd" opacity="0.5" d="M14 0a2 2 0 0 1 2 2v5.5H0V2a2 2 0 0 1 2-2zM6.5 3.5a.5.5 0 0 0 0 1h3a.5.5 0 0 0 0-1z" clip-rule="evenodd"></path>
|
|
164
|
+
</svg>`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function wrapIcon(active: boolean): string {
|
|
168
|
+
return `<svg viewBox="0 0 16 16" aria-hidden="true">
|
|
169
|
+
<path fill="currentColor" fill-rule="evenodd" clip-rule="evenodd" opacity="${active ? "1" : "0.85"}" d="M3.868 3.449a1.21 1.21 0 0 0-.473-.329c-.274-.111-.623-.15-1.055-.076a3.5 3.5 0 0 0-.71.208c-.082.035-.16.077-.235.125l-.043.03v1.056l.168-.139c.15-.124.326-.225.527-.303.196-.074.4-.113.604-.113.188 0 .33.051.431.157.087.095.137.248.147.456l-.962.144c-.219.03-.41.086-.57.166a1.245 1.245 0 0 0-.398.311c-.103.125-.181.27-.229.426-.097.33-.093.68.011 1.008a1.096 1.096 0 0 0 .638.67c.155.063.328.093.528.093a1.25 1.25 0 0 0 .978-.441v.345h1.007V4.65c0-.255-.03-.484-.089-.681a1.423 1.423 0 0 0-.275-.52zm-.636 1.896v.236c0 .119-.018.231-.055.341a.745.745 0 0 1-.377.447.694.694 0 0 1-.512.027.454.454 0 0 1-.156-.094.389.389 0 0 1-.094-.139.474.474 0 0 1-.035-.186c0-.077.01-.147.024-.212a.33.33 0 0 1 .078-.141.436.436 0 0 1 .161-.109 1.3 1.3 0 0 1 .305-.073l.661-.097zm5.051-1.067a2.253 2.253 0 0 0-.244-.656 1.354 1.354 0 0 0-.436-.459 1.165 1.165 0 0 0-.642-.173 1.136 1.136 0 0 0-.69.223 1.33 1.33 0 0 0-.264.266V1H5.09v6.224h.918v-.281c.123.152.287.266.472.328.098.032.208.047.33.047.255 0 .483-.06.677-.177.192-.115.355-.278.486-.486a2.29 2.29 0 0 0 .293-.718 3.87 3.87 0 0 0 .096-.886 3.714 3.714 0 0 0-.078-.773zm-.86.758c0 .232-.02.439-.06.613-.036.172-.09.315-.159.424a.639.639 0 0 1-.233.237.582.582 0 0 1-.565.014.683.683 0 0 1-.21-.183.925.925 0 0 1-.142-.283A1.187 1.187 0 0 1 6 5.5v-.517c0-.164.02-.314.06-.447.036-.132.087-.242.156-.336a.668.668 0 0 1 .228-.208.584.584 0 0 1 .29-.071.554.554 0 0 1 .496.279c.063.099.108.214.143.354.031.143.05.306.05.482zM2.407 9.9a.913.913 0 0 1 .316-.239c.218-.1.547-.105.766-.018.104.042.204.1.32.184l.33.26V8.945l-.097-.062a1.932 1.932 0 0 0-.905-.215c-.308 0-.593.057-.846.168-.25.11-.467.27-.647.475-.18.21-.318.453-.403.717-.09.272-.137.57-.137.895 0 .289.043.561.13.808.086.249.211.471.373.652.161.185.361.333.597.441.232.104.493.155.778.155.233 0 .434-.028.613-.084.165-.05.322-.123.466-.217l.078-.061v-.889l-.2.095a.4.4 0 0 1-.076.026c-.05.017-.099.035-.128.049-.036.023-.227.09-.227.09-.06.024-.14.043-.218.059a.977.977 0 0 1-.599-.057.827.827 0 0 1-.306-.225 1.088 1.088 0 0 1-.205-.376 1.728 1.728 0 0 1-.076-.529c0-.21.028-.399.083-.56.054-.158.13-.294.22-.4zM14 6h-4V5h4.5l.5.5v6l-.5.5H7.879l2.07 2.071-.706.707-2.89-2.889v-.707l2.89-2.89L9.95 9l-2 2H14V6z"></path>
|
|
170
|
+
</svg>`;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function backgroundIcon(active: boolean): string {
|
|
174
|
+
if (active) {
|
|
175
|
+
return `<svg viewBox="0 0 16 16" aria-hidden="true">
|
|
176
|
+
<path fill="currentColor" opacity="0.5" d="M0 2.25a.75.75 0 0 1 .75-.75h10.5a.75.75 0 0 1 0 1.5H.75A.75.75 0 0 1 0 2.25"></path>
|
|
177
|
+
<path fill="currentColor" fill-rule="evenodd" d="M15 5a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1H1a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1zM2.5 9a.5.5 0 0 0 0 1h8a.5.5 0 0 0 0-1zm0-2a.5.5 0 0 0 0 1h11a.5.5 0 0 0 0-1z" clip-rule="evenodd"></path>
|
|
178
|
+
<path fill="currentColor" opacity="0.5" d="M0 14.75A.75.75 0 0 1 .75 14h5.5a.75.75 0 0 1 0 1.5H.75a.75.75 0 0 1-.75-.75"></path>
|
|
179
|
+
</svg>`;
|
|
180
|
+
}
|
|
181
|
+
return `<svg viewBox="0 0 16 16" aria-hidden="true">
|
|
182
|
+
<path fill="currentColor" opacity="0.34" d="M0 2.25a.75.75 0 0 1 .75-.75h10.5a.75.75 0 0 1 0 1.5H.75A.75.75 0 0 1 0 2.25"></path>
|
|
183
|
+
<path fill="currentColor" opacity="0.34" fill-rule="evenodd" d="M15 5a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1H1a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1zM2.5 9a.5.5 0 0 0 0 1h8a.5.5 0 0 0 0-1zm0-2a.5.5 0 0 0 0 1h11a.5.5 0 0 0 0-1z" clip-rule="evenodd"></path>
|
|
184
|
+
<path fill="currentColor" opacity="0.34" d="M0 14.75A.75.75 0 0 1 .75 14h5.5a.75.75 0 0 1 0 1.5H.75a.75.75 0 0 1-.75-.75"></path>
|
|
185
|
+
<path d="M2.5 13.5 13.5 2.5" stroke="currentColor" stroke-width="1.35" stroke-linecap="round"></path>
|
|
186
|
+
</svg>`;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function themeIcon(theme: DiffTheme): string {
|
|
190
|
+
if (theme === "dark") {
|
|
191
|
+
return `<svg viewBox="0 0 16 16" aria-hidden="true">
|
|
192
|
+
<path fill="currentColor" d="M10.794 3.647a.217.217 0 0 1 .412 0l.387 1.162c.173.518.58.923 1.097 1.096l1.162.388a.217.217 0 0 1 0 .412l-1.162.386a1.73 1.73 0 0 0-1.097 1.097l-.387 1.162a.217.217 0 0 1-.412 0l-.387-1.162A1.74 1.74 0 0 0 9.31 7.092l-1.162-.386a.217.217 0 0 1 0-.412l1.162-.388a1.73 1.73 0 0 0 1.097-1.096zM13.863.598a.144.144 0 0 1 .221-.071.14.14 0 0 1 .053.07l.258.775c.115.345.386.616.732.731l.774.258a.145.145 0 0 1 0 .274l-.774.259a1.16 1.16 0 0 0-.732.732l-.258.773a.145.145 0 0 1-.274 0l-.258-.773a1.16 1.16 0 0 0-.732-.732l-.774-.259a.145.145 0 0 1 0-.273l.774-.259c.346-.115.617-.386.732-.732z"></path>
|
|
193
|
+
<path fill="currentColor" d="M6.25 1.742a.67.67 0 0 1 .07.75 6.3 6.3 0 0 0-.768 3.028c0 2.746 1.746 5.084 4.193 5.979H1.774A7.2 7.2 0 0 1 1 8.245c0-3.013 1.85-5.598 4.484-6.694a.66.66 0 0 1 .766.19M.75 12.499a.75.75 0 0 0 0 1.5h14.5a.75.75 0 0 0 0-1.5z"></path>
|
|
194
|
+
</svg>`;
|
|
195
|
+
}
|
|
196
|
+
return `<svg viewBox="0 0 16 16" aria-hidden="true">
|
|
197
|
+
<path fill="currentColor" d="M8.21 2.109a.256.256 0 0 0-.42 0L6.534 3.893a.256.256 0 0 1-.316.085l-1.982-.917a.256.256 0 0 0-.362.21l-.196 2.174a.256.256 0 0 1-.232.232l-2.175.196a.256.256 0 0 0-.209.362l.917 1.982a.256.256 0 0 1-.085.316L.11 9.791a.256.256 0 0 0 0 .418L1.23 11H3.1a5 5 0 1 1 9.8 0h1.869l1.123-.79a.256.256 0 0 0 0-.42l-1.785-1.257a.256.256 0 0 1-.085-.316l.917-1.982a.256.256 0 0 0-.21-.362l-2.174-.196a.256.256 0 0 1-.232-.232l-.196-2.175a.256.256 0 0 0-.362-.209l-1.982.917a.256.256 0 0 1-.316-.085z"></path>
|
|
198
|
+
<path fill="currentColor" d="M4 10q.001.519.126 1h7.748A4 4 0 1 0 4 10M.75 12a.75.75 0 0 0 0 1.5h14.5a.75.75 0 0 0 0-1.5z"></path>
|
|
199
|
+
</svg>`;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function createToolbar(): HTMLElement {
|
|
203
|
+
const toolbar = document.createElement("div");
|
|
204
|
+
toolbar.className = "oc-diff-toolbar";
|
|
205
|
+
applyToolbarStyles(toolbar);
|
|
206
|
+
|
|
207
|
+
toolbar.append(
|
|
208
|
+
createToolbarButton({
|
|
209
|
+
title: viewerState.layout === "unified" ? "Switch to split diff" : "Switch to unified diff",
|
|
210
|
+
active: viewerState.layout === "split",
|
|
211
|
+
iconMarkup: viewerState.layout === "split" ? splitIcon() : unifiedIcon(),
|
|
212
|
+
onClick: () => {
|
|
213
|
+
viewerState.layout = viewerState.layout === "unified" ? "split" : "unified";
|
|
214
|
+
syncAllControllers();
|
|
215
|
+
},
|
|
216
|
+
}),
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
toolbar.append(
|
|
220
|
+
createToolbarButton({
|
|
221
|
+
title: viewerState.wrapEnabled ? "Disable word wrap" : "Enable word wrap",
|
|
222
|
+
active: viewerState.wrapEnabled,
|
|
223
|
+
iconMarkup: wrapIcon(viewerState.wrapEnabled),
|
|
224
|
+
onClick: () => {
|
|
225
|
+
viewerState.wrapEnabled = !viewerState.wrapEnabled;
|
|
226
|
+
syncAllControllers();
|
|
227
|
+
},
|
|
228
|
+
}),
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
toolbar.append(
|
|
232
|
+
createToolbarButton({
|
|
233
|
+
title: viewerState.backgroundEnabled
|
|
234
|
+
? "Hide background highlights"
|
|
235
|
+
: "Show background highlights",
|
|
236
|
+
active: viewerState.backgroundEnabled,
|
|
237
|
+
iconMarkup: backgroundIcon(viewerState.backgroundEnabled),
|
|
238
|
+
onClick: () => {
|
|
239
|
+
viewerState.backgroundEnabled = !viewerState.backgroundEnabled;
|
|
240
|
+
syncAllControllers();
|
|
241
|
+
},
|
|
242
|
+
}),
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
toolbar.append(
|
|
246
|
+
createToolbarButton({
|
|
247
|
+
title: viewerState.theme === "dark" ? "Switch to light theme" : "Switch to dark theme",
|
|
248
|
+
active: viewerState.theme === "dark",
|
|
249
|
+
iconMarkup: themeIcon(viewerState.theme),
|
|
250
|
+
onClick: () => {
|
|
251
|
+
viewerState.theme = viewerState.theme === "dark" ? "light" : "dark";
|
|
252
|
+
syncAllControllers();
|
|
253
|
+
},
|
|
254
|
+
}),
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
return toolbar;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function createRenderOptions(payload: DiffViewerPayload): FileDiffOptions<undefined> {
|
|
261
|
+
return {
|
|
262
|
+
theme: payload.options.theme,
|
|
263
|
+
themeType: viewerState.theme,
|
|
264
|
+
diffStyle: viewerState.layout,
|
|
265
|
+
diffIndicators: payload.options.diffIndicators,
|
|
266
|
+
expandUnchanged: payload.options.expandUnchanged,
|
|
267
|
+
overflow: viewerState.wrapEnabled ? "wrap" : "scroll",
|
|
268
|
+
disableLineNumbers: payload.options.disableLineNumbers,
|
|
269
|
+
disableBackground: !viewerState.backgroundEnabled,
|
|
270
|
+
unsafeCSS: payload.options.unsafeCSS,
|
|
271
|
+
renderHeaderMetadata: () => createToolbar(),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function syncDocumentTheme(): void {
|
|
276
|
+
document.body.dataset.theme = viewerState.theme;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function applyState(controller: DiffController): void {
|
|
280
|
+
controller.diff.setOptions(createRenderOptions(controller.payload));
|
|
281
|
+
controller.diff.rerender();
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function syncAllControllers(): void {
|
|
285
|
+
syncDocumentTheme();
|
|
286
|
+
for (const controller of controllers) {
|
|
287
|
+
applyState(controller);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
async function hydrateViewer(): Promise<void> {
|
|
292
|
+
const cards = await Promise.all(
|
|
293
|
+
getCards().map(async ({ host, payload }) => ({
|
|
294
|
+
host,
|
|
295
|
+
payload: await normalizeDiffViewerPayloadLanguages(payload),
|
|
296
|
+
})),
|
|
297
|
+
);
|
|
298
|
+
const langs = new Set<SupportedLanguages>();
|
|
299
|
+
const firstPayload = cards[0]?.payload;
|
|
300
|
+
|
|
301
|
+
if (firstPayload) {
|
|
302
|
+
viewerState.theme = firstPayload.options.themeType;
|
|
303
|
+
viewerState.layout = firstPayload.options.diffStyle;
|
|
304
|
+
viewerState.backgroundEnabled = firstPayload.options.backgroundEnabled;
|
|
305
|
+
viewerState.wrapEnabled = firstPayload.options.overflow === "wrap";
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
for (const { payload } of cards) {
|
|
309
|
+
for (const lang of payload.langs) {
|
|
310
|
+
langs.add(lang);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
await preloadHighlighter({
|
|
315
|
+
themes: ["pierre-light", "pierre-dark"],
|
|
316
|
+
langs: [...langs],
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
syncDocumentTheme();
|
|
320
|
+
|
|
321
|
+
for (const { host, payload } of cards) {
|
|
322
|
+
ensureShadowRoot(host);
|
|
323
|
+
const diff = new FileDiff(createRenderOptions(payload));
|
|
324
|
+
diff.hydrate({
|
|
325
|
+
fileContainer: host,
|
|
326
|
+
prerenderedHTML: payload.prerenderedHTML,
|
|
327
|
+
...getHydrateProps(payload),
|
|
328
|
+
});
|
|
329
|
+
const controller = { payload, diff };
|
|
330
|
+
controllers.push(controller);
|
|
331
|
+
applyState(controller);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async function main(): Promise<void> {
|
|
336
|
+
try {
|
|
337
|
+
await hydrateViewer();
|
|
338
|
+
document.documentElement.dataset.autobotDiffsReady = "true";
|
|
339
|
+
} catch (error) {
|
|
340
|
+
document.documentElement.dataset.autobotDiffsError = "true";
|
|
341
|
+
console.error("Failed to hydrate diff viewer", error);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (typeof document !== "undefined") {
|
|
346
|
+
if (document.readyState === "loading") {
|
|
347
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
348
|
+
void main();
|
|
349
|
+
});
|
|
350
|
+
} else {
|
|
351
|
+
void main();
|
|
352
|
+
}
|
|
353
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { DIFF_INDICATORS, DIFF_LAYOUTS, DIFF_THEMES } from "./types.js";
|
|
2
|
+
import type { DiffViewerPayload } from "./types.js";
|
|
3
|
+
|
|
4
|
+
const OVERFLOW_VALUES = ["scroll", "wrap"] as const;
|
|
5
|
+
|
|
6
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
7
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function parseViewerPayloadJson(raw: string): DiffViewerPayload {
|
|
11
|
+
let parsed: unknown;
|
|
12
|
+
try {
|
|
13
|
+
parsed = JSON.parse(raw);
|
|
14
|
+
} catch {
|
|
15
|
+
throw new Error("Diff payload is not valid JSON.");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (!isDiffViewerPayload(parsed)) {
|
|
19
|
+
throw new Error("Diff payload has invalid shape.");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return parsed;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function isDiffViewerPayload(value: unknown): value is DiffViewerPayload {
|
|
26
|
+
if (!isRecord(value)) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (typeof value.prerenderedHTML !== "string") {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!Array.isArray(value.langs) || !value.langs.every((lang) => typeof lang === "string")) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!isViewerOptions(value.options)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const hasFileDiff = isRecord(value.fileDiff);
|
|
43
|
+
const hasBeforeAfterFiles = isRecord(value.oldFile) && isRecord(value.newFile);
|
|
44
|
+
if (!hasFileDiff && !hasBeforeAfterFiles) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isViewerOptions(value: unknown): boolean {
|
|
52
|
+
if (!isRecord(value)) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!isRecord(value.theme)) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (value.theme.light !== "pierre-light" || value.theme.dark !== "pierre-dark") {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!includesValue(DIFF_LAYOUTS, value.diffStyle)) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
if (!includesValue(DIFF_INDICATORS, value.diffIndicators)) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
if (!includesValue(DIFF_THEMES, value.themeType)) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
if (!includesValue(OVERFLOW_VALUES, value.overflow)) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (typeof value.disableLineNumbers !== "boolean") {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
if (typeof value.expandUnchanged !== "boolean") {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
if (typeof value.backgroundEnabled !== "boolean") {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
if (typeof value.unsafeCSS !== "string") {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function includesValue<T extends readonly string[]>(values: T, value: unknown): value is T[number] {
|
|
93
|
+
return typeof value === "string" && values.includes(value as T[number]);
|
|
94
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../tsconfig.package-boundary.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "."
|
|
5
|
+
},
|
|
6
|
+
"include": ["./*.ts", "./src/**/*.ts"],
|
|
7
|
+
"exclude": [
|
|
8
|
+
"./**/*.test.ts",
|
|
9
|
+
"./dist/**",
|
|
10
|
+
"./node_modules/**",
|
|
11
|
+
"./src/test-support/**",
|
|
12
|
+
"./src/**/*test-helpers.ts",
|
|
13
|
+
"./src/**/*test-harness.ts",
|
|
14
|
+
"./src/**/*test-support.ts"
|
|
15
|
+
]
|
|
16
|
+
}
|