Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd5976e2

Browse files
Merge pull requestRustPython#563 from coolreader18/demo-bits-and-bobs
Quality of life improvements for the demo.
2 parents9a36951 +c4513a1 commitd5976e2

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎wasm/demo/src/main.js‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ const editor = CodeMirror.fromTextArea(document.getElementById('code'), {
1212
'Cmd-Enter':runCodeFromTextarea,
1313
'Shift-Tab':'indentLess',
1414
'Ctrl-/':'toggleComment',
15-
'Cmd-/':'toggleComment'
15+
'Cmd-/':'toggleComment',
16+
Tab:editor=>{
17+
varspaces=Array(editor.getOption('indentUnit')+1).join(' ');
18+
editor.replaceSelection(spaces);
19+
}
1620
},
1721
lineNumbers:true,
1822
mode:'text/x-python',
1923
indentUnit:4,
2024
autofocus:true
2125
});
2226

23-
functionrunCodeFromTextarea(){
24-
constconsoleElement=document.getElementById('console');
25-
consterrorElement=document.getElementById('error');
27+
constconsoleElement=document.getElementById('console');
28+
consterrorElement=document.getElementById('error');
2629

30+
functionrunCodeFromTextarea(){
2731
// Clean the console and errors
2832
consoleElement.value='';
2933
errorElement.textContent='';
@@ -51,7 +55,7 @@ document
5155

5256
constsnippets=document.getElementById('snippets');
5357

54-
snippets.addEventListener('change',()=>{
58+
constupdateSnippet=()=>{
5559
constselected=snippets.value;
5660

5761
// the require here creates a webpack context; it's fine to use it
@@ -60,9 +64,11 @@ snippets.addEventListener('change', () => {
6064
constsnippet=require(`raw-loader!../snippets/${selected}.py`);
6165

6266
editor.setValue(snippet);
63-
6467
runCodeFromTextarea();
65-
});
68+
};
69+
70+
snippets.addEventListener('change',updateSnippet);
6671

67-
// Run once for demo
68-
runCodeFromTextarea();
72+
// Run once for demo (updateSnippet b/c the browser might try to keep the same
73+
// option selected for the `select`, but the textarea won't be updated)
74+
updateSnippet();

‎wasm/lib/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ wasm-bindgen = "0.2"
1818
wasm-bindgen-futures ="0.3"
1919
js-sys ="0.3"
2020
futures ="0.1"
21+
num-traits ="0.2"
2122

2223
[dependencies.web-sys]
2324
version ="0.3"

‎wasm/lib/src/browser_module.rs‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
usecrate::{convert, vm_class::AccessibleVM, wasm_builtins::window};
22
use futures::{future,Future};
33
use js_sys::Promise;
4+
use num_traits::cast::ToPrimitive;
45
use rustpython_vm::obj::{objint, objstr};
56
use rustpython_vm::pyobject::{PyContext,PyFuncArgs,PyObjectRef,PyResult,TypeProtocol};
67
use rustpython_vm::VirtualMachine;
@@ -167,11 +168,12 @@ fn browser_request_animation_frame(vm: &mut VirtualMachine, args: PyFuncArgs) ->
167168
fnbrowser_cancel_animation_frame(vm:&mutVirtualMachine,args:PyFuncArgs) ->PyResult{
168169
arg_check!(vm, args, required =[(id,Some(vm.ctx.int_type()))]);
169170

170-
// fine because
171-
let id = objint::get_value(id)
172-
.to_string()
173-
.parse()
174-
.expect("bigint.to_string() to be parsable as i32");
171+
let id = objint::get_value(id).to_i32().ok_or_else(||{
172+
vm.new_exception(
173+
vm.ctx.exceptions.value_error.clone(),
174+
"Integer too large to convert to i32 for animationFrame id".into(),
175+
)
176+
})?;
175177

176178
window()
177179
.cancel_animation_frame(id)

‎wasm/lib/src/wasm_builtins.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ pub fn print_to_html(text: &str, selector: &str) -> Result<(), JsValue> {
2626
let textarea = element
2727
.dyn_ref::<HtmlTextAreaElement>()
2828
.ok_or_else(|| js_sys::TypeError::new("Element must be a textarea"))?;
29+
2930
let value = textarea.value();
31+
32+
let scroll_height = textarea.scroll_height();
33+
let scrolled_to_bottom = scroll_height - textarea.scroll_top() == textarea.client_height();
34+
3035
textarea.set_value(&format!("{}{}", value, text));
36+
37+
if scrolled_to_bottom{
38+
textarea.scroll_with_x_and_y(0.0, scroll_height.into());
39+
}
40+
3141
Ok(())
3242
}
3343

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp