-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from contentstack/EB-1848
Eb 1848: Release Preview Compare utility
- Loading branch information
Showing
11 changed files
with
4,113 additions
and
2,799 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import postRobot from "post-robot"; | ||
import { loadCompareGlobalStyle } from "./styles/compare"; | ||
|
||
const voidElements = new Set([ | ||
"area", | ||
"base", | ||
"br", | ||
"col", | ||
"embed", | ||
"hr", | ||
"img", | ||
"input", | ||
"keygen", | ||
"link", | ||
"meta", | ||
"param", | ||
"source", | ||
"track", | ||
"wbr", | ||
]); | ||
|
||
export function handleWebCompare() { | ||
loadCompareGlobalStyle(); | ||
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 map: Record<string, string> = {}; | ||
for (const element of elements) { | ||
const cslp = element.getAttribute("data-cslp")!; | ||
if ( | ||
element.hasAttributes() && | ||
voidElements.has(element.tagName.toLowerCase()) | ||
) { | ||
let attributes = ""; | ||
for (const attr of element.attributes) { | ||
attributes += `${attr.name} -> ${attr.value}\n`; | ||
} | ||
map[cslp] = attributes; | ||
} else { | ||
map[cslp] = element.innerHTML; | ||
} | ||
} | ||
return map; | ||
}); | ||
|
||
const mergeColors = (className = ".cs-compare--added") => { | ||
const elements = Array.from(document.querySelectorAll(className)); | ||
for (let i = 1; i < elements.length; i++) { | ||
const prev = elements[i - 1]; | ||
const next = elements[i]; | ||
if (prev.nextElementSibling === next) | ||
prev.appendChild(prev.nextSibling!); | ||
} | ||
}; | ||
|
||
postRobot.on("diff-value", async ({ data }) => { | ||
const { diff, type } = data; | ||
const operation = type === "base" ? "removed" : "added"; | ||
const elements = Array.from(document.querySelectorAll("[data-cslp]")); | ||
for (const element of elements) { | ||
const path = element.getAttribute("data-cslp")!; | ||
if (!diff[path]) continue; | ||
|
||
if (voidElements.has(element.tagName.toLowerCase())) { | ||
element.classList.add(`cs-compare__void--${operation}`); | ||
} else { | ||
element.innerHTML = diff[path]; | ||
} | ||
} | ||
|
||
mergeColors(`.cs-compare--${operation}`); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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--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--removed { | ||
background: rgba(214, 36, 0, 0.2); | ||
outline: 2px solid rgba(214, 36, 0, 1); | ||
} | ||
`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters