From eb009e88949c834fa1ba5b50e2a7ccdb5e14baf0 Mon Sep 17 00:00:00 2001 From: dy-xiaodong2022 Date: Tue, 21 Nov 2023 20:03:27 +0800 Subject: [PATCH] refactor(utils): update 3 files --- src/main.ts | 2 +- src/utils/index.ts | 5 ++- src/utils/methods.ts | 93 +++++++++++++++++++------------------------- 3 files changed, 43 insertions(+), 57 deletions(-) diff --git a/src/main.ts b/src/main.ts index 4b945156c..fd025c1c0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,7 +7,7 @@ import {FastjsDate} from "./date"; import dom from "./dom"; import {FastjsDom, FastjsDomList} from "./dom"; import utils from "./utils"; -import {rand, copy} from "./utils/methods"; +import {rand, copy} from "./utils"; if (__DEV__) { console.info("You are running fastjs in development mode.\n" + diff --git a/src/utils/index.ts b/src/utils/index.ts index 55d0c4cd4..c23ac6053 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,2 +1,3 @@ -/** @todo Need work */ -export default {} \ No newline at end of file +import utils from "./methods"; +export default utils; +export * from "./methods"; \ No newline at end of file diff --git a/src/utils/methods.ts b/src/utils/methods.ts index 4e315eba5..4f3e44e48 100644 --- a/src/utils/methods.ts +++ b/src/utils/methods.ts @@ -1,67 +1,52 @@ -/** @todo Rewrite, and remove some duplicate code */ +import _dev from "../dev"; -// import _dev from "../dev"; +async function copy(text: string): Promise { + const input = await createDomElement(text); + const selection = selectText(input); + copyToClipboard(selection); + input.remove(); -let fastjs = { - async copy(text: string): Promise { + async function createDomElement(text: string): Promise { const {FastjsDom} = await import("../dom"); - // copy text to clipboard let input = new FastjsDom("span"); - // input.html(text.replaceAll("\n", "
").replaceAll(" ", " ")).push(); - input.html(text.replace(/\n/g, "
").replace(/ /g, " ")).push(); - // create range to select text + input.html(replaceNewLinesAndSpaces(text)).push(); + return input._el; + + function replaceNewLinesAndSpaces(text: string): string { + return text.replace(/\n/g, "
").replace(/ /g, " "); + } + } + + function selectText(element: HTMLElement): Selection | null { const range: Range = document.createRange(); - range.setStart(input._el, 0); - range.setEnd(input._el, input._el.childNodes.length); + range.setStart(element, 0); + range.setEnd(element, element.childNodes.length); const selection: Selection | null = window.getSelection(); - if (!selection) return; + if (!selection) return null; selection.removeAllRanges(); selection.addRange(range); - // copy text + return selection; + } + + function copyToClipboard(selection: Selection | null): void { + if (!selection) { + if (__DEV__) { + _dev.warn("fastjs/utils/copy", "selection is null"); + } + return; + } document.execCommand("copy"); - input.remove(); - }, - rand(min: number, max: number, decimal: number = 0): number { - // return Math.floor(Math.random() * (max - min + 1)) + min; - return Number( - (Math.random() * (max - min) + min).toFixed(decimal) - ); - }, - // install( - // name: T - // ): T extends "FastjsDom" ? typeof FastjsDom : typeof FastjsArray { - // interface module { - // 0: string; - // 1: Function; - // } - // type method = Function; - // - // // module - // const install = eval(`new ${name}()`); - // // get all methods - // let methods: Array = Object.entries(install); - // const moduleList: Array = []; - // const methodList: Array = []; - // methods.forEach((v: module) => { - // // if function and not private - // if (typeof v[1] === "function" && !v[0].startsWith("#")) - // // add to moduleList - // moduleList.push(v); - // }); - // moduleList.forEach((v: module) => { - // methodList.push((obj: any) => { - // // fade class - // const fade = eval(`new ${module}(${obj})`); - // // do method - // return fade[v[0]](...Array.from(arguments).slice(1)); - // }); - // }); - // // @ts-ignore - // return methodList; - // }, -}; + } +} -export const {copy, rand} = fastjs; +function rand(min: number, max: number, decimal: number = 0): string { + return (Math.random() * (max - min) + min).toFixed(decimal); +} + +export { + copy, + rand +}; export default { copy, rand