Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/components/menu-bar/menu-bar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import ProjectTitleInput from './project-title-input.jsx';
import AccountNav from '../../containers/account-nav.jsx';
import LoginDropdown from './login-dropdown.jsx';
import SB3Downloader from '../../containers/sb3-downloader.jsx';
import RubyDownloader from '../../containers/ruby-downloader.jsx';
import DeletionRestorer from '../../containers/deletion-restorer.jsx';
import TurboMode from '../../containers/turbo-mode.jsx';

Expand Down Expand Up @@ -373,18 +374,17 @@ class MenuBar extends React.Component {
/>
</MenuItem>
)}</SB3Downloader>
<MenuItemTooltip
id="download_ruby_code"
isRtl={this.props.isRtl}
>
<MenuItem>
<RubyDownloader>{downloadProject => (
<MenuItem
onClick={this.handleCloseFileMenuAndThen(downloadProject)}
>
<FormattedMessage
defaultMessage="Download Ruby code to your computer"
defaultMessage="Save Ruby code to your computer"
description="Menu bar item for downloading Ruby code to your computer"
id="gui.smalruby3.menuBar.downloadRubyCodeToComputer"
/>
</MenuItem>
</MenuItemTooltip>
)}</RubyDownloader>
</MenuSection>
</MenuBarMenu>
</div>
Expand Down
74 changes: 74 additions & 0 deletions src/containers/ruby-downloader.jsx
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

Choose a reason for hiding this comment

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

このままだと、rubyCodeが更新されていないと古いままのスクリプトが出力されてしまいますね。
ここでは、rubyCodeを使わずに、RubyDownloader の内部で vm の targets の blocks を使って、rubyCode を出力しないといけないだろうな。

Choose a reason for hiding this comment

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

workspaceからしか変換したことがないので、少し時間が掛かってしまうかもしれません。

はい、理解しています。じっくりいきましょう。これが武本さんの最後のタスクになると思います。

Choose a reason for hiding this comment

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

最終的にはこの方法はよくないのですが、とりいそぎにやり方です。

プログラムで、ステージ、スプライトの先頭から末尾、というように順番に選択して、選択するたびに、 workspaceToCode を呼び出します。
そうすると、ステージとすべてのスプライトの rubyCode が得られます。

RubyDownloader の内部で vm の targets の blocks を使って、rubyCode を出力しないといけないのですが、 blocks から直接 ruby のコードを生成できるようにするのは次の段階とします。 scratch-vm のコードを読む限り、 scratch-blocks の block からコードを生成することよりも簡単そうなんだけど、Generatorが使えないけど、僕の方でなんとかする。

Choose a reason for hiding this comment

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

ステージやスプライトを変更すると、少なくともundoがリセットされるという不都合があるため、最終的には、この方法は採用しません。

});

export default connect(
mapStateToProps,
() => ({}) // omit dispatch prop
)(RubyDownloader);