From fc055bc1e9d4d32c208e898a8b769eeaad3113e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Thu, 19 May 2022 09:36:43 +0800 Subject: [PATCH] feat: safe.append and safe.remove --- packages/preload/loading.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/preload/loading.ts b/packages/preload/loading.ts index e4babed..84d72df 100644 --- a/packages/preload/loading.ts +++ b/packages/preload/loading.ts @@ -43,12 +43,25 @@ export function useLoading() { return { appendLoading() { - document.head.appendChild(oStyle) - document.body.appendChild(oDiv) + safe.append(document.head, oStyle) + safe.append(document.body, oDiv) }, removeLoading() { - document.head.removeChild(oStyle) - document.body.removeChild(oDiv) + safe.remove(document.head, oStyle) + safe.remove(document.body, oDiv) }, } } + +const safe = { + append(parent: HTMLElement, child: HTMLElement) { + if (!Array.from(parent.children).find(e => e === child)) { + return parent.appendChild(child) + } + }, + remove(parent: HTMLElement, child: HTMLElement) { + if (Array.from(parent.children).find(e => e === child)) { + return parent.removeChild(child) + } + }, +}