Skip to content

Commit

Permalink
perf: compatibility on safari and firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernankez committed Jun 21, 2023
1 parent fd491b0 commit c9cf140
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
11 changes: 0 additions & 11 deletions src/components/InfoDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@
<div class="text-4 text-default">
上面是预览区,下面是操作区,要改哪里点哪里就行。
</div>
<ClientOnly>
<NPopover placement="bottom" raw :show-arrow="false" :delay="500">
<template #trigger>
<div>目前还是只支持Edge或Chrome浏览器😩如果有大佬想要增加适配欢迎pr🥺</div>
</template>
<div class="flex flex-col rounded-2 p-y-1 p-x-2 box-border bg-#ffffff">
Safari和火狐目前的问题在于生成图片时,为了生成更清晰的图片,我搞了一个尺寸两倍的display: none的组件丢给dom-to-image,此时需要给dom-to-image传一个Element的宽高,但是在Safari和火狐上获取不到这个宽高。<br />
另外火狐默认不支持ClipboardItem,没办法复制。
</div>
</NPopover>
</ClientOnly>
</div>
</NModal>
</template>
Expand Down
17 changes: 14 additions & 3 deletions src/composables/useHiddenElementStyle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Fn, MaybeComputedElementRef, MaybeElement, UnRefElementReturn } from "@vueuse/core";

export function useHiddenElementStyle(target: MaybeComputedElementRef) {
const style = ref({} as CSSStyleDeclaration);
const style = ref<Record<keyof CSSStyleDeclaration, string>>({} as any);

function updateStyle<T extends MaybeElement>(el: UnRefElementReturn<T>) {
if (!el) { return; }
Expand All @@ -15,12 +15,12 @@ export function useHiddenElementStyle(target: MaybeComputedElementRef) {
el.style.display = "unset";
el.style.visibility = "hidden";
el.style.position = "absolute";
style.value = { ...window.getComputedStyle(el) };
style.value = transformCSSProperties(window.getComputedStyle(el));
el.style.position = <string>position;
el.style.visibility = <string>visibility;
el.style.display = <string>display;
} else {
style.value = { ...window.getComputedStyle(el) };
style.value = transformCSSProperties(window.getComputedStyle(el));
}
}

Expand Down Expand Up @@ -51,6 +51,17 @@ export function useHiddenElementStyle(target: MaybeComputedElementRef) {
};
}

function transformCSSProperties(styles: CSSStyleDeclaration) {
const style: Record<string, string> = {};
const length = styles.length;
for (let i = 0; i < length; i++) {
const prop = styles[i];
const value = styles.getPropertyValue(prop);
style[prop] = value;
}
return style as Record<keyof CSSStyleDeclaration, string>;
}

function createStyleSelector(options?: { style?: CSSStyleDeclaration; computedStyle?: CSSStyleDeclaration }) {
const { style = {}, computedStyle = {} } = options || {};
return {
Expand Down

0 comments on commit c9cf140

Please sign in to comment.