From 2736fec28516a062d4b69a68d566d295f1393b9a Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Fri, 6 Nov 2020 11:12:48 +0100 Subject: [PATCH] refactor: create createElement.ts --- assets/ts/createElement.ts | 34 ++++++++++++++++++++++++++++++++++ assets/ts/main.ts | 12 +++++++++++- assets/ts/search.tsx | 35 ----------------------------------- 3 files changed, 45 insertions(+), 36 deletions(-) create mode 100644 assets/ts/createElement.ts diff --git a/assets/ts/createElement.ts b/assets/ts/createElement.ts new file mode 100644 index 0000000..3a1e85e --- /dev/null +++ b/assets/ts/createElement.ts @@ -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; \ No newline at end of file diff --git a/assets/ts/main.ts b/assets/ts/main.ts index b9164cc..ae6153f 100644 --- a/assets/ts/main.ts +++ b/assets/ts/main.ts @@ -9,6 +9,7 @@ import { createGallery } from "./gallery" import { getColor } from './color'; import menu from './menu'; +import createElement from './createElement'; let Stack = { init: () => { @@ -74,4 +75,13 @@ window.addEventListener('load', () => { }, 0); }) -window.Stack = Stack; \ No newline at end of file + +declare global { + interface Window { + createElement: any; + Stack: any + } +} + +window.Stack = Stack; +window.createElement = createElement; \ No newline at end of file diff --git a/assets/ts/search.tsx b/assets/ts/search.tsx index 46da04f..8be4b7d 100644 --- a/assets/ts/search.tsx +++ b/assets/ts/search.tsx @@ -15,41 +15,6 @@ const searchResultTitle = document.querySelector('.search-result--title') as HTM 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) { return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); }