Skip to content
This repository has been archived by the owner on Jul 31, 2019. It is now read-only.

[WIP] Ability to insert code snippets into files (addresses Issue#1630) #1826

Merged

Conversation

twigz20
Copy link
Contributor

@twigz20 twigz20 commented Mar 7, 2017

This is still a work in progress.

I've currently got the core functionality in place. However, I feel as though it lacks in terms of efficiency.

863aa41c-02ac-11e7-9a8b-184b793d3d7a

As you can see in the gif above there is a slight freeze in the UI after inserting a snippet. For the time being, I'm storing all the snippets in snippets.js. On initial load up of Thimble, I've set the default file type to be HTML because there is no way to determine what file I'm on at that point in time.
Afterward, whenever the file is changed Thimble receives an activeEditorChanged event which also comes with the full file name. With access to that information, I'm then able to identify the current file type and repopulate the li elements of the snippets menu.

Any further assistance would be highly appreciated.

@flukeout
Copy link
Contributor

flukeout commented Mar 7, 2017

Hey @twigz20 - really excited to see this. I'll try out your branch and get back to you with some design guidance sometime tomorrow. Thanks for your patience!

@flukeout
Copy link
Contributor

flukeout commented Mar 9, 2017

Sorry for the delay @twigz20 - will tackle this today.

@flukeout
Copy link
Contributor

flukeout commented Mar 9, 2017

@twigz20 Would you mind rebasing this to the latest master so it merges cleanly?

@flukeout
Copy link
Contributor

flukeout commented Mar 9, 2017

Started a couple of issues related to this...

Design of the Menu - #1826
Ideas for Snippet Types - https://github.com/mozilla/thimble.mozilla.org/issues/1857

@Pomax
Copy link
Contributor

Pomax commented Mar 11, 2017

This PR looks like it needs a rebase to make sure it stays in sync with master so it can be merged when it's ready

@humphd
Copy link
Contributor

humphd commented Mar 16, 2017

@twigz20 how is this going? Looks like we've stalled a bit.

@twigz20 twigz20 force-pushed the Bug-1630-Adding-Code-Snippets-Thimble branch from eb28ded to 1a83dd0 Compare March 27, 2017 19:40
@flukeout
Copy link
Contributor

Hey @twigz20 - what's the state of this PR? Let me know how I can help - is it ready for testing?

@twigz20
Copy link
Contributor Author

twigz20 commented Mar 29, 2017

@flukeout Yes it is. Although i'd like to try using the design mockup you showed here: https://github.com/mozilla/thimble.mozilla.org/issues/1855

@flukeout
Copy link
Contributor

flukeout commented Mar 30, 2017

Awesome @twigz20 - looks like you got the 'detect file' functionality in there too! This is exciting. So how do you want to proceed? I'm happy to take over for a bit and add some styling / markup for the updated menu in #1855 or is that something you'd like to tackle?

Let me know. I think we should go with this option...

image

This allows people to copy & paste the code as well. While I like the 'flyout' preview - I think losing copy paste would be annoying.

What do you think? In the meantime, I'll provide an icon for the snippet button shortly.

@flukeout
Copy link
Contributor

Hey @twigz20 - just checking into the markup and noticed you're using tabs instead of spaces...

image

Please switch to spaces for consistency. Cheers!

@flukeout
Copy link
Contributor

flukeout commented Mar 30, 2017

OK, I made an icon - snippets-icon.zip

Here's how it looks...

Here's how to make it look like that...

<div title="Snippets" id="editor-pane-nav-snippets">
  <img src="/img/icon/snippets.svg">
</div>
#editor-pane-nav-snippets {
  background-color: #2B2B2B;
  height: 30px;
  width: 30px;
  text-align: center;
  border-radius: 50%;
  margin-left: 6px;
  position: relative;
  .icon;

  img {
    opacity: 0.5;
    height: 20px;
    width: 20px;
    position: absolute;
    top: 5px;
    left: 5px;
  }
  
  &:hover img {
    opacity: .7;
  }
}

@twigz20
Copy link
Contributor Author

twigz20 commented Mar 30, 2017

@flukeout That would be very helpful if you were to take over a bit. Will make the appropriate changes!

