Skip to content

feat: add watermark #6300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add mutationObserver
  • Loading branch information
aibayanyu20 committed Feb 21, 2023
commit 6cc6e61f81be93fd19a73bd79a2967fd29b4b5ef
62 changes: 62 additions & 0 deletions components/_util/hooks/_vueuse/useMutationObserver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { tryOnScopeDispose } from './tryOnScopeDispose';
import { watch } from 'vue';
import type { MaybeElementRef } from './unrefElement';
import { unrefElement } from './unrefElement';
import { useSupported } from './useSupported';
import type { ConfigurableWindow } from './_configurable';
import { defaultWindow } from './_configurable';

export interface UseMutationObserverOptions extends MutationObserverInit, ConfigurableWindow {}

/**
* Watch for changes being made to the DOM tree.
*
* @see https://vueuse.org/useMutationObserver
* @see https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver MutationObserver MDN
* @param target
* @param callback
* @param options
*/
export function useMutationObserver(
target: MaybeElementRef,
callback: MutationCallback,
options: UseMutationObserverOptions = {},
) {
const { window = defaultWindow, ...mutationOptions } = options;
let observer: MutationObserver | undefined;
const isSupported = useSupported(() => window && 'MutationObserver' in window);

const cleanup = () => {
if (observer) {
observer.disconnect();
observer = undefined;
}
};

const stopWatch = watch(
() => unrefElement(target),
el => {
cleanup();

if (isSupported.value && window && el) {
observer = new MutationObserver(callback);
observer!.observe(el, mutationOptions);
}
},
{ immediate: true },
);

const stop = () => {
cleanup();
stopWatch();
};

tryOnScopeDispose(stop);

return {
isSupported,
stop,
};
}

export type UseMutationObserverReturn = ReturnType<typeof useMutationObserver>;
27 changes: 0 additions & 27 deletions components/_util/mutateObserver.tsx

This file was deleted.

3 changes: 3 additions & 0 deletions components/watermark/__tests__/demo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import demoTest from '../../../tests/shared/demoTest';

demoTest('watermark');
29 changes: 29 additions & 0 deletions components/watermark/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Watermark from '..';
import mountTest from '../../../tests/shared/mountTest';
import { mount } from '@vue/test-utils';

describe('Watermark', () => {
mountTest(Watermark);
const mockSrcSet = jest.spyOn(Image.prototype, 'src', 'set');
beforeAll(() => {
mockSrcSet.mockImplementation(function fn() {
this.onload?.();
});
});

afterAll(() => {
mockSrcSet.mockRestore();
});

it('The watermark should render successfully ', function () {
const wrapper = mount({
setup() {
return () => {
return <Watermark class="watermark" content="Ant Design" />;
};
},
});
expect(wrapper.find('.watermark').exists()).toBe(true);
expect(wrapper.html()).toMatchSnapshot();
});
});
29 changes: 17 additions & 12 deletions components/watermark/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { PropType, ExtractPropTypes, CSSProperties } from 'vue';
import { computed, defineComponent, onBeforeUnmount, onMounted, shallowRef, watch } from 'vue';
import { getStyleStr, getPixelRatio, rotateWatermark } from './utils';
import { getStyleStr, getPixelRatio, rotateWatermark, reRendering } from './utils';
import { withInstall } from '../_util/type';
import MutateObserver from '../_util/mutateObserver';
import { useMutationObserver } from '../_util/hooks/_vueuse/useMutationObserver';

/**
* Base size of the canvas, 1 for parallel layout and 2 for alternate layout
Expand Down Expand Up @@ -231,22 +231,27 @@ const Watermark = defineComponent({
onBeforeUnmount(() => {
destroyWatermark();
});
const onMutate = () => {
const onMutate = (mutations: MutationRecord[]) => {
if (stopObservation.value) {
return;
}
mutations.forEach(mutation => {
if (reRendering(mutation, watermarkRef.value)) {
destroyWatermark();
renderWatermark();
}
});
};
useMutationObserver(containerRef, onMutate);
return () => {
return (
<MutateObserver onMutate={onMutate}>
<div
ref={containerRef}
class={[attrs.class, props.rootClassName]}
style={{ position: 'relative' }}
>
{slots.default?.()}
</div>
</MutateObserver>
<div
ref={containerRef}
class={[attrs.class, props.rootClassName]}
style={{ position: 'relative' }}
>
{slots.default?.()}
</div>
);
};
},
Expand Down