Skip to content

Commit

Permalink
Forgot to handled hydrate condition, reverting commit
Browse files Browse the repository at this point in the history
Revert "feat: manage  adobe-embed-api with react refs"

This reverts commit cb903e9.
  • Loading branch information
Ziping Liu committed Nov 15, 2023
1 parent cb903e9 commit 3bc57fc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 123 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-adobe-embed",
"version": "11.1.5",
"version": "11.2.5",
"homepage": "https://ziping-liu-corporation.github.io/react-adobe-embed/#/home",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.canary.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const config = {
"dist",
],

babelHelpers: 'inline',
babelHelpers: 'external',
include: [
"canary/App.tsx"
],
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.cdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const config = {
babel({
exclude: 'node_modules/**',

babelHelpers: 'inline',
babelHelpers: 'external',
presets: [
"@babel/preset-env",
"@babel/preset-react",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default [
localResolve(),
babel({ exclude: 'node_modules/**, src/tests/**' ,

babelHelpers: 'inline',
babelHelpers: 'external',
presets: [
"@babel/preset-env",
"@babel/preset-react",
Expand Down
163 changes: 44 additions & 119 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from "react";
import ReactDOM from "react-dom/client";

export function previewFile({
divId,
Expand Down Expand Up @@ -123,126 +122,52 @@ export default function ReactViewAdobe(props: ReactViewAdobeProps) {
}
}, [adobePDFProgrammeInstalled]);

const [scriptRef, setScriptRef] = React.useState<React.RefObject<HTMLScriptElement> | null>(null);




const useHooksForLoading =
React[props?.useReactHookWhenLoadingAdobeAPI || "useEffect"];

useHooksForLoading(() => {
if (scriptRef == null) {
const newRef = React.createRef<HTMLScriptElement>() as React.RefObject<HTMLScriptElement>;

setScriptRef(newRef);


}
if (scriptViewerLoaded === false) {
const scriptExistsALreadyElem = document.querySelector(
const scriptExistsALready = document.querySelector(
`script.react-adobe-embed-handholding-adobe-api-loading-idiocy[data-adobe-pdf-id="${props.id || DefaultConfigs.staticDivId}"]`,
);

const scriptExistsALready = scriptRef;
if (scriptExistsALready && scriptExistsALreadyElem) {
if (scriptExistsALready) {
if (props.debug)
console.info(`\x1b[1mAdobe SDK Check\x1b[0m`, 'Reloading and Rerendering Adobe SDK');

// Lightbox mode renders from ui event triggered by user, so no need to render
if (props.previewConfig?.embedMode !== "LIGHT_BOX") {
setComponentNeedsRendering(true);
if(props.previewConfig?.embedMode !== "LIGHT_BOX"){
setComponentNeedsRendering(true);
}

scriptExistsALready.current!.setAttribute(
scriptExistsALready.setAttribute(
"data-testid",
"react-adobe-embed-handholding-adobe-api-loading-idiocy-reused",
);
} else {
if (props.debug)
console.info(`\x1b[1mAdobe SDK Check\x1b[0m`, 'Initial Adobe SDK Load');


const ScriptNew = () => {
const [componentDidMount, setComponentDidMount] = React.useState(false);
const [componentDidUpdate, setComponentDidUpdate] = React.useState(false);
React.useEffect(() => {
if (componentDidMount === false) {
setComponentDidMount(true);
}
if (componentDidMount
&& componentDidUpdate === false
) {
const script = document.createElement("script");
// const script = document.createElement("script");
script.setAttribute(
"data-testid",
"react-adobe-embed-handholding-adobe-api-loading-idiocy-initial",
);



script.setAttribute(
"data-adobe-pdf-id",
props.id || DefaultConfigs.staticDivId,
);
script.setAttribute(
"class",
"react-adobe-embed-handholding-adobe-api-loading-idiocy",
);
script.src =
props.previewConfig?.viewSdkViewerScript ||
DefaultConfigs.staticDefaultConfig.viewSdkViewerScript;
script.async = true;
script.onload = () => {
setScriptViewerLoaded(true);
};

document.body.appendChild(script);

setComponentDidUpdate(true);
}
}, [componentDidMount, scriptViewerLoaded, props]);



return <script
data-testid="react-adobe-embed-handholding-adobe-api-loading-idiocy-initial"
data-adobe-pdf-id={props.id || DefaultConfigs.staticDivId}
className="react-adobe-embed-handholding-adobe-api-loading-idiocy"
src={props.previewConfig?.viewSdkViewerScript ||
DefaultConfigs.staticDefaultConfig.viewSdkViewerScript}
async={true}
ref={scriptRef}

></script>
}

// render by appending to body, via creating a div element, and then appending the script to the div element


const scriptElement = document.createElement("script");
scriptElement.setAttribute(
console.info(`\x1b[1mAdobe SDK Check\x1b[0m`, 'Initial Adobe SDK Load');
const script = document.createElement("script");
script.setAttribute(
"data-testid",
"react-adobe-embed-handholding-adobe-api-loading-idiocy-initial",
);

scriptElement.setAttribute(
script.setAttribute(
"data-adobe-pdf-id",
props.id || DefaultConfigs.staticDivId,
);

document.body.appendChild(scriptElement);



ReactDOM.createRoot(scriptElement).render(<ScriptNew />);






script.setAttribute(
"class",
"react-adobe-embed-handholding-adobe-api-loading-idiocy",
);
script.src =
props.previewConfig?.viewSdkViewerScript ||
DefaultConfigs.staticDefaultConfig.viewSdkViewerScript;
script.async = true;
script.onload = () => {
setScriptViewerLoaded(true);
};
document.body.appendChild(script);
}
}
}, [props.id, scriptViewerLoaded, adobePDFProgrammeInstalled]);
Expand Down Expand Up @@ -370,12 +295,12 @@ export type PreviewFileConfig = {
Users can use the swipe gesture to navigate to other pages which will be displayed one at a time.
*/
defaultViewMode:
| "FIT_WIDTH"
| "FIT_PAGE"
| "TWO_COLUMN"
| "TWO_COLUMN_FIT_PAGE"
| "CONTINUOUS"
| "SINGLE_PAGE";
| "FIT_WIDTH"
| "FIT_PAGE"
| "TWO_COLUMN"
| "TWO_COLUMN_FIT_PAGE"
| "CONTINUOUS"
| "SINGLE_PAGE";
enableFormFilling: boolean;
showDownloadPDF: boolean;
showPrintPDF: boolean;
Expand Down Expand Up @@ -422,20 +347,20 @@ export type ReactHooks = {
[key in Extract<keyof typeof React, `use${string}`>]: [] extends Parameters<
(typeof React)[key]
>
? never
: key extends "useReducer"
? never
: key extends "useDeferredValue"
? never
: (typeof React)[key] extends (
factory: React.EffectCallback,
deps?: React.DependencyList | undefined,
) => void
? key
: (typeof React)[key] extends (
factory: () => any,
deps: React.DependencyList | undefined,
) => void
? key
: never;
? never
: key extends "useReducer"
? never
: key extends "useDeferredValue"
? never
: (typeof React)[key] extends (
factory: React.EffectCallback,
deps?: React.DependencyList | undefined,
) => void
? key
: (typeof React)[key] extends (
factory: () => any,
deps: React.DependencyList | undefined,
) => void
? key
: never;
}[Extract<keyof typeof React, `use${string}`>];

0 comments on commit 3bc57fc

Please sign in to comment.