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

Code injector addon to dynamically inject code in the iFrame #4152

Closed
wants to merge 7 commits into from
Closed

Code injector addon to dynamically inject code in the iFrame #4152

wants to merge 7 commits into from

Conversation

nm123github
Copy link
Contributor

@nm123github nm123github commented Sep 10, 2018

Issue: At this point i don't believe there is a way to add remove css resources dynamically in a running storybook instance. I think there might be use-cases where folks want to check if a component works fine in different versions of a css/js library.

What I did

GIF demo
There is still work to be done on this. Just wanted to get some preliminary comments/feedback. Also, it probably wont be trivial to swap in/out javascript versions so mostly this will probably work with just css (at-least to begin with).

Updated

Updated PR with new code and details below (in comments). Please ignore ^ initial details and demo.

@Hypnosphi
Copy link
Member

Makes sense to me

@Hypnosphi
Copy link
Member

Please add some docs on usage

}

static getDerivedStateFromProps(props) {
return { resources: props.resources };
Copy link
Member

@Hypnosphi Hypnosphi Sep 22, 2018

Choose a reason for hiding this comment

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

it's called on every setState in React 16.4+. So state.resources will always be the same as props.resources

@nm123github
Copy link
Contributor Author

Thanks @Hypnosphi, i'm probably going to change a lot in here. but thanks for the initial feedback.

@ndelangen
Copy link
Member

Hey Nevilla, I know others are requesting a similar addon.

Think you'd have time to work on this more or share the work with someone else?

@nm123github
Copy link
Contributor Author

Hey @ndelangen , let me try to finish or atleast get it to a point where someone else could take over 👍. Let me know if you have any deadlines as far as releases go..

@ndelangen
Copy link
Member

No deadlines apply to new addons.

@ndelangen ndelangen self-assigned this Oct 14, 2018
@codecov
Copy link

codecov bot commented Oct 14, 2018

Codecov Report

Merging #4152 into next will decrease coverage by 0.35%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##             next    #4152      +/-   ##
==========================================
- Coverage   35.23%   34.88%   -0.36%     
==========================================
  Files         564      567       +3     
  Lines        6924     6994      +70     
  Branches      929      937       +8     
==========================================
  Hits         2440     2440              
- Misses       3992     4054      +62     
- Partials      492      500       +8
Impacted Files Coverage Δ
addons/codeinjector/src/register.js 0% <0%> (ø)
addons/codeinjector/src/CodeinjectorPanel.js 0% <0%> (ø)
addons/codeinjector/register.js 0% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cd02d36...a4fe2c7. Read the comment docs.

@ndelangen ndelangen assigned nm123github and unassigned ndelangen Oct 14, 2018
@nm123github
Copy link
Contributor Author

nm123github commented Oct 20, 2018

  • Quick demo available here.
  • Allow users to add/remove scripts and css in the iframe at runtime 🏃
  • Allow users to add a default
addDecorator(
  withResources({
    resources: `<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"></link>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"></link>`,
  })
);
  • Resources added/removed will be sticky (meaning they will persist across stories).
  • Resources added/removed via the add-on will not change any of the resources added statically (via preview-head.html).
  • Remember, in some cases (like adding event listeners on components) you might need to re-apply resources for the story!

Copy link
Member

@igor-dv igor-dv left a comment

Choose a reason for hiding this comment

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

);

storiesOf('Addons|Resources', module)
.add('Primary Large Button', myButton('Primary Large Button', 'btn btn-lg btn-primary'))
Copy link
Member

Choose a reason for hiding this comment

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

Why do you use this strange components syntax? I didn't see it in use in the other stories... I think we need to be consistent with the examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to make it consistent with some of the other examples i see..

name: 'withResources',
parameterName: 'resources',
skipIfNoParametersOrOptions: true,
allowDeprecatedUsage: true,
Copy link
Member

Choose a reason for hiding this comment

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

should we?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably not. changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably not (& changed to false).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably not (& changed to false).

import PropTypes from 'prop-types';
import { Button } from '@storybook/components';
import styled from '@emotion/styled';
import AceEditor from 'react-ace';
Copy link
Member

Choose a reason for hiding this comment

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

Note for the future: I think we need to stick to a single editor in all these code highlighting components.

  • Storysource has now a react-syntax-highlighter which is not an editor thought,
  • Here it's an AceEditor
  • IMO @libetl is adding another one

Copy link
Member

Choose a reason for hiding this comment

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

We are not yet decided whether to add codemirror or monaco for the moment.
The change will probably happen in the tech/overhaul-ui branch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, in future we can change it to whatever we decide on.

/>
</div>
<div style={{ float: 'right', 'padding-top': '17px' }}>
<Button onClick={this.apply.bind(this)}>APPLY RESOURCES</Button>
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to have a "Disable" button ? Looks like it's needed to delete everything from the editor to disable the resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added one.

loadResources() {
const { resources = `` } = this.state;

const addElements = (domElements, i) => {
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this function could be extracted, it doesn't use any inner state from the loadResources func

@@ -15,6 +15,7 @@
|[links](addons/links) |+|+|+|+|+|+|+| |+|+|+|
|[notes](addons/notes) |+|+*|+|+|+|+|+| |+|+|+|
|[options](addons/options) |+|+|+|+|+|+|+| |+|+|+|
|[resources](addons/resources) |+|+|+|+|+|+|+|+|+|+|+|
Copy link
Member

Choose a reason for hiding this comment

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

I can assume that RN is not supported

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated.

@ndelangen ndelangen added this to the next milestone Oct 23, 2018
@igor-dv
Copy link
Member

igor-dv commented Oct 23, 2018

@nm123github, you need to update the snapshots

@igor-dv
Copy link
Member

igor-dv commented Oct 23, 2018

IDK how but looks like svelteshots test is also broken =/

@nm123github
Copy link
Contributor Author

Just had a brief chat with @shilman . This will go in 4.1 (so lets not merge for now).

@ndelangen
Copy link
Member

Yeah that's what I feel is best. also.

Let's release 4.0.0 first.

@nm123github nm123github changed the title Resources addon to dynamically add/remove css Code injector addon to dynamically inject code in the iFrame Oct 30, 2018
@Hypnosphi Hypnosphi changed the base branch from master to next November 5, 2018 17:04
@igor-dv
Copy link
Member

igor-dv commented Nov 13, 2018

@nm123github is anything missing here ?

@nm123github
Copy link
Contributor Author

Adding css is probably a much more common use case which should be covered in the addon-cssresources. Also, looks like there's a PR being worked on to integrate monaco-editor so in anycase probably best to wait for that.

Maybe will resurrect this sometime in future if there's a need.

@nm123github nm123github closed this Dec 1, 2018
@wildeyes
Copy link

wildeyes commented Apr 1, 2020

@nm123github what is the PR that integrates monaco-editor?

@ndelangen
Copy link
Member

@playma256 has been working on a tiny lightweight story editor, but focus has shifted a bit. The storyState was changed for Args & my time was spend on the composition feature.

I'm sure we'll work on a true editor within storybook Q2.

The status is "on hold"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants