Skip to content

Commit

Permalink
docs: fixed typo
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxFrank13 committed Dec 12, 2023
1 parent bc42b37 commit 11a8db9
Showing 1 changed file with 0 additions and 104 deletions.
104 changes: 0 additions & 104 deletions src/plugins/data/hooks.js
Original file line number Diff line number Diff line change
@@ -1,107 +1,3 @@
import {
useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState,
} from 'react';
import { getConfig } from '@edx/frontend-platform';
import { PLUGIN_MOUNTED, PLUGIN_READY, PLUGIN_UNMOUNTED } from './constants';

export function usePluginSlot(id) {
if (getConfig().plugins[id] !== undefined) {
return getConfig().plugins[id];
}
return { keepDefault: true, plugins: [] };
}

export function useMessageEvent(srcWindow, type, callback) {
useLayoutEffect(() => {
const listener = (event) => {
// Filter messages to those from our source window.
if (event.source === srcWindow) {
if (event.data.type === type) {
callback({ type, payload: event.data.payload });
}
}
};
if (srcWindow !== null) {
global.addEventListener('message', listener);
}
return () => {
global.removeEventListener('message', listener);
};
}, [srcWindow, type, callback]);
}

export function useHostEvent(type, callback) {
useMessageEvent(global.parent, type, callback);
}

export function usePluginEvent(iframeElement, type, callback) {
const contentWindow = iframeElement ? iframeElement.contentWindow : null;
useMessageEvent(contentWindow, type, callback);
}

export function dispatchMessageEvent(targetWindow, message, targetOrigin) {
// Checking targetOrigin falsiness here since '', null or undefined would all be reasons not to
// try to post a message to the origin.
if (targetOrigin) {
targetWindow.postMessage(message, targetOrigin);
}
}

export function dispatchPluginEvent(iframeElement, message, targetOrigin) {
dispatchMessageEvent(iframeElement.contentWindow, message, targetOrigin);
}

export function dispatchHostEvent(message) {
dispatchMessageEvent(global.parent, message, global.document.referrer);
}

export function dispatchReadyEvent() {
dispatchHostEvent({ type: PLUGIN_READY });
}

export function dispatchMountedEvent() {
dispatchHostEvent({ type: PLUGIN_MOUNTED });
}

export function dispatchUnmountedEvent() {
dispatchHostEvent({ type: PLUGIN_UNMOUNTED });
}

export function useElementSize() {
const observerRef = useRef();

const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
const [offset, setOffset] = useState({ x: 0, y: 0 });

const [element, setElement] = useState(null);

const measuredRef = useCallback(_element => {
setElement(_element);
}, []);

useEffect(() => {
observerRef.current = new ResizeObserver(() => {
if (element) {
setDimensions({
width: element.clientWidth,
height: element.clientHeight,
});
setOffset({
x: element.offsetLeft,
y: element.offsetTop,
});
}
});
if (element) {
observerRef.current.observe(element);
}
}, [element]);

return useMemo(
() => ([measuredRef, element, dimensions.width, dimensions.height, offset.x, offset.y]),
[measuredRef, element, dimensions, offset],
);
}
/**
* Hooks file for functions that handle the communication between a Plugin and its Host
*/
Expand Down

0 comments on commit 11a8db9

Please sign in to comment.