Skip to content

Commit

Permalink
[EDT-814] Add Page Width, Height and OnPageSizeChanged (#255)
Browse files Browse the repository at this point in the history
[EDT-814] Add Page Width, Height and OnPageSizeChanged methods and event.
  • Loading branch information
Matthiee authored Mar 6, 2023
1 parent e4ebc11 commit 466ed95
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/controllers/PageController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EditorAPI, Id } from '../../types/CommonTypes';
import { getEditorResponseData } from '../utils/EditorResponseData';
import { Page } from '../../types/PageTypes';
import { getCalculatedValue } from '../utils/getCalculatedValue';

/**
* The PageController is responsible for all communication regarding Pages.
Expand Down Expand Up @@ -37,4 +38,44 @@ export class PageController {
const res = await this.#editorAPI;
return res.getPageById(pageId).then((result) => getEditorResponseData<Page>(result));
};

/**
* This method will set the width of the page to a specific value.
* This only works if the document is a project.
* @param pageId The ID of a specific page
* @param value The string value that will be calculated (f.e. 1+1 will result in 2) The notation is in pixels
* @returns
*/
setPageWidth = async (pageId: Id, value: string) => {
const res = await this.#editorAPI;
const calc = getCalculatedValue(value);

if (calc === null || calc === Infinity) {
return null;
}

return res
.setPageWidth(pageId, parseFloat(calc.toString()))
.then((result) => getEditorResponseData<null>(result));
};

/**
* This method will set the height of the page to a specific value.
* This only works if the document is a project.
* @param pageId The ID of a specific page
* @param value The string value that will be calculated (f.e. 1+1 will result in 2) The notation is in pixels
* @returns
*/
setPageHeight = async (pageId: Id, value: string) => {
const res = await this.#editorAPI;
const calc = getCalculatedValue(value);

if (calc === null || calc === Infinity) {
return null;
}

return res
.setPageHeight(pageId, parseFloat(calc.toString()))
.then((result) => getEditorResponseData<null>(result));
};
}
10 changes: 10 additions & 0 deletions src/controllers/SubscriberController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,14 @@ export class SubscriberController {
const callBack = this.config.onConnectorEvent;
callBack && callBack(JSON.parse(connectorEvent));
};

/**
* Listener on page size, this listener will get triggered when the page size is changed, while the document is a `project`.
* This will not emit anything if your document is a `template`.
* @param pageSize Stringified object of the PageSize
*/
onPageSizeChanged = (pageSize: string) => {
const callBack = this.config.onPageSizeChanged;
callBack && callBack(JSON.parse(pageSize));
};
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export class SDK {
onLayoutsChanged: this.subscriber.onLayoutsChanged,
onConnectorEvent: this.subscriber.onConnectorEvent,
onZoomChanged: this.subscriber.onZoomChanged,
onPageSizeChanged: this.subscriber.onPageSizeChanged,
},
this.setConnection,
this.config.editorId,
Expand Down
2 changes: 2 additions & 0 deletions src/interactions/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface ConfigParameterTypes {
onLayoutsChanged: (layouts: string) => void;
onConnectorEvent: (state: string) => void;
onZoomChanged: (scaleFactor: string) => void;
onPageSizeChanged: (scaleFactor: string) => void;
}

