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

[Feature] Anchor to page #516

Merged
merged 17 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
69 changes: 69 additions & 0 deletions packages/sdk/src/controllers/FrameController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
AutoGrowDeltaUpdate,
AutoGrowDirection,
AutoGrowResetUpdate,
FrameAnchorType,
FrameAnchorProperties,
AnchorTarget,
} from '../types/FrameTypes';
import { ColorUsage } from '../types/ColorStyleTypes';
import { ShapeType } from '../types/ShapeTypes';
Expand Down Expand Up @@ -836,4 +839,70 @@ export class FrameController {
.resetAutoGrowSettings(id, JSON.stringify(update))
.then((result) => getEditorResponseData<null>(result));
};

private setAnchor = async (
id: Id,
horizontal: boolean,
anchorType: FrameAnchorType,
anchorTarget: AnchorTarget,
endAnchorTarget?: AnchorTarget | null,
) => {
const properties: FrameAnchorProperties = {
horizontal: horizontal,
type: anchorType,
target: anchorTarget,
endTarget: endAnchorTarget,
};

const res = await this.#editorAPI;
return res
.setAnchorProperties(id, JSON.stringify(properties))
.then((result) => getEditorResponseData<null>(result));
};

/**
* @experimental
* This method will set the vertical anchor to target frame on a specified frame.
* @param id the id of the frame that needs to get a frame anchor set
* @param anchorType the type of positioning to be set to the frame
* @param anchorTarget the target frame to which it is anchored
* @param endAnchorTarget the target (end) frame to which it can be anchored
* @returns
*/
setVerticalAnchor = async (
id: Id,
anchorType: FrameAnchorType,
anchorTarget: AnchorTarget,
endAnchorTarget?: AnchorTarget | null,
) => {
return this.setAnchor(id, false, anchorType, anchorTarget, endAnchorTarget);
};

/**
* @experimental
* This method will set the horizontal anchor to target frame on a specified frame.
* @param id the id of the frame that needs to get a frame anchor set
* @param anchorType the type of positioning to be set to the frame
* @param anchorTarget the target frame to which it is anchored
* @param endAnchorTarget the target (end) frame to which it can be anchored
* @returns
*/
setHorizontalAnchor = async (
id: Id,
anchorType: FrameAnchorType,
anchorTarget: AnchorTarget,
endAnchorTarget?: AnchorTarget | null,
) => {
return this.setAnchor(id, true, anchorType, anchorTarget, endAnchorTarget);
};

/**
* This method will reset all anchoring properties of a specified frame to their original values
* @param id the id of the frame that needs to get reset
* @returns
*/
resetAnchoring = async (id: Id) => {
const res = await this.#editorAPI;
return res.resetAnchorProperties(id).then((result) => getEditorResponseData<null>(result));
};
}
49 changes: 49 additions & 0 deletions packages/sdk/src/tests/controllers/FrameController.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { EditorAPI, Id } from '../../types/CommonTypes';
import {
AnchorTargetEdgeType,
AutoGrowDirection,
BlendMode,
FitMode,
FrameAnchorProperties,
FrameAnchorTarget,
FrameAnchorType,
FrameTypeEnum,
ImageSourceTypeEnum,
PageAnchorTarget,
UpdateZIndexMethod,
VerticalAlign,
} from '../../types/FrameTypes';
Expand Down Expand Up @@ -72,6 +77,8 @@ const mockedEditorApi: EditorAPI = {
cancelCropMode: async () => getEditorResponseData(castToEditorResponse(null)),
resetAutoGrowSettings: async () => getEditorResponseData(castToEditorResponse(null)),
updateAutoGrowSettings: async () => getEditorResponseData(castToEditorResponse(null)),
setAnchorProperties: async () => getEditorResponseData(castToEditorResponse(null)),
resetAnchorProperties: async () => getEditorResponseData(castToEditorResponse(null)),
};

beforeEach(() => {
Expand Down Expand Up @@ -128,6 +135,8 @@ beforeEach(() => {
jest.spyOn(mockedEditorApi, 'cancelCropMode');
jest.spyOn(mockedEditorApi, 'resetAutoGrowSettings');
jest.spyOn(mockedEditorApi, 'updateAutoGrowSettings');
jest.spyOn(mockedEditorApi, 'setAnchorProperties');
jest.spyOn(mockedEditorApi, 'resetAnchorProperties');

id = mockSelectFrame.id;
});
Expand Down Expand Up @@ -636,3 +645,43 @@ describe('Auto grow updating', () => {
);
});
});

