Skip to content

Commit

Permalink
Fix repeated loading script in dyncLoadScript and add abc audio control.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhuix committed Aug 2, 2024
1 parent 7c3ac49 commit c4a2ee7
Show file tree
Hide file tree
Showing 22 changed files with 563 additions and 115 deletions.
4 changes: 2 additions & 2 deletions docs/dist/showdowns.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/dist/showdowns.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/dist/showdowns.min.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions docs/showdowns-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -855,12 +855,13 @@ Bob-->Alice: I am good thanks!
#### ABC example

```abc
X: 1
T: Cooley's
M: 4/4
Q: 1/4=120
L: 1/8
R: reel
K: Emin
|:D2|EB{c}BA B2 EB|~B2 AB dBAG|FDAD BDAD|FDAD dAFD|
|:{E}D2|EB{c}BA B2 EB|~B2 AB dBAG|FDAD BDAD|FDAD dAFD|
EBBA B2 EB|B2 AB defg|afe^c dBAF|DEFD E2:|
|:gf|eB B2 efge|eB B2 gedB|A2 FA DAFA|A2 FA defg|
eB B2 eBgB|eB B2 defg|afe^c dBAF|DEFD E2:|
Expand Down
5 changes: 3 additions & 2 deletions public/showdowns-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -855,12 +855,13 @@ Bob-->Alice: I am good thanks!
#### ABC example

```abc
X: 1
T: Cooley's
M: 4/4
Q: 1/4=120
L: 1/8
R: reel
K: Emin
|:D2|EB{c}BA B2 EB|~B2 AB dBAG|FDAD BDAD|FDAD dAFD|
|:{E}D2|EB{c}BA B2 EB|~B2 AB dBAG|FDAD BDAD|FDAD dAFD|
EBBA B2 EB|B2 AB defg|afe^c dBAF|DEFD E2:|
|:gf|eB B2 efge|eB B2 gedB|A2 FA DAFA|A2 FA defg|
eB B2 eBgB|eB B2 defg|afe^c dBAF|DEFD E2:|
Expand Down
88 changes: 70 additions & 18 deletions src/extensions/cdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ function getCDN() {
return cdnName;
}

function getName(name) {
if (typeof name === 'object') {
const key = Object.keys(name)[0];
const val = name[key];
if (typeof val === 'string') {
return key + "-" + val;
}
}

return name;
}

function getSrc(name, def) {
if (cdnSrc.hasOwnProperty(cdnName)) {
const cdn = cdnSrc[cdnName];
Expand Down Expand Up @@ -142,51 +154,91 @@ function getSrc(name, def) {
return def;
}

function loadScript(src, name) {
return new Promise((res, rej) => {
if (!src || typeof document === 'undefined') {
rej('Args is invaild!');
function loadScript(name, src) {
return new Promise((resovle, reject) => {
if (!name || typeof document === 'undefined') {
reject('Args is invaild!');
}

if (typeof name === 'undefined') {
name = src;
if (typeof src === 'undefined') {
src = '';
}

src = getSrc(name, src);
if (!src) {
reject(name + ' script source invaild!');
}

const id = 'script-' + getName(name).toLowerCase();
let script = document.getElementById(id);
if (script) {
return resovle(name);
}

const head = document.head || document.getElementsByTagName('head')[0];
const script = document.createElement('script');
script = document.createElement('script');
script.src = src;
script.id = id;
script.onload = () => {
res(name);
resovle(name);
};
head.appendChild(script);
});
}

function loadStyleSheet(css, name) {
if (!css || typeof document === 'undefined') {
function unloadScript(name) {
name = getName(name);
const e = document.getElementById('script-' + name.toLowerCase());
if (e) {
const head = document.head || document.getElementsByTagName('head')[0];
head.removeChild(e);
}
}

function loadStyleSheet(name, css) {
if (!name || typeof document === 'undefined') {
return '';
}

if (typeof name === 'undefined') {
name = css;
if (typeof css === 'undefined') {
css = '';
}

css = getSrc(name, css);
var head = document.head || document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = css;
head.appendChild(link);
if (!css) {
return '';
}

const id = 'css-' + getName(name).toLowerCase();
let script = document.getElementById(id);
if (!script) {
var head = document.head || document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = css;
link.id = id;
head.appendChild(link);
}
return cdnName === 'local' ? '' : css;
}

function unloadStyleSheet(name) {
name = getName(name);
const e = document.getElementById('css-' + name.toLowerCase());
if (e) {
const head = document.head || document.getElementsByTagName('head')[0];
head.removeChild(e);
}
}

const cdnjs = {
setCDN,
getCDN,
getSrc,
loadScript,
loadStyleSheet
unloadScript,
loadStyleSheet,
unloadStyleSheet
};

export default cdnjs;
13 changes: 9 additions & 4 deletions src/extensions/date-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ Date.prototype.Format = function(fmt) {
S: this.getMilliseconds() //毫秒
};
if (!fmt) fmt = 'yyyy-MM-dd hh:mm:ss.S';
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
for (var k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
const matches = /(y+)/.exec(fmt);
if (matches) fmt = fmt.replace(matches[1], (this.getFullYear() + '').substring(4 - matches[1].length));
for (const k in o) {
const m = new RegExp('(' + k + ')').exec(fmt)
if (m) {
let n = o[k];
if (m[1].length == 1) n = ('000' + o[k]).substring(('' + o[k]).length);
else if (m[1].length == 2) n = ('00' + o[k]).substring(('' + o[k]).length);
fmt = fmt.replace(m[1], n);
}
}
return fmt;
Expand Down
14 changes: 14 additions & 0 deletions src/extensions/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2019-present, Jhuix (Hui Jin) <[email protected]>. All rights reserved.
* Use of this source code is governed by a MIT license that can be found in the LICENSE file.
* Description: dynamic loading js libs for local or cdnjs or jsdelivr
*/
'use strict';

import './date-format.js';

function format(s) {
return `${new Date().Format('yyyy-MM-dd HH:mm:ss.S')} ${s}`
}

export default format;
Loading

0 comments on commit c4a2ee7

Please sign in to comment.