-
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
33eb10b
Run code PrismJS button registration
cherifGsoul 2939c7d
remove comment in the code
cherifGsoul d579268
DOM queries enhancements
cherifGsoul 536289a
Prevent run button to be registred for every demo
cherifGsoul 8b1aa0c
Check if codepen iframe is selected
cherifGsoul df6865a
Be specific to div when selecting the codepen link
cherifGsoul 96d22a3
use event delegation on run button
cherifGsoul 7a18b43
One delegated event listener on the body
cherifGsoul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -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,71 @@ 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); | ||
//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"; | ||
btn.addEventListener('click', function() { | ||
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. do we need to setup this event handler for every codepen? Could event delegation be used to help peformance? 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. @justinbmeyer updated, thank you! |
||
|
||
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 { | ||
createCodePen(data); | ||
console.warn('Unable to create a codepen for this demo'); | ||
} | ||
|
||
} 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); | ||
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); | ||
} | ||
} | ||
|
||
} | ||
|
||
}); | ||
return btn; | ||
} | ||
}); | ||
}; |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.