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

Add source editor component #4419

Merged
merged 23 commits into from
Jan 11, 2018
Merged

Add source editor component #4419

merged 23 commits into from
Jan 11, 2018

Conversation

edmundoa
Copy link
Contributor

This PR moves the source code editor we used in the pipelines to the web interface, so other plugins can reuse it if they need it.

I manage to make a few improvements in there, hopefully they will improve the experience of using it:

Bonus: Add dark theme support!
screen shot 2017-12-13 at 17 36 42

*Dispatching a pasting event from JS doesn't really work for privacy reasons, so the user must trigger an action in the OS. The button therefore only shows a small tooltip indicating it can be done in the Edit menu.

@edmundoa edmundoa added this to the 3.0.0 milestone Dec 13, 2017
@ghost ghost assigned edmundoa Dec 13, 2017
@edmundoa
Copy link
Contributor Author

PS: For now the dark theme is only supported when specified via props. I guess we can do something better with that once we do some other improvements 🙂

@edmundoa edmundoa removed their assignment Dec 13, 2017
@ghost ghost assigned edmundoa Dec 13, 2017
@edmundoa edmundoa removed their assignment Dec 13, 2017
@kroepke kroepke self-requested a review December 19, 2017 07:56
Copy link
Member

@kroepke kroepke left a comment

Choose a reason for hiding this comment

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

Apart from the questions and minor glitch in the docs, me gusta!

* `[{ row: 0, column: 2, type: 'error', text: 'Some error.'}]`
* The type value must be one of `error`, `warning`, or `info`.
*/
annotations: PropTypes.array,
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth using a PropTypes.shape here or do we not bother because this typically comes from the backend anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm most of the time against those shape objects, as you need to define the whole schema of an object (or an array of objects in this case) in a way that is not very readable and it's easy to make you waste more time removing warnings than helping catching errors. Additionally, in this case the shape is highly dependant on ace, and they don't even have good documentation for it. For instance, the column property didn't seem to have any effect when I tested it, as all annotations appear in the gutter on the right of the editor. I also had to go to the source code to see what types were supported.
In my opinion, having an example and the vales the type property accepts is good enough.

Copy link
Member

Choose a reason for hiding this comment

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

Works for me :)

editorProps={{ $blockScrolling: 'Infinity' }}
focus={this.props.focus}
fontSize={this.props.fontSize}
mode="text"
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to extract this into an optional prop, too?
Might come in handy if we ever use this to display/edit JSON or other modes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main problem is that each mode requires an import from brace. I couldn't find a way of importing all the modes brace provides, and I'm unsure if it's even safe to do so. I can play a bit more with it and see how it goes. Otherwise I would vote for extending the component when we need it.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah makes sense, let's not overcomplicated it for now.

value={code} />
```

Non-resizable editor without toolbar and with custom height and width:
Copy link
Member

Choose a reason for hiding this comment

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

States that the editor isn't resizable, when in fact it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was me changing props and texts last minute 😅 Will fix it.

@ghost ghost assigned edmundoa Dec 20, 2017
@edmundoa
Copy link
Contributor Author

@kroepke addressed your comments and other things I saw:

  • Now we support JSON, Lua, Markdown, and YAML languages with the mode prop
  • Improved the logic to disabled copy and paste buttons
  • Avoided resizing editor and text selection when they don't make sense
  • Added a note regarding uncontrolled usages of the component, also updated examples to always keep the state themselves. React-Ace is supposed to keep the text value if you don't update it by hand, but that seems to fail in some situations

@edmundoa
Copy link
Contributor Author

edmundoa commented Jan 2, 2018

I rebased this against master and also removed the hack to reset the undo history. The issue was fixed upstream.

@edmundoa
Copy link
Contributor Author

edmundoa commented Jan 2, 2018

The failing test is unrelated to the changes, but to the new year 🎉 I'll submit another PR fixing it.

@edmundoa edmundoa removed their assignment Jan 2, 2018
Edmundo Alvarez added 14 commits January 2, 2018 13:10
This is unfortunately not possible when setting `width` to `Infinity`,
but at least can be used when it has other values.
Reset undo history when the editor is fully loaded to avoid that users
clear textare performing an undo command.

The problem behind this is that the editor is created empty and ReactAce
does a `setValue` to set the given value for the editor. This is
registered as a change by ace, so when pressing undo in an apparently
not-changed input, the input is reset.

Fixes Graylog2/graylog-plugin-pipeline-processor#224
Copying uses our ClipboardButton component that works without external
dependencies.
Pasting is currently not possible without the user triggering a system
event for it, so we just show a small tooltip.
Hovering over the buttons should display a tooltip with the action they
perform.
Edmundo Alvarez added 8 commits January 2, 2018 13:10
We only add support for languages that make more sense to write from the
web interface.
- Do not resize editor when is not resizable
- Do not get text selection when copy button cannot be used
I also noticed an error when using the component in an uncontrolled way,
so I updated the examples and added a note on that regard.
@ghost ghost assigned edmundoa Jan 2, 2018
@edmundoa edmundoa removed their assignment Jan 2, 2018
@bernd bernd self-assigned this Jan 5, 2018
onSuccess={this.focusEditor}
text={this.state.selectedText}
buttonTitle="Copy (Ctrl+C / ⌘C)"
disabled={this.isCopyDisabled} />
Copy link
Member

Choose a reason for hiding this comment

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

This passes the isCopyDisabled function instead of executing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good finding! This got broken in one of the changes I did.

buttonTitle="Copy (Ctrl+C / ⌘C)"
disabled={this.isCopyDisabled} />
<OverlayTrigger placement="top" trigger="click" overlay={overlay} rootClose>
<Button bsStyle="link" bsSize="sm" title="Paste (Ctrl+V / &#8984;V)" disabled={this.isPasteDisabled}>
Copy link
Member

Choose a reason for hiding this comment

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

This passes the isPasteDisabled function instead of executing it.

@ghost ghost assigned edmundoa Jan 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants