Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eb 1850 #128

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/compare.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import postRobot from "post-robot";
import { loadCompareGlobalStyle } from "./styles/compare";
import { registerCompareElement } from "./utils/compare";

const voidElements = new Set([
"area",
Expand All @@ -19,14 +20,19 @@ const voidElements = new Set([
"wbr",
]);

const LEAF_CSLP_SELECTOR = "[data-cslp]:not(:has([data-cslp]))";

export function handleWebCompare() {
loadCompareGlobalStyle();
registerCompareElement();
postRobot.on("send-current-base-route", async () => {
return { url: window.location.href.split("?")[0] };
});

postRobot.on("send-cslp-data", async () => {
const elements = Array.from(document.querySelectorAll("[data-cslp]"));
const elements = Array.from(
document.querySelectorAll(LEAF_CSLP_SELECTOR)
);
const map: Record<string, string> = {};
for (const element of elements) {
const cslp = element.getAttribute("data-cslp")!;
Expand Down Expand Up @@ -59,7 +65,9 @@ export function handleWebCompare() {
postRobot.on("diff-value", async ({ data }) => {
const { diff, type } = data;
const operation = type === "base" ? "removed" : "added";
const elements = Array.from(document.querySelectorAll("[data-cslp]"));
const elements = Array.from(
document.querySelectorAll(LEAF_CSLP_SELECTOR)
);
for (const element of elements) {
const path = element.getAttribute("data-cslp")!;
if (!diff[path]) continue;
Expand Down
32 changes: 17 additions & 15 deletions src/styles/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ import { glob } from "goober";
export const loadCompareGlobalStyle = () => {
const css = glob;
css`
.cs-compare--added {
background: rgba(0, 122, 82, 0.2);
border-bottom: 2px solid rgba(0, 122, 82);
}
cs-compare {
&.cs-compare--added {
background: rgba(0, 122, 82, 0.2);
border-bottom: 2px solid rgba(0, 122, 82);
}

.cs-compare--removed {
background: rgba(214, 36, 0, 0.2);
text-decoration: line-through 2px solid rgba(214, 36, 0, 1);
}
&.cs-compare--removed {
background: rgba(214, 36, 0, 0.2);
text-decoration: line-through 2px solid rgba(214, 36, 0, 1);
}

.cs-compare__void--added {
background: rgba(0, 122, 82, 0.2);
outline: 2px solid rgba(0, 122, 82);
}
&.cs-compare__void--added {
background: rgba(0, 122, 82, 0.2);
outline: 2px solid rgba(0, 122, 82);
}

.cs-compare__void--removed {
background: rgba(214, 36, 0, 0.2);
outline: 2px solid rgba(214, 36, 0, 1);
&.cs-compare__void--removed {
background: rgba(214, 36, 0, 0.2);
outline: 2px solid rgba(214, 36, 0, 1);
}
}
`;
};
13 changes: 13 additions & 0 deletions src/utils/compare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const DIFF_WRAPPER = "cs-compare";

export function registerCompareElement() {
class Compare extends HTMLSpanElement {
constructor() {
super();
}
}

if (!customElements.get(DIFF_WRAPPER)) {
customElements.define(DIFF_WRAPPER, Compare);
}
}
Loading