-
-
Notifications
You must be signed in to change notification settings - Fork 16
コンピュータにルビーのコードを保存するの機能を実装しました #115
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import bindAll from 'lodash.bindall'; | ||
| import PropTypes from 'prop-types'; | ||
| import React from 'react'; | ||
| import {connect} from 'react-redux'; | ||
| import {projectTitleInitialState} from '../reducers/project-title'; | ||
|
|
||
| class RubyDownloader extends React.Component { | ||
| constructor (props) { | ||
| super(props); | ||
| bindAll(this, [ | ||
| 'downloadProject' | ||
| ]); | ||
| } | ||
| saveRuby () { | ||
| const code = `require "smalruby3"\n\n${this.props.rubyCode}`; | ||
| return new Blob([code], { | ||
| type: 'text/x-ruby-script' | ||
| }); | ||
| } | ||
| downloadProject () { | ||
| const downloadLink = document.createElement('a'); | ||
| document.body.appendChild(downloadLink); | ||
|
|
||
| const content = this.saveRuby(); | ||
| if (this.props.onSaveFinished) { | ||
| this.props.onSaveFinished(); | ||
| } | ||
| // Use special ms version if available to get it working on Edge. | ||
| if (navigator.msSaveOrOpenBlob) { | ||
| navigator.msSaveOrOpenBlob(content, this.props.projectFilename); | ||
| return; | ||
| } | ||
|
|
||
| const url = window.URL.createObjectURL(content); | ||
| downloadLink.href = url; | ||
| downloadLink.download = this.props.projectFilename; | ||
| downloadLink.click(); | ||
| window.URL.revokeObjectURL(url); | ||
| document.body.removeChild(downloadLink); | ||
| } | ||
| render () { | ||
| const { | ||
| children | ||
| } = this.props; | ||
| return children( | ||
| this.downloadProject | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const getProjectFilename = (curTitle, defaultTitle) => { | ||
| let filenameTitle = curTitle; | ||
| if (!filenameTitle || filenameTitle.length === 0) { | ||
| filenameTitle = defaultTitle; | ||
| } | ||
| return `${filenameTitle.substring(0, 100)}.rb`; | ||
| }; | ||
|
|
||
| RubyDownloader.propTypes = { | ||
| children: PropTypes.func, | ||
| onSaveFinished: PropTypes.func, | ||
| projectFilename: PropTypes.string, | ||
| rubyCode: PropTypes.string | ||
| }; | ||
|
|
||
| const mapStateToProps = state => ({ | ||
| projectFilename: getProjectFilename(state.scratchGui.projectTitle, projectTitleInitialState), | ||
| rubyCode: state.scratchGui.rubyCode.code | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このままだと、rubyCodeが更新されていないと古いままのスクリプトが出力されてしまいますね。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
はい、理解しています。じっくりいきましょう。これが武本さんの最後のタスクになると思います。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 最終的にはこの方法はよくないのですが、とりいそぎにやり方です。 プログラムで、ステージ、スプライトの先頭から末尾、というように順番に選択して、選択するたびに、 RubyDownloader の内部で vm の targets の blocks を使って、rubyCode を出力しないといけないのですが、 blocks から直接 ruby のコードを生成できるようにするのは次の段階とします。 scratch-vm のコードを読む限り、 scratch-blocks の block からコードを生成することよりも簡単そうなんだけど、Generatorが使えないけど、僕の方でなんとかする。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ステージやスプライトを変更すると、少なくともundoがリセットされるという不都合があるため、最終的には、この方法は採用しません。 |
||
| }); | ||
|
|
||
| export default connect( | ||
| mapStateToProps, | ||
| () => ({}) // omit dispatch prop | ||
| )(RubyDownloader); | ||
Uh oh!
There was an error while loading. Please reload this page.