Skip to content

Commit 94bf4f9

Browse files
nmetulevbeth-panx
andauthored
[mgt.dev] Added support for importing toolkit exports in stories (#773)
* Changed story rendering from div to iframe and added support for importing mgt exports * removed commented out code * fixed conflict * Apply suggestions from code review Co-authored-by: Beth Pan <[email protected]> Co-authored-by: Beth Pan <[email protected]>
1 parent f3f26ec commit 94bf4f9

File tree

8 files changed

+117
-15
lines changed

8 files changed

+117
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
dist/
22
www/
33
lib/
4+
assets/*.js
45

56
*~
67
*.sw[mnpcod]

.storybook/addons/codeEditorAddon/codeAddon.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { makeDecorator } from '@storybook/addons';
22
import { EditorElement } from './editor';
33

4+
const mgtScriptName = './mgt.storybook.js';
5+
46
// function is used for dragging and moving
57
const setupEditorResize = (first, separator, last, dragComplete) => {
68
var md; // remember mouse down info
@@ -63,14 +65,11 @@ export const withCodeEditor = makeDecorator({
6365
let storyHtml;
6466
const root = document.createElement('div');
6567
let storyElementWrapper = document.createElement('div');
66-
let storyElement;
6768

6869
if (story.strings) {
6970
storyHtml = story.strings[0];
70-
storyElement = document.createElement('div');
7171
} else {
7272
storyHtml = story.innerHTML;
73-
storyElement = story;
7473
}
7574

7675
let scriptMatches = scriptRegex.exec(storyHtml);
@@ -93,8 +92,46 @@ export const withCodeEditor = makeDecorator({
9392
};
9493

9594
editor.addEventListener('fileUpdated', () => {
96-
storyElement.innerHTML = editor.files.html + `<style>${editor.files.css}</style>`;
97-
eval(editor.files.js);
95+
const storyElement = document.createElement('iframe');
96+
97+
storyElement.addEventListener('load', () => {
98+
let doc = storyElement.contentDocument;
99+
100+
let { html, css, js } = editor.files;
101+
js = js.replace(
102+
/import \{([^\}]+)\}\s+from\s+['"]@microsoft\/mgt['"];/gm,
103+
`import {$1} from '${mgtScriptName}';`
104+
);
105+
106+
const docContent = `
107+
<html>
108+
<head>
109+
<script type="module" src="${mgtScriptName}"></script>
110+
<script type="module">
111+
import {Providers, MockProvider} from "${mgtScriptName}";
112+
Providers.globalProvider = new MockProvider(true);
113+
</script>
114+
<style>
115+
${css}
116+
</style>
117+
</head>
118+
<body>
119+
${html}
120+
<script type="module">
121+
${js}
122+
</script>
123+
</body>
124+
</html>
125+
`;
126+
127+
doc.open();
128+
doc.write(docContent);
129+
doc.close();
130+
});
131+
132+
storyElement.className = 'story-mgt-preview';
133+
storyElementWrapper.innerHTML = '';
134+
storyElementWrapper.appendChild(storyElement);
98135
});
99136

100137
const separator = document.createElement('div');
@@ -103,12 +140,10 @@ export const withCodeEditor = makeDecorator({
103140

104141
root.className = 'story-mgt-root';
105142
storyElementWrapper.className = 'story-mgt-preview-wrapper';
106-
storyElement.className = 'story-mgt-preview';
107143
separator.className = 'story-mgt-separator';
108144
editor.className = 'story-mgt-editor';
109145

110146
root.appendChild(storyElementWrapper);
111-
storyElementWrapper.appendChild(storyElement);
112147
root.appendChild(separator);
113148
root.appendChild(editor);
114149

.storybook/manager.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { addons, types } from '@storybook/addons';
1010
import { STORIES_CONFIGURED, STORY_MISSING } from '@storybook/core-events';
1111
import { AddonPanel } from '@storybook/components';
1212
import { useParameter, useChannel } from '@storybook/api';
13-
import { MsalProvider } from '../packages/mgt/dist/commonjs';
13+
import { MsalProvider } from '../packages/mgt/dist/es6';
1414
import { Providers, LoginType } from '../packages/mgt-element/dist';
1515
import { CLIENTID, GETPROVIDER_EVENT, SETPROVIDER_EVENT } from './env';
1616

@@ -54,8 +54,8 @@ const SignInPanel = () => {
5454
{_allow_signin ? (
5555
<mgt-login />
5656
) : (
57-
'All components are using mock data - sign in function will be available in a future release'
58-
)}
57+
'All components are using mock data - sign in function will be available in a future release'
58+
)}
5959
</div>
6060
);
6161
};

.storybook/preview-body.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@
2020
}
2121

2222
.story-mgt-preview {
23-
margin: 0.5em;
23+
height: 100%;
24+
width: 100%;
25+
margin: 0;
26+
border: none;
27+
background: none;
28+
padding: 0;
2429
}
2530

2631
.story-mgt-preview-wrapper {
2732
position: relative;
2833
height: 100%;
2934
width: 50%;
3035
float: left;
31-
overflow: auto;
3236
background-color: #ffffff;
3337
}
3438

.storybook/rollup.config.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import resolve from 'rollup-plugin-node-resolve';
2+
import babel from 'rollup-plugin-babel';
3+
import postcss from 'rollup-plugin-postcss';
4+
import { terser } from 'rollup-plugin-terser';
5+
6+
const extensions = ['.js'];
7+
8+
const getBabelConfig = isEs5 => {
9+
return {
10+
plugins: [
11+
['@babel/plugin-proposal-decorators', isEs5 ? { legacy: true } : { decoratorsBeforeExport: true, legacy: false }],
12+
['@babel/proposal-class-properties', { loose: isEs5 }],
13+
'@babel/proposal-object-rest-spread'
14+
],
15+
include: [
16+
'packages/**/*',
17+
'node_modules/lit-element/**/*',
18+
'node_modules/lit-html/**/*',
19+
'node_modules/@microsoft/microsoft-graph-client/lib/es/**/*',
20+
'node_modules/msal/lib-es6/**/*'
21+
]
22+
};
23+
};
24+
25+
const es6Bundle = {
26+
input: ['./packages/mgt/dist/es6/index.js'],
27+
output: {
28+
dir: 'assets',
29+
entryFileNames: 'mgt.storybook.js',
30+
format: 'esm',
31+
},
32+
plugins: [
33+
babel({
34+
extensions,
35+
...getBabelConfig(false)
36+
}),
37+
resolve({ module: true, jsnext: true, extensions }),
38+
postcss(),
39+
terser({ keep_classnames: true, keep_fnames: true })
40+
]
41+
};
42+
43+
export default [es6Bundle];

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
],
1111
"scripts": {
1212
"build": "npm run prettier:check && npm run clean && lerna run build --scope @microsoft/*",
13+
"build:compile": "npm run prettier:check && npm run clean && lerna run build:compile --scope @microsoft/*",
1314
"build:mgt": "cd ./packages/mgt && npm run build",
1415
"build:mgt-element": "cd ./packages/mgt-element && npm run build",
1516
"build:mgt-react": "lerna run build --scope @microsoft/mgt-react",
@@ -31,9 +32,10 @@
3132
"prettier:check": "npm run prettier:base -- --list-different \"packages/**/*.{ts,tsx}\"",
3233
"prettier:write": "npm run prettier:base -- --write \"packages/**/*.{ts,tsx}\"",
3334
"storybook": "start-storybook -p 6006 -s assets",
34-
"storybook:dev": "npm run build && wca analyze packages --format json --outFile custom-elements.json",
35-
"storybook:build": "build-storybook -s assets && cpx .storybook/CNAME storybook-static",
36-
"storybook:deploy": "npm run storybook:build && storybook-to-ghpages -e storybook-static"
35+
"storybook:dev": "npm run build:compile && npm run storybook:bundle && wca analyze packages --format json --outFile custom-elements.json",
36+
"storybook:build": "npm run storybook:dev && build-storybook -s assets && cpx .storybook/CNAME storybook-static",
37+
"storybook:deploy": "npm run storybook:build && storybook-to-ghpages -e storybook-static",
38+
"storybook:bundle": "rollup -c ./.storybook/rollup.config.js"
3739
},
3840
"storybook-deployer": {
3941
"gitUsername": "@microsoft/mgt",

packages/mgt-element/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export * from './utils/TemplateContext';
2323
export * from './utils/TemplateHelper';
2424
export * from './utils/GraphPageIterator';
2525
export * from './utils/LocalizationHelper';
26+
export * from './mock/MockProvider';
27+
export * from './mock/mgt-mock-provider';

stories/samples/templating.stories.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,18 @@ export const TeamsMessages = () => html`
371371
}
372372
</style>
373373
`;
374+
375+
export const ChangeBindingSyntax = () => html`
376+
<mgt-agenda>
377+
<template data-type="event">
378+
<div>
379+
[[event.subject]]
380+
</div>
381+
</template>
382+
</mgt-agenda>
383+
<script>
384+
import { TemplateHelper } from '@microsoft/mgt';
385+
386+
TemplateHelper.setBindingSyntax('[[', ']]');
387+
</script>
388+
`;

0 commit comments

Comments
 (0)