Skip to content
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

TypeScript: Convert pageDataUrls provider #12727

Merged
merged 35 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f7e4fdb
initial commit
timarney Nov 22, 2022
ffb32f0
update types
timarney Nov 22, 2022
30b51e9
Merge branch 'main' into fix/page-data-urls-12668
timarney Nov 25, 2022
08cda45
update config
timarney Nov 25, 2022
0078336
cleanup
timarney Nov 25, 2022
a2ad967
Merge branch 'main' into fix/page-data-urls-12668
timarney Nov 28, 2022
f1c53c9
update child type
timarney Nov 28, 2022
538d2c8
re-add await
timarney Nov 28, 2022
934a446
add imports
timarney Nov 28, 2022
ec647fa
Merge branch 'main' into fix/page-data-urls-12668
timarney Nov 28, 2022
78c21cc
Merge branch 'main' into fix/page-data-urls-12668
timarney Nov 29, 2022
82db1d2
Merge branch 'main' into fix/page-data-urls-12668
timarney Nov 29, 2022
7a90edb
Merge branch 'main' into fix/page-data-urls-12668
timarney Nov 30, 2022
9ebd090
update selector
timarney Nov 30, 2022
dfa6492
cleanup
timarney Nov 30, 2022
ccd9bf4
fix param
timarney Nov 30, 2022
c30bdf0
update use state
timarney Nov 30, 2022
9deab7c
use type
timarney Nov 30, 2022
482ed63
Update packages/story-editor/src/app/pageDataUrls/pageDataUrlsProvide…
timarney Nov 30, 2022
686f5a8
update config
timarney Nov 30, 2022
e479e6d
cleanup
timarney Nov 30, 2022
3a1607d
Merge branch 'main' into fix/page-data-urls-12668
timarney Nov 30, 2022
6fdae5f
Merge branch 'main' into fix/page-data-urls-12668
timarney Dec 1, 2022
7db1da4
remove casting
timarney Dec 1, 2022
6b28252
merge
timarney Dec 2, 2022
8392d3f
Merge branch 'main' into fix/page-data-urls-12668
swissspidy Dec 7, 2022
9891753
Continue migration
swissspidy Dec 7, 2022
6b07f91
Merge main
miina Jan 2, 2023
00ed9b5
Move things around
miina Jan 2, 2023
e9e22ef
Fix
miina Jan 2, 2023
6b249c4
Revert
miina Jan 2, 2023
38f58b9
Fix lint
miina Jan 2, 2023
a900175
Revert
miina Jan 2, 2023
bc3dc68
Merge remote-tracking branch 'origin/main' into fix/page-data-urls-12668
miina Jan 5, 2023
fba8475
PR feedback
miina Jan 5, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import type { Page } from '@googleforcreators/elements';
import { useMemo, useCallback, useState } from '@googleforcreators/react';

/**
Expand All @@ -26,12 +26,8 @@ import useIdleTaskQueue from '../../utils/useIdleTaskQueue';
import storyPageToDataUrl from '../../utils/storyPageToDataUrl';
import Context from './context';

/**
* @typedef {import('../../types.js').Page} Page
*/

function PageDataUrlProvider({ children }) {
const [dataUrls, setDataUrls] = useState({});
function PageDataUrlProvider({ children }: { children: React.ReactNode }) {
timarney marked this conversation as resolved.
Show resolved Hide resolved
const [dataUrls, setDataUrls] = useState<Record<string, string>>({});
const queueIdleTask = useIdleTaskQueue();

/**
Expand All @@ -41,20 +37,25 @@ function PageDataUrlProvider({ children }) {
* @param {Page} storyPage Page object.
* @return {Function} function to cancel image generation request
*/
const queuePageImageGeneration = useCallback(
(storyPage) => {
const idleTaskUid = storyPage.id;
const idleTask = async () => {
const queuePageImageGeneration: (Page) => void = useCallback(
(storyPage: Page) => {
const idleTaskUid: string = storyPage.id;
const idleTask: () => Promise<void> = async () => {
const dataUrl = await storyPageToDataUrl(storyPage, {});
setDataUrls((state) => ({
...state,
[storyPage.id]: dataUrl,
[storyPage?.id]: dataUrl,
}));
};

const clearQueueOfPageTask = queueIdleTask([idleTaskUid, idleTask]);
return () => {
clearQueueOfPageTask();
const clearQueueOfPageTask = queueIdleTask([
idleTaskUid,
idleTask,
]) as () => void;
return (): void => {
if (typeof clearQueueOfPageTask === 'function') {
clearQueueOfPageTask();
}
};
},
[queueIdleTask]
Expand All @@ -74,8 +75,5 @@ function PageDataUrlProvider({ children }) {

return <Context.Provider value={value}>{children}</Context.Provider>;
}
PageDataUrlProvider.propTypes = {
children: PropTypes.node,
};

export default PageDataUrlProvider;
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,38 @@
* External dependencies
*/
import { PAGE_RATIO } from '@googleforcreators/units';
import type { Page } from '@googleforcreators/elements';
import type { Options } from 'html-to-image/lib/types';
miina marked this conversation as resolved.
Show resolved Hide resolved
/**
* Internal dependencies
*/
import storyPageToNode from './storyPageToNode';

/**
* @typedef {import('../../../types').Page} Page
*/

/**
* Async method to generate a dataUrl from a story page.
*
* @param {Page} page Page object.
* @param {Object} options options to pass to htmlToImage.toJpeg
* @param {number} options.width desired width of image. Dictates height and container height
* @return {Promise<string>} jpeg dataUrl
* @param page Page object.
* @param options options to pass to htmlToImage.toJpeg
* @param options.width desired width of image. Dictates height and container height
* @return jpeg dataUrl
*/
async function storyPageToDataUrl(page, { width = 400, ...options }) {
async function storyPageToDataUrl(
page: Page,
{ width = 400, ...options }: Options
): Promise<string> {
const htmlToImage = await import(
/* webpackChunkName: "chunk-html-to-image" */ 'html-to-image'
);

const [node, cleanup] = await storyPageToNode(page, width);
const [node, cleanup] = storyPageToNode(page, width);
timarney marked this conversation as resolved.
Show resolved Hide resolved

const dataUrl = await htmlToImage.toJpeg(node, {
...options,
width,
height: width * (1 / PAGE_RATIO),
canvasHeight: width * (1 / PAGE_RATIO),
canvasWidth: width,
});
} as Options);
timarney marked this conversation as resolved.
Show resolved Hide resolved

cleanup();

Expand Down
3 changes: 2 additions & 1 deletion packages/story-editor/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"src/app/config/*",
"src/app/history",
"src/types",
"src/utils/usePreventWindowUnload.ts"
"src/utils/usePreventWindowUnload.ts",
"src/app/pageDataUrls/*"
]
}