refactor: create createElement.ts
This commit is contained in:
parent
b3df22940d
commit
2736fec285
34
assets/ts/createElement.ts
Normal file
34
assets/ts/createElement.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* createElement
|
||||||
|
* Edited from:
|
||||||
|
* @link https://stackoverflow.com/a/42405694
|
||||||
|
*/
|
||||||
|
function createElement(tag, attrs, children) {
|
||||||
|
var element = document.createElement(tag);
|
||||||
|
|
||||||
|
for (let name in attrs) {
|
||||||
|
if (name && attrs.hasOwnProperty(name)) {
|
||||||
|
let value = attrs[name];
|
||||||
|
|
||||||
|
if (name == "dangerouslySetInnerHTML") {
|
||||||
|
element.innerHTML = value.__html;
|
||||||
|
}
|
||||||
|
else if (value === true) {
|
||||||
|
element.setAttribute(name, name);
|
||||||
|
} else if (value !== false && value != null) {
|
||||||
|
element.setAttribute(name, value.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let i = 2; i < arguments.length; i++) {
|
||||||
|
let child = arguments[i];
|
||||||
|
if (child) {
|
||||||
|
element.appendChild(
|
||||||
|
child.nodeType == null ?
|
||||||
|
document.createTextNode(child.toString()) : child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default createElement;
|
@ -9,6 +9,7 @@
|
|||||||
import { createGallery } from "./gallery"
|
import { createGallery } from "./gallery"
|
||||||
import { getColor } from './color';
|
import { getColor } from './color';
|
||||||
import menu from './menu';
|
import menu from './menu';
|
||||||
|
import createElement from './createElement';
|
||||||
|
|
||||||
let Stack = {
|
let Stack = {
|
||||||
init: () => {
|
init: () => {
|
||||||
@ -74,4 +75,13 @@ window.addEventListener('load', () => {
|
|||||||
}, 0);
|
}, 0);
|
||||||
})
|
})
|
||||||
|
|
||||||
window.Stack = Stack;
|
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
createElement: any;
|
||||||
|
Stack: any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.Stack = Stack;
|
||||||
|
window.createElement = createElement;
|
@ -15,41 +15,6 @@ const searchResultTitle = document.querySelector('.search-result--title') as HTM
|
|||||||
|
|
||||||
let data: pageData[];
|
let data: pageData[];
|
||||||
|
|
||||||
/**
|
|
||||||
* createElement
|
|
||||||
* Edited from:
|
|
||||||
* @link https://stackoverflow.com/a/42405694
|
|
||||||
*/
|
|
||||||
function createElement(tag, attrs, children) {
|
|
||||||
var element = document.createElement(tag);
|
|
||||||
|
|
||||||
for (let name in attrs) {
|
|
||||||
if (name && attrs.hasOwnProperty(name)) {
|
|
||||||
let value = attrs[name];
|
|
||||||
|
|
||||||
if (name == "dangerouslySetInnerHTML") {
|
|
||||||
element.innerHTML = value.__html;
|
|
||||||
}
|
|
||||||
else if (value === true) {
|
|
||||||
element.setAttribute(name, name);
|
|
||||||
} else if (value !== false && value != null) {
|
|
||||||
element.setAttribute(name, value.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (let i = 2; i < arguments.length; i++) {
|
|
||||||
let child = arguments[i];
|
|
||||||
if (child) {
|
|
||||||
element.appendChild(
|
|
||||||
child.nodeType == null ?
|
|
||||||
document.createTextNode(child.toString()) : child);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return element;
|
|
||||||
}
|
|
||||||
|
|
||||||
window.createElement = createElement;
|
|
||||||
|
|
||||||
function escapeRegExp(string) {
|
function escapeRegExp(string) {
|
||||||
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
|
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user