I'm working on trying to improve the efficiency of how I'm loading the snippets and creating the menus. I'm also trying to figure out a way to get the Bramble to send an onActiveEditorChanged event so when the project loads it can default to the appropriate snippets list.

@gideonthomas gideonthomas self-assigned this Mar 30, 2017
@Pomax Pomax requested a review from gideonthomas March 30, 2017 20:32
@twigz20
Copy link
Contributor Author

twigz20 commented Mar 31, 2017

@gideonthomas to check in with @flukeout about markup for the dropdown

@flukeout
Copy link
Contributor

flukeout commented Mar 31, 2017

@twigz20 I'm going to work on the menu on top of the current state of your branch and submit a PR against your working branch - sound good?

I'll also include the icon for the dropdown menu.

@twigz20
Copy link
Contributor Author

twigz20 commented Mar 31, 2017

@flukeout Yes, thats sounds good!

@flukeout
Copy link
Contributor

Hey @twigz20 - I made a PR against your working branch with the markup for the menu. It's got the icon and localized strings too.

Should look like this...

snippet-menu

I'll let you sort out the functionality along with your other changes. The idea is that the snippet type will be preselected in the header depending on the type of file you have. HTML will be the default.

The user should be able to toggle between the snippet types by clicking those options too.

Let me know if you have any questions!

@twigz20
Copy link
Contributor Author

twigz20 commented Mar 31, 2017

@flukeout Thanks! I'll begin testing will let you know if I anything comes up! I do have 1 question. In nav-options.html , in the div i noticed there is a data-adapt-order="4" field. Care to explain to me why is it used? Since I haven't added that to mine.

@flukeout
Copy link
Contributor

Yeah, the data-adapt-order attribute is something I created to handle cases when the editor bar is too narrow to fit all of the icons. Items that have that attribute basically start disappearing (or being modified in some way) according to their order. So adapt-data-order=1 is the first item to be removed, and also means it's the least important. It's a new feature, but you should be able to observe it action when you change the width of the editor and previews.

@flukeout
Copy link
Contributor

So anyway, if you are able to get the different snippet types in there and update your PR, then I can take another design pass.

@gideonthomas
Copy link
Contributor

@twigz20 let me know how far you get with implementing this with the approach I mentioned in class.

@twigz20
Copy link
Contributor Author

twigz20 commented Apr 4, 2017

@flukeout Alright, I've gotten a "dirty" implementation of it working with the new design markup. See below.

new thimble mark-up demo v2

@gideonthomas I've been attempting the method you suggested. However, as soon as i add in this 1 line var snippets = require("../../lib/snippets"); into editor.js thimble would no longer be connected and instead displays this to me. Note: I've placed snippets.js into the appropriate directory. I've even tried making `snippets.js' an empty file.

screenshot_1

So I'm not able to progress any further. Any ideas how I can solve this? @humphd in case you know about this as well!

@humphd
Copy link
Contributor

humphd commented Apr 5, 2017

@twigz20 what's on your dev console when you load it? That error doesn't look like a require error but rather a network error.

@twigz20
Copy link
Contributor Author

twigz20 commented Apr 5, 2017

@humphd On first refresh my dev console displays this:

screenshot_2

Then with each refresh attempt, the dev console is blank

screenshot_3

It doesn't give me any information.

@humphd
Copy link
Contributor

humphd commented Apr 5, 2017

I'd say your server isn't running. Better debug that, run npm run logs and see what's happening.

@twigz20
Copy link
Contributor Author

twigz20 commented Apr 5, 2017

@humphd After running npm run logs i've been getting this error:

screenshot_4

@humphd
Copy link
Contributor

humphd commented Apr 5, 2017

define is not defined is my favorite error message of the day! So it looks like you're doing this in node.js code, which is different from the requirejs stuff we do in the browser. Do you need to load this module in the browser and in node on the server, or just the server? Basically, you need to rewrite snippets.js to be a proper Common JS module for node.

@twigz20 twigz20 force-pushed the Bug-1630-Adding-Code-Snippets-Thimble branch from 6287102 to 08cb0bb Compare April 21, 2017 18:45
@twigz20
Copy link
Contributor Author

twigz20 commented Apr 21, 2017

@gideonthomas Done. Let me know if i missed anything.

@gideonthomas
Copy link
Contributor

@twigz20 awesome! Thanks! I'll open up a PR against your branch with a couple of changes. Thanks for getting all of this done!

