Skip to content

Commit

Permalink
refactor(utils): update 3 files
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaodong2008 committed Nov 21, 2023
1 parent 1afb7ee commit eb009e8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down
5 changes: 3 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/** @todo Need work */
export default {}
import utils from "./methods";
export default utils;
export * from "./methods";
93 changes: 39 additions & 54 deletions src/utils/methods.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const input = await createDomElement(text);
const selection = selectText(input);
copyToClipboard(selection);
input.remove();

let fastjs = {
async copy(text: string): Promise<void> {
async function createDomElement(text: string): Promise<HTMLElement> {
const {FastjsDom} = await import("../dom");
// copy text to clipboard
let input = new FastjsDom("span");
// input.html(text.replaceAll("\n", "<br>").replaceAll(" ", "&nbsp;")).push();
input.html(text.replace(/\n/g, "<br>").replace(/ /g, "&nbsp;")).push();
// create range to select text
input.html(replaceNewLinesAndSpaces(text)).push();
return input._el;

function replaceNewLinesAndSpaces(text: string): string {
return text.replace(/\n/g, "<br>").replace(/ /g, "&nbsp;");
}
}

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<T extends "FastjsDom" | "FastjsArray">(
// 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<module> = Object.entries(install);
// const moduleList: Array<module> = [];
// const methodList: Array<method> = [];
// 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
Expand Down

0 comments on commit eb009e8

Please sign in to comment.