-
Notifications
You must be signed in to change notification settings - Fork 0
Run code PrismJS button registration #14
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
Changes from 7 commits
33eb10b
2939c7d
d579268
536289a
8b1aa0c
df6865a
96d22a3
7a18b43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,16 @@ function findSelector(start, selector) { | |
} | ||
} | ||
|
||
function findDemoWrapper(el) { | ||
while(el && el.parentNode) { | ||
if(matches.call(el.parentNode, '.demo_wrapper')) { | ||
var demoWrapper = el.parentNode; | ||
return demoWrapper; | ||
} | ||
el = el.parentNode; | ||
} | ||
} | ||
|
||
function getStylesFromIframe(iframe) { | ||
var styles = iframe.contentDocument.documentElement.querySelectorAll("style"); | ||
var cssText = ""; | ||
|
@@ -100,62 +110,73 @@ function getStylesFromIframe(iframe) { | |
} | ||
|
||
module.exports = function() { | ||
var codepens = document.querySelectorAll('div.codepen'); | ||
//remove the old codepen links | ||
codepens.forEach(function(codepen, i){ | ||
var wrapper = findSelector(codepen, "pre, .demo_wrapper"); | ||
//the CodePen iframe wrapper has ".codepen" class too | ||
if (wrapper) { | ||
wrapper.setAttribute('data-has-run', true); | ||
codepen.parentNode.removeChild(codepen); | ||
} | ||
}); | ||
|
||
document.body.addEventListener("click", function(ev){ | ||
if(matches.call(ev.target, ".codepen")){ | ||
|
||
var el = findSelector(ev.target, "pre, .demo_wrapper"); | ||
if(el && matches.call(el, "pre")) { | ||
var preElement = el; | ||
var codeElement = preElement.querySelector("code"); | ||
var language = codeElement.className.match(languageHTML)[1]; | ||
var text = codeElement.textContent; | ||
|
||
var data = types[language](text); | ||
|
||
if(data.js) { | ||
data.js = data.js.trim(); | ||
} | ||
if(data.html) { | ||
data.html = data.html.trim(); | ||
} | ||
if(data) { | ||
cleanCodePenData(data); | ||
if(window.CREATE_CODE_PEN) { | ||
CREATE_CODE_PEN(data); | ||
} else { | ||
createCodePen(data); | ||
//Register PrismJS "Run" custom button | ||
Prism.plugins.toolbar.registerButton("run-code", function(env) { | ||
var demoWrapper = findDemoWrapper(env.element); | ||
var pre = env.element.parentElement; | ||
var hasRunBtn = demoWrapper ? demoWrapper.getAttribute("data-has-run") : pre.getAttribute("data-has-run"); | ||
//prevent other demos without codepen link to register Run button | ||
if (hasRunBtn) { | ||
var btn = document.createElement("button"); | ||
btn.innerHTML = "Run"; | ||
document.body.addEventListener('click', function (ev) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This event listener only gets added once, right? It doesn’t get added for each button? [I see the event delegation, which is nice, but I want to double check that the listener isn’t added multiple times. If it is, this logic should be moved outside the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @chasenlehara Fixed, thank you! |
||
ev.stopPropagation(); | ||
if (ev.target === btn) { | ||
if (!demoWrapper && matches.call(env.element.parentNode, 'pre')) { | ||
var language = env.language; | ||
var text = env.code; | ||
var data = types[language](text); | ||
if (data.js) { | ||
data.js = data.js.trim(); | ||
} | ||
if (data.html) { | ||
data.html = data.html.trim(); | ||
} | ||
if (data) { | ||
cleanCodePenData(data); | ||
if (window.CREATE_CODE_PEN) { | ||
CREATE_CODE_PEN(data); | ||
} else { | ||
createCodePen(data); | ||
} | ||
} else { | ||
console.warn('Unable to create a codepen for this demo'); | ||
} | ||
} | ||
if (demoWrapper && matches.call(demoWrapper, '.demo_wrapper')) { | ||
var htmlCode = demoWrapper.querySelector('[data-for=html] code'); | ||
var htmlText = htmlCode ? htmlCode.textContent.trim() : ''; | ||
var jsCode = demoWrapper.querySelector('[data-for=js] code'); | ||
var jsText = jsCode ? jsCode.textContent.trim() : ''; | ||
var cssText = getStylesFromIframe(demoWrapper.querySelector('iframe')); | ||
var codePen = { | ||
html: htmlText, | ||
js: jsText, | ||
js_module: true, | ||
editors: '1011', | ||
css: cssText.trim() | ||
}; | ||
cleanCodePenData(codePen); | ||
if (window.CREATE_CODE_PEN) { | ||
CREATE_CODE_PEN(codePen); | ||
} else { | ||
createCodePen(codePen); | ||
} | ||
} | ||
|
||
} else { | ||
console.warn("Unable to create a codepen for this demo"); | ||
} | ||
} | ||
if(el && matches.call(el, ".demo_wrapper")) { | ||
var htmlCode = el.querySelector("[data-for=html] code"); | ||
var htmlText = htmlCode ? htmlCode.textContent.trim() : ""; | ||
|
||
var jsCode = el.querySelector("[data-for=js] code"); | ||
var jsText = jsCode ? jsCode.textContent.trim() : ""; | ||
|
||
var cssText = getStylesFromIframe( el.querySelector("iframe") ); | ||
|
||
var codePen = { | ||
html: htmlText, | ||
js: jsText, | ||
js_module: true, | ||
editors: "1011", | ||
css: cssText.trim() | ||
}; | ||
cleanCodePenData(codePen); | ||
if(window.CREATE_CODE_PEN) { | ||
CREATE_CODE_PEN(codePen); | ||
} else { | ||
createCodePen(codePen); | ||
} | ||
|
||
} | ||
|
||
}); | ||
return btn; | ||
} | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can just return
demoWrapper
instead of assigning it to a variable.