const Connect = (
Expand Down Expand Up @@ -122,6 +123,7 @@ const Connect = (
layoutListChanged: params.onLayoutsChanged,
connectorEvent: params.onConnectorEvent,
zoomChanged: params.onZoomChanged,
pageSizeChanged: params.onPageSizeChanged,
},
}),
);
Expand Down
4 changes: 4 additions & 0 deletions src/tests/__mocks__/MockEditorAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export const mockExitTextEditMode = jest.fn().mockResolvedValue({ success: true,
export const mockResetAnimation = jest.fn().mockResolvedValue({ success: true, status: 0 });
export const mockGetPages = jest.fn().mockResolvedValue({ success: true, status: 0 });
export const mockGetPageById = jest.fn().mockResolvedValue({ success: true, status: 0 });
export const mockSetPageHeight = jest.fn().mockResolvedValue({ success: true, status: 0 });
export const mockSetPageWidth = jest.fn().mockResolvedValue({ success: true, status: 0 });
export const mockGetAnimationsOnSelectedLayout = jest.fn().mockResolvedValue({ success: true, status: 0 });
export const mockGetAnimationByFrameId = jest.fn().mockResolvedValue({ success: true, status: 0 });
export const mockGetAnimationsByLayoutId = jest.fn().mockResolvedValue({ success: true, status: 0 });
Expand Down Expand Up @@ -268,6 +270,8 @@ const MockEditorAPI = {
setTool: mockSetTool,
getPages: mockGetPages,
getPageById: mockGetPageById,
setPageWidth: mockSetPageWidth,
setPageHeight: mockSetPageHeight,
getLogs: mockGetLogs,
enableDebug: mockEnableDebug,
disableDebug: mockDisableDebug,
Expand Down
1 change: 1 addition & 0 deletions src/tests/__mocks__/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ const mockConfig: ConfigType = {
onLayoutsChanged: defaultMockReturn,
onConnectorEvent: defaultMockReturn,
onZoomChanged: defaultMockReturn,
onPageSizeChanged: defaultMockReturn,
};
export default mockConfig;
10 changes: 10 additions & 0 deletions src/tests/controllers/PageController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ beforeEach(() => {
mockedPageProperties = new PageController(MockEditorAPI);
jest.spyOn(mockedPageProperties, 'getPages');
jest.spyOn(mockedPageProperties, 'getPageById');
jest.spyOn(mockedPageProperties, 'setPageWidth');
jest.spyOn(mockedPageProperties, 'setPageHeight');
});

afterAll(() => {
Expand All @@ -20,5 +22,13 @@ describe('PageProperties', () => {
mockedPageProperties.getPageById('4');
expect(mockedPageProperties.getPageById).toHaveBeenCalledTimes(1);
expect(mockedPageProperties.getPageById).toHaveBeenCalledWith('4');

mockedPageProperties.setPageWidth('id', '4');
expect(mockedPageProperties.setPageWidth).toHaveBeenCalledTimes(1);
expect(mockedPageProperties.setPageWidth).toHaveBeenCalledWith('id', '4');

mockedPageProperties.setPageHeight('id', '4');
expect(mockedPageProperties.setPageHeight).toHaveBeenCalledTimes(1);
expect(mockedPageProperties.setPageHeight).toHaveBeenCalledWith('id', '4');
});
});
7 changes: 7 additions & 0 deletions src/tests/controllers/SubscriberContoller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { VariableType } from '../../../types/VariableTypes';

import { ToolType } from '../../utils/enums';
import { ConnectorStateType } from '../../../types/ConnectorTypes';
import { PageSize } from '../../../types/PageTypes';

let mockedSDK: SDK;
let mockedAnimation: FrameAnimationType;
Expand Down Expand Up @@ -39,6 +40,7 @@ beforeEach(() => {
jest.spyOn(mockedSubscribers, 'onConnectorEvent');
jest.spyOn(mockedSubscribers, 'onZoomChanged');
jest.spyOn(mockedSubscribers, 'onActionsChanged');
jest.spyOn(mockedSubscribers, 'onPageSizeChanged');
});

afterEach(() => {
Expand Down Expand Up @@ -124,6 +126,11 @@ describe('Subscriber methods', () => {
mockedSubscribers.onActionsChanged(JSON.stringify(actions));
expect(mockedSDK.config.onActionsChanged).toHaveBeenCalledTimes(18);
expect(mockedSDK.config.onActionsChanged).toHaveBeenCalledWith(actions);

const pageSize: PageSize = { pageId: 'pageId', width: 123, height: 456 };

mockedSubscribers.onPageSizeChanged(JSON.stringify(pageSize));
expect(mockedSDK.config.onPageSizeChanged).toHaveBeenCalledWith(pageSize);
});

it('Should call trigger the SelectedToolChanged subscriber when triggered', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/tests/interactions/connector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe.skip('Editor Link Validator', () => {
onLayoutsChanged: () => null,
onConnectorEvent: () => null,
onZoomChanged: () => null,
onPageSizeChanged: () => null,
},
() => null,
);
Expand Down Expand Up @@ -71,6 +72,7 @@ describe.skip('Editor Link Validator', () => {
onLayoutsChanged: () => null,
onConnectorEvent: () => null,
onZoomChanged: () => null,
onPageSizeChanged: () => null,
},
() => null,
);
Expand Down Expand Up @@ -109,6 +111,7 @@ describe.skip('Editor Link Validator', () => {
onLayoutsChanged: () => null,
onConnectorEvent: () => null,
onZoomChanged: () => null,
onPageSizeChanged: () => null,
},
() => null,
);
Expand Down
2 changes: 2 additions & 0 deletions types/CommonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ParagraphStyle } from './ParagraphStyleTypes';
import { DocumentFont } from './FontTypes';
import { CharacterStyle } from './CharacterStyleTypes';
import { ConnectorEvent } from './ConnectorTypes';
import { PageSize } from './PageTypes';

export type Id = string;

Expand Down Expand Up @@ -40,6 +41,7 @@ export type ConfigType = {
onLayoutsChanged?: (layouts: LayoutListItemType[]) => void;
onConnectorEvent?: (event: ConnectorEvent) => void;
onZoomChanged?: (scaleFactor: number) => void;
onPageSizeChanged?: (pageSize: PageSize) => void;
};

export interface EditorResponse<T> {
Expand Down
8 changes: 7 additions & 1 deletion types/PageTypes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Id } from "./CommonTypes";
import { Id } from './CommonTypes';

export type Page = {
pageId: Id;
pageNumber: number;
width?: number;
height?: number;
};

export type PageSize = {
pageId: Id;
width: number;
height: number;
};

0 comments on commit 466ed95

Please sign in to comment.