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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Screenshotting] PoC print media #126913

Closed
33 changes: 27 additions & 6 deletions packages/kbn-optimizer/src/worker/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ const nodeModulesButNotKbnPackages = (path: string) => {
return !path.includes(`node_modules${Path.sep}@kbn${Path.sep}`);
};

const mediaAwareStyleLoaders = [
{
resourceQuery: /print/,
loader: 'style-loader',
options: {
attributes: { 'data-print-media-style': 'true' },
},
},
{
resourceQuery: undefined,
loader: 'style-loader',
options: {
attributes: { media: 'screen, projection' },
},
},
];

Comment on lines +39 to +55
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The version of style-loader we are using has a known bug for setting the media attribute on style tags webpack-contrib/style-loader#458

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a work-around that instead uses the MutationObserver hacked into core for updating media attributes based on the presence of the data-print-media-style. The result of this is the print styles "flash" once as they are applied by the browser until the MutationObserver kicks in. Very hacky, but work-around for rather not updating style-loader + webpack to latest version.

export function getWebpackConfig(bundle: Bundle, bundleRefs: BundleRefs, worker: WorkerConfig) {
const ENTRY_CREATOR = require.resolve('./entry_point_creator');

Expand Down Expand Up @@ -129,13 +146,15 @@ export function getWebpackConfig(bundle: Bundle, bundleRefs: BundleRefs, worker:
},
],
},
{
test: /\.css$/,
include: /node_modules/,
oneOf: mediaAwareStyleLoaders,
},
{
test: /\.css$/,
include: /node_modules/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
Expand All @@ -144,16 +163,18 @@ export function getWebpackConfig(bundle: Bundle, bundleRefs: BundleRefs, worker:
},
],
},
{
test: /\.scss$/,
exclude: nodeModulesButNotKbnPackages,
oneOf: mediaAwareStyleLoaders,
},
{
test: /\.scss$/,
exclude: nodeModulesButNotKbnPackages,
oneOf: [
...worker.themeTags.map((theme) => ({
resourceQuery: `?${theme}`,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
Expand Down
23 changes: 23 additions & 0 deletions src/core/public/core_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,29 @@ export class CoreSystem {
this.rootDomElement.appendChild(notificationsTargetDomElement);
this.rootDomElement.appendChild(overlayTargetDomElement);

// HACKS: scope all current styles to screen or projection media
const setTargetMedia = (el: HTMLElement) => {
if (el.getAttribute('data-print-media-style') === 'true') {
el.setAttribute('media', 'print');
} else {
el.setAttribute('media', 'screen, projection');
}
};
document.querySelectorAll('link').forEach(setTargetMedia);
document.querySelectorAll('style').forEach(setTargetMedia);
new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (
node instanceof HTMLElement &&
(node.tagName.toLowerCase() === 'link' || node.tagName.toLowerCase() === 'style')
) {
setTargetMedia(node);
}
});
});
}).observe(document.head, { childList: true, subtree: true });

this.rendering.start({
application,
chrome,
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/dashboard/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ import { dashboardFeatureCatalog } from './dashboard_strings';
import { replaceUrlHashQuery } from '../../kibana_utils/public';
import { SpacesPluginStart } from './services/spaces';

import './print.scss?v8light&print';

export interface DashboardFeatureFlagConfig {
allowByValueEmbeddables: boolean;
}
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/dashboard/public/print.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
html {
background-color: rgb(255, 0, 0);
}