mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Remove builtin_print_html, move functionality to JS
This commit is contained in:
@@ -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`;
|
||||
|
||||
@@ -26,7 +26,6 @@ features = [
|
||||
"console",
|
||||
"Document",
|
||||
"Element",
|
||||
"HtmlTextAreaElement",
|
||||
"Window",
|
||||
"Headers",
|
||||
"Request",
|
||||
|
||||
@@ -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::<web_sys::HtmlTextAreaElement>() {
|
||||
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(
|
||||
|
||||
@@ -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<String, PyObjectRef> {
|
||||
// Handle 'sep' kwarg:
|
||||
let sep_arg = args
|
||||
@@ -85,16 +66,6 @@ pub fn format_print_args(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<St
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
pub fn builtin_print_html(
|
||||
vm: &mut VirtualMachine,
|
||||
args: PyFuncArgs,
|
||||
element: &HtmlTextAreaElement,
|
||||
) -> 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 {
|
||||
|
||||
Reference in New Issue
Block a user