describe('Anchoring', () => {
it('should be possible to set the vertical anchor settings', async () => {
const anchorType = FrameAnchorType.startAndEnd;
const startTarget = new FrameAnchorTarget('target-id', AnchorTargetEdgeType.end);
const endTarget = new PageAnchorTarget();

await mockedFrameController.setVerticalAnchor(id, anchorType, startTarget, endTarget);
expect(mockedEditorApi.setAnchorProperties).toHaveBeenCalledTimes(1);

const expectedProperties: FrameAnchorProperties = {
horizontal: false,
type: anchorType,
target: startTarget,
endTarget: endTarget,
};
expect(mockedEditorApi.setAnchorProperties).toHaveBeenCalledWith(id, JSON.stringify(expectedProperties));
});

it('should be possible to set the horizontal anchor settings', async () => {
const anchorType = FrameAnchorType.center;
const startTarget = new FrameAnchorTarget('target-id', AnchorTargetEdgeType.start);

await mockedFrameController.setHorizontalAnchor(id, anchorType, startTarget);
expect(mockedEditorApi.setAnchorProperties).toHaveBeenCalledTimes(2);

const expectedProperties: FrameAnchorProperties = {
horizontal: true,
type: anchorType,
target: startTarget,
};
expect(mockedEditorApi.setAnchorProperties).toHaveBeenCalledWith(id, JSON.stringify(expectedProperties));
});

it('should be possible to reset anchor settings', async () => {
await mockedFrameController.resetAnchoring(id);
expect(mockedEditorApi.resetAnchorProperties).toHaveBeenCalledTimes(1);
expect(mockedEditorApi.resetAnchorProperties).toHaveBeenLastCalledWith(id);
});
});
83 changes: 83 additions & 0 deletions packages/sdk/src/types/FrameTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { CornerRadiusAll, CornerRadiusNone, CornerRadiusOnly, ShapeType } from '
export type FrameLayoutType = {
id: Id;
layoutId: Id;
horizontal: FrameAnchor;
vertical: FrameAnchor;
x: PropertyState<number>;
y: PropertyState<number>;
width: PropertyState<number>;
Expand Down Expand Up @@ -274,3 +276,84 @@ export enum UpdateZIndexMethod {
bringForward = 'bringForward',
sendBackward = 'sendBackward',
}

export enum FrameAnchorType {
relative = 'relative',
start = 'start',
end = 'end',
startAndEnd = 'startAndEnd',
center = 'center',
}

export enum AnchorTargetType {
page = 'page',
frame = 'frame',
}

export enum AnchorTargetEdgeType {
start = 'start',
end = 'end',
center = 'center',
}

export class PageAnchorTarget {
type = AnchorTargetType.page;
}

export class FrameAnchorTarget {
id: Id;
edge: AnchorTargetEdgeType;
type = AnchorTargetType.frame;

constructor(id: Id, edge: AnchorTargetEdgeType) {
(this.id = id), (this.edge = edge);
}
Dvergar marked this conversation as resolved.
Show resolved Hide resolved
}

export type AnchorTarget = PageAnchorTarget | FrameAnchorTarget;

export type RelativeFrameAnchor = {
start: PropertyState<number>;
end: PropertyState<number>;
target: AnchorTarget;
type: FrameAnchorType.relative;
};

export type StartFrameAnchor = {
offset: PropertyState<number>;
target: AnchorTarget;
type: FrameAnchorType.start;
};

export type EndFrameAnchor = {
offset: PropertyState<number>;
target: AnchorTarget;
type: FrameAnchorType.end;
};

export type StartAndEndFrameAnchor = {
start: PropertyState<number>;
end: PropertyState<number>;
target: AnchorTarget;
type: FrameAnchorType.startAndEnd;
};

export type CenterFrameAnchor = {
offset: PropertyState<number>;
target: AnchorTarget;
type: FrameAnchorType.center;
};

export type FrameAnchor =
| RelativeFrameAnchor
| StartFrameAnchor
| EndFrameAnchor
| StartAndEndFrameAnchor
| CenterFrameAnchor;

export type FrameAnchorProperties = {
horizontal: boolean;
type: FrameAnchorType;
target: AnchorTarget;
endTarget?: AnchorTarget | null;
};
Loading