-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Screenshotting] Add custom context support in the screenshot mode #124942
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c277eb3
Fix screenshot mode implementation typings
dokmic 092c68c
Add custom context support to the screenshot mode plugin
dokmic ccd5e04
Delegate custom context handling to the screenshot mode plugin
dokmic 0c88fb9
Move screenshot layout flag to the custom context
dokmic 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,30 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import { getScreenshotContext, setScreenshotContext } from './context'; | ||
|
|
||
| describe('getScreenshotContext', () => { | ||
| it('should return a default value if there is no data', () => { | ||
| expect(getScreenshotContext('key', 'default')).toBe('default'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('setScreenshotContext', () => { | ||
| it('should store data in the context', () => { | ||
| setScreenshotContext('key', 'value'); | ||
|
|
||
| expect(getScreenshotContext('key')).toBe('value'); | ||
| }); | ||
|
|
||
| it('should not overwrite data on repetitive calls', () => { | ||
| setScreenshotContext('key1', 'value1'); | ||
| setScreenshotContext('key2', 'value2'); | ||
|
|
||
| expect(getScreenshotContext('key1')).toBe('value1'); | ||
| }); | ||
| }); |
This file contains hidden or 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,53 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| // **PLEASE NOTE** | ||
| // The functionality in this file targets a browser environment AND is intended to be used both in public and server. | ||
| // For instance, reporting uses these functions when starting puppeteer to set the current browser into "screenshot" mode. | ||
|
|
||
| type Context = Record<string, unknown>; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| __KBN_SCREENSHOT_CONTEXT__?: Context; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Stores a value in the screenshotting context. | ||
| * @param key Context key to set. | ||
| * @param value Value to set. | ||
| */ | ||
| export function setScreenshotContext<T = unknown>(key: string, value: T): void { | ||
| // Literal value to prevent adding an external reference | ||
| const KBN_SCREENSHOT_CONTEXT = '__KBN_SCREENSHOT_CONTEXT__'; | ||
|
|
||
| if (!window[KBN_SCREENSHOT_CONTEXT]) { | ||
| Object.defineProperty(window, KBN_SCREENSHOT_CONTEXT, { | ||
| enumerable: true, | ||
| writable: true, | ||
| configurable: false, | ||
| value: {}, | ||
| }); | ||
| } | ||
|
|
||
| window[KBN_SCREENSHOT_CONTEXT]![key] = value; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves a value from the screenshotting context. | ||
| * @param key Context key to get. | ||
| * @param defaultValue Value to return if the key is not found. | ||
| * @return The value stored in the screenshotting context. | ||
| */ | ||
| export function getScreenshotContext<T = unknown>(key: string, defaultValue?: T): T | undefined { | ||
| // Literal value to prevent adding an external reference | ||
| const KBN_SCREENSHOT_CONTEXT = '__KBN_SCREENSHOT_CONTEXT__'; | ||
|
|
||
| return (window[KBN_SCREENSHOT_CONTEXT]?.[key] as T) ?? defaultValue; | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -17,6 +17,5 @@ | |
| "navigation", | ||
| "screenshotMode", | ||
| "share" | ||
| ], | ||
| "requiredBundles": ["screenshotting"] | ||
| ] | ||
| } | ||
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.