@gideonthomas
Copy link
Contributor

@twigz20 opened up a PR against your branch!

Gideon Thomas and others added 3 commits April 22, 2017 14:14
Copy link
Contributor

@gideonthomas gideonthomas left a comment

Choose a reason for hiding this comment

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

@twigz20 awesome work! make these final changes and let's get @humphd to do a quick final review of this.

@@ -3,6 +3,7 @@ define(function(require) {
var $ = require("jquery");
var PopupMenu = require("fc/bramble-popupmenu");
var analytics = require("analytics");
var fileType = "HTML";
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't use this anymore so let's get rid of it.

}
}];

const CSS = [{
Copy link
Contributor

Choose a reason for hiding this comment

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

darn CSS is an object on window. @twigz20 I would advise that you make all of these lowercase throughout your code.

# Code Snippet Data
snippetHTMLComment=Comment
snippetHTMLCommentTitle=Add a Comment
snippetHTMLCommentData=<!-- Your HTML comment here -->\n
Copy link
Contributor

Choose a reason for hiding this comment

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

@TheoChevalier I would love your input here. I'm not sure if this is a good way to localize code snippets that we can insert into Thimble's editor. I wanted to localize code snippets that have text content in them. However, each string has a bunch of \n's in them as well as spaces before tag names for formatting purposes when they are rendered. I don't think I was able to come up with a way around this. Let me know what you think.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for asking, @gideonthomas :) That’s indeed tricky, and I’m not sure there’s a perfect solution. Having a loooong string with lots of \n and embedded code would work, technically, but I think what would be better here is extracting the text from the code snippets, then doing the formatting on the code side.

To be more explicit:
snippetHTMLCommentData=Your HTML comment here

In .js file, somewhere:
var myString = '<!--' + l10nLib.getString('snippetHTMLCommentData') + '-->\n';

Concatenating strings is generally a bad practice, but in this case it’s actually fine since we don’t want localizers to change the markup/code. Would that work with the rest of the snippets? It can be okay to include pieces of code when it makes string extraction easier, as long as there is a comment saying that this part should not be translated.

In the example above, we would add a comment explaining the context (concatenated with <!-- -->)

Does that make sense, or maybe I’m missing something?

Copy link
Contributor

Choose a reason for hiding this comment

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

no makes perfect sense! Thanks for the input! I think I'll go with that approach.

@twigz20
Copy link
Contributor Author

twigz20 commented Apr 28, 2017

@gideonthomas Changes Made!

@gideonthomas
Copy link
Contributor

@twigz20 awesome! Thanks, expect another PR (from me) to your branch soon which will incorporate the localization changes suggested above.

@flukeout
Copy link
Contributor

flukeout commented May 2, 2017

Just an update to keep you in the loop @twigz20: @gideonthomas has created a branch with localization changes and we're working on it together to finish it up. Expect a PR against your branch within a day or so! Thanks for your patience - we are close!

snippetJSIfElseData=if (condition1) {\n // instructions for condition 1\n} else if (condition2) {\n // instructions for condition 2\n} else {\n // fallback instructions\n}\n
snippetJSComment=Comment
snippetJSCommentTitle=Add a Comment
snippetJSCommentData=/* Your Javascript comment here */\n
Copy link
Contributor

Choose a reason for hiding this comment

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

JavaScript

Copy link
Contributor

@TheoChevalier TheoChevalier left a comment

Choose a reason for hiding this comment

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

Thanks for the fixes @gideonthomas :) Had a quick look at the l10n part, everything looks good to me 👍

@gideonthomas
Copy link
Contributor

Thanks for the review :)

@gideonthomas gideonthomas merged commit aa63cc8 into mozilla:master May 4, 2017
@gideonthomas
Copy link
Contributor

Thanks a lot for the hard work @twigz20! You've added an amazing feature to Thimble :)

@twigz20
Copy link
Contributor Author

twigz20 commented May 4, 2017

@gideonthomas Thanks! I couldn't have done it without the support of the core team!

@flukeout
Copy link
Contributor

flukeout commented May 4, 2017

Yeah awesome work @twigz20 - really happy we got this in! I changed a lot of the snippets, give them a try to see if they make sense or need some changes! 👍 💯

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
6 participants