Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for ace editor theme preview
- Loading branch information
Showing
7 changed files
with
171 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/gwt/src/org/rstudio/core/client/widget/DynamicIFrame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.rstudio.core.client.widget; | ||
|
||
import com.google.gwt.core.client.GWT; | ||
import com.google.gwt.dom.client.Document; | ||
import com.google.gwt.resources.client.ClientBundle; | ||
import com.google.gwt.user.client.ui.Frame; | ||
import org.rstudio.core.client.dom.IFrameElementEx; | ||
import org.rstudio.core.client.dom.WindowEx; | ||
import org.rstudio.core.client.resources.StaticDataResource; | ||
|
||
public abstract class DynamicIFrame extends Frame | ||
{ | ||
interface Resources extends ClientBundle | ||
{ | ||
@Source("dynamicFrame.html") | ||
StaticDataResource dynamicFrame(); | ||
} | ||
|
||
public DynamicIFrame() | ||
{ | ||
Resources res = GWT.create(Resources.class); | ||
setUrl(res.dynamicFrame().getUrl()); | ||
attachCallback(); | ||
} | ||
|
||
protected abstract void onFrameLoaded(); | ||
|
||
protected IFrameElementEx getIFrame() | ||
{ | ||
return getElement().cast(); | ||
} | ||
|
||
protected WindowEx getWindow() | ||
{ | ||
return getIFrame().getContentWindow(); | ||
} | ||
|
||
protected final Document getDocument() | ||
{ | ||
return getWindow().getDocument(); | ||
} | ||
|
||
private native final void attachCallback() /*-{ | ||
var self = this; | ||
var el = this.@com.google.gwt.user.client.ui.UIObject::getElement()(); | ||
el.__dynamic_init__ = $entry(function() { | ||
self.@org.rstudio.core.client.widget.DynamicIFrame::onFrameLoaded()(); | ||
}); | ||
}-*/; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/gwt/src/org/rstudio/core/client/widget/dynamicFrame.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script type="text/javascript"> | ||
window.onload = function() { | ||
if (window.frameElement && window.frameElement.__dynamic_init__) | ||
window.frameElement.__dynamic_init__(); | ||
}; | ||
</script> | ||
</head> | ||
<body></body> | ||
</html> |
82 changes: 82 additions & 0 deletions
82
src/gwt/src/org/rstudio/studio/client/workbench/prefs/views/AceEditorPreview.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.rstudio.studio.client.workbench.prefs.views; | ||
|
||
import com.google.gwt.dom.client.*; | ||
import com.google.gwt.dom.client.Style.BorderStyle; | ||
import com.google.gwt.dom.client.Style.Unit; | ||
import org.rstudio.core.client.ExternalJavaScriptLoader; | ||
import org.rstudio.core.client.ExternalJavaScriptLoader.Callback; | ||
import org.rstudio.core.client.widget.DynamicIFrame; | ||
import org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceResources; | ||
|
||
public class AceEditorPreview extends DynamicIFrame | ||
{ | ||
public AceEditorPreview() | ||
{ | ||
Style style = getStyleElement().getStyle(); | ||
style.setBorderColor("#CCC"); | ||
style.setBorderWidth(1, Unit.PX); | ||
style.setBorderStyle(BorderStyle.SOLID); | ||
style.setWidth(85, Unit.PCT); | ||
|
||
} | ||
|
||
@Override | ||
protected void onFrameLoaded() | ||
{ | ||
isFrameLoaded_ = true; | ||
if (initialThemeUrl_ != null) | ||
setTheme(initialThemeUrl_); | ||
|
||
final Document doc = getDocument(); | ||
final BodyElement body = doc.getBody(); | ||
body.getStyle().setMargin(0, Unit.PX); | ||
|
||
StyleElement style = doc.createStyleElement(); | ||
style.setType("text/css"); | ||
style.setInnerText(".ace_editor {border: none !important;}"); | ||
body.appendChild(style); | ||
|
||
DivElement div = doc.createDivElement(); | ||
div.setId("editor"); | ||
div.getStyle().setWidth(100, Unit.PCT); | ||
div.getStyle().setHeight(100, Unit.PCT); | ||
div.setInnerText("hello"); | ||
body.appendChild(div); | ||
|
||
ExternalJavaScriptLoader loader = new ExternalJavaScriptLoader( | ||
doc, AceResources.INSTANCE.acejs().getUrl()); | ||
loader.addCallback(new Callback() | ||
{ | ||
public void onLoaded() | ||
{ | ||
body.appendChild(doc.createScriptElement( | ||
"var editor = ace.edit('editor');\n" + | ||
"editor.renderer.setHScrollBarAlwaysVisible(false);\n" + | ||
"editor.setHighlightActiveLine(false);")); | ||
} | ||
}); | ||
} | ||
|
||
public void setTheme(String themeUrl) | ||
{ | ||
if (!isFrameLoaded_) | ||
{ | ||
initialThemeUrl_ = themeUrl; | ||
return; | ||
} | ||
|
||
if (currentStyleLink_ != null) | ||
currentStyleLink_.removeFromParent(); | ||
|
||
Document doc = getDocument(); | ||
currentStyleLink_ = doc.createLinkElement(); | ||
currentStyleLink_.setRel("stylesheet"); | ||
currentStyleLink_.setType("text/css"); | ||
currentStyleLink_.setHref(themeUrl); | ||
doc.getBody().appendChild(currentStyleLink_); | ||
} | ||
|
||
private LinkElement currentStyleLink_; | ||
private boolean isFrameLoaded_; | ||
private String initialThemeUrl_; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters