Skip to content

Commit

Permalink
Merge pull request #128 from contentstack/EB-1850
Browse files Browse the repository at this point in the history
Eb 1850
  • Loading branch information
RavenColEvol authored May 20, 2024
2 parents ebd942e + ec7c921 commit a07f560
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
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);
}
}

0 comments on commit a07f560

Please sign in to comment.