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 story editor utils #12737

Merged
merged 77 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 64 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
bfb383d
First pass.
spacedmonkey Nov 25, 2022
145c95d
Merge branch 'main' into try/convert-ts-utils
spacedmonkey Nov 30, 2022
c300979
Fixes.
spacedmonkey Nov 30, 2022
a758ac6
More fixes.
spacedmonkey Nov 30, 2022
b7122f5
Merge branch 'main' into try/convert-ts-utils
spacedmonkey Dec 1, 2022
f788c62
More conversions.
spacedmonkey Dec 1, 2022
6609d98
More fixes.
spacedmonkey Dec 1, 2022
2070bdb
Many more fixes.
spacedmonkey Dec 1, 2022
ad57793
Restore file.
spacedmonkey Dec 2, 2022
e0b4600
Copy tweaks from story provider PR.
spacedmonkey Dec 2, 2022
6aab963
Fix unit tests.
spacedmonkey Dec 2, 2022
d9ba538
Fix unit tests again.
spacedmonkey Dec 2, 2022
6ee8cd5
Merge branch 'main' into try/convert-ts-utils
spacedmonkey Dec 2, 2022
0add1f1
Move files.
spacedmonkey Dec 2, 2022
dc41767
generateGroupName to type script.
spacedmonkey Dec 2, 2022
f8d0140
More fixes.
spacedmonkey Dec 2, 2022
2bafd0f
Type script copy paste.
spacedmonkey Dec 2, 2022
fade3b2
Convert usePerformanceTracking.
spacedmonkey Dec 5, 2022
27af8e7
Revert copy and paste.
spacedmonkey Dec 5, 2022
dc19d68
Fixes.
spacedmonkey Dec 5, 2022
88d8160
Revert change.
spacedmonkey Dec 5, 2022
acc7cd1
Fix tests.
spacedmonkey Dec 5, 2022
686fb9b
Fix lints.
spacedmonkey Dec 5, 2022
1f68f28
Merge branch 'main' into try/convert-ts-utils
spacedmonkey Dec 5, 2022
fbc0443
Bump softprops/action-gh-release from 0.1.14 to 0.1.15 (#12765)
dependabot[bot] Dec 5, 2022
5278f28
Bump peter-evans/find-comment from 2.0.1 to 2.1.0 (#12766)
dependabot[bot] Dec 5, 2022
02e9969
Bump github/codeql-action from 2.1.32 to 2.1.35 (#12767)
dependabot[bot] Dec 5, 2022
e07a353
Bump szepeviktor/phpstan-wordpress from 1.1.5 to 1.1.6 (#12768)
dependabot[bot] Dec 5, 2022
94af272
Bump lint-staged from 13.0.3 to 13.1.0 (#12772)
dependabot[bot] Dec 5, 2022
8a4aa69
Bump @types/node from 18.11.9 to 18.11.11 (#12774)
dependabot[bot] Dec 5, 2022
0e34984
Bump stylelint from 14.15.0 to 14.16.0 (#12775)
dependabot[bot] Dec 5, 2022
76c928d
Bump cssnano from 5.1.13 to 5.1.14 (#12776)
dependabot[bot] Dec 5, 2022
c255aca
Bump @pmmmwh/react-refresh-webpack-plugin from 0.5.8 to 0.5.10 (#12770)
dependabot[bot] Dec 5, 2022
e5ee487
Remove no longer needed dependency
swissspidy Dec 6, 2022
0943425
Bump typescript from 4.8.4 to 4.9.3 (#12719)
dependabot[bot] Dec 6, 2022
de5514f
Layers visibility (#12738)
merapi Dec 6, 2022
11280a3
Bump qs from 6.5.2 to 6.5.3 (#12781)
dependabot[bot] Dec 6, 2022
319392c
Update readme
swissspidy Dec 6, 2022
25c3d9a
Enables page level advancement settings by default (#12780)
miina Dec 6, 2022
ea20056
Post-release version bump
googleforcreators-bot Dec 6, 2022
3737484
Use group type.
spacedmonkey Dec 7, 2022
0648200
Move cast.
spacedmonkey Dec 7, 2022
b476594
Add type.
spacedmonkey Dec 7, 2022
0568558
Feedback 1.
spacedmonkey Dec 7, 2022
d9e9ade
Feedback 2.
spacedmonkey Dec 7, 2022
855fcf8
Merge branch 'main' into try/convert-ts-utils
spacedmonkey Dec 7, 2022
c14ed54
Remove typing.
spacedmonkey Dec 7, 2022
24ff408
Feedback 3.
spacedmonkey Dec 7, 2022
419118e
Combine if statements.
spacedmonkey Dec 7, 2022
e8c0dc5
Merge branch 'main' into try/convert-ts-utils
spacedmonkey Dec 7, 2022
3958bfd
Use native types.
spacedmonkey Dec 7, 2022
1785892
Apply suggestions from code review
spacedmonkey Dec 7, 2022
7fe71f4
Add direction type.
spacedmonkey Dec 7, 2022
f0a3502
Use noop in a couple of places.
spacedmonkey Dec 7, 2022
ffb4529
Remove inRange util.
spacedmonkey Dec 7, 2022
aa4b06a
Apply suggestions from code review
spacedmonkey Dec 7, 2022
b1de221
PointEventValue update.
spacedmonkey Dec 7, 2022
7e74995
Apply suggestions from code review
spacedmonkey Dec 7, 2022
4c50d56
Add type.
spacedmonkey Dec 7, 2022
c15bec6
Merge branch 'try/convert-ts-utils' of github.com:google/web-stories-…
spacedmonkey Dec 7, 2022
54ea66e
Fix tests.
spacedmonkey Dec 7, 2022
d8a3120
Feedback.
spacedmonkey Dec 7, 2022
6f5cd63
Merge branch 'main' into try/convert-ts-utils
spacedmonkey Dec 7, 2022
290f238
Fix lint.
spacedmonkey Dec 8, 2022
5e661c5
Merge branch 'main' into try/convert-ts-utils
spacedmonkey Dec 8, 2022
a152bd3
Rebase
spacedmonkey Dec 8, 2022
5073d8b
Feedback.
spacedmonkey Dec 8, 2022
8a65cc7
Feedback 2.
spacedmonkey Dec 8, 2022
33eae34
xMerge branch 'main' into try/convert-ts-utils
spacedmonkey Dec 9, 2022
e8afa49
Feedback 3.
spacedmonkey Dec 9, 2022
b39a424
Update packages/story-editor/src/utils/getInsertedElementSize.ts
spacedmonkey Dec 9, 2022
1eb551b
Feedback 4.
spacedmonkey Dec 9, 2022
e5c166c
Feedback 5.
spacedmonkey Dec 9, 2022
dc043a8
Unpack.
spacedmonkey Dec 9, 2022
4f6e456
Merge branch 'main' into try/convert-ts-utils
spacedmonkey Dec 9, 2022
c82bb25
Fix lints.
spacedmonkey Dec 9, 2022
6fd2808
Feedback 6.
spacedmonkey Dec 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/elements/src/types/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export interface ProductElement extends Element {
}

export interface TextElement extends Element {
backgroundColor: Solid;
content: string;
font: {
service: string;
Expand Down
10 changes: 9 additions & 1 deletion packages/elements/src/types/elementDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export interface DisplayProps<E extends Element> {
element: E;
}

export type Direction = [0 | 1, 0 | 1];

export interface ElementDefinition<E extends Element = Element> {
type: ElementType;
name: string;
Expand All @@ -71,7 +73,7 @@ export interface ElementDefinition<E extends Element = Element> {
Frame: React.VoidFunctionComponent<FrameProps<E>>;
Output: React.VoidFunctionComponent<OutputProps<E>>;
LayerIcon: React.VoidFunctionComponent<LayerIconProps<E>>;
TextContent: React.VoidFunctionComponent<TextContentProps<E>>;
TextContent: (element: E) => string;
Display: React.VoidFunctionComponent<DisplayProps<E>>;
canFlip: boolean;
isMaskable: boolean;
Expand All @@ -90,6 +92,12 @@ export interface ElementDefinition<E extends Element = Element> {
minHeight: number;
};
panels: string[];
updateForResizeEvent?: (
element: Element,
direction: Direction,
newWidth: number,
newHeight: number
) => { height: number };
}

export type ElementTypes = Record<ElementType, ElementDefinition<Element>>;
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import { useCallback, useRef } from '@googleforcreators/react';
/**
* Internal dependencies
*/
import storyPageToCanvas from '../../utils/storyPageToCanvas';
import { getAccessibleTextColorsFromPixels } from '../../utils/contrastUtils';
import useIdleQueue from '../../utils/useIdleTaskQueue';
import { useStory } from '../story';
import { STABLE_ARRAY } from '../../constants';
import storyPageToCanvas from './utils/storyPageToCanvas';
import Context from './context';
import getPixelDataFromCanvas from './getPixelDataFromCanvas';
import usePageCanvasMap from './usePageCanvasMap';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import { act, renderHook } from '@testing-library/react-hooks';
* Internal dependencies
*/
import { PageCanvasProvider, usePageCanvas } from '..';
import storyPageToCanvas from '../../../utils/storyPageToCanvas';
import storyPageToCanvas from '../utils/storyPageToCanvas';
import useStory from '../../story/useStory';
import createMockPage from '../testUtils/createMockPage';
import { MockStoryProvider, useStoryMock } from '../testUtils/useStoryMock';

jest.mock('../../story/useStory');
jest.mock('../../../utils/storyPageToCanvas');
jest.mock('../utils/storyPageToCanvas');

jest.mock('../../../utils/contrastUtils', () => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import { TransformProvider } from '@googleforcreators/transform';
/**
* Internal dependencies
*/
import { FontProvider } from '../app/font';
import DisplayElement from '../components/canvas/displayElement';
import { FontProvider } from '../../font';
import DisplayElement from '../../../components/canvas/displayElement';

const Page = styled.div`
display: block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useMemo, useCallback, useState } from '@googleforcreators/react';
* Internal dependencies
*/
import useIdleTaskQueue from '../../utils/useIdleTaskQueue';
import storyPageToDataUrl from '../../utils/storyPageToDataUrl';
import storyPageToDataUrl from '../pageCanvas/utils/storyPageToDataUrl';
import Context from './context';

/**
Expand Down Expand Up @@ -52,7 +52,10 @@ function PageDataUrlProvider({ children }) {
}));
};

const clearQueueOfPageTask = queueIdleTask([idleTaskUid, idleTask]);
const clearQueueOfPageTask = queueIdleTask({
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
taskId: idleTaskUid,
task: idleTask,
});
return () => {
clearQueueOfPageTask();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jest.mock('../../../utils/idleCallback', () => {
};
});

jest.mock('../../../utils/storyPageToDataUrl', () => {
jest.mock('../../pageCanvas/utils/storyPageToDataUrl', () => {
return {
__esModule: true,
// make dataUrl generation predictable
Expand All @@ -60,19 +60,19 @@ describe('usePageDataUrls', () => {
mockIdleCallbacks = [];
runIdleCallbacks = () => {
while (mockIdleCallbacks.length > 0) {
const [, callback] = mockIdleCallbacks.shift();
callback();
const { task } = mockIdleCallbacks.shift();
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
task();
}
};

requestIdleCallback.mockImplementation((callback) => {
const idleCallbackId = Symbol();
mockIdleCallbacks.push([idleCallbackId, callback]);
mockIdleCallbacks.push({ taskId: idleCallbackId, task: callback });
return idleCallbackId;
});
cancelIdleCallback.mockImplementation((idleCallbackId) => {
mockIdleCallbacks = mockIdleCallbacks.filter(
([id]) => id !== idleCallbackId
({ taskId }) => taskId !== idleCallbackId
);
});
});
Expand Down
5 changes: 2 additions & 3 deletions packages/story-editor/src/components/canvas/frameElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import WithProductPill from '../shopping/frame';
import useDoubleClick from '../../utils/useDoubleClick';
import usePerformanceTracking from '../../utils/usePerformanceTracking';
import { TRACKING_EVENTS } from '../../constants';
import { noop } from '../../utils/noop';
import {
FOCUS_GROUPS,
useFocusGroupRef,
Expand Down Expand Up @@ -103,8 +104,6 @@ const EmptyFrame = styled.div`
pointer-events: none;
`;

const NOOP = () => {};

const FRAME_ELEMENT_MESSAGE = sprintf(
/* translators: %s: Ctrl+Alt+P keyboard shortcut. */
__(
Expand Down Expand Up @@ -219,7 +218,7 @@ function FrameElement({ id }) {
},
[id, setEditingElement, handleSelectElement, isSelected]
);
const handleMediaClick = useDoubleClick(NOOP, handleMediaDoubleClick);
const handleMediaClick = useDoubleClick(noop, handleMediaDoubleClick);

/**
* Announce keyboard options on element.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import { safeDecodeURIComponent } from '@googleforcreators/url';
*/
import { useStory } from '../../../../app/story';
import cleanForSlug from '../../../../utils/cleanForSlug';
import inRange from '../../../../utils/inRange';
import { Row } from '../../../form';
import { SimplePanel } from '../../panel';
import { inputContainerStyleOverride } from '../../shared/styles';

const inRange = (value, { MIN, MAX }) => value >= MIN && value <= MAX;

export const MIN_MAX = {
PERMALINK: {
MIN: 1,
Expand Down
3 changes: 1 addition & 2 deletions packages/story-editor/src/components/tabview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { useConfig } from '../../app';
import Tooltip from '../tooltip';
import usePerformanceTracking from '../../utils/usePerformanceTracking';
import { TRACKING_EVENTS } from '../../constants';
import { noop } from '../../utils/noop';

const ALERT_ICON_SIZE = 28;
export const TAB_HEIGHT = 32;
Expand Down Expand Up @@ -146,8 +147,6 @@ const TabText = styled(Headline).attrs({
color: inherit;
`;

const noop = () => {};

function UnreffedTab(
{ children, tooltip = null, placement, refId, tabRefs, ...rest },
ref
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ import removeAccents from 'remove-accents';
* for octets, HTML entities, or other encoded characters.
*
* @see https://github.com/WordPress/gutenberg/blob/5ff5acd2815d5260db15ab155fcaf4b3b80d7c1b/packages/url/src/clean-for-slug.js
* @param {string} string Title or slug to be processed.
* @param {boolean} isEditing Flag the user is currently editing the input
* @param string Title or slug to be processed.
* @param isEditing Flag the user is currently editing the input
allowing extra hyphens (default false)
* @return {string} Processed string.
* @return Processed string.
*/
export default function cleanForSlug(string, isEditing = false) {
export default function cleanForSlug(string: string, isEditing = false) {
if (!string) {
return '';
}

return (
return String(
[removeAccents(string)]
// Convert each group of whitespace, periods, and forward slashes to a hyphen.
.map((s) => s.replace(/[\s./_]/g, '-'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@
* limitations under the License.
*/

class CustomError extends Error {
public file = '';
public isUserError = false;
}
/**
* Helper function to get create a js error.
*
* @param {string} name Error name.
* @param {string} fileName File name.
* @param {string} message Message in error.
* @return {Error} Error Object.
* @param name Error name.
* @param fileName File name.
* @param message Message in error.
* @return Error Object.
*/
function createError(name, fileName, message) {
const validError = new Error();
function createError(name: string, fileName: string, message: string) {
const validError = new CustomError();

validError.name = name;
validError.file = fileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import { PAGE_RATIO } from '@googleforcreators/units';
* Creates a thumbnail aspect ratio canvas when given a fullbleed aspect
* ratio canvas.
*
* @param {HTMLCanvasElement} fullbleedCanvas fullbleed aspect ratio canvas element
* @return {HTMLCanvasElement} a new thumbnail aspect ratio canvas element
* @param fullbleedCanvas fullbleed aspect ratio canvas element
* @return a new thumbnail aspect ratio canvas element
*/
function createThumbnailCanvasFromFullbleedCanvas(fullbleedCanvas) {
function createThumbnailCanvasFromFullbleedCanvas(
fullbleedCanvas: HTMLCanvasElement
) {
const thumbnailCanvas = document.createElement('canvas');
const thumbnailContext = thumbnailCanvas.getContext('2d');

Expand All @@ -34,8 +36,9 @@ function createThumbnailCanvasFromFullbleedCanvas(fullbleedCanvas) {
const dy = (fullbleedHeight - thumbnailHeight) / 2;
thumbnailCanvas.width = fullbleedCanvas.width;
thumbnailCanvas.height = thumbnailHeight;
thumbnailContext.drawImage(fullbleedCanvas, 0, -dy);

if (thumbnailContext) {
thumbnailContext.drawImage(fullbleedCanvas, 0, -dy);
}
return thumbnailCanvas;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,31 @@
* limitations under the License.
*/

/**
* External dependencies
*/
import type { DragEvent } from 'react';

/**
* Get Drag Type from event.
*
* @param {DragEvent} e The drag event.
* @param {string} type The type of transfer payload to test against
* @return {boolean} Whether the drag is of the specified type
* @param e The drag event.
* @param type The type of transfer payload to test against
* @return Whether the drag is of the specified type
*/
export function isDragType(e, type) {
export function isDragType(e: DragEvent, type: string) {
if (!e?.dataTransfer?.types) {
return false;
}
return Array.isArray(e.dataTransfer.types)
? e.dataTransfer.types.includes(type)
: e.dataTransfer.types.contains(type);
return e.dataTransfer.types.includes(type);
}

/**
* Get Drag Type from event.
*
* @param {DragEvent} e The drag event.
* @return {boolean} Whether the drag event is relating to a file transfer
* @param e The drag event.
* @return Whether the drag event is relating to a file transfer
*/
export function isDraggingFile(e) {
export function isDraggingFile(e: DragEvent) {
return isDragType(e, 'Files');
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
* External dependencies
*/
import { __, sprintf } from '@googleforcreators/i18n';
import type { Groups } from '@googleforcreators/elements';

export function getNextGroupNumber(groups) {
export function getNextGroupNumber(groups: Groups) {
const nums = [0];
const defaultName = __('Group', 'web-stories');
for (const prop in groups) {
Expand All @@ -34,13 +35,13 @@ export function getNextGroupNumber(groups) {
return Math.max(...nums) + 1;
}

function generateGroupName(groups, name) {
function generateGroupName(groups: Groups, name = '') {
if (!name) {
const groupNumber = getNextGroupNumber(groups);
return sprintf(
/* translators: %d: group number. */
__('Group %d', 'web-stories'),
groupNumber
groupNumber.toString()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,30 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import type { MediaElement } from '@googleforcreators/elements';
/**
* Internal dependencies
*/
import { isOffCanvas } from './isOffCanvas';

export function getCropParams(selectedElement) {
// TODO, reuse CropParams from media utils.
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
type CropParams = {
newWidth: number;
newHeight: number;
cropElement: {
x: number;
y: number;
};
cropWidth: number;
cropHeight: number;
cropX: number;
cropY: number;
};

export function getCropParams(selectedElement: MediaElement): CropParams {
const { offCanvasLeft, offCanvasRight, offCanvasTop, offCanvasBottom } =
isOffCanvas(selectedElement);
const percentage = selectedElement.width / selectedElement.resource.width;
Expand Down