-
Notifications
You must be signed in to change notification settings - Fork 224
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 #85 from viljamis/feature/static-html
Render static html and show it together with Vue markup
- Loading branch information
Showing
8 changed files
with
190 additions
and
3 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
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,78 @@ | ||
import CodeMirror from "codemirror" | ||
import CodeTabs from "../utils/tabs.js" | ||
|
||
function format(node, level) { | ||
const indentBefore = new Array(level++ + 1).join(" ") | ||
const indentAfter = new Array(level - 1).join(" ") | ||
let textNode | ||
|
||
for (let i = 0; i < node.children.length; i++) { | ||
textNode = document.createTextNode("\n" + indentBefore) | ||
node.insertBefore(textNode, node.children[i]) | ||
|
||
format(node.children[i], level) | ||
|
||
if (node.lastElementChild == node.children[i]) { | ||
textNode = document.createTextNode("\n" + indentAfter) | ||
node.appendChild(textNode) | ||
} | ||
} | ||
|
||
return node | ||
} | ||
|
||
export default previewComponent => { | ||
// https://vuejs.org/v2/guide/render-function.html | ||
return { | ||
render(createElement) { | ||
return createElement(previewComponent) | ||
}, | ||
mounted() { | ||
CodeTabs.clean() | ||
const tabs = CodeTabs.create() | ||
const strDiv = this.$el.innerHTML.replace(/<!---->/g, "").replace(/data-v-\w*=""/g, "") | ||
const div = document.createElement("div") | ||
div.innerHTML = | ||
"<" + | ||
this.$el.localName + | ||
" class='" + | ||
this.$el.className + | ||
"'>" + | ||
strDiv.trim() + | ||
"</" + | ||
this.$el.localName + | ||
">" | ||
|
||
const elemText = format(div, 0).innerHTML.replace(/ class=""/g, "") | ||
const elem = document.createElement("div") | ||
const pre = document.createElement("pre") | ||
const parent = document.querySelector("article div[class^='rsg--tab']") | ||
pre.appendChild(document.createTextNode(elemText.trim())) | ||
elem.appendChild(pre) | ||
if (parent) { | ||
// Allow some time to pass to make sure codemirror is visible first | ||
setTimeout(() => { | ||
parent.appendChild(elem) | ||
parent.appendChild(tabs) | ||
|
||
CodeMirror( | ||
function(code) { | ||
elem.parentNode.replaceChild(code, elem) | ||
code.className += " vueds-html vueds-hidden" | ||
}, | ||
{ | ||
value: pre.innerText || pre.textContent, | ||
mode: "jsx", | ||
lineNumbers: false, | ||
readOnly: true, | ||
dragDrop: false, | ||
theme: "night", | ||
} | ||
) | ||
|
||
CodeTabs.init() | ||
}, 300) | ||
} | ||
}, | ||
} | ||
} |
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,42 @@ | ||
/** | ||
* This is Vue Design System’s helper util for code tabs | ||
*/ | ||
|
||
export default { | ||
clean() { | ||
const oldElem = document.querySelector(".vueds-html") | ||
const oldTabs = document.querySelector(".vueds-tabs") | ||
if (oldElem) { | ||
oldElem.parentNode.removeChild(oldElem) | ||
} | ||
if (oldTabs) { | ||
oldTabs.parentNode.removeChild(oldTabs) | ||
} | ||
}, | ||
create() { | ||
const tabs = document.createElement("div") | ||
tabs.className = "vueds-tabs" | ||
tabs.innerHTML = | ||
"<button class='vueds-tab vue vueds-tab--active'>VUE</button><button class='vueds-tab html'>HTML</button>" | ||
return tabs | ||
}, | ||
init() { | ||
const tabs = document.querySelectorAll(".vueds-tab") | ||
if (tabs) { | ||
tabs.forEach(function(element) { | ||
element.addEventListener("click", event => { | ||
event.preventDefault() | ||
document.querySelector(".vueds-tab--active").classList.remove("vueds-tab--active") | ||
element.classList.add("vueds-tab--active") | ||
document.querySelector(".vueds-hidden").classList.remove("vueds-hidden") | ||
if (event.target.classList.contains("html")) { | ||
const container = document.querySelector("article div[class^='rsg--tab']") | ||
container.querySelector("div").classList.add("vueds-hidden") | ||
} else { | ||
document.querySelector(".vueds-html").classList.add("vueds-hidden") | ||
} | ||
}) | ||
}) | ||
} | ||
}, | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "vue-design-system", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Vue Design System is an open-source tool for building Design Systems with Vue.js", | ||
"author": "viljamis <[email protected]>", | ||
"main": "dist/system/system.js", | ||
|
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