diff --git a/wasm/demo/src/main.js b/wasm/demo/src/main.js index b35c9aaa2..7e934dfc9 100644 --- a/wasm/demo/src/main.js +++ b/wasm/demo/src/main.js @@ -36,7 +36,15 @@ function runCodeFromTextarea() { const code = editor.getValue(); try { const result = rp.pyEval(code, { - stdout: consoleElement + stdout: output => { + const shouldScroll = + consoleElement.scrollHeight - consoleElement.scrollTop === + consoleElement.clientHeight; + consoleElement.value += output; + if (shouldScroll) { + consoleElement.scrollTop = consoleElement.scrollHeight; + } + } }); if (result !== null) { consoleElement.value += `\n${result}\n`; diff --git a/wasm/lib/Cargo.toml b/wasm/lib/Cargo.toml index 3f89ba9ac..769455aea 100644 --- a/wasm/lib/Cargo.toml +++ b/wasm/lib/Cargo.toml @@ -26,7 +26,6 @@ features = [ "console", "Document", "Element", - "HtmlTextAreaElement", "Window", "Headers", "Request", diff --git a/wasm/lib/src/vm_class.rs b/wasm/lib/src/vm_class.rs index bdf3eb325..be3bbd813 100644 --- a/wasm/lib/src/vm_class.rs +++ b/wasm/lib/src/vm_class.rs @@ -273,8 +273,7 @@ impl WASMVirtualMachine { }| { fn error() -> JsValue { TypeError::new( - "Unknown stdout option, please pass a function, a textarea element, or \ - 'console'", + "Unknown stdout option, please pass a function or 'console'", ) .into() } @@ -284,13 +283,6 @@ impl WASMVirtualMachine { "console" => Box::new(wasm_builtins::builtin_print_console), _ => return Err(error()), } - } else if let Some(element) = stdout.dyn_ref::() { - let element = element.clone(); - Box::new( - move |vm: &mut VirtualMachine, args: PyFuncArgs| -> PyResult { - wasm_builtins::builtin_print_html(vm, args, &element) - }, - ) } else if stdout.is_function() { let func = js_sys::Function::from(stdout); Box::new( diff --git a/wasm/lib/src/wasm_builtins.rs b/wasm/lib/src/wasm_builtins.rs index a5867ec02..8bb6a2db2 100644 --- a/wasm/lib/src/wasm_builtins.rs +++ b/wasm/lib/src/wasm_builtins.rs @@ -4,35 +4,16 @@ //! desktop. //! Implements functions listed here: https://docs.python.org/3/library/builtins.html. -use crate::convert; use js_sys::{self, Array}; use rustpython_vm::obj::{objstr, objtype}; use rustpython_vm::pyobject::{IdProtocol, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol}; use rustpython_vm::VirtualMachine; -use wasm_bindgen::prelude::*; -use web_sys::{self, console, HtmlTextAreaElement}; +use web_sys::{self, console}; pub(crate) fn window() -> web_sys::Window { web_sys::window().expect("Window to be available") } -// The HTML id of the element element that act as our STDOUT - -pub fn print_to_html(text: &str, element: &HtmlTextAreaElement) -> Result<(), JsValue> { - let value = element.value(); - - let scroll_height = element.scroll_height(); - let scrolled_to_bottom = scroll_height - element.scroll_top() == element.client_height(); - - element.set_value(&format!("{}{}", value, text)); - - if scrolled_to_bottom { - element.scroll_with_x_and_y(0.0, scroll_height.into()); - } - - Ok(()) -} - pub fn format_print_args(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result { // Handle 'sep' kwarg: let sep_arg = args @@ -85,16 +66,6 @@ pub fn format_print_args(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result PyResult { - let output = format_print_args(vm, args)?; - print_to_html(&output, element).map_err(|err| convert::js_to_py(vm, err))?; - Ok(vm.get_none()) -} - pub fn builtin_print_console(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { let arr = Array::new(); for arg in args.args {