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

fix bug && add new feature #1

Merged
merged 5 commits into from Aug 9, 2017
Merged

fix bug && add new feature #1

merged 5 commits into from Aug 9, 2017

Conversation

xqin
Copy link

@xqin xqin commented Aug 8, 2017

  • 修复 rm 没有加 -f 参数, 当build目录下没有 *.js 文件时会报错的问题.
  • URL的参数值增加 encodeURIComponent, 以修复当执行的合集有&的字符时, &被当成URL参数分隔符的问题.
  • 增加对方向键(/)的支持, 当触发按键时, 自动从 history 中取对应的 cmd 填充至当前的输入框中.
  • 增加 clear 命令, 用于清除目前已展示的历史数据.
  • 调整 UI 使其更像一个应用程序窗口.
  • 增加滚动条, 以解决当输出内容过多时, 无法查看之前的输出内容的问题.
  • 当在没有输入内容的情况下按回车时, 不向服务器发送请求.
  • 优化 addHistory 方法, 使其支持批量添加数据, 减少调用 addHistory 的次数.
  • 修复 当输出的内容为 json 格式时, 将 res.data 直接 + '' 会导致结果变为 [object Object], 而非真正的内容的问题. (比如执行这个命令: cat /var/fc/runtime/nodejs4.4/package.json)

@@ -1,11 +1,14 @@
var React = require('react');
var axios = require('axios');

const Prompt = 'fc@aliyun $ ';
const ShellApi = 'http://api.rockuw.com/shell';
var Prompt = 'fc@aliyun $ ';
Copy link

Choose a reason for hiding this comment

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

这个为啥要改成var?

Copy link
Author

Choose a reason for hiding this comment

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

因为按你目前仓库里的配置, 我用的 node v8.1.2 加上 browserify v14.4.0 在进行转换的时候会报错(操作系统是Windows7 x64).

ERROR: Unexpected token: keyword (const)

@@ -17,7 +20,9 @@ var App = React.createClass({
execShellCommand: function(cmd) {
var that = this;
that.setState({'prompt': ''})
axios.get(ShellApi+'?cmd='+cmd).then(function (res) {
that.offset = 0
that.cmds.push(cmd)
Copy link

Choose a reason for hiding this comment

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

这里让that.offset = that.cmds.length;

Copy link
Author

@xqin xqin Aug 9, 2017

Choose a reason for hiding this comment

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

that.offset = 0 是为了重置偏移值为最后面, 因为往上的时候 是 this.cmds.length - ++this.offset.
如果写成 that.offset = that.cmds.length 这样, 效果就不一对了.

this.clearInput();
break
case 'ArrowUp':
if (this.offset === 0) {
Copy link

Choose a reason for hiding this comment

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

这个逻辑可以简化一下:

this.offset = Math.max(this.offset-1, -1);
this.refs.term.getDOMNode().value = this.cmds[this.offset] || '';
return false;

Copy link
Author

@xqin xqin Aug 9, 2017

Choose a reason for hiding this comment

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

详细的解释见 ArrowDown 处提出的疑问处的回答.

src/app.js Outdated
this.refs.term.getDOMNode().value = this.cmds[this.cmds.length - ++this.offset] || this.cmds[(this.offset = this.cmds.length, 0)] || this.lastCmd
return false
case 'ArrowDown':
var history = this.state.history.slice(1)
Copy link

Choose a reason for hiding this comment

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

这里可以变成:

this.offset = Math.min(this.offset+1, this.cmds.length);
this.refs.term.getDOMNode().value = this.cmds[this.offset] || '';
return false;

Copy link
Author

@xqin xqin Aug 9, 2017

Choose a reason for hiding this comment

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

根据你上面提出的 offset 使用的修改, 我自己在我本地测试了一下, 修改之后, 有以下几个问题:

  1. 在输入命令执行之后, 在输入框中按箭头(多按几次), 回到第一次输入的命令之后, 再按会变成空. 而正常的 bash 中在达到 历史记录中的第一个的时候,再按 是保持不变的(即显示的还是第一个命令).

  2. 在有输入命令,但没有按回车的情况下, 按再按, 之前输入的内容就没有了(因没有保存用户现有输入内容的逻辑).

  3. 在有输入命令,但没按回车的情况下, 按 箭头, 之前输入的内容变成空了.

而我现在所提交的代码是没有上述问题的.

Copy link

Choose a reason for hiding this comment

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

OK

@@ -91,7 +110,7 @@ var App = React.createClass({
{output}
<p>
<span className="prompt">{this.state.prompt}</span>
<input type="text" onKeyPress={this.handleInput} ref="term" />
<input type="text" onKeyDown={this.handleInput} ref="term" />
Copy link

Choose a reason for hiding this comment

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

这里为啥改成onKeyDown

Copy link
Author

Choose a reason for hiding this comment

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

因为用 onKeyPress 事件是接收不到 ArrowUp/ArrowDown 的, 需要改为 onKeyDown 才可以.

POC:
run this command `cat /var/fc/runtime/nodejs4.4/package.json`
while get `[object Object]`, and it's wrong.
@rockuw
Copy link

rockuw commented Aug 9, 2017

Thanks.

@rockuw rockuw merged commit d04a6de into awesome-fc:master Aug 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants