-
Notifications
You must be signed in to change notification settings - Fork 350
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
IframeContentRenderer: formalize iframe communication #1565
Open
jeremywiebe
wants to merge
13
commits into
feature/editor-preview-cleanup
Choose a base branch
from
jer/editor-preview-11
base: feature/editor-preview-cleanup
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
921bc54
IframeContentRenderer: introduce message types and helper functions t…
jeremywiebe ea7e30c
Changeset
jeremywiebe bc9fddd
Add tests
jeremywiebe ff553e8
Make mobile emulation prop explicit instead of single-use and vague d…
jeremywiebe 9c754f0
Remove debugging code
jeremywiebe d4da0f7
Formalize accessing url params for iframe further
jeremywiebe d21f95d
Update snapshots
jeremywiebe c1b5e13
Cleanup lint suppressions
jeremywiebe ecfae49
Accept URLSearchParams on getIframeParameter()
jeremywiebe ad1e516
Accept Record<string, string> on getIframeParameter()
jeremywiebe b703ee6
Stronly type data-changed message
jeremywiebe 7c0b92c
Type fixes
jeremywiebe 12ecccd
Don't throw for unhandled messages
jeremywiebe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/perseus-editor": major | ||
--- | ||
|
||
IframeContentRenderer: introduce message types and helper functions to formalize iframe communication |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/perseus-editor/src/__tests__/iframe-utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import {getIframeParameter, setIframeParameter} from "../iframe-utils"; | ||
|
||
describe("iframe-utils", () => { | ||
it("should set parameter on URL", () => { | ||
// Arrange | ||
const url = new URL("https://www.example.com/path/to/preview"); | ||
|
||
// Act | ||
setIframeParameter(url, "emulate-mobile", "true"); | ||
|
||
// Assert | ||
expect(url.toString()).toBe( | ||
"https://www.example.com/path/to/preview?emulate-mobile=true", | ||
); | ||
}); | ||
|
||
it("should not duplicate parameter if set multiple times", () => { | ||
// Arrange | ||
const url = new URL("https://www.example.com/path/to/preview"); | ||
|
||
// Act | ||
setIframeParameter(url, "emulate-mobile", "true"); | ||
setIframeParameter(url, "emulate-mobile", "false"); | ||
|
||
// Assert | ||
expect(url.toString()).toBe( | ||
"https://www.example.com/path/to/preview?emulate-mobile=false", | ||
); | ||
}); | ||
|
||
it("should set all eligible parameters", () => { | ||
// Arrange | ||
const url = new URL("https://www.example.com/path/to/preview"); | ||
|
||
// Act | ||
setIframeParameter(url, "frame-id", "100"); | ||
setIframeParameter(url, "emulate-mobile", "true"); | ||
setIframeParameter(url, "lint-gutter", "true"); | ||
|
||
// Assert | ||
expect(url.toString()).toBe( | ||
"https://www.example.com/path/to/preview?frame-id=100&emulate-mobile=true&lint-gutter=true", | ||
); | ||
}); | ||
}); | ||
|
||
describe("getIframeParameter", () => { | ||
it("should get parameter from url string", () => { | ||
// Arrange | ||
const url = "https://www.example.com/path/to/preview?frame-id=100"; | ||
|
||
// Act | ||
const value = getIframeParameter(url, "frame-id"); | ||
|
||
// Assert | ||
expect(value).toBe("100"); | ||
}); | ||
|
||
it("should get parameter from url object", () => { | ||
// Arrange | ||
const url = new URL( | ||
"https://www.example.com/path/to/preview?frame-id=100", | ||
); | ||
|
||
// Act | ||
const value = getIframeParameter(url, "frame-id"); | ||
|
||
// Assert | ||
expect(value).toBe("100"); | ||
}); | ||
|
||
it("should get parameter from search params", () => { | ||
// Arrange | ||
const searchParams = new URLSearchParams("?frame-id=100"); | ||
|
||
// Act | ||
const value = getIframeParameter(searchParams, "frame-id"); | ||
|
||
// Assert | ||
expect(value).toBe("100"); | ||
}); | ||
|
||
it("should get parameter from generic key/value pairs", () => { | ||
// Arrange | ||
const searchParams = {"frame-id": "100", "ignored-param": "unkonwn"}; | ||
|
||
// Act | ||
const value = getIframeParameter(searchParams, "frame-id"); | ||
|
||
// Assert | ||
expect(value).toBe("100"); | ||
}); | ||
|
||
it("should return null if parameter not set", () => { | ||
// Arrange | ||
const url = new URL("https://www.example.com/path/to/preview"); | ||
|
||
// Act | ||
const value = getIframeParameter(url, "frame-id"); | ||
|
||
// Assert | ||
expect(value).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only ever passed "mobile" here, so I've encoded that as a single prop instead of this generic key/value pair. This will make it easier to understand on the consuming side also (webapp).