修改动态加载lib

This commit is contained in:
Harvey Zack committed 2018-11-26 11:01:12 +08:00
1 parent 4ffc46a2c4
commit 24e9c67718
5 files changed
+110 -35

No files matched your search

+15 -4
View File
@@ -21,7 +21,7 @@ html, body {
overflow: hidden; overflow: hidden;
} }
.part-left .controls { .part-left .header {
display: flex; display: flex;
align-items: center; align-items: center;
height: 40px; height: 40px;
@@ -31,17 +31,28 @@ html, body {
box-sizing: border-box; box-sizing: border-box;
} }
.part-left .controls .left { .part-left .header .left {
display: flex; display: flex;
align-items: center; align-items: center;
flex: 1; flex: 1;
} }
.part-left .controls .left .logo { .part-left .header .left .logo {
padding-right: 30px; padding-right: 30px;
} }
.part-left .controls .run { .part-left .lib {
height: 50px;
padding: 5px;
outline: none;
resize: none;
border: none;
font-size: 13px;
color: #666;
border-bottom: 1px solid #ddd;
}
.part-left .header .run {
height: 30px; height: 30px;
width: 100px; width: 100px;
} }
View File
Whitespace-only changes.
+3 -7
View File
@@ -19,15 +19,15 @@
<body> <body>
<div class="container"> <div class="container">
<div class="part-left"> <div class="part-left">
<div class="controls"> <div class="header">
<div class="left"> <div class="left">
<a href="./" class="logo">ArtPlayer.js Lab</a> <a href="./" class="logo">ArtPlayer.js Lab</a>
<a href="../docs" class="logo">Github</a>
<a href="../docs" class="logo">Document</a> <a href="../docs" class="logo">Document</a>
<iframe src="https://ghbtns.com/github-btn.html?user=zhw2590582&repo=ArtPlayer&type=star&count=true" frameborder="0" scrolling="0" width="100px" height="20px"></iframe>
<iframe src="https://ghbtns.com/github-btn.html?user=zhw2590582&repo=ArtPlayer&type=fork&count=true" frameborder="0" scrolling="0" width="100px" height="20px"></iframe>
</div> </div>
<input class="run" type="button" value="Run Code"> <input class="run" type="button" value="Run Code">
</div> </div>
<textarea class="lib" cols="30" rows="10" placeholder="Other dependencies, multiple line breaks" spellcheck="false"></textarea>
<div class="code"></div> <div class="code"></div>
</div> </div>
<div class="part-right"> <div class="part-right">
@@ -41,7 +41,6 @@
<script src="./plugins/codemirror.js"></script> <script src="./plugins/codemirror.js"></script>
<script src="./plugins/javascript.js"></script> <script src="./plugins/javascript.js"></script>
<script src="./js/artplayer.js"></script> <script src="./js/artplayer.js"></script>
<script src="./js/artplayer-plugin-subtitle.js"></script>
<script src="./js/common.js"></script> <script src="./js/common.js"></script>
<script> <script>
var _hmt = _hmt || []; var _hmt = _hmt || [];
@@ -52,9 +51,6 @@
s.parentNode.insertBefore(hm, s); s.parentNode.insertBefore(hm, s);
})(); })();
</script> </script>
<script>
loadCode('./js/index.js');
</script>
</body> </body>
</html> </html>
+92 -24
View File
@@ -1,25 +1,26 @@
(function(win) { (function(win) {
var $code = document.querySelector('.code'); var $code = document.querySelector('.code');
var $run = document.querySelector('.run'); var $run = document.querySelector('.run');
var fileCode = ''; var $lib = document.querySelector('.lib');
var defaultLib = './js/artplayer-plugin-subtitle.js';
var loaddLib = [];
consola.creat({ consola.creat({
target: '.console', target: '.console',
size: '100%', size: '100%',
zIndex: 99 zIndex: 99,
}); });
var mirror = CodeMirror($code, { var mirror = CodeMirror($code, {
lineNumbers: true, lineNumbers: true,
mode: 'javascript', mode: 'javascript',
matchBrackets: true, matchBrackets: true,
value: '' value: '',
}); });
function initApp(app) { function initArt(art) {
consola.clean();
Artplayer.config.video.events.forEach(item => { Artplayer.config.video.events.forEach(item => {
app.on('video:' + item, event => { art.on('video:' + item, event => {
console.log('video: ' + event.type); console.log('video: ' + event.type);
}); });
}); });
@@ -27,57 +28,124 @@
function getURLParameters(url) { function getURLParameters(url) {
return (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce( return (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(
(a, v) => ( (a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),
(a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a {},
),
{}
); );
} }
function loadCode(url) { function getExt(url) {
const { code } = getURLParameters(window.location.href); if (url.includes('?')) {
return getExt(url.split('?')[0]);
}
if (url.includes('#')) {
return getExt(url.split('#')[0]);
}
return url
.trim()
.toLowerCase()
.split('.')
.pop();
}
function loadScript(url) {
return new Promise(function(resolve, reject) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.onload = function() {
resolve(url);
};
document.querySelector('head').appendChild(script);
});
}
function loadStyle(url) {
return new Promise(function(resolve) {
var style = document.createElement('style');
style.type = 'text/css';
style.src = url;
document.querySelector('head').appendChild(style);
resolve(url);
});
}
function loadLib(libs = defaultLib) {
var libPromise = [];
libs.split(',')
.filter(function(url) {
return !loaddLib.includes(url);
})
.forEach(function(url) {
var ext = getExt(url);
if (ext === 'js') {
libPromise.push(loadScript(url));
} else if (ext === 'css') {
libPromise.push(loadStyle(url));
}
});
$lib.value = decodeURIComponent(libs);
return Promise.all(libPromise);
}
function loadCode(code) {
return new Promise(function(resolve, reject) {
if (code) { if (code) {
mirror.setValue(decodeURIComponent(code).trim()); mirror.setValue(decodeURIComponent(code).trim());
runCode(); runCode();
resolve(code);
} else { } else {
fetch(url) fetch('./js/example.js')
.then(response => { .then(response => {
return response.text(); return response.text();
}) })
.then(text => { .then(text => {
var code = text.trim(); mirror.setValue(text);
mirror.setValue(code);
runCode(); runCode();
fileCode = code; resolve(text);
return text;
}) })
.catch(err => { .catch(err => {
console.error(err.message); console.error(err.message);
}); });
} }
});
}
function initApp() {
const { code, libs } = getURLParameters(window.location.href);
loadLib(libs)
.then(function(result) {
loaddLib = loaddLib.concat(result);
loadCode(code).then(() => {
console.info('App initialization completed');
});
})
.catch(err => {
console.error(err.message);
});
} }
function runCode() { function runCode() {
Artplayer.instances.forEach(ins => { Artplayer.instances.forEach(art => {
ins.destroy(true); art.destroy(true);
}); });
var code = mirror.getValue(); var code = mirror.getValue();
eval(code); eval(code);
initApp(Artplayer.instances[0]); initArt(Artplayer.instances[0]);
} }
$run.addEventListener('click', function(e) { $run.addEventListener('click', function(e) {
runCode(); runCode();
if (fileCode !== mirror.getValue()) { const libs = encodeURIComponent($lib.value);
const code = encodeURIComponent(mirror.getValue()); const code = encodeURIComponent(mirror.getValue());
const url = window.location.origin + window.location.pathname + '?code=' + code; const url = window.location.origin + window.location.pathname + '?libs=' + libs + '&code=' + code;
history.pushState(null, null, url); history.pushState(null, null, url);
}
}); });
win.addEventListener('error', err => { win.addEventListener('error', err => {
console.error(err.message); console.error(err.message);
}); });
win.loadCode = loadCode; initApp();
})(window); })(window);
File renamed without changes.