Skip to content

Commit 6ff5545

Browse files
authored
Merge pull request #1669 from wilzbach/open-in-editor
Add 'Open in editor' button for runnable code examples merged-on-behalf-of: Vladimir Panteleev <github@thecybershadow.net>
2 parents d47df2c + 40af0a6 commit 6ff5545

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

css/style.css

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,7 @@ div.d_code_output, div.d_code_stdin, div.d_code_args, div.d_code_unittest
16861686
.CodeMirror {line-height: normal; border: 1px solid #CCC; background: #FCFCFC; height: auto}
16871687
.cm-s-eclipse .CodeMirror-matchingbracket {border:1px solid grey;color:black !important;}
16881688

1689-
input.runButton, input.resetButton, input.argsButton, input.inputButton, input.editButton
1689+
input.runButton, input.resetButton, input.argsButton, input.inputButton, input.editButton, input.openInEditorButton
16901690
{
16911691
-moz-box-shadow:inset 0 1px 0 0 #ffffff;
16921692
-webkit-box-shadow:inset 0 1px 0 0 #ffffff;
@@ -1727,6 +1727,29 @@ input.editButton:active
17271727
}
17281728
input.resetButton{display: none}
17291729

1730+
div.editButton, div.openInEditorButton, div.runButton, div.resetButton {
1731+
display: inline-block;
1732+
margin-right: 10px;
1733+
cursor: pointer;
1734+
color: #333;
1735+
background: white;
1736+
border: 1px solid #CCC;
1737+
border-radius: 4px;
1738+
padding: 0.3em 0.6em;
1739+
display: inline-block;
1740+
text-decoration: none;
1741+
}
1742+
div.openInEditorButton {
1743+
margin-right: 0;
1744+
float: right;
1745+
}
1746+
.unittest_examples .d_example_buttons {
1747+
margin-top: 10px;
1748+
}
1749+
.unittest_examples .CodeMirror, .unittest_examples .d_code_output {
1750+
margin-top: 10px;
1751+
}
1752+
17301753
/* Style for the example run buttons on the Phobos library documentation */
17311754
.d_example_buttons {
17321755
text-align: left;

js/run.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ function setupTextarea(el, opts)
229229
return (parseInt(par.css('height')) - diff) + 'px';
230230
};
231231

232-
var runBtn = parent.children("input.runButton");
233-
var editBtn = parent.children("input.editButton");
234-
var resetBtn = parent.children("input.resetButton");
232+
var runBtn = parent.children(".runButton");
233+
var editBtn = parent.children(".editButton");
234+
var resetBtn = parent.children(".resetButton");
235+
var openInEditorBtn = parent.children(".openInEditorButton");
235236

236237
var code = $(editor.getWrapperElement());
237238
code.css('display', 'none');
@@ -358,6 +359,10 @@ function setupTextarea(el, opts)
358359
}
359360
});
360361
});
362+
openInEditorBtn.click(function(){
363+
var url = "https://tour.dlang.org/editor?source=" + encodeURIComponent(opts.transformOutput(editor.getValue()));
364+
window.open(url, "_blank");
365+
});
361366
return editor;
362367
};
363368

js/run_examples.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ function wrapIntoMain(code) {
2525
codeOut += code;
2626
}
2727
else {
28-
var codeOut = "void main(){ ";
29-
codeOut += "import " + currentPackage + "; ";
28+
var codeOut = "void main()\n{\n";
29+
codeOut += " import " + currentPackage + ";\n";
3030
// writing to the stdout is probably often used
31-
codeOut += "import std.stdio: write, writeln, writef, writefln; ";
32-
codeOut += code;
31+
codeOut += " import std.stdio: write, writeln, writef, writefln;\n ";
32+
codeOut += code.split("\n").join("\n ");
3333
codeOut += "\n}";
3434
}
3535
return codeOut;
@@ -70,18 +70,19 @@ $(document).ready(function()
7070
return;
7171

7272
currentExample.replaceWith(
73-
'<div>'
74-
+ '<div class="d_example_buttons">'
75-
+ '<input type="button" class="editButton" value="Edit">'
76-
+ '<input type="button" class="runButton" value="Run">'
77-
+ '<input type="button" class="resetButton" value="Reset">'
78-
+ '</div>'
73+
'<div class="unittest_examples">'
7974
+ '<div class="d_code">'
8075
+ '<pre class="d_code">'+orig+'</pre>'
8176
+ '</div>'
8277
+ '<div class="d_run_code" style="display: block">'
8378
+ '<textarea class="d_code" style="display: none;"></textarea>'
8479
+ '</div>'
80+
+ '<div class="d_example_buttons">'
81+
+ '<div class="editButton"><i class="fa fa-edit" aria-hidden="true"></i> Edit</div>'
82+
+ '<div class="runButton"><i class="fa fa-play" aria-hidden="true"></i> Run</div>'
83+
+ '<div class="resetButton"><i class="fa fa-undo " aria-hidden="true"></i> Reset</div>'
84+
+ '<div class="openInEditorButton" title="Open in an external editor"><i class="fa fa-external-link" aria-hidden="true"></i></div>'
85+
+ '</div>'
8586
+ '<div class="d_code_output"><span class="d_code_title">Application output</span><br><pre class="d_code_output" readonly>Running...</pre>'
8687
+ '</div>'
8788
);

0 commit comments

Comments
 (0)